1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21 /*
22 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23 * Copyright 2012 DEY Storage Systems, Inc.
24 * Copyright (c) 2018, Joyent, Inc.
25 *
26 * Portions of this file developed by DEY Storage Systems, Inc. are licensed
27 * under the terms of the Common Development and Distribution License (CDDL)
28 * version 1.0 only. The use of subsequent versions of the License are
29 * is specifically prohibited unless those terms are not in conflict with
30 * version 1.0 of the License. You can find this license on-line at
31 * http://www.illumos.org/license/CDDL
32 */
33 /*
34 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
35 * Use is subject to license terms.
36 */
37
38 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
39 /* All Rights Reserved */
40
41
42 #include <stdio.h>
43 #include <sys/types.h>
44 #include <sys/wait.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <stdarg.h>
49 #include <stdlib.h>
50 #include <limits.h>
51 #include <wchar.h>
52 #include <locale.h>
53 #include <langinfo.h>
54 #include <stropts.h>
55 #include <poll.h>
56 #include <errno.h>
57 #include <stdarg.h>
58 #include <sys/fork.h>
59 #include "getresponse.h"
60
61 #define HEAD 0
62 #define TAIL 1
63 #define FALSE 0
64 #define TRUE 1
65 #define MAXSBUF 255
66 #define MAXIBUF 512
67 #define MAXINSERTS 5
68 #define BUFSIZE LINE_MAX
69 #define MAXARGS 255
70 #define INSPAT_STR "{}" /* default replstr string for -[Ii] */
71 #define FORK_RETRY 5
72
73 #define QBUF_STARTLEN 255 /* start size of growable string buffer */
74 #define QBUF_INC 100 /* how much to grow a growable string by */
75
76 /* We use these macros to help make formatting look "consistent" */
77 #define EMSG(s) ermsg(gettext(s "\n"))
78 #define EMSG2(s, a) ermsg(gettext(s "\n"), a)
79 #define PERR(s) perror(gettext("xargs: " s))
80
81 /* Some common error messages */
82
83 #define LIST2LONG "Argument list too long"
84 #define ARG2LONG "A single argument was greater than %d bytes"
85 #define MALLOCFAIL "Memory allocation failure"
86 #define CORRUPTFILE "Corrupt input file"
87 #define WAITFAIL "Wait failure"
88 #define CHILDSIG "Child killed with signal %d"
89 #define CHILDFAIL "Command could not continue processing data"
90 #define FORKFAIL "Could not fork child"
91 #define EXECFAIL "Could not exec command"
92 #define MISSQUOTE "Missing quote"
93 #define BADESCAPE "Incomplete escape"
94 #define IBUFOVERFLOW "Insert buffer overflow"
95 #define NOCHILDSLOT "No free child slot available"
96
97 #define _(x) gettext(x)
98
99 static wctype_t blank;
100 static char *arglist[MAXARGS+1];
101 static char argbuf[BUFSIZE * 2 + 1];
102 static char lastarg[BUFSIZE + 1];
103 static char **ARGV = arglist;
104 static char *LEOF = "_";
105 static char *INSPAT = INSPAT_STR;
106 static char ins_buf[MAXIBUF];
107 static char *p_ibuf;
108
109 static struct inserts {
110 char **p_ARGV; /* where to put newarg ptr in arg list */
111 char *p_skel; /* ptr to arg template */
112 } saveargv[MAXINSERTS];
113
114 static int PROMPT = -1;
115 static int BUFLIM = BUFSIZE;
116 static int MAXPROCS = 1;
117 static int N_ARGS = 0;
118 static int N_args = 0;
119 static int N_lines = 0;
120 static int DASHX = FALSE;
121 static int MORE = TRUE;
122 static int PER_LINE = FALSE;
123 static int LINE_CONT = FALSE;
124 static int EAT_LEAD = FALSE;
125 static int ERR = FALSE;
126 static int OK = TRUE;
127 static int LEGAL = FALSE;
128 static int TRACE = FALSE;
129 static int INSERT = FALSE;
130 static int ZERO = FALSE;
131 static int linesize = 0;
132 static int ibufsize = 0;
133 static int exitstat = 0; /* our exit status */
134 static int mac; /* modified argc, after parsing */
135 static char **mav; /* modified argv, after parsing */
136 static int n_inserts; /* # of insertions. */
137 static pid_t *procs; /* pids of children */
138 static int n_procs; /* # of child processes. */
139
140 /* our usage message: */
141 #define USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\
142 "[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-P maxprocs] "\
143 "[-s size] [cmd [args ...]]\n"
144
145 static int echoargs();
146 static wint_t getwchr(char *, size_t *);
147 static void lcall(char *sub, char **subargs);
148 static void addibuf(struct inserts *p);
149 static void ermsg(char *messages, ...);
150 static char *addarg(char *arg);
151 static void store_str(char **, char *, size_t);
152 static char *getarg(char *);
153 static char *insert(char *pattern, char *subst);
154 static void usage();
155 static void parseargs();
156 static int procs_find(pid_t child);
157 static void procs_store(pid_t child);
158 static boolean_t procs_delete(pid_t child);
159 static pid_t procs_waitpid(boolean_t blocking, int *stat_loc);
160 static void procs_wait(boolean_t blocking);
161
162 int
main(int argc,char ** argv)163 main(int argc, char **argv)
164 {
165 int j;
166 long l;
167 struct inserts *psave;
168 int c;
169 int initsize;
170 char *cmdname, **initlist;
171 char *arg;
172 char *next;
173 char *eptr;
174
175 /* initialization */
176 blank = wctype("blank");
177 n_inserts = 0;
178 psave = saveargv;
179 (void) setlocale(LC_ALL, "");
180 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
181 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
182 #endif
183 (void) textdomain(TEXT_DOMAIN);
184 if (init_yes() < 0) {
185 ermsg(_(ERR_MSG_INIT_YES), strerror(errno));
186 exit(1);
187 }
188
189 parseargs(argc, argv);
190
191 /* handling all of xargs arguments: */
192 while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:P:s:x")) != EOF) {
193 switch (c) {
194 case '0':
195 ZERO = TRUE;
196 break;
197
198 case 't': /* -t: turn trace mode on */
199 TRACE = TRUE;
200 break;
201
202 case 'p': /* -p: turn on prompt mode. */
203 if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) {
204 PERR("can't read from tty for -p");
205 } else {
206 TRACE = TRUE;
207 }
208 break;
209
210 case 'e':
211 /*
212 * -e[eofstr]: set/disable end-of-file.
213 * N.B. that an argument *isn't* required here; but
214 * parseargs forced an argument if not was given. The
215 * forced argument is the default...
216 */
217 LEOF = optarg; /* can be empty */
218 break;
219
220 case 'E':
221 /*
222 * -E eofstr: change end-of-file string.
223 * eofstr *is* required here, but can be empty:
224 */
225 LEOF = optarg;
226 break;
227
228 case 'I':
229 /* -I replstr: Insert mode. replstr *is* required. */
230 INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
231 LINE_CONT = FALSE;
232 N_ARGS = 0;
233 INSPAT = optarg;
234 if (*optarg == '\0') {
235 ermsg(_("Option requires an argument: -%c\n"),
236 c);
237 }
238 break;
239
240 case 'i':
241 /*
242 * -i [replstr]: insert mode, with *optional* replstr.
243 * N.B. that an argument *isn't* required here; if
244 * it's not given, then the string INSPAT_STR will
245 * be assumed.
246 *
247 * Since getopts(3C) doesn't handle the case of an
248 * optional variable argument at all, we have to
249 * parse this by hand:
250 */
251
252 INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
253 LINE_CONT = FALSE;
254 N_ARGS = 0;
255 if ((optarg != NULL) && (*optarg != '\0')) {
256 INSPAT = optarg;
257 } else {
258 /*
259 * here, there is no next argument. so
260 * we reset INSPAT to the INSPAT_STR.
261 * we *have* to do this, as -i/I may have
262 * been given previously, and XCU4 requires
263 * that only "the last one specified takes
264 * effect".
265 */
266 INSPAT = INSPAT_STR;
267 }
268 break;
269
270 case 'L':
271 /*
272 * -L number: # of times cmd is executed
273 * number *is* required here:
274 */
275 PER_LINE = LINE_CONT = TRUE;
276 N_ARGS = 0;
277 INSERT = EAT_LEAD = FALSE;
278 if ((PER_LINE = atoi(optarg)) <= 0) {
279 ermsg(_("#lines must be positive int: %s\n"),
280 optarg);
281 }
282 break;
283
284 case 'l':
285 /*
286 * -l [number]: # of times cmd is executed
287 * N.B. that an argument *isn't* required here; if
288 * it's not given, then 1 is assumed.
289 *
290 * parseargs handles the optional arg processing.
291 */
292
293 PER_LINE = LINE_CONT = LEGAL = TRUE;
294 N_ARGS = 0;
295 INSERT = EAT_LEAD = FALSE;
296
297 if ((optarg != NULL) && (*optarg != '\0')) {
298 if ((PER_LINE = atoi(optarg)) <= 0)
299 PER_LINE = 1;
300 }
301 break;
302
303 case 'n': /* -n number: # stdin args */
304 /*
305 * -n number: # stdin args.
306 * number *is* required here:
307 */
308 if ((N_ARGS = atoi(optarg)) <= 0) {
309 ermsg(_("#args must be positive int: %s\n"),
310 optarg);
311 } else {
312 LEGAL = DASHX || N_ARGS == 1;
313 INSERT = PER_LINE = LINE_CONT = FALSE;
314 }
315 break;
316
317 case 'P': /* -P maxprocs: # of child processses */
318 errno = 0;
319 l = strtol(optarg, &eptr, 10);
320 if (*eptr != '\0' || errno != 0) {
321 ermsg(_("failed to parse maxprocs (-P): %s\n"),
322 optarg);
323 break;
324 }
325
326 if (l < 0) {
327 ermsg(_("maximum number of processes (-P) "
328 "cannot be negative\n"));
329 break;
330 }
331
332 /*
333 * Come up with an upper bound that'll probably fit in
334 * memory.
335 */
336 if (l == 0 || l > ((INT_MAX / sizeof (pid_t) >> 1))) {
337 l = INT_MAX / sizeof (pid_t) >> 1;
338 }
339 MAXPROCS = (int)l;
340 break;
341
342 case 's': /* -s size: set max size of each arg list */
343 BUFLIM = atoi(optarg);
344 if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
345 ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
346 BUFSIZE, optarg);
347 }
348 break;
349
350 case 'x': /* -x: terminate if args > size limit */
351 DASHX = LEGAL = TRUE;
352 break;
353
354 default:
355 /*
356 * bad argument. complain and get ready to die.
357 */
358 usage();
359 exit(2);
360 break;
361 }
362 }
363
364 /*
365 * if anything called ermsg(), something screwed up, so
366 * we exit early.
367 */
368 if (OK == FALSE) {
369 usage();
370 exit(2);
371 }
372
373 /*
374 * we're finished handling xargs's options, so now pick up
375 * the command name (if any), and it's options.
376 */
377
378
379 mac -= optind; /* dec arg count by what we've processed */
380 mav += optind; /* inc to current mav */
381
382 procs = calloc(MAXPROCS, sizeof (pid_t));
383 if (procs == NULL) {
384 PERR(MALLOCFAIL);
385 exit(1);
386 }
387
388 if (mac <= 0) { /* if there're no more args to process, */
389 cmdname = "/usr/bin/echo"; /* our default command */
390 *ARGV++ = addarg(cmdname); /* use the default cmd. */
391 } else { /* otherwise keep parsing rest of the string. */
392 /*
393 * note that we can't use getopts(3C), and *must* parse
394 * this by hand, as we don't know apriori what options the
395 * command will take.
396 */
397 cmdname = *mav; /* get the command name */
398
399
400 /* pick up the remaining args from the command line: */
401 while ((OK == TRUE) && (mac-- > 0)) {
402 /*
403 * while we haven't crapped out, and there's
404 * work to do:
405 */
406 if (INSERT && ! ERR) {
407 if (strstr(*mav, INSPAT) != NULL) {
408 if (++n_inserts > MAXINSERTS) {
409 ermsg(_("too many args "
410 "with %s\n"), INSPAT);
411 ERR = TRUE;
412 }
413 psave->p_ARGV = ARGV;
414 (psave++)->p_skel = *mav;
415 }
416 }
417 *ARGV++ = addarg(*mav++);
418 }
419 }
420
421 /* pick up args from standard input */
422
423 initlist = ARGV;
424 initsize = linesize;
425 lastarg[0] = '\0';
426
427 while (OK) {
428 N_args = 0;
429 N_lines = 0;
430 ARGV = initlist;
431 linesize = initsize;
432 next = argbuf;
433
434 while (MORE || (lastarg[0] != '\0')) {
435 int l;
436
437 if (*lastarg != '\0') {
438 arg = strcpy(next, lastarg);
439 *lastarg = '\0';
440 } else if ((arg = getarg(next)) == NULL) {
441 break;
442 }
443
444 l = strlen(arg) + 1;
445 linesize += l;
446 next += l;
447
448 /* Inserts are handled specially later. */
449 if ((n_inserts == 0) && (linesize >= BUFLIM)) {
450 /*
451 * Legal indicates hard fail if the list is
452 * truncated due to size. So fail, or if we
453 * cannot create any list because it would be
454 * too big.
455 */
456 if (LEGAL || N_args == 0) {
457 EMSG(LIST2LONG);
458 procs_wait(B_TRUE);
459 exit(2);
460 /* NOTREACHED */
461 }
462
463 /*
464 * Otherwise just save argument for later.
465 */
466 (void) strcpy(lastarg, arg);
467 break;
468 }
469
470 *ARGV++ = arg;
471
472 N_args++;
473
474 if ((PER_LINE && (N_lines >= PER_LINE)) ||
475 (N_ARGS && (N_args >= N_ARGS))) {
476 break;
477 }
478
479
480 if ((ARGV - arglist) == MAXARGS) {
481 break;
482 }
483 }
484
485 *ARGV = NULL;
486 if (N_args == 0) {
487 /* Reached the end with no more work. */
488 break;
489 }
490
491 /* insert arg if requested */
492
493 if (!ERR && INSERT) {
494
495 p_ibuf = ins_buf;
496 ARGV--;
497 j = ibufsize = 0;
498 for (psave = saveargv; ++j <= n_inserts; ++psave) {
499 addibuf(psave);
500 if (ERR)
501 break;
502 }
503 }
504 *ARGV = NULL;
505
506 if (n_inserts > 0) {
507 /*
508 * if we've done any insertions, re-calculate the
509 * linesize. bomb out if we've exceeded our length.
510 */
511 linesize = 0;
512 for (ARGV = arglist; *ARGV != NULL; ARGV++) {
513 linesize += strlen(*ARGV) + 1;
514 }
515 if (linesize >= BUFLIM) {
516 EMSG(LIST2LONG);
517 procs_wait(B_TRUE);
518 exit(2);
519 /* NOTREACHED */
520 }
521 }
522
523 /* exec command */
524
525 if (!ERR) {
526 if (!MORE &&
527 (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
528 exit(exitstat);
529 OK = TRUE;
530 j = TRACE ? echoargs() : TRUE;
531 if (j) {
532 /*
533 * for xcu4, all invocations of cmdname must
534 * return 0, in order for us to return 0.
535 * so if we have a non-zero status here,
536 * quit immediately.
537 */
538 (void) lcall(cmdname, arglist);
539 }
540 }
541 }
542
543 procs_wait(B_TRUE);
544
545 if (OK)
546 return (exitstat);
547
548 /*
549 * if exitstat was set, to match XCU4 complience,
550 * return that value, otherwise, return 1.
551 */
552 return (exitstat ? exitstat : 1);
553 }
554
555 static char *
addarg(char * arg)556 addarg(char *arg)
557 {
558 linesize += (strlen(arg) + 1);
559 return (arg);
560 }
561
562
563 static void
store_str(char ** buffer,char * str,size_t len)564 store_str(char **buffer, char *str, size_t len)
565 {
566 (void) memcpy(*buffer, str, len);
567 (*buffer)[len] = '\0';
568 *buffer += len;
569 }
570
571
572 static char *
getarg(char * arg)573 getarg(char *arg)
574 {
575 char *xarg = arg;
576 wchar_t c = 0;
577 char mbc[MB_LEN_MAX];
578 size_t len;
579 int escape = 0;
580 int inquote = 0;
581 int last = 0;
582
583 arg[0] = '\0';
584
585 while (MORE) {
586
587 len = 0;
588 last = c;
589 c = getwchr(mbc, &len);
590
591 if (((arg - xarg) + len) > BUFLIM) {
592 EMSG2(ARG2LONG, BUFLIM);
593 exit(2);
594 ERR = TRUE;
595 return (NULL);
596 }
597
598 switch (c) {
599 case '\n':
600 if (ZERO) {
601 store_str(&arg, mbc, len);
602 continue;
603 }
604 /*
605 * NB: Some other versions rip off all of the trailing
606 * blanks. The spec only claims that this should
607 * be done for a single blank. We follow the spec.
608 */
609 if (LINE_CONT && iswctype(last, blank)) {
610 len = 0;
611 *arg = 0;
612 continue;
613 }
614 /* FALLTHRU */
615
616 case '\0':
617 case WEOF: /* Note WEOF == EOF */
618
619 if (escape) {
620 EMSG(BADESCAPE);
621 ERR = TRUE;
622 return (NULL);
623 }
624 if (inquote) {
625 EMSG(MISSQUOTE);
626 ERR = TRUE;
627 return (NULL);
628 }
629
630 N_lines++;
631 break;
632
633 case '"':
634 if (ZERO || escape || (inquote == 1)) {
635 /* treat it literally */
636 escape = 0;
637 store_str(&arg, mbc, len);
638
639 } else if (inquote == 2) {
640 /* terminating double quote */
641 inquote = 0;
642
643 } else {
644 /* starting quoted string */
645 inquote = 2;
646 }
647 continue;
648
649 case '\'':
650 if (ZERO || escape || (inquote == 2)) {
651 /* treat it literally */
652 escape = 0;
653 store_str(&arg, mbc, len);
654
655 } else if (inquote == 1) {
656 /* terminating single quote */
657 inquote = 0;
658
659 } else {
660 /* starting quoted string */
661 inquote = 1;
662 }
663 continue;
664
665 case '\\':
666 /*
667 * Any unquoted character can be escaped by
668 * preceding it with a backslash.
669 */
670 if (ZERO || inquote || escape) {
671 escape = 0;
672 store_str(&arg, mbc, len);
673 } else {
674 escape = 1;
675 }
676 continue;
677
678 default:
679 /* most times we will just want to store it */
680 if (inquote || escape || ZERO || !iswctype(c, blank)) {
681 escape = 0;
682 store_str(&arg, mbc, len);
683 continue;
684 }
685 if (EAT_LEAD && last == 0) {
686 c = 0; /* Roll it back */
687 continue;
688 }
689 if (PER_LINE) {
690 store_str(&arg, mbc, len);
691 continue;
692 }
693
694 /* unquoted blank without special handling */
695 break;
696 }
697
698 /*
699 * At this point we are processing a complete argument.
700 */
701 if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') {
702 MORE = FALSE;
703 return (NULL);
704 }
705 if (c == WEOF) {
706 MORE = FALSE;
707 }
708 if (xarg[0] == '\0')
709 continue;
710 break;
711 }
712
713 return (xarg[0] == '\0' ? NULL : xarg);
714 }
715
716 /*
717 * ermsg(): print out an error message, and indicate failure globally.
718 *
719 * Assumes that message has already been gettext()'d. It would be
720 * nice if we could just do the gettext() here, but we can't, since
721 * since xgettext(1M) wouldn't be able to pick up our error message.
722 */
723 /* PRINTFLIKE1 */
724 static void
ermsg(char * messages,...)725 ermsg(char *messages, ...)
726 {
727 va_list ap;
728
729 va_start(ap, messages);
730
731 (void) fprintf(stderr, "xargs: ");
732 (void) vfprintf(stderr, messages, ap);
733
734 va_end(ap);
735 OK = FALSE;
736 }
737
738 static int
echoargs()739 echoargs()
740 {
741 char **anarg;
742 char **tanarg; /* tmp ptr */
743 int i;
744 char reply[LINE_MAX];
745
746 tanarg = anarg = arglist-1;
747
748 /*
749 * write out each argument, separated by a space. the tanarg
750 * nonsense is for xcu4 testsuite compliance - so that an
751 * extra space isn't echoed after the last argument.
752 */
753 while (*++anarg) { /* while there's an argument */
754 ++tanarg; /* follow anarg */
755 (void) write(2, *anarg, strlen(*anarg));
756
757 if (*++tanarg) { /* if there's another argument: */
758 (void) write(2, " ", 1); /* add a space */
759 --tanarg; /* reset back to anarg */
760 }
761 }
762 if (PROMPT == -1) {
763 (void) write(2, "\n", 1);
764 return (TRUE);
765 }
766
767 (void) write(2, "?...", 4); /* ask the user for input */
768
769 for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
770 if (reply[i] == '\n') {
771 if (i == 0)
772 return (FALSE);
773 break;
774 }
775 }
776 reply[i] = 0;
777
778 /* flush remainder of line if necessary */
779 if (i == LINE_MAX) {
780 char bitbucket;
781
782 while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
783 ;
784 }
785
786 return (yes_check(reply));
787 }
788
789
790 static char *
insert(char * pattern,char * subst)791 insert(char *pattern, char *subst)
792 {
793 static char buffer[MAXSBUF+1];
794 int len, ipatlen;
795 char *pat;
796 char *bufend;
797 char *pbuf;
798
799 len = strlen(subst);
800 ipatlen = strlen(INSPAT) - 1;
801 pat = pattern - 1;
802 pbuf = buffer;
803 bufend = &buffer[MAXSBUF];
804
805 while (*++pat) {
806 if (strncmp(pat, INSPAT, ipatlen + 1) == 0) {
807 if (pbuf + len >= bufend) {
808 break;
809 } else {
810 (void) strcpy(pbuf, subst);
811 pat += ipatlen;
812 pbuf += len;
813 }
814 } else {
815 *pbuf++ = *pat;
816 if (pbuf >= bufend)
817 break;
818 }
819 }
820
821 if (!*pat) {
822 *pbuf = '\0';
823 return (buffer);
824 } else {
825 ermsg(gettext("Maximum argument size with insertion via %s's "
826 "exceeded\n"), INSPAT);
827 ERR = TRUE;
828 return (NULL);
829 }
830 }
831
832
833 static void
addibuf(struct inserts * p)834 addibuf(struct inserts *p)
835 {
836 char *newarg, *skel, *sub;
837 int l;
838
839 skel = p->p_skel;
840 sub = *ARGV;
841 newarg = insert(skel, sub);
842 if (ERR)
843 return;
844
845 l = strlen(newarg) + 1;
846 if ((ibufsize += l) > MAXIBUF) {
847 EMSG(IBUFOVERFLOW);
848 ERR = TRUE;
849 }
850 (void) strcpy(p_ibuf, newarg);
851 *(p->p_ARGV) = p_ibuf;
852 p_ibuf += l;
853 }
854
855
856 /*
857 * getwchr(): get the next wide character.
858 * description:
859 * we get the next character from stdin. This returns WEOF if no
860 * character is present. If ZERO is set, it gets a single byte instead
861 * a wide character.
862 */
863 static wint_t
getwchr(char * mbc,size_t * sz)864 getwchr(char *mbc, size_t *sz)
865 {
866 size_t i;
867 int c;
868 wchar_t wch;
869
870 i = 0;
871 while (i < MB_CUR_MAX) {
872
873 if ((c = fgetc(stdin)) == EOF) {
874
875 if (i == 0) {
876 /* TRUE EOF has been reached */
877 return (WEOF);
878 }
879
880 /*
881 * We have some characters in our buffer still so it
882 * must be an invalid character right before EOF.
883 */
884 break;
885 }
886 mbc[i++] = (char)c;
887
888 /* If this succeeds then we are done */
889 if (ZERO) {
890 *sz = i;
891 return ((char)c);
892 }
893 if (mbtowc(&wch, mbc, i) != -1) {
894 *sz = i;
895 return ((wint_t)wch);
896 }
897 }
898
899 /*
900 * We have now encountered an illegal character sequence.
901 * There is nothing much we can do at this point but
902 * return an error. If we attempt to recover we may in fact
903 * return garbage as arguments, from the customer's point
904 * of view. After all what if they are feeding us a file
905 * generated in another locale?
906 */
907 errno = EILSEQ;
908 PERR(CORRUPTFILE);
909 exit(1);
910 /* NOTREACHED */
911 }
912
913
914 static void
lcall(char * sub,char ** subargs)915 lcall(char *sub, char **subargs)
916 {
917 int retry = 0;
918 pid_t child;
919
920 for (;;) {
921 switch (child = forkx(FORK_NOSIGCHLD)) {
922 default:
923 procs_store(child);
924 /*
925 * Note, if we have used up all of our slots, then this
926 * call may end up blocking.
927 */
928 procs_wait(B_FALSE);
929 return;
930 case 0:
931 (void) execvp(sub, subargs);
932 PERR(EXECFAIL);
933 if (errno == EACCES)
934 exit(126);
935 exit(127);
936 /* NOTREACHED */
937 case -1:
938 if (errno != EAGAIN && retry++ < FORK_RETRY) {
939 PERR(FORKFAIL);
940 exit(123);
941 }
942 (void) sleep(1);
943 }
944 }
945 }
946
947 /*
948 * Return the index of child in the procs array.
949 */
950 static int
procs_find(pid_t child)951 procs_find(pid_t child)
952 {
953 int i;
954
955 for (i = 0; i < MAXPROCS; i++) {
956 if (procs[i] == child) {
957 return (i);
958 }
959 }
960
961 return (-1);
962 }
963
964 static void
procs_store(pid_t child)965 procs_store(pid_t child)
966 {
967 int i;
968
969 i = procs_find(0);
970 if (i < 0) {
971 EMSG(NOCHILDSLOT);
972 exit(1);
973 }
974 procs[i] = child;
975 n_procs++;
976 }
977
978 static boolean_t
procs_delete(pid_t child)979 procs_delete(pid_t child)
980 {
981 int i;
982
983 i = procs_find(child);
984 if (i < 0) {
985 return (B_FALSE);
986 }
987
988 procs[i] = (pid_t)0;
989 n_procs--;
990
991 return (B_TRUE);
992 }
993
994 static pid_t
procs_waitpid(boolean_t blocking,int * stat_loc)995 procs_waitpid(boolean_t blocking, int *stat_loc)
996 {
997 pid_t child;
998 int options;
999
1000 if (n_procs == 0) {
1001 errno = ECHILD;
1002 return (-1);
1003 }
1004
1005 options = 0;
1006 if (!blocking) {
1007 options |= WNOHANG;
1008 }
1009
1010 while ((child = waitpid((pid_t)-1, stat_loc, options)) > 0) {
1011 if (procs_delete(child)) {
1012 break;
1013 }
1014 }
1015
1016 return (child);
1017 }
1018
1019 static void
procs_wait(boolean_t blocking)1020 procs_wait(boolean_t blocking)
1021 {
1022 pid_t child;
1023 int stat_loc;
1024
1025 /*
1026 * If we currently have filled all of our slots, then we need to block
1027 * further execution.
1028 */
1029 if (n_procs >= MAXPROCS)
1030 blocking = B_TRUE;
1031 while ((child = procs_waitpid(blocking, &stat_loc)) > 0) {
1032 if (WIFSIGNALED(stat_loc)) {
1033 EMSG2(CHILDSIG, WTERMSIG(stat_loc));
1034 exit(125);
1035 /* NOTREACHED */
1036 } else if ((WEXITSTATUS(stat_loc) & 0377) == 0377) {
1037 EMSG(CHILDFAIL);
1038 exit(124);
1039 /* NOTREACHED */
1040 } else {
1041 exitstat |= WEXITSTATUS(stat_loc);
1042 }
1043 }
1044
1045 if (child == (pid_t)(-1) && errno != ECHILD) {
1046 EMSG(WAITFAIL);
1047 exit(122);
1048 /* NOTREACHED */
1049 }
1050 }
1051
1052 static void
usage()1053 usage()
1054 {
1055 ermsg(_(USAGEMSG));
1056 OK = FALSE;
1057 }
1058
1059
1060
1061 /*
1062 * parseargs(): modify the args
1063 * since the -e, -i and -l flags all take optional subarguments,
1064 * and getopts(3C) is clueless about this nonsense, we change the
1065 * our local argument count and strings to separate this out,
1066 * and make it easier to handle via getopts(3c).
1067 *
1068 * -e -> "-e ""
1069 * -e3 -> "-e "3"
1070 * -Estr -> "-E "str"
1071 * -i -> "-i "{}"
1072 * -irep -> "-i "rep"
1073 * -l -> "-l "1"
1074 * -l10 -> "-l "10"
1075 *
1076 * since the -e, -i and -l flags all take optional subarguments,
1077 */
1078 static void
parseargs(int ac,char ** av)1079 parseargs(int ac, char **av)
1080 {
1081 int i; /* current argument */
1082 int cflag; /* 0 = not processing cmd arg */
1083
1084 if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
1085 PERR(MALLOCFAIL);
1086 exit(1);
1087 }
1088
1089 /* for each argument, see if we need to change things: */
1090 for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
1091 if ((mav[mac] = strdup(av[i])) == NULL) {
1092 PERR(MALLOCFAIL);
1093 exit(1);
1094 }
1095
1096 /* -- has been found or argument list is fully processes */
1097 if (cflag)
1098 continue;
1099
1100 /*
1101 * if we're doing special processing, and we've got a flag
1102 */
1103 else if ((av[i][0] == '-') && (av[i][1] != '\0')) {
1104 char *def;
1105
1106 switch (av[i][1]) {
1107 case 'e':
1108 def = ""; /* -e with no arg turns off eof */
1109 goto process_special;
1110 case 'i':
1111 def = INSPAT_STR;
1112 goto process_special;
1113 case 'l':
1114 def = "1";
1115 process_special:
1116 /*
1117 * if there's no sub-option, we *must* add
1118 * a default one. this is because xargs must
1119 * be able to distinguish between a valid
1120 * suboption, and a command name.
1121 */
1122 if (av[i][2] == '\0') {
1123 mav[++mac] = strdup(def);
1124 } else {
1125 /* clear out our version: */
1126 mav[mac][2] = '\0';
1127 mav[++mac] = strdup(&av[i][2]);
1128 }
1129 if (mav[mac] == NULL) {
1130 PERR(MALLOCFAIL);
1131 exit(1);
1132 }
1133 break;
1134
1135 /* flags with required subarguments: */
1136
1137 /*
1138 * there are two separate cases here. either the
1139 * flag can have the normal XCU4 handling
1140 * (of the form: -X subargument); or it can have
1141 * the old solaris 2.[0-4] handling (of the
1142 * form: -Xsubargument). in order to maintain
1143 * backwards compatibility, we must support the
1144 * latter case. we handle the latter possibility
1145 * first so both the old solaris way of handling
1146 * and the new XCU4 way of handling things are allowed.
1147 */
1148 case 'n': /* FALLTHROUGH */
1149 case 'P': /* FALLTHROUGH */
1150 case 's': /* FALLTHROUGH */
1151 case 'E': /* FALLTHROUGH */
1152 case 'I': /* FALLTHROUGH */
1153 case 'L':
1154 /*
1155 * if the second character isn't null, then
1156 * the user has specified the old syntax.
1157 * we move the subargument into our
1158 * mod'd argument list.
1159 */
1160 if (av[i][2] != '\0') {
1161 /* first clean things up: */
1162 mav[mac][2] = '\0';
1163
1164 /* now add the separation: */
1165 ++mac; /* inc to next mod'd arg */
1166 if ((mav[mac] = strdup(&av[i][2])) ==
1167 NULL) {
1168 PERR(MALLOCFAIL);
1169 exit(1);
1170 }
1171 break;
1172 }
1173 i++;
1174 mac++;
1175
1176 if (av[i] == NULL) {
1177 mav[mac] = NULL;
1178 return;
1179 }
1180 if ((mav[mac] = strdup(av[i])) == NULL) {
1181 PERR(MALLOCFAIL);
1182 exit(1);
1183 }
1184 break;
1185
1186 /* flags */
1187 case 'p' :
1188 case 't' :
1189 case 'x' :
1190 case '0' :
1191 break;
1192
1193 case '-' :
1194 default:
1195 /*
1196 * here we've hit the cmd argument. so
1197 * we'll stop special processing, as the
1198 * cmd may have a "-i" etc., argument,
1199 * and we don't want to add a "" to it.
1200 */
1201 cflag = 1;
1202 break;
1203 }
1204 } else if (i > 0) { /* if we're not the 1st arg */
1205 /*
1206 * if it's not a flag, then it *must* be the cmd.
1207 * set cflag, so we don't mishandle the -[eil] flags.
1208 */
1209 cflag = 1;
1210 }
1211 }
1212
1213 mav[mac] = NULL;
1214 }
1215