1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
21 lockprefix = "crunch-run-"
23 brokenfile = "crunch-run-broken"
26 // procinfo is saved in each process's lockfile.
27 type procinfo struct {
32 // Detach acquires a lock for the given uuid, and starts the current
33 // program as a child process (with -no-detach prepended to the given
34 // arguments so the child knows not to detach again). The lock is
35 // passed along to the child process.
37 // Stdout and stderr in the child process are sent to the systemd
38 // journal using the systemd-cat program.
39 func Detach(uuid string, prog string, args []string, stdout, stderr io.Writer) int {
40 return exitcode(stderr, detach(uuid, prog, args, stdout, stderr))
42 func detach(uuid string, prog string, args []string, stdout, stderr io.Writer) error {
43 lockfile, err := func() (*os.File, error) {
44 // We must hold the dir-level lock between
45 // opening/creating the lockfile and acquiring LOCK_EX
46 // on it, to avoid racing with the ListProcess's
47 // alive-checking and garbage collection.
48 dirlock, err := lockall()
53 lockfilename := filepath.Join(lockdir, lockprefix+uuid+locksuffix)
54 lockfile, err := os.OpenFile(lockfilename, os.O_CREATE|os.O_RDWR, 0700)
56 return nil, fmt.Errorf("open %s: %s", lockfilename, err)
58 err = syscall.Flock(int(lockfile.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
61 return nil, fmt.Errorf("lock %s: %s", lockfilename, err)
68 defer lockfile.Close()
71 execargs := append([]string{"-no-detach"}, args...)
72 if strings.HasSuffix(prog, " crunch-run") {
73 // invoked as "/path/to/arvados-server crunch-run"
74 // (see arvados/lib/cmd.Multi)
75 execargs = append([]string{strings.TrimSuffix(prog, " crunch-run"), "crunch-run"}, execargs...)
77 // invoked as "/path/to/crunch-run"
78 execargs = append([]string{prog}, execargs...)
80 execargs = append([]string{
81 // Here, if the inner systemd-cat can't exec
82 // crunch-run, it writes an error message to stderr,
83 // and the outer systemd-cat writes it to the journal
84 // where the operator has a chance to discover it. (If
85 // we only used one systemd-cat command, it would be
86 // up to us to report the error -- but we are going to
87 // detach and exit, not wait for something to appear
88 // on stderr.) Note these systemd-cat calls don't
89 // result in additional processes -- they just connect
90 // stderr/stdout to sockets and call exec().
91 "systemd-cat", "--identifier=crunch-run",
92 "systemd-cat", "--identifier=crunch-run",
95 cmd := exec.Command(execargs[0], execargs[1:]...)
96 // Child inherits lockfile.
97 cmd.ExtraFiles = []*os.File{lockfile}
98 // Ensure child isn't interrupted even if we receive signals
99 // from parent (sshd) while sending lockfile content to
101 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
104 return fmt.Errorf("exec %s: %s", cmd.Path, err)
107 w := io.MultiWriter(stdout, lockfile)
108 return json.NewEncoder(w).Encode(procinfo{
110 PID: cmd.Process.Pid,
114 // KillProcess finds the crunch-run process corresponding to the given
115 // uuid, and sends the given signal to it. It then waits up to 1
116 // second for the process to die. It returns 0 if the process is
117 // successfully killed or didn't exist in the first place.
118 func KillProcess(uuid string, signal syscall.Signal, stdout, stderr io.Writer) int {
119 return exitcode(stderr, kill(uuid, signal, stdout, stderr))
122 func kill(uuid string, signal syscall.Signal, stdout, stderr io.Writer) error {
123 path := filepath.Join(lockdir, lockprefix+uuid+locksuffix)
124 f, err := os.Open(path)
125 if os.IsNotExist(err) {
127 } else if err != nil {
128 return fmt.Errorf("open %s: %s", path, err)
133 err = json.NewDecoder(f).Decode(&pi)
135 return fmt.Errorf("decode %s: %s\n", path, err)
138 if pi.UUID != uuid || pi.PID == 0 {
139 return fmt.Errorf("%s: bogus procinfo: %+v", path, pi)
142 proc, err := os.FindProcess(pi.PID)
144 // FindProcess should have succeeded, even if the
145 // process does not exist.
146 return fmt.Errorf("%s: find process %d: %s", uuid, pi.PID, err)
149 // Send the requested signal once, then send signal 0 a few
150 // times. When proc.Signal() returns an error (process no
151 // longer exists), return success. If that doesn't happen
152 // within 1 second, return an error.
153 err = proc.Signal(signal)
154 for deadline := time.Now().Add(time.Second); err == nil && time.Now().Before(deadline); time.Sleep(time.Second / 100) {
155 err = proc.Signal(syscall.Signal(0))
158 // Reached deadline without a proc.Signal() error.
159 return fmt.Errorf("%s: pid %d: sent signal %d (%s) but process is still alive", uuid, pi.PID, signal, signal)
161 fmt.Fprintf(stderr, "%s: pid %d: %s\n", uuid, pi.PID, err)
165 // List UUIDs of active crunch-run processes.
166 func ListProcesses(stdout, stderr io.Writer) int {
167 // filepath.Walk does not follow symlinks, so we must walk
168 // lockdir+"/." in case lockdir itself is a symlink.
169 walkdir := lockdir + "/."
170 return exitcode(stderr, filepath.Walk(walkdir, func(path string, info os.FileInfo, err error) error {
171 if info.IsDir() && path != walkdir {
172 return filepath.SkipDir
174 if name := info.Name(); name == brokenfile {
175 fmt.Fprintln(stdout, "broken")
177 } else if !strings.HasPrefix(name, lockprefix) || !strings.HasSuffix(name, locksuffix) {
180 if info.Size() == 0 {
181 // race: process has opened/locked but hasn't yet written pid/uuid
185 f, err := os.Open(path)
191 // Ensure other processes don't acquire this lockfile
192 // after we have decided it is abandoned but before we
194 dirlock, err := lockall()
198 err = syscall.Flock(int(f.Fd()), syscall.LOCK_SH|syscall.LOCK_NB)
201 err := os.Remove(path)
204 fmt.Fprintf(stderr, "unlink %s: %s\n", f.Name(), err)
211 err = json.NewDecoder(f).Decode(&pi)
213 fmt.Fprintf(stderr, "%s: %s\n", path, err)
216 if pi.UUID == "" || pi.PID == 0 {
217 fmt.Fprintf(stderr, "%s: bogus procinfo: %+v", path, pi)
221 fmt.Fprintln(stdout, pi.UUID)
226 // If err is nil, return 0 ("success"); otherwise, print err to stderr
228 func exitcode(stderr io.Writer, err error) int {
230 fmt.Fprintln(stderr, err)
236 // Acquire a dir-level lock. Must be held while creating or deleting
237 // container-specific lockfiles, to avoid races during the intervals
238 // when those container-specific lockfiles are open but not locked.
240 // Caller releases the lock by closing the returned file.
241 func lockall() (*os.File, error) {
242 lockfile := filepath.Join(lockdir, lockprefix+"all"+locksuffix)
243 f, err := os.OpenFile(lockfile, os.O_CREATE|os.O_RDWR, 0700)
245 return nil, fmt.Errorf("open %s: %s", lockfile, err)
247 err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
250 return nil, fmt.Errorf("lock %s: %s", lockfile, err)