75 void Daemon::
startDaemon(
const std::string& pidFileName,
const std::string& userName,
const std::string& groupName) {
76 if (std::filesystem::exists(pidFileName)) {
77 throw DaemonFailure("Pid file '" + pidFileName +
"' exists. Daemon already running?");
97 if (signal(SIGHUP, SIG_IGN) == SIG_ERR) {
110 std::ofstream pidFile(pidFileName, std::ofstream::out);
112 if (!pidFile.good()) {
116 pidFile << pid << std::endl;
122 const struct passwd* pw =
nullptr;
123 const struct group* gr =
nullptr;
125 if (((
void) (errno = 0), gr = getgrnam(groupName.c_str())) ==
nullptr) {
131 if (setegid(gr->gr_gid) != 0) {
134 if (((
void) (errno = 0), (pw = getpwnam(userName.c_str())) ==
nullptr)) {
140 if (seteuid(pw->pw_uid) != 0) {
144 if (chdir(
"/") != 0) {
149 close(STDOUT_FILENO);
150 close(STDERR_FILENO);
152 if (std::freopen(
"/dev/null",
"r", stdin) ==
nullptr) {
154 if (std::freopen(
"/dev/null",
"w+", stdout) ==
nullptr) {
156 if (std::freopen(
"/dev/null",
"w+", stderr) ==
nullptr) {
161 if (pidFileName.empty()) {
164 std::ifstream pidFile(pidFileName, std::ifstream::in);
166 if (!pidFile.good()) {
174 const int pidfd =
static_cast<
int>(syscall(SYS_pidfd_open, pid, 0));
180 if (kill(pid, SIGTERM) != 0) {
183 struct pollfd pollfd{};
185 pollfd.events = POLLIN;
187 const int ready = core::system::poll(&pollfd, 1, 5000);