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 (
|
|
|
|
"os";
|
|
|
|
"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.
|
|
|
|
func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File)
|
2009-04-17 01:08:24 -06:00
|
|
|
(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?
|
|
|
|
//
|
|
|
|
// TODO(rsc): Certainly need to have os.Rusage struct,
|
|
|
|
// 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-03-07 17:56:44 -07:00
|
|
|
Pid int; // The process's id.
|
|
|
|
syscall.WaitStatus; // System-dependent status info.
|
|
|
|
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-03-07 17:56:44 -07: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;
|
2009-07-13 20:28:00 -06:00
|
|
|
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;
|
|
|
|
if options & WRUSAGE != 0 {
|
|
|
|
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-05-25 15:38:38 -06:00
|
|
|
// Getpid returns the process id of the caller.
|
|
|
|
func Getpid() int {
|
|
|
|
p, r2, e := syscall.Syscall(syscall.SYS_GETPID, 0, 0, 0);
|
|
|
|
return int(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getppid returns the process id of the caller's parent.
|
|
|
|
func Getppid() int {
|
|
|
|
p, r2, e := syscall.Syscall(syscall.SYS_GETPPID, 0, 0, 0);
|
|
|
|
return int(p)
|
|
|
|
}
|