test-skeleton.c 11 KB

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