1 |
747 |
jeremybenn |
// 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.
|
4 |
|
|
|
5 |
|
|
// +build darwin freebsd linux netbsd openbsd
|
6 |
|
|
|
7 |
|
|
// Fork, exec, wait, etc.
|
8 |
|
|
|
9 |
|
|
package syscall
|
10 |
|
|
|
11 |
|
|
import (
|
12 |
|
|
"runtime"
|
13 |
|
|
"sync"
|
14 |
|
|
"unsafe"
|
15 |
|
|
)
|
16 |
|
|
|
17 |
|
|
//sysnb raw_fork() (pid Pid_t, err Errno)
|
18 |
|
|
//fork() Pid_t
|
19 |
|
|
|
20 |
|
|
//sysnb raw_setsid() (err Errno)
|
21 |
|
|
//setsid() Pid_t
|
22 |
|
|
|
23 |
|
|
//sysnb raw_setpgid(pid int, pgid int) (err Errno)
|
24 |
|
|
//setpgid(pid Pid_t, pgid Pid_t) int
|
25 |
|
|
|
26 |
|
|
//sysnb raw_chroot(path *byte) (err Errno)
|
27 |
|
|
//chroot(path *byte) int
|
28 |
|
|
|
29 |
|
|
//sysnb raw_chdir(path *byte) (err Errno)
|
30 |
|
|
//chdir(path *byte) int
|
31 |
|
|
|
32 |
|
|
//sysnb raw_fcntl(fd int, cmd int, arg int) (val int, err Errno)
|
33 |
|
|
//fcntl(fd int, cmd int, arg int) int
|
34 |
|
|
|
35 |
|
|
//sysnb raw_close(fd int) (err Errno)
|
36 |
|
|
//close(fd int) int
|
37 |
|
|
|
38 |
|
|
//sysnb raw_ioctl(fd int, cmd int, val int) (rval int, err Errno)
|
39 |
|
|
//ioctl(fd int, cmd int, val int) int
|
40 |
|
|
|
41 |
|
|
//sysnb raw_execve(argv0 *byte, argv **byte, envv **byte) (err Errno)
|
42 |
|
|
//execve(argv0 *byte, argv **byte, envv **byte) int
|
43 |
|
|
|
44 |
|
|
//sysnb raw_write(fd int, buf *byte, count int) (err Errno)
|
45 |
|
|
//write(fd int, buf *byte, count Size_t) Ssize_t
|
46 |
|
|
|
47 |
|
|
//sysnb raw_exit(status int)
|
48 |
|
|
//_exit(status int)
|
49 |
|
|
|
50 |
|
|
//sysnb raw_dup2(oldfd int, newfd int) (err Errno)
|
51 |
|
|
//dup2(oldfd int, newfd int) int
|
52 |
|
|
|
53 |
|
|
// Note: not raw, returns error rather than Errno.
|
54 |
|
|
//sys read(fd int, p *byte, np int) (n int, err error)
|
55 |
|
|
//read(fd int, buf *byte, count Size_t) Ssize_t
|
56 |
|
|
|
57 |
|
|
// Lock synchronizing creation of new file descriptors with fork.
|
58 |
|
|
//
|
59 |
|
|
// We want the child in a fork/exec sequence to inherit only the
|
60 |
|
|
// file descriptors we intend. To do that, we mark all file
|
61 |
|
|
// descriptors close-on-exec and then, in the child, explicitly
|
62 |
|
|
// unmark the ones we want the exec'ed program to keep.
|
63 |
|
|
// Unix doesn't make this easy: there is, in general, no way to
|
64 |
|
|
// allocate a new file descriptor close-on-exec. Instead you
|
65 |
|
|
// have to allocate the descriptor and then mark it close-on-exec.
|
66 |
|
|
// If a fork happens between those two events, the child's exec
|
67 |
|
|
// will inherit an unwanted file descriptor.
|
68 |
|
|
//
|
69 |
|
|
// This lock solves that race: the create new fd/mark close-on-exec
|
70 |
|
|
// operation is done holding ForkLock for reading, and the fork itself
|
71 |
|
|
// is done holding ForkLock for writing. At least, that's the idea.
|
72 |
|
|
// There are some complications.
|
73 |
|
|
//
|
74 |
|
|
// Some system calls that create new file descriptors can block
|
75 |
|
|
// for arbitrarily long times: open on a hung NFS server or named
|
76 |
|
|
// pipe, accept on a socket, and so on. We can't reasonably grab
|
77 |
|
|
// the lock across those operations.
|
78 |
|
|
//
|
79 |
|
|
// It is worse to inherit some file descriptors than others.
|
80 |
|
|
// If a non-malicious child accidentally inherits an open ordinary file,
|
81 |
|
|
// that's not a big deal. On the other hand, if a long-lived child
|
82 |
|
|
// accidentally inherits the write end of a pipe, then the reader
|
83 |
|
|
// of that pipe will not see EOF until that child exits, potentially
|
84 |
|
|
// causing the parent program to hang. This is a common problem
|
85 |
|
|
// in threaded C programs that use popen.
|
86 |
|
|
//
|
87 |
|
|
// Luckily, the file descriptors that are most important not to
|
88 |
|
|
// inherit are not the ones that can take an arbitrarily long time
|
89 |
|
|
// to create: pipe returns instantly, and the net package uses
|
90 |
|
|
// non-blocking I/O to accept on a listening socket.
|
91 |
|
|
// The rules for which file descriptor-creating operations use the
|
92 |
|
|
// ForkLock are as follows:
|
93 |
|
|
//
|
94 |
|
|
// 1) Pipe. Does not block. Use the ForkLock.
|
95 |
|
|
// 2) Socket. Does not block. Use the ForkLock.
|
96 |
|
|
// 3) Accept. If using non-blocking mode, use the ForkLock.
|
97 |
|
|
// Otherwise, live with the race.
|
98 |
|
|
// 4) Open. Can block. Use O_CLOEXEC if available (GNU/Linux).
|
99 |
|
|
// Otherwise, live with the race.
|
100 |
|
|
// 5) Dup. Does not block. Use the ForkLock.
|
101 |
|
|
// On GNU/Linux, could use fcntl F_DUPFD_CLOEXEC
|
102 |
|
|
// instead of the ForkLock, but only for dup(fd, -1).
|
103 |
|
|
|
104 |
|
|
var ForkLock sync.RWMutex
|
105 |
|
|
|
106 |
|
|
// Convert array of string to array
|
107 |
|
|
// of NUL-terminated byte pointer.
|
108 |
|
|
func StringSlicePtr(ss []string) []*byte {
|
109 |
|
|
bb := make([]*byte, len(ss)+1)
|
110 |
|
|
for i := 0; i < len(ss); i++ {
|
111 |
|
|
bb[i] = StringBytePtr(ss[i])
|
112 |
|
|
}
|
113 |
|
|
bb[len(ss)] = nil
|
114 |
|
|
return bb
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
func CloseOnExec(fd int) { fcntl(fd, F_SETFD, FD_CLOEXEC) }
|
118 |
|
|
|
119 |
|
|
func SetNonblock(fd int, nonblocking bool) (err error) {
|
120 |
|
|
flag, err := fcntl(fd, F_GETFL, 0)
|
121 |
|
|
if err != nil {
|
122 |
|
|
return err
|
123 |
|
|
}
|
124 |
|
|
if nonblocking {
|
125 |
|
|
flag |= O_NONBLOCK
|
126 |
|
|
} else {
|
127 |
|
|
flag &= ^O_NONBLOCK
|
128 |
|
|
}
|
129 |
|
|
_, err = fcntl(fd, F_SETFL, flag)
|
130 |
|
|
return err
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
// Credential holds user and group identities to be assumed
|
134 |
|
|
// by a child process started by StartProcess.
|
135 |
|
|
type Credential struct {
|
136 |
|
|
Uid uint32 // User ID.
|
137 |
|
|
Gid uint32 // Group ID.
|
138 |
|
|
Groups []uint32 // Supplementary group IDs.
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
// ProcAttr holds attributes that will be applied to a new process started
|
142 |
|
|
// by StartProcess.
|
143 |
|
|
type ProcAttr struct {
|
144 |
|
|
Dir string // Current working directory.
|
145 |
|
|
Env []string // Environment.
|
146 |
|
|
Files []int // File descriptors.
|
147 |
|
|
Sys *SysProcAttr
|
148 |
|
|
}
|
149 |
|
|
|
150 |
|
|
var zeroProcAttr ProcAttr
|
151 |
|
|
var zeroSysProcAttr SysProcAttr
|
152 |
|
|
|
153 |
|
|
func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error) {
|
154 |
|
|
var p [2]int
|
155 |
|
|
var n int
|
156 |
|
|
var err1 Errno
|
157 |
|
|
var wstatus WaitStatus
|
158 |
|
|
|
159 |
|
|
if attr == nil {
|
160 |
|
|
attr = &zeroProcAttr
|
161 |
|
|
}
|
162 |
|
|
sys := attr.Sys
|
163 |
|
|
if sys == nil {
|
164 |
|
|
sys = &zeroSysProcAttr
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
p[0] = -1
|
168 |
|
|
p[1] = -1
|
169 |
|
|
|
170 |
|
|
// Convert args to C form.
|
171 |
|
|
argv0p := StringBytePtr(argv0)
|
172 |
|
|
argvp := StringSlicePtr(argv)
|
173 |
|
|
envvp := StringSlicePtr(attr.Env)
|
174 |
|
|
|
175 |
|
|
if runtime.GOOS == "freebsd" && len(argv[0]) > len(argv0) {
|
176 |
|
|
argvp[0] = argv0p
|
177 |
|
|
}
|
178 |
|
|
|
179 |
|
|
var chroot *byte
|
180 |
|
|
if sys.Chroot != "" {
|
181 |
|
|
chroot = StringBytePtr(sys.Chroot)
|
182 |
|
|
}
|
183 |
|
|
var dir *byte
|
184 |
|
|
if attr.Dir != "" {
|
185 |
|
|
dir = StringBytePtr(attr.Dir)
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
// Acquire the fork lock so that no other threads
|
189 |
|
|
// create new fds that are not yet close-on-exec
|
190 |
|
|
// before we fork.
|
191 |
|
|
ForkLock.Lock()
|
192 |
|
|
|
193 |
|
|
// Allocate child status pipe close on exec.
|
194 |
|
|
if err = Pipe(p[0:]); err != nil {
|
195 |
|
|
goto error
|
196 |
|
|
}
|
197 |
|
|
if _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC); err != nil {
|
198 |
|
|
goto error
|
199 |
|
|
}
|
200 |
|
|
if _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC); err != nil {
|
201 |
|
|
goto error
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
// Kick off child.
|
205 |
|
|
pid, err1 = forkAndExecInChild(argv0p, argvp, envvp, chroot, dir, attr, sys, p[1])
|
206 |
|
|
if err1 != 0 {
|
207 |
|
|
goto error
|
208 |
|
|
}
|
209 |
|
|
ForkLock.Unlock()
|
210 |
|
|
|
211 |
|
|
// Read child error status from pipe.
|
212 |
|
|
Close(p[1])
|
213 |
|
|
n, err = read(p[0], (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
|
214 |
|
|
Close(p[0])
|
215 |
|
|
if err != nil || n != 0 {
|
216 |
|
|
if n == int(unsafe.Sizeof(err1)) {
|
217 |
|
|
err = Errno(err1)
|
218 |
|
|
}
|
219 |
|
|
if err == nil {
|
220 |
|
|
err = EPIPE
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
// Child failed; wait for it to exit, to make sure
|
224 |
|
|
// the zombies don't accumulate.
|
225 |
|
|
_, err1 := Wait4(pid, &wstatus, 0, nil)
|
226 |
|
|
for err1 == EINTR {
|
227 |
|
|
_, err1 = Wait4(pid, &wstatus, 0, nil)
|
228 |
|
|
}
|
229 |
|
|
return 0, err
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
// Read got EOF, so pipe closed on exec, so exec succeeded.
|
233 |
|
|
return pid, nil
|
234 |
|
|
|
235 |
|
|
error:
|
236 |
|
|
if p[0] >= 0 {
|
237 |
|
|
Close(p[0])
|
238 |
|
|
Close(p[1])
|
239 |
|
|
}
|
240 |
|
|
ForkLock.Unlock()
|
241 |
|
|
return 0, err
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
// Combination of fork and exec, careful to be thread safe.
|
245 |
|
|
func ForkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err error) {
|
246 |
|
|
return forkExec(argv0, argv, attr)
|
247 |
|
|
}
|
248 |
|
|
|
249 |
|
|
// StartProcess wraps ForkExec for package os.
|
250 |
|
|
func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid int, handle uintptr, err error) {
|
251 |
|
|
pid, err = forkExec(argv0, argv, attr)
|
252 |
|
|
return pid, 0, err
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
// Ordinary exec.
|
256 |
|
|
func Exec(argv0 string, argv []string, envv []string) (err error) {
|
257 |
|
|
err1 := raw_execve(StringBytePtr(argv0),
|
258 |
|
|
&StringSlicePtr(argv)[0],
|
259 |
|
|
&StringSlicePtr(envv)[0])
|
260 |
|
|
return Errno(err1)
|
261 |
|
|
}
|