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.
|
|
|
|
|
2011-12-17 08:29:18 -07:00
|
|
|
// +build darwin freebsd linux netbsd openbsd 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 (
|
|
|
|
"runtime"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UnixSignal int32
|
|
|
|
|
|
|
|
func (sig UnixSignal) String() string {
|
|
|
|
s := runtime.Signame(int32(sig))
|
|
|
|
if len(s) > 0 {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return "UnixSignal"
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
|
|
|
|
// StartProcess starts a new process with the program, arguments and attributes
|
|
|
|
// specified by name, argv and attr.
|
2011-07-20 12:38:18 -06:00
|
|
|
//
|
2011-12-22 08:25:43 -07:00
|
|
|
// StartProcess is a low-level interface. The os/exec package provides
|
2011-07-20 12:38:18 -06:00
|
|
|
// higher-level interfaces.
|
2012-02-08 22:55:36 -07:00
|
|
|
//
|
|
|
|
// If there is an error, it will be of type *PathError.
|
2011-11-01 19:49:08 -06:00
|
|
|
func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
|
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 {
|
|
|
|
sysattr.Env = Environ()
|
|
|
|
}
|
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)
|
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
|
|
|
}
|
|
|
|
return newProcess(pid, h), nil
|
|
|
|
}
|
|
|
|
|
2011-06-06 03:53:30 -06:00
|
|
|
// Kill causes the Process to exit immediately.
|
2011-11-01 19:49:08 -06:00
|
|
|
func (p *Process) Kill() error {
|
2012-01-27 15:47:02 -07:00
|
|
|
return p.Signal(UnixSignal(syscall.SIGKILL))
|
2011-06-06 03:53:30 -06:00
|
|
|
}
|
|
|
|
|
2011-04-02 15:28:58 -06:00
|
|
|
// TODO(rsc): Should os implement its own syscall.WaitStatus
|
|
|
|
// wrapper with the methods, or is exposing the underlying one enough?
|
|
|
|
//
|
|
|
|
// TODO(rsc): Certainly need to have Rusage struct,
|
|
|
|
// since syscall one might have different field types across
|
|
|
|
// different OS.
|
|
|
|
|
|
|
|
// Waitmsg stores the information about an exited process as reported by Wait.
|
|
|
|
type Waitmsg struct {
|
|
|
|
Pid int // The process's id.
|
|
|
|
syscall.WaitStatus // System-dependent status info.
|
|
|
|
Rusage *syscall.Rusage // System-dependent resource usage info.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for process pid to exit or stop, and then returns a
|
2011-11-01 19:49:08 -06:00
|
|
|
// Waitmsg describing its status and an error, if any. The options
|
2011-04-02 15:28:58 -06:00
|
|
|
// (WNOHANG etc.) affect the behavior of the Wait call.
|
|
|
|
// Wait is equivalent to calling FindProcess and then Wait
|
|
|
|
// and Release on the result.
|
2011-11-01 19:49:08 -06:00
|
|
|
func Wait(pid int, options int) (w *Waitmsg, err error) {
|
2011-04-02 15:28:58 -06:00
|
|
|
p, e := FindProcess(pid)
|
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
defer p.Release()
|
|
|
|
return p.Wait(options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert i to decimal string.
|
|
|
|
func itod(i int) string {
|
|
|
|
if i == 0 {
|
|
|
|
return "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
u := uint64(i)
|
|
|
|
if i < 0 {
|
|
|
|
u = -u
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble decimal in reverse order.
|
|
|
|
var b [32]byte
|
|
|
|
bp := len(b)
|
|
|
|
for ; u > 0; u /= 10 {
|
|
|
|
bp--
|
|
|
|
b[bp] = byte(u%10) + '0'
|
|
|
|
}
|
|
|
|
|
|
|
|
if i < 0 {
|
|
|
|
bp--
|
|
|
|
b[bp] = '-'
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(b[bp:])
|
|
|
|
}
|
|
|
|
|
2011-06-20 13:42:17 -06:00
|
|
|
func (w *Waitmsg) String() string {
|
|
|
|
if w == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2011-04-02 15:28:58 -06:00
|
|
|
// TODO(austin) Use signal names when possible?
|
|
|
|
res := ""
|
|
|
|
switch {
|
|
|
|
case w.Exited():
|
|
|
|
res = "exit status " + itod(w.ExitStatus())
|
|
|
|
case w.Signaled():
|
|
|
|
res = "signal " + itod(w.Signal())
|
|
|
|
case w.Stopped():
|
|
|
|
res = "stop signal " + itod(w.StopSignal())
|
|
|
|
if w.StopSignal() == syscall.SIGTRAP && w.TrapCause() != 0 {
|
|
|
|
res += " (trap " + itod(w.TrapCause()) + ")"
|
|
|
|
}
|
|
|
|
case w.Continued():
|
|
|
|
res = "continued"
|
|
|
|
}
|
|
|
|
if w.CoreDump() {
|
|
|
|
res += " (core dumped)"
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|