1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // The exec package runs external commands.
20 // A Cmd represents a running command.
21 // Stdin, Stdout, and Stderr are Files representing pipes
22 // connected to the running command's standard input, output, and error,
23 // or else nil, depending on the arguments to Run.
24 // Pid is the running command's operating system process ID.
32 // Given mode (DevNull, etc), return file for child
33 // and file to record in Cmd structure.
34 func modeToFiles(mode, fd int) (*os.File, *os.File, os.Error) {
41 f, err := os.Open(os.DevNull, rw, 0)
46 return os.Stdin, nil, nil
48 return os.Stdout, nil, nil
50 return os.Stderr, nil, nil
53 r, w, err := os.Pipe()
62 return nil, nil, os.EINVAL
65 // Run starts the named binary running with
66 // arguments argv and environment envv.
67 // It returns a pointer to a new Cmd representing
68 // the command or an error.
70 // The parameters stdin, stdout, and stderr
71 // specify how to handle standard input, output, and error.
72 // The choices are DevNull (connect to /dev/null),
73 // PassThrough (connect to the current process's standard stream),
74 // Pipe (connect to an operating system pipe), and
75 // MergeWithStdout (only for standard error; use the same
76 // file descriptor as was used for standard output).
77 // If a parameter is Pipe, then the corresponding field (Stdin, Stdout, Stderr)
78 // of the returned Cmd is the other end of the pipe.
79 // Otherwise the field in Cmd is nil.
80 func Run(name string, argv, envv []string, dir string, stdin, stdout, stderr int) (p *Cmd, err os.Error) {
84 if fd[0], p.Stdin, err = modeToFiles(stdin, 0); err != nil {
87 if fd[1], p.Stdout, err = modeToFiles(stdout, 1); err != nil {
90 if stderr == MergeWithStdout {
92 } else if fd[2], p.Stderr, err = modeToFiles(stderr, 2); err != nil {
97 p.Pid, err = os.ForkExec(name, argv, envv, dir, fd[0:])
101 if fd[0] != os.Stdin {
104 if fd[1] != os.Stdout {
107 if fd[2] != os.Stderr && fd[2] != fd[1] {
113 if fd[0] != os.Stdin && fd[0] != nil {
116 if fd[1] != os.Stdout && fd[1] != nil {
119 if fd[2] != os.Stderr && fd[2] != nil && fd[2] != fd[1] {
134 // Wait waits for the running command p,
135 // returning the Waitmsg returned by os.Wait and an error.
136 // The options are passed through to os.Wait.
137 // Setting options to 0 waits for p to exit;
138 // other options cause Wait to return for other
139 // process events; see package os for details.
140 func (p *Cmd) Wait(options int) (*os.Waitmsg, os.Error) {
142 return nil, os.ErrorString("exec: invalid use of Cmd.Wait")
144 w, err := os.Wait(p.Pid, options)
145 if w != nil && (w.Exited() || w.Signaled()) {
151 // Close waits for the running command p to exit,
152 // if it hasn't already, and then closes the non-nil file descriptors
153 // p.Stdin, p.Stdout, and p.Stderr.
154 func (p *Cmd) Close() os.Error {
156 // Loop on interrupt, but
157 // ignore other errors -- maybe
158 // caller has already waited for pid.
160 for err == os.EINTR {
165 // Close the FDs that are still open.
167 if p.Stdin != nil && p.Stdin.Fd() >= 0 {
168 if err1 := p.Stdin.Close(); err1 != nil {
172 if p.Stdout != nil && p.Stdout.Fd() >= 0 {
173 if err1 := p.Stdout.Close(); err1 != nil && err != nil {
177 if p.Stderr != nil && p.Stderr != p.Stdout && p.Stderr.Fd() >= 0 {
178 if err1 := p.Stderr.Close(); err1 != nil && err != nil {