cmd/syncthing: Use os.executable in monitor, only fallback to PATH (#8502)

This commit is contained in:
Simon Frei
2022-08-17 08:57:33 +02:00
committed by GitHub
parent b10d106a55
commit 6dedffe3f7
+14 -31
View File
@@ -83,14 +83,10 @@ func monitorMain(options serveOptions) {
} }
args := os.Args args := os.Args
binary := args[0] binary, err := getBinary(args[0])
if build.IsWindows { if err != nil {
var err error l.Warnln("Error starting the main Syncthing process:", err)
binary, err = expandExecutableInCurrentDirectory(binary) panic("Error starting the main Syncthing process")
if err != nil {
l.Warnln("Error starting the main Syncthing process:", err)
panic("Error starting the main Syncthing process")
}
} }
var restarts [restartCounts]time.Time var restarts [restartCounts]time.Time
@@ -212,19 +208,17 @@ func monitorMain(options serveOptions) {
} }
} }
func expandExecutableInCurrentDirectory(args0 string) (string, error) { func getBinary(args0 string) (string, error) {
// Works around a restriction added in go1.19 that executables in the e, err := os.Executable()
// current directory are not resolved when specifying just an executable if err == nil {
// name (like e.g. "syncthing") return e, nil
if !strings.ContainsRune(args0, os.PathSeparator) {
// Check if it's in PATH
_, err := exec.LookPath(args0)
if err != nil {
// Try to get the path to the current executable
return os.Executable()
}
} }
return args0, nil // Check if args0 cuts it
e, lerr := exec.LookPath(args0)
if lerr == nil {
return e, nil
}
return "", err
} }
func copyStderr(stderr io.Reader, dst io.Writer) { func copyStderr(stderr io.Reader, dst io.Writer) {
@@ -352,17 +346,6 @@ func restartMonitor(binary string, args []string) error {
} }
func restartMonitorUnix(binary string, args []string) error { func restartMonitorUnix(binary string, args []string) error {
if !strings.ContainsRune(binary, os.PathSeparator) {
// The path to the binary doesn't contain a slash, so it should be
// found in $PATH.
var err error
binary, err = exec.LookPath(binary)
if err != nil {
return err
}
args[0] = binary
}
return syscall.Exec(args[0], args, os.Environ()) return syscall.Exec(args[0], args, os.Environ())
} }