#include #include #include #include #include #include #include #include #include bool daemonize() { pid_t pid = fork(); if (pid < 0) { return false; } if (pid > 0) { exit(0); } if (setsid() < 0) { return false; } pid = fork(); if (pid < 0) { return false; } if (pid > 0) { exit(0); } umask(0); chdir("/"); FILE *fp; fp = fopen("/Users/mdy/Development/langage_C/System2/daemonize/test.txt", "w"); close(STDIN_FILENO); close(STDOUT_FILENO); dup(fileno(fp)); close(STDERR_FILENO); dup(fileno(fp)); return true; } void handler(int signum) { if (signum == SIGUSR1) { FILE *fp; fp = fopen("/Users/mdy/Development/langage_C/System2/daemonize/coucou.txt", "w"); fprintf(fp, "coucou, je suis bien en vie!\n"); fclose(fp); } else if (signum == SIGUSR2) { exit(EXIT_SUCCESS); } else { printf("received signal %d\n", signum); } } int main(void) { daemonize(); if (signal(SIGUSR1, handler) == SIG_ERR) printf("can't catch SIGUSR1\n"); if (signal(SIGUSR2, handler) == SIG_ERR) printf("can't catch SIGUSR2\n"); for (;;) pause(); }