Source file src/os/exec_posix.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 || windows
     6  
     7  package os
     8  
     9  import (
    10  	"internal/itoa"
    11  	"internal/syscall/execenv"
    12  	"runtime"
    13  	"syscall"
    14  )
    15  
    16  // unsetHandle is a value for Process.handle used when the handle is not set.
    17  // Same as syscall.InvalidHandle for Windows.
    18  const unsetHandle = ^uintptr(0)
    19  
    20  // The only signal values guaranteed to be present in the os package on all
    21  // systems are os.Interrupt (send the process an interrupt) and os.Kill (force
    22  // the process to exit). On Windows, sending os.Interrupt to a process with
    23  // os.Process.Signal is not implemented; it will return an error instead of
    24  // sending a signal.
    25  var (
    26  	Interrupt Signal = syscall.SIGINT
    27  	Kill      Signal = syscall.SIGKILL
    28  )
    29  
    30  func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
    31  	// If there is no SysProcAttr (ie. no Chroot or changed
    32  	// UID/GID), double-check existence of the directory we want
    33  	// to chdir into. We can make the error clearer this way.
    34  	if attr != nil && attr.Sys == nil && attr.Dir != "" {
    35  		if _, err := Stat(attr.Dir); err != nil {
    36  			pe := err.(*PathError)
    37  			pe.Op = "chdir"
    38  			return nil, pe
    39  		}
    40  	}
    41  
    42  	sysattr := &syscall.ProcAttr{
    43  		Dir: attr.Dir,
    44  		Env: attr.Env,
    45  		Sys: ensurePidfd(attr.Sys),
    46  	}
    47  	if sysattr.Env == nil {
    48  		sysattr.Env, err = execenv.Default(sysattr.Sys)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  	}
    53  	sysattr.Files = make([]uintptr, 0, len(attr.Files))
    54  	for _, f := range attr.Files {
    55  		sysattr.Files = append(sysattr.Files, f.Fd())
    56  	}
    57  
    58  	pid, h, e := syscall.StartProcess(name, argv, sysattr)
    59  
    60  	// Make sure we don't run the finalizers of attr.Files.
    61  	runtime.KeepAlive(attr)
    62  
    63  	if e != nil {
    64  		return nil, &PathError{Op: "fork/exec", Path: name, Err: e}
    65  	}
    66  
    67  	// For Windows, syscall.StartProcess above already returned a process handle.
    68  	if runtime.GOOS != "windows" {
    69  		h = getPidfd(sysattr.Sys)
    70  	}
    71  
    72  	return newProcess(pid, h), nil
    73  }
    74  
    75  func (p *Process) kill() error {
    76  	return p.Signal(Kill)
    77  }
    78  
    79  // ProcessState stores information about a process, as reported by Wait.
    80  type ProcessState struct {
    81  	pid    int                // The process's id.
    82  	status syscall.WaitStatus // System-dependent status info.
    83  	rusage *syscall.Rusage
    84  }
    85  
    86  // Pid returns the process id of the exited process.
    87  func (p *ProcessState) Pid() int {
    88  	return p.pid
    89  }
    90  
    91  func (p *ProcessState) exited() bool {
    92  	return p.status.Exited()
    93  }
    94  
    95  func (p *ProcessState) success() bool {
    96  	return p.status.ExitStatus() == 0
    97  }
    98  
    99  func (p *ProcessState) sys() any {
   100  	return p.status
   101  }
   102  
   103  func (p *ProcessState) sysUsage() any {
   104  	return p.rusage
   105  }
   106  
   107  func (p *ProcessState) String() string {
   108  	if p == nil {
   109  		return "<nil>"
   110  	}
   111  	status := p.Sys().(syscall.WaitStatus)
   112  	res := ""
   113  	switch {
   114  	case status.Exited():
   115  		code := status.ExitStatus()
   116  		if runtime.GOOS == "windows" && uint(code) >= 1<<16 { // windows uses large hex numbers
   117  			res = "exit status " + itoa.Uitox(uint(code))
   118  		} else { // unix systems use small decimal integers
   119  			res = "exit status " + itoa.Itoa(code) // unix
   120  		}
   121  	case status.Signaled():
   122  		res = "signal: " + status.Signal().String()
   123  	case status.Stopped():
   124  		res = "stop signal: " + status.StopSignal().String()
   125  		if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
   126  			res += " (trap " + itoa.Itoa(status.TrapCause()) + ")"
   127  		}
   128  	case status.Continued():
   129  		res = "continued"
   130  	}
   131  	if status.CoreDump() {
   132  		res += " (core dumped)"
   133  	}
   134  	return res
   135  }
   136  
   137  // ExitCode returns the exit code of the exited process, or -1
   138  // if the process hasn't exited or was terminated by a signal.
   139  func (p *ProcessState) ExitCode() int {
   140  	// return -1 if the process hasn't started.
   141  	if p == nil {
   142  		return -1
   143  	}
   144  	return p.status.ExitStatus()
   145  }
   146  

View as plain text