setup daemon
This commit is contained in:
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