test-pexecute.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. /* Pexecute test program,
  2. Copyright (C) 2005-2022 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. /* Check empty parameters don't get lost. */
  246. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, "temp");
  247. subargv[1] = "echo";
  248. subargv[2] = "foo";
  249. subargv[3] = "";
  250. subargv[4] = "bar";
  251. subargv[5] = NULL;
  252. TEST_PEX_RUN (pex1, 0, "./test-pexecute", subargv, NULL, NULL);
  253. e = TEST_PEX_READ_OUTPUT (pex1);
  254. CHECK_LINE (e, "foo bar"); /* Two spaces! */
  255. if (TEST_PEX_GET_STATUS_1 (pex1) != 0)
  256. ERROR ("echo exit status failed");
  257. pex_free (pex1);
  258. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, "temp");
  259. subargv[1] = "echo";
  260. subargv[2] = "bar";
  261. subargv[3] = NULL;
  262. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", NULL);
  263. subargv[1] = "copy";
  264. subargv[2] = NULL;
  265. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  266. e = TEST_PEX_READ_OUTPUT (pex1);
  267. CHECK_LINE (e, "bar");
  268. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  269. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  270. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  271. ERROR ("copy exit status failed");
  272. pex_free (pex1);
  273. if (fopen ("temp.x", "r") != NULL || fopen ("temp.y", "r") != NULL)
  274. ERROR ("temporary files exist");
  275. pex1 = TEST_PEX_INIT (0, "temp");
  276. subargv[1] = "echo";
  277. subargv[2] = "bar";
  278. subargv[3] = NULL;
  279. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", NULL);
  280. subargv[1] = "copy";
  281. subargv[2] = NULL;
  282. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  283. e = TEST_PEX_READ_OUTPUT (pex1);
  284. CHECK_LINE (e, "bar");
  285. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  286. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  287. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  288. ERROR ("copy exit status failed");
  289. pex_free (pex1);
  290. if (fopen ("temp.x", "r") != NULL || fopen ("temp.y", "r") != NULL)
  291. ERROR ("temporary files exist");
  292. pex1 = TEST_PEX_INIT (PEX_SAVE_TEMPS, "temp");
  293. subargv[1] = "echo";
  294. subargv[2] = "quux";
  295. subargv[3] = NULL;
  296. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", NULL);
  297. subargv[1] = "copy";
  298. subargv[2] = NULL;
  299. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  300. e = TEST_PEX_READ_OUTPUT (pex1);
  301. CHECK_LINE (e, "quux");
  302. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  303. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  304. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  305. ERROR ("copy temp exit status failed");
  306. e = fopen ("temp.x", "r");
  307. if (e == NULL)
  308. FATAL_ERROR ("fopen temp.x failed in copy temp", errno);
  309. CHECK_LINE (e, "quux");
  310. fclose (e);
  311. e = fopen ("temp.y", "r");
  312. if (e == NULL)
  313. FATAL_ERROR ("fopen temp.y failed in copy temp", errno);
  314. CHECK_LINE (e, "quux");
  315. fclose (e);
  316. pex_free (pex1);
  317. remove ("temp.x");
  318. remove ("temp.y");
  319. pex1 = TEST_PEX_INIT (PEX_USE_PIPES, "temp");
  320. subargv[1] = "echoerr";
  321. subargv[2] = "one";
  322. subargv[3] = "two";
  323. subargv[4] = NULL;
  324. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".x", "temp2.x");
  325. subargv[1] = "write";
  326. subargv[2] = "temp2.y";
  327. subargv[3] = NULL;
  328. TEST_PEX_RUN (pex1, PEX_SUFFIX, "./test-pexecute", subargv, ".y", NULL);
  329. TEST_PEX_GET_STATUS (pex1, 2, statuses);
  330. if (!WIFEXITED (statuses[0]) || WEXITSTATUS (statuses[0]) != EXIT_SUCCESS
  331. || !WIFEXITED (statuses[1]) || WEXITSTATUS (statuses[1]) != EXIT_SUCCESS)
  332. ERROR ("echoerr exit status failed");
  333. pex_free (pex1);
  334. if (fopen ("temp.x", "r") != NULL || fopen ("temp.y", "r") != NULL)
  335. ERROR ("temporary files exist");
  336. e = fopen ("temp2.x", "r");
  337. if (e == NULL)
  338. FATAL_ERROR ("fopen temp2.x failed in echoerr", errno);
  339. CHECK_LINE (e, "two");
  340. fclose (e);
  341. e = fopen ("temp2.y", "r");
  342. if (e == NULL)
  343. FATAL_ERROR ("fopen temp2.y failed in echoerr", errno);
  344. CHECK_LINE (e, "one");
  345. fclose (e);
  346. remove ("temp2.x");
  347. remove ("temp2.y");
  348. /* Test the old pexecute interface. */
  349. {
  350. int pid1, pid2;
  351. char *errmsg_fmt;
  352. char *errmsg_arg;
  353. char errbuf1[1000];
  354. char errbuf2[1000];
  355. subargv[1] = "echo";
  356. subargv[2] = "oldpexecute";
  357. subargv[3] = NULL;
  358. pid1 = pexecute ("./test-pexecute", subargv, "test-pexecute", "temp",
  359. &errmsg_fmt, &errmsg_arg, PEXECUTE_FIRST);
  360. if (pid1 < 0)
  361. {
  362. snprintf (errbuf1, sizeof errbuf1, errmsg_fmt, errmsg_arg);
  363. snprintf (errbuf2, sizeof errbuf2, "pexecute 1 failed: %s", errbuf1);
  364. FATAL_ERROR (errbuf2, 0);
  365. }
  366. subargv[1] = "write";
  367. subargv[2] = "temp.y";
  368. subargv[3] = NULL;
  369. pid2 = pexecute ("./test-pexecute", subargv, "test-pexecute", "temp",
  370. &errmsg_fmt, &errmsg_arg, PEXECUTE_LAST);
  371. if (pid2 < 0)
  372. {
  373. snprintf (errbuf1, sizeof errbuf1, errmsg_fmt, errmsg_arg);
  374. snprintf (errbuf2, sizeof errbuf2, "pexecute 2 failed: %s", errbuf1);
  375. FATAL_ERROR (errbuf2, 0);
  376. }
  377. if (pwait (pid1, &status, 0) < 0)
  378. FATAL_ERROR ("write pwait 1 failed", errno);
  379. if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
  380. ERROR ("write exit status 1 failed");
  381. if (pwait (pid2, &status, 0) < 0)
  382. FATAL_ERROR ("write pwait 1 failed", errno);
  383. if (!WIFEXITED (status) || WEXITSTATUS (status) != EXIT_SUCCESS)
  384. ERROR ("write exit status 2 failed");
  385. e = fopen ("temp.y", "r");
  386. if (e == NULL)
  387. FATAL_ERROR ("fopen temp.y failed in copy temp", errno);
  388. CHECK_LINE (e, "oldpexecute");
  389. fclose (e);
  390. remove ("temp.y");
  391. }
  392. if (trace)
  393. fprintf (stderr, "Exiting with status %d\n", error_count);
  394. return error_count;
  395. }
  396. /* Execute one of the special testing commands. */
  397. static void
  398. do_cmd (int argc, char **argv)
  399. {
  400. const char *s;
  401. /* Try to prevent generating a core dump. */
  402. #ifdef RLIMIT_CORE
  403. {
  404. struct rlimit r;
  405. r.rlim_cur = 0;
  406. r.rlim_max = 0;
  407. setrlimit (RLIMIT_CORE, &r);
  408. }
  409. #endif
  410. s = argv[1];
  411. if (strcmp (s, "exit") == 0)
  412. exit (EXIT_SUCCESS);
  413. else if (strcmp (s, "echo") == 0)
  414. {
  415. int i;
  416. for (i = 2; i < argc; ++i)
  417. {
  418. if (i > 2)
  419. putchar (' ');
  420. fputs (argv[i], stdout);
  421. }
  422. putchar ('\n');
  423. exit (EXIT_SUCCESS);
  424. }
  425. else if (strcmp (s, "echoerr") == 0)
  426. {
  427. int i;
  428. for (i = 2; i < argc; ++i)
  429. {
  430. if (i > 3)
  431. putc (' ', (i & 1) == 0 ? stdout : stderr);
  432. fputs (argv[i], (i & 1) == 0 ? stdout : stderr);
  433. }
  434. putc ('\n', stdout);
  435. putc ('\n', stderr);
  436. exit (EXIT_SUCCESS);
  437. }
  438. else if (strcmp (s, "error") == 0)
  439. exit (EXIT_FAILURE);
  440. else if (strcmp (s, "abort") == 0)
  441. abort ();
  442. else if (strcmp (s, "copy") == 0)
  443. {
  444. int c;
  445. while ((c = getchar ()) != EOF)
  446. putchar (c);
  447. exit (EXIT_SUCCESS);
  448. }
  449. else if (strcmp (s, "write") == 0)
  450. {
  451. FILE *e;
  452. int c;
  453. e = fopen (argv[2], "w");
  454. if (e == NULL)
  455. FATAL_ERROR ("fopen for write failed", errno);
  456. while ((c = getchar ()) != EOF)
  457. putc (c, e);
  458. if (fclose (e) != 0)
  459. FATAL_ERROR ("fclose for write failed", errno);
  460. exit (EXIT_SUCCESS);
  461. }
  462. else
  463. {
  464. char buf[1000];
  465. snprintf (buf, sizeof buf, "unrecognized command %s", argv[1]);
  466. FATAL_ERROR (buf, 0);
  467. }
  468. exit (EXIT_FAILURE);
  469. }