2016-05-03 12:28:06 +08:00
|
|
|
// Functions that we may safely call after fork(), of which there are very few. In particular we
|
|
|
|
// cannot allocate memory, since we're insane enough to call fork from a multithreaded process.
|
2012-02-28 11:20:27 +08:00
|
|
|
#ifndef FISH_POSTFORK_H
|
|
|
|
#define FISH_POSTFORK_H
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2017-02-13 12:24:22 +08:00
|
|
|
#include <stddef.h>
|
2016-04-21 14:00:54 +08:00
|
|
|
#include <unistd.h>
|
2012-08-15 15:57:56 +08:00
|
|
|
#if HAVE_SPAWN_H
|
|
|
|
#include <spawn.h>
|
|
|
|
#endif
|
|
|
|
#ifndef FISH_USE_POSIX_SPAWN
|
2012-11-19 08:30:30 +08:00
|
|
|
#define FISH_USE_POSIX_SPAWN HAVE_SPAWN_H
|
2012-08-15 15:57:56 +08:00
|
|
|
#endif
|
|
|
|
|
2016-04-21 14:00:54 +08:00
|
|
|
class io_chain_t;
|
|
|
|
class job_t;
|
|
|
|
class process_t;
|
2012-08-15 15:57:56 +08:00
|
|
|
|
2017-07-27 10:45:22 +08:00
|
|
|
bool set_child_group(job_t *j, pid_t child_pid); //called by parent
|
|
|
|
bool child_set_group(job_t *j, process_t *p); //called by child
|
2012-03-01 03:27:14 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Initialize a new child process. This should be called right away after forking in the child
|
|
|
|
/// process. If job control is enabled for this job, the process is put in the process group of the
|
|
|
|
/// job, all signal handlers are reset, signals are unblocked (this function may only be called
|
|
|
|
/// inside the exec function, which blocks all signals), and all IO redirections and other file
|
|
|
|
/// descriptor actions are performed.
|
|
|
|
///
|
|
|
|
/// \param j the job to set up the IO for
|
|
|
|
/// \param p the child process to set up
|
|
|
|
/// \param io_chain the IO chain to use
|
|
|
|
///
|
|
|
|
/// \return 0 on sucess, -1 on failiure. When this function returns, signals are always unblocked.
|
|
|
|
/// On failiure, signal handlers, io redirections and process group of the process is undefined.
|
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
|
|
|
int setup_child_process(job_t *j, process_t *p, const io_chain_t &io_chain);
|
2012-02-28 11:20:27 +08:00
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Call fork(), optionally waiting until we are no longer multithreaded. If the forked child
|
|
|
|
/// doesn't do anything that could allocate memory, take a lock, etc. (like call exec), then it's
|
|
|
|
/// not necessary to wait for threads to die. If the forked child may do those things, it should
|
|
|
|
/// wait for threads to die.
|
2012-02-29 07:11:46 +08:00
|
|
|
pid_t execute_fork(bool wait_for_threads_to_die);
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Perform output from builtins. Returns true on success.
|
2013-01-12 07:09:33 +08:00
|
|
|
bool do_builtin_io(const char *out, size_t outlen, const char *err, size_t errlen);
|
|
|
|
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Report an error from failing to exec or posix_spawn a command.
|
|
|
|
void safe_report_exec_error(int err, const char *actual_cmd, const char *const *argv,
|
|
|
|
const char *const *envv);
|
2012-08-15 15:57:56 +08:00
|
|
|
|
|
|
|
#if FISH_USE_POSIX_SPAWN
|
2016-05-03 12:28:06 +08:00
|
|
|
/// Initializes and fills in a posix_spawnattr_t; on success, the caller should destroy it via
|
|
|
|
/// posix_spawnattr_destroy.
|
|
|
|
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);
|
2012-08-15 15:57:56 +08:00
|
|
|
#endif
|
|
|
|
|
2012-02-28 11:20:27 +08:00
|
|
|
#endif
|