OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package os | 5 package os |
6 | 6 |
7 import ( | 7 import ( |
8 "syscall" | 8 "syscall" |
9 ) | 9 ) |
10 | 10 |
11 // ForkExec forks the current process and invokes Exec with the file, arguments, | 11 // ForkExec forks the current process and invokes Exec with the program, argumen
ts, |
12 // and environment specified by argv0, argv, and envv. It returns the process | 12 // and environment specified by name, argv, and envv. It returns the process |
13 // id of the forked process and an Error, if any. The fd array specifies the | 13 // id of the forked process and an Error, if any. The fd array specifies the |
14 // file descriptors to be set up in the new process: fd[0] will be Unix file | 14 // file descriptors to be set up in the new process: fd[0] will be Unix file |
15 // descriptor 0 (standard input), fd[1] descriptor 1, and so on. A nil entry | 15 // descriptor 0 (standard input), fd[1] descriptor 1, and so on. A nil entry |
16 // will cause the child to have no open file descriptor with that index. | 16 // will cause the child to have no open file descriptor with that index. |
17 // If dir is not empty, the child chdirs into the directory before execing the p
rogram. | 17 // If dir is not empty, the child chdirs into the directory before execing the p
rogram. |
18 func ForkExec(argv0 string, argv []string, envv []string, dir string, fd []*File
) (pid int, err Error) { | 18 func ForkExec(name string, argv []string, envv []string, dir string, fd []*File)
(pid int, err Error) { |
19 if envv == nil { | 19 if envv == nil { |
20 envv = Environ() | 20 envv = Environ() |
21 } | 21 } |
22 // Create array of integer (system) fds. | 22 // Create array of integer (system) fds. |
23 intfd := make([]int, len(fd)) | 23 intfd := make([]int, len(fd)) |
24 for i, f := range fd { | 24 for i, f := range fd { |
25 if f == nil { | 25 if f == nil { |
26 intfd[i] = -1 | 26 intfd[i] = -1 |
27 } else { | 27 } else { |
28 intfd[i] = f.Fd() | 28 intfd[i] = f.Fd() |
29 } | 29 } |
30 } | 30 } |
31 | 31 |
32 » p, e := syscall.ForkExec(argv0, argv, envv, dir, intfd) | 32 » p, e := syscall.ForkExec(name, argv, envv, dir, intfd) |
33 if e != 0 { | 33 if e != 0 { |
34 » » return 0, &PathError{"fork/exec", argv0, Errno(e)} | 34 » » return 0, &PathError{"fork/exec", name, Errno(e)} |
35 } | 35 } |
36 return p, nil | 36 return p, nil |
37 } | 37 } |
38 | 38 |
39 // Exec replaces the current process with an execution of the program | 39 // Exec replaces the current process with an execution of the |
40 // named by argv0, with arguments argv and environment envv. | 40 // named binary, with arguments argv and environment envv. |
41 // If successful, Exec never returns. If it fails, it returns an Error. | 41 // If successful, Exec never returns. If it fails, it returns an Error. |
42 // ForkExec is almost always a better way to execute a program. | 42 // ForkExec is almost always a better way to execute a program. |
43 func Exec(argv0 string, argv []string, envv []string) Error { | 43 func Exec(name string, argv []string, envv []string) Error { |
44 if envv == nil { | 44 if envv == nil { |
45 envv = Environ() | 45 envv = Environ() |
46 } | 46 } |
47 » e := syscall.Exec(argv0, argv, envv) | 47 » e := syscall.Exec(name, argv, envv) |
48 if e != 0 { | 48 if e != 0 { |
49 » » return &PathError{"exec", argv0, Errno(e)} | 49 » » return &PathError{"exec", name, Errno(e)} |
50 } | 50 } |
51 return nil | 51 return nil |
52 } | 52 } |
53 | 53 |
54 // TODO(rsc): Should os implement its own syscall.WaitStatus | 54 // TODO(rsc): Should os implement its own syscall.WaitStatus |
55 // wrapper with the methods, or is exposing the underlying one enough? | 55 // wrapper with the methods, or is exposing the underlying one enough? |
56 // | 56 // |
57 // TODO(rsc): Certainly need to have Rusage struct, | 57 // TODO(rsc): Certainly need to have Rusage struct, |
58 // since syscall one might have different field types across | 58 // since syscall one might have different field types across |
59 // different OS. | 59 // different OS. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 res += " (core dumped)" | 145 res += " (core dumped)" |
146 } | 146 } |
147 return res | 147 return res |
148 } | 148 } |
149 | 149 |
150 // Getpid returns the process id of the caller. | 150 // Getpid returns the process id of the caller. |
151 func Getpid() int { return syscall.Getpid() } | 151 func Getpid() int { return syscall.Getpid() } |
152 | 152 |
153 // Getppid returns the process id of the caller's parent. | 153 // Getppid returns the process id of the caller's parent. |
154 func Getppid() int { return syscall.Getppid() } | 154 func Getppid() int { return syscall.Getppid() } |
OLD | NEW |