Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ahDaemonize.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
39#include <unistd.h>
40#include <fcntl.h>
41#include <sys/stat.h>
42#include <sys/types.h>
43# include <cstring> // for strerror
44#include <ah-signal.H> // for Signal
45#include <ah-errors.H> // for error handling macros
46#include <ahDaemonize.H>
47
48namespace {
49
50constexpr int MAXIMUM_FILE_DESC = 256;
51
52void close_nonstandard_fd(const int fd) noexcept
53{
54 if (fd > STDERR_FILENO)
55 close(fd);
56}
57
58void dup2_or_throw(const int null_fd, const int target_fd, const char *name)
59{
60 if (dup2(null_fd, target_fd) == target_fd)
61 return;
62
63 close_nonstandard_fd(null_fd);
64 ah_runtime_error() << "Cannot redirect " << name;
65}
66
71void redirect_standard_fds()
72{
73 const int null_fd = open("/dev/null", O_RDWR);
74 ah_runtime_error_if(null_fd == -1) << "Cannot open /dev/null";
75
76 // Redirect standard file descriptors to /dev/null
77 dup2_or_throw(null_fd, STDIN_FILENO, "stdin");
78 dup2_or_throw(null_fd, STDOUT_FILENO, "stdout");
79 dup2_or_throw(null_fd, STDERR_FILENO, "stderr");
80
81 // Close the original file descriptor if it's not a standard one
82 close_nonstandard_fd(null_fd);
83}
84
85} // anonymous namespace
86
87void daemonize(const char *program_name, int facility)
88{
89 // Input validation
90 ah_invalid_argument_if(program_name == nullptr or *program_name == '\0')
91 << "Program name cannot be null or empty";
92
93 // First fork to detach from parent
94 pid_t pid = fork();
95 ah_runtime_error_if(pid < 0) << "First fork failed";
96
97 if (pid > 0)
98 _exit(0); // Parent exits
99
100 // Create new session and become session leader
101 ah_runtime_error_if(setsid() < 0) << "setsid failed";
102
103 // Handle SIGHUP to prevent termination when the terminal is closed
104 Signal signalHandler(SIGHUP, SIG_IGN);
105
106 // Second fork to ensure we're not a session leader
107 pid = fork();
108 ah_runtime_error_if(pid < 0) << "Second fork failed";
109
110 if (pid > 0)
111 _exit(0); // First child exits
112
113 // Change working directory to root to avoid keeping directories in use
114 ah_runtime_error_if(chdir("/") < 0) << "Cannot change directory to root";
115
116 // Clear file mode creation mask to allow creation of files with any permissions
117 umask(0);
118
119 // Close all open file descriptors
120 for (int i = 0; i < MAXIMUM_FILE_DESC; i++)
121 close(i);
122
123 // Redirect standard file descriptors to /dev/null
124 redirect_standard_fds();
125
126 // Initialize syslog for logging
127 openlog(program_name, LOG_PID | LOG_NDELAY, facility);
128
129 // Set daemonized flag (if Aleph::daemonized is defined)
130 #ifdef ALEPH_DAEMONIZED_FLAG
131 Aleph::daemonized = true;
132 #endif
133}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error()
Throws std::runtime_error unconditionally.
Definition ah-errors.H:287
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:271
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:644
POSIX signal handling utilities with RAII semantics.
void daemonize(const char *program_name, int facility)
Converts the calling process into a Unix daemon.
Definition ahDaemonize.C:87
Functionality for daemonizing a process.
RAII wrapper for POSIX signal handler registration.
Definition ah-signal.H:354
bool daemonized
Flag indicating if the process is running as a daemon.
Definition ahDefs.C:40