test-skeleton.c 10 KB

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