Debian Codegraph @

libgo/go/syscall/exec_hurd.go

raw
1 // Copyright 2022 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 hurd
6
7 package syscall
8
9 import (
10 "runtime"
11 "unsafe"
12 )
13
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.
21 Setpgid bool
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.
26 Setctty bool
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.
34 Foreground bool
35 Pgid int // Child's process group ID if Setpgid.
36 }
37
38 // Implemented in runtime package.
39 func runtime_BeforeFork()
40 func runtime_AfterFork()
41 func runtime_AfterForkInChild()
42
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.
52 //
53 //go:norace
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).
57 var (
58 r1 Pid_t
59 err1 Errno
60 nextfd int
61 i int
62 )
63
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) {
71 nextfd = int(ufd)
72 }
73 fd[i] = int(ufd)
74 }
75 nextfd++
76
77 // About to call fork.
78 // No more allocation or calls of non-assembly functions.
79 runtime_BeforeFork()
80 r1, err1 = raw_fork()
81 if err1 != 0 {
82 runtime_AfterFork()
83 return 0, err1
84 }
85
86 if r1 != 0 {
87 // parent; return PID
88 runtime_AfterFork()
89 return int(r1), 0
90 }
91
92 // Fork succeeded, now in child.
93
94 // Enable tracing if requested.
95 if sys.Ptrace {
96 err1 = raw_ptrace(_PTRACE_TRACEME, 0, 0, 0)
97 if err1 != 0 {
98 goto childerror
99 }
100 }
101
102 // Session ID
103 if sys.Setsid {
104 err1 = raw_setsid()
105 if err1 != 0 {
106 goto childerror
107 }
108 }
109
110 // Set process group
111 if sys.Setpgid || sys.Foreground {
112 // Place child in process group.
113 err1 = raw_setpgid(0, sys.Pgid)
114 if err1 != 0 {
115 goto childerror
116 }
117 }
118
119 if sys.Foreground {
120 pgrp := Pid_t(sys.Pgid)
121 if pgrp == 0 {
122 pgrp = raw_getpid()
123 }
124
125 // Place process group in foreground.
126 _, err1 = raw_ioctl_ptr(sys.Ctty, TIOCSPGRP, unsafe.Pointer(&pgrp))
127 if err1 != 0 {
128 goto childerror
129 }
130 }
131
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()
135
136 // Chroot
137 if chroot != nil {
138 err1 = raw_chroot(chroot)
139 if err1 != 0 {
140 goto childerror
141 }
142 }
143
144 // User and groups
145 if cred := sys.Credential; cred != nil {
146 ngroups := len(cred.Groups)
147 var groups unsafe.Pointer
148 if ngroups > 0 {
149 groups = unsafe.Pointer(&cred.Groups[0])
150 }
151 if !cred.NoSetGroups {
152 err1 = raw_setgroups(ngroups, groups)
153 if err1 != 0 {
154 goto childerror
155 }
156 }
157 err2 := Setgid(int(cred.Gid))
158 if err2 != nil {
159 err1 = err2.(Errno)
160 goto childerror
161 }
162 err2 = Setuid(int(cred.Uid))
163 if err2 != nil {
164 err1 = err2.(Errno)
165 goto childerror
166 }
167 }
168
169 // Chdir
170 if dir != nil {
171 err1 = raw_chdir(dir)
172 if err1 != 0 {
173 goto childerror
174 }
175 }
176
177 // Pass 1: look for fd[i] < i and move those up above len(fd)
178 // so that pass 2 won't stomp on an fd it needs later.
179 if pipe < nextfd {
180 switch runtime.GOOS {
181 case "netbsd":
182 err1 = raw_dup3(pipe, nextfd, O_CLOEXEC)
183 if err1 != 0 {
184 goto childerror
185 }
186 default:
187 err1 = raw_dup2(pipe, nextfd)
188 if err1 != 0 {
189 goto childerror
190 }
191 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
192 }
193 pipe = nextfd
194 nextfd++
195 }
196 for i = 0; i < len(fd); i++ {
197 if fd[i] >= 0 && fd[i] < int(i) {
198 if nextfd == pipe { // don't stomp on pipe
199 nextfd++
200 }
201 switch runtime.GOOS {
202 case "netbsd":
203 err1 = raw_dup3(fd[i], nextfd, O_CLOEXEC)
204 if err1 != 0 {
205 goto childerror
206 }
207 default:
208 err1 = raw_dup2(fd[i], nextfd)
209 if err1 != 0 {
210 goto childerror
211 }
212 raw_fcntl(nextfd, F_SETFD, FD_CLOEXEC)
213 }
214 fd[i] = nextfd
215 nextfd++
216 }
217 }
218
219 // Pass 2: dup fd[i] down onto i.
220 for i = 0; i < len(fd); i++ {
221 if fd[i] == -1 {
222 raw_close(i)
223 continue
224 }
225 if fd[i] == int(i) {
226 // dup2(i, i) won't clear close-on-exec flag on Linux,
227 // probably not elsewhere either.
228 _, err1 = raw_fcntl(fd[i], F_SETFD, 0)
229 if err1 != 0 {
230 goto childerror
231 }
232 continue
233 }
234 // The new fd is created NOT close-on-exec,
235 // which is exactly what we want.
236 err1 = raw_dup2(fd[i], i)
237 if err1 != 0 {
238 goto childerror
239 }
240 }
241
242 // By convention, we don't close-on-exec the fds we are
243 // started with, so if len(fd) < 3, close 0, 1, 2 as needed.
244 // Programs that know they inherit fds >= 3 will need
245 // to set them close-on-exec.
246 for i = len(fd); i < 3; i++ {
247 raw_close(i)
248 }
249
250 // Detach fd 0 from tty
251 if sys.Noctty {
252 _, err1 = raw_ioctl(0, TIOCNOTTY, 0)
253 if err1 != 0 {
254 goto childerror
255 }
256 }
257
258 // Set the controlling TTY to Ctty
259 if sys.Setctty {
260 if TIOCSCTTY == 0 {
261 err1 = ENOSYS
262 goto childerror
263 }
264 _, err1 = raw_ioctl(sys.Ctty, TIOCSCTTY, 0)
265 if err1 != 0 {
266 goto childerror
267 }
268 }
269
270 // Time to exec.
271 err1 = raw_execve(argv0, &argv[0], &envv[0])
272
273 childerror:
274 // send error code on pipe
275 raw_write(pipe, (*byte)(unsafe.Pointer(&err1)), int(unsafe.Sizeof(err1)))
276 for {
277 raw_exit(253)
278 }
279 }