diff options
Diffstat (limited to 'pullreqd.c')
-rw-r--r-- | pullreqd.c | 33 |
1 files changed, 33 insertions, 0 deletions
@@ -29,3 +29,36 @@ void child_handler(int sig_num) { void hangup_handler(int sig_num) { signal(SIGHUP, hangup_handler); } + +void init_daemon(int change_dir, char *path) { + /* Fork the parent. */ + pid_t pid = fork_proc(); + + /* Exit if the child process isn't the session leader. */ + if (setsid() < 0) { + exit(EXIT_FAILURE); + } + + /* Catch, ignore, and/or handle signals. */ + signal(SIGCHLD, child_handler); + signal(SIGHUP, hangup_handler); + + /* Fork again. */ + pid = fork_proc(); + + /* Set new file permissions. */ + umask(0); + + /* If change_dir is true, change the working directory to path, or + * the root directory if path isn't set. + */ + if (change_dir) { + chdir((!path) ? path : "/"); + } + + /* Close any open file descriptors. */ + for (int i = sysconf(_SC_OPEN_MAX); i >= 0; close(i--)); + + /* Open the logfile. */ + openlog("pullreqd", LOG_PID, LOG_DAEMON); +} |