Black Lives Matter. Support the Equal Justice Initiative.

Source file src/os/exec_unix.go

Documentation: os

     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 aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris
     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  
    21  	// If we can block until Wait4 will succeed immediately, do so.
    22  	ready, err := p.blockUntilWaitable()
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	if ready {
    27  		// Mark the process done now, before the call to Wait4,
    28  		// so that Process.signal will not send a signal.
    29  		p.setDone()
    30  		// Acquire a write lock on sigMu to wait for any
    31  		// active call to the signal method to complete.
    32  		p.sigMu.Lock()
    33  		p.sigMu.Unlock()
    34  	}
    35  
    36  	var (
    37  		status syscall.WaitStatus
    38  		rusage syscall.Rusage
    39  		pid1   int
    40  		e      error
    41  	)
    42  	for {
    43  		pid1, e = syscall.Wait4(p.Pid, &status, 0, &rusage)
    44  		if e != syscall.EINTR {
    45  			break
    46  		}
    47  	}
    48  	if e != nil {
    49  		return nil, NewSyscallError("wait", e)
    50  	}
    51  	if pid1 != 0 {
    52  		p.setDone()
    53  	}
    54  	ps = &ProcessState{
    55  		pid:    pid1,
    56  		status: status,
    57  		rusage: &rusage,
    58  	}
    59  	return ps, nil
    60  }
    61  
    62  var errFinished = errors.New("os: process already finished")
    63  
    64  func (p *Process) signal(sig Signal) error {
    65  	if p.Pid == -1 {
    66  		return errors.New("os: process already released")
    67  	}
    68  	if p.Pid == 0 {
    69  		return errors.New("os: process not initialized")
    70  	}
    71  	p.sigMu.RLock()
    72  	defer p.sigMu.RUnlock()
    73  	if p.done() {
    74  		return errFinished
    75  	}
    76  	s, ok := sig.(syscall.Signal)
    77  	if !ok {
    78  		return errors.New("os: unsupported signal type")
    79  	}
    80  	if e := syscall.Kill(p.Pid, s); e != nil {
    81  		if e == syscall.ESRCH {
    82  			return errFinished
    83  		}
    84  		return e
    85  	}
    86  	return nil
    87  }
    88  
    89  func (p *Process) release() error {
    90  	// NOOP for unix.
    91  	p.Pid = -1
    92  	// no need for a finalizer anymore
    93  	runtime.SetFinalizer(p, nil)
    94  	return nil
    95  }
    96  
    97  func findProcess(pid int) (p *Process, err error) {
    98  	// NOOP for unix.
    99  	return newProcess(pid, 0), nil
   100  }
   101  
   102  func (p *ProcessState) userTime() time.Duration {
   103  	return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
   104  }
   105  
   106  func (p *ProcessState) systemTime() time.Duration {
   107  	return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
   108  }
   109  

View as plain text