Debian Codegraph @

libgo/go/syscall/exec_unix_test.go

raw
1 // Copyright 2015 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 aix || darwin || dragonfly || freebsd || hurd || linux || netbsd || openbsd || solaris
6
7 package syscall_test
8
9 import (
10 "internal/testenv"
11 "io"
12 "math/rand"
13 "os"
14 "os/exec"
15 "os/signal"
16 "runtime"
17 "syscall"
18 "testing"
19 "time"
20 "unsafe"
21 )
22
23 type command struct {
24 pipe io.WriteCloser
25 proc *exec.Cmd
26 test *testing.T
27 }
28
29 func (c *command) Info() (pid, pgrp int) {
30 pid = c.proc.Process.Pid
31
32 pgrp, err := syscall.Getpgid(pid)
33 if err != nil {
34 c.test.Fatal(err)
35 }
36
37 return
38 }
39
40 func (c *command) Start() {
41 if err := c.proc.Start(); err != nil {
42 c.test.Fatal(err)
43 }
44 }
45
46 func (c *command) Stop() {
47 c.pipe.Close()
48 if err := c.proc.Wait(); err != nil {
49 c.test.Fatal(err)
50 }
51 }
52
53 func create(t *testing.T) *command {
54 testenv.MustHaveExec(t)
55
56 proc := exec.Command("cat")
57 stdin, err := proc.StdinPipe()
58 if err != nil {
59 t.Fatal(err)
60 }
61
62 return &command{stdin, proc, t}
63 }
64
65 func parent() (pid, pgrp int) {
66 return syscall.Getpid(), syscall.Getpgrp()
67 }
68
69 func TestZeroSysProcAttr(t *testing.T) {
70 ppid, ppgrp := parent()
71
72 cmd := create(t)
73
74 cmd.Start()
75 defer cmd.Stop()
76
77 cpid, cpgrp := cmd.Info()
78
79 if cpid == ppid {
80 t.Fatalf("Parent and child have the same process ID")
81 }
82
83 if cpgrp != ppgrp {
84 t.Fatalf("Child is not in parent's process group")
85 }
86 }
87
88 func TestSetpgid(t *testing.T) {
89 ppid, ppgrp := parent()
90
91 cmd := create(t)
92
93 cmd.proc.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
94 cmd.Start()
95 defer cmd.Stop()
96
97 cpid, cpgrp := cmd.Info()
98
99 if cpid == ppid {
100 t.Fatalf("Parent and child have the same process ID")
101 }
102
103 if cpgrp == ppgrp {
104 t.Fatalf("Parent and child are in the same process group")
105 }
106
107 if cpid != cpgrp {
108 t.Fatalf("Child's process group is not the child's process ID")
109 }
110 }
111
112 func TestPgid(t *testing.T) {
113 ppid, ppgrp := parent()
114
115 cmd1 := create(t)
116
117 cmd1.proc.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
118 cmd1.Start()
119 defer cmd1.Stop()
120
121 cpid1, cpgrp1 := cmd1.Info()
122
123 if cpid1 == ppid {
124 t.Fatalf("Parent and child 1 have the same process ID")
125 }
126
127 if cpgrp1 == ppgrp {
128 t.Fatalf("Parent and child 1 are in the same process group")
129 }
130
131 if cpid1 != cpgrp1 {
132 t.Fatalf("Child 1's process group is not its process ID")
133 }
134
135 cmd2 := create(t)
136
137 cmd2.proc.SysProcAttr = &syscall.SysProcAttr{
138 Setpgid: true,
139 Pgid: cpgrp1,
140 }
141 cmd2.Start()
142 defer cmd2.Stop()
143
144 cpid2, cpgrp2 := cmd2.Info()
145
146 if cpid2 == ppid {
147 t.Fatalf("Parent and child 2 have the same process ID")
148 }
149
150 if cpgrp2 == ppgrp {
151 t.Fatalf("Parent and child 2 are in the same process group")
152 }
153
154 if cpid2 == cpgrp2 {
155 t.Fatalf("Child 2's process group is its process ID")
156 }
157
158 if cpid1 == cpid2 {
159 t.Fatalf("Child 1 and 2 have the same process ID")
160 }
161
162 if cpgrp1 != cpgrp2 {
163 t.Fatalf("Child 1 and 2 are not in the same process group")
164 }
165 }
166
167 func TestForeground(t *testing.T) {
168 if runtime.GOOS == "hurd" {
169 t.Skip("skipping; TestForeground: fails on GNU/Hurd")
170 }
171 signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)
172 defer signal.Reset()
173
174 tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
175 if err != nil {
176 t.Skipf("Can't test Foreground. Couldn't open /dev/tty: %s", err)
177 }
178 defer tty.Close()
179
180 fpgrp := syscall.Pid_t(0)
181
182 errno := syscall.Ioctl(tty.Fd(), syscall.TIOCGPGRP, unsafe.Pointer(&fpgrp))
183 if errno != 0 {
184 t.Fatalf("TIOCGPGRP failed with error code: %s", errno)
185 }
186
187 if fpgrp == 0 {
188 t.Fatalf("Foreground process group is zero")
189 }
190
191 ppid, ppgrp := parent()
192
193 cmd := create(t)
194
195 cmd.proc.SysProcAttr = &syscall.SysProcAttr{
196 Ctty: int(tty.Fd()),
197 Foreground: true,
198 }
199 cmd.Start()
200
201 cpid, cpgrp := cmd.Info()
202
203 if cpid == ppid {
204 t.Fatalf("Parent and child have the same process ID")
205 }
206
207 if cpgrp == ppgrp {
208 t.Fatalf("Parent and child are in the same process group")
209 }
210
211 if cpid != cpgrp {
212 t.Fatalf("Child's process group is not the child's process ID")
213 }
214
215 cmd.Stop()
216
217 // This call fails on darwin/arm64. The failure doesn't matter, though.
218 // This is just best effort.
219 syscall.Ioctl(tty.Fd(), syscall.TIOCSPGRP, unsafe.Pointer(&fpgrp))
220 }
221
222 func TestForegroundSignal(t *testing.T) {
223 tty, err := os.OpenFile("/dev/tty", os.O_RDWR, 0)
224 if err != nil {
225 t.Skipf("couldn't open /dev/tty: %s", err)
226 }
227 defer tty.Close()
228
229 // This should really be pid_t, however _C_int (aka int32) is generally
230 // equivalent.
231 fpgrp := int32(0)
232
233 errno := syscall.Ioctl(tty.Fd(), syscall.TIOCGPGRP, unsafe.Pointer(&fpgrp))
234 if errno != 0 {
235 t.Fatalf("TIOCGPGRP failed with error code: %s", errno)
236 }
237
238 if fpgrp == 0 {
239 t.Fatalf("Foreground process group is zero")
240 }
241
242 defer func() {
243 signal.Ignore(syscall.SIGTTIN, syscall.SIGTTOU)
244 syscall.Ioctl(tty.Fd(), syscall.TIOCSPGRP, unsafe.Pointer(&fpgrp))
245 signal.Reset()
246 }()
247
248 ch1 := make(chan os.Signal, 1)
249 ch2 := make(chan bool)
250
251 signal.Notify(ch1, syscall.SIGTTIN, syscall.SIGTTOU)
252 defer signal.Stop(ch1)
253
254 cmd := create(t)
255
256 go func() {
257 cmd.proc.SysProcAttr = &syscall.SysProcAttr{
258 Ctty: int(tty.Fd()),
259 Foreground: true,
260 }
261 cmd.Start()
262 cmd.Stop()
263 close(ch2)
264 }()
265
266 timer := time.NewTimer(30 * time.Second)
267 defer timer.Stop()
268 for {
269 select {
270 case sig := <-ch1:
271 t.Errorf("unexpected signal %v", sig)
272 case <-ch2:
273 // Success.
274 return
275 case <-timer.C:
276 t.Fatal("timed out waiting for child process")
277 }
278 }
279 }
280
281 // Test a couple of cases that SysProcAttr can't handle. Issue 29458.
282 func TestInvalidExec(t *testing.T) {
283 t.Parallel()
284 t.Run("SetCtty-Foreground", func(t *testing.T) {
285 t.Parallel()
286 cmd := create(t)
287 cmd.proc.SysProcAttr = &syscall.SysProcAttr{
288 Setctty: true,
289 Foreground: true,
290 Ctty: 0,
291 }
292 if err := cmd.proc.Start(); err == nil {
293 t.Error("expected error setting both SetCtty and Foreground")
294 }
295 })
296 t.Run("invalid-Ctty", func(t *testing.T) {
297 t.Parallel()
298 cmd := create(t)
299 cmd.proc.SysProcAttr = &syscall.SysProcAttr{
300 Setctty: true,
301 Ctty: 3,
302 }
303 if err := cmd.proc.Start(); err == nil {
304 t.Error("expected error with invalid Ctty value")
305 }
306 })
307 }
308
309 // TestExec is for issue #41702.
310 func TestExec(t *testing.T) {
311 testenv.MustHaveExec(t)
312 cmd := exec.Command(os.Args[0], "-test.run=TestExecHelper")
313 cmd.Env = append(os.Environ(), "GO_WANT_HELPER_PROCESS=2")
314 o, err := cmd.CombinedOutput()
315 if err != nil {
316 t.Errorf("%s\n%v", o, err)
317 }
318 }
319
320 // TestExecHelper is used by TestExec. It does nothing by itself.
321 // In testing on macOS 10.14, this used to fail with
322 // "signal: illegal instruction" more than half the time.
323 func TestExecHelper(t *testing.T) {
324 if os.Getenv("GO_WANT_HELPER_PROCESS") != "2" {
325 return
326 }
327
328 // We don't have to worry about restoring these values.
329 // We are in a child process that only runs this test,
330 // and we are going to call syscall.Exec anyhow.
331 os.Setenv("GO_WANT_HELPER_PROCESS", "3")
332
333 stop := time.Now().Add(time.Second)
334 for i := 0; i < 100; i++ {
335 go func(i int) {
336 r := rand.New(rand.NewSource(int64(i)))
337 for time.Now().Before(stop) {
338 r.Uint64()
339 }
340 }(i)
341 }
342
343 time.Sleep(10 * time.Millisecond)
344
345 argv := []string{os.Args[0], "-test.run=TestExecHelper"}
346 syscall.Exec(os.Args[0], argv, os.Environ())
347
348 t.Error("syscall.Exec returned")
349 }