13 #ifndef __PROCESS_SUBPROCESS_HPP__
14 #define __PROCESS_SUBPROCESS_HPP__
118 const std::string& path,
119 std::vector<std::string> argv,
125 const Option<lambda::function<
126 pid_t(
const lambda::function<
int()>&)>>&
clone,
127 const std::vector<Subprocess::ParentHook>& parent_hooks,
128 const std::vector<Subprocess::ChildHook>& child_hooks);
138 lambda::function<Try<InputFileDescriptors>()> input;
143 lambda::function<Try<OutputFileDescriptors>()> output;
176 #endif // __WINDOWS__
213 #endif // __WINDOWS__
230 const lambda::function<Try<Nothing>()> child_setup;
235 static IO
PATH(
const std::string& path);
298 const std::string& path,
299 std::vector<std::string> argv,
305 const Option<lambda::function<
306 pid_t(
const lambda::function<
int()>&)>>&
clone,
307 const std::vector<Subprocess::ParentHook>& parent_hooks,
308 const std::vector<Subprocess::ChildHook>& child_hooks);
314 if (in.isSome()) {
os::close(in.get()); }
315 if (out.isSome()) {
os::close(out.get()); }
316 if (err.isSome()) {
os::close(err.get()); }
323 #endif // __WINDOWS__
333 Future<Option<int>>
status;
336 Subprocess() : data(new Data()) {}
338 std::shared_ptr<Data> data;
370 const std::string& path,
371 std::vector<std::string> argv,
377 const Option<lambda::function<
379 const std::vector<Subprocess::ParentHook>& parent_hooks = {},
380 const std::vector<Subprocess::ChildHook>& child_hooks = {});
408 const std::string& command,
413 const Option<lambda::function<
415 const std::vector<Subprocess::ParentHook>& parent_hooks = {},
416 const std::vector<Subprocess::ChildHook>& child_hooks = {})
435 #endif // __PROCESS_SUBPROCESS_HPP__
Option< int_fd > in() const
Definition: subprocess.hpp:248
Try< Subprocess > subprocess(const std::string &path, std::vector< std::string > argv, const Subprocess::IO &in=Subprocess::FD(STDIN_FILENO), const Subprocess::IO &out=Subprocess::FD(STDOUT_FILENO), const Subprocess::IO &err=Subprocess::FD(STDERR_FILENO), const flags::FlagsBase *flags=nullptr, const Option< std::map< std::string, std::string >> &environment=None(), const Option< lambda::function< pid_t(const lambda::function< int()> &)>> &clone=None(), const std::vector< Subprocess::ParentHook > &parent_hooks={}, const std::vector< Subprocess::ChildHook > &child_hooks={})
Forks a subprocess and execs the specified 'path' with the specified 'argv', redirecting stdin...
constexpr const char * arg1
Definition: shell.hpp:43
Try< pid_t > clone(pid_t target, int nstypes, const lambda::function< int()> &f, int flags)
Performs an os::clone after entering a set of namespaces for the specified target process...
Subprocess::IO::OutputFileDescriptors OutputFileDescriptors
Definition: subprocess.hpp:342
A ChildHook can be passed to a subprocess call.
Definition: subprocess.hpp:189
static ChildHook DUP2(int oldFd, int newFd)
ChildHook for duplicating a file descriptor.
static ChildHook SUPERVISOR()
ChildHook for starting a Supervisor process monitoring and killing the child process if the parent pr...
static IO PATH(const std::string &path)
The file descriptor is not duplicated before being passed to the subprocess.
Definition: subprocess.hpp:110
For output file descriptors a child writes to the write file descriptor and a parent may read from th...
Definition: subprocess.hpp:88
pid_t pid() const
Definition: subprocess.hpp:241
static ChildHook CHDIR(const std::string &working_directory)
ChildHook for changing the working directory.
#define STDERR_FILENO
Definition: windows.hpp:161
Subprocess::IO::InputFileDescriptors InputFileDescriptors
Definition: subprocess.hpp:341
constexpr const char * arg0
Definition: shell.hpp:42
Represents a fork() exec()ed subprocess.
Definition: subprocess.hpp:43
Environment * environment
DWORD pid_t
Definition: windows.hpp:187
static IO FD(int_fd fd, IO::FDType type=IO::DUPLICATED)
#define STDOUT_FILENO
Definition: windows.hpp:160
Option< int_fd > err() const
Definition: subprocess.hpp:268
int_fd write
Definition: subprocess.hpp:91
Try< Nothing > close(int fd)
Definition: close.hpp:24
friend Try< Subprocess > subprocess(const std::string &path, std::vector< std::string > argv, const Subprocess::IO &in, const Subprocess::IO &out, const Subprocess::IO &err, const flags::FlagsBase *flags, const Option< std::map< std::string, std::string >> &environment, const Option< lambda::function< pid_t(const lambda::function< int()> &)>> &clone, const std::vector< Subprocess::ParentHook > &parent_hooks, const std::vector< Subprocess::ChildHook > &child_hooks)
Describes how the I/O is redirected for stdin/stdout/stderr.
Definition: subprocess.hpp:61
ParentHook(const lambda::function< Try< Nothing >(pid_t)> &_parent_setup)
The file descriptor is duplicated before being passed to the subprocess.
Definition: subprocess.hpp:103
#define STDIN_FILENO
Definition: windows.hpp:159
Option< int_fd > out() const
Definition: subprocess.hpp:258
const lambda::function< Try< Nothing >pid_t)> parent_setup
The callback that must be specified for execution after the child has been cloned, but before it starts executing the new process.
Definition: subprocess.hpp:162
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
#define flags
Definition: decoder.hpp:18
FDType
Describes the lifecycle of a file descriptor passed into a subprocess via the Subprocess::FD helper...
Definition: subprocess.hpp:98
Try< uint32_t > type(const std::string &path)
Option< int_fd > read
Definition: subprocess.hpp:90
A hook can be passed to a subprocess call.
Definition: subprocess.hpp:151
int int_fd
Definition: int_fd.hpp:35
constexpr const char * name
Definition: shell.hpp:41
Future< Option< int > > status() const
Exit status of this subprocess captured as a Future (completed when the subprocess exits)...
Definition: subprocess.hpp:294
Try< Nothing > operator()() const
Definition: subprocess.hpp:225
friend Try< Subprocess > subprocess(const std::string &path, std::vector< std::string > argv, const Subprocess::IO &in, const Subprocess::IO &out, const Subprocess::IO &err, const flags::FlagsBase *flags, const Option< std::map< std::string, std::string >> &environment, const Option< lambda::function< pid_t(const lambda::function< int()> &)>> &clone, const std::vector< Subprocess::ParentHook > &parent_hooks, const std::vector< Subprocess::ChildHook > &child_hooks)
static ChildHook SETSID()
ChildHook for generating a new session id.
Definition: future.hpp:57
static ChildHook UNSET_CLOEXEC(int fd)
ChildHook to unset CLOEXEC on a file descriptor.