2016-05-03 12:28:06 +08:00
|
|
|
// Functions that we may safely call after fork().
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <errno.h>
|
2016-05-03 12:28:06 +08:00
|
|
|
#include <fcntl.h>
|
2015-07-25 23:14:25 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <memory>
|
|
|
|
#if FISH_USE_POSIX_SPAWN
|
|
|
|
#include <spawn.h>
|
|
|
|
#endif
|
|
|
|
|
2015-07-25 23:14:25 +08:00
|
|
|
#include "common.h"
|
2012-02-29 07:11:46 +08:00
|
|
|
#include "exec.h"
|
2016-04-21 14:00:54 +08:00
|
|
|
#include "io.h"
|
2016-05-03 12:28:06 +08:00
|
|
|
#include "iothread.h"
|
|
|
|
#include "postfork.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#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
|
|
|
/// Base open mode to pass to calls to open.
|
2012-02-29 07:11:46 +08:00
|
|
|
#define OPEN_MASK 0666
|
|
|
|
|
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
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// File redirection clobbering error message.
|
2012-03-09 15:21:07 +08:00
|
|
|
#define NOCLOB_ERROR "The file '%s' already exists"
|
2012-02-29 07:11:46 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// File redirection error message.
|
2012-03-09 15:21:07 +08:00
|
|
|
#define FILE_ERROR "An error occurred while redirecting file '%s'"
|
2012-02-29 07:11:46 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// File descriptor redirection error message.
|
|
|
|
#define FD_ERROR "An error occurred while redirecting file descriptor %s"
|
2012-02-29 07:11:46 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Pipe error message.
|
2012-03-09 15:21:07 +08:00
|
|
|
#define LOCAL_PIPE_ERROR "An error occurred while setting up pipe"
|
|
|
|
|
2013-08-19 07:55:01 +08:00
|
|
|
static bool log_redirections = false;
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Cover for debug_safe that can take an int. The format string should expect a %s.
|
|
|
|
static void debug_safe_int(int level, const char *format, int val) {
|
2012-03-09 15:21:07 +08:00
|
|
|
char buff[128];
|
|
|
|
format_long_safe(buff, val);
|
|
|
|
debug_safe(level, format, buff);
|
|
|
|
}
|
2012-02-28 11:20:27 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
int set_child_group(job_t *j, process_t *p, int print_errors) {
|
2012-11-19 08:30:30 +08:00
|
|
|
int res = 0;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (job_get_flag(j, JOB_CONTROL)) {
|
|
|
|
if (!j->pgid) {
|
2012-11-19 08:30:30 +08:00
|
|
|
j->pgid = p->pid;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (setpgid(p->pid, j->pgid)) {
|
|
|
|
if (getpgid(p->pid) != j->pgid && print_errors) {
|
2012-03-09 15:21:07 +08:00
|
|
|
char pid_buff[128];
|
|
|
|
char job_id_buff[128];
|
|
|
|
char getpgid_buff[128];
|
|
|
|
char job_pgid_buff[128];
|
2016-02-28 17:38:28 +08:00
|
|
|
char argv0[64];
|
|
|
|
char command[64];
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-03-09 15:21:07 +08:00
|
|
|
format_long_safe(pid_buff, p->pid);
|
|
|
|
format_long_safe(job_id_buff, j->job_id);
|
2012-11-19 08:30:30 +08:00
|
|
|
format_long_safe(getpgid_buff, getpgid(p->pid));
|
2012-03-09 15:21:07 +08:00
|
|
|
format_long_safe(job_pgid_buff, j->pgid);
|
2016-02-28 17:38:28 +08:00
|
|
|
narrow_string_safe(argv0, p->argv0());
|
|
|
|
narrow_string_safe(command, j->command_wcstr());
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
debug_safe(
|
|
|
|
1, "Could not send process %s, '%s' in job %s, '%s' from group %s to group %s",
|
|
|
|
pid_buff, argv0, job_id_buff, command, getpgid_buff, job_pgid_buff);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("setpgid");
|
2012-11-19 08:30:30 +08:00
|
|
|
res = -1;
|
|
|
|
}
|
|
|
|
}
|
2016-05-03 12:28:06 +08:00
|
|
|
} else {
|
2012-11-19 08:30:30 +08:00
|
|
|
j->pgid = getpid();
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (job_get_flag(j, JOB_TERMINAL) && job_get_flag(j, JOB_FOREGROUND)) {
|
|
|
|
if (tcsetpgrp(0, j->pgid) && print_errors) {
|
2016-02-28 17:38:28 +08:00
|
|
|
char job_id_buff[64];
|
|
|
|
char command_buff[64];
|
2012-03-09 15:21:07 +08:00
|
|
|
format_long_safe(job_id_buff, j->job_id);
|
2016-02-28 17:38:28 +08:00
|
|
|
narrow_string_safe(command_buff, j->command_wcstr());
|
|
|
|
debug_safe(1, "Could not send job %s ('%s') to foreground", job_id_buff, command_buff);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("tcsetpgrp");
|
2012-11-19 08:30:30 +08:00
|
|
|
res = -1;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-02-28 11:20:27 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return res;
|
2012-02-28 11:20:27 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Set up a childs io redirections. Should only be called by setup_child_process(). Does the
|
|
|
|
/// following: First it closes any open file descriptors not related to the child by calling
|
|
|
|
/// close_unused_internal_pipes() and closing the universal variable server file descriptor. It then
|
|
|
|
/// goes on to perform all the redirections described by \c io.
|
|
|
|
///
|
|
|
|
/// \param io the list of IO redirections for the child
|
|
|
|
///
|
|
|
|
/// \return 0 on sucess, -1 on failure
|
|
|
|
static int handle_child_io(const io_chain_t &io_chain) {
|
|
|
|
for (size_t idx = 0; idx < io_chain.size(); idx++) {
|
2014-04-12 00:50:12 +08:00
|
|
|
const io_data_t *io = io_chain.at(idx).get();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (io->io_mode == IO_FD && io->fd == static_cast<const io_fd_t *>(io)->old_fd) {
|
2012-11-19 08:30:30 +08:00
|
|
|
continue;
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
switch (io->io_mode) {
|
|
|
|
case IO_CLOSE: {
|
2013-08-19 07:55:01 +08:00
|
|
|
if (log_redirections) fprintf(stderr, "%d: close %d\n", getpid(), io->fd);
|
2016-05-03 12:28:06 +08:00
|
|
|
if (close(io->fd)) {
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe_int(0, "Failed to close file descriptor %s", io->fd);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("close");
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
case IO_FILE: {
|
|
|
|
// Here we definitely do not want to set CLO_EXEC because our child needs access.
|
2014-04-12 00:50:12 +08:00
|
|
|
CAST_INIT(const io_file_t *, io_file, io);
|
2016-03-04 10:49:12 +08:00
|
|
|
int tmp = open(io_file->filename_cstr, io_file->flags, OPEN_MASK);
|
2016-05-03 12:28:06 +08:00
|
|
|
if (tmp < 0) {
|
|
|
|
if ((io_file->flags & O_EXCL) && (errno == EEXIST)) {
|
2013-01-15 16:18:03 +08:00
|
|
|
debug_safe(1, NOCLOB_ERROR, io_file->filename_cstr);
|
2016-05-03 12:28:06 +08:00
|
|
|
} else {
|
2013-01-15 16:18:03 +08:00
|
|
|
debug_safe(1, FILE_ERROR, io_file->filename_cstr);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("open");
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2016-05-03 12:28:06 +08:00
|
|
|
} else if (tmp != io->fd) {
|
|
|
|
// This call will sometimes fail, but that is ok, this is just a precausion.
|
2012-11-19 16:31:03 +08:00
|
|
|
close(io->fd);
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (dup2(tmp, io->fd) == -1) {
|
|
|
|
debug_safe_int(1, FD_ERROR, io->fd);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("dup2");
|
2015-07-20 17:34:57 +08:00
|
|
|
exec_close(tmp);
|
2012-11-19 16:31:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
exec_close(tmp);
|
|
|
|
}
|
|
|
|
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 IO_FD: {
|
2013-08-19 07:55:01 +08:00
|
|
|
int old_fd = static_cast<const io_fd_t *>(io)->old_fd;
|
2016-05-03 12:28:06 +08:00
|
|
|
if (log_redirections)
|
|
|
|
fprintf(stderr, "%d: fd dup %d to %d\n", getpid(), old_fd, io->fd);
|
2013-08-19 07:55:01 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// This call will sometimes fail, but that is ok, this is just a precausion.
|
2012-11-19 08:30:30 +08:00
|
|
|
close(io->fd);
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (dup2(old_fd, io->fd) == -1) {
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe_int(1, FD_ERROR, io->fd);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("dup2");
|
2012-11-19 08:30:30 +08:00
|
|
|
return -1;
|
|
|
|
}
|
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
|
|
|
case IO_BUFFER:
|
2016-05-03 12:28:06 +08:00
|
|
|
case IO_PIPE: {
|
2014-04-12 00:50:12 +08:00
|
|
|
CAST_INIT(const io_pipe_t *, io_pipe, io);
|
2016-05-03 12:28:06 +08:00
|
|
|
// If write_pipe_idx is 0, it means we're connecting to the read end (first pipe
|
|
|
|
// fd). If it's 1, we're connecting to the write end (second pipe fd).
|
2013-01-16 11:55:57 +08:00
|
|
|
unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1);
|
2016-05-03 12:28:06 +08:00
|
|
|
#if 0
|
|
|
|
debug( 0, L"%ls %ls on fd %d (%d %d)", write_pipe?L"write":L"read",
|
|
|
|
(io->io_mode == IO_BUFFER)?L"buffer":L"pipe", io->fd, io->pipe_fd[0],
|
|
|
|
io->pipe_fd[1]);
|
|
|
|
#endif
|
|
|
|
if (log_redirections)
|
|
|
|
fprintf(stderr, "%d: %s dup %d to %d\n", getpid(),
|
|
|
|
io->io_mode == IO_BUFFER ? "buffer" : "pipe",
|
|
|
|
io_pipe->pipe_fd[write_pipe_idx], io->fd);
|
|
|
|
if (dup2(io_pipe->pipe_fd[write_pipe_idx], io->fd) != io->fd) {
|
2012-11-19 16:31:03 +08:00
|
|
|
debug_safe(1, LOCAL_PIPE_ERROR);
|
2013-01-10 09:06:20 +08:00
|
|
|
safe_perror("dup2");
|
2012-11-19 16:31:03 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (io_pipe->pipe_fd[0] >= 0) exec_close(io_pipe->pipe_fd[0]);
|
|
|
|
if (io_pipe->pipe_fd[1] >= 0) exec_close(io_pipe->pipe_fd[1]);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
int setup_child_process(job_t *j, process_t *p, const io_chain_t &io_chain) {
|
|
|
|
bool ok = true;
|
2012-02-29 07:11:46 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (p) {
|
2012-11-19 08:30:30 +08:00
|
|
|
ok = (0 == set_child_group(j, p, 1));
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (ok) {
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
|
|
|
ok = (0 == handle_child_io(io_chain));
|
2016-05-03 12:28:06 +08:00
|
|
|
if (p != 0 && !ok) {
|
2012-11-19 08:30:30 +08:00
|
|
|
exit_without_destructors(1);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (ok) {
|
|
|
|
// Set the handling for job control signals back to the default.
|
2012-11-19 08:30:30 +08:00
|
|
|
signal_reset_handlers();
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
signal_unblock(); // remove all signal blocks
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-11-19 08:30:30 +08:00
|
|
|
return ok ? 0 : -1;
|
2012-02-29 07:11:46 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 07:12:37 +08:00
|
|
|
int g_fork_count = 0;
|
|
|
|
|
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.
|
|
|
|
pid_t execute_fork(bool wait_for_threads_to_die) {
|
2012-02-29 07:11:46 +08:00
|
|
|
ASSERT_IS_MAIN_THREAD();
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (wait_for_threads_to_die || JOIN_THREADS_BEFORE_FORK) {
|
|
|
|
// 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.
|
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
|
|
|
|
2012-03-07 07:12:37 +08:00
|
|
|
g_fork_count++;
|
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) {
|
2012-11-19 08:30:30 +08:00
|
|
|
nanosleep(&pollint, NULL);
|
|
|
|
}
|
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
|
2016-05-03 12:28:06 +08:00
|
|
|
bool fork_actions_make_spawn_properties(posix_spawnattr_t *attr,
|
|
|
|
posix_spawn_file_actions_t *actions, job_t *j, process_t *p,
|
|
|
|
const io_chain_t &io_chain) {
|
|
|
|
// Initialize the output.
|
|
|
|
if (posix_spawnattr_init(attr) != 0) {
|
2012-08-15 15:57:56 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (posix_spawn_file_actions_init(actions) != 0) {
|
2012-08-15 15:57:56 +08:00
|
|
|
posix_spawnattr_destroy(attr);
|
|
|
|
return false;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-08-15 15:57:56 +08:00
|
|
|
bool should_set_parent_group_id = false;
|
|
|
|
int desired_parent_group_id = 0;
|
2016-05-03 12:28:06 +08:00
|
|
|
if (job_get_flag(j, JOB_CONTROL)) {
|
2012-08-15 15:57:56 +08:00
|
|
|
should_set_parent_group_id = true;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// PCA: I'm quite fuzzy on process groups, but I believe that the default value of 0 means
|
|
|
|
// that the process becomes its own group leader, which is what set_child_group did in this
|
|
|
|
// case. So we want this to be 0 if j->pgid is 0.
|
2012-08-15 15:57:56 +08:00
|
|
|
desired_parent_group_id = j->pgid;
|
|
|
|
}
|
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;
|
|
|
|
if (should_set_parent_group_id) flags |= POSIX_SPAWN_SETPGROUP;
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2012-08-15 15:57:56 +08:00
|
|
|
int err = 0;
|
2016-05-03 12:28:06 +08:00
|
|
|
if (!err) err = posix_spawnattr_setflags(attr, flags);
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (!err && should_set_parent_group_id)
|
2012-08-16 08:32:57 +08:00
|
|
|
err = posix_spawnattr_setpgroup(attr, desired_parent_group_id);
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Everybody gets default handlers.
|
|
|
|
if (!err && reset_signal_handlers) {
|
2012-08-15 15:57:56 +08:00
|
|
|
sigset_t sigdefault;
|
|
|
|
get_signals_with_handlers(&sigdefault);
|
|
|
|
err = posix_spawnattr_setsigdefault(attr, &sigdefault);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// No signals blocked.
|
2012-08-15 15:57:56 +08:00
|
|
|
sigset_t sigmask;
|
|
|
|
sigemptyset(&sigmask);
|
2016-05-03 12:28:06 +08:00
|
|
|
if (!err && reset_sigmask) err = posix_spawnattr_setsigmask(attr, &sigmask);
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < io_chain.size(); idx++) {
|
Big fat refactoring of how redirections work. In fish 1.x and 2.0.0, the redirections for a process were flattened into a big list associated with the job, so there was no way to tell which redirections applied to each process. Each process therefore got all the redirections associated with the job. See https://github.com/fish-shell/fish-shell/issues/877 for how this could manifest.
With this change, jobs only track their block-level redirections. Process level redirections are correctly associated with the process, and at exec time we stitch them together (block, pipe, and process redirects).
This fixes the weird issues where redirects bleed across pipelines (like #877), and also allows us to play with the order in which redirections are applied, since the final list is constructed right before it's needed. This lets us put pipes after block level redirections but before process level redirections, so that a 2>&1-type redirection gets picked up after the pipe, i.e. it should fix https://github.com/fish-shell/fish-shell/issues/110
This is a significant change. The tests all pass. Cross your fingers.
2013-08-20 07:16:41 +08:00
|
|
|
const shared_ptr<const io_data_t> io = io_chain.at(idx);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (io->io_mode == IO_FD) {
|
2013-01-15 15:37:33 +08:00
|
|
|
CAST_INIT(const io_fd_t *, io_fd, io.get());
|
2016-05-03 12:28:06 +08:00
|
|
|
if (io->fd == io_fd->old_fd) continue;
|
2012-11-19 08:30:30 +08:00
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
switch (io->io_mode) {
|
|
|
|
case IO_CLOSE: {
|
|
|
|
if (!err) err = posix_spawn_file_actions_addclose(actions, io->fd);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-19 08:30:30 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
case IO_FILE: {
|
2013-01-15 16:18:03 +08:00
|
|
|
CAST_INIT(const io_file_t *, io_file, io.get());
|
2016-05-03 12:28:06 +08:00
|
|
|
if (!err)
|
|
|
|
err = posix_spawn_file_actions_addopen(actions, io->fd, io_file->filename_cstr,
|
|
|
|
io_file->flags /* mode */, OPEN_MASK);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
case IO_FD: {
|
2013-01-15 15:37:33 +08:00
|
|
|
CAST_INIT(const io_fd_t *, io_fd, io.get());
|
2016-05-03 12:28:06 +08:00
|
|
|
if (!err)
|
|
|
|
err = posix_spawn_file_actions_adddup2(actions, io_fd->old_fd /* from */,
|
|
|
|
io->fd /* to */);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2012-11-19 16:31:03 +08:00
|
|
|
|
|
|
|
case IO_BUFFER:
|
2016-05-03 12:28:06 +08:00
|
|
|
case IO_PIPE: {
|
2013-01-15 17:31:36 +08:00
|
|
|
CAST_INIT(const io_pipe_t *, io_pipe, io.get());
|
2013-01-16 11:55:57 +08:00
|
|
|
unsigned int write_pipe_idx = (io_pipe->is_input ? 0 : 1);
|
2013-01-15 17:31:36 +08:00
|
|
|
int from_fd = io_pipe->pipe_fd[write_pipe_idx];
|
2012-11-19 16:31:03 +08:00
|
|
|
int to_fd = io->fd;
|
2016-05-03 12:28:06 +08:00
|
|
|
if (!err) err = posix_spawn_file_actions_adddup2(actions, from_fd, to_fd);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (write_pipe_idx > 0) {
|
|
|
|
if (!err) err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[0]);
|
|
|
|
if (!err) err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[1]);
|
|
|
|
} else {
|
|
|
|
if (!err) err = posix_spawn_file_actions_addclose(actions, io_pipe->pipe_fd[0]);
|
2012-11-19 16:31:03 +08:00
|
|
|
}
|
|
|
|
break;
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// Clean up on error.
|
|
|
|
if (err) {
|
2012-08-15 15:57:56 +08:00
|
|
|
posix_spawnattr_destroy(attr);
|
|
|
|
posix_spawn_file_actions_destroy(actions);
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
return !err;
|
2012-08-15 15:57:56 +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++) {
|
|
|
|
sz += 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++) {
|
|
|
|
sz += 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) {
|
2015-08-09 01:35:17 +08:00
|
|
|
format_size_safe(sz2, static_cast<unsigned long long>(arg_max));
|
2016-05-03 12:28:06 +08:00
|
|
|
debug_safe(0,
|
|
|
|
"The total size of the argument and environment lists %s exceeds the "
|
|
|
|
"operating system limit of %s.",
|
|
|
|
sz1, sz2);
|
|
|
|
} 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().
|
2012-11-19 16:31:03 +08:00
|
|
|
char interpreter_buff[128] = {}, *interpreter;
|
|
|
|
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)) {
|
|
|
|
debug_safe(0,
|
|
|
|
"The file '%s' specified the interpreter '%s', which is not an "
|
|
|
|
"executable command.",
|
|
|
|
actual_cmd, interpreter);
|
|
|
|
} 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);
|
2012-11-18 18:23:22 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
// debug(0, L"The file '%ls' is marked as an executable but could not be run by the
|
|
|
|
// operating system.", p->actual_cmd);
|
2012-11-19 16:31:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-11-18 18:23:22 +08:00
|
|
|
}
|
2012-08-15 15:57:56 +08:00
|
|
|
}
|
2013-01-12 07:09:33 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Perform output from builtins. May be called from a forked child, so don't do anything that may
|
|
|
|
/// allocate memory, etc.
|
|
|
|
bool do_builtin_io(const char *out, size_t outlen, const char *err, size_t errlen) {
|
2013-01-12 07:09:33 +08:00
|
|
|
bool success = true;
|
2016-05-03 12:28:06 +08:00
|
|
|
if (out && outlen) {
|
|
|
|
if (write_loop(STDOUT_FILENO, out, outlen) < 0) {
|
2015-09-16 09:24:11 +08:00
|
|
|
int e = errno;
|
2013-01-13 02:10:34 +08:00
|
|
|
debug_safe(0, "Error while writing to stdout");
|
2013-01-12 07:09:33 +08:00
|
|
|
safe_perror("write_loop");
|
|
|
|
success = false;
|
2015-09-16 09:24:11 +08:00
|
|
|
errno = e;
|
2013-01-12 07:09:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
if (err && errlen) {
|
|
|
|
if (write_loop(STDERR_FILENO, err, errlen) < 0) {
|
2013-01-12 07:09:33 +08:00
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|