summaryrefslogtreecommitdiff
path: root/pullreqd.c
diff options
context:
space:
mode:
authormrb0nk500 <b0nk@b0nk.xyz>2021-06-04 10:16:59 -0400
committermrb0nk500 <b0nk@b0nk.xyz>2021-06-04 10:16:59 -0400
commit0ec73fc148769b727b1c64abdb4a914a3e1d9ef8 (patch)
tree8cc403bd69b092e9712b53daad96ecab662a296e /pullreqd.c
parentc94f102527763904e059791b110972e1b13075a3 (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.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/pullreqd.c b/pullreqd.c
index e783aa6..cb6d680 100644
--- a/pullreqd.c
+++ b/pullreqd.c
@@ -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);
+}