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