2016-05-03 12:28:06 +08:00
|
|
|
// Functions that we may safely call after fork().
|
2016-05-19 06:30:21 +08:00
|
|
|
#include "config.h" // IWYU pragma: keep
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <errno.h>
|
2016-05-03 12:28:06 +08:00
|
|
|
#include <fcntl.h>
|
2021-03-09 08:29:45 +08:00
|
|
|
#include <paths.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
2019-10-14 06:50:48 +08:00
|
|
|
|
2019-05-05 18:09:25 +08:00
|
|
|
#include <cstring>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
2020-04-05 10:15:08 +08:00
|
|
|
#ifdef FISH_USE_POSIX_SPAWN
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <spawn.h>
|
|
|
|
#endif
|
2019-03-13 05:06:01 +08:00
|
|
|
#include <cwchar>
|
2016-04-21 14:00:54 +08:00
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "common.h"
|
2012-02-29 07:11:46 +08:00
|
|
|
#include "exec.h"
|
2019-05-19 05:51:16 +08:00
|
|
|
#include "flog.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.h"
|
2016-05-03 12:28:06 +08:00
|
|
|
#include "iothread.h"
|
2020-07-20 07:41:58 +08:00
|
|
|
#include "job_group.h"
|
2016-05-03 12:28:06 +08:00
|
|
|
#include "postfork.h"
|
|
|
|
#include "proc.h"
|
2019-01-29 06:35:56 +08:00
|
|
|
#include "redirection.h"
|
2016-05-03 12:28:06 +08:00
|
|
|
#include "signal.h"
|
|
|
|
#include "wutil.h" // IWYU pragma: keep
|
2012-02-29 07:11:46 +08:00
|
|
|
|
2013-01-17 19:46:33 +08:00
|
|
|
#ifndef JOIN_THREADS_BEFORE_FORK
|
2013-02-01 07:57:08 +08:00
|
|
|
#define JOIN_THREADS_BEFORE_FORK 0
|
2013-01-17 19:46:33 +08:00
|
|
|
#endif
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// The number of times to try to call fork() before giving up.
|
2012-02-29 07:11:46 +08:00
|
|
|
#define FORK_LAPS 5
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// The number of nanoseconds to sleep between attempts to call fork().
|
2012-02-29 07:11:46 +08:00
|
|
|
#define FORK_SLEEP_TIME 1000000
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Fork error message.
|
2012-03-09 15:21:07 +08:00
|
|
|
#define FORK_ERROR "Could not create child process - exiting"
|
2012-02-29 07:11:46 +08:00
|
|
|
|
2021-03-09 08:29:45 +08:00
|
|
|
extern bool is_thompson_shell_script(const char *path);
|
2020-01-30 05:40:17 +08:00
|
|
|
static char *get_interpreter(const char *command, char *buffer, size_t buff_size);
|
|
|
|
|
2020-01-31 03:24:02 +08:00
|
|
|
/// Report the error code \p err for a failed setpgid call.
|
2020-11-21 04:22:42 +08:00
|
|
|
void report_setpgid_error(int err, bool is_parent, pid_t desired_pgid, const job_t *j,
|
|
|
|
const process_t *p) {
|
2020-01-31 03:24:02 +08:00
|
|
|
char pid_buff[128];
|
|
|
|
char job_id_buff[128];
|
|
|
|
char getpgid_buff[128];
|
|
|
|
char job_pgid_buff[128];
|
|
|
|
char argv0[64];
|
|
|
|
char command[64];
|
|
|
|
|
|
|
|
format_long_safe(pid_buff, p->pid);
|
|
|
|
format_long_safe(job_id_buff, j->job_id());
|
|
|
|
format_long_safe(getpgid_buff, getpgid(p->pid));
|
2020-05-30 05:51:48 +08:00
|
|
|
format_long_safe(job_pgid_buff, desired_pgid);
|
2020-01-31 03:24:02 +08:00
|
|
|
narrow_string_safe(argv0, p->argv0());
|
|
|
|
narrow_string_safe(command, j->command_wcstr());
|
|
|
|
|
2020-11-21 04:22:42 +08:00
|
|
|
debug_safe(1, "Could not send %s %s, '%s' in job %s, '%s' from group %s to group %s",
|
|
|
|
is_parent ? "child" : "self", pid_buff, argv0, job_id_buff, command, getpgid_buff,
|
|
|
|
job_pgid_buff);
|
2020-01-31 03:24:02 +08:00
|
|
|
|
|
|
|
if (is_windows_subsystem_for_linux() && errno == EPERM) {
|
|
|
|
debug_safe(1,
|
|
|
|
"Please update to Windows 10 1809/17763 or higher to address known issues "
|
|
|
|
"with process groups and zombie processes.");
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = err;
|
|
|
|
safe_perror("setpgid");
|
|
|
|
}
|
|
|
|
|
2020-05-30 05:51:48 +08:00
|
|
|
int execute_setpgid(pid_t pid, pid_t pgroup, bool is_parent) {
|
|
|
|
// Historically we have looped here to support WSL.
|
|
|
|
unsigned eperm_count = 0;
|
|
|
|
for (;;) {
|
|
|
|
if (setpgid(pid, pgroup) == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int err = errno;
|
|
|
|
if (err == EACCES && is_parent) {
|
|
|
|
// We are the parent process and our child has called exec().
|
|
|
|
// This is an unavoidable benign race.
|
|
|
|
return 0;
|
|
|
|
} else if (err == EINTR) {
|
|
|
|
// Paranoia.
|
|
|
|
continue;
|
|
|
|
} else if (err == EPERM && eperm_count++ < 100) {
|
2020-01-31 03:24:02 +08:00
|
|
|
// The setpgid(2) man page says that EPERM is returned only if attempts are made
|
|
|
|
// to move processes into groups across session boundaries (which can never be
|
|
|
|
// the case in fish, anywhere) or to change the process group ID of a session
|
|
|
|
// leader (again, can never be the case). I'm pretty sure this is a WSL bug, as
|
|
|
|
// we see the same with tcsetpgrp(2) in other places and it disappears on retry.
|
|
|
|
debug_safe(2, "setpgid(2) returned EPERM. Retrying");
|
|
|
|
continue;
|
2017-07-27 10:45:22 +08:00
|
|
|
}
|
2020-11-21 04:18:02 +08:00
|
|
|
#ifdef __BSD__
|
|
|
|
// POSIX.1 doesn't specify that zombie processes are required to be considered extant and/or
|
|
|
|
// children of the parent for purposes of setpgid(2). In particular, FreeBSD (at least up to
|
|
|
|
// 12.2) does not consider a child that has already forked, exec'd, and exited to "exist"
|
|
|
|
// and returns ESRCH (process not found) instead of EACCES (child has called exec).
|
|
|
|
// See https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=251227
|
|
|
|
else if (err == ESRCH && is_parent) {
|
|
|
|
// Handle this just like we would EACCES above, as we're virtually certain that
|
|
|
|
// setpgid(2) was called against a process that was at least at one point in time a
|
|
|
|
// valid child.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-05-30 05:51:48 +08:00
|
|
|
return err;
|
2017-07-27 10:45:22 +08:00
|
|
|
}
|
2018-02-15 09:08:12 +08:00
|
|
|
}
|
|
|
|
|
2020-07-27 08:55:00 +08:00
|
|
|
int child_setup_process(pid_t new_termowner, pid_t fish_pgrp, const job_t &job, bool is_forked,
|
2020-04-22 01:24:33 +08:00
|
|
|
const dup2_list_t &dup2s) {
|
2019-06-23 06:10:54 +08:00
|
|
|
// Note we are called in a forked child.
|
2019-01-29 06:35:56 +08:00
|
|
|
for (const auto &act : dup2s.get_actions()) {
|
2019-12-14 08:51:49 +08:00
|
|
|
int err;
|
|
|
|
if (act.target < 0) {
|
|
|
|
err = close(act.src);
|
|
|
|
} else if (act.target != act.src) {
|
|
|
|
// Normal redirection.
|
|
|
|
err = dup2(act.src, act.target);
|
|
|
|
} else {
|
|
|
|
// This is a weird case like /bin/cmd 6< file.txt
|
|
|
|
// The opened file (which is CLO_EXEC) wants to be dup2'd to its own fd.
|
|
|
|
// We need to unset the CLO_EXEC flag.
|
|
|
|
err = set_cloexec(act.src, false);
|
|
|
|
}
|
2019-01-29 06:35:56 +08:00
|
|
|
if (err < 0) {
|
2019-07-01 15:47:10 +08:00
|
|
|
if (is_forked) {
|
2019-06-23 06:10:54 +08:00
|
|
|
debug_safe(4, "redirect_in_child_after_fork failed in child_setup_process");
|
2019-01-29 06:35:56 +08:00
|
|
|
exit_without_destructors(1);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2019-01-29 06:35:56 +08:00
|
|
|
return err;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2020-07-27 08:55:00 +08:00
|
|
|
if (new_termowner != INVALID_PID && new_termowner != fish_pgrp) {
|
2019-06-23 03:33:06 +08:00
|
|
|
// Assign the terminal within the child to avoid the well-known race between tcsetgrp() in
|
|
|
|
// the parent and the child executing. We are not interested in error handling here, except
|
|
|
|
// we try to avoid this for non-terminals; in particular pipelines often make non-terminal
|
|
|
|
// stdin.
|
2020-07-27 08:55:00 +08:00
|
|
|
// Only do this if the tty currently belongs to fish's pgrp. Don't try to steal it away from
|
|
|
|
// another process which may happen if we are run in the background with job control
|
|
|
|
// enabled. Note if stdin is not a tty, then tcgetpgrp() will return -1 and we will not
|
|
|
|
// enter this.
|
|
|
|
if (tcgetpgrp(STDIN_FILENO) == fish_pgrp) {
|
2019-06-29 02:48:33 +08:00
|
|
|
// Ensure this doesn't send us to the background (see #5963)
|
|
|
|
signal(SIGTTIN, SIG_IGN);
|
|
|
|
signal(SIGTTOU, SIG_IGN);
|
2019-07-02 01:57:09 +08:00
|
|
|
(void)tcsetpgrp(STDIN_FILENO, new_termowner);
|
2019-06-23 03:33:06 +08:00
|
|
|
}
|
|
|
|
}
|
2020-04-05 16:24:26 +08:00
|
|
|
sigset_t sigmask;
|
|
|
|
sigemptyset(&sigmask);
|
|
|
|
if (blocked_signals_for_job(job, &sigmask)) {
|
|
|
|
sigprocmask(SIG_SETMASK, &sigmask, nullptr);
|
|
|
|
}
|
2019-06-29 02:48:33 +08:00
|
|
|
// Set the handling for job control signals back to the default.
|
|
|
|
// Do this after any tcsetpgrp call so that we swallow SIGTTIN.
|
|
|
|
signal_reset_handlers();
|
2012-11-19 08:30:30 +08:00
|
|
|
return 0;
|
2012-02-29 07:11:46 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// This function is a wrapper around fork. If the fork calls fails with EAGAIN, it is retried
|
|
|
|
/// FORK_LAPS times, with a very slight delay between each lap. If fork fails even then, the process
|
|
|
|
/// will exit with an error message.
|
2019-11-24 04:36:44 +08:00
|
|
|
pid_t execute_fork() {
|
2012-02-29 07:11:46 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2019-11-24 04:36:44 +08:00
|
|
|
if (JOIN_THREADS_BEFORE_FORK) {
|
2016-05-03 12:28:06 +08:00
|
|
|
// Make sure we have no outstanding threads before we fork. This is a pretty sketchy thing
|
|
|
|
// to do here, both because exec.cpp shouldn't have to know about iothreads, and because the
|
|
|
|
// completion handlers may do unexpected things.
|
2017-07-30 01:40:07 +08:00
|
|
|
debug_safe(4, "waiting for threads to drain.");
|
2012-02-29 07:11:46 +08:00
|
|
|
iothread_drain_all();
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
pid_t pid;
|
|
|
|
struct timespec pollint;
|
|
|
|
int i;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
for (i = 0; i < FORK_LAPS; i++) {
|
2012-11-19 08:30:30 +08:00
|
|
|
pid = fork();
|
2016-05-03 12:28:06 +08:00
|
|
|
if (pid >= 0) {
|
2012-11-19 08:30:30 +08:00
|
|
|
return pid;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (errno != EAGAIN) {
|
2012-11-19 08:30:30 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
pollint.tv_sec = 0;
|
|
|
|
pollint.tv_nsec = FORK_SLEEP_TIME;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Don't sleep on the final lap - sleeping might change the value of errno, which will break
|
|
|
|
// the error reporting below.
|
|
|
|
if (i != FORK_LAPS - 1) {
|
2019-11-19 10:34:50 +08:00
|
|
|
nanosleep(&pollint, nullptr);
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
debug_safe(0, FORK_ERROR);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("fork");
|
2012-11-19 08:30:30 +08:00
|
|
|
FATAL_EXIT();
|
2012-03-04 18:35:30 +08:00
|
|
|
return 0;
|
2012-02-29 07:11:46 +08:00
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
|
|
|
|
#if FISH_USE_POSIX_SPAWN
|
2020-06-10 05:56:03 +08:00
|
|
|
|
|
|
|
// Given an error code, if it is the first error, record it.
|
|
|
|
// \return whether we have any error.
|
|
|
|
bool posix_spawner_t::check_fail(int err) {
|
|
|
|
if (error_ == 0) error_ = err;
|
|
|
|
return error_ != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
posix_spawner_t::~posix_spawner_t() {
|
|
|
|
if (attr_) {
|
|
|
|
posix_spawnattr_destroy(this->attr());
|
|
|
|
}
|
|
|
|
if (actions_) {
|
|
|
|
posix_spawn_file_actions_destroy(this->actions());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
posix_spawner_t::posix_spawner_t(const job_t *j, const dup2_list_t &dup2s) {
|
|
|
|
// Initialize our fields. This may fail.
|
|
|
|
{
|
|
|
|
posix_spawnattr_t attr;
|
|
|
|
if (check_fail(posix_spawnattr_init(&attr))) return;
|
|
|
|
this->attr_ = attr;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2020-06-10 05:56:03 +08:00
|
|
|
{
|
|
|
|
posix_spawn_file_actions_t actions;
|
|
|
|
if (check_fail(posix_spawn_file_actions_init(&actions))) return;
|
|
|
|
this->actions_ = actions;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2020-05-30 05:51:48 +08:00
|
|
|
// desired_pgid tracks the pgroup for the process. If it is none, the pgroup is left unchanged.
|
|
|
|
// If it is zero, create a new pgroup from the pid. If it is >0, join that pgroup.
|
|
|
|
maybe_t<pid_t> desired_pgid = none();
|
2020-05-31 05:05:07 +08:00
|
|
|
if (auto job_pgid = j->group->get_pgid()) {
|
2020-05-30 05:51:48 +08:00
|
|
|
desired_pgid = *job_pgid;
|
|
|
|
} else {
|
2020-05-31 05:05:07 +08:00
|
|
|
assert(j->group->needs_pgid_assignment() && "We should be expecting a pgid");
|
|
|
|
// We are the first external proc in the job group. Set the desired_pgid to 0 to indicate we
|
2020-05-30 05:51:48 +08:00
|
|
|
// should creating a new process group.
|
|
|
|
desired_pgid = 0;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Set the handling for job control signals back to the default.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool reset_signal_handlers = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Remove all signal blocks.
|
2012-11-19 08:30:30 +08:00
|
|
|
bool reset_sigmask = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Set our flags.
|
2012-08-15 15:57:56 +08:00
|
|
|
short flags = 0;
|
2016-05-03 12:28:06 +08:00
|
|
|
if (reset_signal_handlers) flags |= POSIX_SPAWN_SETSIGDEF;
|
|
|
|
if (reset_sigmask) flags |= POSIX_SPAWN_SETSIGMASK;
|
2020-05-30 05:51:48 +08:00
|
|
|
if (desired_pgid.has_value()) flags |= POSIX_SPAWN_SETPGROUP;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2020-06-10 05:56:03 +08:00
|
|
|
if (check_fail(posix_spawnattr_setflags(attr(), flags))) return;
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2020-06-10 05:56:03 +08:00
|
|
|
if (desired_pgid.has_value()) {
|
|
|
|
if (check_fail(posix_spawnattr_setpgroup(attr(), *desired_pgid))) return;
|
2020-05-30 05:51:48 +08:00
|
|
|
}
|
2012-08-16 08:32:57 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Everybody gets default handlers.
|
2020-06-10 05:56:03 +08:00
|
|
|
if (reset_signal_handlers) {
|
2012-08-15 15:57:56 +08:00
|
|
|
sigset_t sigdefault;
|
|
|
|
get_signals_with_handlers(&sigdefault);
|
2020-06-10 05:56:03 +08:00
|
|
|
if (check_fail(posix_spawnattr_setsigdefault(attr(), &sigdefault))) return;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// No signals blocked.
|
2020-06-10 05:56:03 +08:00
|
|
|
if (reset_sigmask) {
|
|
|
|
sigset_t sigmask;
|
|
|
|
sigemptyset(&sigmask);
|
2020-04-05 16:24:26 +08:00
|
|
|
blocked_signals_for_job(*j, &sigmask);
|
2020-06-10 05:56:03 +08:00
|
|
|
if (check_fail(posix_spawnattr_setsigmask(attr(), &sigmask))) return;
|
2020-04-05 16:24:26 +08:00
|
|
|
}
|
2016-05-03 12:28:06 +08:00
|
|
|
|
2019-01-29 16:34:38 +08:00
|
|
|
// Apply our dup2s.
|
|
|
|
for (const auto &act : dup2s.get_actions()) {
|
|
|
|
if (act.target < 0) {
|
2020-06-10 05:56:03 +08:00
|
|
|
if (check_fail(posix_spawn_file_actions_addclose(actions(), act.src))) return;
|
2019-01-29 16:34:38 +08:00
|
|
|
} else {
|
2020-06-10 05:56:03 +08:00
|
|
|
if (check_fail(posix_spawn_file_actions_adddup2(actions(), act.src, act.target)))
|
|
|
|
return;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-10 05:56:03 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2020-06-10 05:56:03 +08:00
|
|
|
maybe_t<pid_t> posix_spawner_t::spawn(const char *cmd, char *const argv[], char *const envp[]) {
|
|
|
|
if (get_error()) return none();
|
|
|
|
pid_t pid = -1;
|
2021-03-09 08:29:45 +08:00
|
|
|
if (check_fail(posix_spawn(&pid, cmd, &*actions_, &*attr_, argv, envp))) {
|
|
|
|
// The shebang wasn't introduced until UNIX Seventh Edition, so if
|
|
|
|
// the kernel won't run the binary we hand it off to the intpreter
|
|
|
|
// after performing a binary safety check, recommended by POSIX: a
|
|
|
|
// line needs to exist before the first \0 with a lowercase letter
|
|
|
|
if (error_ == ENOEXEC && is_thompson_shell_script(cmd)) {
|
|
|
|
error_ = 0;
|
2021-03-28 07:07:57 +08:00
|
|
|
// Create a new argv with /bin/sh prepended.
|
|
|
|
std::vector<char *> argv2;
|
2021-03-09 08:29:45 +08:00
|
|
|
char interp[] = _PATH_BSHELL;
|
2021-03-28 07:07:57 +08:00
|
|
|
argv2.push_back(interp);
|
|
|
|
for (size_t i = 0; argv[i] != nullptr; i++) {
|
|
|
|
argv2.push_back(argv[i]);
|
2021-03-09 08:29:45 +08:00
|
|
|
}
|
2021-03-28 07:07:57 +08:00
|
|
|
argv2.push_back(nullptr);
|
|
|
|
if (check_fail(posix_spawn(&pid, interp, &*actions_, &*attr_, &argv2[0], envp))) {
|
2021-03-09 08:29:45 +08:00
|
|
|
return none();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return none();
|
|
|
|
}
|
|
|
|
}
|
2020-06-10 05:56:03 +08:00
|
|
|
return pid;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2020-06-10 05:56:03 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
#endif // FISH_USE_POSIX_SPAWN
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
void safe_report_exec_error(int err, const char *actual_cmd, const char *const *argv,
|
|
|
|
const char *const *envv) {
|
2012-11-19 08:30:30 +08:00
|
|
|
debug_safe(0, "Failed to execute process '%s'. Reason:", actual_cmd);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
switch (err) {
|
|
|
|
case E2BIG: {
|
2012-11-19 16:31:03 +08:00
|
|
|
char sz1[128], sz2[128];
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
long arg_max = -1;
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
size_t sz = 0;
|
2016-05-03 12:28:06 +08:00
|
|
|
const char *const *p;
|
|
|
|
for (p = argv; *p; p++) {
|
2019-03-13 06:07:07 +08:00
|
|
|
sz += std::strlen(*p) + 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
for (p = envv; *p; p++) {
|
2019-03-13 06:07:07 +08:00
|
|
|
sz += std::strlen(*p) + 1;
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
format_size_safe(sz1, sz);
|
|
|
|
arg_max = sysconf(_SC_ARG_MAX);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (arg_max > 0) {
|
2020-03-25 00:23:41 +08:00
|
|
|
if (sz >= static_cast<unsigned long long>(arg_max)) {
|
|
|
|
format_size_safe(sz2, static_cast<unsigned long long>(arg_max));
|
2020-03-27 03:45:40 +08:00
|
|
|
debug_safe(
|
|
|
|
0,
|
|
|
|
"The total size of the argument and environment lists %s exceeds the "
|
|
|
|
"operating system limit of %s.",
|
|
|
|
sz1, sz2);
|
2020-03-25 00:23:41 +08:00
|
|
|
} else {
|
2020-03-27 03:45:40 +08:00
|
|
|
// MAX_ARG_STRLEN, a linux thing that limits the size of one argument. It's
|
2020-06-26 22:44:27 +08:00
|
|
|
// defined in binfmts.h, but we don't want to include that just to be able to
|
2020-03-27 03:45:40 +08:00
|
|
|
// print the real limit.
|
2020-03-25 00:23:41 +08:00
|
|
|
debug_safe(0,
|
2020-03-27 03:45:40 +08:00
|
|
|
"One of your arguments exceeds the operating system's argument "
|
|
|
|
"length limit.");
|
2020-03-25 00:23:41 +08:00
|
|
|
}
|
2016-05-03 12:28:06 +08:00
|
|
|
} else {
|
|
|
|
debug_safe(0,
|
|
|
|
"The total size of the argument and environment lists (%s) exceeds the "
|
|
|
|
"operating system limit.",
|
|
|
|
sz1);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe(0, "Try running the command again with fewer arguments.");
|
|
|
|
break;
|
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
case ENOEXEC: {
|
2013-01-10 09:06:20 +08:00
|
|
|
const char *err = safe_strerror(errno);
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe(0, "exec: %s", err);
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
debug_safe(0,
|
|
|
|
"The file '%s' is marked as an executable but could not be run by the "
|
|
|
|
"operating system.",
|
|
|
|
actual_cmd);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
case ENOENT: {
|
|
|
|
// ENOENT is returned by exec() when the path fails, but also returned by posix_spawn if
|
|
|
|
// an open file action fails. These cases appear to be impossible to distinguish. We
|
|
|
|
// address this by not using posix_spawn for file redirections, so all the ENOENTs we
|
|
|
|
// find must be errors from exec().
|
2020-01-30 05:40:17 +08:00
|
|
|
char interpreter_buff[128] = {};
|
|
|
|
const char *interpreter =
|
|
|
|
get_interpreter(actual_cmd, interpreter_buff, sizeof interpreter_buff);
|
2016-05-03 12:28:06 +08:00
|
|
|
if (interpreter && 0 != access(interpreter, X_OK)) {
|
2020-09-25 22:44:02 +08:00
|
|
|
// Detect windows line endings and complain specifically about them.
|
|
|
|
auto len = strlen(interpreter);
|
|
|
|
if (len && interpreter[len - 1] == '\r') {
|
|
|
|
debug_safe(0,
|
2020-11-22 21:39:48 +08:00
|
|
|
"The file uses windows line endings (\\r\\n). Run dos2unix or "
|
|
|
|
"similar to fix it.");
|
2020-09-25 22:44:02 +08:00
|
|
|
} else {
|
|
|
|
debug_safe(0,
|
|
|
|
"The file '%s' specified the interpreter '%s', which is not an "
|
|
|
|
"executable command.",
|
|
|
|
actual_cmd, interpreter);
|
|
|
|
}
|
2016-05-03 12:28:06 +08:00
|
|
|
} else {
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe(0, "The file '%s' does not exist or could not be executed.", actual_cmd);
|
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
case ENOMEM: {
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe(0, "Out of memory");
|
|
|
|
break;
|
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
default: {
|
2013-01-10 09:06:20 +08:00
|
|
|
const char *err = safe_strerror(errno);
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe(0, "exec: %s", err);
|
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2020-01-30 05:40:17 +08:00
|
|
|
|
|
|
|
/// Returns the interpreter for the specified script. Returns NULL if file is not a script with a
|
|
|
|
/// shebang.
|
|
|
|
static char *get_interpreter(const char *command, char *buffer, size_t buff_size) {
|
|
|
|
// OK to not use CLO_EXEC here because this is only called after fork.
|
|
|
|
int fd = open(command, O_RDONLY);
|
|
|
|
if (fd >= 0) {
|
|
|
|
size_t idx = 0;
|
|
|
|
while (idx + 1 < buff_size) {
|
|
|
|
char ch;
|
|
|
|
ssize_t amt = read(fd, &ch, sizeof ch);
|
|
|
|
if (amt <= 0) break;
|
|
|
|
if (ch == '\n') break;
|
|
|
|
buffer[idx++] = ch;
|
|
|
|
}
|
|
|
|
buffer[idx++] = '\0';
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
|
2021-02-09 05:09:10 +08:00
|
|
|
if (std::strncmp(buffer, "#! /", const_strlen("#! /")) == 0) {
|
2020-01-30 05:40:17 +08:00
|
|
|
return buffer + 3;
|
2021-02-09 05:09:10 +08:00
|
|
|
} else if (std::strncmp(buffer, "#!/", const_strlen("#!/")) == 0) {
|
2020-01-30 05:40:17 +08:00
|
|
|
return buffer + 2;
|
|
|
|
}
|
|
|
|
return nullptr;
|
2020-06-10 05:56:03 +08:00
|
|
|
};
|