test-skeleton.c 10 KB

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