/**
* 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
* exported variables
*/
time_t curtime;
-int operating_mode;
static void sigIntHandler (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);
}
}
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 ();
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)
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;
}
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 */
-#if 0
- char *host;
- char *type;
- char *instance;
- char *values;
-
- int error_counter = 0;
- int status;
-
- while ((loop == 0) && (error_counter < 3))
- {
- status = network_receive (&host, &type, &instance, &values);
-
- if (status != 0)
- {
- if (status < 0)
- error_counter++;
- continue;
- }
- error_counter = 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;
- }
-
-#endif
+ 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)
{
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 : "<null>");
return (unlink (file));
} /* static int pidfile_remove (const char *file) */
#endif /* COLLECT_DAEMON */
{
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 */
break;
#if COLLECT_DAEMON
case 'P':
- pidfile = optarg;
+ global_option_set ("PIDFile", optarg);
break;
case 'f':
daemonize = 0;
} /* 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
* 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);
/*
* 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)
setsid ();
/* Write pidfile */
- if (pidfile_create (pidfile))
+ if (pidfile_create ())
exit (2);
/* close standard descriptors */
/*
* 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 ();
-
- plugin_shutdown_all ();
+ do_init ();
+ do_loop ();
+ do_shutdown ();
#if COLLECT_DEBUG
if (logfile != NULL)
#if COLLECT_DAEMON
if (daemonize)
- pidfile_remove (pidfile);
+ pidfile_remove ();
#endif /* COLLECT_DAEMON */
return (0);
-} /* int main (int argc, char **argv) */
+} /* int main */