1 /* xargs -- build and execute command lines from standard input
2 Copyright (C) 1990, 1991, 1992, 1993, 1994, 2000, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /* Written by Mike Rendell <michael@cs.mun.ca>
20 and David MacKenzie <djm@gnu.org>.
26 /* config.h must be included first. */
43 #include <sys/types.h>
54 #include "safe-read.h"
60 #include "findutils-version.h"
64 # define _(Text) gettext (Text)
67 #define textdomain(Domain)
68 #define bindtextdomain(Package, Directory)
71 # define N_(String) gettext_noop(String)
73 /* See locate.c for explanation as to why not use (String) */
74 # define N_(String) String
78 #define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
81 #define ISBLANK(c) (isascii (c) && isblank (c))
82 #define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
83 || (c) == '\f' || (c) == '\v')
85 /* Return nonzero if S is the EOF string. */
86 #define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))
88 extern char *version_string;
90 static FILE *input_stream;
92 /* Buffer for reading arguments from input. */
95 static int keep_stdin = 0;
97 /* Line number in stdin since the last command was executed. */
98 static size_t lineno = 0;
100 static struct buildcmd_state bc_state;
101 static struct buildcmd_control bc_ctl;
103 /* Did we already complain about NUL characters in the input? */
104 static int nullwarning_given = 0;
107 /* If nonzero, when this string is read on stdin it is treated as
109 IEEE Std 1003.1, 2004 Edition allows this to be NULL.
110 In findutils releases up to and including 4.2.8, this was "_".
112 static char *eof_str = NULL;
114 /* Number of chars in the initial args. */
115 /* static int initial_argv_chars = 0; */
117 /* true when building up initial arguments in `cmd_argv'. */
118 static bool initial_args = true;
120 /* If nonzero, the maximum number of child processes that can be running
122 /* TODO: check conversion safety (i.e. range) for -P option. */
123 static volatile sig_atomic_t proc_max = 1;
125 /* Did we fork a child yet? */
126 static bool procs_executed = false;
128 /* The number of elements in `pids'. */
129 static unsigned long int procs_executing = 0uL;
131 /* List of child processes currently executing. */
132 static pid_t *pids = NULL;
134 /* The number of allocated elements in `pids'. */
135 static size_t pids_alloc = 0u;
137 /* Process ID of the parent xargs process. */
140 /* If nonzero, we've been signaled that we can start more child processes. */
141 static volatile sig_atomic_t stop_waiting = 0;
143 /* Exit status; nonzero if any child process exited with a
145 static volatile int child_error = EXIT_SUCCESS;
147 static volatile int original_exit_value;
149 /* If true, print each command on stderr before executing it. */
150 static bool print_command = false; /* Option -t */
152 /* If true, query the user before executing each command, and only
153 execute the command if the user responds affirmatively. */
154 static bool query_before_executing = false;
156 /* The delimiter for input arguments. This is only consulted if the
157 * -0 or -d option had been given.
159 static char input_delimiter = '\0';
162 /* Name of the environment variable which indicates which 'slot'
163 * the child process is in. This can be used to do some kind of basic
164 * load distribution. We guarantee not to allow two processes to run
165 * at the same time with the same value of this variable.
167 static char* slot_var_name = NULL;
169 enum LongOptionIdentifier
171 PROCESS_SLOT_VAR = CHAR_MAX+1
174 static struct option const longopts[] =
176 {"null", no_argument, NULL, '0'},
177 {"arg-file", required_argument, NULL, 'a'},
178 {"delimiter", required_argument, NULL, 'd'},
179 {"eof", optional_argument, NULL, 'e'},
180 {"replace", optional_argument, NULL, 'I'},
181 {"max-lines", optional_argument, NULL, 'l'},
182 {"max-args", required_argument, NULL, 'n'},
183 {"interactive", no_argument, NULL, 'p'},
184 {"no-run-if-empty", no_argument, NULL, 'r'},
185 {"max-chars", required_argument, NULL, 's'},
186 {"verbose", no_argument, NULL, 't'},
187 {"show-limits", no_argument, NULL, 'S'},
188 {"exit", no_argument, NULL, 'x'},
189 {"max-procs", required_argument, NULL, 'P'},
190 {"process-slot-var", required_argument, NULL, PROCESS_SLOT_VAR},
191 {"version", no_argument, NULL, 'v'},
192 {"help", no_argument, NULL, 'h'},
193 {NULL, no_argument, NULL, 0}
196 /* xargs exits with status values with specific meanings (this is a POSIX requirement).
197 These are the values.
199 enum XargsStatusValues {
200 XARGS_EXIT_CLIENT_EXIT_NONZERO = 123, /* utility exited with nonzero status */
201 XARGS_EXIT_CLIENT_EXIT_255 = 124, /* utility exites with status 255 */
202 XARGS_EXIT_CLIENT_FATAL_SIG = 125, /* utility died from a fatal signal */
203 XARGS_EXIT_COMMAND_CANNOT_BE_RUN = 126, /* canot run the command */
204 XARGS_EXIT_COMMAND_NOT_FOUND = 127, /* cannot find the command */
206 /* Exit status values the child might use. */
207 enum ClientStatusValues {
208 CHILD_EXIT_PLEASE_STOP_IMMEDIATELY = 255
211 static int read_line (void);
212 static int read_string (void);
213 static bool print_args (bool ask);
214 /* static void do_exec (void); */
215 static int xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv);
216 static void exec_if_possible (void);
217 static unsigned int add_proc (pid_t pid);
218 static void wait_for_proc (bool all, unsigned int minreap);
219 static void wait_for_proc_all (void);
220 static void increment_proc_max (int);
221 static void decrement_proc_max (int);
222 static long parse_num (char *str, int option, long min, long max, int fatal);
223 static void usage (FILE * stream);
227 get_char_oct_or_hex_escape (const char *s)
234 assert ('\\' == s[0]);
242 else if (isdigit ((unsigned char) s[1]))
250 p = NULL; /* Silence compiler warning. */
251 error (EXIT_FAILURE, 0,
252 _("Invalid escape sequence %s in input delimiter specification."),
257 val = strtoul (p, &endp, base);
259 /* This if condition is carefully constructed to do
260 * the right thing if UCHAR_MAX has the same
261 * value as ULONG_MAX. IF UCHAR_MAX==ULONG_MAX,
262 * then val can never be greater than UCHAR_MAX.
264 if ((ULONG_MAX == val && ERANGE == errno)
265 || (val > UCHAR_MAX))
269 error (EXIT_FAILURE, 0,
270 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
271 s, (unsigned long)UCHAR_MAX);
275 error (EXIT_FAILURE, 0,
276 _("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
277 s, (unsigned long)UCHAR_MAX);
281 /* check for trailing garbage */
284 error (EXIT_FAILURE, 0,
285 _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
294 get_input_delimiter (const char *s)
324 return get_char_oct_or_hex_escape (s);
329 error (EXIT_FAILURE, 0,
330 _("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
345 fail_due_to_env_size (void)
347 error (EXIT_FAILURE, 0, _("environment is too large for exec"));
351 smaller_of (size_t a, size_t b)
362 main (int argc, char **argv)
364 int optc, option_index;
365 int show_limits = 0; /* --show-limits */
366 int always_run_command = 1;
367 const char *input_file = "-"; /* "-" is stdin */
368 char default_cmd[] = "echo";
369 char *default_arglist[1];
370 int (*read_args) (void) = read_line;
371 void (*act_on_init_result)(void) = noop;
372 enum BC_INIT_STATUS bcstatus;
373 enum { XARGS_POSIX_HEADROOM = 2048u };
374 struct sigaction sigact;
377 set_program_name (argv[0]);
379 set_program_name ("xargs");
382 original_exit_value = EXIT_SUCCESS;
384 #ifdef HAVE_SETLOCALE
385 setlocale (LC_ALL, "");
387 bindtextdomain (PACKAGE, LOCALEDIR);
388 textdomain (PACKAGE);
390 if (atexit (close_stdin) || atexit (wait_for_proc_all))
392 error (EXIT_FAILURE, errno, _("The atexit library function failed"));
395 /* xargs is required by POSIX to allow 2048 bytes of headroom
396 * for extra environment variables (that perhaps the utliity might
397 * want to set before execing something else).
399 bcstatus = bc_init_controlinfo (&bc_ctl, XARGS_POSIX_HEADROOM);
401 /* The bc_init_controlinfo call may have determined that the
402 * environment is too big. In that case, we will fail with
403 * an error message after processing the command-line options,
404 * as "xargs --help" should still work even if the environment is
407 * Some of the argument processing depends on the contents of
408 * bc_ctl, which will be in an undefined state if bc_init_controlinfo ()
411 if (BC_INIT_ENV_TOO_BIG == bcstatus)
413 act_on_init_result = fail_due_to_env_size;
415 else if (BC_INIT_CANNOT_ACCOMODATE_HEADROOM == bcstatus)
417 /* All POSIX systems are required to support ARG_MAX of at least
418 * 4096. For everything to work the total of (command line +
419 * headroom + environment) must fit into this. POSIX requires
420 * that we use a headroom of 2048 bytes. The user is in control
421 * of the size of the environment.
423 * In general if bc_init_controlinfo () returns
424 * BC_INIT_CANNOT_ACCOMODATE_HEADROOM, its caller can try again
425 * with a smaller headroom. However, in the case of xargs, this
426 * would not be POSIX-compliant.
428 act_on_init_result = fail_due_to_env_size;
432 /* IEEE Std 1003.1, 2003 specifies that the combined argument and
433 * environment list shall not exceed {ARG_MAX}-2048 bytes. It also
434 * specifies that it shall be at least LINE_MAX.
438 val = sysconf (_SC_ARG_MAX);
441 assert (val > XARGS_POSIX_HEADROOM);
442 /* Note that val can in fact be greater than ARG_MAX
443 * and bc_ctl.arg_max can also be greater than ARG_MAX.
445 bc_ctl.arg_max = smaller_of (bc_ctl.arg_max,
446 (size_t)val-XARGS_POSIX_HEADROOM);
451 assert (ARG_MAX > XARGS_POSIX_HEADROOM);
452 bc_ctl.arg_max = smaller_of (bc_ctl.arg_max,
453 (ARG_MAX - XARGS_POSIX_HEADROOM));
458 assert (ARG_MAX > XARGS_POSIX_HEADROOM);
459 bc_ctl.arg_max = smaller_of (bc_ctl.arg_max,
460 (ARG_MAX - XARGS_POSIX_HEADROOM));
465 /* This assertion ensures that this xargs implementation
466 * conforms to the POSIX requirement that the default command
467 * line length shall be at least LINE_MAX.
469 assert (bc_ctl.arg_max >= LINE_MAX);
472 bc_ctl.exec_callback = xargs_do_exec;
474 /* Start with a reasonable default size, though this can be
475 * adjusted via the -s option.
477 bc_use_sensible_arg_max (&bc_ctl);
480 while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
481 longopts, &option_index)) != -1)
486 read_args = read_string;
487 input_delimiter = '\0';
491 read_args = read_string;
492 input_delimiter = get_input_delimiter (optarg);
495 case 'E': /* POSIX */
496 case 'e': /* deprecated */
497 if (optarg && (strlen (optarg) > 0))
507 case 'I': /* POSIX */
508 case 'i': /* deprecated */
510 bc_ctl.replace_pat = optarg;
512 bc_ctl.replace_pat = "{}";
513 /* -i excludes -n -l. */
514 bc_ctl.args_per_exec = 0;
515 bc_ctl.lines_per_exec = 0;
518 case 'L': /* POSIX */
519 bc_ctl.lines_per_exec = parse_num (optarg, 'L', 1L, -1L, 1);
520 /* -L excludes -i -n. */
521 bc_ctl.args_per_exec = 0;
522 bc_ctl.replace_pat = NULL;
525 case 'l': /* deprecated */
527 bc_ctl.lines_per_exec = parse_num (optarg, 'l', 1L, -1L, 1);
529 bc_ctl.lines_per_exec = 1;
530 /* -l excludes -i -n. */
531 bc_ctl.args_per_exec = 0;
532 bc_ctl.replace_pat = NULL;
536 bc_ctl.args_per_exec = parse_num (optarg, 'n', 1L, -1L, 1);
537 /* -n excludes -i -l. */
538 bc_ctl.lines_per_exec = 0;
539 if (bc_ctl.args_per_exec == 1 && bc_ctl.replace_pat)
540 /* ignore -n1 in '-i -n1' */
541 bc_ctl.args_per_exec = 0;
543 bc_ctl.replace_pat = NULL;
546 /* The POSIX standard specifies that it is not an error
547 * for the -s option to specify a size that the implementation
548 * cannot support - in that case, the relevant limit is used.
553 act_on_init_result ();
554 arg_size = parse_num (optarg, 's', 1L,
555 bc_ctl.posix_arg_size_max, 0);
556 if (arg_size > bc_ctl.posix_arg_size_max)
559 _("warning: value %ld for -s option is too large, "
560 "using %ld instead"),
561 (long) arg_size, (long) bc_ctl.posix_arg_size_max);
562 arg_size = bc_ctl.posix_arg_size_max;
564 bc_ctl.arg_max = arg_size;
573 print_command = true;
577 bc_ctl.exit_if_size_exceeded = true;
581 query_before_executing = true;
582 print_command = true;
586 always_run_command = 0;
590 /* Allow only up to LONG_MAX child processes. */
591 proc_max = parse_num (optarg, 'P', 0L, LONG_MAX, 1);
599 display_findutils_version ("xargs");
602 case PROCESS_SLOT_VAR:
603 if (strchr (optarg, '='))
605 error (EXIT_FAILURE, 0,
606 _("option --%s may not be set to a value which includes `='"),
607 longopts[option_index].name);
609 slot_var_name = optarg;
610 if (0 != unsetenv (slot_var_name))
612 /* This is a fatal error, otherwise some child process
613 may not be able to guarantee that no two children
614 have the same value for this variable; see
617 error (EXIT_FAILURE, errno,
618 _("failed to unset environment variable %s"),
629 if (eof_str && (read_args == read_string))
632 _("warning: the -E option has no effect if -0 or -d is used.\n"));
635 /* If we had deferred failing due to problems in bc_init_controlinfo (),
638 * We issue this error message after processing command line
639 * arguments so that it is possible to use "xargs --help" even if
640 * the environment is too large.
642 act_on_init_result ();
643 assert (BC_INIT_OK == bcstatus);
647 /* Accept signals to increase or decrease the number of running
648 child processes. Do this as early as possible after setting
650 sigact.sa_handler = increment_proc_max;
651 sigemptyset(&sigact.sa_mask);
653 if (0 != sigaction (SIGUSR1, &sigact, (struct sigaction *)NULL))
654 error (0, errno, _("Cannot set SIGUSR1 signal handler"));
656 sigact.sa_handler = decrement_proc_max;
657 sigemptyset(&sigact.sa_mask);
659 if (0 != sigaction (SIGUSR2, &sigact, (struct sigaction *)NULL))
660 error (0, errno, _("Cannot set SIGUSR2 signal handler"));
665 if (0 == strcmp (input_file, "-"))
667 input_stream = stdin;
671 keep_stdin = 1; /* see prep_child_for_exec () */
672 input_stream = fopen (input_file, "r");
673 if (NULL == input_stream)
675 error (EXIT_FAILURE, errno,
676 _("Cannot open input file %s"),
677 quotearg_n_style (0, locale_quoting_style, input_file));
681 if (bc_ctl.replace_pat || bc_ctl.lines_per_exec)
682 bc_ctl.exit_if_size_exceeded = true;
688 default_arglist[0] = default_cmd;
689 argv = default_arglist;
695 _("Your environment variables take up %" PRIuMAX " bytes\n"),
696 (uintmax_t)bc_size_of_environment ());
698 _("POSIX upper limit on argument length (this system): %" PRIuMAX "\n"),
699 (uintmax_t)bc_ctl.posix_arg_size_max);
701 _("POSIX smallest allowable upper limit on argument length (all systems): %" PRIuMAX "\n"),
702 (uintmax_t)bc_ctl.posix_arg_size_min);
704 _("Maximum length of command we could actually use: %" PRIuMAX "\n"),
705 (uintmax_t)(bc_ctl.posix_arg_size_max - bc_size_of_environment ()));
707 _("Size of command buffer we are actually using: %" PRIuMAX "\n"),
708 (uintmax_t)bc_ctl.arg_max);
710 if (isatty (STDIN_FILENO))
714 "Execution of xargs will continue now, and it will "
715 "try to read its input and run commands; if this is "
716 "not what you wanted to happen, please type the "
717 "end-of-file keystroke.\n"));
718 if (always_run_command)
721 _("Warning: %s will be run at least once. "
722 "If you do not want that to happen, then press "
723 "the interrupt keystroke.\n"),
729 linebuf = xmalloc (bc_ctl.arg_max + 1);
730 bc_state.argbuf = xmalloc (bc_ctl.arg_max + 1);
732 /* Make sure to listen for the kids. */
733 signal (SIGCHLD, SIG_DFL);
735 if (!bc_ctl.replace_pat)
737 for (; optind < argc; optind++)
738 bc_push_arg (&bc_ctl, &bc_state,
739 argv[optind], strlen (argv[optind]) + 1,
742 initial_args = false;
743 bc_ctl.initial_argc = bc_state.cmd_argc;
744 bc_state.cmd_initial_argv_chars = bc_state.cmd_argv_chars;
745 bc_ctl.initial_argc = bc_state.cmd_argc;
746 /*fprintf (stderr, "setting initial_argc=%d\n", bc_state.cmd_initial_argc);*/
748 while ((*read_args) () != -1)
749 if (bc_ctl.lines_per_exec && lineno >= bc_ctl.lines_per_exec)
751 bc_do_exec (&bc_ctl, &bc_state);
755 /* SYSV xargs seems to do at least one exec, even if the
757 if (bc_state.cmd_argc != bc_ctl.initial_argc
758 || (always_run_command && procs_executed==0))
759 bc_do_exec (&bc_ctl, &bc_state);
765 size_t *arglen = xmalloc (sizeof (size_t) * argc);
767 for (i = optind; i < argc; i++)
768 arglen[i] = strlen (argv[i]);
769 bc_ctl.rplen = strlen (bc_ctl.replace_pat);
770 while ((args = (*read_args) ()) != -1)
772 size_t len = (size_t) args;
773 /* Don't do insert on the command name. */
774 bc_clear_args (&bc_ctl, &bc_state);
775 bc_state.cmd_argv_chars = 0; /* begin at start of buffer */
777 bc_push_arg (&bc_ctl, &bc_state,
778 argv[optind], arglen[optind] + 1,
782 initial_args = false;
784 for (i = optind + 1; i < argc; i++)
785 bc_do_insert (&bc_ctl, &bc_state,
790 bc_do_exec (&bc_ctl, &bc_state);
794 original_exit_value = child_error;
799 /* Read a line of arguments from the input and add them to the list of
800 arguments to pass to the command. Ignore blank lines and initial blanks.
801 Single and double quotes and backslashes quote metacharacters and blanks
802 as they do in the shell.
803 Return -1 if eof (either physical or logical) is reached,
804 otherwise the length of the last string read (including the null). */
809 /* States for read_line. */
817 static bool eof = false;
818 /* Start out in mode SPACE to always strip leading spaces (even with -i). */
819 enum read_line_state state = SPACE; /* The type of character we last read. */
820 int prevc; /* The previous value of c. */
821 int quotc = 0; /* The last quote character read. */
823 bool first = true; /* true if reading first arg on line. */
824 bool seen_arg = false; /* true if we have seen any arg (or part of one) yet */
827 /* Including the NUL, the args must not grow past this point. */
828 char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
835 c = getc (input_stream);
839 /* COMPAT: SYSV seems to ignore stuff on a line that
840 ends without a \n; we don't. */
849 error (EXIT_FAILURE, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
850 quotc == '"' ? _("double") : _("single"));
852 if (first && EOF_STR (linebuf))
854 if (!bc_ctl.replace_pat)
855 bc_push_arg (&bc_ctl, &bc_state,
872 if (!ISBLANK (prevc))
873 lineno++; /* For -l. */
878 /* An empty argument, add it to the list as normal. */
889 if (EOF_STR (linebuf))
892 return first ? -1 : len;
894 if (!bc_ctl.replace_pat)
895 bc_push_arg (&bc_ctl, &bc_state,
903 /* POSIX: In the POSIX locale, the separators are <SPC> and
904 * <TAB>, but not <FF> or <VT>.
906 if (!bc_ctl.replace_pat && ISBLANK (c))
910 if (EOF_STR (linebuf))
913 return first ? -1 : len;
915 bc_push_arg (&bc_ctl, &bc_state,
942 error (EXIT_FAILURE, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
943 quotc == '"' ? _("double") : _("single"));
948 seen_arg = true; /* Makes a difference for e.g. just '' or "" as the first arg on a line */
958 if ( (0 == c) && !nullwarning_given )
960 /* This is just a warning message. We only issue it once. */
962 _("WARNING: a NUL character occurred in the input. "
963 "It cannot be passed through in the argument list. "
964 "Did you mean to use the --null option?"));
965 nullwarning_given = 1;
972 error (EXIT_FAILURE, 0, _("argument line too long"));
976 append_char_to_buf (&linebuf, &endbuf, &p, c);
981 /* Read a string (terminated by the delimiter, which may be NUL) from
982 the input and add it to the list of arguments to pass to the
985 The return value is the length of the added argument, including its
986 terminating NUL. The added argument is always terminated by NUL,
987 even if that is not the delimiter.
989 If we reach physical EOF before seeing the delimiter, we treat any
990 characters read as the final argument.
992 If no argument was read (that is, we reached physical EOF before
993 reading any characters) then -1 is returned. */
997 static bool eof = false;
1000 /* Including the NUL, the args must not grow past this point. */
1001 char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
1007 int c = getc (input_stream);
1015 if (!bc_ctl.replace_pat)
1016 bc_push_arg (&bc_ctl, &bc_state,
1022 if (c == input_delimiter)
1024 lineno++; /* For -l. */
1027 if (!bc_ctl.replace_pat)
1028 bc_push_arg (&bc_ctl, &bc_state,
1036 exec_if_possible ();
1037 error (EXIT_FAILURE, 0, _("argument line too long"));
1043 /* Print the arguments of the command to execute.
1044 If ASK is nonzero, prompt the user for a response, and
1045 if the user responds affirmatively, return true;
1046 otherwise, return false. */
1049 print_args (bool ask)
1053 for (i = 0; i < bc_state.cmd_argc - 1; i++)
1055 if (fprintf (stderr, "%s ", bc_state.cmd_argv[i]) < 0)
1056 error (EXIT_FAILURE, errno, _("Failed to write to stderr"));
1061 static FILE *tty_stream;
1066 tty_stream = fopen ("/dev/tty", "r");
1068 error (EXIT_FAILURE, errno,
1069 _("failed to open /dev/tty for reading"));
1071 fputs ("?...", stderr);
1072 if (fflush (stderr) != 0)
1073 error (EXIT_FAILURE, errno, _("Failed to write to stderr"));
1075 c = savec = getc (tty_stream);
1076 while (c != EOF && c != '\n')
1077 c = getc (tty_stream);
1079 error (EXIT_FAILURE, errno, _("Failed to read from stdin"));
1080 if (savec == 'y' || savec == 'Y')
1084 putc ('\n', stderr);
1089 /* Set SOME_ENVIRONMENT_VARIABLE=n in the environment. */
1091 set_slot_var (unsigned int n)
1093 static const char *fmt = "%u";
1098 /* Determine the length of the buffer we need.
1100 If the result would be zero-length or have length (not value) >
1101 INT_MAX, the assumptions we made about how snprintf behaves (or
1102 what UINT_MAX is) are wrong. Hence we have a design error (not
1103 an environmental error).
1105 size = snprintf (NULL, 0u, fmt, n);
1109 /* Failures here are undesirable but not fatal, since we can still
1110 guarantee that this child does not have a duplicate value of the
1111 indicated environment variable set (since the parent unset it on
1114 if (NULL == (buf = malloc (size+1)))
1116 error (0, errno, _("unable to allocate memory"));
1120 snprintf (buf, size+1, fmt, n);
1122 /* If the user doesn't want us to set the variable, there is
1123 nothing to do. However, we defer the bail-out until this
1124 point in order to get better test coverage.
1128 if (setenv (slot_var_name, buf, 1) < 0)
1131 _("failed to set environment variable %s"), slot_var_name);
1139 /* Close stdin and attach /dev/null to it.
1140 * This resolves Savannah bug #3992.
1143 prep_child_for_exec (void)
1145 /* The parent will call add_proc to allocate a slot. We do the same in the
1146 child to make sure we get the same value.
1148 We use 0 here in order to avoid generating a data structure that appears
1149 to indicate that we (the child) have a child. */
1150 unsigned int slot = add_proc (0);
1151 set_slot_var (slot);
1155 const char inputfile[] = "/dev/null";
1156 /* fprintf (stderr, "attaching stdin to /dev/null\n"); */
1159 if (open (inputfile, O_RDONLY) < 0)
1161 /* This is not entirely fatal, since
1162 * executing the child with a closed
1163 * stdin is almost as good as executing it
1164 * with its stdin attached to /dev/null.
1166 error (0, errno, "%s",
1167 quotearg_n_style (0, locale_quoting_style, inputfile));
1173 /* Execute the command that has been built in `cmd_argv'. This may involve
1174 waiting for processes that were previously executed.
1176 There are a number of cases where we want to terminate the current (child)
1177 process. We do this by calling _exit () rather than exit () in order to
1178 avoid the invocation of wait_for_proc_all (), which was registered by the parent
1179 as an atexit () function.
1182 xargs_do_exec (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv)
1195 while (procs_executing >= proc_max)
1197 wait_for_proc (false, 1u);
1201 if (!query_before_executing || print_args (true))
1203 if (!query_before_executing && print_command)
1206 /* Before forking, reap any already-exited child. We do this so
1207 that we don't leave unreaped children around while we build a
1208 new command line. For example this command will spend most
1209 of its time waiting for sufficient arguments to launch
1210 another command line:
1212 seq 1 1000 | fmt | while read x ; do echo $x; sleep 1 ; done |
1213 ./xargs -P 200 -n 20 sh -c 'echo "$@"; sleep $((1 + $RANDOM % 5))' sleeper
1215 wait_for_proc (false, 0u);
1218 error (EXIT_FAILURE, errno, _("could not create pipe before fork"));
1219 fcntl (fd[1], F_SETFD, FD_CLOEXEC);
1221 /* If we run out of processes, wait for a child to return and
1223 while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
1224 wait_for_proc (false, 1u);
1229 error (EXIT_FAILURE, errno, _("cannot fork"));
1231 case 0: /* Child. */
1234 child_error = EXIT_SUCCESS;
1236 prep_child_for_exec ();
1238 if (bc_args_exceed_testing_limit (argv))
1241 execvp (argv[0], argv);
1244 /* Write errno value to parent. We do this even if
1245 * the error was not E2BIG, because we need to
1246 * distinguish successful execs from unsuccessful
1247 * ones. The reason we need to do this is to know
1248 * whether to reap the child here (preventing the
1249 * exit status processing in wait_for_proc () from
1250 * changing the value of child_error) or leave it
1251 * for wait_for_proc () to handle. We need to have
1252 * wait_for_proc () handle the exit values from the
1253 * utility if we run it, for POSIX compliance on the
1254 * handling of exit values.
1256 write (fd[1], &errno, sizeof (int));
1262 error (0, errno, "%s", argv[0]);
1264 /* The actual value returned here should be irrelevant,
1265 * because the parent will test our value of errno.
1267 _exit (errno == ENOENT ? XARGS_EXIT_COMMAND_NOT_FOUND : XARGS_EXIT_COMMAND_CANNOT_BE_RUN);
1278 } /* switch (child) */
1279 /*fprintf (stderr, "forked child (bc_state.cmd_argc=%d) -> ", bc_state.cmd_argc);*/
1281 /* We use safe_read here in order to avoid an error if
1282 SIGUSR[12] is handled during the read system call. */
1283 switch (r = safe_read (fd[0], &buf, sizeof (int)))
1285 case SAFE_READ_ERROR:
1289 _("errno-buffer safe_read failed in xargs_do_exec "
1290 "(this is probably a bug, please report it)"));
1301 /* we know the child is about to exit, so wait for that.
1302 * We have to do this so that wait_for_proc () does not
1303 * change the value of child_error on the basis of the
1304 * return value -- since in this case we did not launch
1307 * We do the wait before deciding if we failed in order to
1308 * avoid creating a zombie, even briefly.
1310 waitpid (child, &childstatus, 0);
1315 return 0; /* Failure; caller should pass fewer args */
1317 else if (ENOENT == buf)
1319 exit (XARGS_EXIT_COMMAND_NOT_FOUND); /* command cannot be found */
1323 exit (XARGS_EXIT_COMMAND_CANNOT_BE_RUN); /* command cannot be run */
1330 /* Failed to read data from pipe; the exec must have
1331 * succeeded. We call add_proc only in this case,
1332 * because it increments procs_executing, and we only
1333 * want to do that if we didn't already wait for the
1341 error (EXIT_FAILURE, errno,
1342 _("read returned unexpected value %zu; "
1343 "this is probably a bug, please report it"), r);
1345 } /* switch on bytes read */
1348 return 1; /* Success */
1351 /* Execute the command if possible. */
1354 exec_if_possible (void)
1356 if (bc_ctl.replace_pat || initial_args ||
1357 bc_state.cmd_argc == bc_ctl.initial_argc || bc_ctl.exit_if_size_exceeded)
1359 bc_do_exec (&bc_ctl, &bc_state);
1362 /* Add the process with id PID to the list of processes that have
1366 add_proc (pid_t pid)
1370 /* Find an empty slot. */
1371 for (i = 0; i < pids_alloc && pids[i]; i++)
1374 /* Extend the array if we failed. */
1375 if (i == pids_alloc)
1377 pids = x2nrealloc (pids, &pids_alloc, sizeof *pids);
1379 /* Zero out the new slots. */
1380 for (j=i; j<pids_alloc; ++j)
1383 /* Verify that we are not destroying the record of some existing child. */
1384 assert (0 == pids[i]);
1386 /* Remember the child. */
1389 procs_executed = true;
1394 /* If ALL is true, wait for all child processes to finish;
1395 otherwise, wait for one child process to finish, or for another signal
1396 that tells us that we can run more child processes.
1397 Remove the processes that finish from the list of executing processes. */
1400 wait_for_proc (bool all, unsigned int minreap)
1402 unsigned int reaped = 0;
1404 while (procs_executing)
1413 if (reaped >= minreap)
1415 /* We already reaped enough children. To save system
1416 * resources, reap any dead children anyway, but do not
1417 * wait for any currently executing children to exit.
1427 /* Wait for any child. We used to use wait () here, but it's
1428 * unlikely that that offers any portability advantage over
1431 while ((pid = waitpid (-1, &status, wflags)) == (pid_t) -1)
1434 error (EXIT_FAILURE, errno,
1435 _("error waiting for child process"));
1437 if (stop_waiting && !all)
1439 /* Receipt of SIGUSR1 gave us an extra slot and we
1440 * don't need to wait for all processes to finish.
1441 * We can stop reaping now, but in any case check for
1442 * further dead children without waiting for another
1449 /* Find the entry in `pids' for the child process
1453 for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1457 while (pid && i == pids_alloc); /* A child died that we didn't start? */
1461 if (!(wflags & WNOHANG))
1463 /* Nothing remained to be reaped. This should not
1464 * happen, because procs_executing should contain the
1465 * number of child processes still executing, so the
1466 * loop should have terminated.
1468 error (0, 0, _("WARNING: Lost track of %lu child processes"),
1473 /* Children are (probably) executing but are not ready
1474 * to be reaped at the moment.
1480 /* Remove the child from the list. */
1485 if (WEXITSTATUS (status) == CHILD_EXIT_PLEASE_STOP_IMMEDIATELY)
1486 error (XARGS_EXIT_CLIENT_EXIT_255, 0,
1487 _("%s: exited with status 255; aborting"), bc_state.cmd_argv[0]);
1488 if (WIFSTOPPED (status))
1489 error (XARGS_EXIT_CLIENT_FATAL_SIG, 0,
1490 _("%s: stopped by signal %d"), bc_state.cmd_argv[0], WSTOPSIG (status));
1491 if (WIFSIGNALED (status))
1492 error (XARGS_EXIT_CLIENT_FATAL_SIG, 0,
1493 _("%s: terminated by signal %d"), bc_state.cmd_argv[0], WTERMSIG (status));
1494 if (WEXITSTATUS (status) != 0)
1495 child_error = XARGS_EXIT_CLIENT_EXIT_NONZERO;
1499 /* Wait for all child processes to finish. */
1502 wait_for_proc_all (void)
1504 static bool waiting = false;
1506 /* This function was registered by the original, parent, process.
1507 * The child processes must not call exit () to terminate, since this
1508 * will mean that their exit status will be inappropriately changed.
1509 * Instead the child processes should call _exit (). If someone
1510 * forgets this rule, you may see the following assert () fail.
1512 assert (getpid () == parent);
1518 wait_for_proc (true, 0u);
1521 if (original_exit_value != child_error)
1523 /* wait_for_proc () changed the value of child_error (). This
1524 * function is registered via atexit (), and so may have been
1525 * called from exit (). We now know that the original value
1526 * passed to exit () is no longer the exit status we require.
1527 * The POSIX standard states that the behaviour if exit () is
1528 * called more than once is undefined. Therefore we now have to
1529 * exit with _exit () instead of exit ().
1531 _exit (child_error);
1537 /* Increment or decrement the number of processes we can start simultaneously,
1538 when we receive a signal from the outside world.
1540 We must take special care around proc_max == 0 (unlimited children),
1541 proc_max == 1 (don't decrement to zero). */
1543 increment_proc_max (int ignore)
1546 /* If user increments from 0 to 1, we'll take it and serialize. */
1548 /* If we're waiting for a process to die before doing something,
1549 no need to wait any more. */
1554 decrement_proc_max (int ignore)
1562 /* Return the value of the number represented in STR.
1563 OPTION is the command line option to which STR is the argument.
1564 If the value does not fall within the boundaries MIN and MAX,
1565 Print an error message mentioning OPTION. If FATAL is true,
1569 parse_num (char *str, int option, long int min, long int max, int fatal)
1574 val = strtol (str, &eptr, 10);
1575 if (eptr == str || *eptr)
1577 fprintf (stderr, _("%s: invalid number for -%c option\n"),
1578 program_name, option);
1580 exit (EXIT_FAILURE);
1584 fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
1585 program_name, option, min);
1589 exit (EXIT_FAILURE);
1596 else if (max >= 0 && val > max)
1598 fprintf (stderr, _("%s: value for -%c option should be < %ld\n"),
1599 program_name, option, max);
1603 exit (EXIT_FAILURE);
1614 usage (FILE *stream)
1617 _("Usage: %s [OPTION]... COMMAND [INITIAL-ARGS]...\n"),
1620 #define HTL(t) fputs (t, stream);
1622 HTL (_("Run COMMAND with arguments INITIAL-ARGS and more arguments read from input.\n"
1624 HTL (_("Mandatory and optional arguments to long options are also\n"
1625 "mandatory or optional for the corresponding short option.\n"));
1626 HTL (_(" -0, --null items are separated by a null, not whitespace;\n"
1627 " disables quote and backslash processing and\n"
1628 " logical EOF processing\n"));
1629 HTL (_(" -a, --arg-file=FILE read arguments from FILE, not standard input\n"));
1630 HTL (_(" -d, --delimiter=CHARACTER items in input stream are separated by CHARACTER,\n"
1631 " not by whitespace; disables quote and backslash\n"
1632 " processing and logical EOF processing\n"));
1633 HTL (_(" -E END set logical EOF string; if END occurs as a line\n"
1634 " of input, the rest of the input is ignored\n"
1635 " (ignored if -0 or -d was specified)\n"));
1636 HTL (_(" -e, --eof[=END] equivalent to -E END if END is specified;\n"
1637 " otherwise, there is no end-of-file string\n"));
1638 HTL (_(" -I R same as --replace=R\n"));
1639 HTL (_(" -i, --replace[=R] replace R in INITIAL-ARGS with names read\n"
1640 " from standard input; if R is unspecified,\n"
1642 HTL (_(" -L, --max-lines=MAX-LINES use at most MAX-LINES non-blank input lines per\n"
1643 " command line\n"));
1644 HTL (_(" -l[MAX-LINES] similar to -L but defaults to at most one non-\n"
1645 " blank input line if MAX-LINES is not specified\n"));
1646 HTL (_(" -n, --max-args=MAX-ARGS use at most MAX-ARGS arguments per command line\n"));
1647 HTL (_(" -P, --max-procs=MAX-PROCS run at most MAX-PROCS processes at a time\n"));
1648 HTL (_(" -p, --interactive prompt before running commands\n"));
1649 HTL (_(" --process-slot-var=VAR set environment variable VAR in child processes\n"));
1650 HTL (_(" -r, --no-run-if-empty if there are no arguments, then do not run COMMAND;\n"
1651 " if this option is not given, COMMAND will be\n"
1652 " run at least once\n"));
1653 HTL (_(" -s, --max-chars=MAX-CHARS limit length of command line to MAX-CHARS\n"));
1654 HTL (_(" --show-limits show limits on command-line length\n"));
1655 HTL (_(" -t, --verbose print commands before executing them\n"));
1656 HTL (_(" -x, --exit exit if the size (see -s) is exceeded\n"));
1658 HTL (_(" --help display this help and exit\n"));
1659 HTL (_(" --version output version information and exit\n"));
1661 "Report bugs to <bug-findutils@gnu.org>.\n"));