2013-12-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / syscall / exec_bsd.go
blob217e0c842df0734e696ec29820ea164b4bae4d68
1 // Copyright 2011 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 // +build darwin dragonfly freebsd netbsd openbsd
7 package syscall
9 import (
10 "unsafe"
13 type SysProcAttr struct {
14 Chroot string // Chroot.
15 Credential *Credential // Credential.
16 Ptrace bool // Enable tracing.
17 Setsid bool // Create session.
18 Setpgid bool // Set process group ID to new pid (SYSV setpgrp)
19 Setctty bool // Set controlling terminal to fd 0
20 Noctty bool // Detach fd 0 from controlling terminal
23 // Implemented in runtime package.
24 func runtime_BeforeFork()
25 func runtime_AfterFork()
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 error 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 // For the same reason compiler does not race instrument it.
34 // The calls to RawSyscall are okay because they are assembly
35 // functions that do not grow the stack.
36 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
37 // Declare all variables at top in case any
38 // declarations require heap allocation (e.g., err1).
39 var (
40 r1 Pid_t
41 err1 Errno
42 nextfd int
43 i int
46 // guard against side effects of shuffling fds below.
47 // Make sure that nextfd is beyond any currently open files so
48 // that we can't run the risk of overwriting any of them.
49 fd := make([]int, len(attr.Files))
50 nextfd = len(attr.Files)
51 for i, ufd := range attr.Files {
52 if nextfd < int(ufd) {
53 nextfd = int(ufd)
55 fd[i] = int(ufd)
57 nextfd++
59 // About to call fork.
60 // No more allocation or calls of non-assembly functions.
61 runtime_BeforeFork()
62 r1, err1 = raw_fork()
63 if err1 != 0 {
64 runtime_AfterFork()
65 return 0, err1
68 if r1 != 0 {
69 // parent; return PID
70 runtime_AfterFork()
71 return int(r1), 0
74 // Fork succeeded, now in child.
76 // Enable tracing if requested.
77 if sys.Ptrace {
78 err1 = raw_ptrace(_PTRACE_TRACEME, 0, nil, nil)
79 if err1 != 0 {
80 goto childerror
84 // Session ID
85 if sys.Setsid {
86 err1 = raw_setsid()
87 if err1 != 0 {
88 goto childerror
92 // Set process group
93 if sys.Setpgid {
94 err1 = raw_setpgid(0, 0)
95 if err1 != 0 {
96 goto childerror
100 // Chroot
101 if chroot != nil {
102 err1 = raw_chroot(chroot)
103 if err1 != 0 {
104 goto childerror
108 // User and groups
109 if cred := sys.Credential; cred != nil {
110 ngroups := len(cred.Groups)
111 if ngroups == 0 {
112 err2 := setgroups(0, nil)
113 if err2 == nil {
114 err1 = 0
115 } else {
116 err1 = err2.(Errno)
118 } else {
119 groups := make([]Gid_t, ngroups)
120 for i, v := range cred.Groups {
121 groups[i] = Gid_t(v)
123 err2 := setgroups(ngroups, &groups[0])
124 if err2 == nil {
125 err1 = 0
126 } else {
127 err1 = err2.(Errno)
130 if err1 != 0 {
131 goto childerror
133 err2 := Setgid(int(cred.Gid))
134 if err2 != nil {
135 err1 = err2.(Errno)
136 goto childerror
138 err2 = Setuid(int(cred.Uid))
139 if err2 != nil {
140 err1 = err2.(Errno)
141 goto childerror
145 // Chdir
146 if dir != nil {
147 err1 = raw_chdir(dir)
148 if err1 != 0 {
149 goto childerror
153 // Pass 1: look for fd[i] < i and move those up above len(fd)
154 // so that pass 2 won't stomp on an fd it needs later.
155 if pipe < nextfd {
156 err1 = raw_dup2(pipe, nextfd)
157 if err1 != 0 {
158 goto childerror
160 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
161 pipe = nextfd
162 nextfd++
164 for i = 0; i < len(fd); i++ {
165 if fd[i] >= 0 && fd[i] < int(i) {
166 err1 = raw_dup2(fd[i], nextfd)
167 if err1 != 0 {
168 goto childerror
170 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
171 fd[i] = nextfd
172 nextfd++
173 if nextfd == pipe { // don't stomp on pipe
174 nextfd++
179 // Pass 2: dup fd[i] down onto i.
180 for i = 0; i < len(fd); i++ {
181 if fd[i] == -1 {
182 raw_close(i)
183 continue
185 if fd[i] == int(i) {
186 // dup2(i, i) won't clear close-on-exec flag on Linux,
187 // probably not elsewhere either.
188 _, err1 = raw_fcntl(fd[i], F_SETFD, 0)
189 if err1 != 0 {
190 goto childerror
192 continue
194 // The new fd is created NOT close-on-exec,
195 // which is exactly what we want.
196 err1 = raw_dup2(fd[i], i)
197 if err1 != 0 {
198 goto childerror
202 // By convention, we don't close-on-exec the fds we are
203 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
204 // Programs that know they inherit fds >= 3 will need
205 // to set them close-on-exec.
206 for i = len(fd); i < 3; i++ {
207 raw_close(i)
210 // Detach fd 0 from tty
211 if sys.Noctty {
212 _, err1 = raw_ioctl(0, TIOCNOTTY, 0)
213 if err1 != 0 {
214 goto childerror
218 // Make fd 0 the tty
219 if sys.Setctty {
220 _, err1 = raw_ioctl(0, TIOCSCTTY, 0)
221 if err1 != 0 {
222 goto childerror
226 // Time to exec.
227 err1 = raw_execve(argv0, &argv[0], &envv[0])
229 childerror:
230 // send error code on pipe
231 raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
232 for {
233 raw_exit(253)
237 // Try to open a pipe with O_CLOEXEC set on both file descriptors.
238 func forkExecPipe(p []int) error {
239 err := Pipe(p)
240 if err != nil {
241 return err
243 _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC)
244 if err != nil {
245 return err
247 _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC)
248 return err