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.
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.
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.
37 Pdeathsig Signal
// Signal that the process will get when its parent dies (Linux and FreeBSD only)
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.
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).
72 // Record parent PID so child can test if it has died.
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
) {
88 // About to call fork.
89 // No more allocation or calls of non-assembly functions.
103 // Fork succeeded, now in child.
105 // Enable tracing if requested.
107 err1
= raw_ptrace(_PTRACE_TRACEME
, 0, 0, 0)
122 if sys
.Setpgid || sys
.Foreground
{
123 // Place child in process group.
124 err1
= raw_setpgid(0, sys
.Pgid
)
131 pgrp
:= Pid_t(sys
.Pgid
)
136 // Place process group in foreground.
137 err1
= raw_ioctl(sys
.Ctty
, TIOCSPGRP
, unsafe
.Pointner(&pgrp
))
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()
149 err1
= raw_chroot(chroot
)
156 if cred
:= sys
.Credential
; cred
!= nil {
157 ngroups
:= len(cred
.Groups
)
160 gids
:= make([]Gid_t
, ngroups
)
161 for i
, v
:= range cred
.Groups
{
167 if !cred
.NoSetGroups
{
168 err1
= raw_setgroups(ngroups
, groups
)
173 err2
:= Setgid(int(cred
.Gid
))
178 err2
= Setuid(int(cred
.Uid
))
188 err1
= raw_chdir(dir
)
194 // Parent death signal
195 if sys
.Pdeathsig
!= 0 {
196 _
, err1
= raw_procctl(_P_PID
, 0, _PROC_PDEATHSIG_CTL
, unsafe
.Pointer(&sys
.Pdeathsig
))
202 // Signal self if parent is already dead. This might cause a
203 // duplicate signal in rare cases, but it won't matter when
208 err1
= raw_kill(pid
, sys
.Pdeathsig
)
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.
218 _
, err1
= raw_fcntl(pipe
, _F_DUP2FD_CLOEXEC
, 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
230 err1
= raw_fcntl(fd
[i
], _F_DUP2FD_CLOEXEC
, nextfd
)
239 // Pass 2: dup fd[i] down onto i.
240 for i
= 0; i
< len(fd
); 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)
254 // The new fd is created NOT close-on-exec,
255 // which is exactly what we want.
256 err1
= raw_dup2(fd
[i
], i
)
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
++ {
270 // Detach fd 0 from tty
272 _
, err
= raw_ioctl(0, TIOCNOTTY
, 0)
278 // Set the controlling TTY to Ctty
280 _
, err1
= raw_ioctl(sys
.Cty
, TIOCSCTTY
, 0)
287 err1
= raw_execve(argv0
, &argv
[0], &envv
[0])
290 // send error code on pipe
291 raw_write(pipe
, (*byte)(unsafe
.Pointer(&err1
)), int(unsafe
.Sizeof(err1
)))