src/rrd_daemon.c: Stat files before creating a tree-node for them.
[rrdtool.git] / src / rrd_daemon.c
index 7a80220..dfdd0e3 100644 (file)
@@ -310,9 +310,6 @@ static int enqueue_cache_item (cache_item_t *ci, /* {{{ */
 {
   int did_insert = 0;
 
-  RRDD_LOG (LOG_DEBUG, "enqueue_cache_item: Adding %s to the update queue.",
-      ci->file);
-
   if (ci == NULL)
     return (-1);
 
@@ -549,9 +546,6 @@ static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
 
     pthread_mutex_unlock (&cache_lock);
 
-    RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
-        file, values_num, (void *) values);
-
     status = rrd_update_r (file, NULL, values_num, (void *) values);
     if (status != 0)
     {
@@ -564,18 +558,19 @@ static void *queue_thread_main (void *args __attribute__((unused))) /* {{{ */
     for (i = 0; i < values_num; i++)
       free (values[i]);
 
-    pthread_mutex_lock (&stats_lock);
-    stats_updates_written++;
-    stats_data_sets_written += values_num;
-    pthread_mutex_unlock (&stats_lock);
+    if (status == 0)
+    {
+      pthread_mutex_lock (&stats_lock);
+      stats_updates_written++;
+      stats_data_sets_written += values_num;
+      pthread_mutex_unlock (&stats_lock);
+    }
 
     pthread_mutex_lock (&cache_lock);
     pthread_cond_broadcast (&flush_cond);
   } /* while (do_shutdown == 0) */
   pthread_mutex_unlock (&cache_lock);
 
-  RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
-
   return (NULL);
 } /* }}} void *queue_thread_main */
 
@@ -914,6 +909,33 @@ static int handle_request_update (int fd, /* {{{ */
   ci = g_tree_lookup (cache_tree, file);
   if (ci == NULL) /* {{{ */
   {
+    struct stat statbuf;
+
+    memset (&statbuf, 0, sizeof (statbuf));
+    status = stat (file, &statbuf);
+    if (status != 0)
+    {
+      pthread_mutex_unlock (&cache_lock);
+      RRDD_LOG (LOG_ERR, "handle_request_update: stat (%s) failed.", file);
+
+      status = errno;
+      if (status == ENOENT)
+        snprintf (answer, sizeof (answer), "-1 No such file: %s", file);
+      else
+        snprintf (answer, sizeof (answer), "-1 stat failed with error %i.\n",
+            status);
+      RRDD_UPDATE_SEND;
+      return (0);
+    }
+    if (!S_ISREG (statbuf.st_mode))
+    {
+      pthread_mutex_unlock (&cache_lock);
+
+      snprintf (answer, sizeof (answer), "-1 Not a regular file: %s", file);
+      RRDD_UPDATE_SEND;
+      return (0);
+    }
+
     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
     if (ci == NULL)
     {
@@ -944,9 +966,6 @@ static int handle_request_update (int fd, /* {{{ */
     ci->flags = CI_FLAGS_IN_TREE;
 
     g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
-
-    RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
-        ci->file);
   } /* }}} */
   assert (ci != NULL);
 
@@ -1329,11 +1348,7 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
   int i;
 
   for (i = 0; i < config_listen_address_list_len; i++)
-  {
-    RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
-        "= %s", i, config_listen_address_list[i]);
     open_listen_socket (config_listen_address_list[i]);
-  }
 
   if (config_listen_address_list_len < 1)
     open_listen_socket (RRDCACHED_DEFAULT_ADDRESS);
@@ -1381,6 +1396,7 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
       struct sockaddr_storage client_sa;
       socklen_t client_sa_size;
       pthread_t tid;
+      pthread_attr_t attr;
 
       if (pollfds[i].revents == 0)
         continue;
@@ -1409,7 +1425,10 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
         continue;
       }
 
-      status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
+      pthread_attr_init (&attr);
+      pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
+
+      status = pthread_create (&tid, &attr, connection_thread_main,
           /* args = */ (void *) client_sd);
       if (status != 0)
       {
@@ -1436,8 +1455,6 @@ static void *listen_thread_main (void *args __attribute__((unused))) /* {{{ */
   }
   pthread_mutex_unlock (&connetion_threads_lock);
 
-  RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
-
   return (NULL);
 } /* }}} void *listen_thread_main */
 
@@ -1525,14 +1542,10 @@ static int daemonize (void) /* {{{ */
 
 static int cleanup (void) /* {{{ */
 {
-  RRDD_LOG (LOG_DEBUG, "cleanup ()");
-
   do_shutdown++;
 
-  RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
   pthread_cond_signal (&cache_cond);
   pthread_join (queue_thread, /* return = */ NULL);
-  RRDD_LOG (LOG_DEBUG, "cleanup: done");
 
   remove_pidfile ();