]>
Commit | Line | Data |
---|---|---|
56290d6e | 1 | /* Test case for async-signal-safe fork (with respect to malloc). |
04277e02 | 2 | Copyright (C) 2016-2019 Free Software Foundation, Inc. |
56290d6e FW |
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 License as | |
7 | published by the Free Software Foundation; either version 2.1 of the | |
8 | 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; see the file COPYING.LIB. If | |
17 | not, see <http://www.gnu.org/licenses/>. */ | |
18 | ||
19 | /* This test will fail if the process is multi-threaded because we | |
20 | only have an async-signal-safe fork in the single-threaded case | |
21 | (where we skip acquiring the malloc heap locks). | |
22 | ||
23 | This test only checks async-signal-safety with regards to malloc; | |
24 | other, more rarely-used glibc subsystems could have locks which | |
25 | still make fork unsafe, even in single-threaded processes. */ | |
26 | ||
27 | #include <errno.h> | |
e2cd73a2 | 28 | #include <sched.h> |
56290d6e FW |
29 | #include <signal.h> |
30 | #include <stdbool.h> | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | #include <sys/wait.h> | |
35 | #include <time.h> | |
36 | #include <unistd.h> | |
37 | ||
38 | /* How many malloc objects to keep arond. */ | |
39 | enum { malloc_objects = 1009 }; | |
40 | ||
41 | /* The maximum size of an object. */ | |
42 | enum { malloc_maximum_size = 70000 }; | |
43 | ||
44 | /* How many signals need to be delivered before the test exits. */ | |
45 | enum { signal_count = 1000 }; | |
46 | ||
14699b6e | 47 | static int do_test (void); |
983a9637 | 48 | #define TIMEOUT 100 |
14699b6e FW |
49 | #define TEST_FUNCTION do_test () |
50 | #include "../test-skeleton.c" | |
56290d6e FW |
51 | |
52 | /* Process ID of the subprocess which sends SIGUSR1 signals. */ | |
53 | static pid_t sigusr1_sender_pid; | |
54 | ||
55 | /* Set to 1 if SIGUSR1 is received. Used to detect a signal during | |
56 | malloc/free. */ | |
57 | static volatile sig_atomic_t sigusr1_received; | |
58 | ||
59 | /* Periodically set to 1, to indicate that the process is making | |
60 | progress. Checked by liveness_signal_handler. */ | |
61 | static volatile sig_atomic_t progress_indicator = 1; | |
62 | ||
56290d6e FW |
63 | static void |
64 | sigusr1_handler (int signo) | |
65 | { | |
66 | /* Let the main program make progress, by temporarily suspending | |
67 | signals from the subprocess. */ | |
68 | if (sigusr1_received) | |
69 | return; | |
e2cd73a2 FW |
70 | /* sigusr1_sender_pid might not be initialized in the parent when |
71 | the first SIGUSR1 signal arrives. */ | |
72 | if (sigusr1_sender_pid > 0 && kill (sigusr1_sender_pid, SIGSTOP) != 0) | |
56290d6e FW |
73 | { |
74 | write_message ("error: kill (SIGSTOP)\n"); | |
75 | abort (); | |
76 | } | |
77 | sigusr1_received = 1; | |
78 | ||
79 | /* Perform a fork with a trivial subprocess. */ | |
80 | pid_t pid = fork (); | |
81 | if (pid == -1) | |
82 | { | |
83 | write_message ("error: fork\n"); | |
84 | abort (); | |
85 | } | |
86 | if (pid == 0) | |
87 | _exit (0); | |
88 | int status; | |
89 | int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)); | |
90 | if (ret < 0) | |
91 | { | |
92 | write_message ("error: waitpid\n"); | |
93 | abort (); | |
94 | } | |
95 | if (status != 0) | |
96 | { | |
97 | write_message ("error: unexpected exit status from subprocess\n"); | |
98 | abort (); | |
99 | } | |
100 | } | |
101 | ||
102 | static void | |
103 | liveness_signal_handler (int signo) | |
104 | { | |
105 | if (progress_indicator) | |
106 | progress_indicator = 0; | |
107 | else | |
108 | write_message ("warning: process seems to be stuck\n"); | |
109 | } | |
110 | ||
111 | static void | |
112 | __attribute__ ((noreturn)) | |
113 | signal_sender (int signo, bool sleep) | |
114 | { | |
115 | pid_t target = getppid (); | |
116 | while (true) | |
117 | { | |
118 | if (kill (target, signo) != 0) | |
119 | { | |
120 | dprintf (STDOUT_FILENO, "error: kill: %m\n"); | |
121 | abort (); | |
122 | } | |
123 | if (sleep) | |
124 | usleep (1 * 1000 * 1000); | |
e2cd73a2 FW |
125 | else |
126 | /* Reduce the rate at which we send signals. */ | |
127 | sched_yield (); | |
56290d6e FW |
128 | } |
129 | } | |
130 | ||
131 | static int | |
132 | do_test (void) | |
133 | { | |
134 | struct sigaction action = | |
135 | { | |
136 | .sa_handler = sigusr1_handler, | |
137 | }; | |
138 | sigemptyset (&action.sa_mask); | |
139 | ||
140 | if (sigaction (SIGUSR1, &action, NULL) != 0) | |
141 | { | |
142 | printf ("error: sigaction: %m"); | |
143 | return 1; | |
144 | } | |
145 | ||
146 | action.sa_handler = liveness_signal_handler; | |
147 | if (sigaction (SIGUSR2, &action, NULL) != 0) | |
148 | { | |
149 | printf ("error: sigaction: %m"); | |
150 | return 1; | |
151 | } | |
152 | ||
153 | pid_t sigusr2_sender_pid = fork (); | |
154 | if (sigusr2_sender_pid == 0) | |
155 | signal_sender (SIGUSR2, true); | |
156 | sigusr1_sender_pid = fork (); | |
157 | if (sigusr1_sender_pid == 0) | |
158 | signal_sender (SIGUSR1, false); | |
159 | ||
160 | void *objects[malloc_objects] = {}; | |
161 | unsigned signals = 0; | |
162 | unsigned seed = 1; | |
163 | time_t last_report = 0; | |
164 | while (signals < signal_count) | |
165 | { | |
166 | progress_indicator = 1; | |
167 | int slot = rand_r (&seed) % malloc_objects; | |
168 | size_t size = rand_r (&seed) % malloc_maximum_size; | |
169 | if (kill (sigusr1_sender_pid, SIGCONT) != 0) | |
170 | { | |
171 | printf ("error: kill (SIGCONT): %m\n"); | |
172 | signal (SIGUSR1, SIG_IGN); | |
173 | kill (sigusr1_sender_pid, SIGKILL); | |
174 | kill (sigusr2_sender_pid, SIGKILL); | |
175 | return 1; | |
176 | } | |
177 | sigusr1_received = false; | |
178 | free (objects[slot]); | |
179 | objects[slot] = malloc (size); | |
180 | if (sigusr1_received) | |
181 | { | |
182 | ++signals; | |
183 | time_t current = time (0); | |
184 | if (current != last_report) | |
185 | { | |
186 | printf ("info: SIGUSR1 signal count: %u\n", signals); | |
187 | last_report = current; | |
188 | } | |
189 | } | |
190 | if (objects[slot] == NULL) | |
191 | { | |
192 | printf ("error: malloc: %m\n"); | |
193 | signal (SIGUSR1, SIG_IGN); | |
194 | kill (sigusr1_sender_pid, SIGKILL); | |
195 | kill (sigusr2_sender_pid, SIGKILL); | |
196 | return 1; | |
197 | } | |
198 | } | |
199 | ||
200 | /* Clean up allocations. */ | |
201 | for (int slot = 0; slot < malloc_objects; ++slot) | |
202 | free (objects[slot]); | |
203 | ||
204 | /* Terminate the signal-sending subprocess. The SIGUSR1 handler | |
205 | should no longer run because it uses sigusr1_sender_pid. */ | |
206 | signal (SIGUSR1, SIG_IGN); | |
207 | kill (sigusr1_sender_pid, SIGKILL); | |
208 | kill (sigusr2_sender_pid, SIGKILL); | |
209 | ||
210 | return 0; | |
211 | } |