2013-12-05 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libgo / go / syscall / exec_linux.go
blob5d14ec385a4856b62bdd4fa2165f5bb4621f6cc9
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 linux
7 package syscall
9 import (
10 "unsafe"
13 //sysnb raw_prctl(option int, arg2 int, arg3 int, arg4 int, arg5 int) (ret int, err Errno)
14 //prctl(option _C_int, arg2 _C_long, arg3 _C_long, arg4 _C_long, arg5 _C_long) _C_int
16 type SysProcAttr struct {
17 Chroot string // Chroot.
18 Credential *Credential // Credential.
19 Ptrace bool // Enable tracing.
20 Setsid bool // Create session.
21 Setpgid bool // Set process group ID to new pid (SYSV setpgrp)
22 Setctty bool // Set controlling terminal to fd Ctty (only meaningful if Setsid is set)
23 Noctty bool // Detach fd 0 from controlling terminal
24 Ctty int // Controlling TTY fd (Linux only)
25 Pdeathsig Signal // Signal that the process will get when its parent dies (Linux only)
26 Cloneflags uintptr // Flags for clone calls (Linux only)
29 // Implemented in runtime package.
30 func runtime_BeforeFork()
31 func runtime_AfterFork()
33 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
34 // If a dup or exec fails, write the errno error to pipe.
35 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
36 // In the child, this function must not acquire any locks, because
37 // they might have been locked at the time of the fork. This means
38 // no rescheduling, no malloc calls, and no new stack segments.
39 // For the same reason compiler does not race instrument it.
40 // The calls to RawSyscall are okay because they are assembly
41 // functions that do not grow the stack.
42 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
43 // Declare all variables at top in case any
44 // declarations require heap allocation (e.g., err1).
45 var (
46 r1 Pid_t
47 err1 Errno
48 nextfd int
49 i int
52 // Guard against side effects of shuffling fds below.
53 // Make sure that nextfd is beyond any currently open files so
54 // that we can't run the risk of overwriting any of them.
55 fd := make([]int, len(attr.Files))
56 nextfd = len(attr.Files)
57 for i, ufd := range attr.Files {
58 if nextfd < int(ufd) {
59 nextfd = int(ufd)
61 fd[i] = int(ufd)
63 nextfd++
65 // About to call fork.
66 // No more allocation or calls of non-assembly functions.
67 runtime_BeforeFork()
68 r1, err1 = raw_fork()
69 if err1 != 0 {
70 runtime_AfterFork()
71 return 0, err1
74 if r1 != 0 {
75 // parent; return PID
76 runtime_AfterFork()
77 return int(r1), 0
80 // Fork succeeded, now in child.
82 // Parent death signal
83 if sys.Pdeathsig != 0 {
84 _, err1 = raw_prctl(PR_SET_PDEATHSIG, int(sys.Pdeathsig), 0, 0, 0)
85 if err1 != 0 {
86 goto childerror
89 // Signal self if parent is already dead. This might cause a
90 // duplicate signal in rare cases, but it won't matter when
91 // using SIGKILL.
92 ppid := Getppid()
93 if ppid == 1 {
94 pid = Getpid()
95 err2 := Kill(pid, sys.Pdeathsig)
96 if err2 != nil {
97 err1 = err2.(Errno)
98 goto childerror
103 // Enable tracing if requested.
104 if sys.Ptrace {
105 err1 = raw_ptrace(_PTRACE_TRACEME, 0, nil, nil)
106 if err1 != 0 {
107 goto childerror
111 // Session ID
112 if sys.Setsid {
113 err1 = raw_setsid()
114 if err1 != 0 {
115 goto childerror
119 // Set process group
120 if sys.Setpgid {
121 err1 = raw_setpgid(0, 0)
122 if err1 != 0 {
123 goto childerror
127 // Chroot
128 if chroot != nil {
129 err1 = raw_chroot(chroot)
130 if err1 != 0 {
131 goto childerror
135 // User and groups
136 if cred := sys.Credential; cred != nil {
137 ngroups := len(cred.Groups)
138 if ngroups == 0 {
139 err2 := setgroups(0, nil)
140 if err2 == nil {
141 err1 = 0
142 } else {
143 err1 = err2.(Errno)
145 } else {
146 groups := make([]Gid_t, ngroups)
147 for i, v := range cred.Groups {
148 groups[i] = Gid_t(v)
150 err2 := setgroups(ngroups, &groups[0])
151 if err2 == nil {
152 err1 = 0
153 } else {
154 err1 = err2.(Errno)
157 if err1 != 0 {
158 goto childerror
160 err2 := Setgid(int(cred.Gid))
161 if err2 != nil {
162 err1 = err2.(Errno)
163 goto childerror
165 err2 = Setuid(int(cred.Uid))
166 if err2 != nil {
167 err1 = err2.(Errno)
168 goto childerror
172 // Chdir
173 if dir != nil {
174 err1 = raw_chdir(dir)
175 if err1 != 0 {
176 goto childerror
180 // Pass 1: look for fd[i] < i and move those up above len(fd)
181 // so that pass 2 won't stomp on an fd it needs later.
182 if pipe < nextfd {
183 err1 = raw_dup2(pipe, nextfd)
184 if err1 != 0 {
185 goto childerror
187 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
188 pipe = nextfd
189 nextfd++
191 for i = 0; i < len(fd); i++ {
192 if fd[i] >= 0 && fd[i] < int(i) {
193 err1 = raw_dup2(fd[i], nextfd)
194 if err1 != 0 {
195 goto childerror
197 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
198 fd[i] = nextfd
199 nextfd++
200 if nextfd == pipe { // don't stomp on pipe
201 nextfd++
206 // Pass 2: dup fd[i] down onto i.
207 for i = 0; i < len(fd); i++ {
208 if fd[i] == -1 {
209 raw_close(i)
210 continue
212 if fd[i] == int(i) {
213 // dup2(i, i) won't clear close-on-exec flag on Linux,
214 // probably not elsewhere either.
215 _, err1 = raw_fcntl(fd[i], F_SETFD, 0)
216 if err1 != 0 {
217 goto childerror
219 continue
221 // The new fd is created NOT close-on-exec,
222 // which is exactly what we want.
223 err1 = raw_dup2(fd[i], i)
224 if err1 != 0 {
225 goto childerror
229 // By convention, we don't close-on-exec the fds we are
230 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
231 // Programs that know they inherit fds >= 3 will need
232 // to set them close-on-exec.
233 for i = len(fd); i < 3; i++ {
234 raw_close(i)
237 // Detach fd 0 from tty
238 if sys.Noctty {
239 _, err1 = raw_ioctl(0, TIOCNOTTY, 0)
240 if err1 != 0 {
241 goto childerror
245 // Make fd 0 the tty
246 if sys.Setctty && sys.Ctty >= 0 {
247 _, err1 = raw_ioctl(0, TIOCSCTTY, sys.Ctty)
248 if err1 != 0 {
249 goto childerror
253 // Time to exec.
254 err1 = raw_execve(argv0, &argv[0], &envv[0])
256 childerror:
257 // send error code on pipe
258 raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
259 for {
260 raw_exit(253)
264 // Try to open a pipe with O_CLOEXEC set on both file descriptors.
265 func forkExecPipe(p []int) (err error) {
266 err = Pipe2(p, O_CLOEXEC)
267 // pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it
268 // might not be implemented.
269 if err == ENOSYS {
270 if err = Pipe(p); err != nil {
271 return
273 if _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC); err != nil {
274 return
276 _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC)
278 return