Ninja
Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
Subprocess Struct Reference

Subprocess wraps a single async subprocess. More...

#include <subprocess.h>

Public Member Functions

bool Done () const
 
ExitStatus Finish ()
 Returns ExitSuccess on successful process exit, ExitInterrupted if the process was interrupted, ExitFailure if it otherwise failed. More...
 
const std::string & GetOutput () const
 
 ~Subprocess ()
 

Private Member Functions

void OnPipeReady ()
 
bool Start (struct SubprocessSet *set, const std::string &command)
 
 Subprocess (bool use_console)
 
bool TryFinish (int waitpid_options)
 Call waitpid() on the subprocess with the provided options and update the pid_ and exit_status_ fields. More...
 

Private Attributes

std::string buf_
 
ExitStatus exit_status_
 In POSIX platforms it is necessary to use waitpid(WNOHANG) to know whether a certain subprocess has finished. More...
 
int fd_
 The file descriptor that will be used in ppoll/pselect() for this process, if any. More...
 
pid_t pid_
 PID of the subprocess. Set to -1 when the subprocess is reaped. More...
 
bool use_console_
 

Friends

struct SubprocessSet
 

Detailed Description

Subprocess wraps a single async subprocess.

It is entirely passive: it expects the caller to notify it when its fds are ready for reading, as well as call Finish() to reap the child once done() is true.

Definition at line 42 of file subprocess.h.

Constructor & Destructor Documentation

◆ ~Subprocess()

Subprocess::~Subprocess ( )

Definition at line 48 of file subprocess-posix.cc.

References fd_, Finish(), and pid_.

◆ Subprocess()

Subprocess::Subprocess ( bool  use_console)
private

Definition at line 44 of file subprocess-posix.cc.

Member Function Documentation

◆ Done()

bool Subprocess::Done ( ) const

Definition at line 214 of file subprocess-posix.cc.

References fd_, pid_, and use_console_.

Referenced by TEST_F().

◆ Finish()

ExitStatus Subprocess::Finish ( )

Returns ExitSuccess on successful process exit, ExitInterrupted if the process was interrupted, ExitFailure if it otherwise failed.

Definition at line 178 of file subprocess-posix.cc.

References exit_status_, pid_, and TryFinish().

Referenced by TEST_F(), RealCommandRunner::WaitForCommand(), and ~Subprocess().

◆ GetOutput()

const string & Subprocess::GetOutput ( ) const

Definition at line 222 of file subprocess-posix.cc.

References buf_.

Referenced by TEST_F(), and RealCommandRunner::WaitForCommand().

◆ OnPipeReady()

void Subprocess::OnPipeReady ( )
private

Definition at line 150 of file subprocess-posix.cc.

References buf_, Fatal(), and fd_.

◆ Start()

bool Subprocess::Start ( struct SubprocessSet set,
const std::string &  command 
)
private

Definition at line 56 of file subprocess-posix.cc.

References environ, Fatal(), fd_, SubprocessSet::old_mask_, pid_, SetCloseOnExec(), and use_console_.

Referenced by SubprocessSet::Add().

◆ TryFinish()

bool Subprocess::TryFinish ( int  waitpid_options)
private

Call waitpid() on the subprocess with the provided options and update the pid_ and exit_status_ fields.

Return a boolean indicating whether the subprocess has indeed terminated.

Definition at line 164 of file subprocess-posix.cc.

References exit_status_, Fatal(), and pid_.

Referenced by Finish().

Friends And Related Function Documentation

◆ SubprocessSet

friend struct SubprocessSet
friend

Definition at line 95 of file subprocess.h.

Member Data Documentation

◆ buf_

std::string Subprocess::buf_
private

Definition at line 58 of file subprocess.h.

Referenced by GetOutput(), and OnPipeReady().

◆ exit_status_

ExitStatus Subprocess::exit_status_
private

In POSIX platforms it is necessary to use waitpid(WNOHANG) to know whether a certain subprocess has finished.

This is done for terminal subprocesses. However, this also causes the subprocess to be reaped before Finish() is called, so we need to store the ExitStatus so that a later Finish() invocation can return it.

Definition at line 86 of file subprocess.h.

Referenced by Finish(), and TryFinish().

◆ fd_

int Subprocess::fd_
private

The file descriptor that will be used in ppoll/pselect() for this process, if any.

Otherwise -1. In non-console mode, this is the read-side of a pipe that was created specifically for this subprocess. The write-side of the pipe is given to the subprocess as combined stdout and stderr. In console mode no pipe is created: fd_ is -1, and process termination is detected using the SIGCHLD signal and waitpid(WNOHANG).

Definition at line 78 of file subprocess.h.

Referenced by Done(), OnPipeReady(), Start(), and ~Subprocess().

◆ pid_

pid_t Subprocess::pid_
private

PID of the subprocess. Set to -1 when the subprocess is reaped.

Definition at line 80 of file subprocess.h.

Referenced by Done(), Finish(), Start(), TryFinish(), and ~Subprocess().

◆ use_console_

bool Subprocess::use_console_
private

Definition at line 93 of file subprocess.h.

Referenced by Done(), and Start().


The documentation for this struct was generated from the following files: