From 4f0a5e7572a4257894b4bfede42c26d65152609e Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 13 Aug 2005 21:25:09 +0000 Subject: * strip glib from avahi-core * implement glib memory allocator * add new documentation file MALLOC * initialize pseudo-RNG from /dev/urandom in avahi-daemon * remove some gcc 4.0 warnings * beef up watch system with real timeouts * move GCC __attribute__ macros into its own header avahi-common/gccmacro.h * make use of GCC's sentinel attribute where it make sense * add malloc() implementations that abort on OOM and enable them by default git-svn-id: file:///home/lennart/svn/public/avahi/trunk@308 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-daemon/Makefile.am | 2 +- avahi-daemon/dbus-protocol.c | 16 +++++++++------ avahi-daemon/main.c | 44 ++++++++++++++++++++++++++++++++++++------ avahi-daemon/simple-protocol.c | 24 ++++++++++++++++++----- 4 files changed, 68 insertions(+), 18 deletions(-) (limited to 'avahi-daemon') diff --git a/avahi-daemon/Makefile.am b/avahi-daemon/Makefile.am index 491e5ce..72a513d 100644 --- a/avahi-daemon/Makefile.am +++ b/avahi-daemon/Makefile.am @@ -97,7 +97,7 @@ introspection_DATA = \ endif avahi_daemon_CFLAGS = $(AM_CFLAGS) -avahi_daemon_LDADD = $(AM_LDADD) ../avahi-core/libavahi-core.la ../avahi-common/libavahi-common.la +avahi_daemon_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la ../avahi-glib/libavahi-glib.la ../avahi-core/libavahi-core.la xmllint: xmllint --noout --valid example.service diff --git a/avahi-daemon/dbus-protocol.c b/avahi-daemon/dbus-protocol.c index 41cef18..6526d7d 100644 --- a/avahi-daemon/dbus-protocol.c +++ b/avahi-daemon/dbus-protocol.c @@ -587,13 +587,15 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m, return DBUS_HANDLER_RESULT_HANDLED; } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "GetState")) { - + AvahiEntryGroupState state; + if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) { avahi_log_warn("Error parsing EntryGroup::GetState message"); goto fail; } - return respond_int32(c, m, (gint32) avahi_entry_group_get_state(i->entry_group)); + state = avahi_entry_group_get_state(i->entry_group); + return respond_int32(c, m, (gint32) state); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) { gint32 interface, protocol; @@ -1131,13 +1133,15 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void return respond_string(c, m, PACKAGE_STRING); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetState")) { - + AvahiServerState state; + if (!(dbus_message_get_args(m, &error, DBUS_TYPE_INVALID))) { avahi_log_warn("Error parsing Server::GetState message"); goto fail; } - - return respond_int32(c, m, (gint32) avahi_server_get_state(avahi_server)); + + state = avahi_server_get_state(avahi_server); + return respond_int32(c, m, (gint32) state); } else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) { gint32 idx; @@ -1618,7 +1622,7 @@ int dbus_protocol_setup(GMainLoop *loop) { } dbus_connection_setup_with_g_main(server->bus, NULL); - dbus_connection_set_exit_on_disconnect(server->bus, FALSE); + dbus_connection_set_exit_on_disconnect(server->bus, TRUE); dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error); if (dbus_error_is_set(&error)) { diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c index 480753e..94f89b3 100644 --- a/avahi-daemon/main.c +++ b/avahi-daemon/main.c @@ -33,6 +33,9 @@ #include #include #include +#include +#include +#include #include #include @@ -41,6 +44,8 @@ #include #include +#include +#include #include "main.h" #include "simple-protocol.h" @@ -169,7 +174,7 @@ static void remove_dns_server_entry_groups(void) { 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); @@ -502,9 +507,11 @@ static gint run_server(DaemonConfig *c) { 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) { @@ -518,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; @@ -529,7 +536,7 @@ static gint run_server(DaemonConfig *c) { goto finish; #endif - if (!(avahi_server = avahi_server_new(NULL, &c->server_config, server_callback, c, &error))) { + 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; } @@ -569,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); @@ -689,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; diff --git a/avahi-daemon/simple-protocol.c b/avahi-daemon/simple-protocol.c index 39af043..12d8adb 100644 --- a/avahi-daemon/simple-protocol.c +++ b/avahi-daemon/simple-protocol.c @@ -164,12 +164,19 @@ static void client_output_printf(Client *c, const gchar *format, ...) { } -static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *hostname, const AvahiAddress *a, gpointer userdata) { +static void host_name_resolver_callback( + AvahiHostNameResolver *r, + AvahiIfIndex iface, + AvahiProtocol protocol, + AvahiResolverEvent event, + const char *hostname, + const AvahiAddress *a, + void* userdata) { + Client *c = userdata; g_assert(c); - if (event == AVAHI_RESOLVER_TIMEOUT) client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT); else { @@ -181,9 +188,16 @@ static void host_name_resolver_callback(AvahiHostNameResolver *r, AvahiIfIndex i c->state = CLIENT_DEAD; } -static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex iface, AvahiProtocol protocol, AvahiBrowserEvent event, const AvahiAddress *a, const gchar *hostname, gpointer userdata) { - Client *c = userdata; +static void address_resolver_callback( + AvahiAddressResolver *r, + AvahiIfIndex iface, + AvahiProtocol protocol, + AvahiResolverEvent event, + const AvahiAddress *a, + const char *hostname, + void* userdata) { + Client *c = userdata; g_assert(c); @@ -195,7 +209,7 @@ static void address_resolver_callback(AvahiAddressResolver *r, AvahiIfIndex ifac c->state = CLIENT_DEAD; } -static void dns_server_browser_callback(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *host_name, const AvahiAddress *a, guint16 port, gpointer userdata) { +static void dns_server_browser_callback(AvahiDNSServerBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *host_name, const AvahiAddress *a, uint16_t port, void* userdata) { Client *c = userdata; gchar t[64]; -- cgit