1
1
/* xargs -- build and execute command lines from standard input
2
Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
2
Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005, 2006,
3
2007, 2008 Free Software Foundation, Inc.
4
5
This program is free software: you can redistribute it and/or modify
5
6
it under the terms of the GNU General Public License as published by
205
207
/* If nonzero, the maximum number of child processes that can be running
207
static int proc_max = 1;
209
static unsigned long int proc_max = 1uL;
209
/* Total number of child processes that have been executed. */
210
static int procs_executed = 0;
211
/* Did we fork a child yet? */
212
static boolean procs_executed = false;
212
214
/* The number of elements in `pids'. */
213
static int procs_executing = 0;
215
static unsigned long int procs_executing = 0uL;
215
217
/* List of child processes currently executing. */
216
218
static pid_t *pids = NULL;
218
220
/* The number of allocated elements in `pids'. */
219
static int pids_alloc = 0;
221
static size_t pids_alloc = 0u;
221
223
/* Exit status; nonzero if any child process exited with a
222
224
status of 1-125. */
265
267
static int xargs_do_exec (const struct buildcmd_control *cl, struct buildcmd_state *state);
266
268
static void exec_if_possible PARAMS ((void));
267
269
static void add_proc PARAMS ((pid_t pid));
268
static void wait_for_proc PARAMS ((boolean all));
270
static void wait_for_proc PARAMS ((boolean all, unsigned int minreap));
269
271
static void wait_for_proc_all PARAMS ((void));
270
272
static long parse_num PARAMS ((char *str, int option, long min, long max, int fatal));
271
273
static void usage PARAMS ((FILE * stream));
418
423
atexit (close_stdin);
419
424
atexit (wait_for_proc_all);
421
bcstatus = bc_init_controlinfo(&bc_ctl);
426
/* xargs is required by POSIX to allow 2048 bytes of headroom
427
* for extra environment variables (that perhaps the utliity might
428
* want to set before execing something else).
430
bcstatus = bc_init_controlinfo(&bc_ctl, XARGS_POSIX_HEADROOM);
422
432
/* The bc_init_controlinfo call may have determined that the
423
433
* environment is too big. In that case, we will fail with
424
434
* an error message after processing the command-line options,
434
444
act_on_init_result = fail_due_to_env_size;
446
else if (BC_INIT_CANNOT_ACCOMODATE_HEADROOM == bcstatus)
448
/* All POSIX systems are required to support ARG_MAX of at least
449
* 4096. For everything to work the total of (command line +
450
* headroom + environment) must fit into this. POSIX requires
451
* that we use a headroom of 2048 bytes. The user is in control
452
* of the size of the environment.
454
* In general if bc_init_controlinfo() returns
455
* BC_INIT_CANNOT_ACCOMODATE_HEADROOM, its caller can try again
456
* with a smaller headroom. However, in the case of xargs, this
457
* would not be POSIX-compliant.
459
act_on_init_result = fail_due_to_env_size;
438
463
/* IEEE Std 1003.1, 2003 specifies that the combined argument and
439
464
* environment list shall not exceed {ARG_MAX}-2048 bytes. It also
440
465
* specifies that it shall be at least LINE_MAX.
443
assert(bc_ctl.arg_max <= (ARG_MAX-2048));
468
long val = sysconf(_SC_ARG_MAX);
471
/* Note that val can in fact be greater than ARG_MAX
472
* and bc_ctl.arg_max can also be greater than ARG_MAX.
474
assert (bc_ctl.arg_max <= (val-XARGS_POSIX_HEADROOM));
479
assert (bc_ctl.arg_max <= (ARG_MAX-XARGS_POSIX_HEADROOM));
484
assert (bc_ctl.arg_max <= (ARG_MAX-XARGS_POSIX_HEADROOM));
446
assert(bc_ctl.arg_max >= LINE_MAX);
489
/* This assertion ensures that this xargs implementation
490
* conforms to the POSIX requirement that the default command
491
* line length shall be at least LINE_MAX.
493
assert (bc_ctl.arg_max >= LINE_MAX);
449
496
bc_ctl.exec_callback = xargs_do_exec;
633
680
/* Without SIZE_MAX (i.e. limits.h) this is probably
634
681
* close to the best we can do.
636
assert(sizeof(size_t) <= sizeof(unsigned long));
683
verify_true (sizeof(size_t) <= sizeof(unsigned long));
642
689
_("Your environment variables take up %lu bytes\n"),
643
690
(unsigned long)bc_size_of_environment());
645
_("POSIX lower and upper limits on argument length: %lu, %lu\n"),
646
(unsigned long)bc_ctl.posix_arg_size_min,
692
_("POSIX upper limit on argument length (this system): %lu\n"),
647
693
(unsigned long)bc_ctl.posix_arg_size_max);
695
_("POSIX smallest allowable upper limit on argument length (all systems): %lu\n"),
696
(unsigned long)bc_ctl.posix_arg_size_min);
649
698
_("Maximum length of command we could actually use: %ld\n"),
650
699
(unsigned long)(bc_ctl.posix_arg_size_max -
651
700
bc_size_of_environment()));
653
702
_("Size of command buffer we are actually using: %lu\n"),
654
703
(unsigned long)bc_ctl.arg_max);
656
705
if (isatty(STDIN_FILENO))
660
"Execution of xargs will continue now, and it will "
661
"try to read its input and run commands; if this is "
662
"not what you wanted to happen, please type the "
663
"end-of-file keystroke.\n");
709
"Execution of xargs will continue now, and it will "
710
"try to read its input and run commands; if this is "
711
"not what you wanted to happen, please type the "
712
"end-of-file keystroke.\n"));
713
if (always_run_command)
716
_("Warning: %s will be run at least once. "
717
"If you do not want that to happen, then press "
718
"the interrupt keystroke.\n"),
667
linebuf = (char *) xmalloc (bc_ctl.arg_max + 1);
668
bc_state.argbuf = (char *) xmalloc (bc_ctl.arg_max + 1);
724
linebuf = xmalloc (bc_ctl.arg_max + 1);
725
bc_state.argbuf = xmalloc (bc_ctl.arg_max + 1);
670
727
/* Make sure to listen for the kids. */
671
728
signal (SIGCHLD, SIG_DFL);
802
/* States for read_line. */
745
810
static boolean eof = false;
746
811
/* Start out in mode SPACE to always strip leading spaces (even with -i). */
747
int state = SPACE; /* The type of character we last read. */
812
enum read_line_state state = SPACE; /* The type of character we last read. */
748
813
int prevc; /* The previous value of c. */
749
814
int quotc = 0; /* The last quote character read. */
751
816
boolean first = true; /* true if reading first arg on line. */
817
boolean seen_arg = false; /* true if we have seen any arg (or part of one) yet */
753
819
char *p = linebuf;
754
820
/* Including the NUL, the args must not grow past this point. */
755
821
char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
762
828
c = getc (input_stream);
765
832
/* COMPAT: SYSV seems to ignore stuff on a line that
1013
1107
if (!query_before_executing || print_args (true))
1015
if (proc_max && procs_executing >= proc_max)
1016
wait_for_proc (false);
1111
if (procs_executing >= proc_max)
1112
wait_for_proc (false, proc_max - procs_executing + 1);
1113
assert (procs_executing < proc_max);
1017
1115
if (!query_before_executing && print_command)
1018
1116
print_args (false);
1118
/* Before forking, reap any already-exited child. We do this so
1119
that we don't leave unreaped children around while we build a
1120
new command line. For example this command will spend most
1121
of its time waiting for sufficient arguments to launch
1122
another command line:
1124
seq 1 1000 | fmt | while read x ; do echo $x; sleep 1 ; done |
1125
./xargs -P 200 -n 20 sh -c 'echo "$@"; sleep $((1 + $RANDOM % 5))' sleeper
1127
wait_for_proc (false, 0u);
1019
1129
/* If we run out of processes, wait for a child to return and
1021
1131
while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
1022
wait_for_proc (false);
1132
wait_for_proc (false, 1u);
1057
1168
add_proc (pid_t pid)
1061
1172
/* Find an empty slot. */
1062
1173
for (i = 0; i < pids_alloc && pids[i]; i++)
1176
/* Extend the array if we failed. */
1064
1177
if (i == pids_alloc)
1066
if (pids_alloc == 0)
1068
pids_alloc = proc_max ? proc_max : 64;
1069
pids = (pid_t *) xmalloc (sizeof (pid_t) * pids_alloc);
1074
pids = (pid_t *) xrealloc (pids,
1075
sizeof (pid_t) * pids_alloc);
1077
memset (&pids[i], '\0', sizeof (pid_t) * (pids_alloc - i));
1179
pids = x2nrealloc (pids, &pids_alloc, sizeof *pids);
1181
/* Zero out the new slots. */
1182
for (j=i; j<pids_alloc; ++j)
1185
/* Verify that we are not destroying the record of some existing child. */
1186
assert (0 == pids[i]);
1188
/* Remember the child. */
1080
1190
procs_executing++;
1191
procs_executed = true;
1084
1195
/* If ALL is true, wait for all child processes to finish;
1085
otherwise, wait for one child process to finish.
1196
otherwise, wait for at least MINREAP child processes to finish.
1086
1197
Remove the processes that finish from the list of executing processes. */
1089
wait_for_proc (boolean all)
1200
wait_for_proc (boolean all, unsigned int minreap)
1202
unsigned int reaped = 0;
1091
1204
while (procs_executing)
1213
if (reaped >= minreap)
1215
/* We already reaped enough children. To save system
1216
* resources, reap any dead children anyway, but do not
1217
* wait for any currently executing children to exit.
1099
while ((pid = wait (&status)) == (pid_t) -1)
1101
error (1, errno, _("error waiting for child process"));
1226
/* Wait for any child. We used to use wait() here, but it's
1227
* unlikely that that offers any portability advantage over
1230
while ((pid = waitpid (-1, &status, wflags)) == (pid_t) -1)
1233
error (1, errno, _("error waiting for child process"));
1103
1236
/* Find the entry in `pids' for the child process
1104
1237
that exited. */
1105
for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1240
for (i = 0; i < pids_alloc && pid != pids[i]; i++)
1108
while (i == pids_alloc); /* A child died that we didn't start? */
1244
while (pid && i == pids_alloc); /* A child died that we didn't start? */
1248
if (!(wflags & WNOHANG))
1250
/* Nothing remained to be reaped. This should not
1251
* happen, because procs_executing should contain the
1252
* number of child processes still executing, so the
1253
* loop should have terminated.
1255
error (0, 0, _("Warning: Lost track of %d child processes"),
1260
/* Children are (probably) executing but are not ready
1261
* to be reaped at the moment.
1110
1267
/* Remove the child from the list. */
1112
1269
procs_executing--;
1114
1272
if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
1115
1273
exit (WEXITSTATUS (status)); /* Can't find or run the command. */