| 1 | // Package reexec facilitates the busybox style reexec of a binary. |
| 2 | // |
| 3 | // Handlers can be registered with a name and the argv 0 of the exec of |
| 4 | // the binary will be used to find and execute custom init paths. |
| 5 | // |
| 6 | // It is used to work around forking limitations when using Go. |
| 7 | package reexec |
| 8 | |
| 9 | import ( |
| 10 | "context" |
| 11 | "fmt" |
| 12 | "os" |
| 13 | "os/exec" |
| 14 | "path/filepath" |
| 15 | "runtime" |
| 16 | ) |
| 17 | |
| 18 | var registeredInitializers = make(map[string]func()) |
| 19 | |
| 20 | // Register adds an initialization func under the specified name. It panics |
| 21 | // if the given name is already registered. |
| 22 | func Register(name string, initializer func()) { |
| 23 | if filepath.Base(name) != name { |
| 24 | panic(fmt.Sprintf("reexec func does not expect a path component: %q", name)) |
| 25 | } |
| 26 | if _, exists := registeredInitializers[name]; exists { |
| 27 | panic(fmt.Sprintf("reexec func already registered under name %q", name)) |
| 28 | } |
| 29 | |
| 30 | registeredInitializers[name] = initializer |
| 31 | } |
| 32 | |
| 33 | // Init is called as the first part of the exec process and returns true if an |
| 34 | // initialization function was called. |
| 35 | func Init() bool { |
| 36 | if initializer, ok := registeredInitializers[filepath.Base(os.Args[0])]; ok { |
| 37 | initializer() |
| 38 | return true |
| 39 | } |
| 40 | return false |
| 41 | } |
| 42 | |
| 43 | // Command returns an [*exec.Cmd] with its Path set to the path of the current |
| 44 | // binary using the result of [Self]. |
| 45 | // |
| 46 | // On Linux, the Pdeathsig of [*exec.Cmd.SysProcAttr] is set to SIGTERM. |
| 47 | // This signal is sent to the process when the OS thread that created |
| 48 | // the process dies. |
| 49 | // |
| 50 | // It is the caller's responsibility to ensure that the creating thread is |
| 51 | // not terminated prematurely. See https://go.dev/issue/27505 for more details. |
| 52 | func Command(args ...string) *exec.Cmd { |
| 53 | return command(args...) |
| 54 | } |
| 55 | |
| 56 | // CommandContext is like [Command] but includes a context. It uses |
| 57 | // [exec.CommandContext] under the hood. |
| 58 | // |
| 59 | // The provided context is used to interrupt the process |
| 60 | // (by calling cmd.Cancel or [os.Process.Kill]) |
| 61 | // if the context becomes done before the command completes on its own. |
| 62 | // |
| 63 | // CommandContext sets the command's Cancel function to invoke the Kill method |
| 64 | // on its Process, and leaves its WaitDelay unset. The caller may change the |
| 65 | // cancellation behavior by modifying those fields before starting the command. |
| 66 | func CommandContext(ctx context.Context, args ...string) *exec.Cmd { |
| 67 | return commandContext(ctx, args...) |
| 68 | } |
| 69 | |
| 70 | // Self returns the path to the current process's binary. |
| 71 | // |
| 72 | // On Linux, it returns "/proc/self/exe", which provides the in-memory version |
| 73 | // of the current binary. This makes it safe to delete or replace the on-disk |
| 74 | // binary (os.Args[0]). |
| 75 | // |
| 76 | // On Other platforms, it attempts to look up the absolute path for os.Args[0], |
| 77 | // or otherwise returns os.Args[0] as-is. For example if current binary is |
| 78 | // "my-binary" at "/usr/bin/" (or "my-binary.exe" at "C:\" on Windows), |
| 79 | // then it returns "/usr/bin/my-binary" and "C:\my-binary.exe" respectively. |
| 80 | func Self() string { |
| 81 | if runtime.GOOS == "linux" { |
| 82 | return "/proc/self/exe" |
| 83 | } |
| 84 | return naiveSelf() |
| 85 | } |
| 86 | |
| 87 | func naiveSelf() string { |
| 88 | name := os.Args[0] |
| 89 | if filepath.Base(name) == name { |
| 90 | if lp, err := exec.LookPath(name); err == nil { |
| 91 | return lp |
| 92 | } |
| 93 | } |
| 94 | // handle conversion of relative paths to absolute |
| 95 | if absName, err := filepath.Abs(name); err == nil { |
| 96 | return absName |
| 97 | } |
| 98 | // if we couldn't get absolute name, return original |
| 99 | // (NOTE: Go only errors on Abs() if os.Getwd fails) |
| 100 | return name |
| 101 | } |