diff --git a/src/cmd/uts-server.c b/src/cmd/uts-server.c index e69de29..aedc0b1 100644 --- a/src/cmd/uts-server.c +++ b/src/cmd/uts-server.c @@ -0,0 +1,17 @@ +int main() +{ + skeleton_daemon(); + + while (1) + { + //TODO: Insert daemon code here. + syslog (LOG_NOTICE, "First daemon started."); + sleep (20); + break; + } + + syslog (LOG_NOTICE, "First daemon terminated."); + closelog(); + + return EXIT_SUCCESS; +} diff --git a/src/lib/rfc3161.c b/src/lib/rfc3161.c index e69de29..420048f 100644 --- a/src/lib/rfc3161.c +++ b/src/lib/rfc3161.c @@ -0,0 +1,10 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + diff --git a/src/lib/utils.c b/src/lib/utils.c new file mode 100644 index 0000000..8b4b59b --- /dev/null +++ b/src/lib/utils.c @@ -0,0 +1,64 @@ +/* + * daemonize.c + */ + +#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); + + /* Change the working directory to the root directory */ + /* or another appropriated directory */ + chdir("/"); + + /* Close all open file descriptors */ + int x; + for (x = sysconf(_SC_OPEN_MAX); x>0; x--) + { + close (x); + } + + /* Open the log file */ + openlog ("firstdaemon", LOG_PID, LOG_DAEMON); +}