My Project
unity::util::Daemon Class Referencefinal

Helper class to turn a process into a daemon. More...

#include <unity/util/Daemon.h>

Public Member Functions

void close_fds () noexcept
 Causes daemonize_me() to close all open file descriptors other than the standard file descriptors (which are connected /dev/null).
 
void reset_signals () noexcept
 Causes daemonize_me() to reset all signals to their default behavior.
 
void set_umask (mode_t mask) noexcept
 Causes daemonize_me() to set the umask. More...
 
void set_working_directory (std::string const &working_directory)
 Causes daemonize_me() to set the working directory. More...
 
void daemonize_me ()
 Turns the calling process into a daemon. More...
 

Static Public Member Functions

static UPtr create ()
 Create a Daemon instance. More...
 

Detailed Description

Helper class to turn a process into a daemon.

To turn a process into a daemon, instantiate this class and call daemonize_me().

The new process becomes a session leader without a control terminal. The standard file descriptors (stdin, stdout, and stderr) are closed and re-opened to /dev/null.

By default, any file descriptors (other than the standard three) that are open in the process remain open to the same destinations in the daemon. If you want to have other descriptors closed, call close_fds() before calling daemonize_me(). This will close all file descriptors > 2.

By default, the signal disposition of the daemon is unchanged. To reset all signals to their default disposition, call reset_signals() before calling daemonize_me().

By default, the umask of the daemon is unchanged. To set a different umask, call set_umask() before calling daemonize_me().

By default, the working directory of the daemon is unchanged. To run the daemon with a different working directory, call set_working_dir() before calling daemonize_me(). Note that the working directory should be set to a path that is in the root file system. If the working directory is in any other file system, that file system cannot be unmounted while the daemon is running.

Note: This class is not async signal-safe. Do not call daemonize_me() from a a signal handler.

Member Function Documentation

Daemon::UPtr unity::util::Daemon::create ( )
static

Create a Daemon instance.

Returns
A unique_ptr to the instance.
void unity::util::Daemon::daemonize_me ( )

Turns the calling process into a daemon.

By default, daemonize_me() leaves open file descriptors, signal disposition, umask, and working directory unchanged. Call the corresponding member function before calling daemonize_me() to change this behavior as appropriate.

Note
Calling daemonize_me() more than once is safe; any changes to file descriptors, signal disposition, umask, or working directory as requested by calling the other member functions will be correctly set for the calling process. However, daemonize_me() is not a cheap call because it calls fork(); the normal use pattern is to create a Daemon instance, select the desired settings, call daemonize_me(), and let the instance go out of scope.
void unity::util::Daemon::set_umask ( mode_t  mask)
noexcept

Causes daemonize_me() to set the umask.

Parameters
maskThe umask for the daemon process.
void unity::util::Daemon::set_working_directory ( std::string const &  working_directory)

Causes daemonize_me() to set the working directory.

Parameters
working_directoryThe working directory for the daemon process.
Exceptions
SyscallExceptionThe process could not change the working directory to the specified directory.
Note
Daemon processes should set their working to "/" or to a directory that is part of the root file system. Otherwise, the file system containing the daemon's working directory cannot be unmounted without first killing the daemon process.

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