hwdet::ForkAndPipe Class Reference

Fork process and automatically create pipes. More...

#include <ForkAndPipe.h>

Inheritance diagram for hwdet::ForkAndPipe:

hwdet::MessageReporter hwdet::EqueryPackager::EqueryExec hwdet::GentooFindPackager::FindExec hwdet::RPMPackager::RPMExec

Public Member Functions

 ForkAndPipe (const String &rep_name, MessageReporter *parent_reporter=NULL)
 Constructor.
virtual ~ForkAndPipe (void)
 Destructor.
bool Run (void)
 Fork current process and create appropriate pipes.
virtual void ParentProcess (pid_t child_pid, FILE *file)=0
 Method called in parent process.
virtual void ChildProcess (void)=0
 Method called in the child process.
virtual void OnError (int error_code, const String &message)
 Error handler.

Protected Member Functions

pid_t GetChildPID (void) const
 Get PID of the child process created.
void WaitForChildProcess (void)
 Wait for children process to end.
int GetForkError (void) const
 Get error code after fork.
bool AutoError (int result, const String &message="")
 Test if an error occured and output appropriate message.

Private Member Functions

 ForkAndPipe (const ForkAndPipe &)
 Empty copy ctor, this class should not be copied.
ForkAndPipeoperator= (const ForkAndPipe &)
 Private assignment, never defined -> prevents copying.

Private Attributes

int pipe_desc [2]
 Pipe descriptors.
pid_t pid
 PID of the created child process.
int error_num
 Error number (after fork).

Detailed Description

Fork process and automatically create pipes.


Constructor & Destructor Documentation

hwdet::ForkAndPipe::ForkAndPipe ( const String rep_name,
MessageReporter parent_reporter = NULL 
)

Constructor.

Parameters:
rep_name Name of the message reporter.
parent_reporter Parent message reporter. If NULL, messages are output directly to the stdout.

hwdet::ForkAndPipe::~ForkAndPipe ( void   )  [virtual]

Destructor.

hwdet::ForkAndPipe::ForkAndPipe ( const ForkAndPipe  )  [inline, private]

Empty copy ctor, this class should not be copied.


Member Function Documentation

bool hwdet::ForkAndPipe::AutoError ( int  result,
const String message = "" 
) [protected]

Test if an error occured and output appropriate message.

If an error occured calls OnError handler with given message.

Parameters:
result Return value of the syscall or library fuction. Negative value means that the call failed.
message Message that will be forwarded to the OnError call. Empty string means no message (default value).
Returns:
true if no error occured, false if an error occured prior to the call.

virtual void hwdet::ForkAndPipe::ChildProcess ( void   )  [pure virtual]

Method called in the child process.

This method is called in the child process. You can exec any process you want in it and output will be sent through the pipe to the parent process.

Implemented in hwdet::EqueryPackager::EqueryExec, hwdet::GentooFindPackager::FindExec, and hwdet::RPMPackager::RPMExec.

pid_t hwdet::ForkAndPipe::GetChildPID ( void   )  const [inline, protected]

Get PID of the child process created.

Returns:
PID of the child process created.

int hwdet::ForkAndPipe::GetForkError ( void   )  const [inline, protected]

Get error code after fork.

Call this to get error code after the fork has been called. Note that this does not represent current error code, so it will not change if other error occurs.

void hwdet::ForkAndPipe::OnError ( int  error_code,
const String message 
) [virtual]

Error handler.

Basic error handler. Does nothing but print error name to the stdout.

Parameters:
error_code Error code (value of the errno).
message Message that will be printed alongside the error name. If message is empty, only error name will be output.

ForkAndPipe& hwdet::ForkAndPipe::operator= ( const ForkAndPipe  )  [private]

Private assignment, never defined -> prevents copying.

virtual void hwdet::ForkAndPipe::ParentProcess ( pid_t  child_pid,
FILE *  file 
) [pure virtual]

Method called in parent process.

This method is called in parent process (parent is process from which run has been called). In this method you can read output produced by the child process(es) using pipe descriptors created in the run method. You do not need to wait for the child to terminate, it is done automatically.

Implemented in hwdet::EqueryPackager::EqueryExec, hwdet::GentooFindPackager::FindExec, and hwdet::RPMPackager::RPMExec.

bool hwdet::ForkAndPipe::Run ( void   ) 

Fork current process and create appropriate pipes.

This will create new pipe and fork current process. In child it will call method ChildProcess. In parent process, it will call ParentProcess method. In case of error, error handler is called.

Returns:
true on success, false otherwise.

void hwdet::ForkAndPipe::WaitForChildProcess ( void   )  [inline, protected]

Wait for children process to end.

This will wait until child process terminates. Do NOT call this in the child process. You should not call this in parent either since it is called automatically when parent method ends.


Field Documentation

int hwdet::ForkAndPipe::error_num [private]

Error number (after fork).

pid_t hwdet::ForkAndPipe::pid [private]

PID of the created child process.

int hwdet::ForkAndPipe::pipe_desc[2] [private]

Pipe descriptors.


The documentation for this class was generated from the following files:
Generated on Tue Dec 19 17:43:51 2006 for Detector for Linux by  doxygen 1.4.7