go-daemon/c-daemon/daemon.c

27 lines
580 B
C
Raw Normal View History

2023-07-26 10:28:17 +03:00
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/fs.h>
#include <linux/limits.h>
int _daemon(int nochdir, int noclose){
pid_t pid;
// pid_t defined in sys/types.h
// pid_t is a signed int data type
pid = fork(); // fork of the parent process
// fork() function defined in unistd.h
if (pid < 0){
exit(EXIT_FAILURE); // exit and EXIT_FAILURE defined in stdlib.h
}
else if (pid > 0){
exit(EXIT_SUCCESS);
2023-07-26 10:28:17 +03:00
}
2023-07-26 11:23:33 +03:00
if (setsid() == -1){
return -1;
}
2023-07-26 10:28:17 +03:00
return 0;
}