|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "os/signal" |
| 6 | + "syscall" |
| 7 | + |
| 8 | + "github.com/sirupsen/logrus" |
| 9 | +) |
| 10 | + |
| 11 | +func forkExec() { |
| 12 | + |
| 13 | + // run supercronic in other process |
| 14 | + pwd, err := os.Getwd() |
| 15 | + if err != nil { |
| 16 | + logrus.Fatalf("Failed to get current working directory: %s", err.Error()) |
| 17 | + return |
| 18 | + } |
| 19 | + |
| 20 | + pattrs := &syscall.ProcAttr{ |
| 21 | + Dir: pwd, |
| 22 | + Env: os.Environ(), |
| 23 | + Files: []uintptr{ |
| 24 | + uintptr(syscall.Stdin), |
| 25 | + uintptr(syscall.Stdout), |
| 26 | + uintptr(syscall.Stderr), |
| 27 | + }, |
| 28 | + } |
| 29 | + args := make([]string, 0, len(os.Args)+1) |
| 30 | + // disable reaping for supercronic, avoid no sense warning |
| 31 | + args = append(args, os.Args[0], "-no-reap") |
| 32 | + args = append(args, os.Args[1:]...) |
| 33 | + |
| 34 | + pid, err := syscall.ForkExec(args[0], args, pattrs) |
| 35 | + if err != nil { |
| 36 | + logrus.Fatalf("Failed to fork exec: %s", err.Error()) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + // forward signal to supercronic |
| 41 | + signalToFork(pid) |
| 42 | + // got supercronic exit status |
| 43 | + wstatus := reapChildren(pid) |
| 44 | + os.Exit(wstatus.ExitStatus()) |
| 45 | +} |
| 46 | + |
| 47 | +func signalToFork(pid int) { |
| 48 | + p, err := os.FindProcess(pid) |
| 49 | + if err != nil { |
| 50 | + logrus.Fatalf("Failed findProcess supercronic pid:%d,%s", pid, err.Error()) |
| 51 | + } |
| 52 | + termChan := make(chan os.Signal, 1) |
| 53 | + signal.Notify(termChan, signalList...) |
| 54 | + go func() { |
| 55 | + for { |
| 56 | + s := <-termChan |
| 57 | + if err := p.Signal(s); err != nil { |
| 58 | + logrus.Errorf("Failed to send signal to supercronic: %s", err.Error()) |
| 59 | + } |
| 60 | + } |
| 61 | + }() |
| 62 | +} |
| 63 | + |
| 64 | +// copy from https://github.com/ramr/go-reaper |
| 65 | +// modify for wait exit status of supercronic |
| 66 | +// without modify, supercronic exit status may not be obtained |
| 67 | + |
| 68 | +// Be a good parent - clean up behind the children. |
| 69 | +func reapChildren(superCrondPid int) syscall.WaitStatus { |
| 70 | + var notifications = make(chan os.Signal, 1) |
| 71 | + |
| 72 | + go sigChildHandler(notifications) |
| 73 | + |
| 74 | + // all child |
| 75 | + const rpid = -1 |
| 76 | + var wstatus syscall.WaitStatus |
| 77 | + |
| 78 | + for { |
| 79 | + var sig = <-notifications |
| 80 | + logrus.Debugf("reaper received signal %v\n", sig) |
| 81 | + for { |
| 82 | + pid, err := syscall.Wait4(rpid, &wstatus, 0, nil) |
| 83 | + for syscall.EINTR == err { |
| 84 | + pid, err = syscall.Wait4(pid, &wstatus, 0, nil) |
| 85 | + } |
| 86 | + |
| 87 | + if syscall.ECHILD == err { |
| 88 | + break |
| 89 | + } |
| 90 | + |
| 91 | + if superCrondPid == pid { |
| 92 | + logrus.Debugf("supercronic exit, pid=%d, wstatus=%+v, err=%+v\n", pid, wstatus, err) |
| 93 | + return wstatus |
| 94 | + } |
| 95 | + // note: change output need change test |
| 96 | + logrus.Warnf("reaper cleanup: pid=%d, wstatus=%+v\n", |
| 97 | + pid, wstatus) |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | +} |
| 102 | + |
| 103 | +// Handle death of child (SIGCHLD) messages. Pushes the signal onto the |
| 104 | +// notifications channel if there is a waiter. |
| 105 | +func sigChildHandler(notifications chan os.Signal) { |
| 106 | + var sigs = make(chan os.Signal, 3) |
| 107 | + signal.Notify(sigs, syscall.SIGCHLD) |
| 108 | + |
| 109 | + for { |
| 110 | + var sig = <-sigs |
| 111 | + select { |
| 112 | + case notifications <- sig: /* published it. */ |
| 113 | + default: |
| 114 | + /* |
| 115 | + * Notifications channel full - drop it to the |
| 116 | + * floor. This ensures we don't fill up the SIGCHLD |
| 117 | + * queue. The reaper just waits for any child |
| 118 | + * process (pid=-1), so we ain't loosing it!! ;^) |
| 119 | + */ |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | +} /* End of function sigChildHandler. */ |
0 commit comments