src/rrdd.c: Remove an unused variable.
[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 static int config_write_interval = 300;
79 static int config_flush_interval = 3600;
80
81 static char **config_listen_address_list = NULL;
82 static int config_listen_address_list_len = 0;
83
84 /* 
85  * Functions
86  */
87 static void sig_int_handler (int signal) /* {{{ */
88 {
89   do_shutdown++;
90 } /* }}} void sig_int_handler */
91
92 static void sig_term_handler (int signal) /* {{{ */
93 {
94   do_shutdown++;
95 } /* }}} void sig_term_handler */
96
97 static int cache_tree_compare (const void *v0, const void *v1) /* {{{ */
98 {
99   cache_item_t *c0 = (cache_item_t *) v0;
100   cache_item_t *c1 = (cache_item_t *) v1;
101
102   assert (c0->file != NULL);
103   assert (c1->file != NULL);
104
105   return (strcmp (c0->file, c1->file));
106 } /* }}} int cache_tree_compare */
107
108 static void cache_tree_free (void *v) /* {{{ */
109 {
110   cache_item_t *c = (cache_item_t *) v;
111
112   assert (c->values_num == 0);
113   assert ((c->flags & CI_FLAGS_IN_TREE) != 0);
114   assert ((c->flags & CI_FLAGS_IN_QUEUE) == 0);
115
116   free (c->file);
117   c->file = NULL;
118   free (c);
119 } /* }}} void cache_tree_free */
120
121 static void *queue_thread_main (void *args) /* {{{ */
122 {
123   pthread_mutex_lock (&cache_lock);
124   while ((do_shutdown == 0) || (cache_queue_head != NULL))
125   {
126     cache_item_t *ci;
127
128     char *file;
129     char **values;
130     int values_num;
131     int status;
132     int i;
133
134     if (cache_queue_head == NULL)
135       pthread_cond_wait (&cache_cond, &cache_lock);
136
137     if (cache_queue_head == NULL)
138       continue;
139
140     ci = cache_queue_head;
141
142     /* copy the relevant parts */
143     file = strdup (ci->file);
144     if (file == NULL)
145     {
146       RRDD_LOG (LOG_ERR, "queue_thread_main: strdup failed.");
147       continue;
148     }
149
150     values = ci->values;
151     values_num = ci->values_num;
152
153     ci->values = NULL;
154     ci->values_num = 0;
155
156     ci->last_flush_time = time (NULL);
157     ci->flags &= ~(CI_FLAGS_IN_QUEUE);
158
159     cache_queue_head = ci->next;
160     if (cache_queue_head == NULL)
161       cache_queue_tail = NULL;
162     ci->next = NULL;
163
164     pthread_mutex_unlock (&cache_lock);
165
166     RRDD_LOG (LOG_DEBUG, "queue_thread_main: rrd_update (%s, %i, %p)",
167         file, values_num, (void *) values);
168
169     status = rrd_update_r (file, NULL, values_num, (void *) values);
170     if (status != 0)
171     {
172       RRDD_LOG (LOG_ERR, "queue_thread_main: "
173           "rrd_update_r failed with status %i.",
174           status);
175     }
176
177     free (file);
178     for (i = 0; i < values_num; i++)
179       free (values[i]);
180
181     pthread_mutex_lock (&cache_lock);
182   } /* while (do_shutdown == 0) */
183   pthread_mutex_unlock (&cache_lock);
184
185   RRDD_LOG (LOG_DEBUG, "queue_thread_main: Exiting.");
186
187   return (NULL);
188 } /* }}} void *queue_thread_main */
189
190 static int handle_request_update (int fd, /* {{{ */
191     char *buffer, int buffer_size)
192 {
193   char *file;
194   char *value;
195   char *buffer_ptr;
196   int values_num = 0;
197   int status;
198
199   time_t now;
200
201   avl_node_t *node;
202   cache_item_t ci_temp;
203   cache_item_t *ci;
204
205   char answer[4096];
206
207   now = time (NULL);
208
209   RRDD_LOG (LOG_DEBUG, "handle_request_update (%i, %p, %i)",
210       fd, (void *) buffer, buffer_size);
211
212   buffer_ptr = buffer;
213
214   file = buffer_ptr;
215   buffer_ptr += strlen (file) + 1;
216
217   ci_temp.file = file;
218
219   pthread_mutex_lock (&cache_lock);
220
221   node = avl_search (cache_tree, (void *) &ci_temp);
222   if (node == NULL)
223   {
224     ci = (cache_item_t *) malloc (sizeof (cache_item_t));
225     if (ci == NULL)
226     {
227       pthread_mutex_unlock (&cache_lock);
228       RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
229       return (-1);
230     }
231     memset (ci, 0, sizeof (cache_item_t));
232
233     ci->file = strdup (file);
234     if (ci->file == NULL)
235     {
236       pthread_mutex_unlock (&cache_lock);
237       RRDD_LOG (LOG_ERR, "handle_request_update: malloc failed.");
238       free (ci);
239       return (-1);
240     }
241
242     ci->values = NULL;
243     ci->values_num = 0;
244     ci->last_flush_time = now;
245     ci->flags = CI_FLAGS_IN_TREE;
246
247     if (avl_insert (cache_tree, (void *) ci) == NULL)
248     {
249       pthread_mutex_unlock (&cache_lock);
250       RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
251       free (ci->file);
252       free (ci);
253       return (-1);
254     }
255
256     RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
257         ci->file);
258   }
259   else /* if (ci != NULL) */
260   {
261     ci = (cache_item_t *) node->item;
262   }
263   assert (ci != NULL);
264
265   while (*buffer_ptr != 0)
266   {
267     char **temp;
268
269     value = buffer_ptr;
270     buffer_ptr += strlen (value) + 1;
271
272     temp = (char **) realloc (ci->values,
273         sizeof (char *) * (ci->values_num + 1));
274     if (temp == NULL)
275     {
276       RRDD_LOG (LOG_ERR, "handle_request_update: realloc failed.");
277       continue;
278     }
279     ci->values = temp;
280
281     ci->values[ci->values_num] = strdup (value);
282     if (ci->values[ci->values_num] == NULL)
283     {
284       RRDD_LOG (LOG_ERR, "handle_request_update: strdup failed.");
285       continue;
286     }
287     ci->values_num++;
288
289     values_num++;
290   }
291
292   if (((now - ci->last_flush_time) >= config_write_interval)
293       && ((ci->flags & CI_FLAGS_IN_QUEUE) == 0))
294   {
295     RRDD_LOG (LOG_DEBUG, "handle_request_update: Adding %s to the update queue.",
296         ci->file);
297
298     assert (ci->next == NULL);
299
300     if (cache_queue_tail == NULL)
301       cache_queue_head = ci;
302     else
303       cache_queue_tail->next = ci;
304     cache_queue_tail = ci;
305
306     pthread_cond_signal (&cache_cond);
307   }
308
309   pthread_mutex_unlock (&cache_lock);
310
311   snprintf (answer, sizeof (answer), "0 Enqueued %i value(s)\n", values_num);
312   answer[sizeof (answer) - 1] = 0;
313
314   status = write (fd, answer, sizeof (answer));
315   if (status < 0)
316   {
317     status = errno;
318     RRDD_LOG (LOG_INFO, "handle_request_update: write(2) returned an error.");
319     return (status);
320   }
321
322   return (0);
323 } /* }}} int handle_request_update */
324
325 static int handle_request (int fd) /* {{{ */
326 {
327   char buffer[4096];
328   int buffer_size;
329
330   RRDD_LOG (LOG_DEBUG, "handle_request (%i)", fd);
331
332   buffer_size = read (fd, buffer, sizeof (buffer));
333   if (buffer_size < 1)
334   {
335     RRDD_LOG (LOG_ERR, "handle_request: read(2) failed.");
336     return (-1);
337   }
338   assert (((size_t) buffer_size) <= sizeof (buffer));
339
340   if ((buffer[buffer_size - 2] != 0)
341       || (buffer[buffer_size - 1] != 0))
342   {
343     RRDD_LOG (LOG_INFO, "handle_request: malformed request.");
344     return (-1);
345   }
346
347   /* fields in the buffer a separated by null bytes. */
348   if (strcmp (buffer, "update") == 0)
349   {
350     int offset = strlen ("update") + 1;
351     return (handle_request_update (fd, buffer + offset,
352           buffer_size - offset));
353   }
354   else
355   {
356     RRDD_LOG (LOG_INFO, "handle_request: unknown command: %s.", buffer);
357     return (-1);
358   }
359 } /* }}} int handle_request */
360
361 static void *connection_thread_main (void *args) /* {{{ */
362 {
363   pthread_t self;
364   int i;
365   int fd;
366   
367   fd = *((int *) args);
368
369   RRDD_LOG (LOG_DEBUG, "connection_thread_main: Adding myself to "
370       "connetion_threads[]..");
371   pthread_mutex_lock (&connetion_threads_lock);
372   {
373     pthread_t *temp;
374
375     temp = (pthread_t *) realloc (connetion_threads,
376         sizeof (pthread_t) * (connetion_threads_num + 1));
377     if (temp == NULL)
378     {
379       RRDD_LOG (LOG_ERR, "connection_thread_main: realloc failed.");
380     }
381     else
382     {
383       connetion_threads = temp;
384       connetion_threads[connetion_threads_num] = pthread_self ();
385       connetion_threads_num++;
386     }
387   }
388   pthread_mutex_unlock (&connetion_threads_lock);
389   RRDD_LOG (LOG_DEBUG, "connection_thread_main: done");
390
391   while (do_shutdown == 0)
392   {
393     struct pollfd pollfd;
394     int status;
395
396     pollfd.fd = fd;
397     pollfd.events = POLLIN | POLLPRI;
398     pollfd.revents = 0;
399
400     status = poll (&pollfd, 1, /* timeout = */ 500);
401     if (status == 0) /* timeout */
402       continue;
403     else if (status < 0) /* error */
404     {
405       status = errno;
406       if (status == EINTR)
407         continue;
408       RRDD_LOG (LOG_ERR, "connection_thread_main: poll(2) failed.");
409       continue;
410     }
411
412     if ((pollfd.revents & POLLHUP) != 0) /* normal shutdown */
413     {
414       RRDD_LOG (LOG_DEBUG, "connection_thread_main: "
415           "poll(2) returned POLLHUP.");
416       close (fd);
417       break;
418     }
419     else if ((pollfd.revents & (POLLIN | POLLPRI)) == 0)
420     {
421       RRDD_LOG (LOG_WARNING, "connection_thread_main: "
422           "poll(2) returned something unexpected: %#04hx",
423           pollfd.revents);
424       close (fd);
425       break;
426     }
427
428     status = handle_request (fd);
429     if (status != 0)
430     {
431       close (fd);
432       break;
433     }
434   }
435
436   self = pthread_self ();
437   /* Remove this thread from the connection threads list */
438   pthread_mutex_lock (&connetion_threads_lock);
439   /* Find out own index in the array */
440   for (i = 0; i < connetion_threads_num; i++)
441     if (pthread_equal (connetion_threads[i], self) != 0)
442       break;
443   assert (i < connetion_threads_num);
444
445   /* Move the trailing threads forward. */
446   if (i < (connetion_threads_num - 1))
447   {
448     memmove (connetion_threads + i,
449         connetion_threads + i + 1,
450         sizeof (pthread_t) * (connetion_threads_num - i - 1));
451   }
452
453   connetion_threads_num--;
454   pthread_mutex_unlock (&connetion_threads_lock);
455
456   free (args);
457   return (NULL);
458 } /* }}} void *connection_thread_main */
459
460 static int open_listen_socket_unix (const char *path) /* {{{ */
461 {
462   int fd;
463   struct sockaddr_un sa;
464   listen_socket_t *temp;
465   int status;
466
467   temp = (listen_socket_t *) realloc (listen_fds,
468       sizeof (listen_fds[0]) * (listen_fds_num + 1));
469   if (temp == NULL)
470   {
471     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: realloc failed.");
472     return (-1);
473   }
474   listen_fds = temp;
475   memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
476
477   fd = socket (PF_UNIX, SOCK_STREAM, /* protocol = */ 0);
478   if (fd < 0)
479   {
480     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: socket(2) failed.");
481     return (-1);
482   }
483
484   memset (&sa, 0, sizeof (sa));
485   sa.sun_family = AF_UNIX;
486   strncpy (sa.sun_path, path, sizeof (sa.sun_path) - 1);
487
488   status = bind (fd, (struct sockaddr *) &sa, sizeof (sa));
489   if (status != 0)
490   {
491     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: bind(2) failed.");
492     close (fd);
493     unlink (path);
494     return (-1);
495   }
496
497   status = listen (fd, /* backlog = */ 10);
498   if (status != 0)
499   {
500     RRDD_LOG (LOG_ERR, "open_listen_socket_unix: listen(2) failed.");
501     close (fd);
502     unlink (path);
503     return (-1);
504   }
505   
506   listen_fds[listen_fds_num].fd = fd;
507   snprintf (listen_fds[listen_fds_num].path,
508       sizeof (listen_fds[listen_fds_num].path) - 1,
509       "unix:%s", path);
510   listen_fds_num++;
511
512   return (0);
513 } /* }}} int open_listen_socket_unix */
514
515 static int open_listen_socket (const char *addr) /* {{{ */
516 {
517   struct addrinfo ai_hints;
518   struct addrinfo *ai_res;
519   struct addrinfo *ai_ptr;
520   int status;
521
522   assert (addr != NULL);
523
524   if (strncmp ("unix:", addr, strlen ("unix:")) == 0)
525     return (open_listen_socket_unix (addr + strlen ("unix:")));
526   else if (addr[0] == '/')
527     return (open_listen_socket_unix (addr));
528
529   memset (&ai_hints, 0, sizeof (ai_hints));
530   ai_hints.ai_flags = 0;
531 #ifdef AI_ADDRCONFIG
532   ai_hints.ai_flags |= AI_ADDRCONFIG;
533 #endif
534   ai_hints.ai_family = AF_UNSPEC;
535   ai_hints.ai_socktype = SOCK_STREAM;
536
537   ai_res = NULL;
538   status = getaddrinfo (addr, DEFAULT_PORT, &ai_hints, &ai_res);
539   if (status != 0)
540   {
541     RRDD_LOG (LOG_ERR, "open_listen_socket: getaddrinfo(%s) failed: "
542         "%s", addr, gai_strerror (status));
543     return (-1);
544   }
545
546   for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
547   {
548     int fd;
549     listen_socket_t *temp;
550
551     temp = (listen_socket_t *) realloc (listen_fds,
552         sizeof (listen_fds[0]) * (listen_fds_num + 1));
553     if (temp == NULL)
554     {
555       RRDD_LOG (LOG_ERR, "open_listen_socket: realloc failed.");
556       continue;
557     }
558     listen_fds = temp;
559     memset (listen_fds + listen_fds_num, 0, sizeof (listen_fds[0]));
560
561     fd = socket (ai_ptr->ai_family, ai_ptr->ai_socktype, ai_ptr->ai_protocol);
562     if (fd < 0)
563     {
564       RRDD_LOG (LOG_ERR, "open_listen_socket: socket(2) failed.");
565       continue;
566     }
567
568     status = bind (fd, ai_ptr->ai_addr, ai_ptr->ai_addrlen);
569     if (status != 0)
570     {
571       RRDD_LOG (LOG_ERR, "open_listen_socket: bind(2) failed.");
572       close (fd);
573       continue;
574     }
575
576     status = listen (fd, /* backlog = */ 10);
577     if (status != 0)
578     {
579       RRDD_LOG (LOG_ERR, "open_listen_socket: listen(2) failed.");
580       close (fd);
581       return (-1);
582     }
583
584     listen_fds[listen_fds_num].fd = fd;
585     strncpy (listen_fds[listen_fds_num].path, addr,
586         sizeof (listen_fds[listen_fds_num].path) - 1);
587     listen_fds_num++;
588   } /* for (ai_ptr) */
589
590   return (0);
591 } /* }}} int open_listen_socket */
592
593 static int close_listen_sockets (void) /* {{{ */
594 {
595   size_t i;
596
597   for (i = 0; i < listen_fds_num; i++)
598   {
599     close (listen_fds[i].fd);
600     if (strncmp ("unix:", listen_fds[i].path, strlen ("unix:")) == 0)
601       unlink (listen_fds[i].path + strlen ("unix:"));
602   }
603
604   free (listen_fds);
605   listen_fds = NULL;
606   listen_fds_num = 0;
607
608   return (0);
609 } /* }}} int close_listen_sockets */
610
611 static void *listen_thread_main (void *args) /* {{{ */
612 {
613   char buffer[4096];
614   struct pollfd *pollfds;
615   int pollfds_num;
616   int status;
617   int i;
618
619   for (i = 0; i < config_listen_address_list_len; i++)
620   {
621     RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
622         "= %s", i, config_listen_address_list[i]);
623     open_listen_socket (config_listen_address_list[i]);
624   }
625
626   if (config_listen_address_list_len < 1)
627     open_listen_socket (RRDD_SOCK_PATH);
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  */