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
36 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
46 #include <sys/inotify.h>
50 #include <sys/types.h>
51 #include <sys/event.h>
55 #include <libdaemon/dfork.h>
56 #include <libdaemon/dsignal.h>
57 #include <libdaemon/dlog.h>
58 #include <libdaemon/dpid.h>
60 #include <avahi-common/malloc.h>
61 #include <avahi-common/simple-watch.h>
62 #include <avahi-common/error.h>
63 #include <avahi-common/alternative.h>
64 #include <avahi-common/domain.h>
66 #include <avahi-core/core.h>
67 #include <avahi-core/publish.h>
68 #include <avahi-core/dns-srv-rr.h>
69 #include <avahi-core/log.h>
76 #include "setproctitle.h"
78 #include "simple-protocol.h"
79 #include "static-services.h"
80 #include "static-hosts.h"
81 #include "ini-file-parser.h"
84 #include "dbus-protocol.h"
87 AvahiServer *avahi_server = NULL;
88 AvahiSimplePoll *simple_poll_api = NULL;
89 static char *argv0 = NULL;
102 AvahiServerConfig server_config;
103 DaemonCommand command;
109 int fail_on_missing_dbus;
116 int modify_proc_title;
118 int disable_user_service_publishing;
119 int publish_resolv_conf;
120 char ** publish_dns_servers;
123 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
124 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
127 int rlimit_nproc_set;
132 #define RESOLV_CONF "/etc/resolv.conf"
133 #define BROWSE_DOMAINS_MAX 16
135 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
136 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
138 static char **resolv_conf_name_servers = NULL;
139 static char **resolv_conf_search_domains = NULL;
141 static DaemonConfig config;
143 static int has_prefix(const char *s, const char *prefix) {
148 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
151 static int load_resolv_conf(void) {
156 avahi_strfreev(resolv_conf_name_servers);
157 resolv_conf_name_servers = NULL;
159 avahi_strfreev(resolv_conf_search_domains);
160 resolv_conf_search_domains = NULL;
163 f = avahi_chroot_helper_get_file(RESOLV_CONF);
165 f = fopen(RESOLV_CONF, "r");
169 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
173 resolv_conf_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
174 resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);
180 if (!(fgets(ln, sizeof(ln), f)))
183 ln[strcspn(ln, "\r\n#")] = 0;
184 p = ln + strspn(ln, "\t ");
186 if ((has_prefix(p, "nameserver ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
188 p += strspn(p, "\t ");
189 p[strcspn(p, "\t ")] = 0;
190 resolv_conf_name_servers[i++] = avahi_strdup(p);
193 if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
194 has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {
198 while (j < BROWSE_DOMAINS_MAX) {
201 p += strspn(p, "\t ");
202 k = strcspn(p, "\t ");
205 resolv_conf_search_domains[j++] = avahi_strndup(p, k);
220 avahi_strfreev(resolv_conf_name_servers);
221 resolv_conf_name_servers = NULL;
223 avahi_strfreev(resolv_conf_search_domains);
224 resolv_conf_search_domains = NULL;
233 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
240 g = avahi_s_entry_group_new(s, NULL, NULL);
242 assert(avahi_s_entry_group_is_empty(g));
244 for (p = l; *p; p++) {
247 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
248 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
250 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
251 avahi_s_entry_group_free(g);
252 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
257 avahi_s_entry_group_commit(g);
262 static void remove_dns_server_entry_groups(void) {
264 if (resolv_conf_entry_group)
265 avahi_s_entry_group_reset(resolv_conf_entry_group);
267 if (dns_servers_entry_group)
268 avahi_s_entry_group_reset(dns_servers_entry_group);
271 static void update_wide_area_servers(void) {
272 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
276 if (!resolv_conf_name_servers) {
277 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
281 for (p = resolv_conf_name_servers; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
282 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
283 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
288 avahi_server_set_wide_area_servers(avahi_server, a, n);
291 static AvahiStringList *filter_duplicate_domains(AvahiStringList *l) {
292 AvahiStringList *e, *n, *p;
297 for (p = l, e = l->next; e; e = n) {
300 if (avahi_domain_equal((char*) e->text, (char*) l->text)) {
307 l->next = filter_duplicate_domains(l->next);
311 static void update_browse_domains(void) {
316 if (!resolv_conf_search_domains) {
317 avahi_server_set_browse_domains(avahi_server, NULL);
321 l = avahi_string_list_copy(config.server_config.browse_domains);
323 for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
324 if (!avahi_is_valid_domain_name(*p))
325 avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
327 l = avahi_string_list_add(l, *p);
330 l = filter_duplicate_domains(l);
332 avahi_server_set_browse_domains(avahi_server, l);
335 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
336 DaemonConfig *c = userdata;
341 /* This function is possibly called before the global variable
342 * avahi_server has been set, therefore we do it explicitly */
347 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
348 dbus_protocol_server_state_changed(state);
352 case AVAHI_SERVER_RUNNING:
353 avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
355 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
357 static_service_add_to_server();
358 static_hosts_add_to_server();
360 remove_dns_server_entry_groups();
362 if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
363 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
365 if (c->publish_dns_servers && c->publish_dns_servers[0])
366 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
368 simple_protocol_restart_queries();
371 case AVAHI_SERVER_COLLISION: {
374 avahi_set_proc_title(argv0, "%s: collision", argv0);
376 static_service_remove_from_server();
377 static_hosts_remove_from_server();
378 remove_dns_server_entry_groups();
380 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
381 avahi_log_warn("Host name conflict, retrying with <%s>", n);
382 avahi_server_set_host_name(s, n);
388 case AVAHI_SERVER_FAILURE:
390 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
391 avahi_simple_poll_quit(simple_poll_api);
394 case AVAHI_SERVER_REGISTERING:
396 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
398 static_service_remove_from_server();
399 static_hosts_remove_from_server();
400 remove_dns_server_entry_groups();
404 case AVAHI_SERVER_INVALID:
410 static void help(FILE *f) {
413 " -h --help Show this help\n"
414 " -D --daemonize Daemonize after startup (implies -s)\n"
415 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
416 " -k --kill Kill a running daemon\n"
417 " -r --reload Request a running daemon to reload static services\n"
418 " -c --check Return 0 if a daemon is already running\n"
419 " -V --version Show version\n"
420 " -f --file=FILE Load the specified configuration file instead of\n"
421 " "AVAHI_CONFIG_FILE"\n"
422 " --no-rlimits Don't enforce resource limits\n"
423 " --no-drop-root Don't drop privileges\n"
425 " --no-chroot Don't chroot()\n"
427 " --no-proc-title Don't modify process title\n"
428 " --debug Increase verbosity\n",
433 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
437 OPTION_NO_RLIMITS = 256,
442 OPTION_NO_PROC_TITLE,
446 static const struct option long_options[] = {
447 { "help", no_argument, NULL, 'h' },
448 { "daemonize", no_argument, NULL, 'D' },
449 { "kill", no_argument, NULL, 'k' },
450 { "version", no_argument, NULL, 'V' },
451 { "file", required_argument, NULL, 'f' },
452 { "reload", no_argument, NULL, 'r' },
453 { "check", no_argument, NULL, 'c' },
454 { "syslog", no_argument, NULL, 's' },
455 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
456 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
458 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
460 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
461 { "debug", no_argument, NULL, OPTION_DEBUG },
467 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
474 c->command = DAEMON_HELP;
480 c->command = DAEMON_KILL;
483 c->command = DAEMON_VERSION;
486 avahi_free(c->config_file);
487 c->config_file = avahi_strdup(optarg);
490 c->command = DAEMON_RELOAD;
493 c->command = DAEMON_CHECK;
495 case OPTION_NO_RLIMITS:
498 case OPTION_NO_DROP_ROOT:
502 case OPTION_NO_CHROOT:
506 case OPTION_NO_PROC_TITLE:
507 c->modify_proc_title = 0;
518 fprintf(stderr, "Too many arguments\n");
525 static int is_yes(const char *s) {
528 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
531 static int load_config_file(DaemonConfig *c) {
534 AvahiIniFileGroup *g;
538 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
541 for (g = f->groups; g; g = g->groups_next) {
543 if (strcasecmp(g->name, "server") == 0) {
546 for (p = g->pairs; p; p = p->pairs_next) {
548 if (strcasecmp(p->key, "host-name") == 0) {
549 avahi_free(c->server_config.host_name);
550 c->server_config.host_name = avahi_strdup(p->value);
551 } else if (strcasecmp(p->key, "domain-name") == 0) {
552 avahi_free(c->server_config.domain_name);
553 c->server_config.domain_name = avahi_strdup(p->value);
554 } else if (strcasecmp(p->key, "browse-domains") == 0) {
557 e = avahi_split_csv(p->value);
559 for (t = e; *t; t++) {
560 char cleaned[AVAHI_DOMAIN_NAME_MAX];
562 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
563 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
568 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
573 c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
574 } else if (strcasecmp(p->key, "use-ipv4") == 0)
575 c->server_config.use_ipv4 = is_yes(p->value);
576 else if (strcasecmp(p->key, "use-ipv6") == 0)
577 c->server_config.use_ipv6 = is_yes(p->value);
578 else if (strcasecmp(p->key, "check-response-ttl") == 0)
579 c->server_config.check_response_ttl = is_yes(p->value);
580 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
581 c->server_config.allow_point_to_point = is_yes(p->value);
582 else if (strcasecmp(p->key, "use-iff-running") == 0)
583 c->server_config.use_iff_running = is_yes(p->value);
584 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
585 c->server_config.disallow_other_stacks = is_yes(p->value);
587 else if (strcasecmp(p->key, "enable-dbus") == 0) {
589 if (*(p->value) == 'w' || *(p->value) == 'W') {
590 c->fail_on_missing_dbus = 0;
592 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
593 c->fail_on_missing_dbus = 1;
600 else if (strcasecmp(p->key, "allow-interfaces") == 0) {
603 avahi_string_list_free(c->server_config.allow_interfaces);
604 c->server_config.allow_interfaces = NULL;
605 e = avahi_split_csv(p->value);
608 c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);
611 } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
614 avahi_string_list_free(c->server_config.deny_interfaces);
615 c->server_config.deny_interfaces = NULL;
616 e = avahi_split_csv(p->value);
619 c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);
623 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
628 } else if (strcasecmp(g->name, "publish") == 0) {
631 for (p = g->pairs; p; p = p->pairs_next) {
633 if (strcasecmp(p->key, "publish-addresses") == 0)
634 c->server_config.publish_addresses = is_yes(p->value);
635 else if (strcasecmp(p->key, "publish-hinfo") == 0)
636 c->server_config.publish_hinfo = is_yes(p->value);
637 else if (strcasecmp(p->key, "publish-workstation") == 0)
638 c->server_config.publish_workstation = is_yes(p->value);
639 else if (strcasecmp(p->key, "publish-domain") == 0)
640 c->server_config.publish_domain = is_yes(p->value);
641 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
642 c->publish_resolv_conf = is_yes(p->value);
643 else if (strcasecmp(p->key, "disable-publishing") == 0)
644 c->server_config.disable_publishing = is_yes(p->value);
645 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
646 c->disable_user_service_publishing = is_yes(p->value);
647 else if (strcasecmp(p->key, "add-service-cookie") == 0)
648 c->server_config.add_service_cookie = is_yes(p->value);
649 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
650 avahi_strfreev(c->publish_dns_servers);
651 c->publish_dns_servers = avahi_split_csv(p->value);
652 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
653 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
654 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
655 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
657 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
662 } else if (strcasecmp(g->name, "wide-area") == 0) {
665 for (p = g->pairs; p; p = p->pairs_next) {
667 if (strcasecmp(p->key, "enable-wide-area") == 0)
668 c->server_config.enable_wide_area = is_yes(p->value);
670 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
675 } else if (strcasecmp(g->name, "reflector") == 0) {
678 for (p = g->pairs; p; p = p->pairs_next) {
680 if (strcasecmp(p->key, "enable-reflector") == 0)
681 c->server_config.enable_reflector = is_yes(p->value);
682 else if (strcasecmp(p->key, "reflect-ipv") == 0)
683 c->server_config.reflect_ipv = is_yes(p->value);
685 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
690 } else if (strcasecmp(g->name, "rlimits") == 0) {
693 for (p = g->pairs; p; p = p->pairs_next) {
695 if (strcasecmp(p->key, "rlimit-as") == 0) {
696 c->rlimit_as_set = 1;
697 c->rlimit_as = atoi(p->value);
698 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
699 c->rlimit_core_set = 1;
700 c->rlimit_core = atoi(p->value);
701 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
702 c->rlimit_data_set = 1;
703 c->rlimit_data = atoi(p->value);
704 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
705 c->rlimit_fsize_set = 1;
706 c->rlimit_fsize = atoi(p->value);
707 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
708 c->rlimit_nofile_set = 1;
709 c->rlimit_nofile = atoi(p->value);
710 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
711 c->rlimit_stack_set = 1;
712 c->rlimit_stack = atoi(p->value);
713 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
715 c->rlimit_nproc_set = 1;
716 c->rlimit_nproc = atoi(p->value);
718 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
721 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
728 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
738 avahi_ini_file_free(f);
743 static void log_function(AvahiLogLevel level, const char *txt) {
745 static const int log_level_map[] = {
753 assert(level < AVAHI_LOG_LEVEL_MAX);
756 if (!config.debug && level == AVAHI_LOG_DEBUG)
759 daemon_log(log_level_map[level], "%s", txt);
762 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
763 avahi_log_info("%s", text);
768 static int inotify_fd = -1;
770 static void add_inotify_watches(void) {
772 /* We ignore the return values, because one or more of these files
773 * might not exist and we're OK with that. In addition we never
774 * want to remove these watches, hence we keep their ids? */
777 c = config.use_chroot;
780 inotify_add_watch(inotify_fd, c ? "/services" : AVAHI_SERVICE_DIR, IN_CLOSE_WRITE|IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM|IN_MOVED_TO|IN_MOVE_SELF
785 inotify_add_watch(inotify_fd, c ? "/" : AVAHI_CONFIG_DIR, IN_CLOSE_WRITE|IN_DELETE|IN_DELETE_SELF|IN_MOVED_FROM|IN_MOVED_TO|IN_MOVE_SELF
796 #define NUM_WATCHES 2
799 static int kfds[NUM_WATCHES];
800 static int num_kfds = 0;
802 static void add_kqueue_watch(const char *dir);
804 static void add_kqueue_watches(void) {
808 c = config.use_chroot;
811 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
812 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
815 static void add_kqueue_watch(const char *dir) {
822 if (num_kfds >= NUM_WATCHES)
825 fd = open(dir, O_RDONLY);
828 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
829 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
831 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
836 kfds[num_kfds++] = fd;
841 static void reload_config(void) {
844 /* Refresh in case the config dirs have been removed */
845 add_inotify_watches();
849 add_kqueue_watches();
853 static_service_load(config.use_chroot);
854 static_hosts_load(config.use_chroot);
856 static_service_load(0);
857 static_hosts_load(0);
859 static_service_add_to_server();
860 static_hosts_add_to_server();
862 if (resolv_conf_entry_group)
863 avahi_s_entry_group_reset(resolv_conf_entry_group);
867 update_wide_area_servers();
868 update_browse_domains();
870 if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
871 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
876 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
880 assert(fd == inotify_fd);
883 ioctl(inotify_fd, FIONREAD, &n);
887 buffer = avahi_malloc(n);
888 if (read(inotify_fd, buffer, n) < 0 ) {
890 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
895 avahi_log_info("Files changed, reloading.");
903 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
905 struct timespec nullts = { 0, 0 };
911 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
914 /* Sleep for a half-second to avoid potential races
915 * during install/uninstall. */
917 avahi_log_info("Files changed, reloading.");
920 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
926 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
928 const AvahiPoll *poll_api;
931 assert(simple_poll_api);
933 poll_api = avahi_simple_poll_get(simple_poll_api);
935 if ((sig = daemon_signal_next()) <= 0) {
936 avahi_log_error("daemon_signal_next() failed");
937 poll_api->watch_free(watch);
947 sig == SIGINT ? "SIGINT" :
948 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
949 avahi_simple_poll_quit(simple_poll_api);
953 avahi_log_info("Got SIGHUP, reloading.");
959 avahi_log_info("Got SIGUSR1, dumping record data.");
960 avahi_server_dump(avahi_server, dump, NULL);
964 avahi_log_warn("Got spurious signal, ignoring.");
969 /* Imported from ../avahi-client/nss-check.c */
970 int avahi_nss_support(void);
972 static int run_server(DaemonConfig *c) {
975 const AvahiPoll *poll_api = NULL;
976 AvahiWatch *sig_watch = NULL;
977 int retval_is_sent = 0;
979 AvahiWatch *inotify_watch = NULL;
983 AvahiWatch *kqueue_watch = NULL;
988 if (!(nss_support = avahi_nss_support()))
989 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
991 if (!(simple_poll_api = avahi_simple_poll_new())) {
992 avahi_log_error("Failed to create main loop object.");
996 poll_api = avahi_simple_poll_get(simple_poll_api);
998 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
999 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
1003 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
1004 avahi_log_error( "Failed to create signal watcher");
1008 if (simple_protocol_setup(poll_api) < 0)
1012 if (c->enable_dbus) {
1013 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, !c->fail_on_missing_dbus
1014 #ifdef ENABLE_CHROOT
1015 && !config.use_chroot
1019 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
1021 if (c->fail_on_missing_dbus)
1027 #ifdef ENABLE_CHROOT
1029 if (config.drop_root && config.use_chroot) {
1030 if (chroot(AVAHI_CONFIG_DIR) < 0) {
1031 avahi_log_error("Failed to chroot(): %s", strerror(errno));
1035 avahi_log_info("Successfully called chroot().");
1038 if (avahi_caps_drop_all() < 0) {
1039 avahi_log_error("Failed to drop capabilities.");
1042 avahi_log_info("Successfully dropped remaining capabilities.");
1048 if ((inotify_fd = inotify_init()) < 0)
1049 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
1051 add_inotify_watches();
1053 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
1054 avahi_log_error( "Failed to create inotify watcher");
1061 if ((kq = kqueue()) < 0)
1062 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
1064 add_kqueue_watches();
1066 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
1067 avahi_log_error( "Failed to create kqueue watcher");
1074 #ifdef ENABLE_CHROOT
1075 static_service_load(config.use_chroot);
1076 static_hosts_load(config.use_chroot);
1078 static_service_load(0);
1079 static_hosts_load(0);
1082 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
1083 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
1087 update_wide_area_servers();
1088 update_browse_domains();
1091 daemon_retval_send(0);
1096 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1098 /* We handle signals through an FD, so let's continue */
1102 avahi_log_error("poll(): %s", strerror(errno));
1112 static_service_remove_from_server();
1113 static_service_free_all();
1115 static_hosts_remove_from_server();
1116 static_hosts_free_all();
1118 remove_dns_server_entry_groups();
1120 simple_protocol_shutdown();
1124 dbus_protocol_shutdown();
1128 avahi_server_free(avahi_server);
1129 avahi_server = NULL;
1132 daemon_signal_done();
1135 poll_api->watch_free(sig_watch);
1139 poll_api->watch_free(inotify_watch);
1140 if (inotify_fd >= 0)
1146 poll_api->watch_free(kqueue_watch);
1149 for (i = 0; i < num_kfds; i++) {
1155 if (simple_poll_api) {
1156 avahi_simple_poll_free(simple_poll_api);
1157 simple_poll_api = NULL;
1160 if (!retval_is_sent && c->daemonize)
1161 daemon_retval_send(1);
1166 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1168 static int drop_root(void) {
1173 if (!(pw = getpwnam(AVAHI_USER))) {
1174 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1178 if (!(gr = getgrnam(AVAHI_GROUP))) {
1179 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1183 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);
1185 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1186 avahi_log_error("Failed to change group list: %s", strerror(errno));
1190 #if defined(HAVE_SETRESGID)
1191 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1192 #elif defined(HAVE_SETEGID)
1193 if ((r = setgid(gr->gr_gid)) >= 0)
1194 r = setegid(gr->gr_gid);
1195 #elif defined(HAVE_SETREGID)
1196 r = setregid(gr->gr_gid, gr->gr_gid);
1198 #error "No API to drop privileges"
1202 avahi_log_error("Failed to change GID: %s", strerror(errno));
1206 #if defined(HAVE_SETRESUID)
1207 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1208 #elif defined(HAVE_SETEUID)
1209 if ((r = setuid(pw->pw_uid)) >= 0)
1210 r = seteuid(pw->pw_uid);
1211 #elif defined(HAVE_SETREUID)
1212 r = setreuid(pw->pw_uid, pw->pw_uid);
1214 #error "No API to drop privileges"
1218 avahi_log_error("Failed to change UID: %s", strerror(errno));
1222 set_env("USER", pw->pw_name);
1223 set_env("LOGNAME", pw->pw_name);
1224 set_env("HOME", pw->pw_dir);
1226 avahi_log_info("Successfully dropped root privileges.");
1231 static const char* pid_file_proc(void) {
1232 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1235 static int make_runtime_dir(void) {
1238 int reset_umask = 0;
1243 if (!(pw = getpwnam(AVAHI_USER))) {
1244 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1248 if (!(gr = getgrnam(AVAHI_GROUP))) {
1249 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1256 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1257 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1261 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1263 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1264 avahi_log_error("stat(): %s\n", strerror(errno));
1268 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1269 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1281 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1283 rl.rlim_cur = rl.rlim_max = limit;
1285 if (setrlimit(resource, &rl) < 0)
1286 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1289 static void enforce_rlimits(void) {
1291 if (config.rlimit_as_set)
1292 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1294 if (config.rlimit_core_set)
1295 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1296 if (config.rlimit_data_set)
1297 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1298 if (config.rlimit_fsize_set)
1299 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1300 if (config.rlimit_nofile_set)
1301 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1302 if (config.rlimit_stack_set)
1303 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1305 if (config.rlimit_nproc_set)
1306 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1309 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1310 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
1311 /* We don't need locked memory */
1312 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1316 #define RANDOM_DEVICE "/dev/urandom"
1318 static void init_rand_seed(void) {
1322 /* Try to initialize seed from /dev/urandom, to make it a little
1323 * less predictable, and to make sure that multiple machines
1324 * booted at the same time choose different random seeds. */
1325 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1326 read(fd, &seed, sizeof(seed));
1330 /* If the initialization failed by some reason, we add the time to the seed*/
1331 seed ^= (unsigned) time(NULL);
1336 int main(int argc, char *argv[]) {
1338 int wrote_pid_file = 0;
1340 avahi_set_log_function(log_function);
1344 avahi_server_config_init(&config.server_config);
1345 config.command = DAEMON_RUN;
1346 config.daemonize = 0;
1347 config.config_file = NULL;
1349 config.enable_dbus = 1;
1350 config.fail_on_missing_dbus = 1;
1353 config.drop_root = 1;
1354 config.set_rlimits = 1;
1355 #ifdef ENABLE_CHROOT
1356 config.use_chroot = 1;
1358 config.modify_proc_title = 1;
1360 config.disable_user_service_publishing = 0;
1361 config.publish_dns_servers = NULL;
1362 config.publish_resolv_conf = 0;
1363 config.use_syslog = 0;
1365 config.rlimit_as_set = 0;
1366 config.rlimit_core_set = 0;
1367 config.rlimit_data_set = 0;
1368 config.rlimit_fsize_set = 0;
1369 config.rlimit_nofile_set = 0;
1370 config.rlimit_stack_set = 0;
1372 config.rlimit_nproc_set = 0;
1375 if ((argv0 = strrchr(argv[0], '/')))
1376 argv0 = avahi_strdup(argv0 + 1);
1378 argv0 = avahi_strdup(argv[0]);
1380 daemon_pid_file_ident = (const char *) argv0;
1381 daemon_log_ident = (char*) argv0;
1382 daemon_pid_file_proc = pid_file_proc;
1384 if (parse_command_line(&config, argc, argv) < 0)
1387 if (config.modify_proc_title)
1388 avahi_init_proc_title(argc, argv);
1390 #ifdef ENABLE_CHROOT
1391 config.use_chroot = config.use_chroot && config.drop_root;
1394 if (config.command == DAEMON_HELP) {
1397 } else if (config.command == DAEMON_VERSION) {
1398 printf("%s "PACKAGE_VERSION"\n", argv0);
1400 } else if (config.command == DAEMON_KILL) {
1401 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1402 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1408 } else if (config.command == DAEMON_RELOAD) {
1409 if (daemon_pid_file_kill(SIGHUP) < 0) {
1410 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1416 } else if (config.command == DAEMON_CHECK)
1417 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1418 else if (config.command == DAEMON_RUN) {
1421 if (getuid() != 0 && config.drop_root) {
1422 avahi_log_error("This program is intended to be run as root.");
1426 if ((pid = daemon_pid_file_is_running()) >= 0) {
1427 avahi_log_error("Daemon already running on PID %u", pid);
1431 if (load_config_file(&config) < 0)
1434 if (config.daemonize) {
1435 daemon_retval_init();
1437 if ((pid = daemon_fork()) < 0)
1439 else if (pid != 0) {
1443 if ((ret = daemon_retval_wait(20)) < 0) {
1444 avahi_log_error("Could not receive return value from daemon process.");
1455 if (config.use_syslog || config.daemonize)
1456 daemon_log_use = DAEMON_LOG_SYSLOG;
1458 if (daemon_close_all(-1) < 0) {
1459 avahi_log_error("Failed to close remaining file descriptors: %s", strerror(errno));
1463 if (make_runtime_dir() < 0)
1466 if (config.drop_root) {
1467 #ifdef ENABLE_CHROOT
1468 if (config.use_chroot)
1469 if (avahi_caps_reduce() < 0)
1473 if (drop_root() < 0)
1476 #ifdef ENABLE_CHROOT
1477 if (config.use_chroot)
1478 if (avahi_caps_reduce2() < 0)
1483 if (daemon_pid_file_create() < 0) {
1484 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1486 if (config.daemonize)
1487 daemon_retval_send(1);
1492 if (config.set_rlimits)
1497 #ifdef ENABLE_CHROOT
1498 if (config.drop_root && config.use_chroot)
1499 if (avahi_chroot_helper_start(argv0) < 0) {
1500 avahi_log_error("failed to start chroot() helper daemon.");
1504 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1506 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1508 if (run_server(&config) == 0)
1514 if (config.daemonize)
1515 daemon_retval_done();
1517 avahi_server_config_free(&config.server_config);
1518 avahi_free(config.config_file);
1519 avahi_strfreev(config.publish_dns_servers);
1520 avahi_strfreev(resolv_conf_name_servers);
1521 avahi_strfreev(resolv_conf_search_domains);
1523 if (wrote_pid_file) {
1524 #ifdef ENABLE_CHROOT
1525 avahi_chroot_helper_unlink(pid_file_proc());
1527 daemon_pid_file_remove();
1531 #ifdef ENABLE_CHROOT
1532 avahi_chroot_helper_shutdown();