]>
Commit | Line | Data |
---|---|---|
1 | /*- | |
2 | * SPDX-License-Identifier: BSD-3-Clause | |
3 | * | |
4 | * Copyright (c) 1990, 1993 | |
5 | * The Regents of the University of California. All rights reserved. | |
6 | * | |
7 | * This code is derived from software contributed to Berkeley by | |
8 | * John B. Roll Jr. | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or without | |
11 | * modification, are permitted provided that the following conditions | |
12 | * are met: | |
13 | * 1. Redistributions of source code must retain the above copyright | |
14 | * notice, this list of conditions and the following disclaimer. | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in the | |
17 | * documentation and/or other materials provided with the distribution. | |
18 | * 3. Neither the name of the University nor the names of its contributors | |
19 | * may be used to endorse or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
32 | * SUCH DAMAGE. | |
33 | * | |
34 | * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $ | |
35 | */ | |
36 | ||
37 | #if 0 | |
38 | #ifndef lint | |
39 | static const char copyright[] = | |
40 | "@(#) Copyright (c) 1990, 1993\n\ | |
41 | The Regents of the University of California. All rights reserved.\n"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #ifndef lint | |
45 | static char sccsid[] = "@(#)xargs.c 8.1 (Berkeley) 6/6/93"; | |
46 | #endif /* not lint */ | |
47 | #endif | |
48 | #include <sys/cdefs.h> | |
49 | __FBSDID("$FreeBSD$"); | |
50 | ||
51 | #include <sys/types.h> | |
52 | #include <sys/wait.h> | |
53 | #include <sys/time.h> | |
54 | #include <sys/param.h> | |
55 | #include <sys/resource.h> | |
56 | #include <err.h> | |
57 | #include <errno.h> | |
58 | #include <fcntl.h> | |
59 | #include <langinfo.h> | |
60 | #include <locale.h> | |
61 | #include <paths.h> | |
62 | #include <regex.h> | |
63 | #include <stdio.h> | |
64 | #include <stdlib.h> | |
65 | #include <string.h> | |
66 | #include <unistd.h> | |
67 | ||
68 | #include "pathnames.h" | |
69 | ||
70 | #ifdef __APPLE__ | |
71 | #include <get_compat.h> | |
72 | #else | |
73 | #define COMPAT_MODE(a,b) (1) | |
74 | #endif /* __APPLE__ */ | |
75 | ||
76 | static void parse_input(int, char *[]); | |
77 | static void prerun(int, char *[]); | |
78 | static int prompt(void); | |
79 | static void run(char **); | |
80 | static void usage(void); | |
81 | void strnsubst(char **, const char *, const char *, size_t); | |
82 | static pid_t xwait(int block, int *status); | |
83 | static void xexit(const char *, const int); | |
84 | static void waitchildren(const char *, int); | |
85 | static void pids_init(void); | |
86 | static int pids_empty(void); | |
87 | static int pids_full(void); | |
88 | static void pids_add(pid_t pid); | |
89 | static int pids_remove(pid_t pid); | |
90 | static int findslot(pid_t pid); | |
91 | static int findfreeslot(void); | |
92 | static void clearslot(int slot); | |
93 | ||
94 | static int last_was_newline = 1; | |
95 | static int last_was_blank = 0; | |
96 | ||
97 | static char echo[] = _PATH_ECHO; | |
98 | static char **av, **bxp, **ep, **endxp, **xp; | |
99 | static char *argp, *bbp, *ebp, *inpline, *p, *replstr; | |
100 | static const char *eofstr; | |
101 | static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag; | |
102 | static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag; | |
103 | static int curprocs, maxprocs; | |
104 | static size_t pad9314053; | |
105 | static pid_t *childpids; | |
106 | ||
107 | static volatile int childerr; | |
108 | ||
109 | extern char **environ; | |
110 | ||
111 | int | |
112 | main(int argc, char *argv[]) | |
113 | { | |
114 | long arg_max; | |
115 | int ch, Jflag, nflag, nline; | |
116 | size_t nargs; | |
117 | size_t linelen; | |
118 | struct rlimit rl; | |
119 | char *endptr; | |
120 | const char *errstr; | |
121 | ||
122 | inpline = replstr = NULL; | |
123 | ep = environ; | |
124 | eofstr = ""; | |
125 | Jflag = nflag = 0; | |
126 | ||
127 | (void)setlocale(LC_ALL, ""); | |
128 | ||
129 | /* | |
130 | * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that | |
131 | * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given | |
132 | * that the smallest argument is 2 bytes in length, this means that | |
133 | * the number of arguments is limited to: | |
134 | * | |
135 | * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2. | |
136 | * | |
137 | * We arbitrarily limit the number of arguments to 5000. This is | |
138 | * allowed by POSIX.2 as long as the resulting minimum exec line is | |
139 | * at least LINE_MAX. Realloc'ing as necessary is possible, but | |
140 | * probably not worthwhile. | |
141 | */ | |
142 | nargs = 5000; | |
143 | if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) | |
144 | errx(1, "sysconf(_SC_ARG_MAX) failed"); | |
145 | nline = arg_max - MAXPATHLEN; /* for argv[0] from execvp() */ | |
146 | pad9314053 = sizeof(char *); /* reserve for string area rounding */ | |
147 | while (*ep != NULL) { | |
148 | /* 1 byte for each '\0' */ | |
149 | nline -= strlen(*ep++) + 1 + sizeof(*ep); | |
150 | } | |
151 | nline -= pad9314053; | |
152 | maxprocs = 1; | |
153 | while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:S:s:rtx")) != -1) | |
154 | switch (ch) { | |
155 | case 'E': | |
156 | eofstr = optarg; | |
157 | break; | |
158 | case 'I': | |
159 | Jflag = 0; | |
160 | Iflag = 1; | |
161 | Lflag = 1; | |
162 | replstr = optarg; | |
163 | break; | |
164 | case 'J': | |
165 | Iflag = 0; | |
166 | Jflag = 1; | |
167 | replstr = optarg; | |
168 | break; | |
169 | case 'L': | |
170 | Lflag = strtonum(optarg, 0, INT_MAX, &errstr); | |
171 | if (errstr) | |
172 | errx(1, "-L %s: %s", optarg, errstr); | |
173 | if (COMPAT_MODE("bin/xargs", "Unix2003")) { | |
174 | nflag = 0; /* Override */ | |
175 | nargs = 5000; | |
176 | } | |
177 | break; | |
178 | case 'n': | |
179 | nflag = 1; | |
180 | if ((nargs = strtol(optarg, NULL, 10)) <= 0) | |
181 | errx(1, "illegal argument count"); | |
182 | if (COMPAT_MODE("bin/xargs", "Unix2003")) { | |
183 | Lflag = 0; /* Override */ | |
184 | } | |
185 | break; | |
186 | case 'o': | |
187 | oflag = 1; | |
188 | break; | |
189 | case 'P': | |
190 | maxprocs = strtonum(optarg, 0, INT_MAX, &errstr); | |
191 | if (errstr) | |
192 | errx(1, "-P %s: %s", optarg, errstr); | |
193 | if (getrlimit(RLIMIT_NPROC, &rl) != 0) | |
194 | errx(1, "getrlimit failed"); | |
195 | if (maxprocs == 0 || maxprocs > rl.rlim_cur) | |
196 | maxprocs = rl.rlim_cur; | |
197 | break; | |
198 | case 'p': | |
199 | pflag = 1; | |
200 | break; | |
201 | case 'R': | |
202 | Rflag = strtol(optarg, &endptr, 10); | |
203 | if (*endptr != '\0') | |
204 | errx(1, "replacements must be a number"); | |
205 | break; | |
206 | case 'r': | |
207 | /* GNU compatibility */ | |
208 | break; | |
209 | case 'S': | |
210 | Sflag = strtoul(optarg, &endptr, 10); | |
211 | if (*endptr != '\0') | |
212 | errx(1, "replsize must be a number"); | |
213 | break; | |
214 | case 's': | |
215 | nline = strtonum(optarg, 0, INT_MAX, &errstr); | |
216 | if (errstr) | |
217 | errx(1, "-s %s: %s", optarg, errstr); | |
218 | pad9314053 = 0; /* assume the -s value is valid */ | |
219 | break; | |
220 | case 't': | |
221 | tflag = 1; | |
222 | break; | |
223 | case 'x': | |
224 | xflag = 1; | |
225 | break; | |
226 | case '0': | |
227 | zflag = 1; | |
228 | break; | |
229 | case '?': | |
230 | default: | |
231 | usage(); | |
232 | } | |
233 | argc -= optind; | |
234 | argv += optind; | |
235 | ||
236 | if (!Iflag && Rflag) | |
237 | usage(); | |
238 | if (!Iflag && Sflag) | |
239 | usage(); | |
240 | if (Iflag && !Rflag) | |
241 | Rflag = 5; | |
242 | if (Iflag && !Sflag) | |
243 | Sflag = 255; | |
244 | if (xflag && !nflag) | |
245 | usage(); | |
246 | if (Iflag || Lflag) | |
247 | xflag = 1; | |
248 | if (replstr != NULL && *replstr == '\0') | |
249 | errx(1, "replstr may not be empty"); | |
250 | ||
251 | pids_init(); | |
252 | ||
253 | /* | |
254 | * Allocate pointers for the utility name, the utility arguments, | |
255 | * the maximum arguments to be read from stdin and the trailing | |
256 | * NULL. | |
257 | */ | |
258 | linelen = 1 + argc + nargs + 1; | |
259 | if ((av = bxp = malloc(linelen * sizeof(char *))) == NULL) | |
260 | errx(1, "malloc failed"); | |
261 | ||
262 | /* | |
263 | * Use the user's name for the utility as argv[0], just like the | |
264 | * shell. Echo is the default. Set up pointers for the user's | |
265 | * arguments. | |
266 | */ | |
267 | if (*argv == NULL) | |
268 | cnt = strlen(*bxp++ = echo) + pad9314053; | |
269 | else { | |
270 | do { | |
271 | if (Jflag && strcmp(*argv, replstr) == 0) { | |
272 | char **avj; | |
273 | jfound = 1; | |
274 | argv++; | |
275 | for (avj = argv; *avj; avj++) | |
276 | cnt += strlen(*avj) + 1 + pad9314053; | |
277 | break; | |
278 | } | |
279 | cnt += strlen(*bxp++ = *argv) + 1 + pad9314053; | |
280 | } while (*++argv != NULL); | |
281 | } | |
282 | ||
283 | /* | |
284 | * Set up begin/end/traversing pointers into the array. The -n | |
285 | * count doesn't include the trailing NULL pointer, so the malloc | |
286 | * added in an extra slot. | |
287 | */ | |
288 | endxp = (xp = bxp) + nargs; | |
289 | ||
290 | /* | |
291 | * Allocate buffer space for the arguments read from stdin and the | |
292 | * trailing NULL. Buffer space is defined as the default or specified | |
293 | * space, minus the length of the utility name and arguments. Set up | |
294 | * begin/end/traversing pointers into the array. The -s count does | |
295 | * include the trailing NULL, so the malloc didn't add in an extra | |
296 | * slot. | |
297 | */ | |
298 | nline -= cnt; | |
299 | if (nline <= 0) | |
300 | errx(1, "insufficient space for command"); | |
301 | ||
302 | if ((bbp = malloc((size_t)(nline + 1))) == NULL) | |
303 | errx(1, "malloc failed"); | |
304 | ebp = (argp = p = bbp) + nline - 1; | |
305 | for (;;) | |
306 | parse_input(argc, argv); | |
307 | } | |
308 | ||
309 | static void | |
310 | parse_input(int argc, char *argv[]) | |
311 | { | |
312 | int ch, foundeof; | |
313 | char **avj; | |
314 | int last_was_backslashed = 0; | |
315 | ||
316 | foundeof = 0; | |
317 | ||
318 | switch (ch = getchar()) { | |
319 | case EOF: | |
320 | /* No arguments since last exec. */ | |
321 | if (p == bbp) | |
322 | xexit(*av, rval); | |
323 | goto arg1; | |
324 | case ' ': | |
325 | last_was_blank = 1; | |
326 | case '\t': | |
327 | /* Quotes escape tabs and spaces. */ | |
328 | if (insingle || indouble || zflag) | |
329 | goto addch; | |
330 | goto arg2; | |
331 | case '\0': | |
332 | if (zflag) { | |
333 | /* | |
334 | * Increment 'count', so that nulls will be treated | |
335 | * as end-of-line, as well as end-of-argument. This | |
336 | * is needed so -0 works properly with -I and -L. | |
337 | */ | |
338 | count++; | |
339 | goto arg2; | |
340 | } | |
341 | goto addch; | |
342 | case '\n': | |
343 | if (zflag) | |
344 | goto addch; | |
345 | if (COMPAT_MODE("bin/xargs", "Unix2003")) { | |
346 | if (last_was_newline) { | |
347 | /* don't count empty line */ | |
348 | break; | |
349 | } | |
350 | if (!last_was_blank ) { | |
351 | /* only count if NOT continuation line */ | |
352 | count++; | |
353 | } | |
354 | } else { | |
355 | count++; | |
356 | } | |
357 | last_was_newline = 1; | |
358 | ||
359 | /* Quotes do not escape newlines. */ | |
360 | arg1: if (insingle || indouble) { | |
361 | warnx("unterminated quote"); | |
362 | xexit(*av, 1); | |
363 | } | |
364 | arg2: | |
365 | foundeof = *eofstr != '\0' && | |
366 | strncmp(argp, eofstr, p - argp) == 0; | |
367 | ||
368 | #ifdef __APPLE__ | |
369 | /* 6591323: -I specifies that it processes the entire line, | |
370 | * so only recognize eofstr at the end of a line. */ | |
371 | if (Iflag && !last_was_newline) | |
372 | foundeof = 0; | |
373 | ||
374 | /* 6591323: Essentially the same as the EOF handling above. */ | |
375 | if (foundeof && (p - strlen(eofstr) == bbp)) { | |
376 | waitchildren(*argv, 1); | |
377 | exit(rval); | |
378 | } | |
379 | #endif | |
380 | ||
381 | /* Do not make empty args unless they are quoted */ | |
382 | if ((argp != p || wasquoted) && !foundeof) { | |
383 | *p++ = '\0'; | |
384 | *xp++ = argp; | |
385 | if (Iflag) { | |
386 | size_t curlen; | |
387 | ||
388 | if (inpline == NULL) | |
389 | curlen = 0; | |
390 | else { | |
391 | /* | |
392 | * If this string is not zero | |
393 | * length, append a space for | |
394 | * separation before the next | |
395 | * argument. | |
396 | */ | |
397 | if ((curlen = strlen(inpline))) | |
398 | strcat(inpline, " "); | |
399 | } | |
400 | curlen++; | |
401 | /* | |
402 | * Allocate enough to hold what we will | |
403 | * be holding in a second, and to append | |
404 | * a space next time through, if we have | |
405 | * to. | |
406 | */ | |
407 | inpline = realloc(inpline, curlen + 2 + | |
408 | strlen(argp)); | |
409 | if (inpline == NULL) { | |
410 | warnx("realloc failed"); | |
411 | xexit(*av, 1); | |
412 | } | |
413 | if (curlen == 1) | |
414 | strcpy(inpline, argp); | |
415 | else | |
416 | strcat(inpline, argp); | |
417 | } | |
418 | } | |
419 | ||
420 | /* | |
421 | * If max'd out on args or buffer, or reached EOF, | |
422 | * run the command. If xflag and max'd out on buffer | |
423 | * but not on args, object. Having reached the limit | |
424 | * of input lines, as specified by -L is the same as | |
425 | * maxing out on arguments. | |
426 | */ | |
427 | if (xp == endxp || p + (count * pad9314053) > ebp || ch == EOF || | |
428 | (Lflag <= count && xflag) || foundeof) { | |
429 | if (xflag && xp != endxp && p + (count * pad9314053) > ebp) { | |
430 | warnx("insufficient space for arguments"); | |
431 | xexit(*av, 1); | |
432 | } | |
433 | if (jfound) { | |
434 | for (avj = argv; *avj; avj++) | |
435 | *xp++ = *avj; | |
436 | } | |
437 | prerun(argc, av); | |
438 | if (ch == EOF || foundeof) | |
439 | xexit(*av, rval); | |
440 | p = bbp; | |
441 | xp = bxp; | |
442 | count = 0; | |
443 | } | |
444 | argp = p; | |
445 | wasquoted = 0; | |
446 | break; | |
447 | case '\'': | |
448 | if (indouble || zflag) | |
449 | goto addch; | |
450 | insingle = !insingle; | |
451 | wasquoted = 1; | |
452 | break; | |
453 | case '"': | |
454 | if (insingle || zflag) | |
455 | goto addch; | |
456 | indouble = !indouble; | |
457 | wasquoted = 1; | |
458 | break; | |
459 | case '\\': | |
460 | last_was_backslashed = 1; | |
461 | if (zflag) | |
462 | goto addch; | |
463 | /* Backslash escapes anything, is escaped by quotes. */ | |
464 | if (!insingle && !indouble && (ch = getchar()) == EOF) { | |
465 | warnx("backslash at EOF"); | |
466 | xexit(*av, 1); | |
467 | } | |
468 | /* FALLTHROUGH */ | |
469 | default: | |
470 | addch: if (p < ebp) { | |
471 | *p++ = ch; | |
472 | break; | |
473 | } | |
474 | ||
475 | /* If only one argument, not enough buffer space. */ | |
476 | if (bxp == xp) { | |
477 | warnx("insufficient space for argument"); | |
478 | xexit(*av, 1); | |
479 | } | |
480 | /* Didn't hit argument limit, so if xflag object. */ | |
481 | if (xflag) { | |
482 | warnx("insufficient space for arguments"); | |
483 | xexit(*av, 1); | |
484 | } | |
485 | ||
486 | if (jfound) { | |
487 | for (avj = argv; *avj; avj++) | |
488 | *xp++ = *avj; | |
489 | } | |
490 | prerun(argc, av); | |
491 | xp = bxp; | |
492 | cnt = ebp - argp; | |
493 | memcpy(bbp, argp, (size_t)cnt); | |
494 | p = (argp = bbp) + cnt; | |
495 | *p++ = ch; | |
496 | break; | |
497 | } | |
498 | if (ch != ' ') | |
499 | last_was_blank = 0; | |
500 | if (ch != '\n' || last_was_backslashed) | |
501 | last_was_newline = 0; | |
502 | } | |
503 | ||
504 | /* | |
505 | * Do things necessary before run()'ing, such as -I substitution, | |
506 | * and then call run(). | |
507 | */ | |
508 | static void | |
509 | prerun(int argc, char *argv[]) | |
510 | { | |
511 | char **tmp, **tmp2, **avj; | |
512 | int repls; | |
513 | ||
514 | repls = Rflag; | |
515 | ||
516 | if (argc == 0 || repls == 0) { | |
517 | *xp = NULL; | |
518 | run(argv); | |
519 | return; | |
520 | } | |
521 | ||
522 | avj = argv; | |
523 | ||
524 | /* | |
525 | * Allocate memory to hold the argument list, and | |
526 | * a NULL at the tail. | |
527 | */ | |
528 | tmp = malloc((argc + 1) * sizeof(char *)); | |
529 | if (tmp == NULL) { | |
530 | warnx("malloc failed"); | |
531 | xexit(*argv, 1); | |
532 | } | |
533 | tmp2 = tmp; | |
534 | ||
535 | /* | |
536 | * Save the first argument and iterate over it, we | |
537 | * cannot do strnsubst() to it. | |
538 | */ | |
539 | if ((*tmp++ = strdup(*avj++)) == NULL) { | |
540 | warnx("strdup failed"); | |
541 | xexit(*argv, 1); | |
542 | } | |
543 | ||
544 | /* | |
545 | * For each argument to utility, if we have not used up | |
546 | * the number of replacements we are allowed to do, and | |
547 | * if the argument contains at least one occurrence of | |
548 | * replstr, call strnsubst(), else just save the string. | |
549 | * Iterations over elements of avj and tmp are done | |
550 | * where appropriate. | |
551 | */ | |
552 | while (--argc) { | |
553 | *tmp = *avj++; | |
554 | if (repls && strstr(*tmp, replstr) != NULL) { | |
555 | strnsubst(tmp++, replstr, inpline, (size_t)Sflag); | |
556 | if (repls > 0) | |
557 | repls--; | |
558 | } else { | |
559 | if ((*tmp = strdup(*tmp)) == NULL) { | |
560 | warnx("strdup failed"); | |
561 | xexit(*argv, 1); | |
562 | } | |
563 | tmp++; | |
564 | } | |
565 | } | |
566 | ||
567 | /* | |
568 | * Run it. | |
569 | */ | |
570 | *tmp = NULL; | |
571 | run(tmp2); | |
572 | ||
573 | /* | |
574 | * Walk from the tail to the head, free along the way. | |
575 | */ | |
576 | for (; tmp2 != tmp; tmp--) | |
577 | free(*tmp); | |
578 | /* | |
579 | * Now free the list itself. | |
580 | */ | |
581 | free(tmp2); | |
582 | ||
583 | /* | |
584 | * Free the input line buffer, if we have one. | |
585 | */ | |
586 | if (inpline != NULL) { | |
587 | free(inpline); | |
588 | inpline = NULL; | |
589 | } | |
590 | } | |
591 | ||
592 | static void | |
593 | run(char **argv) | |
594 | { | |
595 | pid_t pid; | |
596 | int fd; | |
597 | char **avec; | |
598 | ||
599 | /* | |
600 | * If the user wants to be notified of each command before it is | |
601 | * executed, notify them. If they want the notification to be | |
602 | * followed by a prompt, then prompt them. | |
603 | */ | |
604 | if (tflag || pflag) { | |
605 | (void)fprintf(stderr, "%s", *argv); | |
606 | for (avec = argv + 1; *avec != NULL; ++avec) | |
607 | (void)fprintf(stderr, " %s", *avec); | |
608 | /* | |
609 | * If the user has asked to be prompted, do so. | |
610 | */ | |
611 | if (pflag) | |
612 | /* | |
613 | * If they asked not to exec, return without execution | |
614 | * but if they asked to, go to the execution. If we | |
615 | * could not open their tty, break the switch and drop | |
616 | * back to -t behaviour. | |
617 | */ | |
618 | switch (prompt()) { | |
619 | case 0: | |
620 | return; | |
621 | case 1: | |
622 | goto exec; | |
623 | case 2: | |
624 | break; | |
625 | } | |
626 | (void)fprintf(stderr, "\n"); | |
627 | (void)fflush(stderr); | |
628 | } | |
629 | exec: | |
630 | childerr = 0; | |
631 | switch (pid = vfork()) { | |
632 | case -1: | |
633 | warn("vfork"); | |
634 | xexit(*argv, 1); | |
635 | case 0: | |
636 | if (oflag) { | |
637 | if ((fd = open(_PATH_TTY, O_RDONLY)) == -1) | |
638 | err(1, "can't open /dev/tty"); | |
639 | } else { | |
640 | fd = open(_PATH_DEVNULL, O_RDONLY); | |
641 | } | |
642 | if (fd > STDIN_FILENO) { | |
643 | if (dup2(fd, STDIN_FILENO) != 0) | |
644 | err(1, "can't dup2 to stdin"); | |
645 | close(fd); | |
646 | } | |
647 | execvp(argv[0], argv); | |
648 | childerr = errno; | |
649 | _exit(1); | |
650 | } | |
651 | pids_add(pid); | |
652 | waitchildren(*argv, 0); | |
653 | } | |
654 | ||
655 | /* | |
656 | * Wait for a tracked child to exit and return its pid and exit status. | |
657 | * | |
658 | * Ignores (discards) all untracked child processes. | |
659 | * Returns -1 and sets errno to ECHILD if no tracked children exist. | |
660 | * If block is set, waits indefinitely for a child process to exit. | |
661 | * If block is not set and no children have exited, returns 0 immediately. | |
662 | */ | |
663 | static pid_t | |
664 | xwait(int block, int *status) { | |
665 | pid_t pid; | |
666 | ||
667 | if (pids_empty()) { | |
668 | errno = ECHILD; | |
669 | return (-1); | |
670 | } | |
671 | ||
672 | while ((pid = waitpid(-1, status, block ? 0 : WNOHANG)) > 0) | |
673 | if (pids_remove(pid)) | |
674 | break; | |
675 | ||
676 | return (pid); | |
677 | } | |
678 | ||
679 | static void | |
680 | xexit(const char *name, const int exit_code) { | |
681 | waitchildren(name, 1); | |
682 | exit(exit_code); | |
683 | } | |
684 | ||
685 | static void | |
686 | waitchildren(const char *name, int waitall) | |
687 | { | |
688 | pid_t pid; | |
689 | int status; | |
690 | int cause_exit = 0; | |
691 | ||
692 | while ((pid = xwait(waitall || pids_full(), &status)) > 0) { | |
693 | /* | |
694 | * If we couldn't invoke the utility or if utility exited | |
695 | * because of a signal or with a value of 255, warn (per | |
696 | * POSIX), and then wait until all other children have | |
697 | * exited before exiting 1-125. POSIX requires us to stop | |
698 | * reading if child exits because of a signal or with 255, | |
699 | * but it does not require us to exit immediately; waiting | |
700 | * is preferable to orphaning. | |
701 | */ | |
702 | if (childerr != 0 && cause_exit == 0) { | |
703 | errno = childerr; | |
704 | waitall = 1; | |
705 | cause_exit = errno == ENOENT ? 127 : 126; | |
706 | warn("%s", name); | |
707 | } else if (WIFSIGNALED(status)) { | |
708 | waitall = cause_exit = 1; | |
709 | warnx("%s: terminated with signal %d; aborting", | |
710 | name, WTERMSIG(status)); | |
711 | } else if (WEXITSTATUS(status) == 255) { | |
712 | waitall = cause_exit = 1; | |
713 | warnx("%s: exited with status 255; aborting", name); | |
714 | } else if (WEXITSTATUS(status)) | |
715 | rval = 1; | |
716 | } | |
717 | ||
718 | if (cause_exit) | |
719 | exit(cause_exit); | |
720 | if (pid == -1 && errno != ECHILD) | |
721 | err(1, "waitpid"); | |
722 | } | |
723 | ||
724 | #define NOPID (0) | |
725 | ||
726 | static void | |
727 | pids_init(void) | |
728 | { | |
729 | int i; | |
730 | ||
731 | if ((childpids = malloc(maxprocs * sizeof(*childpids))) == NULL) | |
732 | errx(1, "malloc failed"); | |
733 | ||
734 | for (i = 0; i < maxprocs; i++) | |
735 | clearslot(i); | |
736 | } | |
737 | ||
738 | static int | |
739 | pids_empty(void) | |
740 | { | |
741 | ||
742 | return (curprocs == 0); | |
743 | } | |
744 | ||
745 | static int | |
746 | pids_full(void) | |
747 | { | |
748 | ||
749 | return (curprocs >= maxprocs); | |
750 | } | |
751 | ||
752 | static void | |
753 | pids_add(pid_t pid) | |
754 | { | |
755 | int slot; | |
756 | ||
757 | slot = findfreeslot(); | |
758 | childpids[slot] = pid; | |
759 | curprocs++; | |
760 | } | |
761 | ||
762 | static int | |
763 | pids_remove(pid_t pid) | |
764 | { | |
765 | int slot; | |
766 | ||
767 | if ((slot = findslot(pid)) < 0) | |
768 | return (0); | |
769 | ||
770 | clearslot(slot); | |
771 | curprocs--; | |
772 | return (1); | |
773 | } | |
774 | ||
775 | static int | |
776 | findfreeslot(void) | |
777 | { | |
778 | int slot; | |
779 | ||
780 | if ((slot = findslot(NOPID)) < 0) | |
781 | errx(1, "internal error: no free pid slot"); | |
782 | return (slot); | |
783 | } | |
784 | ||
785 | static int | |
786 | findslot(pid_t pid) | |
787 | { | |
788 | int slot; | |
789 | ||
790 | for (slot = 0; slot < maxprocs; slot++) | |
791 | if (childpids[slot] == pid) | |
792 | return (slot); | |
793 | return (-1); | |
794 | } | |
795 | ||
796 | static void | |
797 | clearslot(int slot) | |
798 | { | |
799 | ||
800 | childpids[slot] = NOPID; | |
801 | } | |
802 | ||
803 | /* | |
804 | * Prompt the user about running a command. | |
805 | */ | |
806 | static int | |
807 | prompt(void) | |
808 | { | |
809 | regex_t cre; | |
810 | size_t rsize; | |
811 | int match; | |
812 | char *response; | |
813 | FILE *ttyfp; | |
814 | ||
815 | if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) | |
816 | return (2); /* Indicate that the TTY failed to open. */ | |
817 | (void)fprintf(stderr, "?..."); | |
818 | (void)fflush(stderr); | |
819 | if ((response = fgetln(ttyfp, &rsize)) == NULL || | |
820 | regcomp(&cre, nl_langinfo(YESEXPR), REG_BASIC) != 0) { | |
821 | (void)fclose(ttyfp); | |
822 | return (0); | |
823 | } | |
824 | response[rsize - 1] = '\0'; | |
825 | match = regexec(&cre, response, 0, NULL, 0); | |
826 | (void)fclose(ttyfp); | |
827 | regfree(&cre); | |
828 | return (match == 0); | |
829 | } | |
830 | ||
831 | static void | |
832 | usage(void) | |
833 | { | |
834 | ||
835 | fprintf(stderr, | |
836 | "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]\n" | |
837 | " [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]\n" | |
838 | " [-s size] [utility [argument ...]]\n"); | |
839 | exit(1); | |
840 | } |