}
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*));
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;
goto fail;
}
- 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) {
goto fail;
}
- 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));
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) {
# 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
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
#include <signal.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
#include <libdaemon/dfork.h>
#include <libdaemon/dsignal.h>
DAEMON_RUN,
DAEMON_KILL,
DAEMON_VERSION,
- DAEMON_HELP
+ DAEMON_HELP,
+ DAEMON_RELOAD
} DaemonCommand;
typedef struct {
" -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);
}
{ "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':
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;
case SIGHUP:
avahi_log_info("Got SIGHUP, reloading.");
static_service_load();
+ static_service_add_to_server();
break;
default:
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;
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();
#include <expat.h>
#include <avahi-core/llist.h>
+#include <avahi-core/log.h>
#include "main.h"
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) {
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
#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;
}
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);
}
}
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);
}
}
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);