| 1 |
// Copyright (c) 2014-2019 Ludovic Fauvet |
| 2 |
// Licensed under the MIT license |
| 3 |
|
| 4 |
package process |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"net" |
| 10 |
"os" |
| 11 |
"os/exec" |
| 12 |
"path" |
| 13 |
"strconv" |
| 14 |
"syscall" |
| 15 |
|
| 16 |
"github.com/etix/mirrorbits/core" |
| 17 |
"github.com/op/go-logging" |
| 18 |
) |
| 19 |
|
| 20 |
var ( |
| 21 |
// Compile time variable |
| 22 |
defaultPidFile string |
| 23 |
) |
| 24 |
|
| 25 |
var ( |
| 26 |
// ErrInvalidfd is returned when the given file descriptor is invalid |
| 27 |
ErrInvalidfd = errors.New("invalid file descriptor") |
| 28 |
|
| 29 |
log = logging.MustGetLogger("main") |
| 30 |
) |
| 31 |
|
| 32 |
// Relaunch launches {self} as a child process passing listener details |
| 33 |
// to provide a seamless binary upgrade. |
| 34 |
func Relaunch(l net.Listener) error { |
| 35 |
argv0, err := exec.LookPath(os.Args[0]) |
| 36 |
if err != nil { |
| 37 |
return err |
| 38 |
} |
| 39 |
if _, err := os.Stat(argv0); err != nil { |
| 40 |
return err |
| 41 |
} |
| 42 |
wd, err := os.Getwd() |
| 43 |
if err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
|
| 47 |
var file *os.File |
| 48 |
|
| 49 |
switch t := l.(type) { |
| 50 |
case *net.TCPListener: |
| 51 |
file, err = t.File() |
| 52 |
case *net.UnixListener: |
| 53 |
file, err = t.File() |
| 54 |
default: |
| 55 |
return ErrInvalidfd |
| 56 |
} |
| 57 |
if err != nil { |
| 58 |
return err |
| 59 |
} |
| 60 |
|
| 61 |
fd := file.Fd() |
| 62 |
sysfile := file.Name() |
| 63 |
|
| 64 |
listener, ok := l.(*net.TCPListener) |
| 65 |
if ok { |
| 66 |
listenerFile, err := listener.File() |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
fd = listenerFile.Fd() |
| 71 |
sysfile = listenerFile.Name() |
| 72 |
} |
| 73 |
|
| 74 |
if fd < uintptr(syscall.Stderr) { |
| 75 |
return ErrInvalidfd |
| 76 |
} |
| 77 |
|
| 78 |
if err := os.Setenv("OLD_FD", fmt.Sprint(fd)); err != nil { |
| 79 |
return err |
| 80 |
} |
| 81 |
if err := os.Setenv("OLD_NAME", fmt.Sprintf("tcp:%s->", l.Addr().String())); err != nil { |
| 82 |
return err |
| 83 |
} |
| 84 |
if err := os.Setenv("OLD_PPID", fmt.Sprint(syscall.Getpid())); err != nil { |
| 85 |
return err |
| 86 |
} |
| 87 |
|
| 88 |
files := make([]*os.File, fd+1) |
| 89 |
files[syscall.Stdin] = os.Stdin |
| 90 |
files[syscall.Stdout] = os.Stdout |
| 91 |
files[syscall.Stderr] = os.Stderr |
| 92 |
files[fd] = os.NewFile(fd, sysfile) |
| 93 |
p, err := os.StartProcess(argv0, os.Args, &os.ProcAttr{ |
| 94 |
Dir: wd, |
| 95 |
Env: os.Environ(), |
| 96 |
Files: files, |
| 97 |
Sys: &syscall.SysProcAttr{}, |
| 98 |
}) |
| 99 |
if err != nil { |
| 100 |
return err |
| 101 |
} |
| 102 |
log.Infof("Spawned child %d\n", p.Pid) |
| 103 |
return nil |
| 104 |
} |
| 105 |
|
| 106 |
// Recover from a seamless binary upgrade and use an already |
| 107 |
// existing listener to take over the connections |
| 108 |
func Recover() (l net.Listener, ppid int, err error) { |
| 109 |
var fd uintptr |
| 110 |
_, err = fmt.Sscan(os.Getenv("OLD_FD"), &fd) |
| 111 |
if err != nil { |
| 112 |
return |
| 113 |
} |
| 114 |
var i net.Listener |
| 115 |
i, err = net.FileListener(os.NewFile(fd, os.Getenv("OLD_NAME"))) |
| 116 |
if err != nil { |
| 117 |
return |
| 118 |
} |
| 119 |
switch i.(type) { |
| 120 |
case *net.TCPListener: |
| 121 |
l = i.(*net.TCPListener) |
| 122 |
case *net.UnixListener: |
| 123 |
l = i.(*net.UnixListener) |
| 124 |
default: |
| 125 |
err = fmt.Errorf("file descriptor is %T not *net.TCPListener or *net.UnixListener", i) |
| 126 |
return |
| 127 |
} |
| 128 |
if err = syscall.Close(int(fd)); err != nil { |
| 129 |
return |
| 130 |
} |
| 131 |
_, err = fmt.Sscan(os.Getenv("OLD_PPID"), &ppid) |
| 132 |
if err != nil { |
| 133 |
return |
| 134 |
} |
| 135 |
return |
| 136 |
} |
| 137 |
|
| 138 |
// KillParent sends a signal to make the parent exit gracefully with SIGQUIT |
| 139 |
func KillParent(ppid int) error { |
| 140 |
log.Info("Asking parent to quit") |
| 141 |
return syscall.Kill(ppid, syscall.SIGQUIT) |
| 142 |
} |
| 143 |
|
| 144 |
// GetPidLocation finds the location to store our pid file |
| 145 |
// and fallback to /run if none found |
| 146 |
func GetPidLocation() string { |
| 147 |
if core.PidFile == "" { // Runtime |
| 148 |
rdir := os.Getenv("XDG_RUNTIME_DIR") |
| 149 |
if rdir == "" { |
| 150 |
if defaultPidFile == "" { // Compile time |
| 151 |
return "/run/mirrorbits/mirrorbits.pid" // Fallback |
| 152 |
} |
| 153 |
return defaultPidFile |
| 154 |
} |
| 155 |
return rdir + "/mirrorbits.pid" |
| 156 |
} |
| 157 |
return core.PidFile |
| 158 |
} |
| 159 |
|
| 160 |
// WritePidFile writes the current pid file to disk |
| 161 |
func WritePidFile() { |
| 162 |
// Get the pid destination |
| 163 |
p := GetPidLocation() |
| 164 |
|
| 165 |
// Create the whole directory path |
| 166 |
if err := os.MkdirAll(path.Dir(p), 0755); err != nil { |
| 167 |
log.Errorf("Unable to write pid file: %v", err) |
| 168 |
} |
| 169 |
|
| 170 |
// Get our own PID and write it |
| 171 |
pid := strconv.Itoa(os.Getpid()) |
| 172 |
if err := os.WriteFile(p, []byte(pid), 0644); err != nil { |
| 173 |
log.Errorf("Unable to write pid file: %v", err) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
// RemovePidFile removes the current pid file |
| 178 |
func RemovePidFile() { |
| 179 |
pidFile := GetPidLocation() |
| 180 |
if _, err := os.Stat(pidFile); !os.IsNotExist(err) { |
| 181 |
// Ensures we don't remove our forked process pid file |
| 182 |
// This can happen during seamless binary upgrade |
| 183 |
if GetRemoteProcPid() == os.Getpid() { |
| 184 |
if err = os.Remove(pidFile); err != nil { |
| 185 |
log.Errorf("Unable to remove pid file: %v", err) |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
// GetRemoteProcPid gets the pid as it appears in the pid file (maybe not ours) |
| 192 |
func GetRemoteProcPid() int { |
| 193 |
b, err := os.ReadFile(GetPidLocation()) |
| 194 |
if err != nil { |
| 195 |
return -1 |
| 196 |
} |
| 197 |
i, err := strconv.ParseInt(string(b), 10, 0) |
| 198 |
if err != nil { |
| 199 |
return -1 |
| 200 |
} |
| 201 |
return int(i) |
| 202 |
} |