1 // exec.go -- fork/exec syscall support.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Fork, exec, wait, etc.
13 func libc_fcntl(fd int, cmd int, arg int) int __asm__ ("fcntl")
14 func libc_fork() Pid_t __asm__ ("fork")
15 func libc_setsid() Pid_t __asm__ ("setsid")
16 func libc_setpgid(Pid_t, Pid_t) int __asm__ ("setpgid")
17 func libc_chroot(path *byte) int __asm__ ("chroot")
18 func libc_setuid(Uid_t) int __asm__ ("setuid")
19 func libc_setgid(Gid_t) int __asm__ ("setgid")
20 func libc_setgroups(Size_t, *Gid_t) int __asm__ ("setgroups")
21 func libc_chdir(name *byte) int __asm__ ("chdir")
22 func libc_dup2(int, int) int __asm__ ("dup2")
23 func libc_ioctl(int, int) int __asm__ ("ioctl")
24 func libc_execve(*byte, **byte, **byte) int __asm__ ("execve")
25 func libc_sysexit(int) __asm__ ("_exit")
27 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
28 // If a dup or exec fails, write the errno int to pipe.
29 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
30 // In the child, this function must not acquire any locks, because
31 // they might have been locked at the time of the fork. This means
32 // no rescheduling, no malloc calls, and no new stack segments.
33 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err int) {
34 // Declare all variables at top in case any
35 // declarations require heap allocation (e.g., err1).
36 var r1, r2, err1 uintptr
40 // guard against side effects of shuffling fds below.
41 fd := append([]int(nil), attr.Files...)
43 darwin := OS == "darwin"
45 // About to call fork.
46 // No more allocation or calls of non-assembly functions.
57 // Fork succeeded, now in child.
59 // Enable tracing if requested.
61 if libc_ptrace(_PTRACE_TRACEME, 0, 0, nil) < 0 {
68 if libc_setsid() == Pid_t(-1) {
75 if libc_setpgid(0, 0) < 0 {
82 if libc_chroot(chroot) < 0 {
88 if cred := sys.Credential; cred != nil {
89 ngroups := uintptr(len(cred.Groups))
92 groups = (*Gid_t)(unsafe.Pointer(&cred.Groups[0]))
94 if libc_setgroups(Size_t(ngroups), groups) < 0 {
97 if libc_setgid(Gid_t(cred.Gid)) < 0 {
100 if libc_setuid(Uid_t(cred.Uid)) < 0 {
107 if libc_chdir(dir) < 0 {
112 // Pass 1: look for fd[i] < i and move those up above len(fd)
113 // so that pass 2 won't stomp on an fd it needs later.
114 nextfd = int(len(fd))
116 r := libc_dup2(pipe, nextfd)
120 libc_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
124 for i = 0; i < len(fd); i++ {
125 if fd[i] >= 0 && fd[i] < int(i) {
126 r := libc_dup2(fd[i], nextfd)
130 libc_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
133 if nextfd == pipe { // don't stomp on pipe
139 // Pass 2: dup fd[i] down onto i.
140 for i = 0; i < len(fd); i++ {
146 // dup2(i, i) won't clear close-on-exec flag on Linux,
147 // probably not elsewhere either.
148 r := libc_fcntl(fd[i], F_SETFD, 0)
154 // The new fd is created NOT close-on-exec,
155 // which is exactly what we want.
156 r := libc_dup2(fd[i], i)
162 // By convention, we don't close-on-exec the fds we are
163 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
164 // Programs that know they inherit fds >= 3 will need
165 // to set them close-on-exec.
166 for i = len(fd); i < 3; i++ {
170 // Detach fd 0 from tty
172 if libc_ioctl(0, TIOCNOTTY) < 0 {
179 if libc_ioctl(0, TIOCSCTTY) < 0 {
185 libc_execve(argv0, &argv[0], &envv[0])
188 // send error code on pipe
189 var e uintptr = uintptr(GetErrno())
190 libc_write(pipe, (*byte)(unsafe.Pointer(&e)),
191 Size_t(unsafe.Sizeof(err1)))
196 // Calling panic is not actually safe,
197 // but the for loop above won't break
198 // and this shuts up the compiler.
202 // Credential holds user and group identities to be assumed
203 // by a child process started by StartProcess.
204 type Credential struct {
205 Uid uint32 // User ID.
206 Gid uint32 // Group ID.
207 Groups []uint32 // Supplementary group IDs.
210 // ProcAttr holds attributes that will be applied to a new process started
212 type ProcAttr struct {
213 Dir string // Current working directory.
214 Env []string // Environment.
215 Files []int // File descriptors.
219 type SysProcAttr struct {
220 Chroot string // Chroot.
221 Credential *Credential // Credential.
222 Ptrace bool // Enable tracing.
223 Setsid bool // Create session.
224 Setpgid bool // Set process group ID to new pid (SYSV setpgrp)
225 Setctty bool // Set controlling terminal to fd 0
226 Noctty bool // Detach fd 0 from controlling terminal
229 var zeroProcAttr ProcAttr
230 var zeroSysProcAttr SysProcAttr
232 func forkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err int) {
237 var wstatus WaitStatus
244 sys = &zeroSysProcAttr
250 // Convert args to C form.
251 argv0p := StringBytePtr(argv0)
252 argvp := StringArrayPtr(argv)
253 envvp := StringArrayPtr(attr.Env)
255 if OS == "freebsd" && len(argv[0]) > len(argv0) {
260 if sys.Chroot != "" {
261 chroot = StringBytePtr(sys.Chroot)
265 dir = StringBytePtr(attr.Dir)
268 // Acquire the fork lock so that no other threads
269 // create new fds that are not yet close-on-exec
273 // Allocate child status pipe close on exec.
274 if err = Pipe(p[0:]); err != 0 {
277 if _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC); err != 0 {
280 if _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC); err != 0 {
285 pid, err = forkAndExecInChild(argv0p, argvp, envvp, chroot, dir, attr, sys, p[1])
291 // Read child error status from pipe.
293 n = libc_read(p[0], (*byte)(unsafe.Pointer(&err1)),
294 Size_t(unsafe.Sizeof(err1)))
300 if err != 0 || n != 0 {
301 if int(n) == unsafe.Sizeof(err1) {
308 // Child failed; wait for it to exit, to make sure
309 // the zombies don't accumulate.
310 _, err1 := Wait4(pid, &wstatus, 0, nil)
312 _, err1 = Wait4(pid, &wstatus, 0, nil)
317 // Read got EOF, so pipe closed on exec, so exec succeeded.
329 // Combination of fork and exec, careful to be thread safe.
330 func ForkExec(argv0 string, argv []string, attr *ProcAttr) (pid int, err int) {
331 return forkExec(argv0, argv, attr)
334 // StartProcess wraps ForkExec for package os.
335 func StartProcess(argv0 string, argv []string, attr *ProcAttr) (pid, handle int, err int) {
336 pid, err = forkExec(argv0, argv, attr)
341 func Exec(argv0 string, argv []string, envv []string) (err int) {
342 argv_arg := StringArrayPtr(argv)
343 envv_arg := StringArrayPtr(envv)
344 libc_execve(StringBytePtr(argv0), &argv_arg[0], &envv_arg[0])