Skip to content
Snippets Groups Projects
Commit 08f20cc3 authored by Zámolyi Csaba Ferenc's avatar Zámolyi Csaba Ferenc
Browse files

Added basic daemonize

parent ea50784b
No related branches found
No related tags found
No related merge requests found
cmake-build-debug/
.idea/
\ No newline at end of file
cmake_minimum_required(VERSION 3.25)
project(batteryd)
set(CMAKE_CXX_STANDARD 20)
add_executable(batteryd src/main.cpp src/daemonutils.cpp)
/*
* Created by: Cshee, 2023-02-08
* Project: SystemCshee / batteryd
* License: GNU GPL v3.0
*/
#include <cstdlib>
#include <unistd.h>
#include <csignal>
#include <sys/types.h>
#include <sys/stat.h>
#include <syslog.h>
#include <cstdio>
static void daemonize() {
// First fork to lose parent to PID1, then create own session
pid_t pid;
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
if (setsid() < 0)
exit(EXIT_FAILURE);
// Second fork to lose session control and prevent controlling tty
pid = fork();
if (pid < 0)
exit(EXIT_FAILURE);
if (pid > 0)
exit(EXIT_SUCCESS);
// Set new file permissions and work directory
umask(0);
chdir("/");
// Close all open file handles, then assign /dev/null to stdin, -out, -err
for (int i = 0; i < sysconf(_SC_OPEN_MAX); ++i) close(i);
stdin = fopen("/dev/null", "r");
stdout = fopen("/dev/null", "w+");
stderr = fopen("/dev/null", "w+");
}
static void setup() {
signal(SIGCHLD, SIG_IGN);
signal(SIGHUP, SIG_IGN);
/* Open the log file */
openlog("systemcshee-batteryd", LOG_PID, LOG_DAEMON);
}
\ No newline at end of file
#include <iostream>
int main() {
std::cout << "Hello, World!" << std::endl;
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment