X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectdmon.c;h=2c1d58978bc175221bf3fd71448b2930c0a4aaab;hb=9d5c9e04aac3689f49098cba0e630128c0ef4caa;hp=c3f95255a49ba58547c3619fe9b2b2830389ce5b;hpb=dbeee0cfad69e701ea2665001fd9f2bce49c9489;p=collectd.git diff --git a/src/collectdmon.c b/src/collectdmon.c index c3f95255..2c1d5897 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" @@ -80,7 +81,7 @@ static void exit_usage (char *name) "\nFor see collectd.conf(5).\n" - "\n"PACKAGE" "VERSION", http://collectd.org/\n" + "\n"PACKAGE_NAME" "PACKAGE_VERSION", http://collectd.org/\n" "by Florian octo Forster \n" "for contributions see `AUTHORS'\n", name); exit (0); @@ -119,6 +120,7 @@ static int pidfile_delete (void) static int daemonize (void) { struct rlimit rl; + int dev_null; pid_t pid = 0; int i = 0; @@ -152,26 +154,30 @@ static int daemonize (void) for (i = 0; i < (int)rl.rlim_max; ++i) close (i); - errno = 0; - if (open ("/dev/null", O_RDWR) != 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 failed to open /dev/null: %s", strerror (errno)); return -1; } - errno = 0; - if (dup (0) != 1) { - syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", - strerror (errno)); + if (dup2 (dev_null, STDIN_FILENO) == -1) { + syslog (LOG_ERR, "Error: couldn't connect STDIN to /dev/null: %s", strerror (errno)); return -1; } - errno = 0; - if (dup (0) != 2) { - syslog (LOG_ERR, "Error: couldn't connect STDERR to /dev/null: %s", - strerror (errno)); + if (dup2 (dev_null, STDOUT_FILENO) == -1) { + syslog (LOG_ERR, "Error: couldn't connect STDOUT to /dev/null: %s", strerror (errno)); return -1; } + + if (dup2 (dev_null, STDERR_FILENO) == -1) { + 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 */ @@ -317,7 +323,10 @@ int main (int argc, char **argv) openlog ("collectdmon", LOG_CONS | LOG_PID, LOG_DAEMON); if (-1 == daemonize ()) + { + free (collectd_argv); return 1; + } sa.sa_handler = sig_int_term_handler; sa.sa_flags = 0; @@ -325,11 +334,13 @@ int main (int argc, char **argv) if (0 != sigaction (SIGINT, &sa, NULL)) { syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno)); + free (collectd_argv); return 1; } if (0 != sigaction (SIGTERM, &sa, NULL)) { syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno)); + free (collectd_argv); return 1; } @@ -337,6 +348,7 @@ int main (int argc, char **argv) if (0 != sigaction (SIGHUP, &sa, NULL)) { syslog (LOG_ERR, "Error: sigaction() failed: %s", strerror (errno)); + free (collectd_argv); return 1; }