lomiri::util::Daemon Class Referencefinal

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

`#include <lomiri/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

## create()

Daemon::UPtr lomiri::util::Daemon::create | ( |  | ) |

static

Create a Daemon instance.

Returns
  A `unique_ptr` to the instance.

## daemonize_me()

void lomiri::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.

## set_umask()

void lomiri::util::Daemon::set_umask | ( | mode_t | mask | ) |

noexcept

Causes daemonize_me() to set the umask.

Parameters

  mask | The umask for the daemon process.

## set_working_directory()

void lomiri::util::Daemon::set_working_directory | ( | std::string const & | working_directory | ) |

Causes daemonize_me() to set the working directory.

Parameters

  working_directory | The working directory for the daemon process.

Exceptions

  SyscallException | The 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:

- include/lomiri/util/Daemon.h
- src/lomiri/util/Daemon.cpp