]> Cypherpunks.ru repositories - nncp.git/blobdiff - src/toss.go
Merge branch 'develop'
[nncp.git] / src / toss.go
index 67a7311569b41fc7f8f827dba140dddffb127821..75e01c4d93633bb27572dca3b0d6e088814f6d31 100644 (file)
@@ -1,6 +1,6 @@
 /*
 NNCP -- Node to Node copy, utilities for store-and-forward data exchange
-Copyright (C) 2016-2019 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2016-2021 Sergey Matveev <stargrave@stargrave.org>
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -33,6 +33,7 @@ import (
        "path/filepath"
        "strconv"
        "strings"
+       "time"
 
        xdr "github.com/davecgh/go-xdr/xdr2"
        "github.com/dustin/go-humanize"
@@ -68,6 +69,12 @@ func (ctx *Ctx) Toss(
        nice uint8,
        dryRun, doSeen, noFile, noFreq, noExec, noTrns bool,
 ) bool {
+       dirLock, err := ctx.LockDir(nodeId, "toss")
+       if err != nil {
+               ctx.LogE("rx", SDS{}, err, "lock")
+               return false
+       }
+       defer ctx.UnlockDir(dirLock)
        isBad := false
        sendmail := ctx.Neigh[*ctx.SelfId].Exec["sendmail"]
        decompressor, err := zstd.NewReader(nil)
@@ -83,8 +90,7 @@ func (ctx *Ctx) Toss(
                        continue
                }
                pipeR, pipeW := io.Pipe()
-               errs := make(chan error, 1)
-               go func(job Job) {
+               go func(job Job) error {
                        pipeWB := bufio.NewWriter(pipeW)
                        _, _, err := PktEncRead(
                                ctx.Self,
@@ -92,13 +98,14 @@ func (ctx *Ctx) Toss(
                                bufio.NewReader(job.Fd),
                                pipeWB,
                        )
-                       errs <- err
-                       pipeWB.Flush()
-                       pipeW.Close()
-                       job.Fd.Close()
+                       job.Fd.Close() // #nosec G104
                        if err != nil {
-                               ctx.LogE("rx", sds, err, "decryption")
+                               return pipeW.CloseWithError(err)
+                       }
+                       if err = pipeWB.Flush(); err != nil {
+                               return pipeW.CloseWithError(err)
                        }
+                       return pipeW.Close()
                }(job)
                var pkt Pkt
                var err error
@@ -118,7 +125,7 @@ func (ctx *Ctx) Toss(
                sds["size"] = pktSize
                ctx.LogD("rx", sds, "taken")
                switch pkt.Type {
-               case PktTypeExec:
+               case PktTypeExec, PktTypeExecFat:
                        if noExec {
                                goto Closing
                        }
@@ -140,13 +147,15 @@ func (ctx *Ctx) Toss(
                                isBad = true
                                goto Closing
                        }
-                       if err = decompressor.Reset(pipeR); err != nil {
-                               log.Fatalln(err)
+                       if pkt.Type == PktTypeExec {
+                               if err = decompressor.Reset(pipeR); err != nil {
+                                       log.Fatalln(err)
+                               }
                        }
                        if !dryRun {
                                cmd := exec.Command(
                                        cmdline[0],
-                                       append(cmdline[1:len(cmdline)], args...)...,
+                                       append(cmdline[1:], args...)...,
                                )
                                cmd.Env = append(
                                        cmd.Env,
@@ -154,7 +163,11 @@ func (ctx *Ctx) Toss(
                                        "NNCP_SENDER="+sender.Id.String(),
                                        "NNCP_NICE="+strconv.Itoa(int(pkt.Nice)),
                                )
-                               cmd.Stdin = decompressor
+                               if pkt.Type == PktTypeExec {
+                                       cmd.Stdin = decompressor
+                               } else {
+                                       cmd.Stdin = pipeR
+                               }
                                output, err := cmd.Output()
                                if err != nil {
                                        ctx.LogE("rx", sds, err, "handle")
@@ -169,12 +182,14 @@ func (ctx *Ctx) Toss(
                                        if exists {
                                                cmd := exec.Command(
                                                        sendmail[0],
-                                                       append(sendmail[1:len(sendmail)], notify.To)...,
+                                                       append(sendmail[1:], notify.To)...,
                                                )
                                                cmd.Stdin = newNotification(notify, fmt.Sprintf(
                                                        "Exec from %s: %s", sender.Name, argsStr,
                                                ), output)
-                                               cmd.Run()
+                                               if err = cmd.Run(); err != nil {
+                                                       ctx.LogE("rx", sds, err, "notify")
+                                               }
                                        }
                                }
                        }
@@ -182,7 +197,7 @@ func (ctx *Ctx) Toss(
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                               fd.Close() // #nosec G104
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
@@ -233,18 +248,22 @@ func (ctx *Ctx) Toss(
                                        goto Closing
                                }
                                if err = bufW.Flush(); err != nil {
-                                       tmp.Close()
+                                       tmp.Close() // #nosec G104
                                        ctx.LogE("rx", sds, err, "copy")
                                        isBad = true
                                        goto Closing
                                }
                                if err = tmp.Sync(); err != nil {
-                                       tmp.Close()
+                                       tmp.Close() // #nosec G104
+                                       ctx.LogE("rx", sds, err, "copy")
+                                       isBad = true
+                                       goto Closing
+                               }
+                               if err = tmp.Close(); err != nil {
                                        ctx.LogE("rx", sds, err, "copy")
                                        isBad = true
                                        goto Closing
                                }
-                               tmp.Close()
                                dstPathOrig := filepath.Join(*incoming, dst)
                                dstPath := dstPathOrig
                                dstPathCtr := 0
@@ -274,7 +293,7 @@ func (ctx *Ctx) Toss(
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                               fd.Close() // #nosec G104
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
@@ -284,7 +303,7 @@ func (ctx *Ctx) Toss(
                                if len(sendmail) > 0 && ctx.NotifyFile != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
-                                               append(sendmail[1:len(sendmail)], ctx.NotifyFile.To)...,
+                                               append(sendmail[1:], ctx.NotifyFile.To)...,
                                        )
                                        cmd.Stdin = newNotification(ctx.NotifyFile, fmt.Sprintf(
                                                "File from %s: %s (%s)",
@@ -292,7 +311,9 @@ func (ctx *Ctx) Toss(
                                                dst,
                                                humanize.IBytes(uint64(pktSize)),
                                        ), nil)
-                                       cmd.Run()
+                                       if err = cmd.Run(); err != nil {
+                                               ctx.LogE("rx", sds, err, "notify")
+                                       }
                                }
                        }
                case PktTypeFreq:
@@ -341,7 +362,7 @@ func (ctx *Ctx) Toss(
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                               fd.Close() // #nosec G104
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
@@ -351,12 +372,14 @@ func (ctx *Ctx) Toss(
                                if len(sendmail) > 0 && ctx.NotifyFreq != nil {
                                        cmd := exec.Command(
                                                sendmail[0],
-                                               append(sendmail[1:len(sendmail)], ctx.NotifyFreq.To)...,
+                                               append(sendmail[1:], ctx.NotifyFreq.To)...,
                                        )
                                        cmd.Stdin = newNotification(ctx.NotifyFreq, fmt.Sprintf(
                                                "Freq from %s: %s", sender.Name, src,
                                        ), nil)
-                                       cmd.Run()
+                                       if err = cmd.Run(); err != nil {
+                                               ctx.LogE("rx", sds, err, "notify")
+                                       }
                                }
                        }
                case PktTypeTrns:
@@ -385,7 +408,7 @@ func (ctx *Ctx) Toss(
                        if !dryRun {
                                if doSeen {
                                        if fd, err := os.Create(job.Fd.Name() + SeenSuffix); err == nil {
-                                               fd.Close()
+                                               fd.Close() // #nosec G104
                                        }
                                }
                                if err = os.Remove(job.Fd.Name()); err != nil {
@@ -398,7 +421,30 @@ func (ctx *Ctx) Toss(
                        isBad = true
                }
        Closing:
-               pipeR.Close()
+               pipeR.Close() // #nosec G104
        }
        return isBad
 }
+
+func (ctx *Ctx) AutoToss(
+       nodeId *NodeId,
+       nice uint8,
+       doSeen, noFile, noFreq, noExec, noTrns bool,
+) (chan struct{}, chan bool) {
+       finish := make(chan struct{})
+       badCode := make(chan bool)
+       go func() {
+               bad := false
+               for {
+                       select {
+                       case <-finish:
+                               badCode <- bad
+                               break
+                       default:
+                       }
+                       time.Sleep(time.Second)
+                       bad = !ctx.Toss(nodeId, nice, false, doSeen, noFile, noFreq, noExec, noTrns)
+               }
+       }()
+       return finish, badCode
+}