From: oetiker Date: Sun, 4 Oct 2009 11:27:44 +0000 (+0000) Subject: rrdcached: Create the pidfile and (UNIX) socket directories as well. X-Git-Url: https://git.octo.it/?p=rrdtool.git;a=commitdiff_plain;h=d4767fd82947243885f6874c97c0ba477d86c6e7 rrdcached: Create the pidfile and (UNIX) socket directories as well. Those files may be located in a subdirectory of, e.g., /var/run/. To avoid the need to manually create (and recreate, e.g. in case /var/run/ is on a tmpfs) that subdirectory, let the daemon handle the creation of those directories. -- Sebastian Harl git-svn-id: svn://svn.oetiker.ch/rrdtool/trunk/program@1921 a5681a0c-68f1-0310-ab6d-d61299d08faa --- diff --git a/src/rrd_daemon.c b/src/rrd_daemon.c index 710fa01..7913a59 100644 --- a/src/rrd_daemon.c +++ b/src/rrd_daemon.c @@ -105,6 +105,7 @@ #include #include #include +#include #include /* }}} */ @@ -348,12 +349,32 @@ static void install_signal_handlers(void) /* {{{ */ static int open_pidfile(char *action, int oflag) /* {{{ */ { int fd; - char *file; + const char *file; + char *file_copy, *dir; file = (config_pid_file != NULL) ? config_pid_file : LOCALSTATEDIR "/run/rrdcached.pid"; + /* dirname may modify its argument */ + file_copy = strdup(file); + if (file_copy == NULL) + { + fprintf(stderr, "rrdcached: strdup(): %s\n", + rrd_strerror(errno)); + return -1; + } + + dir = dirname(file_copy); + if (rrd_mkdir_p(dir, 0777) != 0) + { + fprintf(stderr, "Failed to create pidfile directory '%s': %s\n", + dir, rrd_strerror(errno)); + return -1; + } + + free(file_copy); + fd = open(file, oflag, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); if (fd < 0) fprintf(stderr, "rrdcached: can't %s pid file '%s' (%s)\n", @@ -2239,11 +2260,31 @@ static int open_listen_socket_unix (const listen_socket_t *sock) /* {{{ */ listen_socket_t *temp; int status; const char *path; + char *path_copy, *dir; path = sock->addr; if (strncmp(path, "unix:", strlen("unix:")) == 0) path += strlen("unix:"); + /* dirname may modify its argument */ + path_copy = strdup(path); + if (path_copy == NULL) + { + fprintf(stderr, "rrdcached: strdup(): %s\n", + rrd_strerror(errno)); + return (-1); + } + + dir = dirname(path_copy); + if (rrd_mkdir_p(dir, 0777) != 0) + { + fprintf(stderr, "Failed to create socket directory '%s': %s\n", + dir, rrd_strerror(errno)); + return (-1); + } + + free(path_copy); + temp = (listen_socket_t *) rrd_realloc (listen_fds, sizeof (listen_fds[0]) * (listen_fds_num + 1)); if (temp == NULL)