aboutsummaryrefslogtreecommitdiffstats
path: root/daemonize.c
diff options
context:
space:
mode:
authorTushar Pankaj <tushar.s.pankaj@gmail.com>2018-11-06 19:46:37 -0600
committerTushar Pankaj <tushar.s.pankaj@gmail.com>2018-11-06 19:46:37 -0600
commitf47c0ab12e6f877ccfa125c6b59f6fe804e01f5a (patch)
tree3394dc91fbc9075a3d58f267d7333765ff54e07b /daemonize.c
parentAdd daemonize function to fork off as a daemon. (diff)
downloadwg-dynamic-f47c0ab12e6f877ccfa125c6b59f6fe804e01f5a.tar.xz
wg-dynamic-f47c0ab12e6f877ccfa125c6b59f6fe804e01f5a.zip
Didn't realize libc has daemon()
Signed-off-by: Tushar Pankaj <tushar.s.pankaj@gmail.com>
Diffstat (limited to 'daemonize.c')
-rw-r--r--daemonize.c57
1 files changed, 0 insertions, 57 deletions
diff --git a/daemonize.c b/daemonize.c
deleted file mode 100644
index 4f480e5..0000000
--- a/daemonize.c
+++ /dev/null
@@ -1,57 +0,0 @@
-/* SPDX-License-Identifier: MIT */
-/*
- * Copyright (C) 2018 Wireguard LLC
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/stat.h>
-
-void daemonize(void) {
- pid_t pid;
-
- /* fork */
- pid = fork();
-
- /* check fork for error */
- if (pid < 0) {
- perror("fork error");
- exit(EXIT_FAILURE);
- }
-
- /* terminate the parent */
- if (pid > 0) {
- exit(EXIT_SUCCESS);
- }
-
- /* become session leader */
- if (setsid() < 0) {
- perror("setsid error");
- exit(EXIT_FAILURE);
- }
-
- /* fork */
- pid = fork();
-
- /* check fork for error */
- if (pid < 0) {
- perror("fork error");
- exit(EXIT_FAILURE);
- }
-
- /* terminate the parent */
- if (pid > 0) {
- exit(EXIT_SUCCESS);
- }
-
- /* set up new environment */
- umask(S_IRWXG | S_IRWXO); /* umask 077 */
- chdir("/"); /* cd / to avoid locking up original cwd */
-
- /* close file descriptors */
- for (int fd = sysconf(_SC_OPEN_MAX); fd >=0; fd--) {
- close(fd);
- }
-}