]>
Commit | Line | Data |
---|---|---|
56290d6e FW |
1 | /* Test case for async-signal-safe fork (with respect to malloc). |
2 | Copyright (C) 2016 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 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 FW |
47 | static int do_test (void); |
48 | #define TEST_FUNCTION do_test () | |
49 | #include "../test-skeleton.c" | |
56290d6e FW |
50 | |
51 | /* Process ID of the subprocess which sends SIGUSR1 signals. */ | |
52 | static pid_t sigusr1_sender_pid; | |
53 | ||
54 | /* Set to 1 if SIGUSR1 is received. Used to detect a signal during | |
55 | malloc/free. */ | |
56 | static volatile sig_atomic_t sigusr1_received; | |
57 | ||
58 | /* Periodically set to 1, to indicate that the process is making | |
59 | progress. Checked by liveness_signal_handler. */ | |
60 | static volatile sig_atomic_t progress_indicator = 1; | |
61 | ||
56290d6e FW |
62 | static void |
63 | sigusr1_handler (int signo) | |
64 | { | |
65 | /* Let the main program make progress, by temporarily suspending | |
66 | signals from the subprocess. */ | |
67 | if (sigusr1_received) | |
68 | return; | |
e2cd73a2 FW |
69 | /* sigusr1_sender_pid might not be initialized in the parent when |
70 | the first SIGUSR1 signal arrives. */ | |
71 | if (sigusr1_sender_pid > 0 && kill (sigusr1_sender_pid, SIGSTOP) != 0) | |
56290d6e FW |
72 | { |
73 | write_message ("error: kill (SIGSTOP)\n"); | |
74 | abort (); | |
75 | } | |
76 | sigusr1_received = 1; | |
77 | ||
78 | /* Perform a fork with a trivial subprocess. */ | |
79 | pid_t pid = fork (); | |
80 | if (pid == -1) | |
81 | { | |
82 | write_message ("error: fork\n"); | |
83 | abort (); | |
84 | } | |
85 | if (pid == 0) | |
86 | _exit (0); | |
87 | int status; | |
88 | int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)); | |
89 | if (ret < 0) | |
90 | { | |
91 | write_message ("error: waitpid\n"); | |
92 | abort (); | |
93 | } | |
94 | if (status != 0) | |
95 | { | |
96 | write_message ("error: unexpected exit status from subprocess\n"); | |
97 | abort (); | |
98 | } | |
99 | } | |
100 | ||
101 | static void | |
102 | liveness_signal_handler (int signo) | |
103 | { | |
104 | if (progress_indicator) | |
105 | progress_indicator = 0; | |
106 | else | |
107 | write_message ("warning: process seems to be stuck\n"); | |
108 | } | |
109 | ||
110 | static void | |
111 | __attribute__ ((noreturn)) | |
112 | signal_sender (int signo, bool sleep) | |
113 | { | |
114 | pid_t target = getppid (); | |
115 | while (true) | |
116 | { | |
117 | if (kill (target, signo) != 0) | |
118 | { | |
119 | dprintf (STDOUT_FILENO, "error: kill: %m\n"); | |
120 | abort (); | |
121 | } | |
122 | if (sleep) | |
123 | usleep (1 * 1000 * 1000); | |
e2cd73a2 FW |
124 | else |
125 | /* Reduce the rate at which we send signals. */ | |
126 | sched_yield (); | |
56290d6e FW |
127 | } |
128 | } | |
129 | ||
130 | static int | |
131 | do_test (void) | |
132 | { | |
133 | struct sigaction action = | |
134 | { | |
135 | .sa_handler = sigusr1_handler, | |
136 | }; | |
137 | sigemptyset (&action.sa_mask); | |
138 | ||
139 | if (sigaction (SIGUSR1, &action, NULL) != 0) | |
140 | { | |
141 | printf ("error: sigaction: %m"); | |
142 | return 1; | |
143 | } | |
144 | ||
145 | action.sa_handler = liveness_signal_handler; | |
146 | if (sigaction (SIGUSR2, &action, NULL) != 0) | |
147 | { | |
148 | printf ("error: sigaction: %m"); | |
149 | return 1; | |
150 | } | |
151 | ||
152 | pid_t sigusr2_sender_pid = fork (); | |
153 | if (sigusr2_sender_pid == 0) | |
154 | signal_sender (SIGUSR2, true); | |
155 | sigusr1_sender_pid = fork (); | |
156 | if (sigusr1_sender_pid == 0) | |
157 | signal_sender (SIGUSR1, false); | |
158 | ||
159 | void *objects[malloc_objects] = {}; | |
160 | unsigned signals = 0; | |
161 | unsigned seed = 1; | |
162 | time_t last_report = 0; | |
163 | while (signals < signal_count) | |
164 | { | |
165 | progress_indicator = 1; | |
166 | int slot = rand_r (&seed) % malloc_objects; | |
167 | size_t size = rand_r (&seed) % malloc_maximum_size; | |
168 | if (kill (sigusr1_sender_pid, SIGCONT) != 0) | |
169 | { | |
170 | printf ("error: kill (SIGCONT): %m\n"); | |
171 | signal (SIGUSR1, SIG_IGN); | |
172 | kill (sigusr1_sender_pid, SIGKILL); | |
173 | kill (sigusr2_sender_pid, SIGKILL); | |
174 | return 1; | |
175 | } | |
176 | sigusr1_received = false; | |
177 | free (objects[slot]); | |
178 | objects[slot] = malloc (size); | |
179 | if (sigusr1_received) | |
180 | { | |
181 | ++signals; | |
182 | time_t current = time (0); | |
183 | if (current != last_report) | |
184 | { | |
185 | printf ("info: SIGUSR1 signal count: %u\n", signals); | |
186 | last_report = current; | |
187 | } | |
188 | } | |
189 | if (objects[slot] == NULL) | |
190 | { | |
191 | printf ("error: malloc: %m\n"); | |
192 | signal (SIGUSR1, SIG_IGN); | |
193 | kill (sigusr1_sender_pid, SIGKILL); | |
194 | kill (sigusr2_sender_pid, SIGKILL); | |
195 | return 1; | |
196 | } | |
197 | } | |
198 | ||
199 | /* Clean up allocations. */ | |
200 | for (int slot = 0; slot < malloc_objects; ++slot) | |
201 | free (objects[slot]); | |
202 | ||
203 | /* Terminate the signal-sending subprocess. The SIGUSR1 handler | |
204 | should no longer run because it uses sigusr1_sender_pid. */ | |
205 | signal (SIGUSR1, SIG_IGN); | |
206 | kill (sigusr1_sender_pid, SIGKILL); | |
207 | kill (sigusr2_sender_pid, SIGKILL); | |
208 | ||
209 | return 0; | |
210 | } |