Files
findutils/lib/buildcmd.h
James Youngman 265a49c66d Fix Savannah bug#27328, segfault if the initial exec for "find -exec" fails, and Savannah bug #27975: Infinite loop for -exec [..] {} +.
2009-11-29  James Youngman  <jay@gnu.org>

	Fix Savannah bug#27328, segfault if the initial exec for "find
	-exec" fails.  Also bug #27975: Infinite loop for -exec [..] {} +.
	* lib/buildcmd.h (struct buildcmd_state): Introduce
	largest_successful_arg_count and smallest_failed_arg_count in
	order to avoid an assumption that we understand how the operatingn
	system interprets ARG_MAX and the assumption that the compile-time
	ARG_MAX value (or the equivalent from sysconf()) is avtually
	correct.
	(struct buildcmd_control): Change the calling convention of the
	exec callback to allow us to pass the argument list separately.
	(bc_args_exceed_testing_limit): declare this new funciton.
	* lib/buildcmd.c (bc_args_complete): New function, NULL-terminates
 	the argv list.   We use this instead of passing NULL to
	bc_push_arg().
	(update_limit): New function.  Decides how many arguments to
	pass to the invoked command on the next attempt.
	(copy_args): Build an argument list containing all the initial
	arguments plus some of the other args (the number to be used is
	decided by update_limit).
	(bc_do_exec): Avoid special-casing the first call to exec.  Use
	update_limit to decide how many arguments to pass and copy_args to
	build the argument list.  The new form of the loop should fix
	Savannah bug #27328.
	(bc_push_arg): Drop support for passing NULL as an argument (to
	terminate the arg list we just pass in a special argument instead).
	(bc_args_exceed_testing_limit): New function, returns nonzero if
	the argument list exceeds a testing limit (used for failure
	injection by tests).
	(exceeds): New support function, implementing part of
	bc_args_exceed_testing_limit.
	(bc_init_state): Initialise largest_successful_arg_count and
	smallest_failed_arg_count.
	* find/defs.h (struct exec_val): New member last_child_status.
	This stores the status of the most recently completed child.
	* find/pred.c (new_impl_pred_exec): Use last_child_status.
	(launch): Use the new calling convention for the exec callback.
	Set last_child_status.  Call bc_args_exceed_testing_limit() to do
	failure injection for unit tests.
	* find/util.c (do_complete_pending_execdirs): Call bc_do_exec
	rather than calling launch directly in order to allow for breaking
	the argument list up if it's too long.
	* xargs/xargs.c (xargs_do_exec): Update to use the new caling
	convention for the exec callback.

Signed-off-by: James Youngman <jay@gnu.org>
2009-11-29 16:52:01 +00:00

143 lines
4.2 KiB
C

/* buildcmd.[ch] -- build command lines from a stream of arguments
Copyright (C) 2005, 2008 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Written by James Youngman.
*/
#ifndef INC_BUILDCMD_H
#define INC_BUILDCMD_H 1
struct buildcmd_state
{
/* Number of valid elements in `cmd_argv', including terminating NULL. */
int cmd_argc; /* 0 */
/* The list of args being built. */
char **cmd_argv; /* NULL */
/* Number of elements allocated for `cmd_argv'. */
int cmd_argv_alloc;
/* Storage for elements of `cmd_argv'. */
char *argbuf;
/* Number of chars being used in `cmd_argv'. */
size_t cmd_argv_chars;
/* Number of chars being used in `cmd_argv' for the initial args.. */
size_t cmd_initial_argv_chars;
/* User context information. */
void *usercontext;
/* to-do flag. */
int todo;
/* Directory in which to perform the exec. */
int dir_fd;
/* Summary of what we think the argv limits are. */
int largest_successful_arg_count;
int smallest_failed_arg_count;
};
struct buildcmd_control
{
/* If true, exit if lines_per_exec or args_per_exec is exceeded. */
int exit_if_size_exceeded; /* false */
/* POSIX limits on the argument length. */
size_t posix_arg_size_max;
size_t posix_arg_size_min;
/* The maximum number of characters that can be used per command line. */
size_t arg_max;
/* max_arg_count: the maximum number of arguments that can be used.
*
* Many systems include the size of the pointers in ARG_MAX.
* Linux on PPC fails if we just subtract 1 here.
*
* However, not all systems define ARG_MAX. Our bc_get_arg_max()
* function returns a useful value even if ARG_MAX is not defined.
* However, sometimes, max_arg_count is LONG_MAX!
*/
long max_arg_count;
/* The length of `replace_pat'. */
size_t rplen;
/* If nonzero, then instead of putting the args from stdin at
the end of the command argument list, they are each stuck into the
initial args, replacing each occurrence of the `replace_pat' in the
initial args. */
char *replace_pat;
/* Number of initial arguments given on the command line. */
int initial_argc; /* 0 */
/* exec callback. */
int (*exec_callback)(struct buildcmd_control *, void *usercontext, int argc, char **argv);
/* If nonzero, the maximum number of nonblank lines from stdin to use
per command line. */
long lines_per_exec; /* 0 */
/* The maximum number of arguments to use per command line. */
long args_per_exec;
};
enum BC_INIT_STATUS
{
BC_INIT_OK = 0,
BC_INIT_ENV_TOO_BIG,
BC_INIT_CANNOT_ACCOMODATE_HEADROOM
};
extern size_t bc_size_of_environment (void);
extern void bc_do_insert (struct buildcmd_control *ctl,
struct buildcmd_state *state,
char *arg, size_t arglen,
const char *prefix, size_t pfxlen,
const char *linebuf, size_t lblen,
int initial_args);
extern void bc_do_exec (struct buildcmd_control *ctl,
struct buildcmd_state *state);
extern void bc_push_arg (struct buildcmd_control *ctl,
struct buildcmd_state *state,
const char *arg, size_t len,
const char *prefix, size_t pfxlen,
int initial_args);
extern void bc_init_state(const struct buildcmd_control *ctl,
struct buildcmd_state *state,
void *usercontext);
extern enum BC_INIT_STATUS bc_init_controlinfo(struct buildcmd_control *ctl,
size_t arglen_headroom);
extern size_t bc_get_arg_max(void);
extern void bc_use_sensible_arg_max(struct buildcmd_control *ctl);
extern void bc_clear_args(const struct buildcmd_control *ctl,
struct buildcmd_state *state);
extern int bc_args_exceed_testing_limit(const char **argv);
#endif