1
0
mirror of https://github.com/golang/go synced 2024-11-13 20:00:23 -07:00
go/src/pkg/os/exec_posix.go
Russ Cox 35586f718c 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 13:52:37 -05:00

124 lines
2.9 KiB
Go

// 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.
// +build darwin freebsd linux netbsd openbsd windows
package os
import (
"syscall"
)
// StartProcess starts a new process with the program, arguments and attributes
// specified by name, argv and attr.
//
// StartProcess is a low-level interface. The os/exec package provides
// higher-level interfaces.
//
// If there is an error, it will be of type *PathError.
func StartProcess(name string, argv []string, attr *ProcAttr) (p *Process, err error) {
sysattr := &syscall.ProcAttr{
Dir: attr.Dir,
Env: attr.Env,
Sys: attr.Sys,
}
if sysattr.Env == nil {
sysattr.Env = Environ()
}
for _, f := range attr.Files {
sysattr.Files = append(sysattr.Files, f.Fd())
}
pid, h, e := syscall.StartProcess(name, argv, sysattr)
if e != nil {
return nil, &PathError{"fork/exec", name, e}
}
return newProcess(pid, h), nil
}
// Kill causes the Process to exit immediately.
func (p *Process) Kill() error {
return p.Signal(Kill)
}
// 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
// Waitmsg describing its status and an error, if any. The options
// (WNOHANG etc.) affect the behavior of the Wait call.
// Wait is equivalent to calling FindProcess and then Wait
// and Release on the result.
func Wait(pid int, options int) (w *Waitmsg, err error) {
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:])
}
func (w *Waitmsg) String() string {
if w == nil {
return "<nil>"
}
// TODO(austin) Use signal names when possible?
res := ""
switch {
case w.Exited():
res = "exit status " + itod(w.ExitStatus())
case w.Signaled():
res = "signal " + itod(int(w.Signal()))
case w.Stopped():
res = "stop signal " + itod(int(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
}