Rudiments
Public Member Functions | Static Public Member Functions
daemonprocess Class Reference

List of all members.

Public Member Functions

 daemonprocess ()
virtual ~daemonprocess ()
bool detach () const
int32_t runAsUser (const char *username) const
int32_t runAsGroup (const char *groupname) const
int32_t runAsUserId (uid_t uid) const
int32_t runAsGroupId (gid_t gid) const

Static Public Member Functions

static int64_t checkForPidFile (const char *filename)
static bool createPidFile (const char *filename, mode_t permissions)
static void handleShutDown (void(*shutdownfunction)(int32_t))
static void handleCrash (void(*crashfunction)(int32_t))
static void waitForChildren ()
static void dontWaitForChildren ()

Detailed Description

The daemonprocess class provides methods that are useful to daemons.

Daemons are long running processes which often detach themselves from the controlling terminal and run in the background. They are frequently started at boot time and run in the background.

Daemons typically perform "housecleaning" tasks or serve data to client programs. See the server class.


Constructor & Destructor Documentation

daemonprocess::daemonprocess ( )

Creates an instance of the daemonprocess class.

virtual daemonprocess::~daemonprocess ( ) [virtual]

Deletes this instance of the daemonprocess class.


Member Function Documentation

static int64_t daemonprocess::checkForPidFile ( const char *  filename) [static]

Checks for filename "filename" and reads the process id out of it, if it exists. Returns the process id on success or -1 on failure.

static bool daemonprocess::createPidFile ( const char *  filename,
mode_t  permissions 
) [static]

Create's file "filename" with permissions "permissions" and puts the current process id in it. Note that when you delete this file during shutdown you must use the full pathname since the detach() method below changes directories to "/". Returns true on success and false on failure.

bool daemonprocess::detach ( ) const

Detach from the controlling terminal and process and run in the background. Also change directories to "/" and set the file creation mask such that all files are created -rw-rw-rw and all directories drwxrwxrwx.

static void daemonprocess::dontWaitForChildren ( ) [static]

This method causes the daemon not to wait on child processes which have exited. Ordinarily, you'd want to wait on child processes, but this interferes with the behavior of WEXITSTATUS() after a call to system() (and possibly other calls). This method allows you to disable waiting on child processes.

static void daemonprocess::handleCrash ( void(*)(int32_t)  crashfunction) [static]

Allows you to designate a function to run if the daemon crashes.

static void daemonprocess::handleShutDown ( void(*)(int32_t)  shutdownfunction) [static]

Allows you to designate a function to run when the daemon is killed.

int32_t daemonprocess::runAsGroup ( const char *  groupname) const

Causes the daemon to run as a different group than the one that started the process. It has no effect unless the process is started by the root user. It returns 1 on success, 0 on failure and -1 on error.

Note that this method uses the groupentry class. If you are using it in a multithreaded application, you may need to supply the groupentry class a mutex. See groupentry.h for more detail.

int32_t daemonprocess::runAsGroupId ( gid_t  gid) const

Causes the daemon to run as a different group than the one that started the process. It has no effect unless the process is started by the root user. It returns 1 on success, 0 on failure and -1 on error.

int32_t daemonprocess::runAsUser ( const char *  username) const

Causes the daemon to run as a different user than the one that started the process. It has no effect unless the process is started by the root user. It returns 1 on success, 0 on failure and -1 on error.

Note that this method uses the passwdentry class. If you are using it in a multithreaded application, you may need to supply the passwdentry class a mutex. See passwdentry.h for more detail.

int32_t daemonprocess::runAsUserId ( uid_t  uid) const

Causes the daemon to run as a different user than the one that started the process. It has no effect unless the process is started by the root user. It returns 1 on success, 0 on failure and -1 on error.

static void daemonprocess::waitForChildren ( ) [static]

This method causes the daemon to wait on child processes which have exited, preventing so-called "zombie" processes from occurring. This method is called in the constructor and is thus the default behavior of this class.