94ddf4065699bd47ee60aa985ead0ef9217b2d03
[rrdd.git] / src / rrdd.c
1 /**
2  * collectd - src/rrdd.c
3  * Copyright (C) 2008 Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #define RRDD_DEBUG 1
23
24 #include "rrdd.h"
25
26 #if RRDD_DEBUG
27 # define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
28 #else
29 # define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
30 #endif
31
32 /*
33  * Types
34  */
35 struct listen_socket_s
36 {
37   int fd;
38   char path[PATH_MAX + 1];
39 };
40 typedef struct listen_socket_s listen_socket_t;
41
42 struct cache_item_s;
43 typedef struct cache_item_s cache_item_t;
44 struct cache_item_s
45 {
46   char *file;
47   char **values;
48   int values_num;
49   time_t last_flush_time;
50 #define CI_FLAGS_IN_TREE  0x01
51 #define CI_FLAGS_IN_QUEUE 0x02
52   int flags;
53
54   cache_item_t *next;
55 };
56
57 /*
58  * Variables
59  */
60 static listen_socket_t *listen_fds = NULL;
61 static size_t listen_fds_num = 0;
62
63 static int do_shutdown = 0;
64
65 static pthread_t queue_thread;
66
67 static pthread_t *connetion_threads = NULL;
68 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
69 static int connetion_threads_num = 0;
70
71 /* Cache stuff */
72 static avl_tree_t     *cache_tree = NULL;
73 static cache_item_t   *cache_queue_head = NULL;
74 static cache_item_t   *cache_queue_tail = NULL;
75 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
76 static pthread_cond_t  cache_cond = PTHREAD_COND_INITIALIZER;
77
78 /* 
79  * Functions
80  */
81 static void sig_int_handler (int signal) /* {{{ */
82 {
83   do_shutdown++;
84 } /* }}} void sig_int_handler */
85
86 static void sig_term_handler (int signal) /* {{{ */
87 {
88   do_shutdown++;
89 } /* }}} void sig_term_handler */
90
91 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
92 {
93   cache_item_t *c0 = (cache_item_t *) v0;
94   cache_item_t *c1 = (cache_item_t *) v1;
95
96   assert (c0->file != NULL);
97   assert (c1->file != NULL);
98
99   return (strcmp (c0->file, c1->file));
100 } /* }}} int cache_tree_compare */
101
102 static void cache_tree_free (void *v) /* {{{ */
103 {
104   cache_item_t *c = (cache_item_t *) v;
105
106   assert (c->values_num == 0);
107   assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
108   assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
109
110   free (c->file);
111   c->file = NULL;
112   free (c);
113 } /* }}} void cache_tree_free */
114
115 static void *queue_thread_main (void *args) /* {{{ */
116 {
117   pthread_mutex_lock (&cache_lock);
118   while ((do_shutdown == 0) || (cache_queue_head != NULL))
119   {
120     cache_item_t *ci;
121
122     char *file;
123     char **values;
124     int values_num;
125     int status;
126     int i;
127
128     if (cache_queue_head == NULL)
129       pthread_cond_wait (&cache_cond, &cache_lock);
130
131     if (cache_queue_head == NULL)
132       continue;
133
134     ci = cache_queue_head;
135
136     /* copy the relevant parts */
137     file = strdup (ci->file);
138     if (file == NULL)
139     {
140       RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
141       continue;
142     }
143
144     values = ci->values;
145     values_num = ci->values_num;
146
147     ci->values = NULL;
148     ci->values_num = 0;
149
150     ci->last_flush_time = time (NULL);
151     ci->flags &= ~(CI_FLAGS_IN_QUEUE);
152
153     cache_queue_head = ci->next;
154     if (cache_queue_head == NULL)
155       cache_queue_tail = NULL;
156     ci->next = NULL;
157
158     pthread_mutex_unlock (&cache_lock);
159
160     RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
161         file, values_num, (void *) values);
162
163     status = rrd_update_r (file, NULL, values_num, values);
164     if (status != 0)
165     {
166       RRDD_LOG (LOG_ERR, "queue_thread_main: "
167           "rrd_update_r failed with status %i.",
168           status);
169     }
170
171     free (file);
172     for (i = 0; i < values_num; i++)
173       free (values[i]);
174
175     pthread_mutex_lock (&cache_lock);
176   } /* while (do_shutdown == 0) */
177   pthread_mutex_unlock (&cache_lock);
178
179   RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
180
181   return (NULL);
182 } /* }}} void *queue_thread_main */
183
184 static int handle_request_update (int fd, /* {{{ */
185     char *buffer, int buffer_size)
186 {
187   char *file;
188   char *value;
189   char *buffer_ptr;
190   int values_num = 0;
191   int status;
192
193   time_t now;
194
195   avl_node_t *node;
196   cache_item_t ci_temp;
197   cache_item_t *ci;
198
199   char answer[4096];
200
201   now = time (NULL);
202
203   RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
204       fd, (void *) buffer, buffer_size);
205
206   buffer_ptr = buffer;
207
208   file = buffer_ptr;
209   buffer_ptr += strlen (file) + 1;
210
211   ci_temp.file = file;
212
213   pthread_mutex_lock (&cache_lock);
214
215   node = avl_search (cache_tree, (void *) &ci_temp);
216   if (node == NULL)
217   {
218     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
219     if (ci == NULL)
220     {
221       pthread_mutex_unlock (&cache_lock);
222       RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
223       return (-1);
224     }
225     memset (ci, 0, sizeof (cache_item_t));
226
227     ci->file = strdup (file);
228     if (ci->file == NULL)
229     {
230       pthread_mutex_unlock (&cache_lock);
231       RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
232       free (ci);
233       return (-1);
234     }
235
236     ci->values = NULL;
237     ci->values_num = 0;
238     ci->last_flush_time = now;
239     ci->flags = CI_FLAGS_IN_TREE;
240
241     if (avl_insert (cache_tree, (void *) ci) == NULL)
242     {
243       pthread_mutex_unlock (&cache_lock);
244       RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
245       free (ci->file);
246       free (ci);
247       return (-1);
248     }
249
250     RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
251         ci->file);
252   }
253   else /* if (ci != NULL) */
254   {
255     ci = (cache_item_t *) node->item;
256   }
257   assert (ci != NULL);
258
259   while (*buffer_ptr != 0)
260   {
261     char **temp;
262
263     value = buffer_ptr;
264     buffer_ptr += strlen (value) + 1;
265
266     temp = (char **) realloc (ci->values,
267         sizeof (char *) * (ci->values_num + 1));
268     if (temp == NULL)
269     {
270       RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
271       continue;
272     }
273     ci->values = temp;
274
275     ci->values[ci->values_num] = strdup (value);
276     if (ci->values[ci->values_num] == NULL)
277     {
278       RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
279       continue;
280     }
281     ci->values_num++;
282
283     values_num++;
284   }
285
286   /* FIXME: Timeout should not be hard-coded. */
287   if (((now - ci->last_flush_time) > 300)
288       && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
289   {
290     RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
291         ci->file);
292
293     assert (ci->next == NULL);
294
295     if (cache_queue_tail == NULL)
296       cache_queue_head = ci;
297     else
298       cache_queue_tail->next = ci;
299     cache_queue_tail = ci;
300
301     pthread_cond_signal (&cache_cond);
302   }
303
304   pthread_mutex_unlock (&cache_lock);
305
306   snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
307   answer[sizeof (answer) - 1] = 0;
308
309   status = write (fd, answer, sizeof (answer));
310   if (status < 0)
311   {
312     status = errno;
313     RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
314     return (status);
315   }
316
317   return (0);
318 } /* }}} int handle_request_update */
319
320 static int handle_request (int fd) /* {{{ */
321 {
322   char buffer[4096];
323   int buffer_size;
324
325   RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
326
327   buffer_size = read (fd, buffer, sizeof (buffer));
328   if (buffer_size < 1)
329   {
330     RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
331     return (-1);
332   }
333   assert (((size_t) buffer_size) <= sizeof (buffer));
334
335   if ((buffer[buffer_size - 2] != 0)
336       || (buffer[buffer_size - 1] != 0))
337   {
338     RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
339     return (-1);
340   }
341
342   /* fields in the buffer a separated by null bytes. */
343   if (strcmp (buffer, "update") == 0)
344   {
345     int offset = strlen ("update") + 1;
346     return (handle_request_update (fd, buffer + offset,
347           buffer_size - offset));
348   }
349   else
350   {
351     RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
352     return (-1);
353   }
354 } /* }}} int handle_request */
355
356 static void *connection_thread_main (void *args) /* {{{ */
357 {
358   pthread_t self;
359   int i;
360   int fd;
361   
362   fd = *((int *) args);
363
364   RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
365       "connetion_threads[]..");
366   pthread_mutex_lock (&connetion_threads_lock);
367   {
368     pthread_t *temp;
369
370     temp = (pthread_t *) realloc (connetion_threads,
371         sizeof (pthread_t) * (connetion_threads_num + 1));
372     if (temp == NULL)
373     {
374       RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
375     }
376     else
377     {
378       connetion_threads = temp;
379       connetion_threads[connetion_threads_num] = pthread_self ();
380       connetion_threads_num++;
381     }
382   }
383   pthread_mutex_unlock (&connetion_threads_lock);
384   RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
385
386   while (do_shutdown == 0)
387   {
388     struct pollfd pollfd;
389     int status;
390
391     pollfd.fd = fd;
392     pollfd.events = POLLIN | POLLPRI;
393     pollfd.revents = 0;
394
395     status = poll (&pollfd, 1, /* timeout = */ 500);
396     if (status == 0) /* timeout */
397       continue;
398     else if (status < 0) /* error */
399     {
400       status = errno;
401       if (status == EINTR)
402         continue;
403       RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
404       continue;
405     }
406
407     if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
408     {
409       close (fd);
410       break;
411     }
412     else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
413     {
414       RRDD_LOG (LOG_WARNING, "connection_thread_main: poll(2) returned "
415           "something unexpected.");
416       close (fd);
417       break;
418     }
419
420     status = handle_request (fd);
421     if (status != 0)
422     {
423       close (fd);
424       break;
425     }
426   }
427
428   self = pthread_self ();
429   /* Remove this thread from the connection threads list */
430   pthread_mutex_lock (&connetion_threads_lock);
431   /* Find out own index in the array */
432   for (i = 0; i < connetion_threads_num; i++)
433     if (pthread_equal (connetion_threads[i], self) != 0)
434       break;
435   assert (i < connetion_threads_num);
436
437   /* Move the trailing threads forward. */
438   if (i < (connetion_threads_num - 1))
439   {
440     memmove (connetion_threads + i,
441         connetion_threads + i + 1,
442         sizeof (pthread_t) * (connetion_threads_num - i - 1));
443   }
444
445   connetion_threads_num--;
446   pthread_mutex_unlock (&connetion_threads_lock);
447
448   free (args);
449   return (NULL);
450 } /* }}} void *connection_thread_main */
451
452 static int open_listen_socket (const char *path) /* {{{ */
453 {
454   int fd;
455   struct sockaddr_un sa;
456   listen_socket_t *temp;
457   int status;
458
459   temp = (listen_socket_t *) realloc (listen_fds,
460       sizeof (listen_fds[0]) * (listen_fds_num + 1));
461   if (temp == NULL)
462   {
463     RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
464     return (-1);
465   }
466   listen_fds = temp;
467   memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
468
469   fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
470   if (fd < 0)
471   {
472     RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
473     return (-1);
474   }
475
476   memset (&sa, 0, sizeof (sa));
477   sa.sun_family = AF_UNIX;
478   strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
479
480   status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
481   if (status != 0)
482   {
483     RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
484     close (fd);
485     unlink (path);
486     return (-1);
487   }
488
489   status = listen (fd, /* backlog = */ 10);
490   if (status != 0)
491   {
492     RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
493     close (fd);
494     unlink (path);
495     return (-1);
496   }
497   
498   listen_fds[listen_fds_num].fd = fd;
499   strncpy (listen_fds[listen_fds_num].path, path,
500       sizeof (listen_fds[listen_fds_num].path) - 1);
501   listen_fds_num++;
502
503   return (0);
504 } /* }}} int open_listen_socket */
505
506 static int close_listen_sockets (void) /* {{{ */
507 {
508   size_t i;
509
510   for (i = 0; i < listen_fds_num; i++)
511   {
512     close (listen_fds[i].fd);
513     unlink (listen_fds[i].path);
514   }
515
516   free (listen_fds);
517   listen_fds = NULL;
518   listen_fds_num = 0;
519
520   return (0);
521 } /* }}} int close_listen_sockets */
522
523 static void *listen_thread_main (void *args) /* {{{ */
524 {
525   char buffer[4096];
526   int status;
527   int i;
528
529   status = open_listen_socket (RRDD_SOCK_PATH);
530   if (status != 0)
531   {
532     RRDD_LOG (LOG_ERR, "listen_thread_main: open_listen_socket failed.");
533     return (NULL);
534   }
535
536   while (do_shutdown == 0)
537   {
538     int *client_sd;
539     struct sockaddr_un client_sa;
540     socklen_t client_sa_size;
541     pthread_t tid;
542
543     client_sd = (int *) malloc (sizeof (int));
544     if (client_sd == NULL)
545     {
546       RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
547       sleep (120);
548       continue;
549     }
550
551     client_sa_size = sizeof (client_sa);
552     /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
553     *client_sd = accept (listen_fds[0].fd,
554         (struct sockaddr *) &client_sa, &client_sa_size);
555     if (*client_sd < 0)
556     {
557       RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
558       continue;
559     }
560
561     RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
562         *client_sd);
563
564     status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
565         /* args = */ (void *) client_sd);
566     if (status != 0)
567     {
568       RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
569       close (*client_sd);
570       free (client_sd);
571       continue;
572     }
573
574     RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
575         "tid = %lu",
576         *((unsigned long *) &tid));
577   } /* while (do_shutdown == 0) */
578
579   close_listen_sockets ();
580
581   pthread_mutex_lock (&connetion_threads_lock);
582   while (connetion_threads_num > 0)
583   {
584     pthread_t wait_for;
585
586     wait_for = connetion_threads[0];
587
588     pthread_mutex_unlock (&connetion_threads_lock);
589     pthread_join (wait_for, /* retval = */ NULL);
590     pthread_mutex_lock (&connetion_threads_lock);
591   }
592   pthread_mutex_unlock (&connetion_threads_lock);
593
594   RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
595
596   return (NULL);
597 } /* }}} void *listen_thread_main */
598
599 static int daemonize (void) /* {{{ */
600 {
601   pid_t child;
602   int status;
603
604 #if !RRDD_DEBUG
605   child = fork ();
606   if (child < 0)
607   {
608     fprintf (stderr, "daemonize: fork(2) failed.\n");
609     return (-1);
610   }
611   else if (child > 0)
612   {
613     return (1);
614   }
615
616   /* Change into the /tmp directory. */
617   chdir ("/tmp");
618
619   /* Become session leader */
620   setsid ();
621
622   /* Open the first three file descriptors to /dev/null */
623   close (2);
624   close (1);
625   close (0);
626
627   open ("/dev/null", O_RDWR);
628   dup (0);
629   dup (0);
630 #endif /* RRDD_DEBUG */
631
632   {
633     struct sigaction sa;
634
635     memset (&sa, 0, sizeof (sa));
636     sa.sa_handler = sig_int_handler;
637     sigaction (SIGINT, &sa, NULL);
638
639     memset (&sa, 0, sizeof (sa));
640     sa.sa_handler = sig_term_handler;
641     sigaction (SIGINT, &sa, NULL);
642
643     memset (&sa, 0, sizeof (sa));
644     sa.sa_handler = SIG_IGN;
645     sigaction (SIGPIPE, &sa, NULL);
646   }
647
648   openlog ("rrdd", LOG_PID, LOG_DAEMON);
649
650   cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
651   if (cache_tree == NULL)
652   {
653     RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
654     return (-1);
655   }
656
657   memset (&queue_thread, 0, sizeof (queue_thread));
658   status = pthread_create (&queue_thread, /* attr = */ NULL,
659       queue_thread_main, /* args = */ NULL);
660   if (status != 0)
661   {
662     RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
663     return (-1);
664   }
665
666   return (0);
667 } /* }}} int daemonize */
668
669 static int cleanup (void) /* {{{ */
670 {
671   RRDD_LOG (LOG_DEBUG, "cleanup ()");
672
673   do_shutdown++;
674
675   RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
676   pthread_cond_signal (&cache_cond);
677   pthread_join (queue_thread, /* return = */ NULL);
678   RRDD_LOG (LOG_DEBUG, "cleanup: done");
679
680   closelog ();
681
682   return (0);
683 } /* }}} int cleanup */
684
685 int main (int argc, char **argv)
686 {
687   int status;
688
689   printf ("%s by Florian Forster, Version %s\n",
690       PACKAGE_NAME, PACKAGE_VERSION);
691
692   status = daemonize ();
693   if (status == 1)
694   {
695     struct sigaction sigchld;
696
697     memset (&sigchld, 0, sizeof (sigchld));
698     sigchld.sa_handler = SIG_IGN;
699     sigaction (SIGCHLD, &sigchld, NULL);
700
701     return (0);
702   }
703   else if (status != 0)
704   {
705     fprintf (stderr, "daemonize failed, exiting.\n");
706     return (1);
707   }
708
709   listen_thread_main (NULL);
710
711   cleanup ();
712
713   return (0);
714 } /* int main */
715
716 /*
717  * vim: set sw=2 sts=2 ts=8 et fdm=marker :
718  */