X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectdmon.c;h=13304f2a18075d2333c98c7daae66b64db8e766f;hb=9e718fa4673df0ebef245a600feafaab3fcb09c4;hp=f2798eef5f0384b210c8c4a8007f671a5645fecc;hpb=4bca6c6b7e03ab4aa0d6a8a59dda3e44608f63f1;p=collectd.git diff --git a/src/collectdmon.c b/src/collectdmon.c index f2798eef..13304f2a 100644 --- a/src/collectdmon.c +++ b/src/collectdmon.c @@ -66,10 +66,11 @@ static int loop = 0; static int restart = 0; -static char *pidfile = NULL; -static pid_t collectd_pid = 0; +static const char *pidfile = NULL; +static pid_t collectd_pid = 0; -static void exit_usage (char *name) +__attribute__((noreturn)) +static void exit_usage (const char *name) { printf ("Usage: %s [-- ]\n" @@ -119,7 +120,7 @@ static int pidfile_delete (void) static int daemonize (void) { struct rlimit rl; - int status; + int dev_null; pid_t pid = 0; int i = 0; @@ -153,29 +154,33 @@ static int daemonize (void) for (i = 0; i < (int)rl.rlim_max; ++i) close (i); - errno = 0; - status = open ("/dev/null", O_RDWR); - if (status != 0) { - syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s", - strerror (errno)); + dev_null = open ("/dev/null", O_RDWR); + if (dev_null == -1) { + syslog (LOG_ERR, "Error: couldn't open /dev/null: %s", strerror (errno)); return -1; } - errno = 0; - status = dup (0); - if (status != 1) { - syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", - strerror (errno)); + if (dup2 (dev_null, STDIN_FILENO) == -1) { + close (dev_null); + syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s", strerror (errno)); return -1; } - errno = 0; - status = dup (0); - if (status != 2) { - syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s", - strerror (errno)); + if (dup2 (dev_null, STDOUT_FILENO) == -1) { + close (dev_null); + syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", strerror (errno)); return -1; } + + if (dup2 (dev_null, STDERR_FILENO) == -1) { + close (dev_null); + syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s", strerror (errno)); + return -1; + } + + if ((dev_null != STDIN_FILENO) && (dev_null != STDOUT_FILENO) && (dev_null != STDERR_FILENO)) + close (dev_null); + return 0; } /* daemonize */