Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahDaemonize.H File Reference

Functionality for daemonizing a process. More...

#include <syslog.h>
#include <stdexcept>
Include dependency graph for ahDaemonize.H:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void daemonize (const char *program_name, int facility=LOG_DAEMON)
 Converts the calling process into a Unix daemon.
 

Detailed Description

Functionality for daemonizing a process.

Author
Leandro Rabindranath León

Definition in file ahDaemonize.H.

Function Documentation

◆ daemonize()

void daemonize ( const char *  program_name,
int  facility = LOG_DAEMON 
)

Converts the calling process into a Unix daemon.

Parameters
program_nameName of the program (for syslog identification)
facilityThe syslog facility to use (default: LOG_DAEMON)
Exceptions
std::runtime_errorif daemonization fails
std::invalid_argumentif program_name is null or empty
Note
Based on daemon_init from "The Unix Network Programming" by Richard Stevens
This function will:
  • Fork and terminate the parent process
  • Create a new session
  • Change the working directory to root
  • Clear the file mode creation mask
  • Close all open file descriptors
  • Redirect stdin, stdout, and stderr to /dev/null
  • Initialize syslog logging
Warning
This function should be called early in program execution
After calling this function, the process will be running in the background
#include <ahDaemonize.H>
int main(int argc, char *argv[]) {
try {
daemonize("my_daemon", LOG_DAEMON);
// Daemon code here
} catch (const std::exception& e) {
syslog(LOG_ERR, "Failed to daemonize: %s", e.what());
return 1;
}
return 0;
}
void daemonize(const char *program_name, int facility)
Converts the calling process into a Unix daemon.
Definition ahDaemonize.C:78
Functionality for daemonizing a process.
int main()

Definition at line 78 of file ahDaemonize.C.

References ah_invalid_argument_if, ah_runtime_error_if, and Aleph::daemonized.