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.
13 //sysnb raw_prctl(option int, arg2 int, arg3 int, arg4 int, arg5 int) (ret int, err Errno)
14 //prctl(option _C_int, arg2 _C_long, arg3 _C_long, arg4 _C_long, arg5 _C_long) _C_int
16 type SysProcAttr
struct {
17 Chroot
string // Chroot.
18 Credential
*Credential
// Credential.
19 Ptrace
bool // Enable tracing.
20 Setsid
bool // Create session.
21 Setpgid
bool // Set process group ID to new pid (SYSV setpgrp)
22 Setctty
bool // Set controlling terminal to fd Ctty (only meaningful if Setsid is set)
23 Noctty
bool // Detach fd 0 from controlling terminal
24 Ctty
int // Controlling TTY fd (Linux only)
25 Pdeathsig Signal
// Signal that the process will get when its parent dies (Linux only)
26 Cloneflags
uintptr // Flags for clone calls (Linux only)
29 // Implemented in runtime package.
30 func runtime_BeforeFork()
31 func runtime_AfterFork()
33 // Fork, dup fd onto 0..len(fd), and exec(argv0, argvv, envv) in child.
34 // If a dup or exec fails, write the errno error to pipe.
35 // (Pipe is close-on-exec so if exec succeeds, it will be closed.)
36 // In the child, this function must not acquire any locks, because
37 // they might have been locked at the time of the fork. This means
38 // no rescheduling, no malloc calls, and no new stack segments.
39 // For the same reason compiler does not race instrument it.
40 // The calls to RawSyscall are okay because they are assembly
41 // functions that do not grow the stack.
42 func forkAndExecInChild(argv0
*byte, argv
, envv
[]*byte, chroot
, dir
*byte, attr
*ProcAttr
, sys
*SysProcAttr
, pipe
int) (pid
int, err Errno
) {
43 // Declare all variables at top in case any
44 // declarations require heap allocation (e.g., err1).
52 // Guard against side effects of shuffling fds below.
53 // Make sure that nextfd is beyond any currently open files so
54 // that we can't run the risk of overwriting any of them.
55 fd
:= make([]int, len(attr
.Files
))
56 nextfd
= len(attr
.Files
)
57 for i
, ufd
:= range attr
.Files
{
58 if nextfd
< int(ufd
) {
65 // About to call fork.
66 // No more allocation or calls of non-assembly functions.
80 // Fork succeeded, now in child.
82 // Parent death signal
83 if sys
.Pdeathsig
!= 0 {
84 _
, err1
= raw_prctl(PR_SET_PDEATHSIG
, int(sys
.Pdeathsig
), 0, 0, 0)
89 // Signal self if parent is already dead. This might cause a
90 // duplicate signal in rare cases, but it won't matter when
95 err2
:= Kill(pid
, sys
.Pdeathsig
)
103 // Enable tracing if requested.
105 err1
= raw_ptrace(_PTRACE_TRACEME
, 0, nil, nil)
121 err1
= raw_setpgid(0, 0)
129 err1
= raw_chroot(chroot
)
136 if cred
:= sys
.Credential
; cred
!= nil {
137 ngroups
:= len(cred
.Groups
)
139 err2
:= setgroups(0, nil)
146 groups
:= make([]Gid_t
, ngroups
)
147 for i
, v
:= range cred
.Groups
{
150 err2
:= setgroups(ngroups
, &groups
[0])
160 err2
:= Setgid(int(cred
.Gid
))
165 err2
= Setuid(int(cred
.Uid
))
174 err1
= raw_chdir(dir
)
180 // Pass 1: look for fd[i] < i and move those up above len(fd)
181 // so that pass 2 won't stomp on an fd it needs later.
183 err1
= raw_dup2(pipe
, nextfd
)
187 raw_fcntl(nextfd
, F_SETFD
, FD_CLOEXEC
)
191 for i
= 0; i
< len(fd
); i
++ {
192 if fd
[i
] >= 0 && fd
[i
] < int(i
) {
193 err1
= raw_dup2(fd
[i
], nextfd
)
197 raw_fcntl(nextfd
, F_SETFD
, FD_CLOEXEC
)
200 if nextfd
== pipe
{ // don't stomp on pipe
206 // Pass 2: dup fd[i] down onto i.
207 for i
= 0; i
< len(fd
); i
++ {
213 // dup2(i, i) won't clear close-on-exec flag on Linux,
214 // probably not elsewhere either.
215 _
, err1
= raw_fcntl(fd
[i
], F_SETFD
, 0)
221 // The new fd is created NOT close-on-exec,
222 // which is exactly what we want.
223 err1
= raw_dup2(fd
[i
], i
)
229 // By convention, we don't close-on-exec the fds we are
230 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
231 // Programs that know they inherit fds >= 3 will need
232 // to set them close-on-exec.
233 for i
= len(fd
); i
< 3; i
++ {
237 // Detach fd 0 from tty
239 _
, err1
= raw_ioctl(0, TIOCNOTTY
, 0)
246 if sys
.Setctty
&& sys
.Ctty
>= 0 {
247 _
, err1
= raw_ioctl(0, TIOCSCTTY
, sys
.Ctty
)
254 err1
= raw_execve(argv0
, &argv
[0], &envv
[0])
257 // send error code on pipe
258 raw_write(pipe
, (*byte)(unsafe
.Pointer(&err1
)), int(unsafe
.Sizeof(err1
)))
264 // Try to open a pipe with O_CLOEXEC set on both file descriptors.
265 func forkExecPipe(p
[]int) (err error
) {
266 err
= Pipe2(p
, O_CLOEXEC
)
267 // pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it
268 // might not be implemented.
270 if err
= Pipe(p
); err
!= nil {
273 if _
, err
= fcntl(p
[0], F_SETFD
, FD_CLOEXEC
); err
!= nil {
276 _
, err
= fcntl(p
[1], F_SETFD
, FD_CLOEXEC
)