My Project
|
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... | |
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.
|
static |
Create a Daemon instance.
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.
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.
|
noexcept |
Causes daemonize_me() to set the umask.
mask | The 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.
working_directory | The working directory for the daemon process. |
SyscallException | The process could not change the working directory to the specified directory. |