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 dragonfly || netbsd || (openbsd && mips64)
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.
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.
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.
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.
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).
64 ngroups, groups uintptr
67 rlim := origRlimitNofile.Load()
69 // guard against side effects of shuffling fds below.
70 // Make sure that nextfd is beyond any currently open files so
71 // that we can't run the risk of overwriting any of them.
72 fd := make([]int, len(attr.Files))
73 nextfd = len(attr.Files)
74 for i, ufd := range attr.Files {
75 if nextfd < int(ufd) {
82 // About to call fork.
83 // No more allocation or calls of non-assembly functions.
85 r1, _, err1 = RawSyscall(SYS_FORK, 0, 0, 0)
97 // Fork succeeded, now in child.
99 // Enable tracing if requested.
101 _, _, err1 = RawSyscall(SYS_PTRACE, uintptr(PTRACE_TRACEME), 0, 0)
109 _, _, err1 = RawSyscall(SYS_SETSID, 0, 0, 0)
116 if sys.Setpgid || sys.Foreground {
117 // Place child in process group.
118 _, _, err1 = RawSyscall(SYS_SETPGID, 0, uintptr(sys.Pgid), 0)
125 // This should really be pid_t, however _C_int (aka int32) is
126 // generally equivalent.
127 pgrp = _C_int(sys.Pgid)
129 r1, _, err1 = RawSyscall(SYS_GETPID, 0, 0, 0)
137 // Place process group in foreground.
138 _, _, err1 = RawSyscall(SYS_IOCTL, 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(SYS_CHROOT, 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(SYS_SETGROUPS, ngroups, groups, 0)
169 _, _, err1 = RawSyscall(SYS_SETGID, uintptr(cred.Gid), 0, 0)
173 _, _, err1 = RawSyscall(SYS_SETUID, uintptr(cred.Uid), 0, 0)
181 _, _, err1 = RawSyscall(SYS_CHDIR, 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 == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
191 _, _, err1 = RawSyscall(_SYS_DUP3, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
192 } else if runtime.GOOS == "dragonfly" {
193 _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(pipe), _F_DUP2FD_CLOEXEC, uintptr(nextfd))
195 _, _, err1 = RawSyscall(SYS_DUP2, uintptr(pipe), uintptr(nextfd), 0)
199 _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
207 for i = 0; i < len(fd); i++ {
208 if fd[i] >= 0 && fd[i] < i {
209 if nextfd == pipe { // don't stomp on pipe
212 if runtime.GOOS == "netbsd" || (runtime.GOOS == "openbsd" && runtime.GOARCH == "mips64") {
213 _, _, err1 = RawSyscall(_SYS_DUP3, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
214 } else if runtime.GOOS == "dragonfly" {
215 _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), _F_DUP2FD_CLOEXEC, uintptr(nextfd))
217 _, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(nextfd), 0)
221 _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(nextfd), F_SETFD, FD_CLOEXEC)
231 // Pass 2: dup fd[i] down onto i.
232 for i = 0; i < len(fd); i++ {
234 RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
238 // dup2(i, i) won't clear close-on-exec flag on Linux,
239 // probably not elsewhere either.
240 _, _, err1 = RawSyscall(SYS_FCNTL, uintptr(fd[i]), F_SETFD, 0)
246 // The new fd is created NOT close-on-exec,
247 // which is exactly what we want.
248 _, _, err1 = RawSyscall(SYS_DUP2, uintptr(fd[i]), uintptr(i), 0)
254 // By convention, we don't close-on-exec the fds we are
255 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
256 // Programs that know they inherit fds >= 3 will need
257 // to set them close-on-exec.
258 for i = len(fd); i < 3; i++ {
259 RawSyscall(SYS_CLOSE, uintptr(i), 0, 0)
262 // Detach fd 0 from tty
264 _, _, err1 = RawSyscall(SYS_IOCTL, 0, uintptr(TIOCNOTTY), 0)
270 // Set the controlling TTY to Ctty
272 _, _, err1 = RawSyscall(SYS_IOCTL, uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
278 // Restore original rlimit.
280 RawSyscall(SYS_SETRLIMIT, uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
284 _, _, err1 = RawSyscall(SYS_EXECVE,
285 uintptr(unsafe.Pointer(argv0)),
286 uintptr(unsafe.Pointer(&argv[0])),
287 uintptr(unsafe.Pointer(&envv[0])))
290 // send error code on pipe
291 RawSyscall(SYS_WRITE, uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
293 RawSyscall(SYS_EXIT, 253, 0, 0)