test-pexecute.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* Pexecute test program,
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor <ian@airs.com>.
  4. This file is part of GNU libiberty.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program 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
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #ifdef HAVE_CONFIG_H
  18. #include "config.h"
  19. #endif
  20. #include "ansidecl.h"
  21. #include "libiberty.h"
  22. #include <stdio.h>
  23. #include <signal.h>
  24. #include <errno.h>
  25. #ifdef HAVE_STRING_H
  26. #include <string.h>
  27. #endif
  28. #include <sys/types.h>
  29. #ifdef HAVE_STDLIB_H
  30. #include <stdlib.h>
  31. #endif
  32. #ifdef HAVE_UNISTD_H
  33. #include <unistd.h>
  34. #endif
  35. #ifdef HAVE_SYS_WAIT_H
  36. #include <sys/wait.h>
  37. #endif
  38. #ifdef HAVE_SYS_TIME_H
  39. #include <sys/time.h>
  40. #endif
  41. #ifdef HAVE_SYS_RESOURCE_H
  42. #include <sys/resource.h>
  43. #endif
  44. #ifndef WIFSIGNALED
  45. #define WIFSIGNALED(S) (((S) & 0xff) != 0 && ((S) & 0xff) != 0x7f)
  46. #endif
  47. #ifndef WTERMSIG
  48. #define WTERMSIG(S) ((S) & 0x7f)
  49. #endif
  50. #ifndef WIFEXITED
  51. #define WIFEXITED(S) (((S) & 0xff) == 0)
  52. #endif
  53. #ifndef WEXITSTATUS
  54. #define WEXITSTATUS(S) (((S) & 0xff00) >> 8)
  55. #endif
  56. #ifndef WSTOPSIG
  57. #define WSTOPSIG WEXITSTATUS
  58. #endif
  59. #ifndef WCOREDUMP
  60. #define WCOREDUMP(S) ((S) & WCOREFLG)
  61. #endif
  62. #ifndef WCOREFLG
  63. #define WCOREFLG 0200
  64. #endif
  65. #ifndef EXIT_SUCCESS
  66. #define EXIT_SUCCESS 0
  67. #endif
  68. #ifndef EXIT_FAILURE
  69. #define EXIT_FAILURE 1
  70. #endif
  71. /* When this program is run with no arguments, it runs some tests of
  72. the libiberty pexecute functions. As a test program, it simply
  73. invokes itself with various arguments.
  74. argv[1]:
  75. *empty string* Run tests, exit with success status
  76. exit Exit success
  77. error Exit error
  78. abort Abort
  79. echo Echo remaining arguments, exit success
  80. echoerr Echo next arg to stdout, next to stderr, repeat
  81. copy Copy stdin to stdout
  82. write Write stdin to file named in next argument
  83. */
  84. static void fatal_error (int, const char *, int) ATTRIBUTE_NORETURN;
  85. static void error (int, const char *);
  86. static void check_line (int, FILE *, const char *);
  87. static void do_cmd (int, char **) ATTRIBUTE_NORETURN;
  88. /* The number of errors we have seen. */
  89. static int error_count;
  90. /* Print a fatal error and exit. LINE is the line number where we
  91. detected the error, ERRMSG is the error message to print, and ERR
  92. is 0 or an errno value to print. */
  93. static void
  94. fatal_error (int line, const char *errmsg, int err)
  95. {
  96. fprintf (stderr, "test-pexecute:%d: %s", line, errmsg);
  97. if (errno != 0)
  98. fprintf (stderr, ": %s", xstrerror (err));
  99. fprintf (stderr, "\n");
  100. exit (EXIT_FAILURE);
  101. }
  102. #define FATAL_ERROR(ERRMSG, ERR) fatal_error (__LINE__, ERRMSG, ERR)
  103. /* Print an error message and bump the error count. LINE is the line
  104. number where we detected the error, ERRMSG is the error to
  105. print. */
  106. static void
  107. error (int line, const char *errmsg)
  108. {
  109. fprintf (stderr, "test-pexecute:%d: %s\n", line, errmsg);
  110. ++error_count;
  111. }
  112. #define ERROR(ERRMSG) error (__LINE__, ERRMSG)
  113. /* Check a line in a file. */
  114. static void
  115. check_line (int line, FILE *e, const char *str)
  116. {
  117. const char *p;
  118. int c;
  119. char buf[1000];
  120. p = str;
  121. while (1)
  122. {
  123. c = getc (e);
  124. if (*p == '\0')
  125. {
  126. if (c != '\n')
  127. {
  128. snprintf (buf, sizeof buf, "got '%c' when expecting newline", c);
  129. fatal_error (line, buf, 0);
  130. }
  131. c = getc (e);
  132. if (c != EOF)
  133. {
  134. snprintf (buf, sizeof buf, "got '%c' when expecting EOF", c);
  135. fatal_error (line, buf, 0);
  136. }
  137. return;
  138. }
  139. if (c != *p)
  140. {
  141. snprintf (buf, sizeof buf, "expected '%c', got '%c'", *p, c);
  142. fatal_error (line, buf, 0);
  143. }
  144. ++p;
  145. }
  146. }
  147. #define CHECK_LINE(E, STR) check_line (__LINE__, E, STR)
  148. /* Main function for the pexecute tester. Run the tests. */
  149. int
  150. main (int argc, char **argv)
  151. {
  152. int trace;
  153. struct pex_obj *test_pex_tmp;
  154. int test_pex_status;
  155. FILE *test_pex_file;
  156. struct pex_obj *pex1;
  157. char *subargv[10];
  158. int status;
  159. FILE *e;
  160. int statuses[10];
  161. trace = 0;
  162. if (argc > 1 && strcmp (argv[1], "-t") == 0)
  163. {
  164. trace = 1;
  165. --argc;
  166. ++argv;
  167. }
  168. if (argc > 1)
  169. do_cmd (argc, argv);
  170. #define TEST_PEX_INIT(FLAGS, TEMPBASE) \
  171. (((test_pex_tmp = pex_init (FLAGS, "test-pexecute", TEMPBASE)) \
  172. != NULL) \
  173. ? test_pex_tmp \
  174. : (FATAL_ERROR ("pex_init failed", 0), NULL))
  175. #define TEST_PEX_RUN(PEXOBJ, FLAGS, EXECUTABLE, ARGV, OUTNAME, ERRNAME) \
  176. do \
  177. { \
  178. int err; \
  179. const char *pex_run_err; \
  180. if (trace) \
  181. fprintf (stderr, "Line %d: running %s %s\n", \
  182. __LINE__, EXECUTABLE, ARGV[0]); \
  183. pex_run_err = pex_run (PEXOBJ, FLAGS, EXECUTABLE, ARGV, OUTNAME, \
  184. ERRNAME, &err); \
  185. if (pex_run_err != NULL) \
  186. FATAL_ERROR (pex_run_err, err); \
  187. } \
  188. while (0)
  189. #define TEST_PEX_GET_STATUS_1(PEXOBJ) \
  190. (pex_get_status (PEXOBJ, 1, &test_pex_status) \
  191. ? test_pex_status \
  192. : (FATAL_ERROR ("pex_get_status failed", errno), 1))
  193. #define TEST_PEX_GET_STATUS(PEXOBJ, COUNT, VECTOR) \
  194. do \
  195. { \
  196. if (!pex_get_status (PEXOBJ, COUNT, VECTOR)) \
  197. FATAL_ERROR ("pex_get_status failed", errno); \
  198. } \
  199. while (0)
  200. #define TEST_PEX_READ_OUTPUT(PEXOBJ) \
  201. ((test_pex_file = pex_read_output (PEXOBJ, 0)) != NULL \
  202. ? test_pex_file \
  203. : (FATAL_ERROR ("pex_read_output failed", errno), NULL))
  204. remove ("temp.x");
  205. remove ("temp.y");
  206. memset (subargv, 0, sizeof subargv);
  207. subargv[0] = "./test-pexecute";
  208. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, NULL);
  209. subargv[1] = "exit";
  210. subargv[2] = NULL;
  211. TEST_PEX_RUN (pex1, PEX_LAST, "./test-pexecute", subargv, NULL, NULL);
  212. status = TEST_PEX_GET_STATUS_1 (pex1);
  213. if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
  214. ERROR ("exit failed");
  215. pex_free (pex1);
  216. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, NULL);
  217. subargv[1] = "error";
  218. subargv[2] = NULL;
  219. TEST_PEX_RUN (pex1, PEX_LAST, "./test-pexecute", subargv, NULL, NULL);
  220. status = TEST_PEX_GET_STATUS_1 (pex1);
  221. if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_FAILURE)
  222. ERROR ("error test failed");
  223. pex_free (pex1);
  224. /* We redirect stderr to a file to avoid an error message which is
  225. printed on mingw32 when the child calls abort. */
  226. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, NULL);
  227. subargv[1] = "abort";
  228. subargv[2] = NULL;
  229. TEST_PEX_RUN (pex1, PEX_LAST, "./test-pexecute", subargv, NULL, "temp.z");
  230. status = TEST_PEX_GET_STATUS_1 (pex1);
  231. if (!WIFSIGNALED (status) || WTERMSIG (status) != SIGABRT)
  232. ERROR ("abort failed");
  233. pex_free (pex1);
  234. remove ("temp.z");
  235. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, "temp");
  236. subargv[1] = "echo";
  237. subargv[2] = "foo";
  238. subargv[3] = NULL;
  239. TEST_PEX_RUN (pex1, 0, "./test-pexecute", subargv, NULL, NULL);
  240. e = TEST_PEX_READ_OUTPUT (pex1);
  241. CHECK_LINE (e, "foo");
  242. if (TEST_PEX_GET_STATUS_1 (pex1) != 0)
  243. ERROR ("echo exit status failed");
  244. pex_free (pex1);
  245. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, "temp");
  246. subargv[1] = "echo";
  247. subargv[2] = "bar";
  248. subargv[3] = NULL;
  249. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", NULL);
  250. subargv[1] = "copy";
  251. subargv[2] = NULL;
  252. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  253. e = TEST_PEX_READ_OUTPUT (pex1);
  254. CHECK_LINE (e, "bar");
  255. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  256. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  257. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  258. ERROR ("copy exit status failed");
  259. pex_free (pex1);
  260. if (fopen ("temp.x", "r") != NULL || fopen ("temp.y", "r") != NULL)
  261. ERROR ("temporary files exist");
  262. pex1 = TEST_PEX_INIT (0, "temp");
  263. subargv[1] = "echo";
  264. subargv[2] = "bar";
  265. subargv[3] = NULL;
  266. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", NULL);
  267. subargv[1] = "copy";
  268. subargv[2] = NULL;
  269. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  270. e = TEST_PEX_READ_OUTPUT (pex1);
  271. CHECK_LINE (e, "bar");
  272. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  273. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  274. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  275. ERROR ("copy exit status failed");
  276. pex_free (pex1);
  277. if (fopen ("temp.x", "r") != NULL || fopen ("temp.y", "r") != NULL)
  278. ERROR ("temporary files exist");
  279. pex1 = TEST_PEX_INIT (PEX_SAVE_TEMPS, "temp");
  280. subargv[1] = "echo";
  281. subargv[2] = "quux";
  282. subargv[3] = NULL;
  283. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", NULL);
  284. subargv[1] = "copy";
  285. subargv[2] = NULL;
  286. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  287. e = TEST_PEX_READ_OUTPUT (pex1);
  288. CHECK_LINE (e, "quux");
  289. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  290. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  291. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  292. ERROR ("copy temp exit status failed");
  293. e = fopen ("temp.x", "r");
  294. if (e == NULL)
  295. FATAL_ERROR ("fopen temp.x failed in copy temp", errno);
  296. CHECK_LINE (e, "quux");
  297. fclose (e);
  298. e = fopen ("temp.y", "r");
  299. if (e == NULL)
  300. FATAL_ERROR ("fopen temp.y failed in copy temp", errno);
  301. CHECK_LINE (e, "quux");
  302. fclose (e);
  303. pex_free (pex1);
  304. remove ("temp.x");
  305. remove ("temp.y");
  306. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, "temp");
  307. subargv[1] = "echoerr";
  308. subargv[2] = "one";
  309. subargv[3] = "two";
  310. subargv[4] = NULL;
  311. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", "temp2.x");
  312. subargv[1] = "write";
  313. subargv[2] = "temp2.y";
  314. subargv[3] = NULL;
  315. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  316. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  317. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  318. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  319. ERROR ("echoerr exit status failed");
  320. pex_free (pex1);
  321. if (fopen ("temp.x", "r") != NULL || fopen ("temp.y", "r") != NULL)
  322. ERROR ("temporary files exist");
  323. e = fopen ("temp2.x", "r");
  324. if (e == NULL)
  325. FATAL_ERROR ("fopen temp2.x failed in echoerr", errno);
  326. CHECK_LINE (e, "two");
  327. fclose (e);
  328. e = fopen ("temp2.y", "r");
  329. if (e == NULL)
  330. FATAL_ERROR ("fopen temp2.y failed in echoerr", errno);
  331. CHECK_LINE (e, "one");
  332. fclose (e);
  333. remove ("temp2.x");
  334. remove ("temp2.y");
  335. /* Test the old pexecute interface. */
  336. {
  337. int pid1, pid2;
  338. char *errmsg_fmt;
  339. char *errmsg_arg;
  340. char errbuf1[1000];
  341. char errbuf2[1000];
  342. subargv[1] = "echo";
  343. subargv[2] = "oldpexecute";
  344. subargv[3] = NULL;
  345. pid1 = pexecute ("./test-pexecute", subargv, "test-pexecute", "temp",
  346. &errmsg_fmt, &errmsg_arg, PEXECUTE_FIRST);
  347. if (pid1 < 0)
  348. {
  349. snprintf (errbuf1, sizeof errbuf1, errmsg_fmt, errmsg_arg);
  350. snprintf (errbuf2, sizeof errbuf2, "pexecute 1 failed: %s", errbuf1);
  351. FATAL_ERROR (errbuf2, 0);
  352. }
  353. subargv[1] = "write";
  354. subargv[2] = "temp.y";
  355. subargv[3] = NULL;
  356. pid2 = pexecute ("./test-pexecute", subargv, "test-pexecute", "temp",
  357. &errmsg_fmt, &errmsg_arg, PEXECUTE_LAST);
  358. if (pid2 < 0)
  359. {
  360. snprintf (errbuf1, sizeof errbuf1, errmsg_fmt, errmsg_arg);
  361. snprintf (errbuf2, sizeof errbuf2, "pexecute 2 failed: %s", errbuf1);
  362. FATAL_ERROR (errbuf2, 0);
  363. }
  364. if (pwait (pid1, &status, 0) < 0)
  365. FATAL_ERROR ("write pwait 1 failed", errno);
  366. if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
  367. ERROR ("write exit status 1 failed");
  368. if (pwait (pid2, &status, 0) < 0)
  369. FATAL_ERROR ("write pwait 1 failed", errno);
  370. if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
  371. ERROR ("write exit status 2 failed");
  372. e = fopen ("temp.y", "r");
  373. if (e == NULL)
  374. FATAL_ERROR ("fopen temp.y failed in copy temp", errno);
  375. CHECK_LINE (e, "oldpexecute");
  376. fclose (e);
  377. remove ("temp.y");
  378. }
  379. if (trace)
  380. fprintf (stderr, "Exiting with status %d\n", error_count);
  381. return error_count;
  382. }
  383. /* Execute one of the special testing commands. */
  384. static void
  385. do_cmd (int argc, char **argv)
  386. {
  387. const char *s;
  388. /* Try to prevent generating a core dump. */
  389. #ifdef RLIMIT_CORE
  390. {
  391. struct rlimit r;
  392. r.rlim_cur = 0;
  393. r.rlim_max = 0;
  394. setrlimit (RLIMIT_CORE, &r);
  395. }
  396. #endif
  397. s = argv[1];
  398. if (strcmp (s, "exit") == 0)
  399. exit (EXIT_SUCCESS);
  400. else if (strcmp (s, "echo") == 0)
  401. {
  402. int i;
  403. for (i = 2; i < argc; ++i)
  404. {
  405. if (i > 2)
  406. putchar (' ');
  407. fputs (argv[i], stdout);
  408. }
  409. putchar ('\n');
  410. exit (EXIT_SUCCESS);
  411. }
  412. else if (strcmp (s, "echoerr") == 0)
  413. {
  414. int i;
  415. for (i = 2; i < argc; ++i)
  416. {
  417. if (i > 3)
  418. putc (' ', (i & 1) == 0 ? stdout : stderr);
  419. fputs (argv[i], (i & 1) == 0 ? stdout : stderr);
  420. }
  421. putc ('\n', stdout);
  422. putc ('\n', stderr);
  423. exit (EXIT_SUCCESS);
  424. }
  425. else if (strcmp (s, "error") == 0)
  426. exit (EXIT_FAILURE);
  427. else if (strcmp (s, "abort") == 0)
  428. abort ();
  429. else if (strcmp (s, "copy") == 0)
  430. {
  431. int c;
  432. while ((c = getchar ()) != EOF)
  433. putchar (c);
  434. exit (EXIT_SUCCESS);
  435. }
  436. else if (strcmp (s, "write") == 0)
  437. {
  438. FILE *e;
  439. int c;
  440. e = fopen (argv[2], "w");
  441. if (e == NULL)
  442. FATAL_ERROR ("fopen for write failed", errno);
  443. while ((c = getchar ()) != EOF)
  444. putc (c, e);
  445. if (fclose (e) != 0)
  446. FATAL_ERROR ("fclose for write failed", errno);
  447. exit (EXIT_SUCCESS);
  448. }
  449. else
  450. {
  451. char buf[1000];
  452. snprintf (buf, sizeof buf, "unrecognized command %s", argv[1]);
  453. FATAL_ERROR (buf, 0);
  454. }
  455. exit (EXIT_FAILURE);
  456. }