Revert {send,sendm,recv,recvm}msg conformance changes
[glibc.git] / malloc / tst-mallocfork2.c
blob4939938a44ba5207e32fef18a2d0fcba979fdec8
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.
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.
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.
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/>. */
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).
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. */
27 #include <errno.h>
28 #include <sched.h>
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>
38 /* How many malloc objects to keep arond. */
39 enum { malloc_objects = 1009 };
41 /* The maximum size of an object. */
42 enum { malloc_maximum_size = 70000 };
44 /* How many signals need to be delivered before the test exits. */
45 enum { signal_count = 1000 };
48 /* Process ID of the subprocess which sends SIGUSR1 signals. */
49 static pid_t sigusr1_sender_pid;
51 /* Set to 1 if SIGUSR1 is received. Used to detect a signal during
52 malloc/free. */
53 static volatile sig_atomic_t sigusr1_received;
55 /* Periodically set to 1, to indicate that the process is making
56 progress. Checked by liveness_signal_handler. */
57 static volatile sig_atomic_t progress_indicator = 1;
59 /* Write the message to standard output. Usable from signal
60 handlers. */
61 static void
62 write_message (const char *str)
64 write (STDOUT_FILENO, str, strlen (str));
67 static void
68 sigusr1_handler (int signo)
70 /* Let the main program make progress, by temporarily suspending
71 signals from the subprocess. */
72 if (sigusr1_received)
73 return;
74 /* sigusr1_sender_pid might not be initialized in the parent when
75 the first SIGUSR1 signal arrives. */
76 if (sigusr1_sender_pid > 0 && kill (sigusr1_sender_pid, SIGSTOP) != 0)
78 write_message ("error: kill (SIGSTOP)\n");
79 abort ();
81 sigusr1_received = 1;
83 /* Perform a fork with a trivial subprocess. */
84 pid_t pid = fork ();
85 if (pid == -1)
87 write_message ("error: fork\n");
88 abort ();
90 if (pid == 0)
91 _exit (0);
92 int status;
93 int ret = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
94 if (ret < 0)
96 write_message ("error: waitpid\n");
97 abort ();
99 if (status != 0)
101 write_message ("error: unexpected exit status from subprocess\n");
102 abort ();
106 static void
107 liveness_signal_handler (int signo)
109 if (progress_indicator)
110 progress_indicator = 0;
111 else
112 write_message ("warning: process seems to be stuck\n");
115 static void
116 __attribute__ ((noreturn))
117 signal_sender (int signo, bool sleep)
119 pid_t target = getppid ();
120 while (true)
122 if (kill (target, signo) != 0)
124 dprintf (STDOUT_FILENO, "error: kill: %m\n");
125 abort ();
127 if (sleep)
128 usleep (1 * 1000 * 1000);
129 else
130 /* Reduce the rate at which we send signals. */
131 sched_yield ();
135 static int
136 do_test (void)
138 struct sigaction action =
140 .sa_handler = sigusr1_handler,
142 sigemptyset (&action.sa_mask);
144 if (sigaction (SIGUSR1, &action, NULL) != 0)
146 printf ("error: sigaction: %m");
147 return 1;
150 action.sa_handler = liveness_signal_handler;
151 if (sigaction (SIGUSR2, &action, NULL) != 0)
153 printf ("error: sigaction: %m");
154 return 1;
157 pid_t sigusr2_sender_pid = fork ();
158 if (sigusr2_sender_pid == 0)
159 signal_sender (SIGUSR2, true);
160 sigusr1_sender_pid = fork ();
161 if (sigusr1_sender_pid == 0)
162 signal_sender (SIGUSR1, false);
164 void *objects[malloc_objects] = {};
165 unsigned signals = 0;
166 unsigned seed = 1;
167 time_t last_report = 0;
168 while (signals < signal_count)
170 progress_indicator = 1;
171 int slot = rand_r (&seed) % malloc_objects;
172 size_t size = rand_r (&seed) % malloc_maximum_size;
173 if (kill (sigusr1_sender_pid, SIGCONT) != 0)
175 printf ("error: kill (SIGCONT): %m\n");
176 signal (SIGUSR1, SIG_IGN);
177 kill (sigusr1_sender_pid, SIGKILL);
178 kill (sigusr2_sender_pid, SIGKILL);
179 return 1;
181 sigusr1_received = false;
182 free (objects[slot]);
183 objects[slot] = malloc (size);
184 if (sigusr1_received)
186 ++signals;
187 time_t current = time (0);
188 if (current != last_report)
190 printf ("info: SIGUSR1 signal count: %u\n", signals);
191 last_report = current;
194 if (objects[slot] == NULL)
196 printf ("error: malloc: %m\n");
197 signal (SIGUSR1, SIG_IGN);
198 kill (sigusr1_sender_pid, SIGKILL);
199 kill (sigusr2_sender_pid, SIGKILL);
200 return 1;
204 /* Clean up allocations. */
205 for (int slot = 0; slot < malloc_objects; ++slot)
206 free (objects[slot]);
208 /* Terminate the signal-sending subprocess. The SIGUSR1 handler
209 should no longer run because it uses sigusr1_sender_pid. */
210 signal (SIGUSR1, SIG_IGN);
211 kill (sigusr1_sender_pid, SIGKILL);
212 kill (sigusr2_sender_pid, SIGKILL);
214 return 0;
217 #define TEST_FUNCTION do_test ()
218 #include "../test-skeleton.c"