src/rrdd.c: Implement listening on multiple sockets.
[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 #define DEFAULT_PORT "42217"
33
34 /*
35  * Types
36  */
37 struct listen_socket_s
38 {
39   int fd;
40   char path[PATH_MAX + 1];
41 };
42 typedef struct listen_socket_s listen_socket_t;
43
44 struct cache_item_s;
45 typedef struct cache_item_s cache_item_t;
46 struct cache_item_s
47 {
48   char *file;
49   char **values;
50   int values_num;
51   time_t last_flush_time;
52 #define CI_FLAGS_IN_TREE  0x01
53 #define CI_FLAGS_IN_QUEUE 0x02
54   int flags;
55
56   cache_item_t *next;
57 };
58
59 /*
60  * Variables
61  */
62 static listen_socket_t *listen_fds = NULL;
63 static size_t listen_fds_num = 0;
64
65 static int do_shutdown = 0;
66
67 static pthread_t queue_thread;
68
69 static pthread_t *connetion_threads = NULL;
70 static pthread_mutex_t connetion_threads_lock = PTHREAD_MUTEX_INITIALIZER;
71 static int connetion_threads_num = 0;
72
73 /* Cache stuff */
74 static avl_tree_t     *cache_tree = NULL;
75 static cache_item_t   *cache_queue_head = NULL;
76 static cache_item_t   *cache_queue_tail = NULL;
77 static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
78 static pthread_cond_t  cache_cond = PTHREAD_COND_INITIALIZER;
79
80 static int config_write_interval = 300;
81 static int config_flush_interval = 3600;
82
83 static char **config_listen_address_list = NULL;
84 static int config_listen_address_list_len = 0;
85
86 /* 
87  * Functions
88  */
89 static void sig_int_handler (int signal) /* {{{ */
90 {
91   do_shutdown++;
92 } /* }}} void sig_int_handler */
93
94 static void sig_term_handler (int signal) /* {{{ */
95 {
96   do_shutdown++;
97 } /* }}} void sig_term_handler */
98
99 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
100 {
101   cache_item_t *c0 = (cache_item_t *) v0;
102   cache_item_t *c1 = (cache_item_t *) v1;
103
104   assert (c0->file != NULL);
105   assert (c1->file != NULL);
106
107   return (strcmp (c0->file, c1->file));
108 } /* }}} int cache_tree_compare */
109
110 static void cache_tree_free (void *v) /* {{{ */
111 {
112   cache_item_t *c = (cache_item_t *) v;
113
114   assert (c->values_num == 0);
115   assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
116   assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
117
118   free (c->file);
119   c->file = NULL;
120   free (c);
121 } /* }}} void cache_tree_free */
122
123 static void *queue_thread_main (void *args) /* {{{ */
124 {
125   pthread_mutex_lock (&cache_lock);
126   while ((do_shutdown == 0) || (cache_queue_head != NULL))
127   {
128     cache_item_t *ci;
129
130     char *file;
131     char **values;
132     int values_num;
133     int status;
134     int i;
135
136     if (cache_queue_head == NULL)
137       pthread_cond_wait (&cache_cond, &cache_lock);
138
139     if (cache_queue_head == NULL)
140       continue;
141
142     ci = cache_queue_head;
143
144     /* copy the relevant parts */
145     file = strdup (ci->file);
146     if (file == NULL)
147     {
148       RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
149       continue;
150     }
151
152     values = ci->values;
153     values_num = ci->values_num;
154
155     ci->values = NULL;
156     ci->values_num = 0;
157
158     ci->last_flush_time = time (NULL);
159     ci->flags &= ~(CI_FLAGS_IN_QUEUE);
160
161     cache_queue_head = ci->next;
162     if (cache_queue_head == NULL)
163       cache_queue_tail = NULL;
164     ci->next = NULL;
165
166     pthread_mutex_unlock (&cache_lock);
167
168     RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
169         file, values_num, (void *) values);
170
171     status = rrd_update_r (file, NULL, values_num, (void *) values);
172     if (status != 0)
173     {
174       RRDD_LOG (LOG_ERR, "queue_thread_main: "
175           "rrd_update_r failed with status %i.",
176           status);
177     }
178
179     free (file);
180     for (i = 0; i < values_num; i++)
181       free (values[i]);
182
183     pthread_mutex_lock (&cache_lock);
184   } /* while (do_shutdown == 0) */
185   pthread_mutex_unlock (&cache_lock);
186
187   RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
188
189   return (NULL);
190 } /* }}} void *queue_thread_main */
191
192 static int handle_request_update (int fd, /* {{{ */
193     char *buffer, int buffer_size)
194 {
195   char *file;
196   char *value;
197   char *buffer_ptr;
198   int values_num = 0;
199   int status;
200
201   time_t now;
202
203   avl_node_t *node;
204   cache_item_t ci_temp;
205   cache_item_t *ci;
206
207   char answer[4096];
208
209   now = time (NULL);
210
211   RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
212       fd, (void *) buffer, buffer_size);
213
214   buffer_ptr = buffer;
215
216   file = buffer_ptr;
217   buffer_ptr += strlen (file) + 1;
218
219   ci_temp.file = file;
220
221   pthread_mutex_lock (&cache_lock);
222
223   node = avl_search (cache_tree, (void *) &ci_temp);
224   if (node == NULL)
225   {
226     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
227     if (ci == NULL)
228     {
229       pthread_mutex_unlock (&cache_lock);
230       RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
231       return (-1);
232     }
233     memset (ci, 0, sizeof (cache_item_t));
234
235     ci->file = strdup (file);
236     if (ci->file == NULL)
237     {
238       pthread_mutex_unlock (&cache_lock);
239       RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
240       free (ci);
241       return (-1);
242     }
243
244     ci->values = NULL;
245     ci->values_num = 0;
246     ci->last_flush_time = now;
247     ci->flags = CI_FLAGS_IN_TREE;
248
249     if (avl_insert (cache_tree, (void *) ci) == NULL)
250     {
251       pthread_mutex_unlock (&cache_lock);
252       RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
253       free (ci->file);
254       free (ci);
255       return (-1);
256     }
257
258     RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
259         ci->file);
260   }
261   else /* if (ci != NULL) */
262   {
263     ci = (cache_item_t *) node->item;
264   }
265   assert (ci != NULL);
266
267   while (*buffer_ptr != 0)
268   {
269     char **temp;
270
271     value = buffer_ptr;
272     buffer_ptr += strlen (value) + 1;
273
274     temp = (char **) realloc (ci->values,
275         sizeof (char *) * (ci->values_num + 1));
276     if (temp == NULL)
277     {
278       RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
279       continue;
280     }
281     ci->values = temp;
282
283     ci->values[ci->values_num] = strdup (value);
284     if (ci->values[ci->values_num] == NULL)
285     {
286       RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
287       continue;
288     }
289     ci->values_num++;
290
291     values_num++;
292   }
293
294   if (((now - ci->last_flush_time) >= config_write_interval)
295       && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
296   {
297     RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
298         ci->file);
299
300     assert (ci->next == NULL);
301
302     if (cache_queue_tail == NULL)
303       cache_queue_head = ci;
304     else
305       cache_queue_tail->next = ci;
306     cache_queue_tail = ci;
307
308     pthread_cond_signal (&cache_cond);
309   }
310
311   pthread_mutex_unlock (&cache_lock);
312
313   snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
314   answer[sizeof (answer) - 1] = 0;
315
316   status = write (fd, answer, sizeof (answer));
317   if (status < 0)
318   {
319     status = errno;
320     RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
321     return (status);
322   }
323
324   return (0);
325 } /* }}} int handle_request_update */
326
327 static int handle_request (int fd) /* {{{ */
328 {
329   char buffer[4096];
330   int buffer_size;
331
332   RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
333
334   buffer_size = read (fd, buffer, sizeof (buffer));
335   if (buffer_size < 1)
336   {
337     RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
338     return (-1);
339   }
340   assert (((size_t) buffer_size) <= sizeof (buffer));
341
342   if ((buffer[buffer_size - 2] != 0)
343       || (buffer[buffer_size - 1] != 0))
344   {
345     RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
346     return (-1);
347   }
348
349   /* fields in the buffer a separated by null bytes. */
350   if (strcmp (buffer, "update") == 0)
351   {
352     int offset = strlen ("update") + 1;
353     return (handle_request_update (fd, buffer + offset,
354           buffer_size - offset));
355   }
356   else
357   {
358     RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
359     return (-1);
360   }
361 } /* }}} int handle_request */
362
363 static void *connection_thread_main (void *args) /* {{{ */
364 {
365   pthread_t self;
366   int i;
367   int fd;
368   
369   fd = *((int *) args);
370
371   RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
372       "connetion_threads[]..");
373   pthread_mutex_lock (&connetion_threads_lock);
374   {
375     pthread_t *temp;
376
377     temp = (pthread_t *) realloc (connetion_threads,
378         sizeof (pthread_t) * (connetion_threads_num + 1));
379     if (temp == NULL)
380     {
381       RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
382     }
383     else
384     {
385       connetion_threads = temp;
386       connetion_threads[connetion_threads_num] = pthread_self ();
387       connetion_threads_num++;
388     }
389   }
390   pthread_mutex_unlock (&connetion_threads_lock);
391   RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
392
393   while (do_shutdown == 0)
394   {
395     struct pollfd pollfd;
396     int status;
397
398     pollfd.fd = fd;
399     pollfd.events = POLLIN | POLLPRI;
400     pollfd.revents = 0;
401
402     status = poll (&pollfd, 1, /* timeout = */ 500);
403     if (status == 0) /* timeout */
404       continue;
405     else if (status < 0) /* error */
406     {
407       status = errno;
408       if (status == EINTR)
409         continue;
410       RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
411       continue;
412     }
413
414     if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
415     {
416       RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
417           "poll(2) returned POLLHUP.");
418       close (fd);
419       break;
420     }
421     else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
422     {
423       RRDD_LOG (LOG_WARNING, "connection_thread_main: "
424           "poll(2) returned something unexpected: %#04hx",
425           pollfd.revents);
426       close (fd);
427       break;
428     }
429
430     status = handle_request (fd);
431     if (status != 0)
432     {
433       close (fd);
434       break;
435     }
436   }
437
438   self = pthread_self ();
439   /* Remove this thread from the connection threads list */
440   pthread_mutex_lock (&connetion_threads_lock);
441   /* Find out own index in the array */
442   for (i = 0; i < connetion_threads_num; i++)
443     if (pthread_equal (connetion_threads[i], self) != 0)
444       break;
445   assert (i < connetion_threads_num);
446
447   /* Move the trailing threads forward. */
448   if (i < (connetion_threads_num - 1))
449   {
450     memmove (connetion_threads + i,
451         connetion_threads + i + 1,
452         sizeof (pthread_t) * (connetion_threads_num - i - 1));
453   }
454
455   connetion_threads_num--;
456   pthread_mutex_unlock (&connetion_threads_lock);
457
458   free (args);
459   return (NULL);
460 } /* }}} void *connection_thread_main */
461
462 static int open_listen_socket_unix (const char *path) /* {{{ */
463 {
464   int fd;
465   struct sockaddr_un sa;
466   listen_socket_t *temp;
467   int status;
468
469   temp = (listen_socket_t *) realloc (listen_fds,
470       sizeof (listen_fds[0]) * (listen_fds_num + 1));
471   if (temp == NULL)
472   {
473     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
474     return (-1);
475   }
476   listen_fds = temp;
477   memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
478
479   fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
480   if (fd < 0)
481   {
482     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
483     return (-1);
484   }
485
486   memset (&sa, 0, sizeof (sa));
487   sa.sun_family = AF_UNIX;
488   strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
489
490   status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
491   if (status != 0)
492   {
493     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
494     close (fd);
495     unlink (path);
496     return (-1);
497   }
498
499   status = listen (fd, /* backlog = */ 10);
500   if (status != 0)
501   {
502     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
503     close (fd);
504     unlink (path);
505     return (-1);
506   }
507   
508   listen_fds[listen_fds_num].fd = fd;
509   snprintf (listen_fds[listen_fds_num].path,
510       sizeof (listen_fds[listen_fds_num].path) - 1,
511       "unix:%s", path);
512   listen_fds_num++;
513
514   return (0);
515 } /* }}} int open_listen_socket_unix */
516
517 static int open_listen_socket (const char *addr) /* {{{ */
518 {
519   struct addrinfo ai_hints;
520   struct addrinfo *ai_res;
521   struct addrinfo *ai_ptr;
522   int status;
523
524   assert (addr != NULL);
525
526   if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
527     return (open_listen_socket_unix (addr + strlen ("unix:")));
528   else if (addr[0] == '/')
529     return (open_listen_socket_unix (addr));
530
531   memset (&ai_hints, 0, sizeof (ai_hints));
532   ai_hints.ai_flags = 0;
533 #ifdef AI_ADDRCONFIG
534   ai_hints.ai_flags |= AI_ADDRCONFIG;
535 #endif
536   ai_hints.ai_family = AF_UNSPEC;
537   ai_hints.ai_socktype = SOCK_STREAM;
538
539   ai_res = NULL;
540   status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
541   if (status != 0)
542   {
543     RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
544         "%s", addr, gai_strerror (status));
545     return (-1);
546   }
547
548   for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
549   {
550     int fd;
551     struct sockaddr_storage sa;
552     listen_socket_t *temp;
553
554     temp = (listen_socket_t *) realloc (listen_fds,
555         sizeof (listen_fds[0]) * (listen_fds_num + 1));
556     if (temp == NULL)
557     {
558       RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
559       continue;
560     }
561     listen_fds = temp;
562     memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
563
564     fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
565     if (fd < 0)
566     {
567       RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
568       continue;
569     }
570
571     status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
572     if (status != 0)
573     {
574       RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
575       close (fd);
576       continue;
577     }
578
579     status = listen (fd, /* backlog = */ 10);
580     if (status != 0)
581     {
582       RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
583       close (fd);
584       return (-1);
585     }
586
587     listen_fds[listen_fds_num].fd = fd;
588     strncpy (listen_fds[listen_fds_num].path, addr,
589         sizeof (listen_fds[listen_fds_num].path) - 1);
590     listen_fds_num++;
591   } /* for (ai_ptr) */
592
593   return (0);
594 } /* }}} int open_listen_socket */
595
596 static int close_listen_sockets (void) /* {{{ */
597 {
598   size_t i;
599
600   for (i = 0; i < listen_fds_num; i++)
601   {
602     close (listen_fds[i].fd);
603     if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
604       unlink (listen_fds[i].path + strlen ("unix:"));
605   }
606
607   free (listen_fds);
608   listen_fds = NULL;
609   listen_fds_num = 0;
610
611   return (0);
612 } /* }}} int close_listen_sockets */
613
614 static void *listen_thread_main (void *args) /* {{{ */
615 {
616   char buffer[4096];
617   struct pollfd *pollfds;
618   int pollfds_num;
619   int status;
620   int i;
621
622   for (i = 0; i < config_listen_address_list_len; i++)
623   {
624     RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
625         "= %s", i, config_listen_address_list[i]);
626     open_listen_socket (config_listen_address_list[i]);
627   }
628
629   if (listen_fds_num < 1)
630   {
631     RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
632         "could be opened. Sorry.");
633     return (NULL);
634   }
635
636   pollfds_num = listen_fds_num;
637   pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
638   if (pollfds == NULL)
639   {
640     RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
641     return (NULL);
642   }
643   memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
644
645   while (do_shutdown == 0)
646   {
647     assert (pollfds_num == listen_fds_num);
648     for (i = 0; i < pollfds_num; i++)
649     {
650       pollfds[i].fd = listen_fds[i].fd;
651       pollfds[i].events = POLLIN | POLLPRI;
652       pollfds[i].revents = 0;
653     }
654
655     status = poll (pollfds, pollfds_num, /* timeout = */ -1);
656     if (status < 1)
657     {
658       status = errno;
659       if (status != EINTR)
660       {
661         RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
662       }
663       continue;
664     }
665
666     for (i = 0; i < pollfds_num; i++)
667     {
668       int *client_sd;
669       struct sockaddr_storage client_sa;
670       socklen_t client_sa_size;
671       pthread_t tid;
672
673       if (pollfds[i].revents == 0)
674         continue;
675
676       if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
677       {
678         RRDD_LOG (LOG_ERR, "listen_thread_main: "
679             "poll(2) returned something unexpected for listen FD #%i.",
680             pollfds[i].fd);
681         continue;
682       }
683
684       client_sd = (int *) malloc (sizeof (int));
685       if (client_sd == NULL)
686       {
687         RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
688         continue;
689       }
690
691       client_sa_size = sizeof (client_sa);
692       *client_sd = accept (pollfds[i].fd,
693           (struct sockaddr *) &client_sa, &client_sa_size);
694       if (*client_sd < 0)
695       {
696         RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
697         continue;
698       }
699
700       RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
701           *client_sd);
702
703       status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
704           /* args = */ (void *) client_sd);
705       if (status != 0)
706       {
707         RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
708         close (*client_sd);
709         free (client_sd);
710         continue;
711       }
712
713       RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
714           "tid = %lu",
715           *((unsigned long *) &tid));
716     } /* for (pollfds_num) */
717   } /* while (do_shutdown == 0) */
718
719   close_listen_sockets ();
720
721   pthread_mutex_lock (&connetion_threads_lock);
722   while (connetion_threads_num > 0)
723   {
724     pthread_t wait_for;
725
726     wait_for = connetion_threads[0];
727
728     pthread_mutex_unlock (&connetion_threads_lock);
729     pthread_join (wait_for, /* retval = */ NULL);
730     pthread_mutex_lock (&connetion_threads_lock);
731   }
732   pthread_mutex_unlock (&connetion_threads_lock);
733
734   RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
735
736   return (NULL);
737 } /* }}} void *listen_thread_main */
738
739 static int daemonize (void) /* {{{ */
740 {
741   pid_t child;
742   int status;
743
744 #if !RRDD_DEBUG
745   child = fork ();
746   if (child < 0)
747   {
748     fprintf (stderr, "daemonize: fork(2) failed.\n");
749     return (-1);
750   }
751   else if (child > 0)
752   {
753     return (1);
754   }
755
756   /* Change into the /tmp directory. */
757   chdir ("/tmp");
758
759   /* Become session leader */
760   setsid ();
761
762   /* Open the first three file descriptors to /dev/null */
763   close (2);
764   close (1);
765   close (0);
766
767   open ("/dev/null", O_RDWR);
768   dup (0);
769   dup (0);
770 #endif /* RRDD_DEBUG */
771
772   {
773     struct sigaction sa;
774
775     memset (&sa, 0, sizeof (sa));
776     sa.sa_handler = sig_int_handler;
777     sigaction (SIGINT, &sa, NULL);
778
779     memset (&sa, 0, sizeof (sa));
780     sa.sa_handler = sig_term_handler;
781     sigaction (SIGINT, &sa, NULL);
782
783     memset (&sa, 0, sizeof (sa));
784     sa.sa_handler = SIG_IGN;
785     sigaction (SIGPIPE, &sa, NULL);
786   }
787
788   openlog ("rrdd", LOG_PID, LOG_DAEMON);
789
790   cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
791   if (cache_tree == NULL)
792   {
793     RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
794     return (-1);
795   }
796
797   memset (&queue_thread, 0, sizeof (queue_thread));
798   status = pthread_create (&queue_thread, /* attr = */ NULL,
799       queue_thread_main, /* args = */ NULL);
800   if (status != 0)
801   {
802     RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
803     return (-1);
804   }
805
806   return (0);
807 } /* }}} int daemonize */
808
809 static int cleanup (void) /* {{{ */
810 {
811   RRDD_LOG (LOG_DEBUG, "cleanup ()");
812
813   do_shutdown++;
814
815   RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
816   pthread_cond_signal (&cache_cond);
817   pthread_join (queue_thread, /* return = */ NULL);
818   RRDD_LOG (LOG_DEBUG, "cleanup: done");
819
820   closelog ();
821
822   return (0);
823 } /* }}} int cleanup */
824
825 static int read_options (int argc, char **argv) /* {{{ */
826 {
827   int option;
828   int status = 0;
829
830   while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
831   {
832     switch (option)
833     {
834       case 'l':
835       {
836         char **temp;
837
838         temp = (char **) realloc (config_listen_address_list,
839             sizeof (char *) * (config_listen_address_list_len + 1));
840         if (temp == NULL)
841         {
842           fprintf (stderr, "read_options: realloc failed.\n");
843           return (2);
844         }
845         config_listen_address_list = temp;
846
847         temp[config_listen_address_list_len] = strdup (optarg);
848         if (temp[config_listen_address_list_len] == NULL)
849         {
850           fprintf (stderr, "read_options: strdup failed.\n");
851           return (2);
852         }
853         config_listen_address_list_len++;
854       }
855       break;
856
857       case 'f':
858       {
859         int temp;
860
861         temp = atoi (optarg);
862         if (temp > 0)
863           config_flush_interval = temp;
864         else
865         {
866           fprintf (stderr, "Invalid flush interval: %s\n", optarg);
867           status = 3;
868         }
869       }
870       break;
871
872       case 'w':
873       {
874         int temp;
875
876         temp = atoi (optarg);
877         if (temp > 0)
878           config_write_interval = temp;
879         else
880         {
881           fprintf (stderr, "Invalid write interval: %s\n", optarg);
882           status = 2;
883         }
884       }
885       break;
886
887       case 'h':
888       case '?':
889         printf ("RRDd %s  Copyright (C) 2008 Florian octo Forster\n"
890             "\n"
891             "Usage: rrdd [options]\n"
892             "\n"
893             "Valid options are:\n"
894             "  -l <address>  Socket address to listen to.\n"
895             "  -w <seconds>  Interval in which to write data.\n"
896             "  -f <seconds>  Interval in which to flush dead data.\n"
897             "\n"
898             "For more information and a detailed description of all options "
899             "please refer\n"
900             "to the rrdd(1) manual page.\n",
901             PACKAGE_VERSION);
902         status = -1;
903         break;
904     } /* switch (option) */
905   } /* while (getopt) */
906
907   return (status);
908 } /* }}} int read_options */
909
910 int main (int argc, char **argv)
911 {
912   int status;
913
914   status = read_options (argc, argv);
915   if (status != 0)
916   {
917     if (status < 0)
918       status = 0;
919     return (status);
920   }
921
922   status = daemonize ();
923   if (status == 1)
924   {
925     struct sigaction sigchld;
926
927     memset (&sigchld, 0, sizeof (sigchld));
928     sigchld.sa_handler = SIG_IGN;
929     sigaction (SIGCHLD, &sigchld, NULL);
930
931     return (0);
932   }
933   else if (status != 0)
934   {
935     fprintf (stderr, "daemonize failed, exiting.\n");
936     return (1);
937   }
938
939   listen_thread_main (NULL);
940
941   cleanup ();
942
943   return (0);
944 } /* int main */
945
946 /*
947  * vim: set sw=2 sts=2 ts=8 et fdm=marker :
948  */