Source file src/os/exec_unix.go

     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  //go:build unix || (js && wasm) || wasip1
     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  	// Wait on pidfd if possible; fallback to using pid on ENOSYS.
    21  	//
    22  	// When pidfd is used, there is no wait/kill race (described in CL 23967)
    23  	// because PID recycle issue doesn't exist (IOW, pidfd, unlike PID, is
    24  	// guaranteed to refer to one particular process). Thus, there is no
    25  	// need for the workaround (blockUntilWaitable + sigMu) below.
    26  	if ps, e := p.pidfdWait(); e != syscall.ENOSYS {
    27  		return ps, NewSyscallError("waitid", e)
    28  	}
    29  
    30  	// If we can block until Wait4 will succeed immediately, do so.
    31  	ready, err := p.blockUntilWaitable()
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  	if ready {
    36  		// Mark the process done now, before the call to Wait4,
    37  		// so that Process.signal will not send a signal.
    38  		p.setDone()
    39  		// Acquire a write lock on sigMu to wait for any
    40  		// active call to the signal method to complete.
    41  		p.sigMu.Lock()
    42  		p.sigMu.Unlock()
    43  	}
    44  
    45  	var (
    46  		status syscall.WaitStatus
    47  		rusage syscall.Rusage
    48  		pid1   int
    49  		e      error
    50  	)
    51  	for {
    52  		pid1, e = syscall.Wait4(p.Pid, &status, 0, &rusage)
    53  		if e != syscall.EINTR {
    54  			break
    55  		}
    56  	}
    57  	if e != nil {
    58  		return nil, NewSyscallError("wait", e)
    59  	}
    60  	p.setDone()
    61  	ps = &ProcessState{
    62  		pid:    pid1,
    63  		status: status,
    64  		rusage: &rusage,
    65  	}
    66  	return ps, nil
    67  }
    68  
    69  func (p *Process) signal(sig Signal) error {
    70  	if p.Pid == -1 {
    71  		return errors.New("os: process already released")
    72  	}
    73  	if p.Pid == 0 {
    74  		return errors.New("os: process not initialized")
    75  	}
    76  	s, ok := sig.(syscall.Signal)
    77  	if !ok {
    78  		return errors.New("os: unsupported signal type")
    79  	}
    80  	// Use pidfd if possible; fallback on ENOSYS.
    81  	if err := p.pidfdSendSignal(s); err != syscall.ENOSYS {
    82  		return err
    83  	}
    84  	p.sigMu.RLock()
    85  	defer p.sigMu.RUnlock()
    86  	if p.done() {
    87  		return ErrProcessDone
    88  	}
    89  	return convertESRCH(syscall.Kill(p.Pid, s))
    90  }
    91  
    92  func convertESRCH(err error) error {
    93  	if err == syscall.ESRCH {
    94  		return ErrProcessDone
    95  	}
    96  	return err
    97  }
    98  
    99  func (p *Process) release() error {
   100  	p.pidfdRelease()
   101  	p.Pid = -1
   102  	// no need for a finalizer anymore
   103  	runtime.SetFinalizer(p, nil)
   104  	return nil
   105  }
   106  
   107  func findProcess(pid int) (p *Process, err error) {
   108  	// NOOP for unix.
   109  	return newProcess(pid, unsetHandle), nil
   110  }
   111  
   112  func (p *ProcessState) userTime() time.Duration {
   113  	return time.Duration(p.rusage.Utime.Nano()) * time.Nanosecond
   114  }
   115  
   116  func (p *ProcessState) systemTime() time.Duration {
   117  	return time.Duration(p.rusage.Stime.Nano()) * time.Nanosecond
   118  }
   119  

View as plain text