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.
14 // The only signal values guaranteed to be present in the os package
15 // on all systems are Interrupt (send the process an interrupt) and
16 // Kill (force the process to exit). Interrupt is not implemented on
17 // Windows; using it with os.Process.Signal will return an error.
19 Interrupt Signal
= syscall
.Note("interrupt")
20 Kill Signal
= syscall
.Note("kill")
23 func startProcess(name
string, argv
[]string, attr
*ProcAttr
) (p
*Process
, err error
) {
24 sysattr
:= &syscall
.ProcAttr
{
30 sysattr
.Files
= make([]uintptr, 0, len(attr
.Files
))
31 for _
, f
:= range attr
.Files
{
32 sysattr
.Files
= append(sysattr
.Files
, f
.Fd())
35 pid
, h
, e
:= syscall
.StartProcess(name
, argv
, sysattr
)
37 return nil, &PathError
{Op
: "fork/exec", Path
: name
, Err
: e
}
40 return newProcess(pid
, h
), nil
43 func (p
*Process
) writeProcFile(file
string, data
string) error
{
44 f
, e
:= OpenFile("/proc/"+itoa
.Itoa(p
.Pid
)+"/"+file
, O_WRONLY
, 0)
49 _
, e
= f
.Write([]byte(data
))
53 func (p
*Process
) signal(sig Signal
) error
{
57 if e
:= p
.writeProcFile("note", sig
.String()); e
!= nil {
58 return NewSyscallError("signal", e
)
63 func (p
*Process
) kill() error
{
67 func (p
*Process
) wait() (ps
*ProcessState
, err error
) {
68 var waitmsg syscall
.Waitmsg
71 return nil, ErrInvalid
73 err
= syscall
.WaitProcess(p
.Pid
, &waitmsg
)
75 return nil, NewSyscallError("wait", err
)
86 func (p
*Process
) release() error
{
89 // no need for a finalizer anymore
90 runtime
.SetFinalizer(p
, nil)
94 func findProcess(pid
int) (p
*Process
, err error
) {
96 return newProcess(pid
, 0), nil
99 // ProcessState stores information about a process, as reported by Wait.
100 type ProcessState
struct {
101 pid
int // The process's id.
102 status
*syscall
.Waitmsg
// System-dependent status info.
105 // Pid returns the process id of the exited process.
106 func (p
*ProcessState
) Pid() int {
110 func (p
*ProcessState
) exited() bool {
111 return p
.status
.Exited()
114 func (p
*ProcessState
) success() bool {
115 return p
.status
.ExitStatus() == 0
118 func (p
*ProcessState
) sys() any
{
122 func (p
*ProcessState
) sysUsage() any
{
126 func (p
*ProcessState
) userTime() time
.Duration
{
127 return time
.Duration(p
.status
.Time
[0]) * time
.Millisecond
130 func (p
*ProcessState
) systemTime() time
.Duration
{
131 return time
.Duration(p
.status
.Time
[1]) * time
.Millisecond
134 func (p
*ProcessState
) String() string {
138 return "exit status: " + p
.status
.Msg
141 // ExitCode returns the exit code of the exited process, or -1
142 // if the process hasn't exited or was terminated by a signal.
143 func (p
*ProcessState
) ExitCode() int {
144 // return -1 if the process hasn't started.
148 return p
.status
.ExitStatus()