test-skeleton.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /* Skeleton for test programs.
  2. Copyright (C) 1998,2000-2004, 2005 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
  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.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <assert.h>
  17. #include <errno.h>
  18. #include <getopt.h>
  19. #include <malloc.h>
  20. #include <search.h>
  21. #include <signal.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/resource.h>
  27. #include <sys/wait.h>
  28. #include <sys/param.h>
  29. #include <time.h>
  30. #include <features.h>
  31. #include "test-skeleton.h"
  32. /* The test function is normally called `do_test' and it is called
  33. with argc and argv as the arguments. We nevertheless provide the
  34. possibility to overwrite this name. */
  35. #ifndef TEST_FUNCTION
  36. # define TEST_FUNCTION do_test (argc, argv)
  37. #endif
  38. #ifndef TEST_DATA_LIMIT
  39. # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
  40. #endif
  41. /* PID of the test itself. */
  42. static pid_t pid;
  43. /* Directory to place temporary files in. */
  44. static const char *test_dir;
  45. /* List of temporary files. */
  46. struct temp_name_list
  47. {
  48. struct qelem q;
  49. char *name;
  50. } *temp_name_list;
  51. /* Add temporary files in list. */
  52. static void
  53. __attribute__ ((unused))
  54. add_temp_file (const char *name)
  55. {
  56. struct temp_name_list *newp
  57. = (struct temp_name_list *) calloc (sizeof (*newp), 1);
  58. char *newname = strdup (name);
  59. if (newp != NULL && newname != NULL)
  60. {
  61. newp->name = newname;
  62. if (temp_name_list == NULL)
  63. temp_name_list = (struct temp_name_list *) &newp->q;
  64. else
  65. insque (newp, temp_name_list);
  66. }
  67. else
  68. free (newp);
  69. }
  70. /* Delete all temporary files. */
  71. static void
  72. delete_temp_files (void)
  73. {
  74. while (temp_name_list != NULL)
  75. {
  76. remove (temp_name_list->name);
  77. free (temp_name_list->name);
  78. struct temp_name_list *next
  79. = (struct temp_name_list *) temp_name_list->q.q_forw;
  80. free (temp_name_list);
  81. temp_name_list = next;
  82. }
  83. }
  84. /* Create a temporary file. Return the opened file descriptor on
  85. success, or -1 on failure. Write the file name to *FILENAME if
  86. FILENAME is not NULL. In this case, the caller is expected to free
  87. *FILENAME. */
  88. static int
  89. __attribute__ ((unused))
  90. create_temp_file (const char *base, char **filename)
  91. {
  92. char *fname;
  93. int _fd;
  94. fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
  95. + sizeof ("XXXXXX"));
  96. if (fname == NULL)
  97. {
  98. puts ("out of memory");
  99. return -1;
  100. }
  101. strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
  102. _fd = mkstemp (fname);
  103. if (_fd == -1)
  104. {
  105. printf ("cannot open temporary file '%s': %s\n", fname, strerror(errno));
  106. free (fname);
  107. return -1;
  108. }
  109. add_temp_file (fname);
  110. if (filename != NULL)
  111. *filename = fname;
  112. else
  113. free (fname);
  114. return _fd;
  115. }
  116. /* Timeout handler. We kill the child and exit with an error. */
  117. static void
  118. __attribute__ ((noreturn))
  119. signal_handler (int sig __attribute__ ((unused)))
  120. {
  121. int killed = 0;
  122. int status;
  123. int i;
  124. assert (pid > 1);
  125. /* Kill the whole process group. */
  126. kill (-pid, SIGKILL);
  127. /* In case setpgid failed in the child, kill it individually too. */
  128. kill (pid, SIGKILL);
  129. /* Wait for it to terminate. */
  130. for (i = 0; i < 5; ++i)
  131. {
  132. #ifdef __UCLIBC_HAS_REALTIME__
  133. struct timespec ts;
  134. #endif
  135. killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
  136. if (killed != 0)
  137. break;
  138. /* Delay, give the system time to process the kill. If the
  139. nanosleep() call return prematurely, all the better. We
  140. won't restart it since this probably means the child process
  141. finally died. */
  142. #ifdef __UCLIBC_HAS_REALTIME__
  143. ts.tv_sec = 0;
  144. ts.tv_nsec = 100000000;
  145. nanosleep (&ts, NULL);
  146. #else
  147. /* No nanosleep, just sleep 1s instead of 0.1s */
  148. sleep(1);
  149. #endif
  150. }
  151. if (killed != 0 && killed != pid)
  152. {
  153. perror ("Failed to kill test process");
  154. exit (1);
  155. }
  156. #ifdef CLEANUP_HANDLER
  157. CLEANUP_HANDLER;
  158. #endif
  159. if (sig == SIGINT)
  160. {
  161. signal (sig, SIG_DFL);
  162. raise (sig);
  163. }
  164. /* If we expected this signal: good! */
  165. #ifdef EXPECTED_SIGNAL
  166. if (EXPECTED_SIGNAL == SIGALRM)
  167. exit (0);
  168. #endif
  169. if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
  170. fputs ("Timed out: killed the child process\n", stderr);
  171. else if (WIFSTOPPED (status))
  172. fprintf (stderr, "Timed out: the child process was %s\n",
  173. strsignal (WSTOPSIG (status)));
  174. else if (WIFSIGNALED (status))
  175. fprintf (stderr, "Timed out: the child process got signal %s\n",
  176. strsignal (WTERMSIG (status)));
  177. else
  178. fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
  179. WEXITSTATUS (status));
  180. /* Exit with an error. */
  181. exit (1);
  182. }
  183. #ifdef __XXX_HANDLE_CTRL_C
  184. static void
  185. __attribute__ ((noreturn))
  186. handler_killpid(int sig)
  187. {
  188. kill(pid, SIGKILL); /* kill test */
  189. signal(sig, SIG_DFL);
  190. raise(sig); /* kill ourself */
  191. _exit(128 + sig); /* paranoia */
  192. }
  193. #endif
  194. /* We provide the entry point here. */
  195. int
  196. main (int argc, char *argv[], char *envp[])
  197. {
  198. int direct = 0; /* Directly call the test function? */
  199. int status;
  200. int opt;
  201. unsigned int timeoutfactor = 1;
  202. pid_t termpid;
  203. char *envstr_timeoutfactor;
  204. char **argv1;
  205. /* Make uses of freed and uninitialized memory known. */
  206. #ifdef __MALLOC_STANDARD__
  207. #ifndef M_PERTURB
  208. # define M_PERTURB -6
  209. #endif
  210. mallopt (M_PERTURB, 42);
  211. #endif
  212. #ifdef STDOUT_UNBUFFERED
  213. setbuf (stdout, NULL);
  214. #endif
  215. # ifndef CMDLINE_OPTIONS
  216. # define CMDLINE_OPTIONS ""
  217. # endif
  218. while ((opt = getopt (argc, argv, "+dt:" CMDLINE_OPTIONS)) >= 0)
  219. switch (opt)
  220. {
  221. case '?':
  222. exit (1);
  223. case 'd':
  224. direct = 1;
  225. break;
  226. case 't':
  227. test_dir = optarg;
  228. break;
  229. #ifdef CMDLINE_PROCESS
  230. CMDLINE_PROCESS
  231. #endif
  232. }
  233. /* If set, read the test TIMEOUTFACTOR value from the environment.
  234. This value is used to scale the default test timeout values. */
  235. envstr_timeoutfactor = getenv ("TIMEOUTFACTOR");
  236. if (envstr_timeoutfactor != NULL)
  237. {
  238. char *envstr_conv = envstr_timeoutfactor;
  239. unsigned long int env_fact;
  240. env_fact = strtoul (envstr_timeoutfactor, &envstr_conv, 0);
  241. if (*envstr_conv == '\0' && envstr_conv != envstr_timeoutfactor)
  242. timeoutfactor = MAX (env_fact, 1);
  243. }
  244. /* Set TMPDIR to specified test directory. */
  245. if (test_dir != NULL)
  246. {
  247. setenv ("TMPDIR", test_dir, 1);
  248. if (chdir (test_dir) < 0)
  249. {
  250. perror ("chdir");
  251. exit (1);
  252. }
  253. }
  254. else
  255. {
  256. test_dir = getenv ("TMPDIR");
  257. if (test_dir == NULL || test_dir[0] == '\0')
  258. test_dir = "/tmp";
  259. }
  260. /* Make sure we see all message, even those on stdout. */
  261. setvbuf (stdout, NULL, _IONBF, 0);
  262. /* make sure temporary files are deleted. */
  263. atexit (delete_temp_files);
  264. /* If we are not expected to fork run the function immediately. */
  265. if (direct)
  266. {
  267. /* Correct for the possible parameters. */
  268. argv[optind - 1] = argv[0];
  269. argv += optind - 1;
  270. argc -= optind - 1;
  271. /* Call the initializing function, if one is available. */
  272. #ifdef PREPARE
  273. PREPARE (argc, argv);
  274. #endif
  275. return TEST_FUNCTION;
  276. }
  277. /* Set up the test environment:
  278. - prevent core dumps
  279. - set up the timer
  280. - fork and execute the function. */
  281. #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
  282. pid = fork ();
  283. if (pid == 0)
  284. {
  285. /* This is the child. */
  286. #ifdef RLIMIT_CORE
  287. /* Try to avoid dumping core. */
  288. struct rlimit core_limit;
  289. core_limit.rlim_cur = 0;
  290. core_limit.rlim_max = 0;
  291. setrlimit (RLIMIT_CORE, &core_limit);
  292. #endif
  293. /* We put the test process in its own pgrp so that if it bogusly
  294. generates any job control signals, they won't hit the whole build. */
  295. if (setpgid (0, 0) != 0)
  296. printf ("Failed to set the process group ID: %m\n");
  297. /* Correct for the possible parameters. */
  298. argv[optind - 1] = argv[0];
  299. argv += optind - 1;
  300. argc -= optind - 1;
  301. /* Call the initializing function, if one is available. */
  302. #ifdef PREPARE
  303. PREPARE (argc, argv);
  304. #endif
  305. /* Execute the test function and exit with the return value. */
  306. exit (TEST_FUNCTION);
  307. }
  308. else if (pid < 0)
  309. {
  310. perror ("Cannot fork test program");
  311. exit (1);
  312. }
  313. #else
  314. argv1 = malloc ((argc + 2) * sizeof(void *));
  315. argv1[0] = argv[0];
  316. argv1[1] = "-d";
  317. memcpy(argv1 + 2, argv + 1, argc * sizeof(void *));
  318. pid = vfork ();
  319. if (pid == 0)
  320. {
  321. /* This is the child. */
  322. /* We put the test process in its own pgrp so that if it bogusly
  323. generates any job control signals, they won't hit the whole build. */
  324. if (setpgid (0, 0) != 0)
  325. printf ("Failed to set the process group ID: %m\n");
  326. if (execve (argv1[0], argv1, envp) < 0)
  327. {
  328. perror ("Cannot exec test program");
  329. _exit (1);
  330. }
  331. }
  332. else if (pid < 0)
  333. {
  334. perror ("Cannot vfork test program");
  335. exit (1);
  336. }
  337. #endif
  338. #ifdef __XXX_HANDLE_CTRL_C
  339. signal (SIGTERM, handler_killpid);
  340. signal (SIGINT, handler_killpid);
  341. signal (SIGQUIT, handler_killpid);
  342. #endif
  343. /* Set timeout. */
  344. #ifndef TIMEOUT
  345. /* Default timeout is two seconds. */
  346. # define TIMEOUT 2
  347. #endif
  348. signal (SIGALRM, signal_handler);
  349. alarm (TIMEOUT * timeoutfactor);
  350. /* Make sure we clean up if the wrapper gets interrupted. */
  351. signal (SIGINT, signal_handler);
  352. /* Wait for the regular termination. */
  353. termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  354. if (termpid == -1)
  355. {
  356. printf ("Waiting for test program failed: %s\n", strerror(errno));
  357. exit (1);
  358. }
  359. if (termpid != pid)
  360. {
  361. printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
  362. (long int) pid, (long int) termpid);
  363. exit (1);
  364. }
  365. #ifndef EXPECTED_SIGNAL
  366. /* We don't expect any signal. */
  367. # define EXPECTED_SIGNAL 0
  368. #endif
  369. if (WTERMSIG (status) != EXPECTED_SIGNAL)
  370. {
  371. if (EXPECTED_SIGNAL != 0)
  372. {
  373. if (WTERMSIG (status) == 0)
  374. fprintf (stderr,
  375. "Expected signal '%s' from child, got none\n",
  376. strsignal (EXPECTED_SIGNAL));
  377. else
  378. fprintf (stderr,
  379. "Incorrect signal from child: got `%s', need `%s'\n",
  380. strsignal (WTERMSIG (status)),
  381. strsignal (EXPECTED_SIGNAL));
  382. }
  383. else
  384. fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
  385. strsignal (WTERMSIG (status)));
  386. exit (1);
  387. }
  388. /* Simply exit with the return value of the test. */
  389. #ifndef EXPECTED_STATUS
  390. return WEXITSTATUS (status);
  391. #else
  392. if (WEXITSTATUS (status) != EXPECTED_STATUS)
  393. {
  394. fprintf (stderr, "Expected status %d, got %d\n",
  395. EXPECTED_STATUS, WEXITSTATUS (status));
  396. exit (1);
  397. }
  398. return 0;
  399. #endif
  400. }