X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fcollectd.c;h=a72ff35f2fc21ec042e809d64967100f35f5a9fd;hb=354f9991530248e45207d236eb74c1cc3d5238ef;hp=1b9c3747a629fb6027460a3d1e88cb379c96ec4b;hpb=a7eecf6018a684dcf8323d4a41a7e704a5d57f02;p=collectd.git diff --git a/src/daemon/collectd.c b/src/daemon/collectd.c index 1b9c3747..a72ff35f 100644 --- a/src/daemon/collectd.c +++ b/src/daemon/collectd.c @@ -32,7 +32,6 @@ #include "configfile.h" #include -#include #include #include @@ -197,8 +196,10 @@ static int change_basedir (const char *orig_dir) while ((dirlen > 0) && (dir[dirlen - 1] == '/')) dir[--dirlen] = '\0'; - if (dirlen <= 0) + if (dirlen == 0) { + free (dir); return (-1); + } status = chdir (dir); if (status == 0) @@ -268,9 +269,10 @@ static void update_kstat (void) /* TODO * Remove all settings but `-f' and `-C' */ +__attribute__((noreturn)) static void exit_usage (int status) { - printf ("Usage: "PACKAGE" [OPTIONS]\n\n" + printf ("Usage: "PACKAGE_NAME" [OPTIONS]\n\n" "Available options:\n" " General:\n" @@ -289,7 +291,7 @@ static void exit_usage (int status) " PID file "PIDFILE"\n" " Plugin directory "PLUGINDIR"\n" " Data directory "PKGLOCALSTATEDIR"\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"); exit (status); @@ -300,6 +302,11 @@ static int do_init (void) #if HAVE_SETLOCALE if (setlocale (LC_NUMERIC, COLLECTD_LOCALE) == NULL) WARNING ("setlocale (\"%s\") failed.", COLLECTD_LOCALE); + + /* Update the environment, so that libraries that are calling + * setlocale(LC_NUMERIC, "") don't accidentally revert these changes. */ + unsetenv ("LC_ALL"); + setenv ("LC_NUMERIC", COLLECTD_LOCALE, /* overwrite = */ 1); #endif #if HAVE_LIBKSTAT @@ -325,9 +332,7 @@ static int do_init (void) } #endif - plugin_init_all (); - - return (0); + return plugin_init_all (); } /* int do_init () */ @@ -381,8 +386,7 @@ static int do_loop (void) static int do_shutdown (void) { - plugin_shutdown_all (); - return (0); + return plugin_shutdown_all (); } /* int do_shutdown */ #if COLLECT_DAEMON @@ -408,75 +412,99 @@ static int pidfile_create (void) static int pidfile_remove (void) { const char *file = global_option_get ("PIDFile"); + if (file == NULL) + return 0; - DEBUG ("unlink (%s)", (file != NULL) ? file : ""); return (unlink (file)); } /* static int pidfile_remove (const char *file) */ #endif /* COLLECT_DAEMON */ #ifdef KERNEL_LINUX -int notify_upstart (void) +static int notify_upstart (void) { - const char *upstart_job = getenv("UPSTART_JOB"); + char const *upstart_job = getenv("UPSTART_JOB"); if (upstart_job == NULL) return 0; if (strcmp(upstart_job, "collectd") != 0) + { + WARNING ("Environment specifies unexpected UPSTART_JOB=\"%s\", expected \"collectd\". Ignoring the variable.", upstart_job); return 0; + } - WARNING ("supervised by upstart, will stop to signal readyness"); + NOTICE("Upstart detected, stopping now to signal readyness."); raise(SIGSTOP); unsetenv("UPSTART_JOB"); return 1; } -int notify_systemd (void) +static int notify_systemd (void) { - int fd = -1; - const char *notifysocket = getenv("NOTIFY_SOCKET"); + int fd; + const char *notifysocket; struct sockaddr_un su; - struct iovec iov; - struct msghdr hdr; + size_t su_size; + char buffer[] = "READY=1\n"; + notifysocket = getenv ("NOTIFY_SOCKET"); if (notifysocket == NULL) return 0; - if ((strchr("@/", notifysocket[0])) == NULL || - strlen(notifysocket) < 2) + if ((strlen (notifysocket) < 2) + || ((notifysocket[0] != '@') && (notifysocket[0] != '/'))) + { + ERROR ("invalid notification socket NOTIFY_SOCKET=\"%s\": path must be absolute", notifysocket); return 0; + } + NOTICE ("Systemd detected, trying to signal readyness."); + + unsetenv ("NOTIFY_SOCKET"); - WARNING ("supervised by systemd, will signal readyness"); - if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) { - WARNING ("cannot contact systemd socket %s", notifysocket); +#if defined(SOCK_CLOEXEC) + fd = socket (AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, /* protocol = */ 0); +#else + fd = socket (AF_UNIX, SOCK_DGRAM, /* protocol = */ 0); +#endif + if (fd < 0) { + char errbuf[1024]; + ERROR ("creating UNIX socket failed: %s", + sstrerror (errno, errbuf, sizeof (errbuf))); return 0; } - bzero(&su, sizeof(su)); + memset (&su, 0, sizeof (su)); su.sun_family = AF_UNIX; - sstrncpy (su.sun_path, notifysocket, sizeof(su.sun_path)); - - if (notifysocket[0] == '@') + if (notifysocket[0] != '@') + { + /* regular UNIX socket */ + sstrncpy (su.sun_path, notifysocket, sizeof (su.sun_path)); + su_size = sizeof (su); + } + else + { + /* Linux abstract namespace socket: specify address as "\0foo", i.e. + * start with a null byte. Since null bytes have no special meaning in + * that case, we have to set su_size correctly to cover only the bytes + * that are part of the address. */ + sstrncpy (su.sun_path, notifysocket, sizeof (su.sun_path)); su.sun_path[0] = 0; + su_size = sizeof (sa_family_t) + strlen (notifysocket); + if (su_size > sizeof (su)) + su_size = sizeof (su); + } - bzero(&iov, sizeof(iov)); - iov.iov_base = "READY=1"; - iov.iov_len = strlen("READY=1"); - - bzero(&hdr, sizeof(hdr)); - hdr.msg_name = &su; - hdr.msg_namelen = offsetof(struct sockaddr_un, sun_path) + - strlen(notifysocket); - hdr.msg_iov = &iov; - hdr.msg_iovlen = 1; - - unsetenv("NOTIFY_SOCKET"); - if (sendmsg(fd, &hdr, MSG_NOSIGNAL) < 0) { - WARNING ("cannot send notification to systemd"); + if (sendto (fd, buffer, strlen (buffer), MSG_NOSIGNAL, (void *) &su, (socklen_t) su_size) < 0) + { + char errbuf[1024]; + ERROR ("sendto(\"%s\") failed: %s", notifysocket, + sstrerror (errno, errbuf, sizeof (errbuf))); close(fd); return 0; } + + unsetenv ("NOTIFY_SOCKET"); close(fd); return 1; } @@ -488,7 +516,7 @@ int main (int argc, char **argv) struct sigaction sig_term_action; struct sigaction sig_usr1_action; struct sigaction sig_pipe_action; - char *configfile = CONFIGFILE; + const char *configfile = CONFIGFILE; int test_config = 0; int test_readall = 0; const char *basedir; @@ -608,6 +636,8 @@ int main (int argc, char **argv) #endif ) { + int status; + if ((pid = fork ()) == -1) { /* error */ @@ -636,19 +666,24 @@ int main (int argc, char **argv) close (1); close (0); - if (open ("/dev/null", O_RDWR) != 0) + status = open ("/dev/null", O_RDWR); + if (status != 0) { - ERROR ("Error: Could not connect `STDIN' to `/dev/null'"); + ERROR ("Error: Could not connect `STDIN' to `/dev/null' (status %d)", status); return (1); } - if (dup (0) != 1) + + status = dup (0); + if (status != 1) { - ERROR ("Error: Could not connect `STDOUT' to `/dev/null'"); + ERROR ("Error: Could not connect `STDOUT' to `/dev/null' (status %d)", status); return (1); } - if (dup (0) != 2) + + status = dup (0); + if (status != 2) { - ERROR ("Error: Could not connect `STDERR' to `/dev/null'"); + ERROR ("Error: Could not connect `STDERR' to `/dev/null', (status %d)", status); return (1); } } /* if (daemonize) */ @@ -691,12 +726,19 @@ int main (int argc, char **argv) /* * run the actual loops */ - do_init (); + if (do_init () != 0) + { + ERROR ("Error: one or more plugin init callbacks failed."); + exit_status = 1; + } if (test_readall) { if (plugin_read_all_once () != 0) + { + ERROR ("Error: one or more plugin read callbacks failed."); exit_status = 1; + } } else { @@ -707,7 +749,11 @@ int main (int argc, char **argv) /* close syslog */ INFO ("Exiting normally."); - do_shutdown (); + if (do_shutdown () != 0) + { + ERROR ("Error: one or more plugin shutdown callbacks failed."); + exit_status = 1; + } #if COLLECT_DAEMON if (daemonize)