Code

properly handle the case where RRDCACHED_ADDRESS is an empty string
[rrdtool.git] / src / rrd_client.c
index 11fb80dc19cb25135c277ca68a8d234deeb0bbc3..e7c227b8564338c1d74ab2f29c8bd033d459944d 100644 (file)
@@ -2,29 +2,37 @@
  * RRDTool - src/rrd_client.c
  * Copyright (C) 2008 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; 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
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ * 
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
  *
  * Authors:
  *   Florian octo Forster <octo at verplant.org>
+ *   Sebastian tokkee Harl <sh at tokkee.org>
  **/
 
 #include "rrd.h"
-#include "rrd_client.h"
 #include "rrd_tool.h"
+#include "rrd_client.h"
 
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <strings.h>
 #include <errno.h>
 #include <assert.h>
 #include <pthread.h>
@@ -32,6 +40,7 @@
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <netdb.h>
+#include <limits.h>
 
 #ifndef ENODATA
 #define ENODATA ENOENT
@@ -51,6 +60,46 @@ static int sd = -1;
 static FILE *sh = NULL;
 static char *sd_path = NULL; /* cache the path for sd */
 
+/* get_path: Return a path name appropriate to be sent to the daemon.
+ *
+ * When talking to a local daemon (thru a UNIX socket), relative path names
+ * are resolved to absolute path names to allow for transparent integration
+ * into existing solutions (as requested by Tobi). Else, absolute path names
+ * are not allowed, since path name translation is done by the server.
+ *
+ * One must hold `lock' when calling this function. */
+static const char *get_path (const char *path, char *resolved_path) /* {{{ */
+{
+  const char *ret = path;
+  int is_unix = 0;
+
+  if ((path == NULL) || (resolved_path == NULL) || (sd_path == NULL))
+    return (NULL);
+
+  if ((*sd_path == '/')
+      || (strncmp ("unix:", sd_path, strlen ("unix:")) == 0))
+    is_unix = 1;
+
+  if (is_unix)
+  {
+    ret = realpath(path, resolved_path);
+    if (ret == NULL)
+      rrd_set_error("realpath(%s): %s", path, rrd_strerror(errno));
+    return ret;
+  }
+  else
+  {
+    if (*path == '/') /* not absolute path */
+    {
+      rrd_set_error ("absolute path names not allowed when talking "
+          "to a remote daemon");
+      return NULL;
+    }
+  }
+
+  return path;
+} /* }}} char *get_path */
+
 /* One must hold `lock' when calling `close_connection'. */
 static void close_connection (void) /* {{{ */
 {
@@ -179,49 +228,50 @@ static void response_free (rrdc_response_t *res) /* {{{ */
 
 static int response_read (rrdc_response_t **ret_response) /* {{{ */
 {
-  rrdc_response_t *ret;
+  rrdc_response_t *ret = NULL;
+  int status = 0;
 
   char buffer[4096];
   char *buffer_ptr;
 
   size_t i;
 
+#define DIE(code) do { status = code; goto err_out; } while(0)
+
   if (sh == NULL)
-    return (-1);
+    DIE(-1);
 
   ret = (rrdc_response_t *) malloc (sizeof (rrdc_response_t));
   if (ret == NULL)
-    return (-2);
+    DIE(-2);
   memset (ret, 0, sizeof (*ret));
   ret->lines = NULL;
   ret->lines_num = 0;
 
   buffer_ptr = fgets (buffer, sizeof (buffer), sh);
   if (buffer_ptr == NULL)
-    return (-3);
+    DIE(-3);
+
   chomp (buffer);
 
   ret->status = strtol (buffer, &ret->message, 0);
   if (buffer == ret->message)
-  {
-    response_free (ret);
-    return (-4);
-  }
+    DIE(-4);
+
   /* Skip leading whitespace of the status message */
   ret->message += strspn (ret->message, " \t");
 
   if (ret->status <= 0)
   {
-    *ret_response = ret;
-    return (0);
+    if (ret->status < 0)
+      rrd_set_error("rrdcached: %s", ret->message);
+    goto out;
   }
 
   ret->lines = (char **) malloc (sizeof (char *) * ret->status);
   if (ret->lines == NULL)
-  {
-    response_free (ret);
-    return (-5);
-  }
+    DIE(-5);
+
   memset (ret->lines, 0, sizeof (char *) * ret->status);
   ret->lines_num = (size_t) ret->status;
 
@@ -229,22 +279,27 @@ static int response_read (rrdc_response_t **ret_response) /* {{{ */
   {
     buffer_ptr = fgets (buffer, sizeof (buffer), sh);
     if (buffer_ptr == NULL)
-    {
-      response_free (ret);
-      return (-6);
-    }
+      DIE(-6);
+
     chomp (buffer);
 
     ret->lines[i] = strdup (buffer);
     if (ret->lines[i] == NULL)
-    {
-      response_free (ret);
-      return (-7);
-    }
+      DIE(-7);
   }
 
+out:
   *ret_response = ret;
-  return (0);
+  fflush(sh);
+  return (status);
+
+err_out:
+  response_free(ret);
+  close_connection();
+  return (status);
+
+#undef DIE
+
 } /* }}} rrdc_response_t *response_read */
 
 static int request (const char *buffer, size_t buffer_size, /* {{{ */
@@ -253,19 +308,15 @@ static int request (const char *buffer, size_t buffer_size, /* {{{ */
   int status;
   rrdc_response_t *res;
 
-  pthread_mutex_lock (&lock);
-
   if (sh == NULL)
-  {
-    pthread_mutex_unlock (&lock);
     return (ENOTCONN);
-  }
 
   status = (int) fwrite (buffer, buffer_size, /* nmemb = */ 1, sh);
   if (status != 1)
   {
     close_connection ();
-    pthread_mutex_unlock (&lock);
+    rrd_set_error("request: socket error (%d) while talking to rrdcached",
+                  status);
     return (-1);
   }
   fflush (sh);
@@ -273,10 +324,12 @@ static int request (const char *buffer, size_t buffer_size, /* {{{ */
   res = NULL;
   status = response_read (&res);
 
-  pthread_mutex_unlock (&lock);
-
   if (status != 0)
+  {
+    if (status < 0)
+      rrd_set_error("request: internal error while talking to rrdcached");
     return (status);
+  }
 
   *ret_response = res;
   return (0);
@@ -297,7 +350,8 @@ int rrdc_is_connected(const char *daemon_addr) /* {{{ */
      * but it is not specified in the current command.
      * Daemon is only implied in this case if set in ENV
      */
-    if (getenv(ENV_RRDCACHED_ADDRESS) != NULL)
+    char *addr = getenv(ENV_RRDCACHED_ADDRESS);
+    if (addr != NULL && strcmp(addr,"") != 0)
       return 1;
     else
       return 0;
@@ -347,15 +401,22 @@ static int rrdc_connect_unix (const char *path) /* {{{ */
   return (0);
 } /* }}} int rrdc_connect_unix */
 
-static int rrdc_connect_network (const char *addr) /* {{{ */
+static int rrdc_connect_network (const char *addr_orig) /* {{{ */
 {
   struct addrinfo ai_hints;
   struct addrinfo *ai_res;
   struct addrinfo *ai_ptr;
+  char addr_copy[NI_MAXHOST];
+  char *addr;
+  char *port;
 
-  assert (addr != NULL);
+  assert (addr_orig != NULL);
   assert (sd == -1);
 
+  strncpy(addr_copy, addr_orig, sizeof(addr_copy));
+  addr_copy[sizeof(addr_copy) - 1] = '\0';
+  addr = addr_copy;
+
   int status;
   memset (&ai_hints, 0, sizeof (ai_hints));
   ai_hints.ai_flags = 0;
@@ -365,10 +426,52 @@ static int rrdc_connect_network (const char *addr) /* {{{ */
   ai_hints.ai_family = AF_UNSPEC;
   ai_hints.ai_socktype = SOCK_STREAM;
 
+  port = NULL;
+  if (*addr == '[') /* IPv6+port format */
+  {
+    /* `addr' is something like "[2001:780:104:2:211:24ff:feab:26f8]:12345" */
+    addr++;
+
+    port = strchr (addr, ']');
+    if (port == NULL)
+    {
+      rrd_set_error("malformed address: %s", addr_orig);
+      return (-1);
+    }
+    *port = 0;
+    port++;
+
+    if (*port == ':')
+      port++;
+    else if (*port == 0)
+      port = NULL;
+    else
+    {
+      rrd_set_error("garbage after address: %s", port);
+      return (-1);
+    }
+  } /* if (*addr == '[') */
+  else
+  {
+    port = rindex(addr, ':');
+    if (port != NULL)
+    {
+      *port = 0;
+      port++;
+    }
+  }
+
   ai_res = NULL;
-  status = getaddrinfo (addr, RRDCACHED_DEFAULT_PORT, &ai_hints, &ai_res);
+  status = getaddrinfo (addr,
+                        port == NULL ? RRDCACHED_DEFAULT_PORT : port,
+                        &ai_hints, &ai_res);
   if (status != 0)
-    return (status);
+  {
+    rrd_set_error ("failed to resolve address `%s' (port %s): %s",
+        addr, port == NULL ? RRDCACHED_DEFAULT_PORT : port,
+        gai_strerror (status));
+    return (-1);
+  }
 
   for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
   {
@@ -400,6 +503,8 @@ static int rrdc_connect_network (const char *addr) /* {{{ */
     break;
   } /* for (ai_ptr) */
 
+  freeaddrinfo(ai_res);
+
   return (status);
 } /* }}} int rrdc_connect_network */
 
@@ -407,11 +512,14 @@ int rrdc_connect (const char *addr) /* {{{ */
 {
   int status = 0;
 
-  if (addr == NULL)
+  if (addr == NULL) {
     addr = getenv (ENV_RRDCACHED_ADDRESS);
+  }
 
-  if (addr == NULL)
-    return 0;
+  if (addr == NULL || strcmp(addr,"") == 0 ) {
+    addr = NULL;
+    return 0;   
+  }
 
   pthread_mutex_lock(&lock);
 
@@ -426,6 +534,7 @@ int rrdc_connect (const char *addr) /* {{{ */
     close_connection();
   }
 
+  rrd_clear_error ();
   if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
     status = rrdc_connect_unix (addr + strlen ("unix:"));
   else if (addr[0] == '/')
@@ -436,10 +545,18 @@ int rrdc_connect (const char *addr) /* {{{ */
   if (status == 0 && sd >= 0)
     sd_path = strdup(addr);
   else
+  {
+    char *err = rrd_test_error () ? rrd_get_error () : "Internal error";
+    /* err points the string that gets written to by rrd_set_error(), thus we
+     * cannot pass it to that function */
+    err = strdup (err);
     rrd_set_error("Unable to connect to rrdcached: %s",
                   (status < 0)
-                  ? "Internal error"
+                  ? (err ? err : "Internal error")
                   : rrd_strerror (status));
+    if (err != NULL)
+      free (err);
+  }
 
   pthread_mutex_unlock (&lock);
   return (status);
@@ -466,6 +583,7 @@ int rrdc_update (const char *filename, int values_num, /* {{{ */
   rrdc_response_t *res;
   int status;
   int i;
+  char file_path[PATH_MAX];
 
   memset (buffer, 0, sizeof (buffer));
   buffer_ptr = &buffer[0];
@@ -475,15 +593,29 @@ int rrdc_update (const char *filename, int values_num, /* {{{ */
   if (status != 0)
     return (ENOBUFS);
 
+  pthread_mutex_lock (&lock);
+  filename = get_path (filename, file_path);
+  if (filename == NULL)
+  {
+    pthread_mutex_unlock (&lock);
+    return (-1);
+  }
+
   status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
   if (status != 0)
+  {
+    pthread_mutex_unlock (&lock);
     return (ENOBUFS);
+  }
 
   for (i = 0; i < values_num; i++)
   {
     status = buffer_add_value (values[i], &buffer_ptr, &buffer_free);
     if (status != 0)
+    {
+      pthread_mutex_unlock (&lock);
       return (ENOBUFS);
+    }
   }
 
   assert (buffer_free < sizeof (buffer));
@@ -493,6 +625,8 @@ int rrdc_update (const char *filename, int values_num, /* {{{ */
 
   res = NULL;
   status = request (buffer, buffer_size, &res);
+  pthread_mutex_unlock (&lock);
+
   if (status != 0)
     return (status);
 
@@ -510,6 +644,7 @@ int rrdc_flush (const char *filename) /* {{{ */
   size_t buffer_size;
   rrdc_response_t *res;
   int status;
+  char file_path[PATH_MAX];
 
   if (filename == NULL)
     return (-1);
@@ -522,9 +657,20 @@ int rrdc_flush (const char *filename) /* {{{ */
   if (status != 0)
     return (ENOBUFS);
 
+  pthread_mutex_lock (&lock);
+  filename = get_path (filename, file_path);
+  if (filename == NULL)
+  {
+    pthread_mutex_unlock (&lock);
+    return (-1);
+  }
+
   status = buffer_add_string (filename, &buffer_ptr, &buffer_free);
   if (status != 0)
+  {
+    pthread_mutex_unlock (&lock);
     return (ENOBUFS);
+  }
 
   assert (buffer_free < sizeof (buffer));
   buffer_size = sizeof (buffer) - buffer_free;
@@ -533,6 +679,8 @@ int rrdc_flush (const char *filename) /* {{{ */
 
   res = NULL;
   status = request (buffer, buffer_size, &res);
+  pthread_mutex_unlock (&lock);
+
   if (status != 0)
     return (status);
 
@@ -554,13 +702,23 @@ int rrdc_flush_if_daemon (const char *opt_daemon, const char *filename) /* {{{ *
 
   if (rrdc_is_connected(opt_daemon))
   {
+    rrd_clear_error();
     status = rrdc_flush (filename);
-    if (status != 0)
+
+    if (status != 0 && !rrd_test_error())
     {
-      rrd_set_error ("rrdc_flush (%s) failed with status %i.",
-                     filename, status);
+      if (status > 0)
+      {
+        rrd_set_error("rrdc_flush (%s) failed: %s",
+                      filename, rrd_strerror(status));
+      }
+      else if (status < 0)
+      {
+        rrd_set_error("rrdc_flush (%s) failed with status %i.",
+                      filename, status);
+      }
     }
-  } /* if (daemon_addr) */
+  } /* if (rrdc_is_connected(..)) */
 
   return status;
 } /* }}} int rrdc_flush_if_daemon */
@@ -587,7 +745,10 @@ int rrdc_stats_get (rrdc_stats_t **ret_stats) /* {{{ */
    * }}} */
 
   res = NULL;
+  pthread_mutex_lock (&lock);
   status = request ("STATS\n", strlen ("STATS\n"), &res);
+  pthread_mutex_unlock (&lock);
+
   if (status != 0)
     return (status);
 
@@ -689,7 +850,7 @@ void rrdc_stats_free (rrdc_stats_t *ret_stats) /* {{{ */
 
     if (this->name != NULL)
     {
-      free (this->name);
+      free ((char *)this->name);
       this->name = NULL;
     }
     free (this);