1 /* Wait for process state tests.
2 Copyright (C) 2020-2022 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <stdio.h>
20 #include <sys/wait.h>
21 #include <unistd.h>
22 #include <errno.h>
23
24 #include <support/test-driver.h>
25 #include <support/process_state.h>
26 #include <support/check.h>
27 #include <support/xsignal.h>
28 #include <support/xunistd.h>
29
30 #ifndef WEXITED
31 # define WEXITED 0
32 #endif
33
34 static void
sigusr1_handler(int signo)35 sigusr1_handler (int signo)
36 {
37 }
38
39 static void
test_child(void)40 test_child (void)
41 {
42 xsignal (SIGUSR1, sigusr1_handler);
43
44 raise (SIGSTOP);
45
46 TEST_COMPARE (pause (), -1);
47 TEST_COMPARE (errno, EINTR);
48
49 while (1)
50 asm ("");
51 }
52
53 static int
do_test(void)54 do_test (void)
55 {
56 pid_t pid = xfork ();
57 if (pid == 0)
58 {
59 test_child ();
60 _exit (127);
61 }
62
63 /* Adding process_state_tracing_stop ('t') allows the test to work under
64 trace programs such as ptrace. */
65 enum support_process_state stop_state = support_process_state_stopped
66 | support_process_state_tracing_stop;
67
68 if (test_verbose)
69 printf ("info: waiting pid %d, state_stopped/state_tracing_stop\n",
70 (int) pid);
71 support_process_state_wait (pid, stop_state);
72
73 if (kill (pid, SIGCONT) != 0)
74 FAIL_RET ("kill (%d, SIGCONT): %m\n", pid);
75
76 if (test_verbose)
77 printf ("info: waiting pid %d, state_sleeping\n", (int) pid);
78 support_process_state_wait (pid, support_process_state_sleeping);
79
80 if (kill (pid, SIGUSR1) != 0)
81 FAIL_RET ("kill (%d, SIGUSR1): %m\n", pid);
82
83 if (test_verbose)
84 printf ("info: waiting pid %d, state_running\n", (int) pid);
85 support_process_state_wait (pid, support_process_state_running);
86
87 if (kill (pid, SIGKILL) != 0)
88 FAIL_RET ("kill (%d, SIGKILL): %m\n", pid);
89
90 if (test_verbose)
91 printf ("info: waiting pid %d, state_zombie\n", (int) pid);
92 support_process_state_wait (pid, support_process_state_zombie);
93
94 siginfo_t info;
95 int r = waitid (P_PID, pid, &info, WEXITED);
96 TEST_COMPARE (r, 0);
97 TEST_COMPARE (info.si_signo, SIGCHLD);
98 TEST_COMPARE (info.si_code, CLD_KILLED);
99 TEST_COMPARE (info.si_status, SIGKILL);
100 TEST_COMPARE (info.si_pid, pid);
101
102 return 0;
103 }
104
105 #include <support/test-driver.c>
106