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
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 // 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
) {
77 // About to call fork.
78 // No more allocation or calls of non-assembly functions.
92 // Fork succeeded, now in child.
94 // Enable tracing if requested.
96 err1
= raw_ptrace(_PTRACE_TRACEME
, 0, 0, 0)
111 if sys
.Setpgid || sys
.Foreground
{
112 // Place child in process group.
113 err1
= raw_setpgid(0, sys
.Pgid
)
120 pgrp
:= Pid_t(sys
.Pgid
)
125 // Place process group in foreground.
126 _
, err1
= raw_ioctl_ptr(sys
.Ctty
, TIOCSPGRP
, unsafe
.Pointer(&pgrp
))
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()
138 err1
= raw_chroot(chroot
)
145 if cred
:= sys
.Credential
; cred
!= nil {
146 ngroups
:= len(cred
.Groups
)
147 var groups unsafe
.Pointer
149 gids
:= make([]Gid_t
, ngroups
)
150 for i
, v
:= range cred
.Groups
{
153 groups
= unsafe
.Pointer(&gids
[0])
155 if !cred
.NoSetGroups
{
156 err1
= raw_setgroups(ngroups
, groups
)
161 err2
:= Setgid(int(cred
.Gid
))
166 err2
= Setuid(int(cred
.Uid
))
175 err1
= raw_chdir(dir
)
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.
184 switch runtime
.GOOS
{
186 err1
= raw_dup3(pipe
, nextfd
, O_CLOEXEC
)
191 err1
= raw_dup2(pipe
, nextfd
)
195 raw_fcntl(nextfd
, F_SETFD
, FD_CLOEXEC
)
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
205 switch runtime
.GOOS
{
207 err1
= raw_dup3(fd
[i
], nextfd
, O_CLOEXEC
)
212 err1
= raw_dup2(fd
[i
], nextfd
)
216 raw_fcntl(nextfd
, F_SETFD
, FD_CLOEXEC
)
223 // Pass 2: dup fd[i] down onto i.
224 for i
= 0; i
< len(fd
); 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)
238 // The new fd is created NOT close-on-exec,
239 // which is exactly what we want.
240 err1
= raw_dup2(fd
[i
], i
)
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
++ {
254 // Detach fd 0 from tty
256 _
, err1
= raw_ioctl(0, TIOCNOTTY
, 0)
262 // Set the controlling TTY to Ctty
268 _
, err1
= raw_ioctl(sys
.Ctty
, TIOCSCTTY
, 0)
275 err1
= raw_execve(argv0
, &argv
[0], &envv
[0])
278 // send error code on pipe
279 raw_write(pipe
, (*byte)(unsafe
.Pointer(&err1
)), int(unsafe
.Sizeof(err1
)))