]> git.meshlink.io Git - catta/blobdiff - avahi-daemon/main.c
* strip glib from avahi-core
[catta] / avahi-daemon / main.c
index 7d83c46858830eac293dbc5e782510e47087feec..94f89b329e0e921f584f9c68f219ec6036271e96 100644 (file)
@@ -33,6 +33,9 @@
 #include <pwd.h>
 #include <sys/stat.h>
 #include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <stdlib.h>
 
 #include <libdaemon/dfork.h>
 #include <libdaemon/dsignal.h>
@@ -41,6 +44,8 @@
 
 #include <avahi-core/core.h>
 #include <avahi-core/log.h>
+#include <avahi-glib/glib-malloc.h>
+#include <avahi-glib/glib-watch.h>
 
 #include "main.h"
 #include "simple-protocol.h"
@@ -131,14 +136,16 @@ finish:
     return ret;
 }
 
-static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
+static AvahiEntryGroup* add_dns_servers(AvahiServer *s, AvahiEntryGroup* g, gchar **l) {
     gchar **p;
-    AvahiEntryGroup *g;
 
     g_assert(s);
     g_assert(l);
 
-    g = avahi_entry_group_new(s, NULL, NULL);
+    if (!g) 
+        g = avahi_entry_group_new(s, NULL, NULL);
+
+    g_assert(avahi_entry_group_is_empty(g));
 
     for (p = l; *p; p++) {
         AvahiAddress a;
@@ -148,6 +155,7 @@ static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
         else
             if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
                 avahi_entry_group_free(g);
+                avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
                 return NULL;
             }
     }
@@ -155,22 +163,18 @@ static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
     avahi_entry_group_commit(g);
 
     return g;
-    
 }
 
 static void remove_dns_server_entry_groups(void) {
-    if (resolv_conf_entry_group) {
-        avahi_entry_group_free(resolv_conf_entry_group);
-        resolv_conf_entry_group = NULL;
-    }
+
+    if (resolv_conf_entry_group)
+        avahi_entry_group_reset(resolv_conf_entry_group);
     
-    if (dns_servers_entry_group) {
-        avahi_entry_group_free(dns_servers_entry_group);
-        dns_servers_entry_group = NULL;
-    }
+    if (dns_servers_entry_group) 
+        avahi_entry_group_reset(dns_servers_entry_group);
 }
 
-static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
+static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
     DaemonConfig *c = userdata;
     
     g_assert(s);
@@ -188,10 +192,10 @@ static void server_callback(AvahiServer *s, AvahiServerState state, gpointer use
         remove_dns_server_entry_groups();
 
         if (resolv_conf && resolv_conf[0])
-            resolv_conf_entry_group = add_dns_servers(s, resolv_conf);
+            resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
 
         if (c->publish_dns_servers && c->publish_dns_servers[0])
-            dns_servers_entry_group = add_dns_servers(s, c->publish_dns_servers);
+            dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
 
         simple_protocol_restart_queries();
         
@@ -207,8 +211,6 @@ static void server_callback(AvahiServer *s, AvahiServerState state, gpointer use
         avahi_server_set_host_name(s, n);
         g_free(n);
     }
-
-    
 }
 
 static void help(FILE *f, const gchar *argv0) {
@@ -476,15 +478,13 @@ static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpoi
             static_service_load();
             static_service_add_to_server();
 
-            if (resolv_conf_entry_group) {
-                avahi_entry_group_free(resolv_conf_entry_group);
-                resolv_conf_entry_group = NULL;
-            }
+            if (resolv_conf_entry_group)
+                avahi_entry_group_reset(resolv_conf_entry_group);
 
             load_resolv_conf(&config);
             
             if (resolv_conf && resolv_conf[0])
-                resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
+                resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
 
             break;
 
@@ -506,9 +506,12 @@ static gint run_server(DaemonConfig *c) {
     gint r = -1;
     GIOChannel *io = NULL;
     guint watch_id = (guint) -1;
+    gint error;
+    AvahiGLibPoll *poll_api;
 
     g_assert(c);
-    
+
+    poll_api = avahi_glib_poll_new(NULL);
     loop = g_main_loop_new(NULL, FALSE);
 
     if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
@@ -522,7 +525,7 @@ static gint run_server(DaemonConfig *c) {
     }
 
     g_io_channel_set_close_on_unref(io, FALSE);
-    g_io_add_watch(io, G_IO_IN, signal_callback, loop);
+    watch_id = g_io_add_watch(io, G_IO_IN, signal_callback, loop);
     
     if (simple_protocol_setup(NULL) < 0)
         goto finish;
@@ -533,17 +536,19 @@ static gint run_server(DaemonConfig *c) {
             goto finish;
 #endif
     
-    if (!(avahi_server = avahi_server_new(NULL, &c->server_config, server_callback, c)))
+    if (!(avahi_server = avahi_server_new(avahi_glib_poll_get(poll_api), &c->server_config, server_callback, c, &error))) {
+        avahi_log_error("Failed to create server: %s", avahi_strerror(error));
         goto finish;
+    }
 
     load_resolv_conf(c);
     
     static_service_load();
 
-    if (c->daemonize) {
+    if (c->daemonize)
         daemon_retval_send(0);
-        r = 0;
-    }
+
+    r = 0;
 
     g_main_loop_run(loop);
 
@@ -560,7 +565,6 @@ finish:
         dbus_protocol_shutdown();
 #endif
 
-
     if (avahi_server)
         avahi_server_free(avahi_server);
 
@@ -572,10 +576,12 @@ finish:
     if (io)
         g_io_channel_unref(io);
 
-        
+    if (poll_api)
+        avahi_glib_poll_free(poll_api);
+
     if (loop)
         g_main_loop_unref(loop);
-
+    
     if (r != 0 && c->daemonize)
         daemon_retval_send(1);
     
@@ -692,12 +698,35 @@ fail:
 
 }
 
+#define RANDOM_DEVICE "/dev/urandom"
+
+static void init_rand_seed(void) {
+    int fd;
+    unsigned seed = 0;
+
+    /* Try to initialize seed from /dev/urandom, to make it a little
+     * less predictable, and to make sure that multiple machines
+     * booted at the same time choose different random seeds.  */
+    if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
+        read(fd, &seed, sizeof(seed));
+        close(fd);
+    }
+
+    /* If the initialization failed by some reason, we add the time to the seed*/
+    seed |= (unsigned) time(NULL);
+
+    srand(seed);
+}
+
 int main(int argc, char *argv[]) {
     gint r = 255;
     const gchar *argv0;
     gboolean wrote_pid_file = FALSE;
 
     avahi_set_log_function(log_function);
+    avahi_set_allocator(avahi_glib_allocator());
+
+    init_rand_seed();
     
     avahi_server_config_init(&config.server_config);
     config.command = DAEMON_RUN;