src/rrdd.c: Only unlink UNIX domain 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   int status;
618   int i;
619
620   for (i = 0; i < config_listen_address_list_len; i++)
621   {
622     RRDD_LOG (LOG_DEBUG, "listen_thread_main: config_listen_address_list[%i] "
623         "= %s", i, config_listen_address_list[i]);
624     open_listen_socket (config_listen_address_list[i]);
625   }
626
627   if (listen_fds_num < 1)
628   {
629     RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
630         "could be opened. Sorry.");
631     return (NULL);
632   }
633
634   while (do_shutdown == 0)
635   {
636     int *client_sd;
637     struct sockaddr_storage client_sa;
638     socklen_t client_sa_size;
639     pthread_t tid;
640
641     client_sd = (int *) malloc (sizeof (int));
642     if (client_sd == NULL)
643     {
644       RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
645       sleep (120);
646       continue;
647     }
648
649     client_sa_size = sizeof (client_sa);
650     /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
651     *client_sd = accept (listen_fds[0].fd,
652         (struct sockaddr *) &client_sa, &client_sa_size);
653     if (*client_sd < 0)
654     {
655       RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
656       continue;
657     }
658
659     RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
660         *client_sd);
661
662     status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
663         /* args = */ (void *) client_sd);
664     if (status != 0)
665     {
666       RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
667       close (*client_sd);
668       free (client_sd);
669       continue;
670     }
671
672     RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
673         "tid = %lu",
674         *((unsigned long *) &tid));
675   } /* while (do_shutdown == 0) */
676
677   close_listen_sockets ();
678
679   pthread_mutex_lock (&connetion_threads_lock);
680   while (connetion_threads_num > 0)
681   {
682     pthread_t wait_for;
683
684     wait_for = connetion_threads[0];
685
686     pthread_mutex_unlock (&connetion_threads_lock);
687     pthread_join (wait_for, /* retval = */ NULL);
688     pthread_mutex_lock (&connetion_threads_lock);
689   }
690   pthread_mutex_unlock (&connetion_threads_lock);
691
692   RRDD_LOG (LOG_DEBUG, "listen_thread_main: Exiting.");
693
694   return (NULL);
695 } /* }}} void *listen_thread_main */
696
697 static int daemonize (void) /* {{{ */
698 {
699   pid_t child;
700   int status;
701
702 #if !RRDD_DEBUG
703   child = fork ();
704   if (child < 0)
705   {
706     fprintf (stderr, "daemonize: fork(2) failed.\n");
707     return (-1);
708   }
709   else if (child > 0)
710   {
711     return (1);
712   }
713
714   /* Change into the /tmp directory. */
715   chdir ("/tmp");
716
717   /* Become session leader */
718   setsid ();
719
720   /* Open the first three file descriptors to /dev/null */
721   close (2);
722   close (1);
723   close (0);
724
725   open ("/dev/null", O_RDWR);
726   dup (0);
727   dup (0);
728 #endif /* RRDD_DEBUG */
729
730   {
731     struct sigaction sa;
732
733     memset (&sa, 0, sizeof (sa));
734     sa.sa_handler = sig_int_handler;
735     sigaction (SIGINT, &sa, NULL);
736
737     memset (&sa, 0, sizeof (sa));
738     sa.sa_handler = sig_term_handler;
739     sigaction (SIGINT, &sa, NULL);
740
741     memset (&sa, 0, sizeof (sa));
742     sa.sa_handler = SIG_IGN;
743     sigaction (SIGPIPE, &sa, NULL);
744   }
745
746   openlog ("rrdd", LOG_PID, LOG_DAEMON);
747
748   cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
749   if (cache_tree == NULL)
750   {
751     RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
752     return (-1);
753   }
754
755   memset (&queue_thread, 0, sizeof (queue_thread));
756   status = pthread_create (&queue_thread, /* attr = */ NULL,
757       queue_thread_main, /* args = */ NULL);
758   if (status != 0)
759   {
760     RRDD_LOG (LOG_ERR, "daemonize: pthread_create failed.");
761     return (-1);
762   }
763
764   return (0);
765 } /* }}} int daemonize */
766
767 static int cleanup (void) /* {{{ */
768 {
769   RRDD_LOG (LOG_DEBUG, "cleanup ()");
770
771   do_shutdown++;
772
773   RRDD_LOG (LOG_DEBUG, "cleanup: Joining queue_thread..");
774   pthread_cond_signal (&cache_cond);
775   pthread_join (queue_thread, /* return = */ NULL);
776   RRDD_LOG (LOG_DEBUG, "cleanup: done");
777
778   closelog ();
779
780   return (0);
781 } /* }}} int cleanup */
782
783 static int read_options (int argc, char **argv) /* {{{ */
784 {
785   int option;
786   int status = 0;
787
788   while ((option = getopt(argc, argv, "l:f:w:h?")) != -1)
789   {
790     switch (option)
791     {
792       case 'l':
793       {
794         char **temp;
795
796         temp = (char **) realloc (config_listen_address_list,
797             sizeof (char *) * (config_listen_address_list_len + 1));
798         if (temp == NULL)
799         {
800           fprintf (stderr, "read_options: realloc failed.\n");
801           return (2);
802         }
803         config_listen_address_list = temp;
804
805         temp[config_listen_address_list_len] = strdup (optarg);
806         if (temp[config_listen_address_list_len] == NULL)
807         {
808           fprintf (stderr, "read_options: strdup failed.\n");
809           return (2);
810         }
811         config_listen_address_list_len++;
812       }
813       break;
814
815       case 'f':
816       {
817         int temp;
818
819         temp = atoi (optarg);
820         if (temp > 0)
821           config_flush_interval = temp;
822         else
823         {
824           fprintf (stderr, "Invalid flush interval: %s\n", optarg);
825           status = 3;
826         }
827       }
828       break;
829
830       case 'w':
831       {
832         int temp;
833
834         temp = atoi (optarg);
835         if (temp > 0)
836           config_write_interval = temp;
837         else
838         {
839           fprintf (stderr, "Invalid write interval: %s\n", optarg);
840           status = 2;
841         }
842       }
843       break;
844
845       case 'h':
846       case '?':
847         printf ("RRDd %s  Copyright (C) 2008 Florian octo Forster\n"
848             "\n"
849             "Usage: rrdd [options]\n"
850             "\n"
851             "Valid options are:\n"
852             "  -l <address>  Socket address to listen to.\n"
853             "  -w <seconds>  Interval in which to write data.\n"
854             "  -f <seconds>  Interval in which to flush dead data.\n"
855             "\n"
856             "For more information and a detailed description of all options "
857             "please refer\n"
858             "to the rrdd(1) manual page.\n",
859             PACKAGE_VERSION);
860         status = -1;
861         break;
862     } /* switch (option) */
863   } /* while (getopt) */
864
865   return (status);
866 } /* }}} int read_options */
867
868 int main (int argc, char **argv)
869 {
870   int status;
871
872   status = read_options (argc, argv);
873   if (status != 0)
874   {
875     if (status < 0)
876       status = 0;
877     return (status);
878   }
879
880   status = daemonize ();
881   if (status == 1)
882   {
883     struct sigaction sigchld;
884
885     memset (&sigchld, 0, sizeof (sigchld));
886     sigchld.sa_handler = SIG_IGN;
887     sigaction (SIGCHLD, &sigchld, NULL);
888
889     return (0);
890   }
891   else if (status != 0)
892   {
893     fprintf (stderr, "daemonize failed, exiting.\n");
894     return (1);
895   }
896
897   listen_thread_main (NULL);
898
899   cleanup ();
900
901   return (0);
902 } /* int main */
903
904 /*
905  * vim: set sw=2 sts=2 ts=8 et fdm=marker :
906  */