1 /* Main worker function for the test driver.
2 Copyright (C) 1998-2022 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
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/test-driver.h>
20 #include <support/check.h>
21 #include <support/temp_file-internal.h>
22 #include <support/support.h>
32 #include <sys/param.h>
33 #include <sys/resource.h>
35 #include <sys/types.h>
42 static const struct option default_options
[] =
48 /* Show people how to run the program. */
50 usage (const struct option
*options
)
54 printf ("Usage: %s [options]\n"
56 "Environment Variables:\n"
57 " TIMEOUTFACTOR An integer used to scale the timeout\n"
58 " TMPDIR Where to place temporary files\n"
59 " TEST_COREDUMPS Do not disable coredumps if set\n"
61 program_invocation_short_name
);
62 printf ("Options:\n");
63 for (i
= 0; options
[i
].name
; ++i
)
67 indent
= printf (" --%s", options
[i
].name
);
68 if (options
[i
].has_arg
== required_argument
)
69 indent
+= printf (" <arg>");
70 printf ("%*s", 25 - indent
, "");
71 switch (options
[i
].val
)
74 printf ("Increase the output verbosity");
77 printf ("Run the test directly (instead of forking & monitoring)");
80 printf ("Override the TMPDIR env var");
87 /* The PID of the test process. */
88 static pid_t test_pid
;
90 /* The cleanup handler passed to test_main. */
91 static void (*cleanup_function
) (void);
94 print_timestamp (const char *what
, struct timespec tv
)
97 /* Casts of tv.tv_nsec below are necessary because the type of
98 tv_nsec is not literally long int on all supported platforms. */
99 if (gmtime_r (&tv
.tv_sec
, &tm
) == NULL
)
100 printf ("%s: %lld.%09ld\n",
101 what
, (long long int) tv
.tv_sec
, (long int) tv
.tv_nsec
);
103 printf ("%s: %04d-%02d-%02dT%02d:%02d:%02d.%09ld\n",
104 what
, 1900 + tm
.tm_year
, tm
.tm_mon
+ 1, tm
.tm_mday
,
105 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, (long int) tv
.tv_nsec
);
108 /* Timeout handler. We kill the child and exit with an error. */
110 __attribute__ ((noreturn
))
111 signal_handler (int sig
)
116 /* Do this first to avoid further interference from the
119 clock_gettime (CLOCK_REALTIME
, &now
);
121 bool st_available
= fstat64 (STDOUT_FILENO
, &st
) == 0 && st
.st_mtime
!= 0;
123 assert (test_pid
> 1);
124 /* Kill the whole process group. */
125 kill (-test_pid
, SIGKILL
);
126 /* In case setpgid failed in the child, kill it individually too. */
127 kill (test_pid
, SIGKILL
);
129 /* Wait for it to terminate. */
131 for (i
= 0; i
< 5; ++i
)
133 killed
= waitpid (test_pid
, &status
, WNOHANG
|WUNTRACED
);
137 /* Delay, give the system time to process the kill. If the
138 nanosleep() call return prematurely, all the better. We
139 won't restart it since this probably means the child process
143 ts
.tv_nsec
= 100000000;
144 nanosleep (&ts
, NULL
);
146 if (killed
!= 0 && killed
!= test_pid
)
148 printf ("Failed to kill test process: %m\n");
152 if (cleanup_function
!= NULL
)
157 signal (sig
, SIG_DFL
);
161 if (killed
== 0 || (WIFSIGNALED (status
) && WTERMSIG (status
) == SIGKILL
))
162 puts ("Timed out: killed the child process");
163 else if (WIFSTOPPED (status
))
164 printf ("Timed out: the child process was %s\n",
165 strsignal (WSTOPSIG (status
)));
166 else if (WIFSIGNALED (status
))
167 printf ("Timed out: the child process got signal %s\n",
168 strsignal (WTERMSIG (status
)));
170 printf ("Timed out: killed the child process but it exited %d\n",
171 WEXITSTATUS (status
));
173 print_timestamp ("Termination time", now
);
175 print_timestamp ("Last write to standard output", st
.st_mtim
);
177 /* Exit with an error. */
181 /* This must be volatile as it will be modified by the debugger. */
182 static volatile int wait_for_debugger
= 0;
184 /* Run test_function or test_function_argv. */
186 run_test_function (int argc
, char **argv
, const struct test_config
*config
)
188 const char *wfd
= getenv("WAIT_FOR_DEBUGGER");
190 wait_for_debugger
= atoi (wfd
);
191 if (wait_for_debugger
)
195 char *gdb_script_name
;
196 int inside_container
= 0;
201 const char *outside_pid
= getenv("PID_OUTSIDE_CONTAINER");
204 mypid
= atoi (outside_pid
);
205 inside_container
= 1;
209 gdb_script_name
= (char *) xmalloc (strlen (argv
[0]) + strlen (".gdb") + 1);
210 sprintf (gdb_script_name
, "%s.gdb", argv
[0]);
211 gdb_script
= xfopen (gdb_script_name
, "w");
213 fprintf (stderr
, "Waiting for debugger, test process is pid %d\n", mypid
);
214 fprintf (stderr
, "gdb -x %s\n", gdb_script_name
);
215 if (inside_container
)
216 fprintf (gdb_script
, "set sysroot %s/testroot.root\n", support_objdir_root
);
217 fprintf (gdb_script
, "file\n");
218 fprintf (gdb_script
, "file %s\n", argv
[0]);
219 fprintf (gdb_script
, "symbol-file %s\n", argv
[0]);
220 fprintf (gdb_script
, "exec-file %s\n", argv
[0]);
221 fprintf (gdb_script
, "attach %ld\n", (long int) mypid
);
222 fprintf (gdb_script
, "set wait_for_debugger = 0\n");
224 free (gdb_script_name
);
227 /* Wait for the debugger to set wait_for_debugger to zero. */
228 while (wait_for_debugger
)
231 if (config
->run_command_mode
)
233 /* In run-command-mode, the child process executes the command line
234 arguments as a new program. */
235 char **argv_
= xmalloc (sizeof (char *) * argc
);
236 memcpy (argv_
, &argv
[1], sizeof (char *) * (argc
- 1));
237 argv_
[argc
- 1] = NULL
;
238 execv (argv_
[0], argv_
);
239 printf ("error: should not return here\n");
243 if (config
->test_function
!= NULL
)
244 return config
->test_function ();
245 else if (config
->test_function_argv
!= NULL
)
246 return config
->test_function_argv (argc
, argv
);
249 printf ("error: no test function defined\n");
254 static bool test_main_called
;
256 const char *test_dir
= NULL
;
257 unsigned int test_verbose
= 0;
259 /* If test failure reporting has been linked in, it may contribute
260 additional test failures. */
262 adjust_exit_status (int status
)
264 if (support_report_failure
!= NULL
)
265 return support_report_failure (status
);
270 support_test_main (int argc
, char **argv
, const struct test_config
*config
)
272 if (test_main_called
)
274 printf ("error: test_main called for a second time\n");
277 test_main_called
= true;
278 const struct option
*options
;
279 if (config
->options
!= NULL
)
280 options
= config
->options
;
282 options
= default_options
;
284 cleanup_function
= config
->cleanup_function
;
286 int direct
= 0; /* Directly call the test function? */
289 unsigned int timeoutfactor
= TIMEOUTFACTOR
;
292 /* If we're debugging the test, we need to disable timeouts and use
293 the initial pid (esp if we're running inside a container). */
294 if (getenv("WAIT_FOR_DEBUGGER") != NULL
)
297 if (!config
->no_mallopt
)
299 /* Make uses of freed and uninitialized memory known. Do not
300 pull in a definition for mallopt if it has not been defined
302 extern __typeof__ (mallopt
) mallopt
__attribute__ ((weak
));
304 mallopt (M_PERTURB
, 42);
307 while ((opt
= getopt_long (argc
, argv
, config
->optstring
, options
, NULL
))
324 if (config
->cmdline_function
!= NULL
)
325 config
->cmdline_function (opt
);
328 /* If set, read the test TIMEOUTFACTOR value from the environment.
329 This value is used to scale the default test timeout values. */
330 char *envstr_timeoutfactor
= getenv ("TIMEOUTFACTOR");
331 if (envstr_timeoutfactor
!= NULL
)
333 char *envstr_conv
= envstr_timeoutfactor
;
334 unsigned long int env_fact
;
336 env_fact
= strtoul (envstr_timeoutfactor
, &envstr_conv
, 0);
337 if (*envstr_conv
== '\0' && envstr_conv
!= envstr_timeoutfactor
)
338 timeoutfactor
= MAX (env_fact
, 1);
341 /* Set TMPDIR to specified test directory. */
342 if (test_dir
!= NULL
)
344 setenv ("TMPDIR", test_dir
, 1);
346 if (chdir (test_dir
) < 0)
348 printf ("chdir: %m\n");
354 test_dir
= getenv ("TMPDIR");
355 if (test_dir
== NULL
|| test_dir
[0] == '\0')
358 if (support_set_test_dir
!= NULL
)
359 support_set_test_dir (test_dir
);
361 int timeout
= config
->timeout
;
363 timeout
= DEFAULT_TIMEOUT
;
365 /* Make sure we see all message, even those on stdout. */
366 if (!config
->no_setvbuf
)
367 setvbuf (stdout
, NULL
, _IONBF
, 0);
369 /* Make sure temporary files are deleted. */
370 if (support_delete_temp_files
!= NULL
)
371 atexit (support_delete_temp_files
);
373 /* Correct for the possible parameters. */
374 argv
[optind
- 1] = argv
[0];
378 /* Call the initializing function, if one is available. */
379 if (config
->prepare_function
!= NULL
)
380 config
->prepare_function (argc
, argv
);
382 const char *envstr_direct
= getenv ("TEST_DIRECT");
383 if (envstr_direct
!= NULL
)
385 FILE *f
= fopen (envstr_direct
, "w");
388 printf ("cannot open TEST_DIRECT output file '%s': %m\n",
393 fprintf (f
, "timeout=%u\ntimeoutfactor=%u\n",
394 config
->timeout
, timeoutfactor
);
395 if (config
->expected_status
!= 0)
396 fprintf (f
, "exit=%u\n", config
->expected_status
);
397 if (config
->expected_signal
!= 0)
398 fprintf (f
, "signal=%s\n", strsignal (config
->expected_signal
));
400 if (support_print_temp_files
!= NULL
)
401 support_print_temp_files (f
);
407 bool disable_coredumps
;
409 const char *coredumps
= getenv ("TEST_COREDUMPS");
410 disable_coredumps
= coredumps
== NULL
|| coredumps
[0] == '\0';
413 /* If we are not expected to fork run the function immediately. */
415 return adjust_exit_status (run_test_function (argc
, argv
, config
));
417 /* Set up the test environment:
420 - fork and execute the function. */
425 /* This is the child. */
426 if (disable_coredumps
)
428 /* Try to avoid dumping core. This is necessary because we
429 run the test from the source tree, and the coredumps
430 would end up there (and not in the build tree). */
431 struct rlimit core_limit
;
432 core_limit
.rlim_cur
= 0;
433 core_limit
.rlim_max
= 0;
434 setrlimit (RLIMIT_CORE
, &core_limit
);
437 /* We put the test process in its own pgrp so that if it bogusly
438 generates any job control signals, they won't hit the whole build. */
439 if (setpgid (0, 0) != 0)
440 printf ("Failed to set the process group ID: %m\n");
442 /* Execute the test function and exit with the return value. */
443 exit (run_test_function (argc
, argv
, config
));
445 else if (test_pid
< 0)
447 printf ("Cannot fork test program: %m\n");
452 signal (SIGALRM
, signal_handler
);
453 alarm (timeout
* timeoutfactor
);
455 /* Make sure we clean up if the wrapper gets interrupted. */
456 signal (SIGINT
, signal_handler
);
458 /* Wait for the regular termination. */
459 termpid
= TEMP_FAILURE_RETRY (waitpid (test_pid
, &status
, 0));
462 printf ("Waiting for test program failed: %m\n");
465 if (termpid
!= test_pid
)
467 printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
468 (long int) test_pid
, (long int) termpid
);
472 /* Process terminated normaly without timeout etc. */
473 if (WIFEXITED (status
))
475 if (config
->expected_status
== 0)
477 if (config
->expected_signal
== 0)
478 /* Exit with the return value of the test. */
479 return adjust_exit_status (WEXITSTATUS (status
));
482 printf ("Expected signal '%s' from child, got none\n",
483 strsignal (config
->expected_signal
));
489 /* Non-zero exit status is expected */
490 if (WEXITSTATUS (status
) != config
->expected_status
)
492 printf ("Expected status %d, got %d\n",
493 config
->expected_status
, WEXITSTATUS (status
));
497 return adjust_exit_status (0);
499 /* Process was killed by timer or other signal. */
502 if (config
->expected_signal
== 0)
504 printf ("Didn't expect signal from child: got `%s'\n",
505 strsignal (WTERMSIG (status
)));
508 else if (WTERMSIG (status
) != config
->expected_signal
)
510 printf ("Incorrect signal from child: got `%s', need `%s'\n",
511 strsignal (WTERMSIG (status
)),
512 strsignal (config
->expected_signal
));
516 return adjust_exit_status (0);