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 darwin freebsd linux openbsd windows
16 func (sig UnixSignal) String() string {
17 s := runtime.Signame(int32(sig))
24 // StartProcess starts a new process with the program, arguments and attributes
25 // specified by name, argv and attr.
27 // StartProcess is a low-level interface. The exec package provides
28 // higher-level interfaces.
29 func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
30 sysattr := &syscall.ProcAttr{
35 if sysattr.Env == nil {
36 sysattr.Env = Environ()
38 for _, f := range attr.Files {
39 sysattr.Files = append(sysattr.Files, f.Fd())
42 pid, h, e := syscall.StartProcess(name, argv, sysattr)
44 return nil, &PathError{"fork/exec", name, Errno(e)}
46 return newProcess(pid, h), nil
49 // Kill causes the Process to exit immediately.
50 func (p *Process) Kill() error {
51 return p.Signal(SIGKILL)
54 // Exec replaces the current process with an execution of the
55 // named binary, with arguments argv and environment envv.
56 // If successful, Exec never returns. If it fails, it returns an error.
58 // To run a child process, see StartProcess (for a low-level interface)
59 // or the exec package (for higher-level interfaces).
60 func Exec(name string, argv []string, envv []string) error {
64 e := syscall.Exec(name, argv, envv)
66 return &PathError{"exec", name, Errno(e)}
71 // TODO(rsc): Should os implement its own syscall.WaitStatus
72 // wrapper with the methods, or is exposing the underlying one enough?
74 // TODO(rsc): Certainly need to have Rusage struct,
75 // since syscall one might have different field types across
78 // Waitmsg stores the information about an exited process as reported by Wait.
80 Pid int // The process's id.
81 syscall.WaitStatus // System-dependent status info.
82 Rusage *syscall.Rusage // System-dependent resource usage info.
85 // Wait waits for process pid to exit or stop, and then returns a
86 // Waitmsg describing its status and an error, if any. The options
87 // (WNOHANG etc.) affect the behavior of the Wait call.
88 // Wait is equivalent to calling FindProcess and then Wait
89 // and Release on the result.
90 func Wait(pid int, options int) (w *Waitmsg, err error) {
91 p, e := FindProcess(pid)
96 return p.Wait(options)
99 // Convert i to decimal string.
100 func itod(i int) string {
110 // Assemble decimal in reverse order.
113 for ; u > 0; u /= 10 {
115 b[bp] = byte(u%10) + '0'
123 return string(b[bp:])
126 func (w *Waitmsg) String() string {
130 // TODO(austin) Use signal names when possible?
134 res = "exit status " + itod(w.ExitStatus())
136 res = "signal " + itod(w.Signal())
138 res = "stop signal " + itod(w.StopSignal())
139 if w.StopSignal() == syscall.SIGTRAP && w.TrapCause() != 0 {
140 res += " (trap " + itod(w.TrapCause()) + ")"
146 res += " (core dumped)"