summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2005-06-19 13:44:50 +0000
committerLennart Poettering <lennart@poettering.net>2005-06-19 13:44:50 +0000
commitad7b145ebe82c3a53632eaa6ec75b014d91a7bea (patch)
treef885f5732b6e6e1560c60b534ed5e5014c229e2a
parent64ab9be453f31b55e35ad5cad8102a9e45244a8c (diff)
* detect other running mDNS stacks
* replace more g_message()/g_warning() calls with avahi_log_xxx() * fix configuration file paths * only load configuration file when running daemon * require uid == 0 * fix static service reloading * add new command line option for reloading -r git-svn-id: file:///home/lennart/svn/public/avahi/trunk@129 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe
-rw-r--r--avahi-core/server.c6
-rw-r--r--avahi-core/socket.c67
-rw-r--r--avahi-daemon/Makefile.am18
-rw-r--r--avahi-daemon/main.c34
-rw-r--r--avahi-daemon/static-services.c45
5 files changed, 119 insertions, 51 deletions
diff --git a/avahi-core/server.c b/avahi-core/server.c
index d03a284..86f29fb 100644
--- a/avahi-core/server.c
+++ b/avahi-core/server.c
@@ -1268,14 +1268,14 @@ AvahiServer *avahi_server_new(GMainContext *c, const AvahiServerConfig *sc, Avah
}
if (s->fd_ipv4 < 0 && s->config.use_ipv4)
- avahi_log_debug("Failed to create IPv4 socket, proceeding in IPv6 only mode");
+ avahi_log_notice("Failed to create IPv4 socket, proceeding in IPv6 only mode");
else if (s->fd_ipv6 < 0 && s->config.use_ipv6)
- avahi_log_debug("Failed to create IPv6 socket, proceeding in IPv4 only mode");
+ avahi_log_notice("Failed to create IPv6 socket, proceeding in IPv4 only mode");
s->fd_legacy_unicast_ipv4 = s->fd_ipv4 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv4() : -1;
s->fd_legacy_unicast_ipv6 = s->fd_ipv6 >= 0 && s->config.enable_reflector ? avahi_open_legacy_unicast_socket_ipv6() : -1;
- g_main_context_ref(c->context = c ? c : g_main_context_default());
+ g_main_context_ref(s->context = (c ? c : g_main_context_default()));
/* Prepare IO source registration */
s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
diff --git a/avahi-core/socket.c b/avahi-core/socket.c
index 55db704..8f3d896 100644
--- a/avahi-core/socket.c
+++ b/avahi-core/socket.c
@@ -156,6 +156,50 @@ int avahi_mdns_mcast_leave_ipv6 (int index, int fd) {
return 0;
}
+static gint bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) {
+ gint yes;
+
+ g_assert(fd >= 0);
+ g_assert(sa);
+ g_assert(l > 0);
+
+ if (bind(fd, sa, l) < 0) {
+
+ if (errno != EADDRINUSE) {
+ avahi_log_warn("bind() failed: %s\n", strerror(errno));
+ return -1;
+ }
+
+ avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***",
+ sa->sa_family == AF_INET ? "IPv4" : "IPv6");
+
+ /* Try again, this time with SO_REUSEADDR set */
+ yes = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
+ avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
+ return -1;
+ }
+
+ if (bind(fd, sa, l) < 0) {
+ avahi_log_warn("bind() failed: %s\n", strerror(errno));
+ return -1;
+ }
+ } else {
+
+ /* We enable SO_REUSEADDR afterwards, to make sure that the
+ * user may run other mDNS implementations if he really
+ * wants. */
+
+ yes = 1;
+ if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
+ avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
gint avahi_open_socket_ipv4(void) {
struct sockaddr_in local;
int fd = -1, ttl, yes;
@@ -178,26 +222,17 @@ gint avahi_open_socket_ipv4(void) {
}
yes = 1;
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
- avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
- goto fail;
- }
-
- yes = 1;
if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) {
avahi_log_warn("IP_MULTICAST_LOOP failed: %s\n", strerror(errno));
goto fail;
}
-
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_port = htons(AVAHI_MDNS_PORT);
-
- if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
- avahi_log_warn("bind() failed: %s\n", strerror(errno));
+
+ if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0)
goto fail;
- }
yes = 1;
if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) {
@@ -254,12 +289,6 @@ gint avahi_open_socket_ipv6(void) {
}
yes = 1;
- if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) {
- avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno));
- goto fail;
- }
-
- yes = 1;
if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) {
avahi_log_warn("IPV6_V6ONLY failed: %s\n", strerror(errno));
goto fail;
@@ -275,10 +304,8 @@ gint avahi_open_socket_ipv6(void) {
local.sin6_family = AF_INET6;
local.sin6_port = htons(AVAHI_MDNS_PORT);
- if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) {
- avahi_log_warn("bind() failed: %s\n", strerror(errno));
+ if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0)
goto fail;
- }
yes = 1;
if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) {
diff --git a/avahi-daemon/Makefile.am b/avahi-daemon/Makefile.am
index bb983a3..5f561db 100644
--- a/avahi-daemon/Makefile.am
+++ b/avahi-daemon/Makefile.am
@@ -17,7 +17,14 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.
-AM_CFLAGS=-I$(top_srcdir) -D_GNU_SOURCE -DAVAHI_SERVICE_DIRECTORY=\"`pwd`\" -DAVAHI_CONFIG_FILE=\"avahi.conf\"
+pkgsysconfdir=$(sysconfdir)/avahi
+servicedir=$(pkgsysconfdir)/services
+
+AM_CFLAGS= \
+ -I$(top_srcdir) \
+ -D_GNU_SOURCE \
+ -DAVAHI_SERVICE_DIRECTORY=\"$(servicedir)\" \
+ -DAVAHI_CONFIG_FILE=\"$(pkgsysconfdir)/avahi.conf\"
AM_LDADD=-lexpat
# GLIB 2.0
@@ -46,6 +53,15 @@ avahi_SOURCES = \
simple-protocol.c simple-protocol.h \
static-services.c static-services.h
+pkgsysconf_DATA = \
+ avahi.conf
+
+service_DATA = \
+ example.service
+
+pkgdata_DATA = \
+ avahi-service.dtd
+
if ENABLE_DBUS
avahi_SOURCES += dbus-protocol.c dbus-protocol.h
endif
diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c
index 0d862ea..ec76114 100644
--- a/avahi-daemon/main.c
+++ b/avahi-daemon/main.c
@@ -28,6 +28,7 @@
#include <signal.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
#include <libdaemon/dfork.h>
#include <libdaemon/dsignal.h>
@@ -48,7 +49,8 @@ typedef enum {
DAEMON_RUN,
DAEMON_KILL,
DAEMON_VERSION,
- DAEMON_HELP
+ DAEMON_HELP,
+ DAEMON_RELOAD
} DaemonCommand;
typedef struct {
@@ -83,8 +85,10 @@ static void help(FILE *f, const gchar *argv0) {
" -h --help Show this help\n"
" -D --daemonize Daemonize after startup\n"
" -k --kill Kill a running daemon\n"
+ " -r --reload Request a running daemon to reload static services\n"
" -V --version Show version\n"
- " -f --file=FILE Load the specified configuration file instead of the default\n",
+ " -f --file=FILE Load the specified configuration file instead of\n"
+ " "AVAHI_CONFIG_FILE"\n",
argv0);
}
@@ -97,12 +101,13 @@ static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
{ "kill", no_argument, NULL, 'k' },
{ "version", no_argument, NULL, 'V' },
{ "file", required_argument, NULL, 'f' },
+ { "reload", no_argument, NULL, 'r' },
};
g_assert(config);
opterr = 0;
- while ((c = getopt_long(argc, argv, "hDkVf:", long_options, NULL)) >= 0) {
+ while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
switch(c) {
case 'h':
@@ -121,6 +126,9 @@ static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
g_free(config->config_file);
config->config_file = g_strdup(optarg);
break;
+ case 'r':
+ config->command = DAEMON_RELOAD;
+ break;
default:
fprintf(stderr, "Invalid command line argument: %c\n", c);
return -1;
@@ -314,6 +322,7 @@ static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpoi
case SIGHUP:
avahi_log_info("Got SIGHUP, reloading.");
static_service_load();
+ static_service_add_to_server();
break;
default:
@@ -425,9 +434,6 @@ int main(int argc, char *argv[]) {
if (parse_command_line(&config, argc, argv) < 0)
goto finish;
- if (load_config_file(&config) < 0)
- goto finish;
-
if (config.command == DAEMON_HELP) {
help(stdout, argv0);
r = 0;
@@ -442,14 +448,30 @@ int main(int argc, char *argv[]) {
r = 0;
+ } else if (config.command == DAEMON_RELOAD) {
+ if (daemon_pid_file_kill(SIGHUP) < 0) {
+ avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
+ goto finish;
+ }
+
+ r = 0;
+
} else if (config.command == DAEMON_RUN) {
pid_t pid;
+
+ if (getuid() != 0) {
+ avahi_log_error("This program is intended to be run as root.");
+ goto finish;
+ }
if ((pid = daemon_pid_file_is_running()) >= 0) {
avahi_log_error("Daemon already running on PID %u", pid);
goto finish;
}
+ if (load_config_file(&config) < 0)
+ goto finish;
+
if (config.daemonize) {
daemon_retval_init();
diff --git a/avahi-daemon/static-services.c b/avahi-daemon/static-services.c
index 8148de4..898edf3 100644
--- a/avahi-daemon/static-services.c
+++ b/avahi-daemon/static-services.c
@@ -35,6 +35,7 @@
#include <expat.h>
#include <avahi-core/llist.h>
+#include <avahi-core/log.h>
#include "main.h"
@@ -178,11 +179,11 @@ static void entry_group_callback(AvahiServer *s, AvahiEntryGroup *eg, AvahiEntry
g_free(g->chosen_name);
g->chosen_name = n;
- g_message("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
+ avahi_log_notice("Service name conflict for \"%s\" (%s), retrying with \"%s\".", g->name, g->filename, g->chosen_name);
add_static_service_group_to_server(g);
- } else
- g_message("Service \"%s\" (%s) successfully establised.", g->chosen_name, g->filename);
+ } else if (state == AVAHI_ENTRY_GROUP_ESTABLISHED)
+ avahi_log_info("Service \"%s\" (%s) successfully established.", g->chosen_name, g->filename);
}
static void add_static_service_group_to_server(StaticServiceGroup *g) {
@@ -212,7 +213,7 @@ static void add_static_service_group_to_server(StaticServiceGroup *g) {
s->type, g->chosen_name,
s->domain_name, s->host_name, s->port,
avahi_string_list_copy(s->txt_records)) < 0) {
- g_message("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename);
+ avahi_log_error("Failed to add service '%s' of type '%s', ignoring service group (%s)", g->chosen_name, s->type, g->filename);
remove_static_service_group_from_server(g);
return;
}
@@ -313,14 +314,14 @@ static void XMLCALL xml_start(void *data, const char *el, const char *attr[]) {
u->current_tag = XML_TAG_TXT_RECORD;
} else {
- g_message("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
+ avahi_log_error("%s: parse failure: didn't expect element <%s>.", u->group->filename, el);
u->failed = TRUE;
}
return;
invalid_attr:
- g_message("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
+ avahi_log_error("%s: parse failure: invalid attribute for element <%s>.", u->group->filename, el);
u->failed = TRUE;
return;
}
@@ -336,7 +337,7 @@ static void XMLCALL xml_end(void *data, const char *el) {
case XML_TAG_SERVICE_GROUP:
if (!u->group->name || !u->group->services) {
- g_message("%s: parse failure: service group incomplete.", u->group->filename);
+ avahi_log_error("%s: parse failure: service group incomplete.", u->group->filename);
u->failed = TRUE;
return;
}
@@ -347,7 +348,7 @@ static void XMLCALL xml_end(void *data, const char *el) {
case XML_TAG_SERVICE:
if (u->service->port == 0 || !u->service->type) {
- g_message("%s: parse failure: service incomplete.", u->group->filename);
+ avahi_log_error("%s: parse failure: service incomplete.", u->group->filename);
u->failed = TRUE;
return;
}
@@ -367,7 +368,7 @@ static void XMLCALL xml_end(void *data, const char *el) {
p = u->buf ? atoi(u->buf) : 0;
if (p <= 0 || p > 0xFFFF) {
- g_message("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
+ avahi_log_error("%s: parse failure: invalid port specification \"%s\".", u->group->filename, u->buf);
u->failed = TRUE;
return;
}
@@ -484,17 +485,17 @@ static gint static_service_group_load(StaticServiceGroup *g) {
g->replace_wildcards = FALSE;
if (!(parser = XML_ParserCreate(NULL))) {
- g_warning("XML_ParserCreate() failed.");
+ avahi_log_error("XML_ParserCreate() failed.");
goto finish;
}
if ((fd = open(g->filename, O_RDONLY)) < 0) {
- g_warning("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
+ avahi_log_error("open(\"%s\", O_RDONLY): %s", g->filename, strerror(errno));
goto finish;
}
if (fstat(fd, &st) < 0) {
- g_warning("fstat(): %s", strerror(errno));
+ avahi_log_error("fstat(): %s", strerror(errno));
goto finish;
}
@@ -511,17 +512,17 @@ static gint static_service_group_load(StaticServiceGroup *g) {
#define BUFSIZE (10*1024)
if (!(buffer = XML_GetBuffer(parser, BUFSIZE))) {
- g_warning("XML_GetBuffer() failed.");
+ avahi_log_error("XML_GetBuffer() failed.");
goto finish;
}
if ((n = read(fd, buffer, BUFSIZE)) < 0) {
- g_warning("read(): %s\n", strerror(errno));
+ avahi_log_error("read(): %s\n", strerror(errno));
goto finish;
}
if (!XML_ParseBuffer(parser, n, n == 0)) {
- g_warning("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
+ avahi_log_error("XML_ParseBuffer() failed at line %d: %s.\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
goto finish;
}
@@ -551,9 +552,11 @@ static void load_file(gchar *n) {
if (strcmp(g->filename, n) == 0)
return;
+ avahi_log_info("Loading service file %s", n);
+
g = static_service_group_new(n);
if (static_service_group_load(g) < 0) {
- g_warning("Failed to load service group file %s, ignoring.", g->filename);
+ avahi_log_error("Failed to load service group file %s, ignoring.", g->filename);
static_service_group_free(g);
}
}
@@ -571,16 +574,16 @@ void static_service_load(void) {
if (stat(g->filename, &st) < 0) {
if (errno == ENOENT)
- g_message("Service group file %s vanished, removing seervices.", g->filename);
+ avahi_log_info("Service group file %s vanished, removing services.", g->filename);
else
- g_warning("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
+ avahi_log_warn("Failed to stat() file %s, ignoring: %s", g->filename, strerror(errno));
static_service_group_free(g);
} else if (st.st_mtime != g->mtime) {
- g_message("Service group file %s changed, reloading.", g->filename);
+ avahi_log_info("Service group file %s changed, reloading.", g->filename);
if (static_service_group_load(g) < 0) {
- g_warning("Failed to load service group file %s, removing service.", g->filename);
+ avahi_log_warn("Failed to load service group file %s, removing service.", g->filename);
static_service_group_free(g);
}
}
@@ -588,7 +591,7 @@ void static_service_load(void) {
memset(&globbuf, 0, sizeof(globbuf));
if (glob(AVAHI_SERVICE_DIRECTORY "/*.service", GLOB_ERR, NULL, &globbuf) != 0)
- g_warning("glob() failed.\n");
+ avahi_log_error("glob() failed.\n");
else {
for (p = globbuf.gl_pathv; *p; p++)
load_file(*p);