github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/os/exec_unix.go (about)

     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  // +build darwin freebsd linux netbsd openbsd
     6  
     7  package os
     8  
     9  import (
    10  	"errors"
    11  	"runtime"
    12  	"syscall"
    13  	"time"
    14  )
    15  
    16  func (p *Process) wait() (ps *ProcessState, err error) {
    17  	if p.Pid == -1 {
    18  		return nil, syscall.EINVAL
    19  	}
    20  	var status syscall.WaitStatus
    21  	var rusage syscall.Rusage
    22  	pid1, e := syscall.Wait4(p.Pid, &status, 0, &rusage)
    23  	if e != nil {
    24  		return nil, NewSyscallError("wait", e)
    25  	}
    26  	if pid1 != 0 {
    27  		p.setDone()
    28  	}
    29  	ps = &ProcessState{
    30  		pid:    pid1,
    31  		status: status,
    32  		rusage: &rusage,
    33  	}
    34  	return ps, nil
    35  }
    36  
    37  func (p *Process) signal(sig Signal) error {
    38  	if p.done() {
    39  		return errors.New("os: process already finished")
    40  	}
    41  	s, ok := sig.(syscall.Signal)
    42  	if !ok {
    43  		return errors.New("os: unsupported signal type")
    44  	}
    45  	if e := syscall.Kill(p.Pid, s); e != nil {
    46  		return e
    47  	}
    48  	return nil
    49  }
    50  
    51  func (p *Process) release() error {
    52  	// NOOP for unix.
    53  	p.Pid = -1
    54  	// no need for a finalizer anymore
    55  	runtime.SetFinalizer(p, nil)
    56  	return nil
    57  }
    58  
    59  func findProcess(pid int) (p *Process, err error) {
    60  	// NOOP for unix.
    61  	return newProcess(pid, 0), nil
    62  }
    63  
    64  func (p *ProcessState) userTime() time.Duration {
    65  	return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
    66  }
    67  
    68  func (p *ProcessState) systemTime() time.Duration {
    69  	return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
    70  }