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.
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
// The exec package runs external commands.
|
2009-02-15 20:35:52 -07:00
|
|
|
package exec
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os";
|
2009-04-13 17:50:42 -06:00
|
|
|
"strings";
|
2009-02-15 20:35:52 -07:00
|
|
|
)
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
// Arguments to Run.
|
2009-02-15 20:35:52 -07:00
|
|
|
const (
|
|
|
|
DevNull = iota;
|
2009-03-05 14:35:45 -07:00
|
|
|
PassThrough;
|
2009-02-15 20:35:52 -07:00
|
|
|
Pipe;
|
|
|
|
MergeWithStdout;
|
|
|
|
)
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
// A Cmd represents a running command.
|
2009-03-11 13:51:10 -06:00
|
|
|
// Stdin, Stdout, and Stderr are Files representing pipes
|
2009-03-05 14:35:45 -07:00
|
|
|
// connected to the running command's standard input, output, and error,
|
|
|
|
// or else nil, depending on the arguments to Run.
|
|
|
|
// Pid is the running command's operating system process ID.
|
2009-02-15 20:35:52 -07:00
|
|
|
type Cmd struct {
|
2009-03-11 13:51:10 -06:00
|
|
|
Stdin *os.File;
|
|
|
|
Stdout *os.File;
|
|
|
|
Stderr *os.File;
|
2009-02-15 20:35:52 -07:00
|
|
|
Pid int;
|
|
|
|
}
|
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
// Given mode (DevNull, etc), return file for child
|
|
|
|
// and file to record in Cmd structure.
|
2009-04-17 01:08:24 -06:00
|
|
|
func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
|
2009-02-15 20:35:52 -07:00
|
|
|
switch mode {
|
|
|
|
case DevNull:
|
|
|
|
rw := os.O_WRONLY;
|
|
|
|
if fd == 0 {
|
|
|
|
rw = os.O_RDONLY;
|
|
|
|
}
|
|
|
|
f, err := os.Open("/dev/null", rw, 0);
|
|
|
|
return f, nil, err;
|
2009-03-05 14:35:45 -07:00
|
|
|
case PassThrough:
|
2009-02-15 20:35:52 -07:00
|
|
|
switch fd {
|
|
|
|
case 0:
|
|
|
|
return os.Stdin, nil, nil;
|
|
|
|
case 1:
|
|
|
|
return os.Stdout, nil, nil;
|
|
|
|
case 2:
|
|
|
|
return os.Stderr, nil, nil;
|
|
|
|
}
|
|
|
|
case Pipe:
|
|
|
|
r, w, err := os.Pipe();
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err;
|
|
|
|
}
|
|
|
|
if fd == 0 {
|
|
|
|
return r, w, nil;
|
|
|
|
}
|
|
|
|
return w, r, nil;
|
|
|
|
}
|
|
|
|
return nil, nil, os.EINVAL;
|
|
|
|
}
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
// Run starts the binary prog running with
|
|
|
|
// arguments argv and environment envv.
|
|
|
|
// It returns a pointer to a new Cmd representing
|
|
|
|
// the command or an error.
|
|
|
|
//
|
|
|
|
// The parameters stdin, stdout, and stderr
|
|
|
|
// specify how to handle standard input, output, and error.
|
|
|
|
// The choices are DevNull (connect to /dev/null),
|
|
|
|
// PassThrough (connect to the current process's standard stream),
|
|
|
|
// Pipe (connect to an operating system pipe), and
|
|
|
|
// MergeWithStdout (only for standard error; use the same
|
|
|
|
// file descriptor as was used for standard output).
|
|
|
|
// If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
|
|
|
|
// of the returned Cmd is the other end of the pipe.
|
|
|
|
// Otherwise the field in Cmd is nil.
|
2009-04-17 01:08:24 -06:00
|
|
|
func Run(argv0 string, argv, envv []string, stdin, stdout, stderr int) (p *Cmd, err os.Error)
|
2009-02-15 20:35:52 -07:00
|
|
|
{
|
|
|
|
p = new(Cmd);
|
2009-03-11 13:51:10 -06:00
|
|
|
var fd [3]*os.File;
|
2009-02-15 20:35:52 -07:00
|
|
|
|
2009-03-11 13:51:10 -06:00
|
|
|
if fd[0], p.Stdin, err = modeToFiles(stdin, 0); err != nil {
|
2009-02-15 20:35:52 -07:00
|
|
|
goto Error;
|
|
|
|
}
|
2009-03-11 13:51:10 -06:00
|
|
|
if fd[1], p.Stdout, err = modeToFiles(stdout, 1); err != nil {
|
2009-02-15 20:35:52 -07:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
if stderr == MergeWithStdout {
|
|
|
|
p.Stderr = p.Stdout;
|
2009-03-11 13:51:10 -06:00
|
|
|
} else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil {
|
2009-02-15 20:35:52 -07:00
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run command.
|
2009-05-15 11:32:05 -06:00
|
|
|
p.Pid, err = os.ForkExec(argv0, argv, envv, "", &fd);
|
2009-02-15 20:35:52 -07:00
|
|
|
if err != nil {
|
|
|
|
goto Error;
|
|
|
|
}
|
|
|
|
if fd[0] != os.Stdin {
|
|
|
|
fd[0].Close();
|
|
|
|
}
|
|
|
|
if fd[1] != os.Stdout {
|
|
|
|
fd[1].Close();
|
|
|
|
}
|
|
|
|
if fd[2] != os.Stderr && fd[2] != fd[1] {
|
|
|
|
fd[2].Close();
|
|
|
|
}
|
|
|
|
return p, nil;
|
|
|
|
|
|
|
|
Error:
|
|
|
|
if fd[0] != os.Stdin && fd[0] != nil {
|
|
|
|
fd[0].Close();
|
|
|
|
}
|
|
|
|
if fd[1] != os.Stdout && fd[1] != nil {
|
|
|
|
fd[1].Close();
|
|
|
|
}
|
|
|
|
if fd[2] != os.Stderr && fd[2] != nil && fd[2] != fd[1] {
|
|
|
|
fd[2].Close();
|
|
|
|
}
|
|
|
|
if p.Stdin != nil {
|
|
|
|
p.Stdin.Close();
|
|
|
|
}
|
|
|
|
if p.Stdout != nil {
|
|
|
|
p.Stdout.Close();
|
|
|
|
}
|
|
|
|
if p.Stderr != nil {
|
|
|
|
p.Stderr.Close();
|
|
|
|
}
|
|
|
|
return nil, err;
|
|
|
|
}
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
// Wait waits for the running command p,
|
|
|
|
// returning the Waitmsg returned by os.Wait and an error.
|
|
|
|
// The options are passed through to os.Wait.
|
|
|
|
// Setting options to 0 waits for p to exit;
|
|
|
|
// other options cause Wait to return for other
|
|
|
|
// process events; see package os for details.
|
2009-06-01 23:14:39 -06:00
|
|
|
func (p *Cmd) Wait(options int) (*os.Waitmsg, os.Error) {
|
2009-06-25 21:24:55 -06:00
|
|
|
if p.Pid <= 0 {
|
|
|
|
return nil, os.ErrorString("exec: invalid use of Cmd.Wait");
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
w, err := os.Wait(p.Pid, options);
|
|
|
|
if w != nil && (w.Exited() || w.Signaled()) {
|
|
|
|
p.Pid = -1;
|
|
|
|
}
|
|
|
|
return w, err;
|
|
|
|
}
|
|
|
|
|
2009-03-05 14:35:45 -07:00
|
|
|
// Close waits for the running command p to exit,
|
|
|
|
// if it hasn't already, and then closes the non-nil file descriptors
|
|
|
|
// p.Stdin, p.Stdout, and p.Stderr.
|
2009-04-17 01:08:24 -06:00
|
|
|
func (p *Cmd) Close() os.Error {
|
2009-06-25 21:24:55 -06:00
|
|
|
if p.Pid > 0 {
|
2009-02-15 20:35:52 -07:00
|
|
|
// Loop on interrupt, but
|
|
|
|
// ignore other errors -- maybe
|
|
|
|
// caller has already waited for pid.
|
2009-09-15 10:41:59 -06:00
|
|
|
_, err := p.Wait(0);
|
2009-02-15 20:35:52 -07:00
|
|
|
for err == os.EINTR {
|
2009-09-15 10:41:59 -06:00
|
|
|
_, err = p.Wait(0);
|
2009-02-15 20:35:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close the FDs that are still open.
|
2009-04-17 01:08:24 -06:00
|
|
|
var err os.Error;
|
2009-02-15 20:35:52 -07:00
|
|
|
if p.Stdin != nil && p.Stdin.Fd() >= 0 {
|
|
|
|
if err1 := p.Stdin.Close(); err1 != nil {
|
|
|
|
err = err1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.Stdout != nil && p.Stdout.Fd() >= 0 {
|
|
|
|
if err1 := p.Stdout.Close(); err1 != nil && err != nil {
|
|
|
|
err = err1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if p.Stderr != nil && p.Stderr != p.Stdout && p.Stderr.Fd() >= 0 {
|
|
|
|
if err1 := p.Stderr.Close(); err1 != nil && err != nil {
|
|
|
|
err = err1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2009-04-13 20:14:09 -06:00
|
|
|
func canExec(file string) bool{
|
2009-04-13 17:50:42 -06:00
|
|
|
d, err := os.Stat(file);
|
|
|
|
if err != nil {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return d.IsRegular() && d.Permission() & 0111 != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookPath searches for an executable binary named file
|
|
|
|
// in the directories named by the PATH environment variable.
|
|
|
|
// If file contains a slash, it is tried directly and the PATH is not consulted.
|
|
|
|
//
|
|
|
|
// TODO(rsc): Does LookPath belong in os instead?
|
2009-04-17 01:08:24 -06:00
|
|
|
func LookPath(file string) (string, os.Error) {
|
2009-04-13 17:50:42 -06:00
|
|
|
// NOTE(rsc): I wish we could use the Plan 9 behavior here
|
|
|
|
// (only bypass the path if file begins with / or ./ or ../)
|
|
|
|
// but that would not match all the Unix shells.
|
|
|
|
|
|
|
|
if strings.Index(file, "/") >= 0 {
|
2009-04-13 20:14:09 -06:00
|
|
|
if canExec(file) {
|
2009-04-13 17:50:42 -06:00
|
|
|
return file, nil;
|
|
|
|
}
|
|
|
|
return "", os.ENOENT;
|
|
|
|
}
|
2009-06-26 21:28:41 -06:00
|
|
|
pathenv := os.Getenv("PATH");
|
2009-09-15 10:41:59 -06:00
|
|
|
for _, dir := range strings.Split(pathenv, ":", 0) {
|
2009-04-13 17:50:42 -06:00
|
|
|
if dir == "" {
|
|
|
|
// Unix shell semantics: path element "" means "."
|
|
|
|
dir = ".";
|
|
|
|
}
|
2009-04-13 20:14:09 -06:00
|
|
|
if canExec(dir+"/"+file) {
|
2009-04-13 17:50:42 -06:00
|
|
|
return dir+"/"+file, nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "", os.ENOENT;
|
|
|
|
}
|
|
|
|
|