From f47c0ab12e6f877ccfa125c6b59f6fe804e01f5a Mon Sep 17 00:00:00 2001 From: Tushar Pankaj Date: Tue, 6 Nov 2018 19:46:37 -0600 Subject: Didn't realize libc has daemon() Signed-off-by: Tushar Pankaj --- daemonize.c | 57 --------------------------------------------------------- daemonize.h | 11 ----------- 2 files changed, 68 deletions(-) delete mode 100644 daemonize.c delete mode 100644 daemonize.h 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 -#include -#include -#include -#include - -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); - } -} diff --git a/daemonize.h b/daemonize.h deleted file mode 100644 index 3720ff1..0000000 --- a/daemonize.h +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: MIT */ -/* - * Copyright (C) 2018 Wireguard LLC - */ - -#ifndef DAEMONIZE_H -#define DAEMONIZE_H - -void daemonize(void); - -#endif -- cgit v1.2.3-59-g8ed1b