match.pd: Check trunc_mod vector obtap before folding.
[official-gcc.git] / libgo / go / syscall / exec_bsd.go
blobe631593cbd95ec4f6c01a4b7ac84574f73cca337
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 //go:build aix || darwin || dragonfly || netbsd || openbsd || solaris
7 package syscall
9 import (
10 "runtime"
11 "unsafe"
14 type SysProcAttr struct {
15 Chroot string // Chroot.
16 Credential *Credential // Credential.
17 Ptrace bool // Enable tracing.
18 Setsid bool // Create session.
19 // Setpgid sets the process group ID of the child to Pgid,
20 // or, if Pgid == 0, to the new child's process ID.
21 Setpgid bool
22 // Setctty sets the controlling terminal of the child to
23 // file descriptor Ctty. Ctty must be a descriptor number
24 // in the child process: an index into ProcAttr.Files.
25 // This is only meaningful if Setsid is true.
26 Setctty bool
27 Noctty bool // Detach fd 0 from controlling terminal
28 Ctty int // Controlling TTY fd
29 // Foreground places the child process group in the foreground.
30 // This implies Setpgid. The Ctty field must be set to
31 // the descriptor of the controlling TTY.
32 // Unlike Setctty, in this case Ctty must be a descriptor
33 // number in the parent process.
34 Foreground bool
35 Pgid int // Child's process group ID if Setpgid.
38 // Implemented in runtime package.
39 func runtime_BeforeFork()
40 func runtime_AfterFork()
41 func runtime_AfterForkInChild()
43 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
44 // If a dup or exec fails, write the errno error to pipe.
45 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
46 // In the child, this function must not acquire any locks, because
47 // they might have been locked at the time of the fork. This means
48 // no rescheduling, no malloc calls, and no new stack segments.
49 // For the same reason compiler does not race instrument it.
50 // The calls to RawSyscall are okay because they are assembly
51 // functions that do not grow the stack.
53 //go:norace
54 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
55 // Declare all variables at top in case any
56 // declarations require heap allocation (e.g., err1).
57 var (
58 r1 Pid_t
59 err1 Errno
60 nextfd int
61 i int
64 // guard against side effects of shuffling fds below.
65 // Make sure that nextfd is beyond any currently open files so
66 // that we can't run the risk of overwriting any of them.
67 fd := make([]int, len(attr.Files))
68 nextfd = len(attr.Files)
69 for i, ufd := range attr.Files {
70 if nextfd < int(ufd) {
71 nextfd = int(ufd)
73 fd[i] = int(ufd)
75 nextfd++
77 // About to call fork.
78 // No more allocation or calls of non-assembly functions.
79 runtime_BeforeFork()
80 r1, err1 = raw_fork()
81 if err1 != 0 {
82 runtime_AfterFork()
83 return 0, err1
86 if r1 != 0 {
87 // parent; return PID
88 runtime_AfterFork()
89 return int(r1), 0
92 // Fork succeeded, now in child.
94 // Enable tracing if requested.
95 if sys.Ptrace {
96 err1 = raw_ptrace(_PTRACE_TRACEME, 0, 0, 0)
97 if err1 != 0 {
98 goto childerror
102 // Session ID
103 if sys.Setsid {
104 err1 = raw_setsid()
105 if err1 != 0 {
106 goto childerror
110 // Set process group
111 if sys.Setpgid || sys.Foreground {
112 // Place child in process group.
113 err1 = raw_setpgid(0, sys.Pgid)
114 if err1 != 0 {
115 goto childerror
119 if sys.Foreground {
120 pgrp := Pid_t(sys.Pgid)
121 if pgrp == 0 {
122 pgrp = raw_getpid()
125 // Place process group in foreground.
126 _, err1 = raw_ioctl_ptr(sys.Ctty, TIOCSPGRP, unsafe.Pointer(&pgrp))
127 if err1 != 0 {
128 goto childerror
132 // Restore the signal mask. We do this after TIOCSPGRP to avoid
133 // having the kernel send a SIGTTOU signal to the process group.
134 runtime_AfterForkInChild()
136 // Chroot
137 if chroot != nil {
138 err1 = raw_chroot(chroot)
139 if err1 != 0 {
140 goto childerror
144 // User and groups
145 if cred := sys.Credential; cred != nil {
146 ngroups := len(cred.Groups)
147 var groups unsafe.Pointer
148 if ngroups > 0 {
149 gids := make([]Gid_t, ngroups)
150 for i, v := range cred.Groups {
151 gids[i] = Gid_t(v)
153 groups = unsafe.Pointer(&gids[0])
155 if !cred.NoSetGroups {
156 err1 = raw_setgroups(ngroups, groups)
157 if err1 != 0 {
158 goto childerror
161 err2 := Setgid(int(cred.Gid))
162 if err2 != nil {
163 err1 = err2.(Errno)
164 goto childerror
166 err2 = Setuid(int(cred.Uid))
167 if err2 != nil {
168 err1 = err2.(Errno)
169 goto childerror
173 // Chdir
174 if dir != nil {
175 err1 = raw_chdir(dir)
176 if err1 != 0 {
177 goto childerror
181 // Pass 1: look for fd[i] < i and move those up above len(fd)
182 // so that pass 2 won't stomp on an fd it needs later.
183 if pipe < nextfd {
184 switch runtime.GOOS {
185 case "netbsd":
186 err1 = raw_dup3(pipe, nextfd, O_CLOEXEC)
187 if err1 != 0 {
188 goto childerror
190 default:
191 err1 = raw_dup2(pipe, nextfd)
192 if err1 != 0 {
193 goto childerror
195 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
197 pipe = nextfd
198 nextfd++
200 for i = 0; i < len(fd); i++ {
201 if fd[i] >= 0 && fd[i] < int(i) {
202 if nextfd == pipe { // don't stomp on pipe
203 nextfd++
205 switch runtime.GOOS {
206 case "netbsd":
207 err1 = raw_dup3(fd[i], nextfd, O_CLOEXEC)
208 if err1 != 0 {
209 goto childerror
211 default:
212 err1 = raw_dup2(fd[i], nextfd)
213 if err1 != 0 {
214 goto childerror
216 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
218 fd[i] = nextfd
219 nextfd++
223 // Pass 2: dup fd[i] down onto i.
224 for i = 0; i < len(fd); i++ {
225 if fd[i] == -1 {
226 raw_close(i)
227 continue
229 if fd[i] == int(i) {
230 // dup2(i, i) won't clear close-on-exec flag on Linux,
231 // probably not elsewhere either.
232 _, err1 = raw_fcntl(fd[i], F_SETFD, 0)
233 if err1 != 0 {
234 goto childerror
236 continue
238 // The new fd is created NOT close-on-exec,
239 // which is exactly what we want.
240 err1 = raw_dup2(fd[i], i)
241 if err1 != 0 {
242 goto childerror
246 // By convention, we don't close-on-exec the fds we are
247 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
248 // Programs that know they inherit fds >= 3 will need
249 // to set them close-on-exec.
250 for i = len(fd); i < 3; i++ {
251 raw_close(i)
254 // Detach fd 0 from tty
255 if sys.Noctty {
256 _, err1 = raw_ioctl(0, TIOCNOTTY, 0)
257 if err1 != 0 {
258 goto childerror
262 // Set the controlling TTY to Ctty
263 if sys.Setctty {
264 if TIOCSCTTY == 0 {
265 err1 = ENOSYS
266 goto childerror
268 _, err1 = raw_ioctl(sys.Ctty, TIOCSCTTY, 0)
269 if err1 != 0 {
270 goto childerror
274 // Time to exec.
275 err1 = raw_execve(argv0, &argv[0], &envv[0])
277 childerror:
278 // send error code on pipe
279 raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
280 for {
281 raw_exit(253)