diff options
author | Zillode <zillode@zillode.be> | 2015-08-19 23:16:32 +0200 |
---|---|---|
committer | Zillode <zillode@zillode.be> | 2015-08-19 23:16:32 +0200 |
commit | e2be051558787521e442ead05e62610b7a7c8bb3 (patch) | |
tree | 3244ca2742ac58d92ec6e9b9ed480a39eb1073a1 | |
parent | 50702eda94d07bb3544c66aa1a0106bd3a2a7f33 (diff) | |
parent | dbb388719e6fbc9fe778f62888ea2f4b118f6fa9 (diff) |
Merge pull request #2169 from calmh/restartmon
Retain standard streams over restart (fixes #2155)
-rw-r--r-- | cmd/syncthing/monitor.go | 41 |
1 files changed, 38 insertions, 3 deletions
diff --git a/cmd/syncthing/monitor.go b/cmd/syncthing/monitor.go index 680f718ebe..a5ff1c30ce 100644 --- a/cmd/syncthing/monitor.go +++ b/cmd/syncthing/monitor.go @@ -146,9 +146,8 @@ func monitorMain() { // binary as part of the upgrade process. l.Infoln("Restarting monitor...") os.Setenv("STNORESTART", "") - err := exec.Command(args[0], args[1:]...).Start() - if err != nil { - l.Warnln("restart:", err) + if err = restartMonitor(args); err != nil { + l.Warnln("Restart:", err) } return } @@ -227,3 +226,39 @@ func copyStdout(stdout io.Reader, dst io.Writer) { dst.Write([]byte(line)) } } + +func restartMonitor(args []string) error { + if runtime.GOOS != "windows" { + // syscall.Exec is the cleanest way to restart on Unixes as it + // replaces the current process with the new one, keeping the pid and + // controlling terminal and so on + return restartMonitorUnix(args) + } + + // but it isn't supported on Windows, so there we start a normal + // exec.Command and return. + return restartMonitorWindows(args) +} + +func restartMonitorUnix(args []string) error { + if !strings.ContainsRune(args[0], os.PathSeparator) { + // The path to the binary doesn't contain a slash, so it should be + // found in $PATH. + binary, err := exec.LookPath(args[0]) + if err != nil { + return err + } + args[0] = binary + } + + return syscall.Exec(args[0], args, os.Environ()) +} + +func restartMonitorWindows(args []string) error { + cmd := exec.Command(args[0], args[1:]...) + // Retain the standard streams + cmd.Stderr = os.Stderr + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + return cmd.Start() +} |