|
5 | 5 | package syscall_test
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "bufio" |
9 | 8 | "fmt"
|
10 | 9 | "io"
|
11 | 10 | "io/fs"
|
12 | 11 | "os"
|
13 | 12 | "os/exec"
|
14 |
| - "os/signal" |
15 | 13 | "path/filepath"
|
16 | 14 | "runtime"
|
17 | 15 | "sort"
|
18 | 16 | "strconv"
|
19 | 17 | "strings"
|
20 | 18 | "syscall"
|
21 | 19 | "testing"
|
22 |
| - "time" |
23 | 20 | "unsafe"
|
24 | 21 | )
|
25 | 22 |
|
@@ -153,120 +150,6 @@ func TestMain(m *testing.M) {
|
153 | 150 | os.Exit(m.Run())
|
154 | 151 | }
|
155 | 152 |
|
156 |
| -func TestLinuxDeathSignal(t *testing.T) { |
157 |
| - if os.Getuid() != 0 { |
158 |
| - t.Skip("skipping root only test") |
159 |
| - } |
160 |
| - |
161 |
| - // Copy the test binary to a location that a non-root user can read/execute |
162 |
| - // after we drop privileges |
163 |
| - tempDir, err := os.MkdirTemp("", "TestDeathSignal") |
164 |
| - if err != nil { |
165 |
| - t.Fatalf("cannot create temporary directory: %v", err) |
166 |
| - } |
167 |
| - defer os.RemoveAll(tempDir) |
168 |
| - os.Chmod(tempDir, 0755) |
169 |
| - |
170 |
| - tmpBinary := filepath.Join(tempDir, filepath.Base(os.Args[0])) |
171 |
| - |
172 |
| - src, err := os.Open(os.Args[0]) |
173 |
| - if err != nil { |
174 |
| - t.Fatalf("cannot open binary %q, %v", os.Args[0], err) |
175 |
| - } |
176 |
| - defer src.Close() |
177 |
| - |
178 |
| - dst, err := os.OpenFile(tmpBinary, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755) |
179 |
| - if err != nil { |
180 |
| - t.Fatalf("cannot create temporary binary %q, %v", tmpBinary, err) |
181 |
| - } |
182 |
| - if _, err := io.Copy(dst, src); err != nil { |
183 |
| - t.Fatalf("failed to copy test binary to %q, %v", tmpBinary, err) |
184 |
| - } |
185 |
| - err = dst.Close() |
186 |
| - if err != nil { |
187 |
| - t.Fatalf("failed to close test binary %q, %v", tmpBinary, err) |
188 |
| - } |
189 |
| - |
190 |
| - cmd := exec.Command(tmpBinary) |
191 |
| - cmd.Env = append(os.Environ(), "GO_DEATHSIG_PARENT=1") |
192 |
| - chldStdin, err := cmd.StdinPipe() |
193 |
| - if err != nil { |
194 |
| - t.Fatalf("failed to create new stdin pipe: %v", err) |
195 |
| - } |
196 |
| - chldStdout, err := cmd.StdoutPipe() |
197 |
| - if err != nil { |
198 |
| - t.Fatalf("failed to create new stdout pipe: %v", err) |
199 |
| - } |
200 |
| - cmd.Stderr = os.Stderr |
201 |
| - |
202 |
| - err = cmd.Start() |
203 |
| - defer cmd.Wait() |
204 |
| - if err != nil { |
205 |
| - t.Fatalf("failed to start first child process: %v", err) |
206 |
| - } |
207 |
| - |
208 |
| - chldPipe := bufio.NewReader(chldStdout) |
209 |
| - |
210 |
| - if got, err := chldPipe.ReadString('\n'); got == "start\n" { |
211 |
| - syscall.Kill(cmd.Process.Pid, syscall.SIGTERM) |
212 |
| - |
213 |
| - go func() { |
214 |
| - time.Sleep(5 * time.Second) |
215 |
| - chldStdin.Close() |
216 |
| - }() |
217 |
| - |
218 |
| - want := "ok\n" |
219 |
| - if got, err = chldPipe.ReadString('\n'); got != want { |
220 |
| - t.Fatalf("expected %q, received %q, %v", want, got, err) |
221 |
| - } |
222 |
| - } else { |
223 |
| - t.Fatalf("did not receive start from child, received %q, %v", got, err) |
224 |
| - } |
225 |
| -} |
226 |
| - |
227 |
| -func deathSignalParent() { |
228 |
| - cmd := exec.Command(os.Args[0]) |
229 |
| - cmd.Env = append(os.Environ(), |
230 |
| - "GO_DEATHSIG_PARENT=", |
231 |
| - "GO_DEATHSIG_CHILD=1", |
232 |
| - ) |
233 |
| - cmd.Stdin = os.Stdin |
234 |
| - cmd.Stdout = os.Stdout |
235 |
| - attrs := syscall.SysProcAttr{ |
236 |
| - Pdeathsig: syscall.SIGUSR1, |
237 |
| - // UID/GID 99 is the user/group "nobody" on RHEL/Fedora and is |
238 |
| - // unused on Ubuntu |
239 |
| - Credential: &syscall.Credential{Uid: 99, Gid: 99}, |
240 |
| - } |
241 |
| - cmd.SysProcAttr = &attrs |
242 |
| - |
243 |
| - err := cmd.Start() |
244 |
| - if err != nil { |
245 |
| - fmt.Fprintf(os.Stderr, "death signal parent error: %v\n", err) |
246 |
| - os.Exit(1) |
247 |
| - } |
248 |
| - cmd.Wait() |
249 |
| - os.Exit(0) |
250 |
| -} |
251 |
| - |
252 |
| -func deathSignalChild() { |
253 |
| - c := make(chan os.Signal, 1) |
254 |
| - signal.Notify(c, syscall.SIGUSR1) |
255 |
| - go func() { |
256 |
| - <-c |
257 |
| - fmt.Println("ok") |
258 |
| - os.Exit(0) |
259 |
| - }() |
260 |
| - fmt.Println("start") |
261 |
| - |
262 |
| - buf := make([]byte, 32) |
263 |
| - os.Stdin.Read(buf) |
264 |
| - |
265 |
| - // We expected to be signaled before stdin closed |
266 |
| - fmt.Println("not ok") |
267 |
| - os.Exit(1) |
268 |
| -} |
269 |
| - |
270 | 153 | func TestParseNetlinkMessage(t *testing.T) {
|
271 | 154 | for i, b := range [][]byte{
|
272 | 155 | {103, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 11, 0, 1, 0, 0, 0, 0, 5, 8, 0, 3,
|
|
0 commit comments