2011-04-02 15:28:58 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2019-10-08 13:19:13 -06:00
|
|
|
// +build aix darwin dragonfly freebsd js,wasm linux netbsd openbsd solaris windows
|
build: add build comments to core packages
The go/build package already recognizes
system-specific file names like
mycode_darwin.go
mycode_darwin_386.go
mycode_386.s
However, it is also common to write files that
apply to multiple architectures, so a recent CL added
to go/build the ability to process comments
listing a set of conditions for building. For example:
// +build darwin freebsd openbsd/386
says that this file should be compiled only on
OS X, FreeBSD, or 32-bit x86 OpenBSD systems.
These conventions are not yet documented
(hence this long CL description).
This CL adds build comments to the multi-system
files in the core library, a step toward making it
possible to use go/build to build them.
With this change go/build can handle crypto/rand,
exec, net, path/filepath, os/user, and time.
os and syscall need additional adjustments.
R=golang-dev, r, gri, r, gustavo
CC=golang-dev
https://golang.org/cl/5011046
2011-09-15 14:48:57 -06:00
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
package os
|
|
|
|
|
2011-06-06 03:53:30 -06:00
|
|
|
import (
|
2019-10-15 08:52:23 -06:00
|
|
|
"runtime"
|
2011-06-06 03:53:30 -06:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2018-09-25 10:22:09 -06:00
|
|
|
// The only signal values guaranteed to be present in the os package on all
|
|
|
|
// systems are os.Interrupt (send the process an interrupt) and os.Kill (force
|
|
|
|
// the process to exit). On Windows, sending os.Interrupt to a process with
|
|
|
|
// os.Process.Signal is not implemented; it will return an error instead of
|
|
|
|
// sending a signal.
|
2012-05-04 04:44:41 -06:00
|
|
|
var (
|
|
|
|
Interrupt Signal = syscall.SIGINT
|
|
|
|
Kill Signal = syscall.SIGKILL
|
|
|
|
)
|
|
|
|
|
2012-03-01 19:56:54 -07:00
|
|
|
func startProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
|
2012-06-24 17:34:06 -06:00
|
|
|
// If there is no SysProcAttr (ie. no Chroot or changed
|
|
|
|
// UID/GID), double-check existence of the directory we want
|
2016-03-01 16:21:55 -07:00
|
|
|
// to chdir into. We can make the error clearer this way.
|
2012-06-24 17:34:06 -06:00
|
|
|
if attr != nil && attr.Sys == nil && attr.Dir != "" {
|
2012-02-29 13:53:57 -07:00
|
|
|
if _, err := Stat(attr.Dir); err != nil {
|
|
|
|
pe := err.(*PathError)
|
|
|
|
pe.Op = "chdir"
|
|
|
|
return nil, pe
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
sysattr := &syscall.ProcAttr{
|
|
|
|
Dir: attr.Dir,
|
|
|
|
Env: attr.Env,
|
2011-06-14 08:49:34 -06:00
|
|
|
Sys: attr.Sys,
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
if sysattr.Env == nil {
|
2019-05-12 06:34:30 -06:00
|
|
|
sysattr.Env, err = environForSysProcAttr(sysattr.Sys)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2019-03-03 16:52:00 -07:00
|
|
|
sysattr.Files = make([]uintptr, 0, len(attr.Files))
|
2011-07-01 08:18:07 -06:00
|
|
|
for _, f := range attr.Files {
|
2012-02-10 14:47:19 -07:00
|
|
|
sysattr.Files = append(sysattr.Files, f.Fd())
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pid, h, e := syscall.StartProcess(name, argv, sysattr)
|
2019-10-15 08:52:23 -06:00
|
|
|
|
|
|
|
// Make sure we don't run the finalizers of attr.Files.
|
|
|
|
runtime.KeepAlive(attr)
|
|
|
|
|
2011-11-13 20:44:52 -07:00
|
|
|
if e != nil {
|
|
|
|
return nil, &PathError{"fork/exec", name, e}
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2019-10-15 08:52:23 -06:00
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
return newProcess(pid, h), nil
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:56:54 -07:00
|
|
|
func (p *Process) kill() error {
|
os/signal: selective signal handling
Restore package os/signal, with new API:
Notify replaces Incoming, allowing clients
to ask for certain signals only. Also, signals
go to everyone who asks, not just one client.
This could plausibly move into package os now
that there are no magic side effects as a result
of the import.
Update runtime for new API: move common Unix
signal handling code into signal_unix.c.
(It's so easy to do this now that we don't have
to edit Makefiles!)
Tested on darwin,linux 386,amd64.
Fixes #1266.
R=r, dsymonds, bradfitz, iant, borman
CC=golang-dev
https://golang.org/cl/3749041
2012-02-13 11:52:37 -07:00
|
|
|
return p.Signal(Kill)
|
2011-06-06 03:53:30 -06:00
|
|
|
}
|
|
|
|
|
2012-03-01 20:07:26 -07:00
|
|
|
// ProcessState stores information about a process, as reported by Wait.
|
2012-02-20 20:10:34 -07:00
|
|
|
type ProcessState struct {
|
2012-02-22 13:51:49 -07:00
|
|
|
pid int // The process's id.
|
|
|
|
status syscall.WaitStatus // System-dependent status info.
|
2012-02-20 20:10:34 -07:00
|
|
|
rusage *syscall.Rusage
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pid returns the process id of the exited process.
|
|
|
|
func (p *ProcessState) Pid() int {
|
|
|
|
return p.pid
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:56:54 -07:00
|
|
|
func (p *ProcessState) exited() bool {
|
2012-02-20 20:10:34 -07:00
|
|
|
return p.status.Exited()
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:56:54 -07:00
|
|
|
func (p *ProcessState) success() bool {
|
2012-02-20 20:10:34 -07:00
|
|
|
return p.status.ExitStatus() == 0
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:56:54 -07:00
|
|
|
func (p *ProcessState) sys() interface{} {
|
2012-02-20 20:10:34 -07:00
|
|
|
return p.status
|
|
|
|
}
|
|
|
|
|
2012-03-01 19:56:54 -07:00
|
|
|
func (p *ProcessState) sysUsage() interface{} {
|
2012-02-20 20:10:34 -07:00
|
|
|
return p.rusage
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
|
|
|
|
2012-02-20 20:10:34 -07:00
|
|
|
func (p *ProcessState) String() string {
|
|
|
|
if p == nil {
|
2011-06-20 13:42:17 -06:00
|
|
|
return "<nil>"
|
|
|
|
}
|
2012-02-22 13:51:49 -07:00
|
|
|
status := p.Sys().(syscall.WaitStatus)
|
2011-04-02 15:28:58 -06:00
|
|
|
res := ""
|
|
|
|
switch {
|
2012-02-20 20:10:34 -07:00
|
|
|
case status.Exited():
|
2014-12-31 13:18:59 -07:00
|
|
|
res = "exit status " + itoa(status.ExitStatus())
|
2012-02-20 20:10:34 -07:00
|
|
|
case status.Signaled():
|
2013-01-31 08:53:18 -07:00
|
|
|
res = "signal: " + status.Signal().String()
|
2012-02-20 20:10:34 -07:00
|
|
|
case status.Stopped():
|
2013-01-31 08:53:18 -07:00
|
|
|
res = "stop signal: " + status.StopSignal().String()
|
2012-02-20 20:10:34 -07:00
|
|
|
if status.StopSignal() == syscall.SIGTRAP && status.TrapCause() != 0 {
|
2014-12-31 13:18:59 -07:00
|
|
|
res += " (trap " + itoa(status.TrapCause()) + ")"
|
2011-04-02 15:28:58 -06:00
|
|
|
}
|
2012-02-20 20:10:34 -07:00
|
|
|
case status.Continued():
|
2011-04-02 15:28:58 -06:00
|
|
|
res = "continued"
|
|
|
|
}
|
2012-02-20 20:10:34 -07:00
|
|
|
if status.CoreDump() {
|
2011-04-02 15:28:58 -06:00
|
|
|
res += " (core dumped)"
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
2018-08-27 19:03:37 -06:00
|
|
|
|
|
|
|
// ExitCode returns the exit code of the exited process, or -1
|
|
|
|
// if the process hasn't exited or was terminated by a signal.
|
|
|
|
func (p *ProcessState) ExitCode() int {
|
|
|
|
// return -1 if the process hasn't started.
|
|
|
|
if p == nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return p.status.ExitStatus()
|
|
|
|
}
|