diff options
author | mrb0nk500 <b0nk@b0nk.xyz> | 2021-06-04 10:16:59 -0400 |
---|---|---|
committer | mrb0nk500 <b0nk@b0nk.xyz> | 2021-06-04 10:16:59 -0400 |
commit | 0ec73fc148769b727b1c64abdb4a914a3e1d9ef8 (patch) | |
tree | 8cc403bd69b092e9712b53daad96ecab662a296e /pullreqd.c | |
parent | c94f102527763904e059791b110972e1b13075a3 (diff) |
Added `init_daemon()`.
Which actually initiallizes, and starts the daemon.
If `change_dir` is true, it will change the working
directory to `path`, or the root directory if `path`
is NULL.
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); +} |