1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 lockprefix = "crunch-run-"
26 // procinfo is saved in each process's lockfile.
27 type procinfo struct {
34 // Detach acquires a lock for the given uuid, and starts the current
35 // program as a child process (with -no-detach prepended to the given
36 // arguments so the child knows not to detach again). The lock is
37 // passed along to the child process.
38 func Detach(uuid string, args []string, stdout, stderr io.Writer) int {
39 return exitcode(stderr, detach(uuid, args, stdout, stderr))
41 func detach(uuid string, args []string, stdout, stderr io.Writer) error {
42 lockfile, err := func() (*os.File, error) {
43 // We must hold the dir-level lock between
44 // opening/creating the lockfile and acquiring LOCK_EX
45 // on it, to avoid racing with the ListProcess's
46 // alive-checking and garbage collection.
47 dirlock, err := lockall()
52 lockfile, err := os.OpenFile(filepath.Join(lockdir, lockprefix+uuid+locksuffix), os.O_CREATE|os.O_RDWR, 0700)
56 err = syscall.Flock(int(lockfile.Fd()), syscall.LOCK_EX|syscall.LOCK_NB)
66 defer lockfile.Close()
69 outfile, err := ioutil.TempFile("", "crunch-run-"+uuid+"-stdout-")
74 errfile, err := ioutil.TempFile("", "crunch-run-"+uuid+"-stderr-")
76 os.Remove(outfile.Name())
81 cmd := exec.Command(args[0], append([]string{"-no-detach"}, args[1:]...)...)
84 // Child inherits lockfile.
85 cmd.ExtraFiles = []*os.File{lockfile}
86 // Ensure child isn't interrupted even if we receive signals
87 // from parent (sshd) while sending lockfile content to
89 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
92 os.Remove(outfile.Name())
93 os.Remove(errfile.Name())
97 w := io.MultiWriter(stdout, lockfile)
98 err = json.NewEncoder(w).Encode(procinfo{
100 PID: cmd.Process.Pid,
101 Stdout: outfile.Name(),
102 Stderr: errfile.Name(),
105 os.Remove(outfile.Name())
106 os.Remove(errfile.Name())
112 // KillProcess finds the crunch-run process corresponding to the given
113 // uuid, and sends the given signal to it. It then waits up to 1
114 // second for the process to die. It returns 0 if the process is
115 // successfully killed or didn't exist in the first place.
116 func KillProcess(uuid string, signal syscall.Signal, stdout, stderr io.Writer) int {
117 return exitcode(stderr, kill(uuid, signal, stdout, stderr))
120 func kill(uuid string, signal syscall.Signal, stdout, stderr io.Writer) error {
121 path := filepath.Join(lockdir, lockprefix+uuid+locksuffix)
122 f, err := os.Open(path)
123 if os.IsNotExist(err) {
125 } else if err != nil {
131 err = json.NewDecoder(f).Decode(&pi)
133 return fmt.Errorf("%s: %s\n", path, err)
136 if pi.UUID != uuid || pi.PID == 0 {
137 return fmt.Errorf("%s: bogus procinfo: %+v", path, pi)
140 proc, err := os.FindProcess(pi.PID)
145 err = proc.Signal(signal)
146 for deadline := time.Now().Add(time.Second); err == nil && time.Now().Before(deadline); time.Sleep(time.Second / 100) {
147 err = proc.Signal(syscall.Signal(0))
150 return fmt.Errorf("pid %d: sent signal %d (%s) but process is still alive", pi.PID, signal, signal)
152 fmt.Fprintf(stderr, "pid %d: %s\n", pi.PID, err)
156 // List UUIDs of active crunch-run processes.
157 func ListProcesses(stdout, stderr io.Writer) int {
158 // filepath.Walk does not follow symlinks, so we must walk
159 // lockdir+"/." in case lockdir itself is a symlink.
160 walkdir := lockdir + "/."
161 return exitcode(stderr, filepath.Walk(walkdir, func(path string, info os.FileInfo, err error) error {
162 if info.IsDir() && path != walkdir {
163 return filepath.SkipDir
165 if name := info.Name(); !strings.HasPrefix(name, lockprefix) || !strings.HasSuffix(name, locksuffix) {
168 if info.Size() == 0 {
169 // race: process has opened/locked but hasn't yet written pid/uuid
173 f, err := os.Open(path)
179 // Ensure other processes don't acquire this lockfile
180 // after we have decided it is abandoned but before we
182 dirlock, err := lockall()
186 err = syscall.Flock(int(f.Fd()), syscall.LOCK_SH|syscall.LOCK_NB)
189 err := os.Remove(path)
192 fmt.Fprintln(stderr, err)
199 err = json.NewDecoder(f).Decode(&pi)
201 fmt.Fprintf(stderr, "%s: %s\n", path, err)
204 if pi.UUID == "" || pi.PID == 0 {
205 fmt.Fprintf(stderr, "%s: bogus procinfo: %+v", path, pi)
209 fmt.Fprintln(stdout, pi.UUID)
214 // If err is nil, return 0 ("success"); otherwise, print err to stderr
216 func exitcode(stderr io.Writer, err error) int {
218 fmt.Fprintln(stderr, err)
224 // Acquire a dir-level lock. Must be held while creating or deleting
225 // container-specific lockfiles, to avoid races during the intervals
226 // when those container-specific lockfiles are open but not locked.
228 // Caller releases the lock by closing the returned file.
229 func lockall() (*os.File, error) {
230 f, err := os.OpenFile(filepath.Join(lockdir, lockprefix+"all"+locksuffix), os.O_CREATE|os.O_RDWR, 0700)
234 err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX)