test-skeleton.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  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. /* The test function is normally called `do_test' and it is called
  32. with argc and argv as the arguments. We nevertheless provide the
  33. possibility to overwrite this name. */
  34. #ifndef TEST_FUNCTION
  35. # define TEST_FUNCTION do_test (argc, argv)
  36. #endif
  37. #ifndef TEST_DATA_LIMIT
  38. # define TEST_DATA_LIMIT (64 << 20) /* Data limit (bytes) to run with. */
  39. #endif
  40. #define OPT_DIRECT 1000
  41. #define OPT_TESTDIR 1001
  42. static struct option options[] =
  43. {
  44. #ifdef CMDLINE_OPTIONS
  45. CMDLINE_OPTIONS
  46. #endif
  47. { "direct", no_argument, NULL, OPT_DIRECT },
  48. { "test-dir", required_argument, NULL, OPT_TESTDIR },
  49. { NULL, 0, NULL, 0 }
  50. };
  51. /* PID of the test itself. */
  52. static pid_t pid;
  53. /* Directory to place temporary files in. */
  54. static const char *test_dir;
  55. /* List of temporary files. */
  56. struct temp_name_list
  57. {
  58. struct qelem q;
  59. const char *name;
  60. } *temp_name_list;
  61. /* Add temporary files in list. */
  62. static void
  63. __attribute__ ((unused))
  64. add_temp_file (const char *name)
  65. {
  66. struct temp_name_list *newp
  67. = (struct temp_name_list *) calloc (sizeof (*newp), 1);
  68. if (newp != NULL)
  69. {
  70. newp->name = name;
  71. if (temp_name_list == NULL)
  72. temp_name_list = (struct temp_name_list *) &newp->q;
  73. else
  74. insque (newp, temp_name_list);
  75. }
  76. }
  77. #if defined __UCLIBC__ && !defined __UCLIBC_SUSV3_LEGACY__
  78. /* mktemp() isn't available, so fake it with tempnam().
  79. We make a lot of assumptions about the format of the
  80. "template" name, but we aren't testing that, so eh. */
  81. __attribute__ ((unused))
  82. static char *mktemp(char *template)
  83. {
  84. char *dir, *pfx, *s;
  85. dir = strdup (template);
  86. pfx = strrchr (dir, '/');
  87. *pfx++ = '\0';
  88. s = strstr (pfx, "XXXXXX");
  89. *s = '\0';
  90. s = tempnam (dir, pfx);
  91. strcpy (template, s);
  92. free (s);
  93. return template;
  94. }
  95. #endif
  96. /* Delete all temporary files. */
  97. static void
  98. delete_temp_files (void)
  99. {
  100. while (temp_name_list != NULL)
  101. {
  102. remove (temp_name_list->name);
  103. temp_name_list = (struct temp_name_list *) temp_name_list->q.q_forw;
  104. }
  105. }
  106. /* Create a temporary file. */
  107. static int
  108. __attribute__ ((unused))
  109. create_temp_file (const char *base, char **filename)
  110. {
  111. char *fname;
  112. int fd;
  113. fname = (char *) malloc (strlen (test_dir) + 1 + strlen (base)
  114. + sizeof ("XXXXXX"));
  115. if (fname == NULL)
  116. {
  117. puts ("out of memory");
  118. return -1;
  119. }
  120. strcpy (stpcpy (stpcpy (stpcpy (fname, test_dir), "/"), base), "XXXXXX");
  121. fd = mkstemp (fname);
  122. if (fd == -1)
  123. {
  124. printf ("cannot open temporary file '%s': %s\n", fname, strerror(errno));
  125. free (fname);
  126. return -1;
  127. }
  128. add_temp_file (fname);
  129. if (filename != NULL)
  130. *filename = fname;
  131. return fd;
  132. }
  133. /* Timeout handler. We kill the child and exit with an error. */
  134. static void
  135. __attribute__ ((noreturn))
  136. timeout_handler (int sig __attribute__ ((unused)))
  137. {
  138. int killed = 0;
  139. int status;
  140. int i;
  141. /* Send signal. */
  142. kill (pid, SIGKILL);
  143. /* Wait for it to terminate. */
  144. for (i = 0; i < 5; ++i)
  145. {
  146. struct timespec ts;
  147. killed = waitpid (pid, &status, WNOHANG|WUNTRACED);
  148. if (killed != 0)
  149. break;
  150. /* Delay, give the system time to process the kill. If the
  151. nanosleep() call return prematurely, all the better. We
  152. won't restart it since this probably means the child process
  153. finally died. */
  154. ts.tv_sec = 0;
  155. ts.tv_nsec = 100000000;
  156. nanosleep (&ts, NULL);
  157. }
  158. if (killed != 0 && killed != pid)
  159. {
  160. perror ("Failed to kill test process");
  161. exit (1);
  162. }
  163. #ifdef CLEANUP_HANDLER
  164. CLEANUP_HANDLER;
  165. #endif
  166. /* If we expected this signal: good! */
  167. #ifdef EXPECTED_SIGNAL
  168. if (EXPECTED_SIGNAL == SIGALRM)
  169. exit (0);
  170. #endif
  171. if (killed == 0 || (WIFSIGNALED (status) && WTERMSIG (status) == SIGKILL))
  172. fputs ("Timed out: killed the child process\n", stderr);
  173. else if (WIFSTOPPED (status))
  174. fprintf (stderr, "Timed out: the child process was %s\n",
  175. strsignal (WSTOPSIG (status)));
  176. else if (WIFSIGNALED (status))
  177. fprintf (stderr, "Timed out: the child process got signal %s\n",
  178. strsignal (WTERMSIG (status)));
  179. else
  180. fprintf (stderr, "Timed out: killed the child process but it exited %d\n",
  181. WEXITSTATUS (status));
  182. /* Exit with an error. */
  183. exit (1);
  184. }
  185. static void
  186. __attribute__ ((noreturn))
  187. handler_killpid(int sig)
  188. {
  189. kill(pid, SIGKILL); /* kill test */
  190. signal(sig, SIG_DFL);
  191. raise(sig); /* kill ourself */
  192. _exit(128 + sig); /* paranoia */
  193. }
  194. /* We provide the entry point here. */
  195. int
  196. main (int argc, char *argv[])
  197. {
  198. #ifdef __ARCH_USE_MMU__
  199. int direct = 0; /* Directly call the test function? */
  200. #else
  201. int direct = 1;
  202. #endif
  203. int status;
  204. int opt;
  205. unsigned int timeoutfactor = 1;
  206. pid_t termpid;
  207. char *envstr_timeoutfactor;
  208. /* Make uses of freed and uninitialized memory known. */
  209. #ifdef __MALLOC_STANDARD__
  210. #ifndef M_PERTURB
  211. # define M_PERTURB -6
  212. #endif
  213. mallopt (M_PERTURB, 42);
  214. #endif
  215. #ifdef STDOUT_UNBUFFERED
  216. setbuf (stdout, NULL);
  217. #endif
  218. while ((opt = getopt_long (argc, argv, "+", options, NULL)) != -1)
  219. switch (opt)
  220. {
  221. case '?':
  222. exit (1);
  223. case OPT_DIRECT:
  224. direct = 1;
  225. break;
  226. case OPT_TESTDIR:
  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. /* Correct for the possible parameters. */
  265. argv[optind - 1] = argv[0];
  266. argv += optind - 1;
  267. argc -= optind - 1;
  268. /* Call the initializing function, if one is available. */
  269. #ifdef PREPARE
  270. PREPARE (argc, argv);
  271. #endif
  272. /* If we are not expected to fork run the function immediately. */
  273. if (direct)
  274. return TEST_FUNCTION;
  275. /* Set up the test environment:
  276. - prevent core dumps
  277. - set up the timer
  278. - fork and execute the function. */
  279. #if defined __ARCH_USE_MMU__ || ! defined __UCLIBC__
  280. pid = fork ();
  281. if (pid == 0)
  282. {
  283. /* This is the child. */
  284. #ifdef RLIMIT_DATA
  285. struct rlimit data_limit;
  286. #endif
  287. #ifdef RLIMIT_CORE
  288. /* Try to avoid dumping core. */
  289. struct rlimit core_limit;
  290. core_limit.rlim_cur = 0;
  291. core_limit.rlim_max = 0;
  292. setrlimit (RLIMIT_CORE, &core_limit);
  293. #endif
  294. #ifdef RLIMIT_DATA
  295. /* Try to avoid eating all memory if a test leaks. */
  296. if (getrlimit (RLIMIT_DATA, &data_limit) == 0)
  297. {
  298. if (TEST_DATA_LIMIT == RLIM_INFINITY)
  299. data_limit.rlim_cur = data_limit.rlim_max;
  300. else if (data_limit.rlim_cur > (rlim_t) TEST_DATA_LIMIT)
  301. data_limit.rlim_cur = MIN ((rlim_t) TEST_DATA_LIMIT,
  302. data_limit.rlim_max);
  303. if (setrlimit (RLIMIT_DATA, &data_limit) < 0)
  304. perror ("setrlimit: RLIMIT_DATA");
  305. }
  306. else
  307. perror ("getrlimit: RLIMIT_DATA");
  308. #endif
  309. /* We put the test process in its own pgrp so that if it bogusly
  310. generates any job control signals, they won't hit the whole build. */
  311. setpgid (0, 0);
  312. /* Execute the test function and exit with the return value. */
  313. exit (TEST_FUNCTION);
  314. }
  315. else if (pid < 0)
  316. #endif
  317. {
  318. perror ("Cannot fork test program");
  319. exit (1);
  320. }
  321. signal (SIGTERM, handler_killpid);
  322. signal (SIGINT, handler_killpid);
  323. signal (SIGQUIT, handler_killpid);
  324. /* Set timeout. */
  325. #ifndef TIMEOUT
  326. /* Default timeout is two seconds. */
  327. # define TIMEOUT 2
  328. #endif
  329. signal (SIGALRM, timeout_handler);
  330. alarm (TIMEOUT * timeoutfactor);
  331. /* Wait for the regular termination. */
  332. termpid = TEMP_FAILURE_RETRY (waitpid (pid, &status, 0));
  333. if (termpid == -1)
  334. {
  335. printf ("Waiting for test program failed: %s\n", strerror(errno));
  336. exit (1);
  337. }
  338. if (termpid != pid)
  339. {
  340. printf ("Oops, wrong test program terminated: expected %ld, got %ld\n",
  341. (long int) pid, (long int) termpid);
  342. exit (1);
  343. }
  344. #ifndef EXPECTED_SIGNAL
  345. /* We don't expect any signal. */
  346. # define EXPECTED_SIGNAL 0
  347. #endif
  348. if (WTERMSIG (status) != EXPECTED_SIGNAL)
  349. {
  350. if (EXPECTED_SIGNAL != 0)
  351. {
  352. if (WTERMSIG (status) == 0)
  353. fprintf (stderr,
  354. "Expected signal '%s' from child, got none\n",
  355. strsignal (EXPECTED_SIGNAL));
  356. else
  357. fprintf (stderr,
  358. "Incorrect signal from child: got `%s', need `%s'\n",
  359. strsignal (WTERMSIG (status)),
  360. strsignal (EXPECTED_SIGNAL));
  361. }
  362. else
  363. fprintf (stderr, "Didn't expect signal from child: got `%s'\n",
  364. strsignal (WTERMSIG (status)));
  365. exit (1);
  366. }
  367. /* Simply exit with the return value of the test. */
  368. #ifndef EXPECTED_STATUS
  369. return WEXITSTATUS (status);
  370. #else
  371. if (WEXITSTATUS (status) != EXPECTED_STATUS)
  372. {
  373. fprintf (stderr, "Expected status %d, got %d\n",
  374. EXPECTED_STATUS, WEXITSTATUS (status));
  375. exit (1);
  376. }
  377. return 0;
  378. #endif
  379. }