PR target/82112
[official-gcc.git] / libgo / go / syscall / exec_bsd.go
blob80991ec5c5d52ae0bbd79804ee935c13b7c3738f
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 aix darwin dragonfly freebsd netbsd openbsd solaris
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 Pgid, or, if Pgid == 0, to new pid.
19 Setctty bool // Set controlling terminal to fd Ctty
20 Noctty bool // Detach fd 0 from controlling terminal
21 Ctty int // Controlling TTY fd
22 Foreground bool // Place child's process group in foreground. (Implies Setpgid. Uses Ctty as fd of controlling TTY)
23 Pgid int // Child's process group ID if Setpgid.
26 // Implemented in runtime package.
27 func runtime_BeforeFork()
28 func runtime_AfterFork()
30 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
31 // If a dup or exec fails, write the errno error to pipe.
32 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
33 // In the child, this function must not acquire any locks, because
34 // they might have been locked at the time of the fork. This means
35 // no rescheduling, no malloc calls, and no new stack segments.
36 // For the same reason compiler does not race instrument it.
37 // The calls to RawSyscall are okay because they are assembly
38 // functions that do not grow the stack.
39 //go:norace
40 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
41 // Declare all variables at top in case any
42 // declarations require heap allocation (e.g., err1).
43 var (
44 r1 Pid_t
45 err1 Errno
46 nextfd int
47 i int
50 // guard against side effects of shuffling fds below.
51 // Make sure that nextfd is beyond any currently open files so
52 // that we can't run the risk of overwriting any of them.
53 fd := make([]int, len(attr.Files))
54 nextfd = len(attr.Files)
55 for i, ufd := range attr.Files {
56 if nextfd < int(ufd) {
57 nextfd = int(ufd)
59 fd[i] = int(ufd)
61 nextfd++
63 // About to call fork.
64 // No more allocation or calls of non-assembly functions.
65 runtime_BeforeFork()
66 r1, err1 = raw_fork()
67 if err1 != 0 {
68 runtime_AfterFork()
69 return 0, err1
72 if r1 != 0 {
73 // parent; return PID
74 runtime_AfterFork()
75 return int(r1), 0
78 // Fork succeeded, now in child.
80 // Enable tracing if requested.
81 if sys.Ptrace {
82 err1 = raw_ptrace(_PTRACE_TRACEME, 0, nil, nil)
83 if err1 != 0 {
84 goto childerror
88 // Session ID
89 if sys.Setsid {
90 err1 = raw_setsid()
91 if err1 != 0 {
92 goto childerror
96 // Set process group
97 if sys.Setpgid || sys.Foreground {
98 // Place child in process group.
99 err1 = raw_setpgid(0, sys.Pgid)
100 if err1 != 0 {
101 goto childerror
105 if sys.Foreground {
106 pgrp := Pid_t(sys.Pgid)
107 if pgrp == 0 {
108 pgrp = raw_getpid()
111 // Place process group in foreground.
112 _, err1 = raw_ioctl_ptr(sys.Ctty, TIOCSPGRP, unsafe.Pointer(&pgrp))
113 if err1 != 0 {
114 goto childerror
118 // Chroot
119 if chroot != nil {
120 err1 = raw_chroot(chroot)
121 if err1 != 0 {
122 goto childerror
126 // User and groups
127 if cred := sys.Credential; cred != nil {
128 ngroups := len(cred.Groups)
129 if ngroups == 0 {
130 err2 := setgroups(0, nil)
131 if err2 == nil {
132 err1 = 0
133 } else {
134 err1 = err2.(Errno)
136 } else {
137 groups := make([]Gid_t, ngroups)
138 for i, v := range cred.Groups {
139 groups[i] = Gid_t(v)
141 err2 := setgroups(ngroups, &groups[0])
142 if err2 == nil {
143 err1 = 0
144 } else {
145 err1 = err2.(Errno)
148 if err1 != 0 {
149 goto childerror
151 err2 := Setgid(int(cred.Gid))
152 if err2 != nil {
153 err1 = err2.(Errno)
154 goto childerror
156 err2 = Setuid(int(cred.Uid))
157 if err2 != nil {
158 err1 = err2.(Errno)
159 goto childerror
163 // Chdir
164 if dir != nil {
165 err1 = raw_chdir(dir)
166 if err1 != 0 {
167 goto childerror
171 // Pass 1: look for fd[i] < i and move those up above len(fd)
172 // so that pass 2 won't stomp on an fd it needs later.
173 if pipe < nextfd {
174 err1 = raw_dup2(pipe, nextfd)
175 if err1 != 0 {
176 goto childerror
178 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
179 pipe = nextfd
180 nextfd++
182 for i = 0; i < len(fd); i++ {
183 if fd[i] >= 0 && fd[i] < int(i) {
184 if nextfd == pipe { // don't stomp on pipe
185 nextfd++
187 err1 = raw_dup2(fd[i], nextfd)
188 if err1 != 0 {
189 goto childerror
191 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
192 fd[i] = nextfd
193 nextfd++
197 // Pass 2: dup fd[i] down onto i.
198 for i = 0; i < len(fd); i++ {
199 if fd[i] == -1 {
200 raw_close(i)
201 continue
203 if fd[i] == int(i) {
204 // dup2(i, i) won't clear close-on-exec flag on Linux,
205 // probably not elsewhere either.
206 _, err1 = raw_fcntl(fd[i], F_SETFD, 0)
207 if err1 != 0 {
208 goto childerror
210 continue
212 // The new fd is created NOT close-on-exec,
213 // which is exactly what we want.
214 err1 = raw_dup2(fd[i], i)
215 if err1 != 0 {
216 goto childerror
220 // By convention, we don't close-on-exec the fds we are
221 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
222 // Programs that know they inherit fds >= 3 will need
223 // to set them close-on-exec.
224 for i = len(fd); i < 3; i++ {
225 raw_close(i)
228 // Detach fd 0 from tty
229 if sys.Noctty {
230 _, err1 = raw_ioctl(0, TIOCNOTTY, 0)
231 if err1 != 0 {
232 goto childerror
236 // Set the controlling TTY to Ctty
237 if sys.Setctty {
238 if TIOCSCTTY == 0 {
239 err1 = ENOSYS
240 goto childerror
242 _, err1 = raw_ioctl(sys.Ctty, TIOCSCTTY, 0)
243 if err1 != 0 {
244 goto childerror
248 // Time to exec.
249 err1 = raw_execve(argv0, &argv[0], &envv[0])
251 childerror:
252 // send error code on pipe
253 raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
254 for {
255 raw_exit(253)
259 // Try to open a pipe with O_CLOEXEC set on both file descriptors.
260 func forkExecPipe(p []int) error {
261 err := Pipe(p)
262 if err != nil {
263 return err
265 _, err = fcntl(p[0], F_SETFD, FD_CLOEXEC)
266 if err != nil {
267 return err
269 _, err = fcntl(p[1], F_SETFD, FD_CLOEXEC)
270 return err