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) 2017, 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 163 main(int argc, char **argv) 164 { 165 int j; 166 unsigned 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 = strtoul(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 /* 327 * Come up with an upper bound that'll probably fit in 328 * memory. 329 */ 330 if (l == 0 || l > ((INT_MAX / sizeof (pid_t) >> 1))) { 331 l = INT_MAX / sizeof (pid_t) >> 1; 332 } 333 MAXPROCS = (int)l; 334 break; 335 336 case 's': /* -s size: set max size of each arg list */ 337 BUFLIM = atoi(optarg); 338 if (BUFLIM > BUFSIZE || BUFLIM <= 0) { 339 ermsg(_("0 < max-cmd-line-size <= %d: %s\n"), 340 BUFSIZE, optarg); 341 } 342 break; 343 344 case 'x': /* -x: terminate if args > size limit */ 345 DASHX = LEGAL = TRUE; 346 break; 347 348 default: 349 /* 350 * bad argument. complain and get ready to die. 351 */ 352 usage(); 353 exit(2); 354 break; 355 } 356 } 357 358 /* 359 * if anything called ermsg(), something screwed up, so 360 * we exit early. 361 */ 362 if (OK == FALSE) { 363 usage(); 364 exit(2); 365 } 366 367 /* 368 * we're finished handling xargs's options, so now pick up 369 * the command name (if any), and it's options. 370 */ 371 372 373 mac -= optind; /* dec arg count by what we've processed */ 374 mav += optind; /* inc to current mav */ 375 376 procs = calloc(MAXPROCS, sizeof (pid_t)); 377 if (procs == NULL) { 378 PERR(MALLOCFAIL); 379 exit(1); 380 } 381 382 if (mac <= 0) { /* if there're no more args to process, */ 383 cmdname = "/usr/bin/echo"; /* our default command */ 384 *ARGV++ = addarg(cmdname); /* use the default cmd. */ 385 } else { /* otherwise keep parsing rest of the string. */ 386 /* 387 * note that we can't use getopts(3C), and *must* parse 388 * this by hand, as we don't know apriori what options the 389 * command will take. 390 */ 391 cmdname = *mav; /* get the command name */ 392 393 394 /* pick up the remaining args from the command line: */ 395 while ((OK == TRUE) && (mac-- > 0)) { 396 /* 397 * while we haven't crapped out, and there's 398 * work to do: 399 */ 400 if (INSERT && ! ERR) { 401 if (strstr(*mav, INSPAT) != NULL) { 402 if (++n_inserts > MAXINSERTS) { 403 ermsg(_("too many args " 404 "with %s\n"), INSPAT); 405 ERR = TRUE; 406 } 407 psave->p_ARGV = ARGV; 408 (psave++)->p_skel = *mav; 409 } 410 } 411 *ARGV++ = addarg(*mav++); 412 } 413 } 414 415 /* pick up args from standard input */ 416 417 initlist = ARGV; 418 initsize = linesize; 419 lastarg[0] = '\0'; 420 421 while (OK) { 422 N_args = 0; 423 N_lines = 0; 424 ARGV = initlist; 425 linesize = initsize; 426 next = argbuf; 427 428 while (MORE || (lastarg[0] != '\0')) { 429 int l; 430 431 if (*lastarg != '\0') { 432 arg = strcpy(next, lastarg); 433 *lastarg = '\0'; 434 } else if ((arg = getarg(next)) == NULL) { 435 break; 436 } 437 438 l = strlen(arg) + 1; 439 linesize += l; 440 next += l; 441 442 /* Inserts are handled specially later. */ 443 if ((n_inserts == 0) && (linesize >= BUFLIM)) { 444 /* 445 * Legal indicates hard fail if the list is 446 * truncated due to size. So fail, or if we 447 * cannot create any list because it would be 448 * too big. 449 */ 450 if (LEGAL || N_args == 0) { 451 EMSG(LIST2LONG); 452 procs_wait(B_TRUE); 453 exit(2); 454 /* NOTREACHED */ 455 } 456 457 /* 458 * Otherwise just save argument for later. 459 */ 460 (void) strcpy(lastarg, arg); 461 break; 462 } 463 464 *ARGV++ = arg; 465 466 N_args++; 467 468 if ((PER_LINE && (N_lines >= PER_LINE)) || 469 (N_ARGS && (N_args >= N_ARGS))) { 470 break; 471 } 472 473 474 if ((ARGV - arglist) == MAXARGS) { 475 break; 476 } 477 } 478 479 *ARGV = NULL; 480 if (N_args == 0) { 481 /* Reached the end with no more work. */ 482 break; 483 } 484 485 /* insert arg if requested */ 486 487 if (!ERR && INSERT) { 488 489 p_ibuf = ins_buf; 490 ARGV--; 491 j = ibufsize = 0; 492 for (psave = saveargv; ++j <= n_inserts; ++psave) { 493 addibuf(psave); 494 if (ERR) 495 break; 496 } 497 } 498 *ARGV = NULL; 499 500 if (n_inserts > 0) { 501 /* 502 * if we've done any insertions, re-calculate the 503 * linesize. bomb out if we've exceeded our length. 504 */ 505 linesize = 0; 506 for (ARGV = arglist; *ARGV != NULL; ARGV++) { 507 linesize += strlen(*ARGV) + 1; 508 } 509 if (linesize >= BUFLIM) { 510 EMSG(LIST2LONG); 511 procs_wait(B_TRUE); 512 exit(2); 513 /* NOTREACHED */ 514 } 515 } 516 517 /* exec command */ 518 519 if (!ERR) { 520 if (!MORE && 521 (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0)) 522 exit(exitstat); 523 OK = TRUE; 524 j = TRACE ? echoargs() : TRUE; 525 if (j) { 526 /* 527 * for xcu4, all invocations of cmdname must 528 * return 0, in order for us to return 0. 529 * so if we have a non-zero status here, 530 * quit immediately. 531 */ 532 (void) lcall(cmdname, arglist); 533 } 534 } 535 } 536 537 procs_wait(B_TRUE); 538 539 if (OK) 540 return (exitstat); 541 542 /* 543 * if exitstat was set, to match XCU4 complience, 544 * return that value, otherwise, return 1. 545 */ 546 return (exitstat ? exitstat : 1); 547 } 548 549 static char * 550 addarg(char *arg) 551 { 552 linesize += (strlen(arg) + 1); 553 return (arg); 554 } 555 556 557 static void 558 store_str(char **buffer, char *str, size_t len) 559 { 560 (void) memcpy(*buffer, str, len); 561 (*buffer)[len] = '\0'; 562 *buffer += len; 563 } 564 565 566 static char * 567 getarg(char *arg) 568 { 569 char *xarg = arg; 570 wchar_t c = 0; 571 char mbc[MB_LEN_MAX]; 572 size_t len; 573 int escape = 0; 574 int inquote = 0; 575 int last = 0; 576 577 arg[0] = '\0'; 578 579 while (MORE) { 580 581 len = 0; 582 last = c; 583 c = getwchr(mbc, &len); 584 585 if (((arg - xarg) + len) > BUFLIM) { 586 EMSG2(ARG2LONG, BUFLIM); 587 exit(2); 588 ERR = TRUE; 589 return (NULL); 590 } 591 592 switch (c) { 593 case '\n': 594 if (ZERO) { 595 store_str(&arg, mbc, len); 596 continue; 597 } 598 /* 599 * NB: Some other versions rip off all of the trailing 600 * blanks. The spec only claims that this should 601 * be done for a single blank. We follow the spec. 602 */ 603 if (LINE_CONT && iswctype(last, blank)) { 604 len = 0; 605 *arg = 0; 606 continue; 607 } 608 /* FALLTHRU */ 609 610 case '\0': 611 case WEOF: /* Note WEOF == EOF */ 612 613 if (escape) { 614 EMSG(BADESCAPE); 615 ERR = TRUE; 616 return (NULL); 617 } 618 if (inquote) { 619 EMSG(MISSQUOTE); 620 ERR = TRUE; 621 return (NULL); 622 } 623 624 N_lines++; 625 break; 626 627 case '"': 628 if (ZERO || escape || (inquote == 1)) { 629 /* treat it literally */ 630 escape = 0; 631 store_str(&arg, mbc, len); 632 633 } else if (inquote == 2) { 634 /* terminating double quote */ 635 inquote = 0; 636 637 } else { 638 /* starting quoted string */ 639 inquote = 2; 640 } 641 continue; 642 643 case '\'': 644 if (ZERO || escape || (inquote == 2)) { 645 /* treat it literally */ 646 escape = 0; 647 store_str(&arg, mbc, len); 648 649 } else if (inquote == 1) { 650 /* terminating single quote */ 651 inquote = 0; 652 653 } else { 654 /* starting quoted string */ 655 inquote = 1; 656 } 657 continue; 658 659 case '\\': 660 /* 661 * Any unquoted character can be escaped by 662 * preceding it with a backslash. 663 */ 664 if (ZERO || inquote || escape) { 665 escape = 0; 666 store_str(&arg, mbc, len); 667 } else { 668 escape = 1; 669 } 670 continue; 671 672 default: 673 /* most times we will just want to store it */ 674 if (inquote || escape || ZERO || !iswctype(c, blank)) { 675 escape = 0; 676 store_str(&arg, mbc, len); 677 continue; 678 } 679 if (EAT_LEAD && last == 0) { 680 c = 0; /* Roll it back */ 681 continue; 682 } 683 if (PER_LINE) { 684 store_str(&arg, mbc, len); 685 continue; 686 } 687 688 /* unquoted blank without special handling */ 689 break; 690 } 691 692 /* 693 * At this point we are processing a complete argument. 694 */ 695 if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') { 696 MORE = FALSE; 697 return (NULL); 698 } 699 if (c == WEOF) { 700 MORE = FALSE; 701 } 702 if (xarg[0] == '\0') 703 continue; 704 break; 705 } 706 707 return (xarg[0] == '\0' ? NULL : xarg); 708 } 709 710 /* 711 * ermsg(): print out an error message, and indicate failure globally. 712 * 713 * Assumes that message has already been gettext()'d. It would be 714 * nice if we could just do the gettext() here, but we can't, since 715 * since xgettext(1M) wouldn't be able to pick up our error message. 716 */ 717 /* PRINTFLIKE1 */ 718 static void 719 ermsg(char *messages, ...) 720 { 721 va_list ap; 722 723 va_start(ap, messages); 724 725 (void) fprintf(stderr, "xargs: "); 726 (void) vfprintf(stderr, messages, ap); 727 728 va_end(ap); 729 OK = FALSE; 730 } 731 732 static int 733 echoargs() 734 { 735 char **anarg; 736 char **tanarg; /* tmp ptr */ 737 int i; 738 char reply[LINE_MAX]; 739 740 tanarg = anarg = arglist-1; 741 742 /* 743 * write out each argument, separated by a space. the tanarg 744 * nonsense is for xcu4 testsuite compliance - so that an 745 * extra space isn't echoed after the last argument. 746 */ 747 while (*++anarg) { /* while there's an argument */ 748 ++tanarg; /* follow anarg */ 749 (void) write(2, *anarg, strlen(*anarg)); 750 751 if (*++tanarg) { /* if there's another argument: */ 752 (void) write(2, " ", 1); /* add a space */ 753 --tanarg; /* reset back to anarg */ 754 } 755 } 756 if (PROMPT == -1) { 757 (void) write(2, "\n", 1); 758 return (TRUE); 759 } 760 761 (void) write(2, "?...", 4); /* ask the user for input */ 762 763 for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) { 764 if (reply[i] == '\n') { 765 if (i == 0) 766 return (FALSE); 767 break; 768 } 769 } 770 reply[i] = 0; 771 772 /* flush remainder of line if necessary */ 773 if (i == LINE_MAX) { 774 char bitbucket; 775 776 while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n')) 777 ; 778 } 779 780 return (yes_check(reply)); 781 } 782 783 784 static char * 785 insert(char *pattern, char *subst) 786 { 787 static char buffer[MAXSBUF+1]; 788 int len, ipatlen; 789 char *pat; 790 char *bufend; 791 char *pbuf; 792 793 len = strlen(subst); 794 ipatlen = strlen(INSPAT) - 1; 795 pat = pattern - 1; 796 pbuf = buffer; 797 bufend = &buffer[MAXSBUF]; 798 799 while (*++pat) { 800 if (strncmp(pat, INSPAT, ipatlen + 1) == 0) { 801 if (pbuf + len >= bufend) { 802 break; 803 } else { 804 (void) strcpy(pbuf, subst); 805 pat += ipatlen; 806 pbuf += len; 807 } 808 } else { 809 *pbuf++ = *pat; 810 if (pbuf >= bufend) 811 break; 812 } 813 } 814 815 if (!*pat) { 816 *pbuf = '\0'; 817 return (buffer); 818 } else { 819 ermsg(gettext("Maximum argument size with insertion via %s's " 820 "exceeded\n"), INSPAT); 821 ERR = TRUE; 822 return (NULL); 823 } 824 } 825 826 827 static void 828 addibuf(struct inserts *p) 829 { 830 char *newarg, *skel, *sub; 831 int l; 832 833 skel = p->p_skel; 834 sub = *ARGV; 835 newarg = insert(skel, sub); 836 if (ERR) 837 return; 838 839 l = strlen(newarg) + 1; 840 if ((ibufsize += l) > MAXIBUF) { 841 EMSG(IBUFOVERFLOW); 842 ERR = TRUE; 843 } 844 (void) strcpy(p_ibuf, newarg); 845 *(p->p_ARGV) = p_ibuf; 846 p_ibuf += l; 847 } 848 849 850 /* 851 * getwchr(): get the next wide character. 852 * description: 853 * we get the next character from stdin. This returns WEOF if no 854 * character is present. If ZERO is set, it gets a single byte instead 855 * a wide character. 856 */ 857 static wint_t 858 getwchr(char *mbc, size_t *sz) 859 { 860 size_t i; 861 int c; 862 wchar_t wch; 863 864 i = 0; 865 while (i < MB_CUR_MAX) { 866 867 if ((c = fgetc(stdin)) == EOF) { 868 869 if (i == 0) { 870 /* TRUE EOF has been reached */ 871 return (WEOF); 872 } 873 874 /* 875 * We have some characters in our buffer still so it 876 * must be an invalid character right before EOF. 877 */ 878 break; 879 } 880 mbc[i++] = (char)c; 881 882 /* If this succeeds then we are done */ 883 if (ZERO) { 884 *sz = i; 885 return ((char)c); 886 } 887 if (mbtowc(&wch, mbc, i) != -1) { 888 *sz = i; 889 return ((wint_t)wch); 890 } 891 } 892 893 /* 894 * We have now encountered an illegal character sequence. 895 * There is nothing much we can do at this point but 896 * return an error. If we attempt to recover we may in fact 897 * return garbage as arguments, from the customer's point 898 * of view. After all what if they are feeding us a file 899 * generated in another locale? 900 */ 901 errno = EILSEQ; 902 PERR(CORRUPTFILE); 903 exit(1); 904 /* NOTREACHED */ 905 } 906 907 908 static void 909 lcall(char *sub, char **subargs) 910 { 911 int retry = 0; 912 pid_t child; 913 914 for (;;) { 915 switch (child = forkx(FORK_NOSIGCHLD)) { 916 default: 917 procs_store(child); 918 /* 919 * Note, if we have used up all of our slots, then this 920 * call may end up blocking. 921 */ 922 procs_wait(B_FALSE); 923 return; 924 case 0: 925 (void) execvp(sub, subargs); 926 PERR(EXECFAIL); 927 if (errno == EACCES) 928 exit(126); 929 exit(127); 930 /* NOTREACHED */ 931 case -1: 932 if (errno != EAGAIN && retry++ < FORK_RETRY) { 933 PERR(FORKFAIL); 934 exit(123); 935 } 936 (void) sleep(1); 937 } 938 } 939 } 940 941 /* 942 * Return the index of child in the procs array. 943 */ 944 static int 945 procs_find(pid_t child) 946 { 947 int i; 948 949 for (i = 0; i < MAXPROCS; i++) { 950 if (procs[i] == child) { 951 return (i); 952 } 953 } 954 955 return (-1); 956 } 957 958 static void 959 procs_store(pid_t child) 960 { 961 int i; 962 963 i = procs_find(0); 964 if (i < 0) { 965 EMSG(NOCHILDSLOT); 966 exit(1); 967 } 968 procs[i] = child; 969 n_procs++; 970 } 971 972 static boolean_t 973 procs_delete(pid_t child) 974 { 975 int i; 976 977 i = procs_find(child); 978 if (i < 0) { 979 return (B_FALSE); 980 } 981 982 procs[i] = (pid_t)0; 983 n_procs--; 984 985 return (B_TRUE); 986 } 987 988 static pid_t 989 procs_waitpid(boolean_t blocking, int *stat_loc) 990 { 991 pid_t child; 992 int options; 993 994 if (n_procs == 0) { 995 errno = ECHILD; 996 return (-1); 997 } 998 999 options = 0; 1000 if (!blocking) { 1001 options |= WNOHANG; 1002 } 1003 1004 while ((child = waitpid((pid_t)-1, stat_loc, options)) > 0) { 1005 if (procs_delete(child)) { 1006 break; 1007 } 1008 } 1009 1010 return (child); 1011 } 1012 1013 static void 1014 procs_wait(boolean_t blocking) 1015 { 1016 pid_t child; 1017 int stat_loc; 1018 1019 /* 1020 * If we currently have filled all of our slots, then we need to block 1021 * further execution. 1022 */ 1023 if (n_procs >= MAXPROCS) 1024 blocking = B_TRUE; 1025 while ((child = procs_waitpid(blocking, &stat_loc)) > 0) { 1026 if (WIFSIGNALED(stat_loc)) { 1027 EMSG2(CHILDSIG, WTERMSIG(stat_loc)); 1028 exit(125); 1029 /* NOTREACHED */ 1030 } else if ((WEXITSTATUS(stat_loc) & 0377) == 0377) { 1031 EMSG(CHILDFAIL); 1032 exit(124); 1033 /* NOTREACHED */ 1034 } else { 1035 exitstat |= WEXITSTATUS(stat_loc); 1036 } 1037 } 1038 1039 if (child == (pid_t)(-1) && errno != ECHILD) { 1040 EMSG(WAITFAIL); 1041 exit(122); 1042 /* NOTREACHED */ 1043 } 1044 } 1045 1046 static void 1047 usage() 1048 { 1049 ermsg(_(USAGEMSG)); 1050 OK = FALSE; 1051 } 1052 1053 1054 1055 /* 1056 * parseargs(): modify the args 1057 * since the -e, -i and -l flags all take optional subarguments, 1058 * and getopts(3C) is clueless about this nonsense, we change the 1059 * our local argument count and strings to separate this out, 1060 * and make it easier to handle via getopts(3c). 1061 * 1062 * -e -> "-e "" 1063 * -e3 -> "-e "3" 1064 * -Estr -> "-E "str" 1065 * -i -> "-i "{}" 1066 * -irep -> "-i "rep" 1067 * -l -> "-l "1" 1068 * -l10 -> "-l "10" 1069 * 1070 * since the -e, -i and -l flags all take optional subarguments, 1071 */ 1072 static void 1073 parseargs(int ac, char **av) 1074 { 1075 int i; /* current argument */ 1076 int cflag; /* 0 = not processing cmd arg */ 1077 1078 if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) { 1079 PERR(MALLOCFAIL); 1080 exit(1); 1081 } 1082 1083 /* for each argument, see if we need to change things: */ 1084 for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) { 1085 if ((mav[mac] = strdup(av[i])) == NULL) { 1086 PERR(MALLOCFAIL); 1087 exit(1); 1088 } 1089 1090 /* -- has been found or argument list is fully processes */ 1091 if (cflag) 1092 continue; 1093 1094 /* 1095 * if we're doing special processing, and we've got a flag 1096 */ 1097 else if ((av[i][0] == '-') && (av[i][1] != NULL)) { 1098 char *def; 1099 1100 switch (av[i][1]) { 1101 case 'e': 1102 def = ""; /* -e with no arg turns off eof */ 1103 goto process_special; 1104 case 'i': 1105 def = INSPAT_STR; 1106 goto process_special; 1107 case 'l': 1108 def = "1"; 1109 process_special: 1110 /* 1111 * if there's no sub-option, we *must* add 1112 * a default one. this is because xargs must 1113 * be able to distinguish between a valid 1114 * suboption, and a command name. 1115 */ 1116 if (av[i][2] == NULL) { 1117 mav[++mac] = strdup(def); 1118 } else { 1119 /* clear out our version: */ 1120 mav[mac][2] = NULL; 1121 mav[++mac] = strdup(&av[i][2]); 1122 } 1123 if (mav[mac] == NULL) { 1124 PERR(MALLOCFAIL); 1125 exit(1); 1126 } 1127 break; 1128 1129 /* flags with required subarguments: */ 1130 1131 /* 1132 * there are two separate cases here. either the 1133 * flag can have the normal XCU4 handling 1134 * (of the form: -X subargument); or it can have 1135 * the old solaris 2.[0-4] handling (of the 1136 * form: -Xsubargument). in order to maintain 1137 * backwards compatibility, we must support the 1138 * latter case. we handle the latter possibility 1139 * first so both the old solaris way of handling 1140 * and the new XCU4 way of handling things are allowed. 1141 */ 1142 case 'n': /* FALLTHROUGH */ 1143 case 'P': /* FALLTHROUGH */ 1144 case 's': /* FALLTHROUGH */ 1145 case 'E': /* FALLTHROUGH */ 1146 case 'I': /* FALLTHROUGH */ 1147 case 'L': 1148 /* 1149 * if the second character isn't null, then 1150 * the user has specified the old syntax. 1151 * we move the subargument into our 1152 * mod'd argument list. 1153 */ 1154 if (av[i][2] != NULL) { 1155 /* first clean things up: */ 1156 mav[mac][2] = NULL; 1157 1158 /* now add the separation: */ 1159 ++mac; /* inc to next mod'd arg */ 1160 if ((mav[mac] = strdup(&av[i][2])) == 1161 NULL) { 1162 PERR(MALLOCFAIL); 1163 exit(1); 1164 } 1165 break; 1166 } 1167 i++; 1168 mac++; 1169 1170 if (av[i] == NULL) { 1171 mav[mac] = NULL; 1172 return; 1173 } 1174 if ((mav[mac] = strdup(av[i])) == NULL) { 1175 PERR(MALLOCFAIL); 1176 exit(1); 1177 } 1178 break; 1179 1180 /* flags */ 1181 case 'p' : 1182 case 't' : 1183 case 'x' : 1184 case '0' : 1185 break; 1186 1187 case '-' : 1188 default: 1189 /* 1190 * here we've hit the cmd argument. so 1191 * we'll stop special processing, as the 1192 * cmd may have a "-i" etc., argument, 1193 * and we don't want to add a "" to it. 1194 */ 1195 cflag = 1; 1196 break; 1197 } 1198 } else if (i > 0) { /* if we're not the 1st arg */ 1199 /* 1200 * if it's not a flag, then it *must* be the cmd. 1201 * set cflag, so we don't mishandle the -[eil] flags. 1202 */ 1203 cflag = 1; 1204 } 1205 } 1206 1207 mav[mac] = NULL; 1208 } 1209