X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcollectd.c;h=ab5564ead694502e0dd510804a406a1bf4e7376c;hb=41c58bbf1dd3fd89d80cb72a07cf659b52fba993;hp=98b9a1623f77b8840e6dbf36dbd7cfa4c69b2f1e;hpb=13c401437fad4b777d06fba5b45652065e54aaf5;p=collectd.git diff --git a/src/collectd.c b/src/collectd.c index 98b9a162..ab5564ea 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -1,11 +1,10 @@ /** * collectd - src/collectd.c - * Copyright (C) 2005,2006 Florian octo Forster + * Copyright (C) 2005-2007 Florian octo Forster * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the - * Free Software Foundation; either version 2 of the License, or (at your - * option) any later version. + * Free Software Foundation; only version 2 of the License is applicable. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -39,7 +38,6 @@ kstat_ctl_t *kc; * exported variables */ time_t curtime; -int operating_mode; static void sigIntHandler (int signal) { @@ -51,34 +49,49 @@ static void sigTermHandler (int signal) loop++; } -static int change_basedir (char *dir) +static int change_basedir (const char *orig_dir) { - int dirlen = strlen (dir); + char *dir = strdup (orig_dir); + int dirlen; + int status; + + if (dir == NULL) + { + syslog (LOG_ERR, "strdup failed: %s", strerror (errno)); + return (-1); + } + dirlen = strlen (dir); while ((dirlen > 0) && (dir[dirlen - 1] == '/')) dir[--dirlen] = '\0'; if (dirlen <= 0) return (-1); - if (chdir (dir) == -1) + status = chdir (dir); + free (dir); + + if (status != 0) { if (errno == ENOENT) { - if (mkdir (dir, 0755) == -1) + if (mkdir (orig_dir, 0755) == -1) { - syslog (LOG_ERR, "mkdir (%s): %s", dir, strerror (errno)); + syslog (LOG_ERR, "mkdir (%s): %s", orig_dir, + strerror (errno)); return (-1); } - else if (chdir (dir) == -1) + else if (chdir (orig_dir) == -1) { - syslog (LOG_ERR, "chdir (%s): %s", dir, strerror (errno)); + syslog (LOG_ERR, "chdir (%s): %s", orig_dir, + strerror (errno)); return (-1); } } else { - syslog (LOG_ERR, "chdir: %s", strerror (errno)); + syslog (LOG_ERR, "chdir (%s): %s", orig_dir, + strerror (errno)); return (-1); } } @@ -143,18 +156,8 @@ static void exit_usage (char *name) exit (0); } /* static void exit_usage (char *name) */ -static int start_client (void) +static int do_init (void) { - int step; - - struct timeval tv_now; - struct timeval tv_next; - struct timespec ts_wait; - - step = atoi (COLLECTD_STEP); - if (step <= 0) - step = 10; - #if HAVE_LIBKSTAT kc = NULL; update_kstat (); @@ -176,6 +179,22 @@ static int start_client (void) plugin_init_all (); + return (0); +} /* int do_init () */ + + +static int do_loop (void) +{ + int step; + + struct timeval tv_now; + struct timeval tv_next; + struct timespec ts_wait; + + step = atoi (COLLECTD_STEP); + if (step <= 0) + step = 10; + while (loop == 0) { if (gettimeofday (&tv_next, NULL) < 0) @@ -193,64 +212,48 @@ static int start_client (void) curtime = time (NULL); /* Issue all plugins */ - plugin_read_all (); + plugin_read_all (&loop); if (gettimeofday (&tv_now, NULL) < 0) { - syslog (LOG_ERR, "gettimeofday failed: %s", strerror (errno)); + syslog (LOG_ERR, "gettimeofday failed: %s", + strerror (errno)); return (-1); } if (timeval_sub_timespec (&tv_next, &tv_now, &ts_wait) != 0) { - syslog (LOG_WARNING, "Not sleeping because `timeval_sub_timespec' returned non-zero!"); + syslog (LOG_WARNING, "Not sleeping because " + "`timeval_sub_timespec' returned " + "non-zero!"); continue; } - while (nanosleep (&ts_wait, &ts_wait) == -1) + while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1)) { if (errno != EINTR) { syslog (LOG_ERR, "nanosleep failed: %s", strerror (errno)); - break; + return (-1); } } - } + } /* while (loop == 0) */ + DBG ("return (0);"); return (0); -} /* static int start_client (void) */ +} /* int do_loop */ -#if HAVE_LIBRRD -static int start_server (void) +static int do_shutdown (void) { - /* FIXME use stack here! */ - char *host; - char *type; - char *instance; - char *values; - - while (loop == 0) - { - if (network_receive (&host, &type, &instance, &values) == 0) - plugin_write (host, type, instance, values); - - if (host != NULL) free (host); host = NULL; - if (type != NULL) free (type); type = NULL; - if (instance != NULL) free (instance); instance = NULL; - if (values != NULL) free (values); values = NULL; - } - + plugin_shutdown_all (); return (0); -} /* static int start_server (void) */ -#endif /* HAVE_LIBRRD */ +} /* int do_shutdown */ #if COLLECT_DAEMON -static int pidfile_create (const char *file) +static int pidfile_create (void) { FILE *fh; - - if (file == NULL) - file = PIDFILE; + const char *file = global_option_get ("PIDFile"); if ((fh = fopen (file, "w")) == NULL) { @@ -263,14 +266,12 @@ static int pidfile_create (const char *file) return (0); } /* static int pidfile_create (const char *file) */ -#endif /* COLLECT_DAEMON */ -#if COLLECT_DAEMON -static int pidfile_remove (const char *file) +static int pidfile_remove (void) { - if (file == NULL) { - file = PIDFILE; - } + const char *file = global_option_get ("PIDFile"); + + DBG ("unlink (%s)", (file != NULL) ? file : ""); return (unlink (file)); } /* static int pidfile_remove (const char *file) */ #endif /* COLLECT_DAEMON */ @@ -279,22 +280,15 @@ int main (int argc, char **argv) { struct sigaction sigIntAction; struct sigaction sigTermAction; - char *datadir = PKGLOCALSTATEDIR; char *configfile = CONFIGFILE; + const char *datadir; #if COLLECT_DAEMON struct sigaction sigChldAction; - char *pidfile = NULL; pid_t pid; int daemonize = 1; #endif #if COLLECT_DEBUG - char *logfile = LOGFILE; -#endif - -#if HAVE_LIBRRD - operating_mode = MODE_LOCAL; -#else - operating_mode = MODE_CLIENT; + const char *logfile; #endif /* open syslog */ @@ -321,7 +315,7 @@ int main (int argc, char **argv) break; #if COLLECT_DAEMON case 'P': - pidfile = optarg; + global_option_set ("PIDFile", optarg); break; case 'f': daemonize = 0; @@ -334,7 +328,7 @@ int main (int argc, char **argv) } /* while (1) */ #if COLLECT_DEBUG - if ((logfile = cf_get_option ("LogFile", LOGFILE)) != NULL) + if ((logfile = global_option_get ("LogFile")) != NULL) DBG_STARTFILE (logfile, "Debug file opened."); #endif @@ -355,12 +349,12 @@ int main (int argc, char **argv) * Change directory. We do this _after_ reading the config and loading * modules to relative paths work as expected. */ - if ((datadir = cf_get_option ("DataDir", PKGLOCALSTATEDIR)) == NULL) + if ((datadir = global_option_get ("BaseDir")) == NULL) { fprintf (stderr, "Don't have a datadir to use. This should not happen. Ever."); return (1); } - if (change_basedir (datadir)) + else if (change_basedir (datadir)) { fprintf (stderr, "Error: Unable to change to directory `%s'.\n", datadir); return (1); @@ -370,16 +364,10 @@ int main (int argc, char **argv) /* * fork off child */ + memset (&sigChldAction, '\0', sizeof (sigChldAction)); sigChldAction.sa_handler = SIG_IGN; sigaction (SIGCHLD, &sigChldAction, NULL); - if ((pidfile == NULL) - && ((pidfile = cf_get_option ("PIDFile", PIDFILE)) == NULL)) - { - fprintf (stderr, "Cannot obtain pidfile. This shoud not happen. Ever."); - return (1); - } - if (daemonize) { if ((pid = fork ()) == -1) @@ -399,7 +387,7 @@ int main (int argc, char **argv) setsid (); /* Write pidfile */ - if (pidfile_create (pidfile)) + if (pidfile_create ()) exit (2); /* close standard descriptors */ @@ -428,21 +416,20 @@ int main (int argc, char **argv) /* * install signal handlers */ + memset (&sigIntAction, '\0', sizeof (sigIntAction)); sigIntAction.sa_handler = sigIntHandler; sigaction (SIGINT, &sigIntAction, NULL); + memset (&sigTermAction, '\0', sizeof (sigTermAction)); sigTermAction.sa_handler = sigTermHandler; sigaction (SIGTERM, &sigTermAction, NULL); /* * run the actual loops */ -#if HAVE_LIBRRD - if (operating_mode == MODE_SERVER) - start_server (); - else /* if (operating_mode == MODE_CLIENT || operating_mode == MODE_LOCAL || operating_mode == MODE_LOG) */ -#endif - start_client (); + do_init (); + do_loop (); + do_shutdown (); #if COLLECT_DEBUG if (logfile != NULL) @@ -455,8 +442,8 @@ int main (int argc, char **argv) #if COLLECT_DAEMON if (daemonize) - pidfile_remove (pidfile); + pidfile_remove (); #endif /* COLLECT_DAEMON */ return (0); -} /* int main (int argc, char **argv) */ +} /* int main */