diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-04-06 15:14:11 +0200 |
commit | 505c19580e0f43fe5224431459cacb7c21edd93d (patch) | |
tree | 79e2634c253d60afc0cc0b2f510dc7dcbb48497b /src/pkg/os/exec_unix.go | |
parent | 1336a7c91e596c423a49d1194ea42d98bca0d958 (diff) | |
download | golang-505c19580e0f43fe5224431459cacb7c21edd93d.tar.gz |
Imported Upstream version 1upstream/1
Diffstat (limited to 'src/pkg/os/exec_unix.go')
-rw-r--r-- | src/pkg/os/exec_unix.go | 75 |
1 files changed, 34 insertions, 41 deletions
diff --git a/src/pkg/os/exec_unix.go b/src/pkg/os/exec_unix.go index 8a4b2e1b8..ecfe5353b 100644 --- a/src/pkg/os/exec_unix.go +++ b/src/pkg/os/exec_unix.go @@ -2,65 +2,53 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build darwin freebsd linux netbsd openbsd + package os import ( + "errors" "runtime" "syscall" + "time" ) -// Options for Wait. -const ( - WNOHANG = syscall.WNOHANG // Don't wait if no process has exited. - WSTOPPED = syscall.WSTOPPED // If set, status of stopped subprocesses is also reported. - WUNTRACED = syscall.WUNTRACED // Usually an alias for WSTOPPED. - WRUSAGE = 1 << 20 // Record resource usage. -) - -// WRUSAGE must not be too high a bit, to avoid clashing with Linux's -// WCLONE, WALL, and WNOTHREAD flags, which sit in the top few bits of -// the options - -// Wait waits for the Process to exit or stop, and then returns a -// Waitmsg describing its status and an Error, if any. The options -// (WNOHANG etc.) affect the behavior of the Wait call. -func (p *Process) Wait(options int) (w *Waitmsg, err Error) { +func (p *Process) wait() (ps *ProcessState, err error) { if p.Pid == -1 { - return nil, EINVAL + return nil, syscall.EINVAL } var status syscall.WaitStatus - var rusage *syscall.Rusage - if options&WRUSAGE != 0 { - rusage = new(syscall.Rusage) - options ^= WRUSAGE - } - pid1, e := syscall.Wait4(p.Pid, &status, options, rusage) - if e != 0 { + var rusage syscall.Rusage + pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage) + if e != nil { return nil, NewSyscallError("wait", e) } - if options&WSTOPPED == 0 { + if pid1 != 0 { p.done = true } - w = new(Waitmsg) - w.Pid = pid1 - w.WaitStatus = status - w.Rusage = rusage - return w, nil + ps = &ProcessState{ + pid: pid1, + status: status, + rusage: &rusage, + } + return ps, nil } -// Signal sends a signal to the Process. -func (p *Process) Signal(sig Signal) Error { +func (p *Process) signal(sig Signal) error { if p.done { - return NewError("os: process already finished") + return errors.New("os: process already finished") } - if e := syscall.Kill(p.Pid, int(sig.(UnixSignal))); e != 0 { - return Errno(e) + s, ok := sig.(syscall.Signal) + if !ok { + return errors.New("os: unsupported signal type") + } + if e := syscall.Kill(p.Pid, s); e != nil { + return e } return nil } -// Release releases any resources associated with the Process. -func (p *Process) Release() Error { +func (p *Process) release() error { // NOOP for unix. p.Pid = -1 // no need for a finalizer anymore @@ -68,10 +56,15 @@ func (p *Process) Release() Error { return nil } -// FindProcess looks for a running process by its pid. -// The Process it returns can be used to obtain information -// about the underlying operating system process. -func FindProcess(pid int) (p *Process, err Error) { +func findProcess(pid int) (p *Process, err error) { // NOOP for unix. return newProcess(pid, 0), nil } + +func (p *ProcessState) userTime() time.Duration { + return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond +} + +func (p *ProcessState) systemTime() time.Duration { + return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond +} |