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 <string.h> // 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
56void redirect_standard_fds()
57{
58 const int null_fd = open("/dev/null", O_RDWR);
59 ah_runtime_error_if(null_fd == -1) << "Cannot open /dev/null";
60
61 // Redirect standard file descriptors to /dev/null
62 ah_runtime_error_if(dup2(null_fd, STDIN_FILENO) != STDIN_FILENO)
63 << "Cannot redirect stdin";
64
65 ah_runtime_error_if(dup2(null_fd, STDOUT_FILENO) != STDOUT_FILENO)
66 << "Cannot redirect stdout";
67
68 ah_runtime_error_if(dup2(null_fd, STDERR_FILENO) != STDERR_FILENO)
69 << "Cannot redirect stderr";
70
71 // Close the original file descriptor if it's not a standard one
72 if (null_fd > STDERR_FILENO)
73 close(null_fd);
74}
75
76} // anonymous namespace
77
78void daemonize(const char *program_name, int facility)
79{
80 // Input validation
81 ah_invalid_argument_if(program_name == nullptr or *program_name == '\0')
82 << "Program name cannot be null or empty";
83
84 // First fork to detach from parent
85 pid_t pid = fork();
86 ah_runtime_error_if(pid < 0) << "First fork failed";
87
88 if (pid > 0)
89 _exit(0); // Parent exits
90
91 // Create new session and become session leader
92 ah_runtime_error_if(setsid() < 0) << "setsid failed";
93
94 // Handle SIGHUP to prevent termination when the terminal is closed
95 Signal signalHandler(SIGHUP, SIG_IGN);
96
97 // Second fork to ensure we're not a session leader
98 pid = fork();
99 ah_runtime_error_if(pid < 0) << "Second fork failed";
100
101 if (pid > 0)
102 _exit(0); // First child exits
103
104 // Change working directory to root to avoid keeping directories in use
105 ah_runtime_error_if(chdir("/") < 0) << "Cannot change directory to root";
106
107 // Clear file mode creation mask to allow creation of files with any permissions
108 umask(0);
109
110 // Close all open file descriptors
111 for (int i = 0; i < MAXIMUM_FILE_DESC; i++)
112 close(i);
113
114 // Redirect standard file descriptors to /dev/null
115 redirect_standard_fds();
116
117 // Initialize syslog for logging
118 openlog(program_name, LOG_PID | LOG_NDELAY, facility);
119
120 // Set daemonized flag (if Aleph::daemonized is defined)
121 #ifdef ALEPH_DAEMONIZED_FLAG
122 Aleph::daemonized = true;
123 #endif
124}
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:266
#define ah_invalid_argument_if(C)
Throws std::invalid_argument if condition holds.
Definition ah-errors.H:639
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:78
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:41