4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
40 #include <libdaemon/dfork.h>
41 #include <libdaemon/dsignal.h>
42 #include <libdaemon/dlog.h>
43 #include <libdaemon/dpid.h>
45 #include <avahi-core/core.h>
46 #include <avahi-core/log.h>
47 #include <avahi-glib/glib-malloc.h>
48 #include <avahi-glib/glib-watch.h>
51 #include "simple-protocol.h"
52 #include "dbus-protocol.h"
53 #include "static-services.h"
55 AvahiServer *avahi_server = NULL;
67 AvahiServerConfig server_config;
68 DaemonCommand command;
73 gboolean publish_resolv_conf;
74 gchar ** publish_dns_servers;
77 #define RESOLV_CONF "/etc/resolv.conf"
79 static AvahiEntryGroup *dns_servers_entry_group = NULL;
80 static AvahiEntryGroup *resolv_conf_entry_group = NULL;
82 static gchar **resolv_conf = NULL;
84 static DaemonConfig config;
86 #define MAX_NAME_SERVERS 10
88 static gint load_resolv_conf(const DaemonConfig *c) {
93 g_strfreev(resolv_conf);
96 if (!c->publish_resolv_conf)
99 if (!(f = fopen(RESOLV_CONF, "r"))) {
100 avahi_log_warn("Failed to open "RESOLV_CONF".");
104 resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
106 while (!feof(f) && i < MAX_NAME_SERVERS) {
110 if (!(fgets(ln, sizeof(ln), f)))
113 ln[strcspn(ln, "\r\n#")] = 0;
114 p = ln + strspn(ln, "\t ");
116 if (g_str_has_prefix(p, "nameserver")) {
118 p += strspn(p, "\t ");
119 p[strcspn(p, "\t ")] = 0;
120 resolv_conf[i++] = strdup(p);
129 g_strfreev(resolv_conf);
139 static AvahiEntryGroup* add_dns_servers(AvahiServer *s, AvahiEntryGroup* g, gchar **l) {
146 g = avahi_entry_group_new(s, NULL, NULL);
148 g_assert(avahi_entry_group_is_empty(g));
150 for (p = l; *p; p++) {
153 if (!avahi_address_parse(*p, AF_UNSPEC, &a))
154 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
156 if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
157 avahi_entry_group_free(g);
158 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
163 avahi_entry_group_commit(g);
168 static void remove_dns_server_entry_groups(void) {
170 if (resolv_conf_entry_group)
171 avahi_entry_group_reset(resolv_conf_entry_group);
173 if (dns_servers_entry_group)
174 avahi_entry_group_reset(dns_servers_entry_group);
177 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
178 DaemonConfig *c = userdata;
185 dbus_protocol_server_state_changed(state);
188 if (state == AVAHI_SERVER_RUNNING) {
189 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
190 static_service_add_to_server();
192 remove_dns_server_entry_groups();
194 if (resolv_conf && resolv_conf[0])
195 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
197 if (c->publish_dns_servers && c->publish_dns_servers[0])
198 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
200 simple_protocol_restart_queries();
202 } else if (state == AVAHI_SERVER_COLLISION) {
205 static_service_remove_from_server();
207 remove_dns_server_entry_groups();
209 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
210 avahi_log_warn("Host name conflict, retrying with <%s>", n);
211 avahi_server_set_host_name(s, n);
216 static void help(FILE *f, const gchar *argv0) {
219 " -h --help Show this help\n"
220 " -D --daemonize Daemonize after startup\n"
221 " -k --kill Kill a running daemon\n"
222 " -r --reload Request a running daemon to reload static services\n"
223 " -c --check Return 0 if a daemon is already running\n"
224 " -V --version Show version\n"
225 " -f --file=FILE Load the specified configuration file instead of\n"
226 " "AVAHI_CONFIG_FILE"\n",
230 static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
233 static const struct option const long_options[] = {
234 { "help", no_argument, NULL, 'h' },
235 { "daemonize", no_argument, NULL, 'D' },
236 { "kill", no_argument, NULL, 'k' },
237 { "version", no_argument, NULL, 'V' },
238 { "file", required_argument, NULL, 'f' },
239 { "reload", no_argument, NULL, 'r' },
240 { "check", no_argument, NULL, 'c' },
246 while ((o = getopt_long(argc, argv, "hDkVf:rc", long_options, NULL)) >= 0) {
250 c->command = DAEMON_HELP;
256 c->command = DAEMON_KILL;
259 c->command = DAEMON_VERSION;
262 g_free(c->config_file);
263 c->config_file = g_strdup(optarg);
266 c->command = DAEMON_RELOAD;
269 c->command = DAEMON_CHECK;
272 fprintf(stderr, "Invalid command line argument: %c\n", o);
278 fprintf(stderr, "Too many arguments\n");
285 static gboolean is_yes(const gchar *s) {
288 return *s == 'y' || *s == 'Y';
291 static gint load_config_file(DaemonConfig *c) {
295 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
299 f = g_key_file_new();
300 g_key_file_set_list_separator(f, ',');
302 if (!g_key_file_load_from_file(f, c->config_file ? c->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
303 fprintf(stderr, "Unable to read config file: %s\n", err->message);
307 groups = g_key_file_get_groups(f, NULL);
309 for (g = groups; *g; g++) {
310 if (g_strcasecmp(*g, "server") == 0) {
313 keys = g_key_file_get_keys(f, *g, NULL, NULL);
315 for (k = keys; *k; k++) {
317 v = g_key_file_get_value(f, *g, *k, NULL);
319 if (g_strcasecmp(*k, "host-name") == 0) {
320 g_free(c->server_config.host_name);
321 c->server_config.host_name = v;
323 } else if (g_strcasecmp(*k, "domain-name") == 0) {
324 g_free(c->server_config.domain_name);
325 c->server_config.domain_name = v;
327 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
328 c->server_config.use_ipv4 = is_yes(v);
329 else if (g_strcasecmp(*k, "use-ipv6") == 0)
330 c->server_config.use_ipv6 = is_yes(v);
331 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
332 c->server_config.check_response_ttl = is_yes(v);
333 else if (g_strcasecmp(*k, "use-iff-running") == 0)
334 c->server_config.use_iff_running = is_yes(v);
335 else if (g_strcasecmp(*k, "enable-dbus") == 0)
336 c->enable_dbus = is_yes(v);
337 else if (g_strcasecmp(*k, "drop-root") == 0)
338 c->drop_root = is_yes(v);
340 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
351 } else if (g_strcasecmp(*g, "publish") == 0) {
354 keys = g_key_file_get_keys(f, *g, NULL, NULL);
356 for (k = keys; *k; k++) {
358 v = g_key_file_get_string(f, *g, *k, NULL);
360 if (g_strcasecmp(*k, "publish-addresses") == 0)
361 c->server_config.publish_addresses = is_yes(v);
362 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
363 c->server_config.publish_hinfo = is_yes(v);
364 else if (g_strcasecmp(*k, "publish-workstation") == 0)
365 c->server_config.publish_workstation = is_yes(v);
366 else if (g_strcasecmp(*k, "publish-domain") == 0)
367 c->server_config.publish_domain = is_yes(v);
368 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
369 c->publish_resolv_conf = is_yes(v);
370 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
371 g_strfreev(c->publish_dns_servers);
372 c->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
374 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
385 } else if (g_strcasecmp(*g, "reflector") == 0) {
388 keys = g_key_file_get_keys(f, *g, NULL, NULL);
390 for (k = keys; *k; k++) {
392 v = g_key_file_get_string(f, *g, *k, NULL);
394 if (g_strcasecmp(*k, "enable-reflector") == 0)
395 c->server_config.enable_reflector = is_yes(v);
396 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
397 c->server_config.reflect_ipv = is_yes(v);
399 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
411 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
433 static void log_function(AvahiLogLevel level, const gchar *txt) {
435 static const int const log_level_map[] = {
443 g_assert(level < AVAHI_LOG_LEVEL_MAX);
446 daemon_log(log_level_map[level], "%s", txt);
449 static void dump(const gchar *text, gpointer userdata) {
450 avahi_log_info("%s", text);
453 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
455 GMainLoop *loop = data;
460 if ((sig = daemon_signal_next()) <= 0) {
461 avahi_log_error("daemon_signal_next() failed");
471 sig == SIGINT ? "SIGINT" :
472 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
473 g_main_loop_quit(loop);
477 avahi_log_info("Got SIGHUP, reloading.");
478 static_service_load();
479 static_service_add_to_server();
481 if (resolv_conf_entry_group)
482 avahi_entry_group_reset(resolv_conf_entry_group);
484 load_resolv_conf(&config);
486 if (resolv_conf && resolv_conf[0])
487 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
492 avahi_log_info("Got SIGUSR1, dumping record data.");
493 avahi_server_dump(avahi_server, dump, NULL);
497 avahi_log_warn("Got spurious signal, ignoring.");
504 static gint run_server(DaemonConfig *c) {
505 GMainLoop *loop = NULL;
507 GIOChannel *io = NULL;
508 guint watch_id = (guint) -1;
510 AvahiGLibPoll *poll_api;
514 poll_api = avahi_glib_poll_new(NULL);
515 loop = g_main_loop_new(NULL, FALSE);
517 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
518 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
522 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
523 avahi_log_error( "Failed to create signal io channel.");
527 g_io_channel_set_close_on_unref(io, FALSE);
528 watch_id = g_io_add_watch(io, G_IO_IN, signal_callback, loop);
530 if (simple_protocol_setup(NULL) < 0)
535 if (dbus_protocol_setup(loop) < 0)
539 if (!(avahi_server = avahi_server_new(avahi_glib_poll_get(poll_api), &c->server_config, server_callback, c, &error))) {
540 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
546 static_service_load();
549 daemon_retval_send(0);
553 g_main_loop_run(loop);
557 static_service_remove_from_server();
558 static_service_free_all();
559 remove_dns_server_entry_groups();
561 simple_protocol_shutdown();
565 dbus_protocol_shutdown();
569 avahi_server_free(avahi_server);
571 daemon_signal_done();
573 if (watch_id != (guint) -1)
574 g_source_remove(watch_id);
577 g_io_channel_unref(io);
580 avahi_glib_poll_free(poll_api);
583 g_main_loop_unref(loop);
585 if (r != 0 && c->daemonize)
586 daemon_retval_send(1);
591 static gint drop_root(void) {
596 if (!(pw = getpwnam(AVAHI_USER))) {
597 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
601 if (!(gr = getgrnam(AVAHI_GROUP))) {
602 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
606 avahi_log_info("Found user '"AVAHI_USER"' (UID %lu) and group '"AVAHI_GROUP"' (GID %lu).", (unsigned long) pw->pw_uid, (unsigned long) gr->gr_gid);
608 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
609 avahi_log_error("Failed to change group list: %s", strerror(errno));
613 #if defined(HAVE_SETRESGID)
614 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
615 #elif defined(HAVE_SETREGID)
616 r = setregid(gr->gr_gid, gr->gr_gid);
618 if ((r = setgid(gr->gr_gid)) >= 0)
619 r = setegid(gr->gr_gid);
623 avahi_log_error("Failed to change GID: %s", strerror(errno));
627 #if defined(HAVE_SETRESUID)
628 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
629 #elif defined(HAVE_SETREUID)
630 r = setreuid(pw->pw_uid, pw->pw_uid);
632 if ((r = setuid(pw->pw_uid)) >= 0)
633 r = seteuid(pw->pw_uid);
637 avahi_log_error("Failed to change UID: %s", strerror(errno));
641 g_setenv("USER", pw->pw_name, 1);
642 g_setenv("LOGNAME", pw->pw_name, 1);
643 g_setenv("HOME", pw->pw_dir, 1);
645 avahi_log_info("Successfully dropped root privileges.");
650 static const char* pid_file_proc(void) {
651 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
654 static gint make_runtime_dir(void) {
657 gboolean reset_umask = FALSE;
662 if (!(pw = getpwnam(AVAHI_USER))) {
663 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
667 if (!(gr = getgrnam(AVAHI_GROUP))) {
668 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
675 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
676 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
680 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
682 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
683 avahi_log_error("stat(): %s\n", strerror(errno));
687 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
688 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
701 #define RANDOM_DEVICE "/dev/urandom"
703 static void init_rand_seed(void) {
707 /* Try to initialize seed from /dev/urandom, to make it a little
708 * less predictable, and to make sure that multiple machines
709 * booted at the same time choose different random seeds. */
710 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
711 read(fd, &seed, sizeof(seed));
715 /* If the initialization failed by some reason, we add the time to the seed*/
716 seed |= (unsigned) time(NULL);
721 int main(int argc, char *argv[]) {
724 gboolean wrote_pid_file = FALSE;
726 avahi_set_log_function(log_function);
727 avahi_set_allocator(avahi_glib_allocator());
731 avahi_server_config_init(&config.server_config);
732 config.command = DAEMON_RUN;
733 config.daemonize = FALSE;
734 config.config_file = NULL;
735 config.enable_dbus = TRUE;
736 config.drop_root = TRUE;
737 config.publish_dns_servers = NULL;
738 config.publish_resolv_conf = FALSE;
740 if ((argv0 = strrchr(argv[0], '/')))
745 daemon_pid_file_ident = (const char *) argv0;
746 daemon_log_ident = (char*) argv0;
747 daemon_pid_file_proc = pid_file_proc;
749 if (parse_command_line(&config, argc, argv) < 0)
752 if (config.command == DAEMON_HELP) {
755 } else if (config.command == DAEMON_VERSION) {
756 printf("%s "PACKAGE_VERSION"\n", argv0);
758 } else if (config.command == DAEMON_KILL) {
759 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
760 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
766 } else if (config.command == DAEMON_RELOAD) {
767 if (daemon_pid_file_kill(SIGHUP) < 0) {
768 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
774 } else if (config.command == DAEMON_CHECK)
775 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
776 else if (config.command == DAEMON_RUN) {
780 avahi_log_error("This program is intended to be run as root.");
784 if ((pid = daemon_pid_file_is_running()) >= 0) {
785 avahi_log_error("Daemon already running on PID %u", pid);
789 if (load_config_file(&config) < 0)
792 if (config.daemonize) {
793 daemon_retval_init();
795 if ((pid = daemon_fork()) < 0)
801 if ((ret = daemon_retval_wait(20)) < 0) {
802 avahi_log_error("Could not recieve return value from daemon process.");
814 printf("%s "PACKAGE_VERSION" starting up.\n", argv0);
818 if (make_runtime_dir() < 0)
821 if (config.drop_root) {
826 if (daemon_pid_file_create() < 0) {
827 avahi_log_error("Failed to create PID file: %s", strerror(errno));
829 if (config.daemonize)
830 daemon_retval_send(1);
833 wrote_pid_file = TRUE;
835 if (run_server(&config) == 0)
841 if (config.daemonize)
842 daemon_retval_done();
844 avahi_server_config_free(&config.server_config);
845 g_free(config.config_file);
846 g_strfreev(config.publish_dns_servers);
847 g_strfreev(resolv_conf);
850 daemon_pid_file_remove();