arm: Fix ICE on arm_mve.h pragma without MVE types [PR117408]
[official-gcc.git] / libgo / go / syscall / exec_freebsd.go
blob8e8ecb7e98910f7cf44fd01c73a88bfaf036cd0a
1 // Copyright 2021 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 package syscall
7 import (
8 "runtime"
9 "unsafe"
12 //sysnb raw_procctl(idtype int, id int64, cmd int, data unsafe.Pointer) (ret int, err Errno)
13 //procctl(idtype _C_int, id int64, cmd int, data unsafe.Pointer) _C_int
15 type SysProcAttr struct {
16 Chroot string // Chroot.
17 Credential *Credential // Credential.
18 Ptrace bool // Enable tracing.
19 Setsid bool // Create session.
20 // Setpgid sets the process group ID of the child to Pgid,
21 // or, if Pgid == 0, to the new child's process ID.
22 Setpgid bool
23 // Setctty sets the controlling terminal of the child to
24 // file descriptor Ctty. Ctty must be a descriptor number
25 // in the child process: an index into ProcAttr.Files.
26 // This is only meaningful if Setsid is true.
27 Setctty bool
28 Noctty bool // Detach fd 0 from controlling terminal
29 Ctty int // Controlling TTY fd
30 // Foreground places the child process group in the foreground.
31 // This implies Setpgid. The Ctty field must be set to
32 // the descriptor of the controlling TTY.
33 // Unlike Setctty, in this case Ctty must be a descriptor
34 // number in the parent process.
35 Foreground bool
36 Pgid int // Child's process group ID if Setpgid.
37 Pdeathsig Signal // Signal that the process will get when its parent dies (Linux and FreeBSD only)
40 const (
41 _P_PID = 0
43 _PROC_PDEATHSIG_CTL = 11
46 // Implemented in runtime package.
47 func runtime_BeforeFork()
48 func runtime_AfterFork()
49 func runtime_AfterForkInChild()
51 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
52 // If a dup or exec fails, write the errno error to pipe.
53 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
54 // In the child, this function must not acquire any locks, because
55 // they might have been locked at the time of the fork. This means
56 // no rescheduling, no malloc calls, and no new stack segments.
57 // For the same reason compiler does not race instrument it.
58 // The calls to RawSyscall are okay because they are assembly
59 // functions that do not grow the stack.
61 //go:norace
62 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err Errno) {
63 // Declare all variables at top in case any
64 // declarations require heap allocation (e.g., err1).
65 var (
66 r1 Pid_t
67 err1 Errno
68 nextfd int
69 i int
72 // Record parent PID so child can test if it has died.
73 ppid := raw_getpid()
75 // guard against side effects of shuffling fds below.
76 // Make sure that nextfd is beyond any currently open files so
77 // that we can't run the risk of overwriting any of them.
78 fd := make([]int, len(attr.Files))
79 nextfd = len(attr.Files)
80 for i, ufd := range attr.Files {
81 if nextfd < int(ufd) {
82 nextfd = int(ufd)
84 fd[i] = int(ufd)
86 nextfd++
88 // About to call fork.
89 // No more allocation or calls of non-assembly functions.
90 runtime_BeforeFork()
91 r1, err1 = raw_fork()
92 if err1 != 0 {
93 runtime_AfterFork()
94 return 0, err1
97 if r1 != 0 {
98 // parent; return PID
99 runtime_AfterFork()
100 return int(r1), 0
103 // Fork succeeded, now in child.
105 // Enable tracing if requested.
106 if sys.Ptrace {
107 err1 = raw_ptrace(_PTRACE_TRACEME, 0, 0, 0)
108 if err1 != 0 {
109 goto childerror
113 // Session ID
114 if sys.Setsid {
115 err1 = raw_setsid()
116 if err1 != 0 {
117 goto childerror
121 // Set process group
122 if sys.Setpgid || sys.Foreground {
123 // Place child in process group.
124 err1 = raw_setpgid(0, sys.Pgid)
125 if err1 != 0 {
126 goto childerror
130 if sys.Foreground {
131 pgrp := Pid_t(sys.Pgid)
132 if pgrp == 0 {
133 pgrp = raw_getpid()
136 // Place process group in foreground.
137 err1 = raw_ioctl(sys.Ctty, TIOCSPGRP, unsafe.Pointner(&pgrp))
138 if err1 != 0 {
139 goto childerror
143 // Restore the signal mask. We do this after TIOCSPGRP to avoid
144 // having the kernel send a SIGTTOU signal to the process group.
145 runtime_AfterForkInChild()
147 // Chroot
148 if chroot != nil {
149 err1 = raw_chroot(chroot)
150 if err1 != 0 {
151 goto childerror
155 // User and groups
156 if cred := sys.Credential; cred != nil {
157 ngroups := len(cred.Groups)
158 var groups *Gid_t
159 if ngroups > 0 {
160 gids := make([]Gid_t, ngroups)
161 for i, v := range cred.Groups {
162 gids[i] = Gid_t(v)
164 groups = &gids[0]
167 if !cred.NoSetGroups {
168 err1 = raw_setgroups(ngroups, groups)
169 if err1 != 0 {
170 goto childerror
173 err2 := Setgid(int(cred.Gid))
174 if err2 != nil {
175 err1 = err2.(Errno)
176 goto childerror
178 err2 = Setuid(int(cred.Uid))
179 if err2 != nil {
180 err1 = err2.(Errno)
181 goto childerror
186 // Chdir
187 if dir != nil {
188 err1 = raw_chdir(dir)
189 if err1 != 0 {
190 goto childerror
194 // Parent death signal
195 if sys.Pdeathsig != 0 {
196 _, err1 = raw_procctl(_P_PID, 0, _PROC_PDEATHSIG_CTL, unsafe.Pointer(&sys.Pdeathsig))
198 if err1 != 0 {
199 goto childerror
202 // Signal self if parent is already dead. This might cause a
203 // duplicate signal in rare cases, but it won't matter when
204 // using SIGKILL.
205 r1 = raw_getppid()
206 if r1 != ppid {
207 pid := raw_getpid()
208 err1 = raw_kill(pid, sys.Pdeathsig)
209 if err1 != 0 {
210 goto childerror
215 // Pass 1: look for fd[i] < i and move those up above len(fd)
216 // so that pass 2 won't stomp on an fd it needs later.
217 if pipe < nextfd {
218 _, err1 = raw_fcntl(pipe, _F_DUP2FD_CLOEXEC, nextfd)
219 if err1 != 0 {
220 goto childerror
222 pipe = nextfd
223 nextfd++
225 for i = 0; i < len(fd); i++ {
226 if fd[i] >= 0 && fd[i] < int(i) {
227 if nextfd == pipe { // don't stomp on pipe
228 nextfd++
230 err1 = raw_fcntl(fd[i], _F_DUP2FD_CLOEXEC, nextfd)
231 if err1 != 0 {
232 goto childerror
234 fd[i] = nextfd
235 nextfd++
239 // Pass 2: dup fd[i] down onto i.
240 for i = 0; i < len(fd); i++ {
241 if fd[i] == -1 {
242 raw_close(i)
243 continue
245 if fd[i] == int(i) {
246 // dup2(i, i) won't clear close-on-exec flag on Linux,
247 // probably not elsewhere either.
248 _, err1 = raw_fcntl(fd[i], _F_SETFD, 0)
249 if err1 != 0 {
250 goto childerror
252 continue
254 // The new fd is created NOT close-on-exec,
255 // which is exactly what we want.
256 err1 = raw_dup2(fd[i], i)
257 if err1 != 0 {
258 goto childerror
262 // By convention, we don't close-on-exec the fds we are
263 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
264 // Programs that know they inherit fds >= 3 will need
265 // to set them close-on-exec.
266 for i = len(fd); i < 3; i++ {
267 raw_close(i)
270 // Detach fd 0 from tty
271 if sys.Noctty {
272 _, err = raw_ioctl(0, TIOCNOTTY, 0)
273 if err1 != 0 {
274 goto childerror
278 // Set the controlling TTY to Ctty
279 if sys.Setctty {
280 _, err1 = raw_ioctl(sys.Cty, TIOCSCTTY, 0)
281 if err1 != 0 {
282 goto childerror
286 // Time to exec.
287 err1 = raw_execve(argv0, &argv[0], &envv[0])
289 childerror:
290 // send error code on pipe
291 raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
292 for {
293 raw_exit(253)