48 lines
1.1 KiB
C
48 lines
1.1 KiB
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>
|
||
|
#define NR_OPEN 1024
|
||
|
|
||
|
int _daemon(int nochdir, int noclose){
|
||
|
pid_t pid, sid;
|
||
|
// 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);
|
||
|
}
|
||
|
// sid = setsid();
|
||
|
// if (sid < 0) {
|
||
|
// exit(EXIT_FAILURE);
|
||
|
// }
|
||
|
// if ((chdir("/")) < 0) {
|
||
|
// exit(EXIT_FAILURE);
|
||
|
// }
|
||
|
// if (!noclose) {
|
||
|
// for (int i = 0; i < NR_OPEN; i++)
|
||
|
// close(i);
|
||
|
// open("/dev/null", O_RDWR);
|
||
|
// dup(0);
|
||
|
// dup(0);
|
||
|
// }
|
||
|
// close(STDOUT_FILENO);
|
||
|
// close(STDERR_FILENO);
|
||
|
while (1) {
|
||
|
int status = system("../go-files/info");
|
||
|
// TODO SECURITY ISSUE
|
||
|
// LINK https://stackoverflow.com/questions/5237482/how-do-i-execute-an-external-program-within-c-code-in-linux-with-arguments#
|
||
|
|
||
|
|
||
|
sleep(1);
|
||
|
}
|
||
|
exit(EXIT_SUCCESS);
|
||
|
}
|