#define RRDD_DEBUG 1
#include "rrdd.h"
+#include <glib-2.0/glib.h>
+#include <rrd.h>
#if RRDD_DEBUG
# define RRDD_LOG(severity, ...) do { fprintf (stderr, __VA_ARGS__); fprintf (stderr, "\n"); } while (0)
# define RRDD_LOG(severity, ...) syslog ((severity), __VA_ARGS__)
#endif
-#define DEFAULT_PORT "42217"
-
/*
* Types
*/
static int connetion_threads_num = 0;
/* Cache stuff */
-static avl_tree_t *cache_tree = NULL;
+static GTree *cache_tree = NULL;
static cache_item_t *cache_queue_head = NULL;
static cache_item_t *cache_queue_tail = NULL;
static pthread_mutex_t cache_lock = PTHREAD_MUTEX_INITIALIZER;
time_t now;
- avl_node_t *node;
- cache_item_t ci_temp;
cache_item_t *ci;
-
char answer[4096];
now = time (NULL);
file = buffer_ptr;
buffer_ptr += strlen (file) + 1;
- ci_temp.file = file;
-
pthread_mutex_lock (&cache_lock);
- node = avl_search (cache_tree, (void *) &ci_temp);
- if (node == NULL)
+ ci = g_tree_lookup (cache_tree, file);
+ if (ci == NULL)
{
ci = (cache_item_t *) malloc (sizeof (cache_item_t));
if (ci == NULL)
ci->last_flush_time = now;
ci->flags = CI_FLAGS_IN_TREE;
- if (avl_insert (cache_tree, (void *) ci) == NULL)
- {
- pthread_mutex_unlock (&cache_lock);
- RRDD_LOG (LOG_ERR, "handle_request_update: avl_insert failed.");
- free (ci->file);
- free (ci);
- return (-1);
- }
+ g_tree_insert (cache_tree, (void *) ci->file, (void *) ci);
- RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new AVL node %s.",
+ RRDD_LOG (LOG_DEBUG, "handle_request_update: Created new tree node %s.",
ci->file);
}
- else /* if (ci != NULL) */
- {
- ci = (cache_item_t *) node->item;
- }
assert (ci != NULL);
while (*buffer_ptr != 0)
for (ai_ptr = ai_res; ai_ptr != NULL; ai_ptr = ai_ptr->ai_next)
{
int fd;
- struct sockaddr_storage sa;
listen_socket_t *temp;
temp = (listen_socket_t *) realloc (listen_fds,
static void *listen_thread_main (void *args) /* {{{ */
{
char buffer[4096];
+ struct pollfd *pollfds;
+ int pollfds_num;
int status;
int i;
open_listen_socket (config_listen_address_list[i]);
}
+ if (config_listen_address_list_len < 1)
+ open_listen_socket (RRDD_SOCK_PATH);
+
if (listen_fds_num < 1)
{
RRDD_LOG (LOG_ERR, "listen_thread_main: No listen sockets "
return (NULL);
}
- while (do_shutdown == 0)
+ pollfds_num = listen_fds_num;
+ pollfds = (struct pollfd *) malloc (sizeof (*pollfds) * pollfds_num);
+ if (pollfds == NULL)
{
- int *client_sd;
- struct sockaddr_storage client_sa;
- socklen_t client_sa_size;
- pthread_t tid;
+ RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
+ return (NULL);
+ }
+ memset (pollfds, 0, sizeof (*pollfds) * pollfds_num);
- client_sd = (int *) malloc (sizeof (int));
- if (client_sd == NULL)
+ while (do_shutdown == 0)
+ {
+ assert (pollfds_num == listen_fds_num);
+ for (i = 0; i < pollfds_num; i++)
{
- RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
- sleep (120);
- continue;
+ pollfds[i].fd = listen_fds[i].fd;
+ pollfds[i].events = POLLIN | POLLPRI;
+ pollfds[i].revents = 0;
}
- client_sa_size = sizeof (client_sa);
- /* FIXME: Don't implement listen_fds as a list or use poll(2) here! */
- *client_sd = accept (listen_fds[0].fd,
- (struct sockaddr *) &client_sa, &client_sa_size);
- if (*client_sd < 0)
+ status = poll (pollfds, pollfds_num, /* timeout = */ -1);
+ if (status < 1)
{
- RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
+ status = errno;
+ if (status != EINTR)
+ {
+ RRDD_LOG (LOG_ERR, "listen_thread_main: poll(2) failed.");
+ }
continue;
}
- RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
- *client_sd);
-
- status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
- /* args = */ (void *) client_sd);
- if (status != 0)
+ for (i = 0; i < pollfds_num; i++)
{
- RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
- close (*client_sd);
- free (client_sd);
- continue;
- }
+ int *client_sd;
+ struct sockaddr_storage client_sa;
+ socklen_t client_sa_size;
+ pthread_t tid;
+
+ if (pollfds[i].revents == 0)
+ continue;
+
+ if ((pollfds[i].revents & (POLLIN | POLLPRI)) == 0)
+ {
+ RRDD_LOG (LOG_ERR, "listen_thread_main: "
+ "poll(2) returned something unexpected for listen FD #%i.",
+ pollfds[i].fd);
+ continue;
+ }
+
+ client_sd = (int *) malloc (sizeof (int));
+ if (client_sd == NULL)
+ {
+ RRDD_LOG (LOG_ERR, "listen_thread_main: malloc failed.");
+ continue;
+ }
+
+ client_sa_size = sizeof (client_sa);
+ *client_sd = accept (pollfds[i].fd,
+ (struct sockaddr *) &client_sa, &client_sa_size);
+ if (*client_sd < 0)
+ {
+ RRDD_LOG (LOG_ERR, "listen_thread_main: accept(2) failed.");
+ continue;
+ }
+
+ RRDD_LOG (LOG_DEBUG, "listen_thread_main: accept(2) returned fd #%i.",
+ *client_sd);
+
+ status = pthread_create (&tid, /* attr = */ NULL, connection_thread_main,
+ /* args = */ (void *) client_sd);
+ if (status != 0)
+ {
+ RRDD_LOG (LOG_ERR, "listen_thread_main: pthread_create failed.");
+ close (*client_sd);
+ free (client_sd);
+ continue;
+ }
- RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
- "tid = %lu",
- *((unsigned long *) &tid));
+ RRDD_LOG (LOG_DEBUG, "listen_thread_main: pthread_create succeeded: "
+ "tid = %lu",
+ *((unsigned long *) &tid));
+ } /* for (pollfds_num) */
} /* while (do_shutdown == 0) */
close_listen_sockets ();
openlog ("rrdd", LOG_PID, LOG_DAEMON);
- cache_tree = avl_alloc_tree (cache_tree_compare, cache_tree_free);
+ cache_tree = g_tree_new ((GCompareFunc) strcmp);
if (cache_tree == NULL)
{
- RRDD_LOG (LOG_ERR, "daemonize: avl_alloc_tree failed.");
+ RRDD_LOG (LOG_ERR, "daemonize: g_tree_new failed.");
return (-1);
}