2009-02-15 20:35:52 -07: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.
|
|
|
|
|
|
|
|
package os
|
|
|
|
|
|
|
|
import (
|
|
|
|
"syscall";
|
|
|
|
)
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// ForkExec forks the current process and invokes Exec with the file, arguments,
|
|
|
|
// and environment specified by argv0, argv, and envv. It returns the process
|
|
|
|
// id of the forked process and an Error, if any. The fd array specifies the
|
|
|
|
// file descriptors to be set up in the new process: fd[0] will be Unix file
|
|
|
|
// descriptor 0 (standard input), fd[1] descriptor 1, and so on. A nil entry
|
|
|
|
// will cause the child to have no open file descriptor with that index.
|
2009-05-15 11:32:05 -06:00
|
|
|
// If dir is not empty, the child chdirs into the directory before execing the program.
|
2009-10-06 20:40:35 -06:00
|
|
|
func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File) (pid int, err Error) {
|
2009-02-15 20:35:52 -07:00
|
|
|
// Create array of integer (system) fds.
|
2009-06-01 23:14:39 -06:00
|
|
|
intfd := make([]int, len(fd));
|
2009-06-24 21:12:50 -06:00
|
|
|
for i, f := range fd {
|
2009-02-15 20:35:52 -07:00
|
|
|
if f == nil {
|
|
|
|
intfd[i] = -1;
|
|
|
|
} else {
|
|
|
|
intfd[i] = f.Fd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-15 11:32:05 -06:00
|
|
|
p, e := syscall.ForkExec(argv0, argv, envv, dir, intfd);
|
2009-06-25 21:24:55 -06:00
|
|
|
if e != 0 {
|
|
|
|
return 0, &PathError{"fork/exec", argv0, Errno(e)};
|
|
|
|
}
|
|
|
|
return p, nil;
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Exec replaces the current process with an execution of the program
|
|
|
|
// named by argv0, with arguments argv and environment envv.
|
|
|
|
// If successful, Exec never returns. If it fails, it returns an Error.
|
|
|
|
// ForkExec is almost always a better way to execute a program.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Exec(argv0 string, argv []string, envv []string) Error {
|
2009-02-15 23:12:35 -07:00
|
|
|
if envv == nil {
|
|
|
|
envv = Environ();
|
|
|
|
}
|
2009-02-15 20:35:52 -07:00
|
|
|
e := syscall.Exec(argv0, argv, envv);
|
2009-06-25 21:24:55 -06:00
|
|
|
if e != 0 {
|
|
|
|
return &PathError{"exec", argv0, Errno(e)};
|
|
|
|
}
|
|
|
|
return nil;
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(rsc): Should os implement its own syscall.WaitStatus
|
|
|
|
// wrapper with the methods, or is exposing the underlying one enough?
|
|
|
|
//
|
2009-08-10 23:02:51 -06:00
|
|
|
// TODO(rsc): Certainly need to have Rusage struct,
|
2009-02-15 20:35:52 -07:00
|
|
|
// since syscall one might have different field types across
|
|
|
|
// different OS.
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Waitmsg stores the information about an exited process as reported by Wait.
|
2009-02-15 20:35:52 -07:00
|
|
|
type Waitmsg struct {
|
2009-10-06 20:40:35 -06:00
|
|
|
Pid int; // The process's id.
|
2009-03-07 17:56:44 -07:00
|
|
|
syscall.WaitStatus; // System-dependent status info.
|
2009-10-06 20:40:35 -06:00
|
|
|
Rusage *syscall.Rusage; // System-dependent resource usage info.
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// Options for Wait.
|
2009-02-15 20:35:52 -07:00
|
|
|
const (
|
2009-10-06 20:40:35 -06:00
|
|
|
WNOHANG = syscall.WNOHANG; // Don't wait if no process has exited.
|
|
|
|
WSTOPPED = syscall.WSTOPPED; // If set, status of stopped subprocesses is also reported.
|
|
|
|
WUNTRACED = WSTOPPED;
|
|
|
|
WRUSAGE = 1<<20; // Record resource usage.
|
2009-02-15 20:35:52 -07:00
|
|
|
)
|
|
|
|
|
2009-07-13 20:28:00 -06:00
|
|
|
// WRUSAGE must not be too high a bit, to avoid clashing with Linux's
|
|
|
|
// WCLONE, WALL, and WNOTHREAD flags, which sit in the top few bits of
|
|
|
|
// the options
|
|
|
|
|
2009-03-07 17:56:44 -07:00
|
|
|
// 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.
|
2009-06-01 23:14:39 -06:00
|
|
|
func Wait(pid int, options int) (w *Waitmsg, err Error) {
|
2009-02-15 20:35:52 -07:00
|
|
|
var status syscall.WaitStatus;
|
|
|
|
var rusage *syscall.Rusage;
|
2009-10-06 20:40:35 -06:00
|
|
|
if options&WRUSAGE != 0 {
|
2009-02-15 20:35:52 -07:00
|
|
|
rusage = new(syscall.Rusage);
|
|
|
|
options ^= WRUSAGE;
|
|
|
|
}
|
2009-06-01 23:14:39 -06:00
|
|
|
pid1, e := syscall.Wait4(pid, &status, options, rusage);
|
2009-02-15 20:35:52 -07:00
|
|
|
if e != 0 {
|
2009-06-25 21:24:55 -06:00
|
|
|
return nil, NewSyscallError("wait", e);
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
w = new(Waitmsg);
|
2009-06-25 21:24:55 -06:00
|
|
|
w.Pid = pid1;
|
2009-02-15 20:35:52 -07:00
|
|
|
w.WaitStatus = status;
|
|
|
|
w.Rusage = rusage;
|
|
|
|
return w, nil;
|
|
|
|
}
|
|
|
|
|
2009-07-15 10:58:42 -06:00
|
|
|
// Convert i to decimal string.
|
|
|
|
func itod(i int) string {
|
|
|
|
if i == 0 {
|
2009-10-06 20:40:35 -06:00
|
|
|
return "0";
|
2009-07-15 10:58:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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--;
|
2009-10-06 20:40:35 -06:00
|
|
|
b[bp] = byte(u%10) + '0';
|
2009-07-15 10:58:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if i < 0 {
|
|
|
|
bp--;
|
2009-10-06 20:40:35 -06:00
|
|
|
b[bp] = '-';
|
2009-07-15 10:58:42 -06:00
|
|
|
}
|
|
|
|
|
2009-10-06 20:40:35 -06:00
|
|
|
return string(b[bp:len(b)]);
|
2009-07-15 10:58:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w Waitmsg) String() string {
|
|
|
|
// 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() {
|
2009-10-06 20:40:35 -06:00
|
|
|
res += " (core dumped)";
|
2009-07-15 10:58:42 -06:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2009-05-25 15:38:38 -06:00
|
|
|
// Getpid returns the process id of the caller.
|
|
|
|
func Getpid() int {
|
2009-09-22 08:49:31 -06:00
|
|
|
return syscall.Getpid();
|
2009-05-25 15:38:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getppid returns the process id of the caller's parent.
|
|
|
|
func Getppid() int {
|
2009-09-22 08:49:31 -06:00
|
|
|
return syscall.Getppid();
|
2009-05-25 15:38:38 -06:00
|
|
|
}
|