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 darwin || (openbsd && !mips64)
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.
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.
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.
36 Pgid int // Child's process group ID if Setpgid.
39 // Implemented in runtime package.
40 func runtime_BeforeFork()
41 func runtime_AfterFork()
42 func runtime_AfterForkInChild()
44 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
45 // If a dup or exec fails, write the errno error to pipe.
46 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
47 // In the child, this function must not acquire any locks, because
48 // they might have been locked at the time of the fork. This means
49 // no rescheduling, no malloc calls, and no new stack segments.
50 // For the same reason compiler does not race instrument it.
51 // The calls to rawSyscall are okay because they are assembly
52 // functions that do not grow the stack.
55 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) {
56 // Declare all variables at top in case any
57 // declarations require heap allocation (e.g., err1).
65 ngroups, groups uintptr
68 rlim := origRlimitNofile.Load()
70 // guard against side effects of shuffling fds below.
71 // Make sure that nextfd is beyond any currently open files so
72 // that we can't run the risk of overwriting any of them.
73 fd := make([]int, len(attr.Files))
74 nextfd = len(attr.Files)
75 for i, ufd := range attr.Files {
76 if nextfd < int(ufd) {
83 // About to call fork.
84 // No more allocation or calls of non-assembly functions.
86 r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
98 // Fork succeeded, now in child.
100 // Enable tracing if requested.
102 if err = ptrace(PTRACE_TRACEME, 0, 0, 0); err != nil {
110 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setsid_trampoline), 0, 0, 0)
117 if sys.Setpgid || sys.Foreground {
118 // Place child in process group.
119 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setpgid_trampoline), 0, uintptr(sys.Pgid), 0)
126 // This should really be pid_t, however _C_int (aka int32) is
127 // generally equivalent.
128 pgrp = _C_int(sys.Pgid)
130 r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0)
137 // Place process group in foreground.
138 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
144 // Restore the signal mask. We do this after TIOCSPGRP to avoid
145 // having the kernel send a SIGTTOU signal to the process group.
146 runtime_AfterForkInChild()
150 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
157 if cred = sys.Credential; cred != nil {
158 ngroups = uintptr(len(cred.Groups))
161 groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
163 if !cred.NoSetGroups {
164 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgroups_trampoline), ngroups, groups, 0)
169 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgid_trampoline), uintptr(cred.Gid), 0, 0)
173 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setuid_trampoline), uintptr(cred.Uid), 0, 0)
181 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 0, 0)
187 // Pass 1: look for fd[i] < i and move those up above len(fd)
188 // so that pass 2 won't stomp on an fd it needs later.
190 if runtime.GOOS == "openbsd" {
191 _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
193 _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), 0)
197 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
205 for i = 0; i < len(fd); i++ {
206 if fd[i] >= 0 && fd[i] < i {
207 if nextfd == pipe { // don't stomp on pipe
210 if runtime.GOOS == "openbsd" {
211 _, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
213 _, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), 0)
217 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
227 // Pass 2: dup fd[i] down onto i.
228 for i = 0; i < len(fd); i++ {
230 rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
234 // dup2(i, i) won't clear close-on-exec flag on Linux,
235 // probably not elsewhere either.
236 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(fd[i]), F_SETFD, 0)
242 // The new fd is created NOT close-on-exec,
243 // which is exactly what we want.
244 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(fd[i]), uintptr(i), 0)
250 // By convention, we don't close-on-exec the fds we are
251 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
252 // Programs that know they inherit fds >= 3 will need
253 // to set them close-on-exec.
254 for i = len(fd); i < 3; i++ {
255 rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
258 // Detach fd 0 from tty
260 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
266 // Set the controlling TTY to Ctty
268 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
274 // Restore original rlimit.
276 rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
280 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_execve_trampoline),
281 uintptr(unsafe.Pointer(argv0)),
282 uintptr(unsafe.Pointer(&argv[0])),
283 uintptr(unsafe.Pointer(&envv[0])))
286 // send error code on pipe
287 rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
289 rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)