setup daemon
This commit is contained in:
3
Makefile
3
Makefile
@ -6,6 +6,9 @@ hdb:
|
|||||||
gcc -ansi -O2 -pipe -o build/hdb src/main.c src/db.c -lsqlite3
|
gcc -ansi -O2 -pipe -o build/hdb src/main.c src/db.c -lsqlite3
|
||||||
strip build/hdb
|
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
|
.PHONY: install uninstall
|
||||||
|
|
||||||
install: hdb
|
install: hdb
|
||||||
|
38
src/hdbd.c
Normal file
38
src/hdbd.c
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
#include <strops.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user