go-daemon/c-daemon/daemon.c

27 lines
No EOL
580 B
C

#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);
}
if (setsid() == -1){
return -1;
}
return 0;
}