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.
5 // +build aix darwin dragonfly freebsd hurd js,wasm linux nacl netbsd openbsd solaris windows
13 // The only signal values guaranteed to be present in the os package on all
14 // systems are os.Interrupt (send the process an interrupt) and os.Kill (force
15 // the process to exit). On Windows, sending os.Interrupt to a process with
16 // os.Process.Signal is not implemented; it will return an error instead of
19 Interrupt Signal = syscall.SIGINT
20 Kill Signal = syscall.SIGKILL
23 func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
24 // If there is no SysProcAttr (ie. no Chroot or changed
25 // UID/GID), double-check existence of the directory we want
26 // to chdir into. We can make the error clearer this way.
27 if attr != nil && attr.Sys == nil && attr.Dir != "" {
28 if _, err := Stat(attr.Dir); err != nil {
29 pe := err.(*PathError)
35 sysattr := &syscall.ProcAttr{
40 if sysattr.Env == nil {
41 sysattr.Env = Environ()
43 for _, f := range attr.Files {
44 sysattr.Files = append(sysattr.Files, f.Fd())
47 pid, h, e := syscall.StartProcess(name, argv, sysattr)
49 return nil, &PathError{"fork/exec", name, e}
51 return newProcess(pid, h), nil
54 func (p *Process) kill() error {
58 // ProcessState stores information about a process, as reported by Wait.
59 type ProcessState struct {
60 pid int // The process's id.
61 status syscall.WaitStatus // System-dependent status info.
62 rusage *syscall.Rusage
65 // Pid returns the process id of the exited process.
66 func (p *ProcessState) Pid() int {
70 func (p *ProcessState) exited() bool {
71 return p.status.Exited()
74 func (p *ProcessState) success() bool {
75 return p.status.ExitStatus() == 0
78 func (p *ProcessState) sys() interface{} {
82 func (p *ProcessState) sysUsage() interface{} {
86 func (p *ProcessState) String() string {
90 status := p.Sys().(syscall.WaitStatus)
94 res = "exit status " + itoa(status.ExitStatus())
95 case status.Signaled():
96 res = "signal: " + status.Signal().String()
97 case status.Stopped():
98 res = "stop signal: " + status.StopSignal().String()
99 if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
100 res += " (trap " + itoa(status.TrapCause()) + ")"
102 case status.Continued():
105 if status.CoreDump() {
106 res += " (core dumped)"
111 // ExitCode returns the exit code of the exited process, or -1
112 // if the process hasn't exited or was terminated by a signal.
113 func (p *ProcessState) ExitCode() int {
114 // return -1 if the process hasn't started.
118 return p.status.ExitStatus()