Debian Codegraph @

src/syscall/exec_libc2.go

raw
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.
4
5 //go:build darwin || (openbsd && !mips64)
6
7 package syscall
8
9 import (
10 "internal/abi"
11 "runtime"
12 "unsafe"
13 )
14
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.
22 Setpgid bool
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.
27 Setctty bool
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.
35 Foreground bool
36 Pgid int // Child's process group ID if Setpgid.
37 }
38
39 // Implemented in runtime package.
40 func runtime_BeforeFork()
41 func runtime_AfterFork()
42 func runtime_AfterForkInChild()
43
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 nosplit
52 // functions that do not grow the stack and are not race
53 // instrumented (go:norace).
54 //
55 //go:norace
56 func forkAndExecInChild(argv0 *byte, argv, envv []*byte, chroot, dir *byte, attr *ProcAttr, sys *SysProcAttr, pipe int) (pid int, err1 Errno) {
57 // Declare all variables at top in case any
58 // declarations require heap allocation (e.g., err1).
59 var (
60 r1 uintptr
61 nextfd int
62 i int
63 pgrp _C_int
64 cred *Credential
65 ngroups, groups uintptr
66 )
67
68 rlim := origRlimitNofile.Load()
69
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) {
77 nextfd = int(ufd)
78 }
79 fd[i] = int(ufd)
80 }
81 nextfd++
82
83 // About to call fork.
84 // No more allocation or calls of non-assembly functions.
85 runtime_BeforeFork()
86 r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fork_trampoline), 0, 0, 0)
87 if err1 != 0 {
88 runtime_AfterFork()
89 return 0, err1
90 }
91
92 if r1 != 0 {
93 // parent; return PID
94 runtime_AfterFork()
95 return int(r1), 0
96 }
97
98 // Fork succeeded, now in child.
99
100 // Enable tracing if requested.
101 if sys.Ptrace {
102 if runtime.GOOS == "ios" {
103 err1 = ENOSYS
104 goto childerror
105 }
106 _, _, err1 = rawSyscall6(abi.FuncPCABI0(libc_ptrace_trampoline), PTRACE_TRACEME, 0, 0, 0, 0, 0)
107 if err1 != 0 {
108 goto childerror
109 }
110 }
111
112 // Session ID
113 if sys.Setsid {
114 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setsid_trampoline), 0, 0, 0)
115 if err1 != 0 {
116 goto childerror
117 }
118 }
119
120 // Set process group
121 if sys.Setpgid || sys.Foreground {
122 // Place child in process group.
123 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setpgid_trampoline), 0, uintptr(sys.Pgid), 0)
124 if err1 != 0 {
125 goto childerror
126 }
127 }
128
129 if sys.Foreground {
130 // This should really be pid_t, however _C_int (aka int32) is
131 // generally equivalent.
132 pgrp = _C_int(sys.Pgid)
133 if pgrp == 0 {
134 r1, _, err1 = rawSyscall(abi.FuncPCABI0(libc_getpid_trampoline), 0, 0, 0)
135 if err1 != 0 {
136 goto childerror
137 }
138 pgrp = _C_int(r1)
139 }
140
141 // Place process group in foreground.
142 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSPGRP), uintptr(unsafe.Pointer(&pgrp)))
143 if err1 != 0 {
144 goto childerror
145 }
146 }
147
148 // Restore the signal mask. We do this after TIOCSPGRP to avoid
149 // having the kernel send a SIGTTOU signal to the process group.
150 runtime_AfterForkInChild()
151
152 // Chroot
153 if chroot != nil {
154 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chroot_trampoline), uintptr(unsafe.Pointer(chroot)), 0, 0)
155 if err1 != 0 {
156 goto childerror
157 }
158 }
159
160 // User and groups
161 if cred = sys.Credential; cred != nil {
162 ngroups = uintptr(len(cred.Groups))
163 groups = uintptr(0)
164 if ngroups > 0 {
165 groups = uintptr(unsafe.Pointer(&cred.Groups[0]))
166 }
167 if !cred.NoSetGroups {
168 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgroups_trampoline), ngroups, groups, 0)
169 if err1 != 0 {
170 goto childerror
171 }
172 }
173 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setgid_trampoline), uintptr(cred.Gid), 0, 0)
174 if err1 != 0 {
175 goto childerror
176 }
177 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_setuid_trampoline), uintptr(cred.Uid), 0, 0)
178 if err1 != 0 {
179 goto childerror
180 }
181 }
182
183 // Chdir
184 if dir != nil {
185 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_chdir_trampoline), uintptr(unsafe.Pointer(dir)), 0, 0)
186 if err1 != 0 {
187 goto childerror
188 }
189 }
190
191 // Pass 1: look for fd[i] < i and move those up above len(fd)
192 // so that pass 2 won't stomp on an fd it needs later.
193 if pipe < nextfd {
194 if runtime.GOOS == "openbsd" {
195 _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), O_CLOEXEC)
196 } else {
197 _, _, err1 = rawSyscall(dupTrampoline, uintptr(pipe), uintptr(nextfd), 0)
198 if err1 != 0 {
199 goto childerror
200 }
201 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
202 }
203 if err1 != 0 {
204 goto childerror
205 }
206 pipe = nextfd
207 nextfd++
208 }
209 for i = 0; i < len(fd); i++ {
210 if fd[i] >= 0 && fd[i] < i {
211 if nextfd == pipe { // don't stomp on pipe
212 nextfd++
213 }
214 if runtime.GOOS == "openbsd" {
215 _, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), O_CLOEXEC)
216 } else {
217 _, _, err1 = rawSyscall(dupTrampoline, uintptr(fd[i]), uintptr(nextfd), 0)
218 if err1 != 0 {
219 goto childerror
220 }
221 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(nextfd), F_SETFD, FD_CLOEXEC)
222 }
223 if err1 != 0 {
224 goto childerror
225 }
226 fd[i] = nextfd
227 nextfd++
228 }
229 }
230
231 // Pass 2: dup fd[i] down onto i.
232 for i = 0; i < len(fd); i++ {
233 if fd[i] == -1 {
234 rawSyscall(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
235 continue
236 }
237 if fd[i] == i {
238 // dup2(i, i) won't clear close-on-exec flag on Linux,
239 // probably not elsewhere either.
240 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_fcntl_trampoline), uintptr(fd[i]), F_SETFD, 0)
241 if err1 != 0 {
242 goto childerror
243 }
244 continue
245 }
246 // The new fd is created NOT close-on-exec,
247 // which is exactly what we want.
248 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_dup2_trampoline), uintptr(fd[i]), uintptr(i), 0)
249 if err1 != 0 {
250 goto childerror
251 }
252 }
253
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(abi.FuncPCABI0(libc_close_trampoline), uintptr(i), 0, 0)
260 }
261
262 // Detach fd 0 from tty
263 if sys.Noctty {
264 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), 0, uintptr(TIOCNOTTY), 0)
265 if err1 != 0 {
266 goto childerror
267 }
268 }
269
270 // Set the controlling TTY to Ctty
271 if sys.Setctty {
272 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_ioctl_trampoline), uintptr(sys.Ctty), uintptr(TIOCSCTTY), 0)
273 if err1 != 0 {
274 goto childerror
275 }
276 }
277
278 // Restore original rlimit.
279 if rlim != nil {
280 rawSyscall(abi.FuncPCABI0(libc_setrlimit_trampoline), uintptr(RLIMIT_NOFILE), uintptr(unsafe.Pointer(rlim)), 0)
281 }
282
283 // Time to exec.
284 _, _, err1 = rawSyscall(abi.FuncPCABI0(libc_execve_trampoline),
285 uintptr(unsafe.Pointer(argv0)),
286 uintptr(unsafe.Pointer(&argv[0])),
287 uintptr(unsafe.Pointer(&envv[0])))
288
289 childerror:
290 // send error code on pipe
291 rawSyscall(abi.FuncPCABI0(libc_write_trampoline), uintptr(pipe), uintptr(unsafe.Pointer(&err1)), unsafe.Sizeof(err1))
292 for {
293 rawSyscall(abi.FuncPCABI0(libc_exit_trampoline), 253, 0, 0)
294 }
295 }
296
297 // forkAndExecFailureCleanup cleans up after an exec failure.
298 func forkAndExecFailureCleanup(attr *ProcAttr, sys *SysProcAttr) {
299 // Nothing to do.
300 }