| 1 |
// Copyright 2009 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
//go:build unix || (js && wasm) || wasip1 |
| 6 |
|
| 7 |
package os |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"syscall" |
| 12 |
"time" |
| 13 |
) |
| 14 |
|
| 15 |
const ( |
| 16 |
// Special values for Process.Pid. |
| 17 |
pidUnset = 0 |
| 18 |
pidReleased = -1 |
| 19 |
) |
| 20 |
|
| 21 |
func (p *Process) wait() (ps *ProcessState, err error) { |
| 22 |
// Which type of Process do we have? |
| 23 |
if p.handle != nil { |
| 24 |
// pidfd |
| 25 |
return p.pidfdWait() |
| 26 |
} else { |
| 27 |
// Regular PID |
| 28 |
return p.pidWait() |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
func (p *Process) pidWait() (*ProcessState, error) { |
| 33 |
// TODO(go.dev/issue/67642): When there are concurrent Wait calls, one |
| 34 |
// may wait on the wrong process if the PID is reused after the |
| 35 |
// completes its wait. |
| 36 |
// |
| 37 |
// Checking for statusDone here would not be a complete fix, as the PID |
| 38 |
// could still be waited on and reused prior to blockUntilWaitable. |
| 39 |
switch p.pidStatus() { |
| 40 |
case statusReleased: |
| 41 |
return nil, syscall.EINVAL |
| 42 |
} |
| 43 |
|
| 44 |
// If we can block until Wait4 will succeed immediately, do so. |
| 45 |
ready, err := p.blockUntilWaitable() |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
if ready { |
| 50 |
// Mark the process done now, before the call to Wait4, |
| 51 |
// so that Process.pidSignal will not send a signal. |
| 52 |
p.doRelease(statusDone) |
| 53 |
// Acquire a write lock on sigMu to wait for any |
| 54 |
// active call to the signal method to complete. |
| 55 |
p.sigMu.Lock() |
| 56 |
p.sigMu.Unlock() |
| 57 |
} |
| 58 |
|
| 59 |
var ( |
| 60 |
status syscall.WaitStatus |
| 61 |
rusage syscall.Rusage |
| 62 |
) |
| 63 |
pid1, err := ignoringEINTR2(func() (int, error) { |
| 64 |
return syscall.Wait4(p.Pid, &status, 0, &rusage) |
| 65 |
}) |
| 66 |
if err != nil { |
| 67 |
return nil, NewSyscallError("wait", err) |
| 68 |
} |
| 69 |
p.doRelease(statusDone) |
| 70 |
return &ProcessState{ |
| 71 |
pid: pid1, |
| 72 |
status: status, |
| 73 |
rusage: &rusage, |
| 74 |
}, nil |
| 75 |
} |
| 76 |
|
| 77 |
func (p *Process) signal(sig Signal) error { |
| 78 |
s, ok := sig.(syscall.Signal) |
| 79 |
if !ok { |
| 80 |
return errors.New("os: unsupported signal type") |
| 81 |
} |
| 82 |
|
| 83 |
// Which type of Process do we have? |
| 84 |
if p.handle != nil { |
| 85 |
// pidfd |
| 86 |
return p.pidfdSendSignal(s) |
| 87 |
} else { |
| 88 |
// Regular PID |
| 89 |
return p.pidSignal(s) |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
func (p *Process) pidSignal(s syscall.Signal) error { |
| 94 |
if p.Pid == pidReleased { |
| 95 |
return errProcessReleased |
| 96 |
} |
| 97 |
if p.Pid == pidUnset { |
| 98 |
return errors.New("os: process not initialized") |
| 99 |
} |
| 100 |
|
| 101 |
p.sigMu.RLock() |
| 102 |
defer p.sigMu.RUnlock() |
| 103 |
|
| 104 |
switch p.pidStatus() { |
| 105 |
case statusDone: |
| 106 |
return ErrProcessDone |
| 107 |
case statusReleased: |
| 108 |
return errProcessReleased |
| 109 |
} |
| 110 |
|
| 111 |
return convertESRCH(syscall.Kill(p.Pid, s)) |
| 112 |
} |
| 113 |
|
| 114 |
func convertESRCH(err error) error { |
| 115 |
if err == syscall.ESRCH { |
| 116 |
return ErrProcessDone |
| 117 |
} |
| 118 |
return err |
| 119 |
} |
| 120 |
|
| 121 |
func findProcess(pid int) (p *Process, err error) { |
| 122 |
h, err := pidfdFind(pid) |
| 123 |
if err == ErrProcessDone { |
| 124 |
// We can't return an error here since users are not expecting |
| 125 |
// it. Instead, return a process with a "done" state already |
| 126 |
// and let a subsequent Signal or Wait call catch that. |
| 127 |
return newDoneProcess(pid), nil |
| 128 |
} else if err != nil { |
| 129 |
// Ignore other errors from pidfdFind, as the callers |
| 130 |
// do not expect them. Fall back to using the PID. |
| 131 |
return newPIDProcess(pid), nil |
| 132 |
} |
| 133 |
// Use the handle. |
| 134 |
return newHandleProcess(pid, h), nil |
| 135 |
} |
| 136 |
|
| 137 |
func (p *ProcessState) userTime() time.Duration { |
| 138 |
return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond |
| 139 |
} |
| 140 |
|
| 141 |
func (p *ProcessState) systemTime() time.Duration { |
| 142 |
return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond |
| 143 |
} |