/**
* 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
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);
}
}
#endif /* HAVE_LIBRRD */
#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");
+
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;
+ const char *logfile;
#endif
#if HAVE_LIBRRD
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);
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 */
#if COLLECT_DAEMON
if (daemonize)
- pidfile_remove (pidfile);
+ pidfile_remove ();
#endif /* COLLECT_DAEMON */
return (0);