Updated copyright years.
[findutils.git] / lib / buildcmd.c
blobdd38e1d48fc0bd542f7a3e6413f40889697bb004
1 /* buildcmd.c -- build command lines from a list of arguments.
2 Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
17 USA.
21 XXX_SOC:
23 One of the aspects of the SOC project is to adapt this module.
24 This module currently makes an initial guess at two things:
26 buildcmd_control->arg_max (The most characters we can fit in)
27 buildcmd_control->max_arg_count (most args)
29 The nature of the SOC task is to adjust these values when exec fails.
30 Optionally (if we have the time) we can make the software adjust them
31 when exec succeeds. If we do the latter, we need to ensure we don't
32 get into some state where we are sitting just below the limit and
33 keep trying to extend, because that would lead to every other exec
34 failing.
36 If our initial guess is successful, there is no pressing need really to
37 increase our guess. Indeed, if we are beign called by xargs (as opposed
38 to find) th user may have specified a limit with "-s" and we should not
39 exceed it.
43 #include <config.h>
45 # ifndef PARAMS
46 # if defined PROTOTYPES || (defined __STDC__ && __STDC__)
47 # define PARAMS(Args) Args
48 # else
49 # define PARAMS(Args) ()
50 # endif
51 # endif
53 #include <string.h>
56 #if DO_MULTIBYTE
57 # if HAVE_MBRLEN
58 # include <wchar.h>
59 # else
60 /* Simulate mbrlen with mblen as best we can. */
61 # define mbstate_t int
62 # define mbrlen(s, n, ps) mblen (s, n)
63 # endif
64 #endif
66 #ifdef HAVE_LOCALE_H
67 #include <locale.h>
68 #endif
69 #if ENABLE_NLS
70 # include <libintl.h>
71 # define _(Text) gettext (Text)
72 #else
73 # define _(Text) Text
74 #define textdomain(Domain)
75 #define bindtextdomain(Package, Directory)
76 #endif
77 #ifdef gettext_noop
78 # define N_(String) gettext_noop (String)
79 #else
80 /* See locate.c for explanation as to why not use (String) */
81 # define N_(String) String
82 #endif
84 #ifndef _POSIX_SOURCE
85 #include <sys/param.h>
86 #endif
88 #ifdef HAVE_LIMITS_H
89 #include <limits.h>
90 #endif
92 /* The presence of unistd.h is assumed by gnulib these days, so we
93 * might as well assume it too.
95 /* for sysconf() */
96 #include <unistd.h>
98 #include <assert.h>
100 /* COMPAT: SYSV version defaults size (and has a max value of) to 470.
101 We try to make it as large as possible. See bc_get_arg_max() below. */
102 #if !defined(ARG_MAX) && defined(NCARGS)
103 #error "You have an unusual system. Once you remove this error message from buildcmd.c, it should work, but please make sure that DejaGnu is installed on your system and that 'make check' passes before using the findutils programs"
104 #define ARG_MAX NCARGS
105 #endif
109 #include <xalloc.h>
110 #include <error.h>
111 #include <openat.h>
113 #include "buildcmd.h"
116 extern char **environ;
119 /* Replace all instances of `replace_pat' in ARG with `linebuf',
120 and add the resulting string to the list of arguments for the command
121 to execute.
122 ARGLEN is the length of ARG, not including the null.
123 LBLEN is the length of LINEBUF, not including the null.
124 PFXLEN is the length of PREFIX. Substitution is not performed on
125 the prefix. The prefix is used if the argument contains replace_pat.
127 COMPAT: insertions on the SYSV version are limited to 255 chars per line,
128 and a max of 5 occurrences of replace_pat in the initial-arguments.
129 Those restrictions do not exist here. */
131 void
132 bc_do_insert (const struct buildcmd_control *ctl,
133 struct buildcmd_state *state,
134 char *arg, size_t arglen,
135 const char *prefix, size_t pfxlen,
136 const char *linebuf, size_t lblen,
137 int initial_args)
139 /* Temporary copy of each arg with the replace pattern replaced by the
140 real arg. */
141 static char *insertbuf;
142 char *p;
143 size_t bytes_left = ctl->arg_max - 1; /* Bytes left on the command line. */
145 /* XXX: on systems lacking an upper limit for exec args, ctl->arg_max
146 * may have been set to LONG_MAX (see bc_get_arg_max()). Hence
147 * this xmalloc call may be a bad idea, especially since we are
148 * adding 1 to it...
150 if (!insertbuf)
151 insertbuf = (char *) xmalloc (ctl->arg_max + 1);
152 p = insertbuf;
156 size_t len; /* Length in ARG before `replace_pat'. */
157 char *s = mbsstr (arg, ctl->replace_pat);
158 if (s)
160 len = s - arg;
162 else
164 len = arglen;
167 if (bytes_left <= len)
168 break;
169 else
170 bytes_left -= len;
172 strncpy (p, arg, len);
173 p += len;
174 arg += len;
175 arglen -= len;
177 if (s)
179 if (bytes_left <= (lblen + pfxlen))
180 break;
181 else
182 bytes_left -= (lblen + pfxlen);
184 if (prefix)
186 strcpy (p, prefix);
187 p += pfxlen;
189 strcpy (p, linebuf);
190 p += lblen;
192 arg += ctl->rplen;
193 arglen -= ctl->rplen;
196 while (*arg);
197 if (*arg)
198 error (1, 0, _("command too long"));
199 *p++ = '\0';
201 bc_push_arg (ctl, state,
202 insertbuf, p - insertbuf,
203 NULL, 0,
204 initial_args);
207 static
208 void do_exec(const struct buildcmd_control *ctl,
209 struct buildcmd_state *state)
211 /* XXX_SOC:
213 Here we are calling the user's function. Currently there is no
214 way for it to report that the argument list was too long. We
215 should introduce an externally callable function that allows them
216 to report this.
218 If the callee does report that the exec failed, we need to retry
219 the exec with a shorter argument list. Once we have reduced the
220 argument list to the point where the exec can succeed, we need to
221 preserve the list of arguments we couldn't exec this time.
223 This also means that the control argument here probably needs not
224 to be const (since the limits are in the control arg).
226 The caller's only requirement on do_exec is that it should
227 free up enough room for at least one argument.
229 (ctl->exec_callback)(ctl, state);
233 /* Return nonzero if there would not be enough room for an additional
234 * argument. We check the total number of arguments only, not the space
235 * occupied by those arguments.
237 * If we return zero, there still may not be enough room for the next
238 * argument, depending on its length.
240 static int
241 bc_argc_limit_reached(int initial_args,
242 const struct buildcmd_control *ctl,
243 struct buildcmd_state *state)
245 /* Check to see if we about to exceed a limit set by xargs' -n option */
246 if (!initial_args && ctl->args_per_exec &&
247 ( (state->cmd_argc - ctl->initial_argc) == ctl->args_per_exec))
248 return 1;
250 /* We deliberately use an equality test here rather than >= in order
251 * to force a software failure if the code is modified in such a way
252 * that it fails to call this function for every new argument.
254 return state->cmd_argc == ctl->max_arg_count;
258 /* Add ARG to the end of the list of arguments `cmd_argv' to pass
259 to the command.
260 LEN is the length of ARG, including the terminating null.
261 If this brings the list up to its maximum size, execute the command.
263 /* XXX: sometimes this function is called (internally)
264 * just to push a NULL onto the and of the arg list.
265 * We should probably do that with a separate function
266 * for greater clarity.
268 void
269 bc_push_arg (const struct buildcmd_control *ctl,
270 struct buildcmd_state *state,
271 const char *arg, size_t len,
272 const char *prefix, size_t pfxlen,
273 int initial_args)
275 if (!initial_args)
277 state->todo = 1;
280 if (arg)
282 /* XXX_SOC: if do_exec() is only guaranteeed to free up one
283 * argument, this if statement may need to become a while loop.
284 * If it becomes a while loop, it needs not to be an infinite
285 * loop...
287 if (state->cmd_argv_chars + len > ctl->arg_max)
289 if (initial_args || state->cmd_argc == ctl->initial_argc)
290 error (1, 0, _("can not fit single argument within argument list size limit"));
291 /* xargs option -i (replace_pat) implies -x (exit_if_size_exceeded) */
292 if (ctl->replace_pat
293 || (ctl->exit_if_size_exceeded &&
294 (ctl->lines_per_exec || ctl->args_per_exec)))
295 error (1, 0, _("argument list too long"));
296 do_exec (ctl, state);
298 /* XXX_SOC: this if may also need to become a while loop. In
299 fact perhaps it is best to factor this out into a separate
300 function which ceeps calling the exec handler until there is
301 space for our next argument. Each exec will free one argc
302 "slot" so the main thing to worry about repeated exec calls
303 for would be total argument length.
305 if (bc_argc_limit_reached(initial_args, ctl, state))
306 do_exec (ctl, state);
309 if (state->cmd_argc >= state->cmd_argv_alloc)
311 /* XXX: we could use extendbuf() here. */
312 if (!state->cmd_argv)
314 state->cmd_argv_alloc = 64;
315 state->cmd_argv = (char **) xmalloc (sizeof (char *) * state->cmd_argv_alloc);
317 else
319 state->cmd_argv_alloc *= 2;
320 state->cmd_argv = (char **) xrealloc (state->cmd_argv,
321 sizeof (char *) * state->cmd_argv_alloc);
325 if (!arg)
326 state->cmd_argv[state->cmd_argc++] = NULL;
327 else
329 state->cmd_argv[state->cmd_argc++] = state->argbuf + state->cmd_argv_chars;
330 if (prefix)
332 strcpy (state->argbuf + state->cmd_argv_chars, prefix);
333 state->cmd_argv_chars += pfxlen;
336 strcpy (state->argbuf + state->cmd_argv_chars, arg);
337 state->cmd_argv_chars += len;
339 /* If we have now collected enough arguments,
340 * do the exec immediately. This must be
341 * conditional on arg!=NULL, since do_exec()
342 * actually calls bc_push_arg(ctl, state, NULL, 0, false).
344 if (bc_argc_limit_reached(initial_args, ctl, state))
345 do_exec (ctl, state);
348 /* If this is an initial argument, set the high-water mark. */
349 if (initial_args)
351 state->cmd_initial_argv_chars = state->cmd_argv_chars;
355 #if 0
356 /* We used to set posix_arg_size_min to the LINE_MAX limit, but
357 * currently we use _POSIX_ARG_MAX (which is the minimum value).
359 static size_t
360 get_line_max(void)
362 long val;
363 #ifdef _SC_LINE_MAX
364 val = sysconf(_SC_LINE_MAX);
365 #else
366 val = -1;
367 #endif
369 if (val > 0)
370 return val;
372 /* either _SC_LINE_MAX was not available or
373 * there is no particular limit.
375 #ifdef LINE_MAX
376 val = LINE_MAX;
377 #endif
379 if (val > 0)
380 return val;
382 return 2048L; /* a reasonable guess. */
384 #endif
386 size_t
387 bc_get_arg_max(void)
389 long val;
391 /* We may resort to using LONG_MAX, so check it fits. */
392 /* XXX: better to do a compile-time check */
393 assert( (~(size_t)0) >= LONG_MAX);
395 #ifdef _SC_ARG_MAX
396 val = sysconf(_SC_ARG_MAX);
397 #else
398 val = -1;
399 #endif
401 if (val > 0)
402 return val;
404 /* either _SC_ARG_MAX was not available or
405 * there is no particular limit.
407 #ifdef ARG_MAX
408 val = ARG_MAX;
409 #endif
411 if (val > 0)
412 return val;
414 /* The value returned by this function bounds the
415 * value applied as the ceiling for the -s option.
416 * Hence it the system won't tell us what its limit
417 * is, we allow the user to specify more or less
418 * whatever value they like.
420 return LONG_MAX;
424 static int cb_exec_noop(const struct buildcmd_control *ctl,
425 struct buildcmd_state *state)
427 /* does nothing. */
428 (void) ctl;
429 (void) state;
431 return 0;
435 /* Return how much of ARG_MAX is used by the environment. */
436 size_t
437 bc_size_of_environment (void)
439 size_t len = 0u;
440 char **envp = environ;
442 while (*envp)
443 len += strlen (*envp++) + 1;
445 return len;
449 enum BC_INIT_STATUS
450 bc_init_controlinfo(struct buildcmd_control *ctl,
451 size_t headroom)
453 size_t size_of_environment = bc_size_of_environment();
455 /* POSIX requires that _POSIX_ARG_MAX is 4096. That is the lowest
456 * possible value for ARG_MAX on a POSIX compliant system. See
457 * http://www.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
459 ctl->posix_arg_size_min = _POSIX_ARG_MAX;
460 ctl->posix_arg_size_max = bc_get_arg_max();
462 ctl->exit_if_size_exceeded = 0;
464 /* Take the size of the environment into account. */
465 if (size_of_environment > ctl->posix_arg_size_max)
467 return BC_INIT_ENV_TOO_BIG;
469 else if ((headroom + size_of_environment) >= ctl->posix_arg_size_max)
471 /* POSIX.2 requires xargs to subtract 2048, but ARG_MAX is
472 * guaranteed to be at least 4096. Although xargs could use an
473 * assertion here, we use a runtime check which returns an error
474 * code, because our caller may not be xargs.
476 return BC_INIT_CANNOT_ACCOMODATE_HEADROOM;
478 else
480 ctl->posix_arg_size_max -= size_of_environment;
481 ctl->posix_arg_size_max -= headroom;
484 /* need to subtract 2 on the following line - for Linux/PPC */
485 ctl->max_arg_count = (ctl->posix_arg_size_max / sizeof(char*)) - 2u;
486 assert(ctl->max_arg_count > 0);
487 ctl->rplen = 0u;
488 ctl->replace_pat = NULL;
489 ctl->initial_argc = 0;
490 ctl->exec_callback = cb_exec_noop;
491 ctl->lines_per_exec = 0;
492 ctl->args_per_exec = 0;
494 /* Set the initial value of arg_max to the largest value we can
495 * tolerate.
497 ctl->arg_max = ctl->posix_arg_size_max;
499 return BC_INIT_OK;
502 void
503 bc_use_sensible_arg_max(struct buildcmd_control *ctl)
505 enum { arg_size = (128u * 1024u) };
507 /* Check against the upper and lower limits. */
508 if (arg_size > ctl->posix_arg_size_max)
509 ctl->arg_max = ctl->posix_arg_size_max;
510 else if (arg_size < ctl->posix_arg_size_min)
511 ctl->arg_max = ctl->posix_arg_size_min;
512 else
513 ctl->arg_max = arg_size;
519 void
520 bc_init_state(const struct buildcmd_control *ctl,
521 struct buildcmd_state *state,
522 void *context)
524 state->cmd_argc = 0;
525 state->cmd_argv_chars = 0;
526 state->cmd_argv = NULL;
527 state->cmd_argv_alloc = 0;
529 /* XXX: the following memory allocation is inadvisable on systems
530 * with no ARG_MAX, because ctl->arg_max may actually be close to
531 * LONG_MAX. Adding one to it is safe though because earlier we
532 * subtracted 2048.
534 assert(ctl->arg_max <= (LONG_MAX - 2048L));
535 state->argbuf = (char *) xmalloc (ctl->arg_max + 1u);
537 state->cmd_argv_chars = state->cmd_initial_argv_chars = 0;
538 state->todo = 0;
539 state->dirfd = -1;
540 state->usercontext = context;
543 void
544 bc_clear_args(const struct buildcmd_control *ctl,
545 struct buildcmd_state *state)
547 state->cmd_argc = ctl->initial_argc;
548 state->cmd_argv_chars = state->cmd_initial_argv_chars;
549 state->todo = 0;
550 state->dirfd = -1;