test-skeleton.c 10 KB

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