From 31dc4671659573ebef04c8ad3ca3da55ed20b4f4 Mon Sep 17 00:00:00 2001 From: AustrianToast Date: Sun, 1 Jun 2025 22:40:56 +0200 Subject: [PATCH] setup daemon --- Makefile | 3 +++ src/hdbd.c | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/hdbd.c diff --git a/Makefile b/Makefile index 01c11ec..de9db0c 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,9 @@ hdb: gcc -ansi -O2 -pipe -o build/hdb src/main.c src/db.c -lsqlite3 strip build/hdb +hdbd: src/db.c src/hdbd.c + gcc -ansi -o build/hdbd src/hdbd.c src/db.c -lsqlite3 -lstrops + .PHONY: install uninstall install: hdb diff --git a/src/hdbd.c b/src/hdbd.c new file mode 100644 index 0000000..284e4a4 --- /dev/null +++ b/src/hdbd.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +int main() { + /* Setup daemon */ + pid_t pid; + pid_t sid; + + pid = fork(); + if (pid == -1) { + fprintf(stderr, "Couldn't fork\n"); + return 1; + } else if (pid > 0) { + printf("Pid of child is %d\n", pid); + return 0; + } + + sid = setsid(); + if (sid == -1) { + return 1; + } + + if (chdir("/") == -1) { + return 1; + } + + close(STDIN_FILENO); + close(STDOUT_FILENO); + close(STDERR_FILENO); + + /* Actual program */ + char should_close = 0; + while(!should_close) { + sleep(1); + } +}