cvsimport
[findutils.git] / lib / buildcmd.c
blob11ae00f300465130e12062394001c96ba510dce6
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 static size_t
356 get_line_max(void)
358 long val;
359 #ifdef _SC_LINE_MAX
360 val = sysconf(_SC_LINE_MAX);
361 #else
362 val = -1;
363 #endif
365 if (val > 0)
366 return val;
368 /* either _SC_LINE_MAX was not available or
369 * there is no particular limit.
371 #ifdef LINE_MAX
372 val = LINE_MAX;
373 #endif
375 if (val > 0)
376 return val;
378 return 2048L; /* a reasonable guess. */
382 size_t
383 bc_get_arg_max(void)
385 long val;
387 /* We may resort to using LONG_MAX, so check it fits. */
388 /* XXX: better to do a compile-time check */
389 assert( (~(size_t)0) >= LONG_MAX);
391 #ifdef _SC_ARG_MAX
392 val = sysconf(_SC_ARG_MAX);
393 #else
394 val = -1;
395 #endif
397 if (val > 0)
398 return val;
400 /* either _SC_ARG_MAX was not available or
401 * there is no particular limit.
403 #ifdef ARG_MAX
404 val = ARG_MAX;
405 #endif
407 if (val > 0)
408 return val;
410 /* The value returned by this function bounds the
411 * value applied as the ceiling for the -s option.
412 * Hence it the system won't tell us what its limit
413 * is, we allow the user to specify more or less
414 * whatever value they like.
416 return LONG_MAX;
420 static int cb_exec_noop(const struct buildcmd_control *ctl,
421 struct buildcmd_state *state)
423 /* does nothing. */
424 (void) ctl;
425 (void) state;
427 return 0;
431 /* Return how much of ARG_MAX is used by the environment. */
432 size_t
433 bc_size_of_environment (void)
435 size_t len = 0u;
436 char **envp = environ;
438 while (*envp)
439 len += strlen (*envp++) + 1;
441 return len;
445 enum BC_INIT_STATUS
446 bc_init_controlinfo(struct buildcmd_control *ctl,
447 size_t headroom)
449 size_t size_of_environment = bc_size_of_environment();
450 int val;
452 /* POSIX requires that _POSIX_ARG_MAX is 4096. That is the lowest
453 * possible value for ARG_MAX on a POSIX compliant system. See
454 * http://www.opengroup.org/onlinepubs/009695399/basedefs/limits.h.html
456 ctl->posix_arg_size_min = _POSIX_ARG_MAX;
457 ctl->posix_arg_size_max = bc_get_arg_max();
459 ctl->exit_if_size_exceeded = 0;
461 /* Take the size of the environment into account. */
462 if (size_of_environment > ctl->posix_arg_size_max)
464 return BC_INIT_ENV_TOO_BIG;
466 else if ((headroom + size_of_environment) >= ctl->posix_arg_size_max)
468 /* POSIX.2 requires xargs to subtract 2048, but ARG_MAX is
469 * guaranteed to be at least 4096. Although xargs could use an
470 * assertion here, we use a runtime check which returns an error
471 * code, because our caller may not be xargs.
473 return BC_INIT_CANNOT_ACCOMODATE_HEADROOM;
475 else
477 ctl->posix_arg_size_max -= size_of_environment;
478 ctl->posix_arg_size_max -= headroom;
481 /* need to subtract 2 on the following line - for Linux/PPC */
482 ctl->max_arg_count = (ctl->posix_arg_size_max / sizeof(char*)) - 2u;
483 assert(ctl->max_arg_count > 0);
484 ctl->rplen = 0u;
485 ctl->replace_pat = NULL;
486 ctl->initial_argc = 0;
487 ctl->exec_callback = cb_exec_noop;
488 ctl->lines_per_exec = 0;
489 ctl->args_per_exec = 0;
491 /* Set the initial value of arg_max to the largest value we can
492 * tolerate.
494 ctl->arg_max = ctl->posix_arg_size_max;
496 return BC_INIT_OK;
499 void
500 bc_use_sensible_arg_max(struct buildcmd_control *ctl)
502 enum { arg_size = (128u * 1024u) };
504 /* Check against the upper and lower limits. */
505 if (arg_size > ctl->posix_arg_size_max)
506 ctl->arg_max = ctl->posix_arg_size_max;
507 else if (arg_size < ctl->posix_arg_size_min)
508 ctl->arg_max = ctl->posix_arg_size_min;
509 else
510 ctl->arg_max = arg_size;
516 void
517 bc_init_state(const struct buildcmd_control *ctl,
518 struct buildcmd_state *state,
519 void *context)
521 state->cmd_argc = 0;
522 state->cmd_argv_chars = 0;
523 state->cmd_argv = NULL;
524 state->cmd_argv_alloc = 0;
526 /* XXX: the following memory allocation is inadvisable on systems
527 * with no ARG_MAX, because ctl->arg_max may actually be close to
528 * LONG_MAX. Adding one to it is safe though because earlier we
529 * subtracted 2048.
531 assert(ctl->arg_max <= (LONG_MAX - 2048L));
532 state->argbuf = (char *) xmalloc (ctl->arg_max + 1u);
534 state->cmd_argv_chars = state->cmd_initial_argv_chars = 0;
535 state->todo = 0;
536 state->dirfd = -1;
537 state->usercontext = context;
540 void
541 bc_clear_args(const struct buildcmd_control *ctl,
542 struct buildcmd_state *state)
544 state->cmd_argc = ctl->initial_argc;
545 state->cmd_argv_chars = state->cmd_initial_argv_chars;
546 state->todo = 0;
547 state->dirfd = -1;