1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/* exec.c -- Implementation of -exec, -execdir, -ok, -okdir.
Copyright (C) 1990-1994, 2000, 2003-2010, 2016 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/>.
*/
/* config.h must be included first. */
#include <config.h>
/* system headers. */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
/* gnulib headers */
#include "cloexec.h"
#include "dirname.h"
#include "error.h"
#include "gettext.h"
#include "save-cwd.h"
#include "xalloc.h"
/* findutils headers */
#include "buildcmd.h"
#include "defs.h"
#include "fdleak.h"
#if ENABLE_NLS
# include <libintl.h>
# define _(Text) gettext (Text)
#else
# define _(Text) Text
#endif
/* Initialize exec->wd_for_exec.
We save in exec->wd_for_exec the directory whose path relative to
cwd_df is dir.
*/
static bool
initialize_wd_for_exec (struct exec_val *execp, int cwd_fd, const char *dir)
{
execp->wd_for_exec = xmalloc (sizeof (*execp->wd_for_exec));
execp->wd_for_exec->name = NULL;
execp->wd_for_exec->desc = openat (cwd_fd, dir, O_RDONLY);
if (execp->wd_for_exec->desc < 0)
return false;
set_cloexec_flag (execp->wd_for_exec->desc, true);
return true;
}
static bool
record_exec_dir (struct exec_val *execp)
{
if (!execp->state.todo)
{
/* working directory not already known, so must be a *dir variant,
and this must be the first arg we added. However, this may
be -execdir foo {} \; (i.e. not multiple). */
assert (!execp->state.todo);
/* Record the WD. If we're using -L or fts chooses to do so for
any other reason, state.cwd_dir_fd may in fact not be the
directory containing the target file. When this happens,
rel_path will contain directory components (since it is the
path from state.cwd_dir_fd to the target file).
We deal with this by extracting any directory part and using
that to adjust what goes into execp->wd_for_exec.
*/
if (strchr (state.rel_pathname, '/'))
{
char *dir = mdir_name (state.rel_pathname);
bool result = initialize_wd_for_exec (execp, state.cwd_dir_fd, dir);
free (dir);
return result;
}
else
{
return initialize_wd_for_exec (execp, state.cwd_dir_fd, ".");
}
}
return true;
}
bool
impl_pred_exec (const char *pathname,
struct stat *stat_buf,
struct predicate *pred_ptr)
{
struct exec_val *execp = &pred_ptr->args.exec_vec;
char *buf = NULL;
const char *target;
bool result;
const bool local = is_exec_in_local_dir (pred_ptr->pred_func);
const char *prefix;
size_t pfxlen;
(void) stat_buf;
if (local)
{
/* For -execdir/-okdir predicates, the parser did not fill in
the wd_for_exec member of struct exec_val. So for those
predicates, we do so now.
*/
if (!record_exec_dir (execp))
{
error (EXIT_FAILURE, errno,
_("Failed to save working directory in order to "
"run a command on %s"),
safely_quote_err_filename (0, pathname));
/*NOTREACHED*/
}
target = buf = base_name (state.rel_pathname);
if ('/' == target[0])
{
/* find / execdir ls -d {} \; */
prefix = NULL;
pfxlen = 0;
}
else
{
prefix = "./";
pfxlen = 2u;
}
}
else
{
/* For the others (-exec, -ok), the parser should
have set wd_for_exec to initial_wd, indicating
that the exec should take place from find's initial
working directory.
*/
assert (execp->wd_for_exec == initial_wd);
target = pathname;
prefix = NULL;
pfxlen = 0u;
}
if (execp->multiple)
{
/* Push the argument onto the current list.
* The command may or may not be run at this point,
* depending on the command line length limits.
*/
bc_push_arg (&execp->ctl,
&execp->state,
target, strlen (target)+1,
prefix, pfxlen,
0);
/* remember that there are pending execdirs. */
if (execp->state.todo)
state.execdirs_outstanding = true;
/* POSIX: If the primary expression is punctuated by a plus
* sign, the primary shall always evaluate as true
*/
result = true;
}
else
{
int i;
for (i=0; i<execp->num_args; ++i)
{
bc_do_insert (&execp->ctl,
&execp->state,
execp->replace_vec[i],
strlen (execp->replace_vec[i]),
prefix, pfxlen,
target, strlen (target),
0);
}
/* Actually invoke the command. */
bc_do_exec (&execp->ctl, &execp->state);
if (WIFEXITED(execp->last_child_status))
{
if (0 == WEXITSTATUS(execp->last_child_status))
result = true; /* The child succeeded. */
else
result = false;
}
else
{
result = false;
}
if (local)
free_cwd (execp->wd_for_exec);
}
if (buf)
{
assert (local);
free (buf);
}
return result;
}
/* 1) fork to get a child; parent remembers the child pid
2) child execs the command requested
3) parent waits for child; checks for proper pid of child
Possible returns:
ret errno status(h) status(l)
pid x signal# 0177 stopped
pid x exit arg 0 term by _exit
pid x 0 signal # term by signal
-1 EINTR parent got signal
-1 other some other kind of error
Return true only if the pid matches, status(l) is
zero, and the exit arg (status high) is 0.
Otherwise return false, possibly printing an error message. */
static bool
prep_child_for_exec (bool close_stdin, const struct saved_cwd *wd)
{
bool ok = true;
if (close_stdin)
{
const char inputfile[] = "/dev/null";
if (close (0) < 0)
{
error (0, errno, _("Cannot close standard input"));
ok = false;
}
else
{
if (open (inputfile, O_RDONLY
#if defined O_LARGEFILE
|O_LARGEFILE
#endif
) < 0)
{
/* This is not entirely fatal, since
* executing the child with a closed
* stdin is almost as good as executing it
* with its stdin attached to /dev/null.
*/
error (0, errno, "%s", safely_quote_err_filename (0, inputfile));
/* do not set ok=false, it is OK to continue anyway. */
}
}
}
/* Even if DebugSearch is set, don't announce our change of
* directory, since we're not going to emit a subsequent
* announcement of a call to stat() anyway, as we're about to exec
* something.
*/
if (0 != restore_cwd (wd))
{
error (0, errno, _("Failed to change directory%s%s"),
(wd->desc < 0 && wd->name) ? ": " : "",
(wd->desc < 0 && wd->name) ? wd->name : "");
ok = false;
}
return ok;
}
int
launch (struct buildcmd_control *ctl, void *usercontext, int argc, char **argv)
{
pid_t child_pid;
static int first_time = 1;
struct exec_val *execp = usercontext;
(void) ctl; /* silence compiler warning */
(void) argc; /* silence compiler warning */
/* Make sure output of command doesn't get mixed with find output. */
fflush (stdout);
fflush (stderr);
/* Make sure to listen for the kids. */
if (first_time)
{
first_time = 0;
signal (SIGCHLD, SIG_DFL);
}
child_pid = fork ();
if (child_pid == -1)
error (EXIT_FAILURE, errno, _("cannot fork"));
if (child_pid == 0)
{
/* We are the child. */
assert (NULL != execp->wd_for_exec);
if (!prep_child_for_exec (execp->close_stdin, execp->wd_for_exec))
{
_exit (1);
}
else
{
if (fd_leak_check_is_enabled ())
{
complain_about_leaky_fds ();
}
}
if (bc_args_exceed_testing_limit (argv))
errno = E2BIG;
else
execvp (argv[0], argv);
/* TODO: use a pipe to pass back the errno value, like xargs does */
error (0, errno, "%s",
safely_quote_err_filename (0, argv[0]));
_exit (1);
}
while (waitpid (child_pid, &(execp->last_child_status), 0) == (pid_t) -1)
{
if (errno != EINTR)
{
error (0, errno, _("error waiting for %s"),
safely_quote_err_filename (0, argv[0]));
state.exit_status = 1;
return 0; /* FAIL */
}
}
if (WIFSIGNALED (execp->last_child_status))
{
error (0, 0, _("%s terminated by signal %d"),
quotearg_n_style (0, options.err_quoting_style, argv[0]),
WTERMSIG (execp->last_child_status));
if (execp->multiple)
{
/* -exec \; just returns false if the invoked command fails.
* -exec {} + returns true if the invoked command fails, but
* sets the program exit status.
*/
state.exit_status = 1;
}
return 1; /* OK */
}
if (0 == WEXITSTATUS (execp->last_child_status))
{
return 1; /* OK */
}
else
{
if (execp->multiple)
{
/* -exec \; just returns false if the invoked command fails.
* -exec {} + returns true if the invoked command fails, but
* sets the program exit status.
*/
state.exit_status = 1;
}
/* The child failed, but this is the exec callback. We
* don't want to run the child again in this case anwyay.
*/
return 1; /* FAIL (but don't try again) */
}
}
|