support_test_main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /* Main worker function for the test driver.
  2. Copyright (C) 1998-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <support/test-driver.h>
  16. #include <support/check.h>
  17. #include <support/temp_file-internal.h>
  18. #include <assert.h>
  19. #include <errno.h>
  20. #include <getopt.h>
  21. #include <malloc.h>
  22. #include <signal.h>
  23. #include <stdbool.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <sys/param.h>
  27. #include <sys/resource.h>
  28. #include <sys/time.h>
  29. #include <sys/types.h>
  30. #include <sys/wait.h>
  31. #include <time.h>
  32. #include <unistd.h>
  33. static const struct option default_options[] =
  34. {
  35. TEST_DEFAULT_OPTIONS
  36. { NULL, 0, NULL, 0 }
  37. };
  38. /* Show people how to run the program. */
  39. static void
  40. usage (const struct option *options)
  41. {
  42. size_t i;
  43. printf ("Usage: %s [options]\n"
  44. "\n"
  45. "Environment Variables:\n"
  46. " TIMEOUTFACTOR An integer used to scale the timeout\n"
  47. " TMPDIR Where to place temporary files\n"
  48. " TEST_COREDUMPS Do not disable coredumps if set\n"
  49. "\n",
  50. program_invocation_short_name);
  51. printf ("Options:\n");
  52. for (i = 0; options[i].name; ++i)
  53. {
  54. int indent;
  55. indent = printf (" --%s", options[i].name);
  56. if (options[i].has_arg == required_argument)
  57. indent += printf (" <arg>");
  58. printf ("%*s", 25 - indent, "");
  59. switch (options[i].val)
  60. {
  61. case 'v':
  62. printf ("Increase the output verbosity");
  63. break;
  64. case OPT_DIRECT:
  65. printf ("Run the test directly (instead of forking & monitoring)");
  66. break;
  67. case OPT_TESTDIR:
  68. printf ("Override the TMPDIR env var");
  69. break;
  70. }
  71. printf ("\n");
  72. }
  73. }
  74. /* The PID of the test process. */
  75. static pid_t test_pid;
  76. /* The cleanup handler passed to test_main. */
  77. static void (*cleanup_function) (void);
  78. static void
  79. print_timestamp (const char *what, struct timeval tv)
  80. {
  81. struct tm tm;
  82. if (gmtime_r (&tv.tv_sec, &tm) == NULL)
  83. printf ("%s: %lld.%06d\n",
  84. what, (long long int) tv.tv_sec, (int) tv.tv_usec);
  85. else
  86. printf ("%s: %04d-%02d-%02dT%02d:%02d:%02d.%06d\n",
  87. what, 1900 + tm.tm_year, tm.tm_mon + 1, tm.tm_mday,
  88. tm.tm_hour, tm.tm_min, tm.tm_sec, (int) tv.tv_usec);
  89. }
  90. /* Timeout handler. We kill the child and exit with an error. */
  91. static void
  92. __attribute__ ((noreturn))
  93. signal_handler (int sig)
  94. {
  95. int killed;
  96. int status;
  97. /* Do this first to avoid further interference from the
  98. subprocess. */
  99. struct timeval now;
  100. bool now_available = gettimeofday (&now, NULL) == 0;
  101. struct stat64 st;
  102. bool st_available = fstat64 (STDOUT_FILENO, &st) == 0 && st.st_mtime != 0;
  103. assert (test_pid > 1);
  104. /* Kill the whole process group. */
  105. kill (-test_pid, SIGKILL);
  106. /* In case setpgid failed in the child, kill it individually too. */
  107. kill (test_pid, SIGKILL);
  108. /* Wait for it to terminate. */
  109. int i;
  110. for (i = 0; i < 5; ++i)
  111. {
  112. killed = waitpid (test_pid, &status, WNOHANG|WUNTRACED);
  113. if (killed != 0)
  114. break;
  115. /* Delay, give the system time to process the kill. If the
  116. nanosleep() call return prematurely, all the better. We
  117. won't restart it since this probably means the child process
  118. finally died. */
  119. struct timespec ts;
  120. ts.tv_sec = 0;
  121. ts.tv_nsec = 100000000;
  122. nanosleep (&ts, NULL);
  123. }
  124. if (killed != 0 && killed != test_pid)
  125. {
  126. printf ("Failed to kill test process: %m\n");
  127. exit (1);
  128. }
  129. if (cleanup_function != NULL)
  130. cleanup_function ();
  131. if (sig == SIGINT)
  132. {
  133. signal (sig, SIG_DFL);
  134. raise (sig);
  135. }
  136. if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
  137. puts ("Timed out: killed the child process");
  138. else if (WIFSTOPPED (status))
  139. printf ("Timed out: the child process was %s\n",
  140. strsignal (WSTOPSIG (status)));
  141. else if (WIFSIGNALED (status))
  142. printf ("Timed out: the child process got signal %s\n",
  143. strsignal (WTERMSIG (status)));
  144. else
  145. printf ("Timed out: killed the child process but it exited %d\n",
  146. WEXITSTATUS (status));
  147. if (now_available)
  148. print_timestamp ("Termination time", now);
  149. if (st_available)
  150. print_timestamp ("Last write to standard output",
  151. (struct timeval) { st.st_mtim.tv_sec,
  152. st.st_mtim.tv_nsec / 1000 });
  153. /* Exit with an error. */
  154. exit (1);
  155. }
  156. /* Run test_function or test_function_argv. */
  157. static int
  158. run_test_function (int argc, char **argv, const struct test_config *config)
  159. {
  160. if (config->test_function != NULL)
  161. return config->test_function ();
  162. else if (config->test_function_argv != NULL)
  163. return config->test_function_argv (argc, argv);
  164. else
  165. {
  166. printf ("error: no test function defined\n");
  167. exit (1);
  168. }
  169. }
  170. static bool test_main_called;
  171. const char *test_dir = NULL;
  172. unsigned int test_verbose = 0;
  173. /* If test failure reporting has been linked in, it may contribute
  174. additional test failures. */
  175. static int
  176. adjust_exit_status (int status)
  177. {
  178. if (support_report_failure != NULL)
  179. return support_report_failure (status);
  180. return status;
  181. }
  182. int
  183. support_test_main (int argc, char **argv, const struct test_config *config)
  184. {
  185. if (test_main_called)
  186. {
  187. printf ("error: test_main called for a second time\n");
  188. exit (1);
  189. }
  190. test_main_called = true;
  191. const struct option *options;
  192. if (config->options != NULL)
  193. options = config->options;
  194. else
  195. options = default_options;
  196. cleanup_function = config->cleanup_function;
  197. int direct = 0; /* Directly call the test function? */
  198. int status;
  199. int opt;
  200. unsigned int timeoutfactor = 1;
  201. pid_t termpid;
  202. if (!config->no_mallopt)
  203. {
  204. /* Make uses of freed and uninitialized memory known. Do not
  205. pull in a definition for mallopt if it has not been defined
  206. already. */
  207. extern __typeof__ (mallopt) mallopt __attribute__ ((weak));
  208. if (mallopt != NULL)
  209. mallopt (M_PERTURB, 42);
  210. }
  211. while ((opt = getopt_long (argc, argv, config->optstring, options, NULL))
  212. != -1)
  213. switch (opt)
  214. {
  215. case '?':
  216. usage (options);
  217. exit (1);
  218. case 'v':
  219. ++test_verbose;
  220. break;
  221. case OPT_DIRECT:
  222. direct = 1;
  223. break;
  224. case OPT_TESTDIR:
  225. test_dir = optarg;
  226. break;
  227. default:
  228. if (config->cmdline_function != NULL)
  229. config->cmdline_function (opt);
  230. }
  231. /* If set, read the test TIMEOUTFACTOR value from the environment.
  232. This value is used to scale the default test timeout values. */
  233. char *envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
  234. if (envstr_timeoutfactor != NULL)
  235. {
  236. char *envstr_conv = envstr_timeoutfactor;
  237. unsigned long int env_fact;
  238. env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
  239. if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
  240. timeoutfactor = MAX (env_fact, 1);
  241. }
  242. /* Set TMPDIR to specified test directory. */
  243. if (test_dir != NULL)
  244. {
  245. setenv ("TMPDIR", test_dir, 1);
  246. if (chdir (test_dir) < 0)
  247. {
  248. printf ("chdir: %m\n");
  249. exit (1);
  250. }
  251. }
  252. else
  253. {
  254. test_dir = getenv ("TMPDIR");
  255. if (test_dir == NULL || test_dir[0] == '\0')
  256. test_dir = "/tmp";
  257. }
  258. if (support_set_test_dir != NULL)
  259. support_set_test_dir (test_dir);
  260. int timeout = config->timeout;
  261. if (timeout == 0)
  262. timeout = DEFAULT_TIMEOUT;
  263. /* Make sure we see all message, even those on stdout. */
  264. if (!config->no_setvbuf)
  265. setvbuf (stdout, NULL, _IONBF, 0);
  266. /* Make sure temporary files are deleted. */
  267. if (support_delete_temp_files != NULL)
  268. atexit (support_delete_temp_files);
  269. /* Correct for the possible parameters. */
  270. argv[optind - 1] = argv[0];
  271. argv += optind - 1;
  272. argc -= optind - 1;
  273. /* Call the initializing function, if one is available. */
  274. if (config->prepare_function != NULL)
  275. config->prepare_function (argc, argv);
  276. const char *envstr_direct = getenv ("TEST_DIRECT");
  277. if (envstr_direct != NULL)
  278. {
  279. FILE *f = fopen (envstr_direct, "w");
  280. if (f == NULL)
  281. {
  282. printf ("cannot open TEST_DIRECT output file '%s': %m\n",
  283. envstr_direct);
  284. exit (1);
  285. }
  286. fprintf (f, "timeout=%u\ntimeoutfactor=%u\n",
  287. config->timeout, timeoutfactor);
  288. if (config->expected_status != 0)
  289. fprintf (f, "exit=%u\n", config->expected_status);
  290. if (config->expected_signal != 0)
  291. fprintf (f, "signal=%s\n", strsignal (config->expected_signal));
  292. if (support_print_temp_files != NULL)
  293. support_print_temp_files (f);
  294. fclose (f);
  295. direct = 1;
  296. }
  297. bool disable_coredumps;
  298. {
  299. const char *coredumps = getenv ("TEST_COREDUMPS");
  300. disable_coredumps = coredumps == NULL || coredumps[0] == '\0';
  301. }
  302. /* If we are not expected to fork run the function immediately. */
  303. if (direct)
  304. return adjust_exit_status (run_test_function (argc, argv, config));
  305. /* Set up the test environment:
  306. - prevent core dumps
  307. - set up the timer
  308. - fork and execute the function. */
  309. test_pid = fork ();
  310. if (test_pid == 0)
  311. {
  312. /* This is the child. */
  313. if (disable_coredumps)
  314. {
  315. /* Try to avoid dumping core. This is necessary because we
  316. run the test from the source tree, and the coredumps
  317. would end up there (and not in the build tree). */
  318. struct rlimit core_limit;
  319. core_limit.rlim_cur = 0;
  320. core_limit.rlim_max = 0;
  321. setrlimit (RLIMIT_CORE, &core_limit);
  322. }
  323. /* We put the test process in its own pgrp so that if it bogusly
  324. generates any job control signals, they won't hit the whole build. */
  325. if (setpgid (0, 0) != 0)
  326. printf ("Failed to set the process group ID: %m\n");
  327. /* Execute the test function and exit with the return value. */
  328. exit (run_test_function (argc, argv, config));
  329. }
  330. else if (test_pid < 0)
  331. {
  332. printf ("Cannot fork test program: %m\n");
  333. exit (1);
  334. }
  335. /* Set timeout. */
  336. signal (SIGALRM, signal_handler);
  337. alarm (timeout * timeoutfactor);
  338. /* Make sure we clean up if the wrapper gets interrupted. */
  339. signal (SIGINT, signal_handler);
  340. /* Wait for the regular termination. */
  341. termpid = TEMP_FAILURE_RETRY (waitpid (test_pid, &status, 0));
  342. if (termpid == -1)
  343. {
  344. printf ("Waiting for test program failed: %m\n");
  345. exit (1);
  346. }
  347. if (termpid != test_pid)
  348. {
  349. printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
  350. (long int) test_pid, (long int) termpid);
  351. exit (1);
  352. }
  353. /* Process terminated normaly without timeout etc. */
  354. if (WIFEXITED (status))
  355. {
  356. if (config->expected_status == 0)
  357. {
  358. if (config->expected_signal == 0)
  359. /* Exit with the return value of the test. */
  360. return adjust_exit_status (WEXITSTATUS (status));
  361. else
  362. {
  363. printf ("Expected signal '%s' from child, got none\n",
  364. strsignal (config->expected_signal));
  365. exit (1);
  366. }
  367. }
  368. else
  369. {
  370. /* Non-zero exit status is expected */
  371. if (WEXITSTATUS (status) != config->expected_status)
  372. {
  373. printf ("Expected status %d, got %d\n",
  374. config->expected_status, WEXITSTATUS (status));
  375. exit (1);
  376. }
  377. }
  378. return adjust_exit_status (0);
  379. }
  380. /* Process was killed by timer or other signal. */
  381. else
  382. {
  383. if (config->expected_signal == 0)
  384. {
  385. printf ("Didn't expect signal from child: got `%s'\n",
  386. strsignal (WTERMSIG (status)));
  387. exit (1);
  388. }
  389. else if (WTERMSIG (status) != config->expected_signal)
  390. {
  391. printf ("Incorrect signal from child: got `%s', need `%s'\n",
  392. strsignal (WTERMSIG (status)),
  393. strsignal (config->expected_signal));
  394. exit (1);
  395. }
  396. return adjust_exit_status (0);
  397. }
  398. }