summaryrefslogtreecommitdiffstats
path: root/avahi-daemon
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2005-08-15 02:42:59 +0000
committerLennart Poettering <lennart@poettering.net>2005-08-15 02:42:59 +0000
commitdeb6bcac8d1e1ecec4aafaee2d27dbe467e4f461 (patch)
tree4c09185727a6b81fba305ade5e22b84cc7142976 /avahi-daemon
parente240bf98f07316031f632330d89400aa0dea2fbf (diff)
* implement ini file parser
* strip glib usage from avahi-daemon * make dbus-watch-glue a static library so that both avahi-client and avahi-daemon can make use of it git-svn-id: file:///home/lennart/svn/public/avahi/trunk@327 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
Diffstat (limited to 'avahi-daemon')
-rw-r--r--avahi-daemon/Makefile.am31
-rw-r--r--avahi-daemon/dbus-protocol.c495
-rw-r--r--avahi-daemon/dbus-protocol.h4
-rw-r--r--avahi-daemon/ini-file-parser-test.c59
-rw-r--r--avahi-daemon/ini-file-parser.c186
-rw-r--r--avahi-daemon/ini-file-parser.h57
-rw-r--r--avahi-daemon/main.c485
-rw-r--r--avahi-daemon/simple-protocol.c214
-rw-r--r--avahi-daemon/simple-protocol.h4
-rw-r--r--avahi-daemon/static-services.c164
10 files changed, 991 insertions, 708 deletions
diff --git a/avahi-daemon/Makefile.am b/avahi-daemon/Makefile.am
index 72a513d..2792f21 100644
--- a/avahi-daemon/Makefile.am
+++ b/avahi-daemon/Makefile.am
@@ -32,16 +32,11 @@ AM_CFLAGS= \
AM_LDADD=-lexpat
-# GLIB 2.0
-AM_CFLAGS+=$(GLIB20_CFLAGS)
-AM_LDADD+=$(GLIB20_LIBS)
-
# libdaemon
AM_CFLAGS+=$(LIBDAEMON_CFLAGS)
AM_LDADD+=$(LIBDAEMON_LIBS)
if ENABLE_DBUS
-# DBUS
AM_CFLAGS+=$(DBUS_CFLAGS)
AM_LDADD+=$(DBUS_LIBS)
endif
@@ -52,10 +47,24 @@ AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")'
sbin_PROGRAMS = \
avahi-daemon
+noinst_PROGRAMS = \
+ ini-file-parser-test
+
avahi_daemon_SOURCES = \
main.c main.h \
simple-protocol.c simple-protocol.h \
- static-services.c static-services.h
+ static-services.c static-services.h \
+ ini-file-parser.c ini-file-parser.h
+
+avahi_daemon_CFLAGS = $(AM_CFLAGS)
+avahi_daemon_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la ../avahi-core/libavahi-core.la
+
+ini_file_parser_test_SOURCES = \
+ ini-file-parser.c ini-file-parser.h \
+ ini-file-parser-test.c
+
+ini_file_parser_test_CFLAGS = $(AM_CFLAGS)
+ini_file_parser_test_LDADD = $(AM_LDADD) ../avahi-common/libavahi-common.la ../avahi-core/libavahi-core.la
pkgsysconf_DATA = \
avahi-daemon.conf
@@ -83,7 +92,12 @@ EXTRA_DIST = \
introspect.xsl
if ENABLE_DBUS
-avahi_daemon_SOURCES += dbus-protocol.c dbus-protocol.h
+avahi_daemon_SOURCES += \
+ dbus-protocol.c \
+ dbus-protocol.h
+
+avahi_daemon_LDADD += \
+ ../avahi-common/libdbus-watch-glue.a
dbusservice_DATA = avahi-dbus.conf
@@ -96,8 +110,7 @@ introspection_DATA = \
endif
-avahi_daemon_CFLAGS = $(AM_CFLAGS)
-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 8d0226f..f72b8d6 100644
--- a/avahi-daemon/dbus-protocol.c
+++ b/avahi-daemon/dbus-protocol.c
@@ -28,15 +28,20 @@
#include <net/if.h>
#include <errno.h>
#include <unistd.h>
-#include <glib.h>
+#include <assert.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <signal.h>
#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
#include <avahi-common/llist.h>
+#include <avahi-common/malloc.h>
+#include <avahi-common/dbus.h>
+#include <avahi-common/dbus-watch-glue.h>
#include <avahi-core/log.h>
#include <avahi-core/core.h>
-#include <avahi-common/dbus.h>
#include "dbus-protocol.h"
#include "main.h"
@@ -58,12 +63,12 @@ typedef struct ServiceResolverInfo ServiceResolverInfo;
/* #define VALGRIND_WORKAROUND */
struct EntryGroupInfo {
- guint id;
+ unsigned id;
Client *client;
AvahiSEntryGroup *entry_group;
- gchar *path;
+ char *path;
- gint n_entries;
+ int n_entries;
AVAHI_LLIST_FIELDS(EntryGroupInfo, entry_groups);
};
@@ -85,28 +90,28 @@ struct AddressResolverInfo {
};
struct DomainBrowserInfo {
- guint id;
+ unsigned id;
Client *client;
AvahiSDomainBrowser *domain_browser;
- gchar *path;
+ char *path;
AVAHI_LLIST_FIELDS(DomainBrowserInfo, domain_browsers);
};
struct ServiceTypeBrowserInfo {
- guint id;
+ unsigned id;
Client *client;
AvahiSServiceTypeBrowser *service_type_browser;
- gchar *path;
+ char *path;
AVAHI_LLIST_FIELDS(ServiceTypeBrowserInfo, service_type_browsers);
};
struct ServiceBrowserInfo {
- guint id;
+ unsigned id;
Client *client;
AvahiSServiceBrowser *service_browser;
- gchar *path;
+ char *path;
AVAHI_LLIST_FIELDS(ServiceBrowserInfo, service_browsers);
};
@@ -120,10 +125,10 @@ struct ServiceResolverInfo {
};
struct Client {
- guint id;
- gchar *name;
- guint current_id;
- gint n_objects;
+ unsigned id;
+ char *name;
+ unsigned current_id;
+ int n_objects;
AVAHI_LLIST_FIELDS(Client, clients);
AVAHI_LLIST_HEAD(EntryGroupInfo, entry_groups);
@@ -138,29 +143,29 @@ struct Client {
struct Server {
DBusConnection *bus;
AVAHI_LLIST_HEAD(Client, clients);
- gint n_clients;
- guint current_id;
+ int n_clients;
+ unsigned current_id;
};
static Server *server = NULL;
static void entry_group_free(EntryGroupInfo *i) {
- g_assert(i);
+ assert(i);
if (i->entry_group)
avahi_s_entry_group_free(i->entry_group);
dbus_connection_unregister_object_path(server->bus, i->path);
- g_free(i->path);
+ avahi_free(i->path);
AVAHI_LLIST_REMOVE(EntryGroupInfo, entry_groups, i->client->entry_groups, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void host_name_resolver_free(HostNameResolverInfo *i) {
- g_assert(i);
+ assert(i);
if (i->host_name_resolver)
avahi_s_host_name_resolver_free(i->host_name_resolver);
@@ -168,13 +173,13 @@ static void host_name_resolver_free(HostNameResolverInfo *i) {
AVAHI_LLIST_REMOVE(HostNameResolverInfo, host_name_resolvers, i->client->host_name_resolvers, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void address_resolver_free(AddressResolverInfo *i) {
- g_assert(i);
+ assert(i);
if (i->address_resolver)
avahi_s_address_resolver_free(i->address_resolver);
@@ -182,58 +187,58 @@ static void address_resolver_free(AddressResolverInfo *i) {
AVAHI_LLIST_REMOVE(AddressResolverInfo, address_resolvers, i->client->address_resolvers, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void domain_browser_free(DomainBrowserInfo *i) {
- g_assert(i);
+ assert(i);
if (i->domain_browser)
avahi_s_domain_browser_free(i->domain_browser);
dbus_connection_unregister_object_path(server->bus, i->path);
- g_free(i->path);
+ avahi_free(i->path);
AVAHI_LLIST_REMOVE(DomainBrowserInfo, domain_browsers, i->client->domain_browsers, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void service_type_browser_free(ServiceTypeBrowserInfo *i) {
- g_assert(i);
+ assert(i);
if (i->service_type_browser)
avahi_s_service_type_browser_free(i->service_type_browser);
dbus_connection_unregister_object_path(server->bus, i->path);
- g_free(i->path);
+ avahi_free(i->path);
AVAHI_LLIST_REMOVE(ServiceTypeBrowserInfo, service_type_browsers, i->client->service_type_browsers, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void service_browser_free(ServiceBrowserInfo *i) {
- g_assert(i);
+ assert(i);
if (i->service_browser)
avahi_s_service_browser_free(i->service_browser);
dbus_connection_unregister_object_path(server->bus, i->path);
- g_free(i->path);
+ avahi_free(i->path);
AVAHI_LLIST_REMOVE(ServiceBrowserInfo, service_browsers, i->client->service_browsers, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void service_resolver_free(ServiceResolverInfo *i) {
- g_assert(i);
+ assert(i);
if (i->service_resolver)
avahi_s_service_resolver_free(i->service_resolver);
@@ -241,15 +246,15 @@ static void service_resolver_free(ServiceResolverInfo *i) {
AVAHI_LLIST_REMOVE(ServiceResolverInfo, service_resolvers, i->client->service_resolvers, i);
i->client->n_objects--;
- g_assert(i->client->n_objects >= 0);
+ assert(i->client->n_objects >= 0);
- g_free(i);
+ avahi_free(i);
}
static void client_free(Client *c) {
- g_assert(server);
- g_assert(c);
+ assert(server);
+ assert(c);
while (c->entry_groups)
entry_group_free(c->entry_groups);
@@ -272,21 +277,21 @@ static void client_free(Client *c) {
while (c->service_resolvers)
service_resolver_free(c->service_resolvers);
- g_assert(c->n_objects == 0);
+ assert(c->n_objects == 0);
- g_free(c->name);
+ avahi_free(c->name);
AVAHI_LLIST_REMOVE(Client, clients, server->clients, c);
- g_free(c);
+ avahi_free(c);
server->n_clients --;
- g_assert(server->n_clients >= 0);
+ assert(server->n_clients >= 0);
}
-static Client *client_get(const gchar *name, gboolean create) {
+static Client *client_get(const char *name, int create) {
Client *client;
- g_assert(server);
- g_assert(name);
+ assert(server);
+ assert(name);
for (client = server->clients; client; client = client->clients_next)
if (!strcmp(name, client->name))
@@ -299,9 +304,9 @@ static Client *client_get(const gchar *name, gboolean create) {
return NULL;
/* If not existant yet, create a new entry */
- client = g_new(Client, 1);
+ client = avahi_new(Client, 1);
client->id = server->current_id++;
- client->name = g_strdup(name);
+ client->name = avahi_strdup(name);
client->current_id = 0;
client->n_objects = 0;
AVAHI_LLIST_HEAD_INIT(EntryGroupInfo, client->entry_groups);
@@ -315,15 +320,15 @@ static Client *client_get(const gchar *name, gboolean create) {
AVAHI_LLIST_PREPEND(Client, clients, server->clients, client);
server->n_clients++;
- g_assert(server->n_clients > 0);
+ assert(server->n_clients > 0);
return client;
}
-static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint error, const gchar *text) {
+static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, int error, const char *text) {
DBusMessage *reply;
- const gchar * const table[- AVAHI_ERR_MAX] = {
+ const char * const table[- AVAHI_ERR_MAX] = {
NULL, /* OK */
AVAHI_DBUS_ERR_FAILURE,
AVAHI_DBUS_ERR_BAD_STATE,
@@ -352,8 +357,8 @@ static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint e
AVAHI_DBUS_ERR_INVALID_OBJECT
};
- g_assert(-error > -AVAHI_OK);
- g_assert(-error < -AVAHI_ERR_MAX);
+ assert(-error > -AVAHI_OK);
+ assert(-error < -AVAHI_ERR_MAX);
reply = dbus_message_new_error(m, table[-error], text ? text : avahi_strerror(error));
dbus_connection_send(c, reply, NULL);
@@ -362,7 +367,7 @@ static DBusHandlerResult respond_error(DBusConnection *c, DBusMessage *m, gint e
return DBUS_HANDLER_RESULT_HANDLED;
}
-static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const gchar *text) {
+static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const char *text) {
DBusMessage *reply;
reply = dbus_message_new_method_return(m);
@@ -373,7 +378,7 @@ static DBusHandlerResult respond_string(DBusConnection *c, DBusMessage *m, const
return DBUS_HANDLER_RESULT_HANDLED;
}
-static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, gint32 i) {
+static DBusHandlerResult respond_int32(DBusConnection *c, DBusMessage *m, int32_t i) {
DBusMessage *reply;
reply = dbus_message_new_method_return(m);
@@ -394,7 +399,7 @@ static DBusHandlerResult respond_ok(DBusConnection *c, DBusMessage *m) {
return DBUS_HANDLER_RESULT_HANDLED;
}
-static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const gchar *path) {
+static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const char *path) {
DBusMessage *reply;
reply = dbus_message_new_method_return(m);
@@ -405,36 +410,83 @@ static DBusHandlerResult respond_path(DBusConnection *c, DBusMessage *m, const g
return DBUS_HANDLER_RESULT_HANDLED;
}
-static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const gchar *fname) {
- gchar *path = NULL;
- gchar *contents;
- GError *gerror = NULL;
+static char *file_get_contents(char *fname) {
+ int fd = -1;
+ struct stat st;
+ ssize_t size;
+ char *buf = NULL;
+
+ assert(fname);
+
+ if (!(fd = open(fname, O_RDONLY))) {
+ avahi_log_error("Failed to open %s: %s", fname, strerror(errno));
+ goto fail;
+ }
+
+ if (fstat(fd, &st) < 0) {
+ avahi_log_error("stat(%s) failed: %s", fname, strerror(errno));
+ goto fail;
+ }
+
+ if (!(S_ISREG(st.st_mode))) {
+ avahi_log_error("Invalid file %s", fname);
+ goto fail;
+ }
+
+ if (st.st_size > 1024*1024) { /** 1MB */
+ avahi_log_error("File too large %s", fname);
+ goto fail;
+ }
+
+ buf = avahi_new(char, st.st_size+1);
+
+ if ((size = read(fd, buf, st.st_size)) < 0) {
+ avahi_log_error("read() failed: %s\n", strerror(errno));
+ goto fail;
+ }
+
+ buf[size] = 0;
+
+ close(fd);
+ return buf;
+
+fail:
+ if (fd >= 0)
+ close(fd);
+
+ if (buf)
+ avahi_free(buf);
+
+ return NULL;
+
+}
+
+static DBusHandlerResult handle_introspect(DBusConnection *c, DBusMessage *m, const char *fname) {
+ char *path, *contents;
DBusError error;
- g_assert(c);
- g_assert(m);
- g_assert(fname);
+ assert(c);
+ assert(m);
+ assert(fname);
dbus_error_init(&error);
if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
- avahi_log_warn("Error parsing Introspect message: %s", error.message);
+ avahi_log_error("Error parsing Introspect message: %s", error.message);
goto fail;
}
- path = g_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
-
- if (!(g_file_get_contents(path, &contents, NULL, &gerror))) {
- avahi_log_warn("Failed to load introspection data: %s", gerror->message);
- g_error_free(gerror);
- g_free(path);
+ path = avahi_strdup_printf("%s/%s", AVAHI_DBUS_INTROSPECTION_DIR, fname);
+ contents = file_get_contents(path);
+ avahi_free(path);
+
+ if (!contents) {
+ avahi_log_error("Failed to load introspection data.");
goto fail;
}
-
- g_free(path);
respond_string(c, m, contents);
- g_free(contents);
+ avahi_free(contents);
return DBUS_HANDLER_RESULT_HANDLED;
@@ -447,7 +499,6 @@ fail:
}
static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *m, void *userdata) {
- GMainLoop *loop = userdata;
DBusError error;
dbus_error_init(&error);
@@ -461,11 +512,13 @@ static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *
/* No, we shouldn't quit, but until we get somewhere
* usefull such that we can restore our state, we will */
avahi_log_warn("Disconnnected from d-bus, terminating...");
- g_main_loop_quit (loop);
+
+ raise(SIGQUIT); /* The signal handler will catch this and terminate the process cleanly*/
+
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameAcquired")) {
- gchar *name;
+ char *name;
if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_INVALID)) {
avahi_log_warn("Error parsing NameAcquired message");
@@ -476,7 +529,7 @@ static DBusHandlerResult msg_signal_filter_impl(DBusConnection *c, DBusMessage *
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_signal(m, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
- gchar *name, *old, *new;
+ char *name, *old, *new;
if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &name, DBUS_TYPE_STRING, &old, DBUS_TYPE_STRING, &new, DBUS_TYPE_INVALID)) {
avahi_log_warn("Error parsing NameOwnerChanged message");
@@ -500,17 +553,17 @@ fail:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, gpointer userdata) {
+static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void* userdata) {
EntryGroupInfo *i = userdata;
DBusMessage *m;
- gint32 t;
+ int32_t t;
- g_assert(s);
- g_assert(g);
- g_assert(i);
+ assert(s);
+ assert(g);
+ assert(i);
m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "StateChanged");
- t = (gint32) state;
+ t = (int32_t) state;
dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
dbus_message_set_destination(m, i->client->name);
dbus_connection_send(server->bus, m, NULL);
@@ -521,9 +574,9 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m,
DBusError error;
EntryGroupInfo *i = userdata;
- g_assert(c);
- g_assert(m);
- g_assert(i);
+ assert(c);
+ assert(m);
+ assert(i);
dbus_error_init(&error);
@@ -573,7 +626,7 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m,
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "IsEmpty")) {
DBusMessage *reply;
- gboolean b;
+ int b;
if (!dbus_message_get_args(m, &error, DBUS_TYPE_INVALID)) {
avahi_log_warn("Error parsing EntryGroup::IsEmpty message");
@@ -598,12 +651,12 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m,
}
state = avahi_s_entry_group_get_state(i->entry_group);
- return respond_int32(c, m, (gint32) state);
+ return respond_int32(c, m, (int32_t) state);
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddService")) {
- gint32 interface, protocol;
- gchar *type, *name, *domain, *host;
- guint16 port;
+ int32_t interface, protocol;
+ char *type, *name, *domain, *host;
+ uint16_t port;
AvahiStringList *strlst;
DBusMessageIter iter, sub;
int j;
@@ -639,12 +692,12 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m,
for (;;) {
DBusMessageIter sub2;
int at, n;
- guint8 *k;
+ uint8_t *k;
if ((at = dbus_message_iter_get_arg_type(&sub)) == DBUS_TYPE_INVALID)
break;
- g_assert(at == DBUS_TYPE_ARRAY);
+ assert(at == DBUS_TYPE_ARRAY);
if (dbus_message_iter_get_element_type(&sub) != DBUS_TYPE_BYTE) {
avahi_log_warn("Error parsing EntryGroup::AddService message");
@@ -681,8 +734,8 @@ static DBusHandlerResult msg_entry_group_impl(DBusConnection *c, DBusMessage *m,
return respond_ok(c, m);
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_ENTRY_GROUP, "AddAddress")) {
- gint32 interface, protocol;
- gchar *name, *address;
+ int32_t interface, protocol;
+ char *name, *address;
AvahiAddress a;
if (!dbus_message_get_args(
@@ -722,24 +775,24 @@ fail:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const gchar *host_name, const AvahiAddress *a, gpointer userdata) {
+static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const char *host_name, const AvahiAddress *a, void* userdata) {
HostNameResolverInfo *i = userdata;
- g_assert(r);
- g_assert(host_name);
- g_assert(i);
+ assert(r);
+ assert(host_name);
+ assert(i);
if (event == AVAHI_RESOLVER_FOUND) {
char t[256], *pt = t;
- gint32 i_interface, i_protocol, i_aprotocol;
+ int32_t i_interface, i_protocol, i_aprotocol;
DBusMessage *reply;
- g_assert(a);
+ assert(a);
avahi_address_snprint(t, sizeof(t), a);
- i_interface = (gint32) interface;
- i_protocol = (gint32) protocol;
- i_aprotocol = (gint32) a->family;
+ i_interface = (int32_t) interface;
+ i_protocol = (int32_t) protocol;
+ i_aprotocol = (int32_t) a->family;
reply = dbus_message_new_method_return(i->message);
dbus_message_append_args(
@@ -754,7 +807,7 @@ static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex
dbus_connection_send(server->bus, reply, NULL);
dbus_message_unref(reply);
} else {
- g_assert(event == AVAHI_RESOLVER_TIMEOUT);
+ assert(event == AVAHI_RESOLVER_TIMEOUT);
respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
}
@@ -762,24 +815,24 @@ static void host_name_resolver_callback(AvahiSHostNameResolver *r, AvahiIfIndex
host_name_resolver_free(i);
}
-static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const gchar *host_name, gpointer userdata) {
+static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex interface, AvahiProtocol protocol, AvahiResolverEvent event, const AvahiAddress *address, const char *host_name, void* userdata) {
AddressResolverInfo *i = userdata;
- g_assert(r);
- g_assert(address);
- g_assert(i);
+ assert(r);
+ assert(address);
+ assert(i);
if (event == AVAHI_RESOLVER_FOUND) {
char t[256], *pt = t;
- gint32 i_interface, i_protocol, i_aprotocol;
+ int32_t i_interface, i_protocol, i_aprotocol;
DBusMessage *reply;
- g_assert(host_name);
+ assert(host_name);
avahi_address_snprint(t, sizeof(t), address);
- i_interface = (gint32) interface;
- i_protocol = (gint32) protocol;
- i_aprotocol = (gint32) address->family;
+ i_interface = (int32_t) interface;
+ i_protocol = (int32_t) protocol;
+ i_aprotocol = (int32_t) address->family;
reply = dbus_message_new_method_return(i->message);
dbus_message_append_args(
@@ -794,7 +847,7 @@ static void address_resolver_callback(AvahiSAddressResolver *r, AvahiIfIndex int
dbus_connection_send(server->bus, reply, NULL);
dbus_message_unref(reply);
} else {
- g_assert(event == AVAHI_RESOLVER_TIMEOUT);
+ assert(event == AVAHI_RESOLVER_TIMEOUT);
respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
}
@@ -805,9 +858,9 @@ static DBusHandlerResult msg_domain_browser_impl(DBusConnection *c, DBusMessage
DBusError error;
DomainBrowserInfo *i = userdata;
- g_assert(c);
- g_assert(m);
- g_assert(i);
+ assert(c);
+ assert(m);
+ assert(i);
dbus_error_init(&error);
@@ -845,17 +898,17 @@ fail:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-static void domain_browser_callback(AvahiSDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *domain, gpointer userdata) {
+static void domain_browser_callback(AvahiSDomainBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *domain, void* userdata) {
DomainBrowserInfo *i = userdata;
DBusMessage *m;
- gint32 i_interface, i_protocol;
+ int32_t i_interface, i_protocol;
- g_assert(b);
- g_assert(domain);
- g_assert(i);
+ assert(b);
+ assert(domain);
+ assert(i);
- i_interface = (gint32) interface;
- i_protocol = (gint32) protocol;
+ i_interface = (int32_t) interface;
+ i_protocol = (int32_t) protocol;
m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_DOMAIN_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
dbus_message_append_args(
@@ -873,9 +926,9 @@ static DBusHandlerResult msg_service_type_browser_impl(DBusConnection *c, DBusMe
DBusError error;
ServiceTypeBrowserInfo *i = userdata;
- g_assert(c);
- g_assert(m);
- g_assert(i);
+ assert(c);
+ assert(m);
+ assert(i);
dbus_error_init(&error);
@@ -913,18 +966,18 @@ fail:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *type, const gchar *domain, gpointer userdata) {
+static void service_type_browser_callback(AvahiSServiceTypeBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *type, const char *domain, void* userdata) {
ServiceTypeBrowserInfo *i = userdata;
DBusMessage *m;
- gint32 i_interface, i_protocol;
+ int32_t i_interface, i_protocol;
- g_assert(b);
- g_assert(type);
- g_assert(domain);
- g_assert(i);
+ assert(b);
+ assert(type);
+ assert(domain);
+ assert(i);
- i_interface = (gint32) interface;
- i_protocol = (gint32) protocol;
+ i_interface = (int32_t) interface;
+ i_protocol = (int32_t) protocol;
m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_TYPE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
dbus_message_append_args(
@@ -943,9 +996,9 @@ static DBusHandlerResult msg_service_browser_impl(DBusConnection *c, DBusMessage
DBusError error;
ServiceBrowserInfo *i = userdata;
- g_assert(c);
- g_assert(m);
- g_assert(i);
+ assert(c);
+ assert(m);
+ assert(i);
dbus_error_init(&error);
@@ -983,19 +1036,19 @@ fail:
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
-static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const gchar *name, const gchar *type, const gchar *domain, gpointer userdata) {
+static void service_browser_callback(AvahiSServiceBrowser *b, AvahiIfIndex interface, AvahiProtocol protocol, AvahiBrowserEvent event, const char *name, const char *type, const char *domain, void* userdata) {
ServiceBrowserInfo *i = userdata;
DBusMessage *m;
- gint32 i_interface, i_protocol;
+ int32_t i_interface, i_protocol;
- g_assert(b);
- g_assert(name);
- g_assert(type);
- g_assert(domain);
- g_assert(i);
+ assert(b);
+ assert(name);
+ assert(type);
+ assert(domain);
+ assert(i);
- i_interface = (gint32) interface;
- i_protocol = (gint32) protocol;
+ i_interface = (int32_t) interface;
+ i_protocol = (int32_t) protocol;
m = dbus_message_new_signal(i->path, AVAHI_DBUS_INTERFACE_SERVICE_BROWSER, event == AVAHI_BROWSER_NEW ? "ItemNew" : "ItemRemove");
dbus_message_append_args(
@@ -1016,36 +1069,36 @@ static void service_resolver_callback(
AvahiIfIndex interface,
AvahiProtocol protocol,
AvahiResolverEvent event,
- const gchar *name,
- const gchar *type,
- const gchar *domain,
- const gchar *host_name,
+ const char *name,
+ const char *type,
+ const char *domain,
+ const char *host_name,
const AvahiAddress *a,
- guint16 port,
+ uint16_t port,
AvahiStringList *txt,
- gpointer userdata) {
+ void* userdata) {
ServiceResolverInfo *i = userdata;
- g_assert(r);
- g_assert(i);
+ assert(r);
+ assert(i);
if (event == AVAHI_RESOLVER_FOUND) {
char t[256], *pt = t;
- gint32 i_interface, i_protocol, i_aprotocol;
- guint n, j;
+ int32_t i_interface, i_protocol, i_aprotocol;
+ unsigned n, j;
AvahiStringList *p;
DBusMessage *reply;
DBusMessageIter iter, sub;
- g_assert(host_name);
+ assert(host_name);
- g_assert(a);
+ assert(a);
avahi_address_snprint(t, sizeof(t), a);
- i_interface = (gint32) interface;
- i_protocol = (gint32) protocol;
- i_aprotocol = (gint32) a->family;
+ i_interface = (int32_t) interface;
+ i_protocol = (int32_t) protocol;
+ i_aprotocol = (int32_t) a->family;
reply = dbus_message_new_method_return(i->message);
dbus_message_append_args(
@@ -1066,7 +1119,7 @@ static void service_resolver_callback(
for (p = txt, j = n-1; p; p = p->next, j--) {
DBusMessageIter sub2;
- const guint8 *data = p->text;
+ const uint8_t *data = p->text;
dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "y", &sub2);
dbus_message_iter_append_fixed_array(&sub2, DBUS_TYPE_BYTE, &data, p->size);
@@ -1078,7 +1131,7 @@ static void service_resolver_callback(
dbus_connection_send(server->bus, reply, NULL);
dbus_message_unref(reply);
} else {
- g_assert(event == AVAHI_RESOLVER_TIMEOUT);
+ assert(event == AVAHI_RESOLVER_TIMEOUT);
respond_error(server->bus, i->message, AVAHI_ERR_TIMEOUT, NULL);
}
@@ -1144,10 +1197,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
}
state = avahi_server_get_state(avahi_server);
- return respond_int32(c, m, (gint32) state);
+ return respond_int32(c, m, (int32_t) state);
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceNameByIndex")) {
- gint32 idx;
+ int32_t idx;
int fd;
struct ifreq ifr;
@@ -1161,8 +1214,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
#else
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- gchar txt[256];
- g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
+ char txt[256];
+ snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
return respond_error(c, m, AVAHI_ERR_OS, txt);
}
@@ -1170,8 +1223,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
ifr.ifr_ifindex = idx;
if (ioctl(fd, SIOCGIFNAME, &ifr) < 0) {
- gchar txt[256];
- g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
+ char txt[256];
+ snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
close(fd);
return respond_error(c, m, AVAHI_ERR_OS, txt);
}
@@ -1182,7 +1235,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
#endif
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetNetworkInterfaceIndexByName")) {
- gchar *n;
+ char *n;
int fd;
struct ifreq ifr;
@@ -1195,17 +1248,17 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
return respond_int32(c, m, 1);
#else
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
- gchar txt[256];
- g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
+ char txt[256];
+ snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
return respond_error(c, m, AVAHI_ERR_OS, txt);
}
memset(&ifr, 0, sizeof(ifr));
- g_snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
+ snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "%s", n);
if (ioctl(fd, SIOCGIFINDEX, &ifr) < 0) {
- gchar txt[256];
- g_snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
+ char txt[256];
+ snprintf(txt, sizeof(txt), "OS Error: %s", strerror(errno));
close(fd);
return respond_error(c, m, AVAHI_ERR_OS, txt);
}
@@ -1216,7 +1269,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
#endif
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeHostName")) {
- gchar *n, * t;
+ char *n, * t;
if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
avahi_log_warn("Error parsing Server::GetAlternativeHostName message");
@@ -1225,12 +1278,12 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
t = avahi_alternative_host_name(n);
respond_string(c, m, t);
- g_free(t);
+ avahi_free(t);
return DBUS_HANDLER_RESULT_HANDLED;
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "GetAlternativeServiceName")) {
- gchar *n, *t;
+ char *n, *t;
if (!(dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &n, DBUS_TYPE_INVALID)) || !n) {
avahi_log_warn("Error parsing Server::GetAlternativeServiceName message");
@@ -1239,7 +1292,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
t = avahi_alternative_service_name(n);
respond_string(c, m, t);
- g_free(t);
+ avahi_free(t);
return DBUS_HANDLER_RESULT_HANDLED;
@@ -1270,10 +1323,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
}
- i = g_new(EntryGroupInfo, 1);
+ i = avahi_new(EntryGroupInfo, 1);
i->id = ++client->current_id;
i->client = client;
- i->path = g_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
+ i->path = avahi_strdup_printf("/Client%u/EntryGroup%u", client->id, i->id);
i->n_entries = 0;
AVAHI_LLIST_PREPEND(EntryGroupInfo, entry_groups, client->entry_groups, i);
client->n_objects++;
@@ -1288,8 +1341,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveHostName")) {
Client *client;
- gint32 interface, protocol, aprotocol;
- gchar *name;
+ int32_t interface, protocol, aprotocol;
+ char *name;
HostNameResolverInfo *i;
if (!dbus_message_get_args(
@@ -1313,7 +1366,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
}
- i = g_new(HostNameResolverInfo, 1);
+ i = avahi_new(HostNameResolverInfo, 1);
i->client = client;
i->message = dbus_message_ref(m);
AVAHI_LLIST_PREPEND(HostNameResolverInfo, host_name_resolvers, client->host_name_resolvers, i);
@@ -1328,8 +1381,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveAddress")) {
Client *client;
- gint32 interface, protocol;
- gchar *address;
+ int32_t interface, protocol;
+ char *address;
AddressResolverInfo *i;
AvahiAddress a;
@@ -1356,7 +1409,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
return respond_error(c, m, AVAHI_ERR_TOO_MANY_OBJECTS, NULL);
}
- i = g_new(AddressResolverInfo, 1);
+ i = avahi_new(AddressResolverInfo, 1);
i->client = client;
i->message = dbus_message_ref(m);
AVAHI_LLIST_PREPEND(AddressResolverInfo, address_resolvers, client->address_resolvers, i);
@@ -1380,8 +1433,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
NULL,
NULL
};
- gint32 interface, protocol, type;
- gchar *domain;
+ int32_t interface, protocol, type;
+ char *domain;
if (!dbus_message_get_args(
@@ -1408,10 +1461,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
if (!*domain)
domain = NULL;
- i = g_new(DomainBrowserInfo, 1);
+ i = avahi_new(DomainBrowserInfo, 1);
i->id = ++client->current_id;
i->client = client;
- i->path = g_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
+ i->path = avahi_strdup_printf("/Client%u/DomainBrowser%u", client->id, i->id);
AVAHI_LLIST_PREPEND(DomainBrowserInfo, domain_browsers, client->domain_browsers, i);
client->n_objects++;
@@ -1434,8 +1487,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
NULL,
NULL
};
- gint32 interface, protocol;
- gchar *domain;
+ int32_t interface, protocol;
+ char *domain;
if (!dbus_message_get_args(
m, &error,
@@ -1461,10 +1514,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
if (!*domain)
domain = NULL;
- i = g_new(ServiceTypeBrowserInfo, 1);
+ i = avahi_new(ServiceTypeBrowserInfo, 1);
i->id = ++client->current_id;
i->client = client;
- i->path = g_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
+ i->path = avahi_strdup_printf("/Client%u/ServiceTypeBrowser%u", client->id, i->id);
AVAHI_LLIST_PREPEND(ServiceTypeBrowserInfo, service_type_browsers, client->service_type_browsers, i);
client->n_objects++;
@@ -1487,8 +1540,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
NULL,
NULL
};
- gint32 interface, protocol;
- gchar *domain, *type;
+ int32_t interface, protocol;
+ char *domain, *type;
if (!dbus_message_get_args(
m, &error,
@@ -1515,10 +1568,10 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
if (!*domain)
domain = NULL;
- i = g_new(ServiceBrowserInfo, 1);
+ i = avahi_new(ServiceBrowserInfo, 1);
i->id = ++client->current_id;
i->client = client;
- i->path = g_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
+ i->path = avahi_strdup_printf("/Client%u/ServiceBrowser%u", client->id, i->id);
AVAHI_LLIST_PREPEND(ServiceBrowserInfo, service_browsers, client->service_browsers, i);
client->n_objects++;
@@ -1532,8 +1585,8 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
} else if (dbus_message_is_method_call(m, AVAHI_DBUS_INTERFACE_SERVER, "ResolveService")) {
Client *client;
- gint32 interface, protocol, aprotocol;
- gchar *name, *type, *domain;
+ int32_t interface, protocol, aprotocol;
+ char *name, *type, *domain;
ServiceResolverInfo *i;
if (!dbus_message_get_args(
@@ -1562,7 +1615,7 @@ static DBusHandlerResult msg_server_impl(DBusConnection *c, DBusMessage *m, void
if (!*domain)
domain = NULL;
- i = g_new(ServiceResolverInfo, 1);
+ i = avahi_new(ServiceResolverInfo, 1);
i->client = client;
i->message = dbus_message_ref(m);
AVAHI_LLIST_PREPEND(ServiceResolverInfo, service_resolvers, client->service_resolvers, i);
@@ -1587,19 +1640,19 @@ fail:
void dbus_protocol_server_state_changed(AvahiServerState state) {
DBusMessage *m;
- gint32 t;
+ int32_t t;
if (!server)
return;
m = dbus_message_new_signal(AVAHI_DBUS_PATH_SERVER, AVAHI_DBUS_INTERFACE_SERVER, "StateChanged");
- t = (gint32) state;
+ t = (int32_t) state;
dbus_message_append_args(m, DBUS_TYPE_INT32, &t, DBUS_TYPE_INVALID);
dbus_connection_send(server->bus, m, NULL);
dbus_message_unref(m);
}
-int dbus_protocol_setup(GMainLoop *loop) {
+int dbus_protocol_setup(const AvahiPoll *poll_api) {
DBusError error;
static const DBusObjectPathVTable server_vtable = {
@@ -1611,10 +1664,11 @@ int dbus_protocol_setup(GMainLoop *loop) {
NULL
};
+
dbus_error_init(&error);
- server = g_malloc(sizeof(Server));
- server->clients = NULL;
+ server = avahi_new(Server, 1);
+ AVAHI_LLIST_HEAD_INIT(Clients, server->clients);
server->current_id = 0;
server->n_clients = 0;
@@ -1624,8 +1678,7 @@ int dbus_protocol_setup(GMainLoop *loop) {
goto fail;
}
- dbus_connection_setup_with_g_main(server->bus, NULL);
- dbus_connection_set_exit_on_disconnect(server->bus, TRUE);
+ avahi_dbus_connection_glue(server->bus, poll_api);
dbus_bus_request_name(server->bus, AVAHI_DBUS_NAME, 0, &error);
if (dbus_error_is_set(&error)) {
@@ -1635,7 +1688,7 @@ int dbus_protocol_setup(GMainLoop *loop) {
dbus_bus_add_match(server->bus, "type='signal',""interface='" DBUS_INTERFACE_DBUS "'", &error);
- dbus_connection_add_filter(server->bus, msg_signal_filter_impl, loop, NULL);
+ dbus_connection_add_filter(server->bus, msg_signal_filter_impl, (void*) poll_api, NULL);
dbus_connection_register_object_path(server->bus, AVAHI_DBUS_PATH_SERVER, &server_vtable, NULL);
return 0;
@@ -1647,7 +1700,7 @@ fail:
}
dbus_error_free (&error);
- g_free(server);
+ avahi_free(server);
server = NULL;
return -1;
}
@@ -1659,14 +1712,14 @@ void dbus_protocol_shutdown(void) {
while (server->clients)
client_free(server->clients);
- g_assert(server->n_clients == 0);
+ assert(server->n_clients == 0);
if (server->bus) {
dbus_connection_disconnect(server->bus);
dbus_connection_unref(server->bus);
}
- g_free(server);
+ avahi_free(server);
server = NULL;
}
}
diff --git a/avahi-daemon/dbus-protocol.h b/avahi-daemon/dbus-protocol.h
index 185fa58..0877426 100644
--- a/avahi-daemon/dbus-protocol.h
+++ b/avahi-daemon/dbus-protocol.h
@@ -22,9 +22,7 @@
USA.
***/
-#include <glib.h>
-
-int dbus_protocol_setup(GMainLoop *loop);
+int dbus_protocol_setup(const AvahiPoll *poll_api);
void dbus_protocol_shutdown(void);
void dbus_protocol_server_state_changed(AvahiServerState state);
diff --git a/avahi-daemon/ini-file-parser-test.c b/avahi-daemon/ini-file-parser-test.c
new file mode 100644
index 0000000..0dff13f
--- /dev/null
+++ b/avahi-daemon/ini-file-parser-test.c
@@ -0,0 +1,59 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <stdio.h>
+
+#include <avahi-common/malloc.h>
+
+#include "ini-file-parser.h"
+
+int main(int argc, char *argv[]) {
+
+ AvahiIniFile *f;
+ AvahiIniFileGroup *g;
+
+ if (!(f = avahi_ini_file_load("avahi-daemon.conf"))) {
+ return 1;
+ }
+
+ printf("%u groups\n", f->n_groups);
+
+ for (g = f->groups; g; g = g->groups_next) {
+ AvahiIniFilePair *p;
+ printf("<%s> (%u pairs)\n", g->name, g->n_pairs);
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+ char **split, **i;
+
+ printf("\t<%s> = ", p->key);
+ split = avahi_split_csv(p->value);
+
+ for (i = split; *i; i++)
+ printf("<%s> ", *i);
+
+ avahi_strfreev(split);
+
+ printf("\n");
+ }
+ }
+
+ avahi_ini_file_free(f);
+}
diff --git a/avahi-daemon/ini-file-parser.c b/avahi-daemon/ini-file-parser.c
new file mode 100644
index 0000000..03a3f8b
--- /dev/null
+++ b/avahi-daemon/ini-file-parser.c
@@ -0,0 +1,186 @@
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <avahi-common/malloc.h>
+#include <avahi-core/log.h>
+
+#include "ini-file-parser.h"
+
+AvahiIniFile* avahi_ini_file_load(const char *fname) {
+ AvahiIniFile *f;
+ FILE *fo;
+ AvahiIniFileGroup *group = NULL;
+ unsigned line;
+
+ assert(fname);
+
+ if (!(fo = fopen(fname, "r"))) {
+ avahi_log_error("Failed to open file '%s': %s", fname, strerror(errno));
+ return NULL;
+ }
+
+ f = avahi_new(AvahiIniFile, 1);
+ AVAHI_LLIST_HEAD_INIT(AvahiIniFileGroup, f->groups);
+ f->n_groups = 0;
+
+ line = 0;
+ while (!feof(fo)) {
+ char ln[256], *s, *e;
+ AvahiIniFilePair *pair;
+
+ if (!(fgets(ln, sizeof(ln), fo)))
+ break;
+
+ line++;
+
+ s = ln + strspn(ln, " \t");
+ s[strcspn(s, "\r\n")] = 0;
+
+ /* Skip comments and empty lines */
+ if (*s == '#' || *s == '%' || *s == 0)
+ continue;
+
+ if (*s == '[') {
+ /* new group */
+
+ if (!(e = strchr(s, ']'))) {
+ avahi_log_error("Unclosed group header in %s:%u: <%s>", fname, line, s);
+ goto fail;
+ }
+
+ *e = 0;
+
+ group = avahi_new(AvahiIniFileGroup, 1);
+ group->name = avahi_strdup(s+1);
+ group->n_pairs = 0;
+ AVAHI_LLIST_HEAD_INIT(AvahiIniFilePair, group->pairs);
+
+ AVAHI_LLIST_PREPEND(AvahiIniFileGroup, groups, f->groups, group);
+ f->n_groups++;
+ } else {
+
+ /* Normal assignment */
+ if (!(e = strchr(s, '='))) {
+ avahi_log_error("Missing assignment in %s:%u: <%s>", fname, line, s);
+ goto fail;
+ }
+
+ if (!group) {
+ avahi_log_error("Assignment outside group in %s:%u <%s>", fname, line, s);
+ goto fail;
+ }
+
+ /* Split the key and the value */
+ *(e++) = 0;
+
+ pair = avahi_new(AvahiIniFilePair, 1);
+ pair->key = avahi_strdup(s);
+ pair->value = avahi_strdup(e);
+
+ AVAHI_LLIST_PREPEND(AvahiIniFilePair, pairs, group->pairs, pair);
+ group->n_pairs++;
+ }
+ }
+
+ fclose(fo);
+
+ return f;
+
+fail:
+
+ if (fo)
+ fclose(fo);
+
+ if (f)
+ avahi_ini_file_free(f);
+
+ return NULL;
+}
+
+void avahi_ini_file_free(AvahiIniFile *f) {
+ AvahiIniFileGroup *g;
+ assert(f);
+
+ while ((g = f->groups)) {
+ AvahiIniFilePair *p;
+
+ while ((p = g->pairs)) {
+ avahi_free(p->key);
+ avahi_free(p->value);
+
+ AVAHI_LLIST_REMOVE(AvahiIniFilePair, pairs, g->pairs, p);
+ avahi_free(p);
+ }
+
+ avahi_free(g->name);
+
+ AVAHI_LLIST_REMOVE(AvahiIniFileGroup, groups, f->groups, g);
+ avahi_free(g);
+ }
+
+ avahi_free(f);
+}
+
+char** avahi_split_csv(const char *t) {
+ unsigned n_comma = 0;
+ const char *p;
+ char **r, **i;
+
+ for (p = t; *p; p++)
+ if (*p == ',')
+ n_comma++;
+
+ i = r = avahi_new(char*, n_comma+2);
+
+ for (;;) {
+ size_t l = strcspn(t, ",");
+
+ *(i++) = avahi_strndup(t, l);
+
+ t += l;
+
+ if (*t == 0)
+ break;
+
+ assert(*t == ',');
+ t++;
+ }
+
+ *i = NULL;
+
+ return r;
+}
+
+void avahi_strfreev(char **p) {
+ char **i;
+
+ if (!p)
+ return;
+
+ for (i = p; *i; i++)
+ avahi_free(*i);
+
+ avahi_free(p);
+}
diff --git a/avahi-daemon/ini-file-parser.h b/avahi-daemon/ini-file-parser.h
new file mode 100644
index 0000000..82c5acf
--- /dev/null
+++ b/avahi-daemon/ini-file-parser.h
@@ -0,0 +1,57 @@
+#ifndef fooinifileparserhfoo
+#define fooinifileparserhfoo
+
+/* $Id$ */
+
+/***
+ This file is part of avahi.
+
+ avahi is free software; you can redistribute it and/or modify it
+ under the terms of the GNU Lesser General Public License as
+ published by the Free Software Foundation; either version 2.1 of the
+ License, or (at your option) any later version.
+
+ avahi is distributed in the hope that it will be useful, but WITHOUT
+ ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
+ Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with avahi; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+ USA.
+***/
+
+#include <avahi-common/llist.h>
+
+typedef struct AvahiIniFile AvahiIniFile;
+typedef struct AvahiIniFilePair AvahiIniFilePair;
+typedef struct AvahiIniFileGroup AvahiIniFileGroup;
+
+struct AvahiIniFilePair {
+ AVAHI_LLIST_FIELDS(AvahiIniFilePair, pairs);
+ char *key, *value;
+};
+
+struct AvahiIniFileGroup {
+ AVAHI_LLIST_FIELDS(AvahiIniFileGroup, groups);
+ char *name;
+
+ AVAHI_LLIST_HEAD(AvahiIniFilePair, pairs);
+ unsigned n_pairs;
+};
+
+struct AvahiIniFile {
+ AVAHI_LLIST_HEAD(AvahiIniFileGroup, groups);
+ unsigned n_groups;
+};
+
+
+AvahiIniFile* avahi_ini_file_load(const char *fname);
+void avahi_ini_file_free(AvahiIniFile *f);
+
+char** avahi_split_csv(const char *t);
+
+void avahi_strfreev(char **);
+
+#endif
diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c
index 04a632b..d0db0dd 100644
--- a/avahi-daemon/main.c
+++ b/avahi-daemon/main.c
@@ -23,6 +23,7 @@
#include <config.h>
#endif
+#include <assert.h>
#include <getopt.h>
#include <string.h>
#include <signal.h>
@@ -44,15 +45,16 @@
#include <libdaemon/dlog.h>
#include <libdaemon/dpid.h>
+#include <avahi-common/malloc.h>
+#include <avahi-common/simple-watch.h>
#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"
#include "dbus-protocol.h"
#include "static-services.h"
+#include "ini-file-parser.h"
AvahiServer *avahi_server = NULL;
@@ -68,21 +70,21 @@ typedef enum {
typedef struct {
AvahiServerConfig server_config;
DaemonCommand command;
- gboolean daemonize;
- gboolean use_syslog;
- gchar *config_file;
- gboolean enable_dbus;
- gboolean drop_root;
- gboolean publish_resolv_conf;
- gchar ** publish_dns_servers;
- gboolean no_rlimits;
- gboolean debug;
-
- gboolean rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
+ int daemonize;
+ int use_syslog;
+ char *config_file;
+ int enable_dbus;
+ int drop_root;
+ int publish_resolv_conf;
+ char ** publish_dns_servers;
+ int no_rlimits;
+ int debug;
+
+ int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
#ifdef RLIMIT_NPROC
- gboolean rlimit_nproc_set;
+ int rlimit_nproc_set;
rlim_t rlimit_nproc;
#endif
} DaemonConfig;
@@ -92,18 +94,26 @@ typedef struct {
static AvahiSEntryGroup *dns_servers_entry_group = NULL;
static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
-static gchar **resolv_conf = NULL;
+static char **resolv_conf = NULL;
static DaemonConfig config;
#define MAX_NAME_SERVERS 10
-static gint load_resolv_conf(const DaemonConfig *c) {
- gint ret = -1;
+static int has_prefix(const char *s, const char *prefix) {
+ size_t l;
+
+ l = strlen(prefix);
+
+ return strlen(s) >= l && strncmp(s, prefix, l) == 0;
+}
+
+static int load_resolv_conf(const DaemonConfig *c) {
+ int ret = -1;
FILE *f;
- gint i = 0;
+ int i = 0;
- g_strfreev(resolv_conf);
+ avahi_strfreev(resolv_conf);
resolv_conf = NULL;
if (!c->publish_resolv_conf)
@@ -114,11 +124,11 @@ static gint load_resolv_conf(const DaemonConfig *c) {
goto finish;
}
- resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
+ resolv_conf = avahi_new0(char*, MAX_NAME_SERVERS+1);
while (!feof(f) && i < MAX_NAME_SERVERS) {
char ln[128];
- gchar *p;
+ char *p;
if (!(fgets(ln, sizeof(ln), f)))
break;
@@ -126,11 +136,11 @@ static gint load_resolv_conf(const DaemonConfig *c) {
ln[strcspn(ln, "\r\n#")] = 0;
p = ln + strspn(ln, "\t ");
- if (g_str_has_prefix(p, "nameserver")) {
+ if (has_prefix(p, "nameserver")) {
p += 10;
p += strspn(p, "\t ");
p[strcspn(p, "\t ")] = 0;
- resolv_conf[i++] = strdup(p);
+ resolv_conf[i++] = avahi_strdup(p);
}
}
@@ -139,7 +149,7 @@ static gint load_resolv_conf(const DaemonConfig *c) {
finish:
if (ret != 0) {
- g_strfreev(resolv_conf);
+ avahi_strfreev(resolv_conf);
resolv_conf = NULL;
}
@@ -149,16 +159,16 @@ finish:
return ret;
}
-static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, gchar **l) {
- gchar **p;
+static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
+ char **p;
- g_assert(s);
- g_assert(l);
+ assert(s);
+ assert(l);
if (!g)
g = avahi_s_entry_group_new(s, NULL, NULL);
- g_assert(avahi_s_entry_group_is_empty(g));
+ assert(avahi_s_entry_group_is_empty(g));
for (p = l; *p; p++) {
AvahiAddress a;
@@ -190,8 +200,8 @@ static void remove_dns_server_entry_groups(void) {
static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
DaemonConfig *c = userdata;
- g_assert(s);
- g_assert(c);
+ assert(s);
+ assert(c);
/** This function is possibly called before the global variable
* avahi_server has been set, therefore we do it explicitly */
@@ -218,7 +228,7 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda
simple_protocol_restart_queries();
} else if (state == AVAHI_SERVER_COLLISION) {
- gchar *n;
+ char *n;
static_service_remove_from_server();
@@ -227,11 +237,11 @@ static void server_callback(AvahiServer *s, AvahiServerState state, void *userda
n = avahi_alternative_host_name(avahi_server_get_host_name(s));
avahi_log_warn("Host name conflict, retrying with <%s>", n);
avahi_server_set_host_name(s, n);
- g_free(n);
+ avahi_free(n);
}
}
-static void help(FILE *f, const gchar *argv0) {
+static void help(FILE *f, const char *argv0) {
fprintf(f,
"%s [options]\n"
" -h --help Show this help\n"
@@ -250,8 +260,8 @@ static void help(FILE *f, const gchar *argv0) {
}
-static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
- gint o;
+static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
+ int o;
enum {
OPTION_NO_RLIMITS = 256,
@@ -274,20 +284,20 @@ static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
{ NULL, 0, NULL, 0 }
};
- g_assert(c);
+ assert(c);
opterr = 0;
while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
switch(o) {
case 's':
- c->use_syslog = TRUE;
+ c->use_syslog = 1;
break;
case 'h':
c->command = DAEMON_HELP;
break;
case 'D':
- c->daemonize = TRUE;
+ c->daemonize = 1;
break;
case 'k':
c->command = DAEMON_KILL;
@@ -296,8 +306,8 @@ static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
c->command = DAEMON_VERSION;
break;
case 'f':
- g_free(c->config_file);
- c->config_file = g_strdup(optarg);
+ avahi_free(c->config_file);
+ c->config_file = avahi_strdup(optarg);
break;
case 'r':
c->command = DAEMON_RELOAD;
@@ -306,13 +316,13 @@ static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
c->command = DAEMON_CHECK;
break;
case OPTION_NO_RLIMITS:
- c->no_rlimits = TRUE;
+ c->no_rlimits = 1;
break;
case OPTION_NO_DROP_ROOT:
- c->drop_root = FALSE;
+ c->drop_root = 0;
break;
case OPTION_DEBUG:
- c->debug = TRUE;
+ c->debug = 1;
break;
default:
fprintf(stderr, "Invalid command line argument: %c\n", o);
@@ -328,177 +338,129 @@ static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
return 0;
}
-static gboolean is_yes(const gchar *s) {
- g_assert(s);
+static int is_yes(const char *s) {
+ assert(s);
return *s == 'y' || *s == 'Y';
}
-static gint load_config_file(DaemonConfig *c) {
+static int load_config_file(DaemonConfig *c) {
int r = -1;
- GKeyFile *f = NULL;
- GError *err = NULL;
- gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
+ AvahiIniFile *f;
+ AvahiIniFileGroup *g;
- g_assert(c);
-
- f = g_key_file_new();
- g_key_file_set_list_separator(f, ',');
-
- if (!g_key_file_load_from_file(f, c->config_file ? c->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
- fprintf(stderr, "Unable to read config file: %s\n", err->message);
- goto finish;
- }
-
- groups = g_key_file_get_groups(f, NULL);
-
- for (g = groups; *g; g++) {
- if (g_strcasecmp(*g, "server") == 0) {
- gchar **k;
-
- keys = g_key_file_get_keys(f, *g, NULL, NULL);
-
- for (k = keys; *k; k++) {
+ assert(c);
- v = g_key_file_get_value(f, *g, *k, NULL);
-
- if (g_strcasecmp(*k, "host-name") == 0) {
- g_free(c->server_config.host_name);
- c->server_config.host_name = v;
- v = NULL;
- } else if (g_strcasecmp(*k, "domain-name") == 0) {
- g_free(c->server_config.domain_name);
- c->server_config.domain_name = v;
- v = NULL;
- } else if (g_strcasecmp(*k, "use-ipv4") == 0)
- c->server_config.use_ipv4 = is_yes(v);
- else if (g_strcasecmp(*k, "use-ipv6") == 0)
- c->server_config.use_ipv6 = is_yes(v);
- else if (g_strcasecmp(*k, "check-response-ttl") == 0)
- c->server_config.check_response_ttl = is_yes(v);
- else if (g_strcasecmp(*k, "use-iff-running") == 0)
- c->server_config.use_iff_running = is_yes(v);
- else if (g_strcasecmp(*k, "enable-dbus") == 0)
- c->enable_dbus = is_yes(v);
- else if (g_strcasecmp(*k, "drop-root") == 0)
- c->drop_root = is_yes(v);
+ if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
+ goto finish;
+
+ for (g = f->groups; g; g = g->groups_next) {
+
+ if (strcasecmp(g->name, "server") == 0) {
+ AvahiIniFilePair *p;
+
+ for (p = g->pairs; p; p = p->pairs_next) {
+
+ if (strcasecmp(p->key, "host-name") == 0) {
+ avahi_free(c->server_config.host_name);
+ c->server_config.host_name = avahi_strdup(p->value);
+ } else if (strcasecmp(p->key, "domain-name") == 0) {
+ avahi_free(c->server_config.domain_name);
+ c->server_config.domain_name = avahi_strdup(p->value);
+ } else if (strcasecmp(p->key, "use-ipv4") == 0)
+ c->server_config.use_ipv4 = is_yes(p->value);
+ else if (strcasecmp(p->key, "use-ipv6") == 0)
+ c->server_config.use_ipv6 = is_yes(p->value);
+ else if (strcasecmp(p->key, "check-response-ttl") == 0)
+ c->server_config.check_response_ttl = is_yes(p->value);
+ else if (strcasecmp(p->key, "use-iff-running") == 0)
+ c->server_config.use_iff_running = is_yes(p->value);
+ else if (strcasecmp(p->key, "enable-dbus") == 0)
+ c->enable_dbus = is_yes(p->value);
+ else if (strcasecmp(p->key, "drop-root") == 0)
+ c->drop_root = is_yes(p->value);
else {
- fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
goto finish;
}
-
- g_free(v);
- v = NULL;
}
-
- g_strfreev(keys);
- keys = NULL;
-
- } else if (g_strcasecmp(*g, "publish") == 0) {
- gchar **k;
- keys = g_key_file_get_keys(f, *g, NULL, NULL);
+ } else if (strcasecmp(g->name, "publish") == 0) {
+ AvahiIniFilePair *p;
- for (k = keys; *k; k++) {
-
- v = g_key_file_get_string(f, *g, *k, NULL);
+ for (p = g->pairs; p; p = p->pairs_next) {
- if (g_strcasecmp(*k, "publish-addresses") == 0)
- c->server_config.publish_addresses = is_yes(v);
- else if (g_strcasecmp(*k, "publish-hinfo") == 0)
- c->server_config.publish_hinfo = is_yes(v);
- else if (g_strcasecmp(*k, "publish-workstation") == 0)
- c->server_config.publish_workstation = is_yes(v);
- else if (g_strcasecmp(*k, "publish-domain") == 0)
- c->server_config.publish_domain = is_yes(v);
- else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
- c->publish_resolv_conf = is_yes(v);
- else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
- g_strfreev(c->publish_dns_servers);
- c->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
+ if (strcasecmp(p->key, "publish-addresses") == 0)
+ c->server_config.publish_addresses = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-hinfo") == 0)
+ c->server_config.publish_hinfo = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-workstation") == 0)
+ c->server_config.publish_workstation = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-domain") == 0)
+ c->server_config.publish_domain = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
+ c->publish_resolv_conf = is_yes(p->value);
+ else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
+ avahi_strfreev(c->publish_dns_servers);
+ c->publish_dns_servers = avahi_split_csv(p->value);
} else {
- fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
goto finish;
}
-
- g_free(v);
- v = NULL;
}
- g_strfreev(keys);
- keys = NULL;
-
- } else if (g_strcasecmp(*g, "reflector") == 0) {
- gchar **k;
-
- keys = g_key_file_get_keys(f, *g, NULL, NULL);
-
- for (k = keys; *k; k++) {
+ } else if (strcasecmp(g->name, "reflector") == 0) {
+ AvahiIniFilePair *p;
- v = g_key_file_get_string(f, *g, *k, NULL);
+ for (p = g->pairs; p; p = p->pairs_next) {
- if (g_strcasecmp(*k, "enable-reflector") == 0)
- c->server_config.enable_reflector = is_yes(v);
- else if (g_strcasecmp(*k, "reflect-ipv") == 0)
- c->server_config.reflect_ipv = is_yes(v);
+ if (strcasecmp(p->key, "enable-reflector") == 0)
+ c->server_config.enable_reflector = is_yes(p->value);
+ else if (strcasecmp(p->key, "reflect-ipv") == 0)
+ c->server_config.reflect_ipv = is_yes(p->value);
else {
- fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
goto finish;
}
-
- g_free(v);
- v = NULL;
}
-
- g_strfreev(keys);
- keys = NULL;
-
- } else if (g_strcasecmp(*g, "rlimits") == 0) {
- gchar **k;
- keys = g_key_file_get_keys(f, *g, NULL, NULL);
-
- for (k = keys; *k; k++) {
+ } else if (strcasecmp(g->name, "rlimits") == 0) {
+ AvahiIniFilePair *p;
- v = g_key_file_get_string(f, *g, *k, NULL);
+ for (p = g->pairs; p; p = p->pairs_next) {
- if (g_strcasecmp(*k, "rlimit-as") == 0) {
- c->rlimit_as_set = TRUE;
- c->rlimit_as = atoi(v);
- } else if (g_strcasecmp(*k, "rlimit-core") == 0) {
- c->rlimit_core_set = TRUE;
- c->rlimit_core = atoi(v);
- } else if (g_strcasecmp(*k, "rlimit-data") == 0) {
- c->rlimit_data_set = TRUE;
- c->rlimit_data = atoi(v);
- } else if (g_strcasecmp(*k, "rlimit-fsize") == 0) {
- c->rlimit_fsize_set = TRUE;
- c->rlimit_fsize = atoi(v);
- } else if (g_strcasecmp(*k, "rlimit-nofile") == 0) {
- c->rlimit_nofile_set = TRUE;
- c->rlimit_nofile = atoi(v);
- } else if (g_strcasecmp(*k, "rlimit-stack") == 0) {
- c->rlimit_stack_set = TRUE;
- c->rlimit_stack = atoi(v);
+ if (strcasecmp(p->key, "rlimit-as") == 0) {
+ c->rlimit_as_set = 1;
+ c->rlimit_as = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-core") == 0) {
+ c->rlimit_core_set = 1;
+ c->rlimit_core = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-data") == 0) {
+ c->rlimit_data_set = 1;
+ c->rlimit_data = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
+ c->rlimit_fsize_set = 1;
+ c->rlimit_fsize = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
+ c->rlimit_nofile_set = 1;
+ c->rlimit_nofile = atoi(p->value);
+ } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
+ c->rlimit_stack_set = 1;
+ c->rlimit_stack = atoi(p->value);
#ifdef RLIMIT_NPROC
- } else if (g_strcasecmp(*k, "rlimit-nproc") == 0) {
- c->rlimit_nproc_set = TRUE;
- c->rlimit_nproc = atoi(v);
+ } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
+ c->rlimit_nproc_set = 1;
+ c->rlimit_nproc = atoi(p->value);
#endif
} else {
- fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
+ avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
goto finish;
}
- g_free(v);
- v = NULL;
}
-
- g_strfreev(keys);
- keys = NULL;
} else {
- fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
+ avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
goto finish;
}
}
@@ -507,20 +469,13 @@ static gint load_config_file(DaemonConfig *c) {
finish:
- g_strfreev(groups);
- g_strfreev(keys);
- g_free(v);
-
- if (err)
- g_error_free (err);
-
if (f)
- g_key_file_free(f);
+ avahi_ini_file_free(f);
return r;
}
-static void log_function(AvahiLogLevel level, const gchar *txt) {
+static void log_function(AvahiLogLevel level, const char *txt) {
static const int const log_level_map[] = {
LOG_ERR,
@@ -530,8 +485,8 @@ static void log_function(AvahiLogLevel level, const gchar *txt) {
LOG_DEBUG
};
- g_assert(level < AVAHI_LOG_LEVEL_MAX);
- g_assert(txt);
+ assert(level < AVAHI_LOG_LEVEL_MAX);
+ assert(txt);
if (!config.debug && level == AVAHI_LOG_DEBUG)
return;
@@ -539,20 +494,24 @@ static void log_function(AvahiLogLevel level, const gchar *txt) {
daemon_log(log_level_map[level], "%s", txt);
}
-static void dump(const gchar *text, gpointer userdata) {
+static void dump(const char *text, void* userdata) {
avahi_log_info("%s", text);
}
-static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
- gint sig;
- GMainLoop *loop = data;
+static void signal_callback(AvahiWatch *watch, int fd, AvahiWatchEvent event, void *userdata) {
+ int sig;
+ AvahiSimplePoll *simple_poll_api = userdata;
+ const AvahiPoll *poll_api;
- g_assert(source);
- g_assert(loop);
+ assert(watch);
+ assert(simple_poll_api);
+
+ poll_api = avahi_simple_poll_get(simple_poll_api);
if ((sig = daemon_signal_next()) <= 0) {
avahi_log_error("daemon_signal_next() failed");
- return FALSE;
+ poll_api->watch_free(watch);
+ return;
}
switch (sig) {
@@ -563,7 +522,7 @@ static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpoi
"Got %s, quitting.",
sig == SIGINT ? "SIGINT" :
(sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
- g_main_loop_quit(loop);
+ avahi_simple_poll_quit(simple_poll_api);
break;
case SIGHUP:
@@ -590,49 +549,47 @@ static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpoi
avahi_log_warn("Got spurious signal, ignoring.");
break;
}
-
- return TRUE;
}
-static gint run_server(DaemonConfig *c) {
- GMainLoop *loop = NULL;
- gint r = -1;
- GIOChannel *io = NULL;
- guint watch_id = (guint) -1;
- gint error;
- AvahiGLibPoll *poll_api;
+static int run_server(DaemonConfig *c) {
+ int r = -1;
+ int error;
+ AvahiSimplePoll *simple_poll_api;
+ const AvahiPoll *poll_api;
+ AvahiWatch *sig_watch;
+
+ assert(c);
- g_assert(c);
+ if (!(simple_poll_api = avahi_simple_poll_new())) {
+ avahi_log_error("Failed to create main loop object.");
+ goto finish;
+ }
- poll_api = avahi_glib_poll_new(NULL, G_PRIORITY_DEFAULT);
- loop = g_main_loop_new(NULL, FALSE);
+ poll_api = avahi_simple_poll_get(simple_poll_api);
if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
goto finish;
}
- if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
- avahi_log_error( "Failed to create signal io channel.");
+ if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
+ avahi_log_error( "Failed to create signal watcher");
goto finish;
}
- g_io_channel_set_close_on_unref(io, FALSE);
- watch_id = g_io_add_watch(io, G_IO_IN, signal_callback, loop);
-
- if (simple_protocol_setup(NULL) < 0)
+ if (simple_protocol_setup(poll_api) < 0)
goto finish;
#ifdef ENABLE_DBUS
if (c->enable_dbus)
- if (dbus_protocol_setup(loop) < 0)
+ if (dbus_protocol_setup(poll_api) < 0)
goto finish;
#endif
load_resolv_conf(c);
static_service_load();
- if (!(avahi_server = avahi_server_new(avahi_glib_poll_get(poll_api), &c->server_config, server_callback, c, &error))) {
+ if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
avahi_log_error("Failed to create server: %s", avahi_strerror(error));
goto finish;
}
@@ -641,9 +598,20 @@ static gint run_server(DaemonConfig *c) {
if (c->daemonize)
daemon_retval_send(0);
- r = 0;
+ for (;;) {
+ if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
- g_main_loop_run(loop);
+ /* We handle signals through an FD, so let's continue */
+ if (errno == EINTR)
+ continue;
+
+ avahi_log_error("poll(): %s", strerror(errno));
+ goto finish;
+ } else if (r > 0)
+ /* Quit */
+ break;
+ }
+
finish:
@@ -663,28 +631,24 @@ finish:
daemon_signal_done();
- if (watch_id != (guint) -1)
- g_source_remove(watch_id);
-
- if (io)
- g_io_channel_unref(io);
+ if (sig_watch)
+ poll_api->watch_free(sig_watch);
- if (poll_api)
- avahi_glib_poll_free(poll_api);
+ if (simple_poll_api)
+ avahi_simple_poll_free(simple_poll_api);
- if (loop)
- g_main_loop_unref(loop);
-
if (r != 0 && c->daemonize)
daemon_retval_send(1);
return r;
}
-static gint drop_root(void) {
+#define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
+
+static int drop_root(void) {
struct passwd *pw;
struct group * gr;
- gint r;
+ int r;
if (!(pw = getpwnam(AVAHI_USER))) {
avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
@@ -731,9 +695,9 @@ static gint drop_root(void) {
return -1;
}
- g_setenv("USER", pw->pw_name, 1);
- g_setenv("LOGNAME", pw->pw_name, 1);
- g_setenv("HOME", pw->pw_dir, 1);
+ set_env("USER", pw->pw_name);
+ set_env("LOGNAME", pw->pw_name);
+ set_env("HOME", pw->pw_dir);
avahi_log_info("Successfully dropped root privileges.");
@@ -744,10 +708,10 @@ static const char* pid_file_proc(void) {
return AVAHI_DAEMON_RUNTIME_DIR"/pid";
}
-static gint make_runtime_dir(void) {
- gint r = -1;
+static int make_runtime_dir(void) {
+ int r = -1;
mode_t u;
- gboolean reset_umask = FALSE;
+ int reset_umask = 0;
struct passwd *pw;
struct group * gr;
struct stat st;
@@ -763,7 +727,7 @@ static gint make_runtime_dir(void) {
}
u = umask(0000);
- reset_umask = TRUE;
+ reset_umask = 1;
if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
@@ -790,7 +754,7 @@ fail:
return r;
}
-static void set_one_rlimit(int resource, rlim_t limit, const gchar *name) {
+static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
struct rlimit rl;
rl.rlim_cur = rl.rlim_max = limit;
@@ -844,35 +808,34 @@ static void init_rand_seed(void) {
}
int main(int argc, char *argv[]) {
- gint r = 255;
- const gchar *argv0;
- gboolean wrote_pid_file = FALSE;
+ int r = 255;
+ const char *argv0;
+ int wrote_pid_file = 0;
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;
- config.daemonize = FALSE;
+ config.daemonize = 0;
config.config_file = NULL;
- config.enable_dbus = TRUE;
- config.drop_root = TRUE;
+ config.enable_dbus = 1;
+ config.drop_root = 1;
config.publish_dns_servers = NULL;
- config.publish_resolv_conf = FALSE;
- config.use_syslog = FALSE;
- config.no_rlimits = FALSE;
- config.debug = FALSE;
+ config.publish_resolv_conf = 0;
+ config.use_syslog = 0;
+ config.no_rlimits = 0;
+ config.debug = 0;
- config.rlimit_as_set = FALSE;
- config.rlimit_core_set = FALSE;
- config.rlimit_data_set = FALSE;
- config.rlimit_fsize_set = FALSE;
- config.rlimit_nofile_set = FALSE;
- config.rlimit_stack_set = FALSE;
+ config.rlimit_as_set = 0;
+ config.rlimit_core_set = 0;
+ config.rlimit_data_set = 0;
+ config.rlimit_fsize_set = 0;
+ config.rlimit_nofile_set = 0;
+ config.rlimit_stack_set = 0;
#ifdef RLIMIT_NPROC
- config.rlimit_nproc_set = FALSE;
+ config.rlimit_nproc_set = 0;
#endif
if ((argv0 = strrchr(argv[0], '/')))
@@ -966,7 +929,7 @@ int main(int argc, char *argv[]) {
daemon_retval_send(1);
goto finish;
} else
- wrote_pid_file = TRUE;
+ wrote_pid_file = 1;
if (!config.no_rlimits)
enforce_rlimits();
@@ -985,9 +948,9 @@ finish:
daemon_retval_done();
avahi_server_config_free(&config.server_config);
- g_free(config.config_file);
- g_strfreev(config.publish_dns_servers);
- g_strfreev(resolv_conf);
+ avahi_free(config.config_file);
+ avahi_strfreev(config.publish_dns_servers);
+ avahi_strfreev(resolv_conf);
if (wrote_pid_file)
daemon_pid_file_remove();
diff --git a/avahi-daemon/simple-protocol.c b/avahi-daemon/simple-protocol.c
index ec43049..c72fb86 100644
--- a/avahi-daemon/simple-protocol.c
+++ b/avahi-daemon/simple-protocol.c
@@ -23,6 +23,7 @@
#include <config.h>
#endif
+#include <assert.h>
#include <string.h>
#include <sys/socket.h>
#include <stdio.h>
@@ -31,9 +32,8 @@
#include <errno.h>
#include <fcntl.h>
-#include <glib.h>
-
#include <avahi-common/llist.h>
+#include <avahi-common/malloc.h>
#include <avahi-core/log.h>
#include "simple-protocol.h"
@@ -59,11 +59,11 @@ struct Client {
ClientState state;
- gint fd;
- GPollFD poll_fd;
+ int fd;
+ AvahiWatch *watch;
- gchar inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
- guint inbuf_length, outbuf_length;
+ char inbuf[BUFFER_SIZE], outbuf[BUFFER_SIZE];
+ size_t inbuf_length, outbuf_length;
AvahiSHostNameResolver *host_name_resolver;
AvahiSAddressResolver *address_resolver;
@@ -75,22 +75,23 @@ struct Client {
};
struct Server {
- GSource source;
- GMainContext *context;
- GPollFD poll_fd;
- gint fd;
+ const AvahiPoll *poll_api;
+ int fd;
+ AvahiWatch *watch;
AVAHI_LLIST_HEAD(Client, clients);
- guint n_clients;
- gboolean bind_successful;
+ unsigned n_clients;
+ int bind_successful;
};
static Server *server = NULL;
+static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata);
+
static void client_free(Client *c) {
- g_assert(c);
+ assert(c);
- g_assert(c->server->n_clients >= 1);
+ assert(c->server->n_clients >= 1);
c->server->n_clients--;
if (c->host_name_resolver)
@@ -101,19 +102,20 @@ static void client_free(Client *c) {
if (c->dns_server_browser)
avahi_s_dns_server_browser_free(c->dns_server_browser);
-
- g_source_remove_poll(&c->server->source, &c->poll_fd);
+
+ c->server->poll_api->watch_free(c->watch);
close(c->fd);
+
AVAHI_LLIST_REMOVE(Client, clients, c->server->clients, c);
- g_free(c);
+ avahi_free(c);
}
static void client_new(Server *s, int fd) {
Client *c;
- g_assert(fd >= 0);
+ assert(fd >= 0);
- c = g_new(Client, 1);
+ c = avahi_new(Client, 1);
c->server = s;
c->fd = fd;
c->state = CLIENT_IDLE;
@@ -124,20 +126,17 @@ static void client_new(Server *s, int fd) {
c->address_resolver = NULL;
c->dns_server_browser = NULL;
- memset(&c->poll_fd, 0, sizeof(GPollFD));
- c->poll_fd.fd = fd;
- c->poll_fd.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
- g_source_add_poll(&s->source, &c->poll_fd);
+ c->watch = s->poll_api->watch_new(s->poll_api, fd, AVAHI_WATCH_IN, client_work, c);
AVAHI_LLIST_PREPEND(Client, clients, s->clients, c);
s->n_clients++;
}
-static void client_output(Client *c, const guint8*data, guint size) {
- guint k, m;
+static void client_output(Client *c, const uint8_t*data, size_t size) {
+ size_t k, m;
- g_assert(c);
- g_assert(data);
+ assert(c);
+ assert(data);
if (!size)
return;
@@ -148,19 +147,19 @@ static void client_output(Client *c, const guint8*data, guint size) {
memcpy(c->outbuf + c->outbuf_length, data, m);
c->outbuf_length += m;
- c->poll_fd.events |= G_IO_OUT;
+ server->poll_api->watch_update(c->watch, AVAHI_WATCH_OUT);
}
-static void client_output_printf(Client *c, const gchar *format, ...) {
- gchar *t;
+static void client_output_printf(Client *c, const char *format, ...) {
+ char *t;
va_list ap;
va_start(ap, format);
- t = g_strdup_vprintf(format, ap);
+ t = avahi_strdup_vprintf(format, ap);
va_end(ap);
- client_output(c, (guint8*) t, strlen(t));
- g_free(t);
+ client_output(c, (uint8_t*) t, strlen(t));
+ avahi_free(t);
}
@@ -175,12 +174,12 @@ static void host_name_resolver_callback(
Client *c = userdata;
- g_assert(c);
+ assert(c);
if (event == AVAHI_RESOLVER_TIMEOUT)
client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
else {
- gchar t[64];
+ char t[64];
avahi_address_snprint(t, sizeof(t), a);
client_output_printf(c, "+ %i %u %s %s\n", iface, protocol, hostname, t);
}
@@ -199,7 +198,7 @@ static void address_resolver_callback(
Client *c = userdata;
- g_assert(c);
+ assert(c);
if (event == AVAHI_RESOLVER_TIMEOUT)
client_output_printf(c, "%+i Query timed out\n", AVAHI_ERR_TIMEOUT);
@@ -220,9 +219,9 @@ static void dns_server_browser_callback(
void* userdata) {
Client *c = userdata;
- gchar t[64];
+ char t[64];
- g_assert(c);
+ assert(c);
if (!a)
return;
@@ -231,12 +230,12 @@ static void dns_server_browser_callback(
client_output_printf(c, "%c %i %u %s %u\n", event == AVAHI_BROWSER_NEW ? '>' : '<', interface, protocol, t, port);
}
-static void handle_line(Client *c, const gchar *s) {
- gchar cmd[64], arg[64];
- gint n_args;
+static void handle_line(Client *c, const char *s) {
+ char cmd[64], arg[64];
+ int n_args;
- g_assert(c);
- g_assert(s);
+ assert(c);
+ assert(s);
if (c->state != CLIENT_IDLE)
return;
@@ -312,16 +311,16 @@ fail:
}
static void handle_input(Client *c) {
- g_assert(c);
+ assert(c);
for (;;) {
- gchar *e;
- guint k;
+ char *e;
+ size_t k;
if (!(e = memchr(c->inbuf, '\n', c->inbuf_length)))
break;
- k = e - (gchar*) c->inbuf;
+ k = e - (char*) c->inbuf;
*e = 0;
handle_line(c, c->inbuf);
@@ -330,10 +329,12 @@ static void handle_input(Client *c) {
}
}
-static void client_work(Client *c) {
- g_assert(c);
+static void client_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
+ Client *c = userdata;
+
+ assert(c);
- if ((c->poll_fd.revents & G_IO_IN) && c->inbuf_length < sizeof(c->inbuf)) {
+ if ((events & AVAHI_WATCH_IN) && c->inbuf_length < sizeof(c->inbuf)) {
ssize_t r;
if ((r = read(c->fd, c->inbuf + c->inbuf_length, sizeof(c->inbuf) - c->inbuf_length)) <= 0) {
@@ -344,12 +345,12 @@ static void client_work(Client *c) {
}
c->inbuf_length += r;
- g_assert(c->inbuf_length <= sizeof(c->inbuf));
+ assert(c->inbuf_length <= sizeof(c->inbuf));
handle_input(c);
}
- if ((c->poll_fd.revents & G_IO_OUT) && c->outbuf_length > 0) {
+ if ((events & AVAHI_WATCH_OUT) && c->outbuf_length > 0) {
ssize_t r;
if ((r = write(c->fd, c->outbuf, c->outbuf_length)) < 0) {
@@ -358,7 +359,7 @@ static void client_work(Client *c) {
return;
}
- g_assert((guint) r <= c->outbuf_length);
+ assert((size_t) r <= c->outbuf_length);
c->outbuf_length -= r;
if (c->outbuf_length)
@@ -370,84 +371,40 @@ static void client_work(Client *c) {
}
}
- c->poll_fd.events =
- G_IO_ERR |
- G_IO_HUP |
- (c->outbuf_length > 0 ? G_IO_OUT : 0) |
- (c->inbuf_length < sizeof(c->inbuf) ? G_IO_IN : 0);
+ c->server->poll_api->watch_update(
+ watch,
+ (c->outbuf_length > 0 ? AVAHI_WATCH_OUT : 0) |
+ (c->inbuf_length < sizeof(c->inbuf) ? AVAHI_WATCH_IN : 0));
}
-static gboolean prepare_func(GSource *source, gint *timeout) {
- g_assert(source);
- g_assert(timeout);
-
- *timeout = -1;
- return FALSE;
-}
-
-static gboolean check_func(GSource *source) {
- Server *s = (Server*) source;
- Client *c;
-
- g_assert(s);
-
- if (s->poll_fd.revents)
- return TRUE;
-
- for (c = s->clients; c; c = c->clients_next)
- if (c->poll_fd.revents)
- return TRUE;
-
- return FALSE;
-}
+static void server_work(AvahiWatch *watch, int fd, AvahiWatchEvent events, void *userdata) {
+ Server *s = userdata;
-static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
- Server *s = (Server*) source;
- Client *c, *n;
-
- g_assert(s);
+ assert(s);
- if (s->poll_fd.revents & G_IO_IN) {
- gint fd;
+ if (events & AVAHI_WATCH_IN) {
+ int cfd;
- if ((fd = accept(s->fd, NULL, NULL)) < 0)
- avahi_log_warn("accept(): %s", strerror(errno));
+ if ((cfd = accept(fd, NULL, NULL)) < 0)
+ avahi_log_error("accept(): %s", strerror(errno));
else
- client_new(s, fd);
- } else if (s->poll_fd.revents)
- g_error("Invalid revents");
-
- for (c = s->clients; c; c = n) {
- n = c->clients_next;
- if (c->poll_fd.revents)
- client_work(c);
+ client_new(s, cfd);
}
-
- return TRUE;
}
-
-int simple_protocol_setup(GMainContext *c) {
+
+int simple_protocol_setup(const AvahiPoll *poll_api) {
struct sockaddr_un sa;
mode_t u;
- static GSourceFuncs source_funcs = {
- prepare_func,
- check_func,
- dispatch_func,
- NULL,
- NULL,
- NULL
- };
-
- g_assert(!server);
+ assert(!server);
- server = (Server*) g_source_new(&source_funcs, sizeof(Server));
- server->bind_successful = FALSE;
+ server = avahi_new(Server, 1);
+ server->poll_api = poll_api;
+ server->bind_successful = 0;
server->fd = -1;
AVAHI_LLIST_HEAD_INIT(Client, server->clients);
- g_main_context_ref(server->context = (c ? c : g_main_context_default()));
- server->clients = NULL;
-
+ server->watch = NULL;
+
u = umask(0000);
if ((server->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
@@ -471,7 +428,7 @@ int simple_protocol_setup(GMainContext *c) {
goto fail;
}
- server->bind_successful = TRUE;
+ server->bind_successful = 1;
if (listen(server->fd, 2) < 0) {
avahi_log_warn("listen(): %s", strerror(errno));
@@ -480,12 +437,7 @@ int simple_protocol_setup(GMainContext *c) {
umask(u);
- memset(&server->poll_fd, 0, sizeof(GPollFD));
- server->poll_fd.fd = server->fd;
- server->poll_fd.events = G_IO_IN|G_IO_ERR;
- g_source_add_poll(&server->source, &server->poll_fd);
-
- g_source_attach(&server->source, server->context);
+ server->watch = poll_api->watch_new(poll_api, server->fd, AVAHI_WATCH_IN, server_work, server);
return 0;
@@ -501,20 +453,20 @@ void simple_protocol_shutdown(void) {
if (server) {
+ if (server->bind_successful)
+ unlink(AVAHI_SOCKET);
+
while (server->clients)
client_free(server->clients);
- if (server->bind_successful)
- unlink(AVAHI_SOCKET);
+ if (server->watch)
+ server->poll_api->watch_free(server->watch);
if (server->fd >= 0)
close(server->fd);
- g_main_context_unref(server->context);
+ avahi_free(server);
- g_source_destroy(&server->source);
- g_source_unref(&server->source);
-
server = NULL;
}
}
@@ -524,7 +476,7 @@ void simple_protocol_restart_queries(void) {
/* Restart queries in case of local domain name changes */
- g_assert(server);
+ assert(server);
for (c = server->clients; c; c = c->clients_next)
if (c->state == CLIENT_BROWSE_DNS_SERVERS && c->dns_server_browser) {
diff --git a/avahi-daemon/simple-protocol.h b/avahi-daemon/simple-protocol.h
index 0ca219c..1e64036 100644
--- a/avahi-daemon/simple-protocol.h
+++ b/avahi-daemon/simple-protocol.h
@@ -22,7 +22,9 @@
USA.
***/
-int simple_protocol_setup(GMainContext *c);
+#include <avahi-common/watch.h>
+
+int simple_protocol_setup(const AvahiPoll *poll_api);
void simple_protocol_shutdown(void);
void simple_protocol_restart_queries(void);
diff --git a/avahi-daemon/static-services.c b/avahi-daemon/static-services.c
index 03824fd..2e6c1bb 100644
--- a/avahi-daemon/static-services.c
+++ b/avahi-daemon/static-services.c
@@ -31,10 +31,10 @@
#include <fcntl.h>
#include <unistd.h>
-#include <glib.h>
#include <expat.h>
#include <avahi-common/llist.h>
+#include <avahi-common/malloc.h>
#include <avahi-core/log.h>
#include "main.h"
@@ -46,10 +46,10 @@ typedef struct StaticServiceGroup StaticServiceGroup;
struct StaticService {
StaticServiceGroup *group;
- gchar *type;
- gchar *domain_name;
- gchar *host_name;
- guint16 port;
+ char *type;
+ char *domain_name;
+ char *host_name;
+ uint16_t port;
AvahiStringList *txt_records;
@@ -57,11 +57,11 @@ struct StaticService {
};
struct StaticServiceGroup {
- gchar *filename;
+ char *filename;
time_t mtime;
- gchar *name, *chosen_name;
- gboolean replace_wildcards;
+ char *name, *chosen_name;
+ int replace_wildcards;
AvahiSEntryGroup *entry_group;
AVAHI_LLIST_HEAD(StaticService, services);
@@ -70,30 +70,30 @@ struct StaticServiceGroup {
static AVAHI_LLIST_HEAD(StaticServiceGroup, groups) = NULL;
-static gchar *replacestr(const gchar *pattern, const gchar *a, const gchar *b) {
- gchar *r = NULL, *e, *n;
+static char *replacestr(const char *pattern, const char *a, const char *b) {
+ char *r = NULL, *e, *n;
while ((e = strstr(pattern, a))) {
- gchar *k;
+ char *k;
- k = g_strndup(pattern, e - pattern);
+ k = avahi_strndup(pattern, e - pattern);
if (r)
- n = g_strconcat(r, k, b, NULL);
+ n = avahi_strdup_printf("%s%s%s", r, k, b);
else
- n = g_strconcat(k, b, NULL);
+ n = avahi_strdup_printf("%s%s", k, b);
- g_free(k);
- g_free(r);
+ avahi_free(k);
+ avahi_free(r);
r = n;
pattern = e + strlen(a);
}
if (!r)
- return g_strdup(pattern);
+ return avahi_strdup(pattern);
- n = g_strconcat(r, pattern, NULL);
- g_free(r);
+ n = avahi_strdup_printf("%s%s", r, pattern);
+ avahi_free(r);
return n;
}
@@ -104,8 +104,8 @@ static void remove_static_service_group_from_server(StaticServiceGroup *g);
static StaticService *static_service_new(StaticServiceGroup *group) {
StaticService *s;
- g_assert(group);
- s = g_new(StaticService, 1);
+ assert(group);
+ s = avahi_new(StaticService, 1);
s->group = group;
s->type = s->host_name = s->domain_name = NULL;
@@ -118,15 +118,15 @@ static StaticService *static_service_new(StaticServiceGroup *group) {
return s;
}
-static StaticServiceGroup *static_service_group_new(gchar *filename) {
+static StaticServiceGroup *static_service_group_new(char *filename) {
StaticServiceGroup *g;
- g_assert(filename);
+ assert(filename);
- g = g_new(StaticServiceGroup, 1);
- g->filename = g_strdup(filename);
+ g = avahi_new(StaticServiceGroup, 1);
+ g->filename = avahi_strdup(filename);
g->mtime = 0;
g->name = g->chosen_name = NULL;
- g->replace_wildcards = FALSE;
+ g->replace_wildcards = 0;
g->entry_group = NULL;
AVAHI_LLIST_HEAD_INIT(StaticService, g->services);
@@ -136,21 +136,21 @@ static StaticServiceGroup *static_service_group_new(gchar *filename) {
}
static void static_service_free(StaticService *s) {
- g_assert(s);
+ assert(s);
AVAHI_LLIST_REMOVE(StaticService, services, s->group->services, s);
- g_free(s->type);
- g_free(s->host_name);
- g_free(s->domain_name);
+ avahi_free(s->type);
+ avahi_free(s->host_name);
+ avahi_free(s->domain_name);
avahi_string_list_free(s->txt_records);
- g_free(s);
+ avahi_free(s);
}
static void static_service_group_free(StaticServiceGroup *g) {
- g_assert(g);
+ assert(g);
if (g->entry_group)
avahi_s_entry_group_free(g->entry_group);
@@ -160,25 +160,25 @@ static void static_service_group_free(StaticServiceGroup *g) {
AVAHI_LLIST_REMOVE(StaticServiceGroup, groups, groups, g);
- g_free(g->filename);
- g_free(g->name);
- g_free(g->chosen_name);
- g_free(g);
+ avahi_free(g->filename);
+ avahi_free(g->name);
+ avahi_free(g->chosen_name);
+ avahi_free(g);
}
-static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, gpointer userdata) {
+static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntryGroupState state, void* userdata) {
StaticServiceGroup *g = userdata;
- g_assert(s);
- g_assert(g);
+ assert(s);
+ assert(g);
if (state == AVAHI_ENTRY_GROUP_COLLISION) {
- gchar *n;
+ char *n;
remove_static_service_group_from_server(g);
n = avahi_alternative_service_name(g->chosen_name);
- g_free(g->chosen_name);
+ avahi_free(g->chosen_name);
g->chosen_name = n;
avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
@@ -191,20 +191,20 @@ static void entry_group_callback(AvahiServer *s, AvahiSEntryGroup *eg, AvahiEntr
static void add_static_service_group_to_server(StaticServiceGroup *g) {
StaticService *s;
- g_assert(g);
+ assert(g);
if (g->chosen_name)
- g_free(g->chosen_name);
+ avahi_free(g->chosen_name);
if (g->replace_wildcards)
g->chosen_name = replacestr(g->name, "%h", avahi_server_get_host_name(avahi_server));
else
- g->chosen_name = g_strdup(g->name);
+ g->chosen_name = avahi_strdup(g->name);
if (!g->entry_group)
g->entry_group = avahi_s_entry_group_new(avahi_server, entry_group_callback, g);
- g_assert(avahi_s_entry_group_is_empty(g->entry_group));
+ assert(avahi_s_entry_group_is_empty(g->entry_group));
for (s = g->services; s; s = s->services_next) {
@@ -227,7 +227,7 @@ static void add_static_service_group_to_server(StaticServiceGroup *g) {
}
static void remove_static_service_group_from_server(StaticServiceGroup *g) {
- g_assert(g);
+ assert(g);
if (g->entry_group)
avahi_s_entry_group_reset(g->entry_group);
@@ -249,14 +249,14 @@ struct xml_userdata {
StaticServiceGroup *group;
StaticService *service;
xml_tag_name current_tag;
- gboolean failed;
- gchar *buf;
+ int failed;
+ char *buf;
};
static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
struct xml_userdata *u = data;
- g_assert(u);
+ assert(u);
if (u->failed)
return;
@@ -286,7 +286,7 @@ static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
if (attr[0])
goto invalid_attr;
- g_assert(!u->service);
+ assert(!u->service);
u->service = static_service_new(u->group);
u->current_tag = XML_TAG_SERVICE;
@@ -317,20 +317,20 @@ static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
u->current_tag = XML_TAG_TXT_RECORD;
} else {
avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
- u->failed = TRUE;
+ u->failed = 1;
}
return;
invalid_attr:
avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
- u->failed = TRUE;
+ u->failed = 1;
return;
}
static void XMLCALL xml_end(void *data, const char *el) {
struct xml_userdata *u = data;
- g_assert(u);
+ assert(u);
if (u->failed)
return;
@@ -340,7 +340,7 @@ static void XMLCALL xml_end(void *data, const char *el) {
if (!u->group->name || !u->group->services) {
avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
- u->failed = TRUE;
+ u->failed = 1;
return;
}
@@ -351,7 +351,7 @@ static void XMLCALL xml_end(void *data, const char *el) {
if (!u->service->type) {
avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
- u->failed = TRUE;
+ u->failed = 1;
return;
}
@@ -365,24 +365,24 @@ static void XMLCALL xml_end(void *data, const char *el) {
case XML_TAG_PORT: {
int p;
- g_assert(u->service);
+ assert(u->service);
p = u->buf ? atoi(u->buf) : 0;
if (p < 0 || p > 0xFFFF) {
avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
- u->failed = TRUE;
+ u->failed = 1;
return;
}
- u->service->port = (guint16) p;
+ u->service->port = (uint16_t) p;
u->current_tag = XML_TAG_SERVICE;
break;
}
case XML_TAG_TXT_RECORD: {
- g_assert(u->service);
+ assert(u->service);
u->service->txt_records = avahi_string_list_add(u->service->txt_records, u->buf ? u->buf : "");
u->current_tag = XML_TAG_SERVICE;
@@ -399,22 +399,22 @@ static void XMLCALL xml_end(void *data, const char *el) {
;
}
- g_free(u->buf);
+ avahi_free(u->buf);
u->buf = NULL;
}
-static gchar *append_cdata(gchar *t, const gchar *n, int length) {
- gchar *r, *k;
+static char *append_cdata(char *t, const char *n, int length) {
+ char *r, *k;
if (!length)
return t;
- k = g_strndup(n, length);
+ k = avahi_strndup(n, length);
if (t) {
- r = g_strconcat(t, k, NULL);
- g_free(k);
- g_free(t);
+ r = avahi_strdup_printf("%s%s", t, k);
+ avahi_free(k);
+ avahi_free(t);
} else
r = k;
@@ -423,7 +423,7 @@ static gchar *append_cdata(gchar *t, const gchar *n, int length) {
static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
struct xml_userdata *u = data;
- g_assert(u);
+ assert(u);
if (u->failed)
return;
@@ -434,17 +434,17 @@ static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
break;
case XML_TAG_TYPE:
- g_assert(u->service);
+ assert(u->service);
u->service->type = append_cdata(u->service->type, s, len);
break;
case XML_TAG_DOMAIN_NAME:
- g_assert(u->service);
+ assert(u->service);
u->service->domain_name = append_cdata(u->service->domain_name, s, len);
break;
case XML_TAG_HOST_NAME:
- g_assert(u->service);
+ assert(u->service);
u->service->host_name = append_cdata(u->service->host_name, s, len);
break;
@@ -460,31 +460,31 @@ static void XMLCALL xml_cdata(void *data, const XML_Char *s, int len) {
}
}
-static gint static_service_group_load(StaticServiceGroup *g) {
+static int static_service_group_load(StaticServiceGroup *g) {
XML_Parser parser = NULL;
- gint fd = -1;
+ int fd = -1;
struct xml_userdata u;
- gint r = -1;
+ int r = -1;
struct stat st;
ssize_t n;
- g_assert(g);
+ assert(g);
u.buf = NULL;
u.group = g;
u.service = NULL;
u.current_tag = XML_TAG_INVALID;
- u.failed = FALSE;
+ u.failed = 0;
/* Cleanup old data in this service group, if available */
remove_static_service_group_from_server(g);
while (g->services)
static_service_free(g->services);
- g_free(g->name);
- g_free(g->chosen_name);
+ avahi_free(g->name);
+ avahi_free(g->chosen_name);
g->name = g->chosen_name = NULL;
- g->replace_wildcards = FALSE;
+ g->replace_wildcards = 0;
if (!(parser = XML_ParserCreate(NULL))) {
avahi_log_error("XML_ParserCreate() failed.");
@@ -541,14 +541,14 @@ finish:
if (parser)
XML_ParserFree(parser);
- g_free(u.buf);
+ avahi_free(u.buf);
return r;
}
-static void load_file(gchar *n) {
+static void load_file(char *n) {
StaticServiceGroup *g;
- g_assert(n);
+ assert(n);
for (g = groups; g; g = g->groups_next)
if (strcmp(g->filename, n) == 0)
@@ -566,7 +566,7 @@ static void load_file(gchar *n) {
void static_service_load(void) {
StaticServiceGroup *g, *n;
glob_t globbuf;
- gchar **p;
+ char **p;
for (g = groups; g; g = n) {
struct stat st;