OSDN Git Service

Update Go compiler, library, and testsuite on gcc 4.7 branch.
[pf3gnuchains/gcc-fork.git] / libgo / go / syscall / exec_bsd.go
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.
4
5 // +build darwin freebsd netbsd openbsd
6
7 package syscall
8
9 import (
10         "unsafe"
11 )
12
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
21 }
22
23 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
24 // If a dup or exec fails, write the errno error to pipe.
25 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
26 // In the child, this function must not acquire any locks, because
27 // they might have been locked at the time of the fork.  This means
28 // no rescheduling, no malloc calls, and no new stack segments.
29 // The calls to RawSyscall are okay because they are assembly
30 // functions that do not grow the stack.
31 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
32         // Declare all variables at top in case any
33         // declarations require heap allocation (e.g., err1).
34         var (
35                 r1     Pid_t
36                 err1   Errno
37                 nextfd int
38                 i      int
39         )
40
41         fd := make([]int, len(attr.Files))
42         for i, ufd := range attr.Files {
43                 fd[i] = int(ufd)
44         }
45
46         // About to call fork.
47         // No more allocation or calls of non-assembly functions.
48         r1, err1 = raw_fork()
49         if err1 != 0 {
50                 return 0, err1
51         }
52
53         if r1 != 0 {
54                 // parent; return PID
55                 return int(r1), 0
56         }
57
58         // Fork succeeded, now in child.
59
60         // Enable tracing if requested.
61         if sys.Ptrace {
62                 err1 = raw_ptrace(_PTRACE_TRACEME, 0, nil, nil)
63                 if err1 != 0 {
64                         goto childerror
65                 }
66         }
67
68         // Session ID
69         if sys.Setsid {
70                 err1 = raw_setsid()
71                 if err1 != 0 {
72                         goto childerror
73                 }
74         }
75
76         // Set process group
77         if sys.Setpgid {
78                 err1 = raw_setpgid(0, 0)
79                 if err1 != 0 {
80                         goto childerror
81                 }
82         }
83
84         // Chroot
85         if chroot != nil {
86                 err1 = raw_chroot(chroot)
87                 if err1 != 0 {
88                         goto childerror
89                 }
90         }
91
92         // User and groups
93         if cred := sys.Credential; cred != nil {
94                 ngroups := len(cred.Groups)
95                 if ngroups == 0 {
96                         err2 := setgroups(0, nil)
97                         if err2 == nil {
98                                 err1 = 0
99                         } else {
100                                 err1 = err2.(Errno)
101                         }
102                 } else {
103                         groups := make([]Gid_t, ngroups)
104                         for i, v := range cred.Groups {
105                                 groups[i] = Gid_t(v)
106                         }
107                         err2 := setgroups(ngroups, &groups[0])
108                         if err2 == nil {
109                                 err1 = 0
110                         } else {
111                                 err1 = err2.(Errno)
112                         }
113                 }
114                 if err1 != 0 {
115                         goto childerror
116                 }
117                 err2 := Setgid(int(cred.Gid))
118                 if err2 != nil {
119                         err1 = err2.(Errno)
120                         goto childerror
121                 }
122                 err2 = Setuid(int(cred.Uid))
123                 if err2 != nil {
124                         err1 = err2.(Errno)
125                         goto childerror
126                 }
127         }
128
129         // Chdir
130         if dir != nil {
131                 err1 = raw_chdir(dir)
132                 if err1 != 0 {
133                         goto childerror
134                 }
135         }
136
137         // Pass 1: look for fd[i] < i and move those up above len(fd)
138         // so that pass 2 won't stomp on an fd it needs later.
139         nextfd = int(len(fd))
140         if pipe < nextfd {
141                 err1 = raw_dup2(pipe, nextfd)
142                 if err1 != 0 {
143                         goto childerror
144                 }
145                 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
146                 pipe = nextfd
147                 nextfd++
148         }
149         for i = 0; i < len(fd); i++ {
150                 if fd[i] >= 0 && fd[i] < int(i) {
151                         err1 = raw_dup2(fd[i], nextfd)
152                         if err1 != 0 {
153                                 goto childerror
154                         }
155                         raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
156                         fd[i] = nextfd
157                         nextfd++
158                         if nextfd == pipe { // don't stomp on pipe
159                                 nextfd++
160                         }
161                 }
162         }
163
164         // Pass 2: dup fd[i] down onto i.
165         for i = 0; i < len(fd); i++ {
166                 if fd[i] == -1 {
167                         raw_close(i)
168                         continue
169                 }
170                 if fd[i] == int(i) {
171                         // dup2(i, i) won't clear close-on-exec flag on Linux,
172                         // probably not elsewhere either.
173                         _, err1 = raw_fcntl(fd[i], F_SETFD, 0)
174                         if err1 != 0 {
175                                 goto childerror
176                         }
177                         continue
178                 }
179                 // The new fd is created NOT close-on-exec,
180                 // which is exactly what we want.
181                 err1 = raw_dup2(fd[i], i)
182                 if err1 != 0 {
183                         goto childerror
184                 }
185         }
186
187         // By convention, we don't close-on-exec the fds we are
188         // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
189         // Programs that know they inherit fds >= 3 will need
190         // to set them close-on-exec.
191         for i = len(fd); i < 3; i++ {
192                 raw_close(i)
193         }
194
195         // Detach fd 0 from tty
196         if sys.Noctty {
197                 _, err1 = raw_ioctl(0, TIOCNOTTY, 0)
198                 if err1 != 0 {
199                         goto childerror
200                 }
201         }
202
203         // Make fd 0 the tty
204         if sys.Setctty {
205                 _, err1 = raw_ioctl(0, TIOCSCTTY, 0)
206                 if err1 != 0 {
207                         goto childerror
208                 }
209         }
210
211         // Time to exec.
212         err1 = raw_execve(argv0, &argv[0], &envv[0])
213
214 childerror:
215         // send error code on pipe
216         raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
217         for {
218                 raw_exit(253)
219         }
220
221         // Calling panic is not actually safe,
222         // but the for loop above won't break
223         // and this shuts up the compiler.
224         panic("unreached")
225 }