|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "os/exec" |
| 10 | + "os/signal" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "syscall" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +var ( |
| 18 | + versionString = "undefined" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + var preStartCmd string |
| 23 | + var mainCmd string |
| 24 | + var postStopCmd string |
| 25 | + var version bool |
| 26 | + |
| 27 | + flag.StringVar(&preStartCmd, "pre", "", "Pre-start command") |
| 28 | + flag.StringVar(&mainCmd, "main", "", "Main command") |
| 29 | + flag.StringVar(&postStopCmd, "post", "", "Post-stop command") |
| 30 | + flag.BoolVar(&version, "version", false, "Display go-init version") |
| 31 | + flag.Parse() |
| 32 | + |
| 33 | + if version { |
| 34 | + fmt.Println(versionString) |
| 35 | + os.Exit(0) |
| 36 | + } |
| 37 | + |
| 38 | + if mainCmd == "" { |
| 39 | + log.Fatal("[go-init] No main command defined, exiting") |
| 40 | + } |
| 41 | + |
| 42 | + // Routine to reap zombies (it's the job of init) |
| 43 | + ctx, cancel := context.WithCancel(context.Background()) |
| 44 | + var wg sync.WaitGroup |
| 45 | + wg.Add(1) |
| 46 | + go removeZombies(ctx, &wg) |
| 47 | + |
| 48 | + // Launch pre-start command |
| 49 | + if preStartCmd == "" { |
| 50 | + log.Println("[go-init] No pre-start command defined, skip") |
| 51 | + } else { |
| 52 | + log.Printf("[go-init] Pre-start command launched : %s\n", preStartCmd) |
| 53 | + err := run(preStartCmd) |
| 54 | + if err != nil { |
| 55 | + log.Println("[go-init] Pre-start command failed") |
| 56 | + log.Printf("[go-init] %s\n", err) |
| 57 | + cleanQuit(cancel, &wg, 1) |
| 58 | + } else { |
| 59 | + log.Printf("[go-init] Pre-start command exited") |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Launch main command |
| 64 | + var mainRC int |
| 65 | + log.Printf("[go-init] Main command launched : %s\n", mainCmd) |
| 66 | + err := run(mainCmd) |
| 67 | + if err != nil { |
| 68 | + log.Println("[go-init] Main command failed") |
| 69 | + log.Printf("[go-init] %s\n", err) |
| 70 | + mainRC = 1 |
| 71 | + } else { |
| 72 | + log.Printf("[go-init] Main command exited") |
| 73 | + } |
| 74 | + |
| 75 | + // Launch post-stop command |
| 76 | + if postStopCmd == "" { |
| 77 | + log.Println("[go-init] No post-stop command defined, skip") |
| 78 | + } else { |
| 79 | + log.Printf("[go-init] Post-stop command launched : %s\n", postStopCmd) |
| 80 | + err := run(postStopCmd) |
| 81 | + if err != nil { |
| 82 | + log.Println("[go-init] Post-stop command failed") |
| 83 | + log.Printf("[go-init] %s\n", err) |
| 84 | + cleanQuit(cancel, &wg, 1) |
| 85 | + } else { |
| 86 | + log.Printf("[go-init] Post-stop command exited") |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Wait removeZombies goroutine |
| 91 | + cleanQuit(cancel, &wg, mainRC) |
| 92 | +} |
| 93 | + |
| 94 | +func removeZombies(ctx context.Context, wg *sync.WaitGroup) { |
| 95 | + for { |
| 96 | + var status syscall.WaitStatus |
| 97 | + |
| 98 | + // Wait for orphaned zombie process |
| 99 | + pid, _ := syscall.Wait4(-1, &status, syscall.WNOHANG, nil) |
| 100 | + |
| 101 | + if pid <= 0 { |
| 102 | + // PID is 0 or -1 if no child waiting |
| 103 | + // so we wait for 1 second for next check |
| 104 | + time.Sleep(1 * time.Second) |
| 105 | + } else { |
| 106 | + // PID is > 0 if a child was reaped |
| 107 | + // we immediately check if another one |
| 108 | + // is waiting |
| 109 | + continue |
| 110 | + } |
| 111 | + |
| 112 | + // Non-blocking test |
| 113 | + // if context is done |
| 114 | + select { |
| 115 | + case <-ctx.Done(): |
| 116 | + // Context is done |
| 117 | + // so we stop goroutine |
| 118 | + wg.Done() |
| 119 | + return |
| 120 | + default: |
| 121 | + } |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func run(command string) error { |
| 126 | + |
| 127 | + var commandStr string |
| 128 | + var argsSlice []string |
| 129 | + |
| 130 | + // Split cmd and args |
| 131 | + commandSlice := strings.Fields(command) |
| 132 | + commandStr = commandSlice[0] |
| 133 | + // if there is args |
| 134 | + if len(commandSlice) > 1 { |
| 135 | + argsSlice = commandSlice[1:] |
| 136 | + } |
| 137 | + |
| 138 | + // Register chan to receive system signals |
| 139 | + sigs := make(chan os.Signal, 1) |
| 140 | + defer close(sigs) |
| 141 | + signal.Notify(sigs) |
| 142 | + defer signal.Reset() |
| 143 | + |
| 144 | + // Define command and rebind |
| 145 | + // stdout and stdin |
| 146 | + cmd := exec.Command(commandStr, argsSlice...) |
| 147 | + cmd.Stdout = os.Stdout |
| 148 | + cmd.Stderr = os.Stderr |
| 149 | + // Create a dedicated pidgroup |
| 150 | + // used to forward signals to |
| 151 | + // main process and all children |
| 152 | + cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} |
| 153 | + |
| 154 | + // Goroutine for signals forwarding |
| 155 | + go func() { |
| 156 | + for sig := range sigs { |
| 157 | + // Ignore SIGCHLD signals since |
| 158 | + // thez are only usefull for go-init |
| 159 | + if sig != syscall.SIGCHLD { |
| 160 | + // Forward signal to main process and all children |
| 161 | + syscall.Kill(-cmd.Process.Pid, sig.(syscall.Signal)) |
| 162 | + } |
| 163 | + } |
| 164 | + }() |
| 165 | + |
| 166 | + // Start defined command |
| 167 | + err := cmd.Start() |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + // Wait for command to exit |
| 173 | + err = cmd.Wait() |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + |
| 178 | + return nil |
| 179 | +} |
| 180 | + |
| 181 | +func cleanQuit(cancel context.CancelFunc, wg *sync.WaitGroup, code int) { |
| 182 | + // Signal zombie goroutine to stop |
| 183 | + // and wait for it to release waitgroup |
| 184 | + cancel() |
| 185 | + wg.Wait() |
| 186 | + |
| 187 | + os.Exit(code) |
| 188 | +} |
0 commit comments