From ab7a7f608e7ed6009c24896032f780b60a52d3cb Mon Sep 17 00:00:00 2001 From: MEUNIER Thibaud Date: Wed, 15 Jul 2020 15:42:43 +0200 Subject: [PATCH] ADD skeleton_daemon --- src/skeleton_daemon.c | 50 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/skeleton_daemon.c diff --git a/src/skeleton_daemon.c b/src/skeleton_daemon.c new file mode 100644 index 0000000..3c8a8cb --- /dev/null +++ b/src/skeleton_daemon.c @@ -0,0 +1,50 @@ +#include +#include +#include +#include +#include +#include +#include + +static void skeleton_daemon() +{ + pid_t pid; + + /* Fork off the parent process */ + pid = fork(); + + /* An error occurred */ + if (pid < 0) + exit(EXIT_FAILURE); + + /* Success: Let the parent terminate */ + if (pid > 0) + exit(EXIT_SUCCESS); + + /* On success: The child process becomes session leader */ + if (setsid() < 0) + exit(EXIT_FAILURE); + + /* Catch, ignore and handle signals */ + //TODO: Implement a working signal handler */ + signal(SIGCHLD, SIG_IGN); + signal(SIGHUP, SIG_IGN); + + /* Fork off for the second time*/ + pid = fork(); + + /* An error occurred */ + if (pid < 0) + exit(EXIT_FAILURE); + + /* Success: Let the parent terminate */ + if (pid > 0) + exit(EXIT_SUCCESS); + + /* Set new file permissions */ + umask(0); + + /* Open the log file */ + openlog ("firstdaemon", LOG_PID, LOG_DAEMON); +} +