2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <sys/ioctl.h>
40 #include <sys/resource.h>
41 #include <sys/socket.h>
44 #include <sys/inotify.h>
48 #include <sys/types.h>
49 #include <sys/event.h>
53 #include <libdaemon/dfork.h>
54 #include <libdaemon/dsignal.h>
55 #include <libdaemon/dlog.h>
56 #include <libdaemon/dpid.h>
58 #include <avahi-common/malloc.h>
59 #include <avahi-common/simple-watch.h>
60 #include <avahi-common/error.h>
61 #include <avahi-common/alternative.h>
62 #include <avahi-common/domain.h>
64 #include <avahi-core/core.h>
65 #include <avahi-core/publish.h>
66 #include <avahi-core/dns-srv-rr.h>
67 #include <avahi-core/log.h>
74 #include "setproctitle.h"
76 #include "simple-protocol.h"
77 #include "static-services.h"
78 #include "static-hosts.h"
79 #include "ini-file-parser.h"
80 #include "sd-daemon.h"
83 #include "dbus-protocol.h"
86 AvahiServer *avahi_server = NULL;
87 AvahiSimplePoll *simple_poll_api = NULL;
88 static char *argv0 = NULL;
101 AvahiServerConfig server_config;
102 DaemonCommand command;
108 int fail_on_missing_dbus;
109 unsigned n_clients_max;
110 unsigned n_objects_per_client_max;
111 unsigned n_entries_per_entry_group_max;
118 int modify_proc_title;
120 int disable_user_service_publishing;
121 int publish_resolv_conf;
122 char ** publish_dns_servers;
125 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
126 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
129 int rlimit_nproc_set;
134 #define RESOLV_CONF "/etc/resolv.conf"
135 #define BROWSE_DOMAINS_MAX 16
137 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
138 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
140 static char **resolv_conf_name_servers = NULL;
141 static char **resolv_conf_search_domains = NULL;
143 static DaemonConfig config;
145 static int has_prefix(const char *s, const char *prefix) {
150 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
153 static int load_resolv_conf(void) {
158 avahi_strfreev(resolv_conf_name_servers);
159 resolv_conf_name_servers = NULL;
161 avahi_strfreev(resolv_conf_search_domains);
162 resolv_conf_search_domains = NULL;
165 f = avahi_chroot_helper_get_file(RESOLV_CONF);
167 f = fopen(RESOLV_CONF, "r");
171 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
175 resolv_conf_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
176 resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);
182 if (!(fgets(ln, sizeof(ln), f)))
185 ln[strcspn(ln, "\r\n#")] = 0;
186 p = ln + strspn(ln, "\t ");
188 if ((has_prefix(p, "nameserver ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
190 p += strspn(p, "\t ");
191 p[strcspn(p, "\t ")] = 0;
192 resolv_conf_name_servers[i++] = avahi_strdup(p);
195 if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
196 has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {
200 while (j < BROWSE_DOMAINS_MAX) {
203 p += strspn(p, "\t ");
204 k = strcspn(p, "\t ");
207 resolv_conf_search_domains[j++] = avahi_strndup(p, k);
222 avahi_strfreev(resolv_conf_name_servers);
223 resolv_conf_name_servers = NULL;
225 avahi_strfreev(resolv_conf_search_domains);
226 resolv_conf_search_domains = NULL;
235 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
242 g = avahi_s_entry_group_new(s, NULL, NULL);
244 assert(avahi_s_entry_group_is_empty(g));
246 for (p = l; *p; p++) {
249 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
250 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
252 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
253 avahi_s_entry_group_free(g);
254 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
259 avahi_s_entry_group_commit(g);
264 static void remove_dns_server_entry_groups(void) {
266 if (resolv_conf_entry_group)
267 avahi_s_entry_group_reset(resolv_conf_entry_group);
269 if (dns_servers_entry_group)
270 avahi_s_entry_group_reset(dns_servers_entry_group);
273 static void update_wide_area_servers(void) {
274 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
278 if (!resolv_conf_name_servers) {
279 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
283 for (p = resolv_conf_name_servers; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
284 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
285 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
290 avahi_server_set_wide_area_servers(avahi_server, a, n);
293 static AvahiStringList *filter_duplicate_domains(AvahiStringList *l) {
294 AvahiStringList *e, *n, *p;
299 for (p = l, e = l->next; e; e = n) {
302 if (avahi_domain_equal((char*) e->text, (char*) l->text)) {
309 l->next = filter_duplicate_domains(l->next);
313 static void update_browse_domains(void) {
318 if (!resolv_conf_search_domains) {
319 avahi_server_set_browse_domains(avahi_server, NULL);
323 l = avahi_string_list_copy(config.server_config.browse_domains);
325 for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
326 if (!avahi_is_valid_domain_name(*p))
327 avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
329 l = avahi_string_list_add(l, *p);
332 l = filter_duplicate_domains(l);
334 avahi_server_set_browse_domains(avahi_server, l);
337 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
338 DaemonConfig *c = userdata;
343 /* This function is possibly called before the global variable
344 * avahi_server has been set, therefore we do it explicitly */
349 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
350 dbus_protocol_server_state_changed(state);
354 case AVAHI_SERVER_RUNNING:
355 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));
357 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
359 static_service_add_to_server();
360 static_hosts_add_to_server();
362 remove_dns_server_entry_groups();
364 if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
365 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
367 if (c->publish_dns_servers && c->publish_dns_servers[0])
368 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
370 simple_protocol_restart_queries();
373 case AVAHI_SERVER_COLLISION: {
376 avahi_set_proc_title(argv0, "%s: collision", argv0);
378 static_service_remove_from_server();
379 static_hosts_remove_from_server();
380 remove_dns_server_entry_groups();
382 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
383 avahi_log_warn("Host name conflict, retrying with <%s>", n);
384 avahi_server_set_host_name(s, n);
390 case AVAHI_SERVER_FAILURE:
392 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
393 avahi_simple_poll_quit(simple_poll_api);
396 case AVAHI_SERVER_REGISTERING:
398 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
400 static_service_remove_from_server();
401 static_hosts_remove_from_server();
402 remove_dns_server_entry_groups();
406 case AVAHI_SERVER_INVALID:
412 static void help(FILE *f) {
415 " -h --help Show this help\n"
416 " -D --daemonize Daemonize after startup (implies -s)\n"
417 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
418 " -k --kill Kill a running daemon\n"
419 " -r --reload Request a running daemon to reload static services\n"
420 " -c --check Return 0 if a daemon is already running\n"
421 " -V --version Show version\n"
422 " -f --file=FILE Load the specified configuration file instead of\n"
423 " "AVAHI_CONFIG_FILE"\n"
424 " --no-rlimits Don't enforce resource limits\n"
425 " --no-drop-root Don't drop privileges\n"
427 " --no-chroot Don't chroot()\n"
429 " --no-proc-title Don't modify process title\n"
430 " --debug Increase verbosity\n",
435 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
439 OPTION_NO_RLIMITS = 256,
444 OPTION_NO_PROC_TITLE,
448 static const struct option long_options[] = {
449 { "help", no_argument, NULL, 'h' },
450 { "daemonize", no_argument, NULL, 'D' },
451 { "kill", no_argument, NULL, 'k' },
452 { "version", no_argument, NULL, 'V' },
453 { "file", required_argument, NULL, 'f' },
454 { "reload", no_argument, NULL, 'r' },
455 { "check", no_argument, NULL, 'c' },
456 { "syslog", no_argument, NULL, 's' },
457 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
458 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
460 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
462 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
463 { "debug", no_argument, NULL, OPTION_DEBUG },
469 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
476 c->command = DAEMON_HELP;
482 c->command = DAEMON_KILL;
485 c->command = DAEMON_VERSION;
488 avahi_free(c->config_file);
489 c->config_file = avahi_strdup(optarg);
492 c->command = DAEMON_RELOAD;
495 c->command = DAEMON_CHECK;
497 case OPTION_NO_RLIMITS:
500 case OPTION_NO_DROP_ROOT:
504 case OPTION_NO_CHROOT:
508 case OPTION_NO_PROC_TITLE:
509 c->modify_proc_title = 0;
520 fprintf(stderr, "Too many arguments\n");
527 static int is_yes(const char *s) {
530 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
533 static int parse_unsigned(const char *s, unsigned *u) {
539 ul = strtoul(s, &e, 0);
541 if (!e || *e || errno != 0)
546 if ((unsigned long) k != ul)
553 static int load_config_file(DaemonConfig *c) {
556 AvahiIniFileGroup *g;
560 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
563 for (g = f->groups; g; g = g->groups_next) {
565 if (strcasecmp(g->name, "server") == 0) {
568 for (p = g->pairs; p; p = p->pairs_next) {
570 if (strcasecmp(p->key, "host-name") == 0) {
571 avahi_free(c->server_config.host_name);
572 c->server_config.host_name = avahi_strdup(p->value);
573 } else if (strcasecmp(p->key, "domain-name") == 0) {
574 avahi_free(c->server_config.domain_name);
575 c->server_config.domain_name = avahi_strdup(p->value);
576 } else if (strcasecmp(p->key, "browse-domains") == 0) {
579 e = avahi_split_csv(p->value);
581 for (t = e; *t; t++) {
582 char cleaned[AVAHI_DOMAIN_NAME_MAX];
584 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
585 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
590 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
595 c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
596 } else if (strcasecmp(p->key, "use-ipv4") == 0)
597 c->server_config.use_ipv4 = is_yes(p->value);
598 else if (strcasecmp(p->key, "use-ipv6") == 0)
599 c->server_config.use_ipv6 = is_yes(p->value);
600 else if (strcasecmp(p->key, "check-response-ttl") == 0)
601 c->server_config.check_response_ttl = is_yes(p->value);
602 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
603 c->server_config.allow_point_to_point = is_yes(p->value);
604 else if (strcasecmp(p->key, "use-iff-running") == 0)
605 c->server_config.use_iff_running = is_yes(p->value);
606 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
607 c->server_config.disallow_other_stacks = is_yes(p->value);
609 else if (strcasecmp(p->key, "enable-dbus") == 0) {
611 if (*(p->value) == 'w' || *(p->value) == 'W') {
612 c->fail_on_missing_dbus = 0;
614 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
615 c->fail_on_missing_dbus = 1;
622 else if (strcasecmp(p->key, "allow-interfaces") == 0) {
625 avahi_string_list_free(c->server_config.allow_interfaces);
626 c->server_config.allow_interfaces = NULL;
627 e = avahi_split_csv(p->value);
630 c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);
633 } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
636 avahi_string_list_free(c->server_config.deny_interfaces);
637 c->server_config.deny_interfaces = NULL;
638 e = avahi_split_csv(p->value);
641 c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);
644 } else if (strcasecmp(p->key, "cache-entries-max") == 0) {
647 if (parse_unsigned(p->value, &k) < 0) {
648 avahi_log_error("Invalid cache-entries-max setting %s", p->value);
652 c->server_config.n_cache_entries_max = k;
654 } else if (strcasecmp(p->key, "clients-max") == 0) {
657 if (parse_unsigned(p->value, &k) < 0) {
658 avahi_log_error("Invalid clients-max setting %s", p->value);
662 c->n_clients_max = k;
663 } else if (strcasecmp(p->key, "objects-per-client-max") == 0) {
666 if (parse_unsigned(p->value, &k) < 0) {
667 avahi_log_error("Invalid objects-per-client-max setting %s", p->value);
671 c->n_objects_per_client_max = k;
672 } else if (strcasecmp(p->key, "entries-per-entry-group-max") == 0) {
675 if (parse_unsigned(p->value, &k) < 0) {
676 avahi_log_error("Invalid entries-per-entry-group-max setting %s", p->value);
680 c->n_entries_per_entry_group_max = k;
683 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
688 } else if (strcasecmp(g->name, "publish") == 0) {
691 for (p = g->pairs; p; p = p->pairs_next) {
693 if (strcasecmp(p->key, "publish-addresses") == 0)
694 c->server_config.publish_addresses = is_yes(p->value);
695 else if (strcasecmp(p->key, "publish-hinfo") == 0)
696 c->server_config.publish_hinfo = is_yes(p->value);
697 else if (strcasecmp(p->key, "publish-workstation") == 0)
698 c->server_config.publish_workstation = is_yes(p->value);
699 else if (strcasecmp(p->key, "publish-domain") == 0)
700 c->server_config.publish_domain = is_yes(p->value);
701 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
702 c->publish_resolv_conf = is_yes(p->value);
703 else if (strcasecmp(p->key, "disable-publishing") == 0)
704 c->server_config.disable_publishing = is_yes(p->value);
705 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
706 c->disable_user_service_publishing = is_yes(p->value);
707 else if (strcasecmp(p->key, "add-service-cookie") == 0)
708 c->server_config.add_service_cookie = is_yes(p->value);
709 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
710 avahi_strfreev(c->publish_dns_servers);
711 c->publish_dns_servers = avahi_split_csv(p->value);
712 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
713 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
714 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
715 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
717 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
722 } else if (strcasecmp(g->name, "wide-area") == 0) {
725 for (p = g->pairs; p; p = p->pairs_next) {
727 if (strcasecmp(p->key, "enable-wide-area") == 0)
728 c->server_config.enable_wide_area = is_yes(p->value);
730 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
735 } else if (strcasecmp(g->name, "reflector") == 0) {
738 for (p = g->pairs; p; p = p->pairs_next) {
740 if (strcasecmp(p->key, "enable-reflector") == 0)
741 c->server_config.enable_reflector = is_yes(p->value);
742 else if (strcasecmp(p->key, "reflect-ipv") == 0)
743 c->server_config.reflect_ipv = is_yes(p->value);
745 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
750 } else if (strcasecmp(g->name, "rlimits") == 0) {
753 for (p = g->pairs; p; p = p->pairs_next) {
755 if (strcasecmp(p->key, "rlimit-as") == 0) {
756 c->rlimit_as_set = 1;
757 c->rlimit_as = atoi(p->value);
758 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
759 c->rlimit_core_set = 1;
760 c->rlimit_core = atoi(p->value);
761 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
762 c->rlimit_data_set = 1;
763 c->rlimit_data = atoi(p->value);
764 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
765 c->rlimit_fsize_set = 1;
766 c->rlimit_fsize = atoi(p->value);
767 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
768 c->rlimit_nofile_set = 1;
769 c->rlimit_nofile = atoi(p->value);
770 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
771 c->rlimit_stack_set = 1;
772 c->rlimit_stack = atoi(p->value);
773 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
775 c->rlimit_nproc_set = 1;
776 c->rlimit_nproc = atoi(p->value);
778 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
781 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
788 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
798 avahi_ini_file_free(f);
803 static void log_function(AvahiLogLevel level, const char *txt) {
805 static const int log_level_map[] = {
813 assert(level < AVAHI_LOG_LEVEL_MAX);
816 if (!config.debug && level == AVAHI_LOG_DEBUG)
819 daemon_log(log_level_map[level], "%s", txt);
822 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
823 avahi_log_info("%s", text);
828 static int inotify_fd = -1;
830 static void add_inotify_watches(void) {
832 /* We ignore the return values, because one or more of these files
833 * might not exist and we're OK with that. In addition we never
834 * want to remove these watches, hence we keep their ids? */
837 c = config.use_chroot;
840 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
845 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
856 #define NUM_WATCHES 2
859 static int kfds[NUM_WATCHES];
860 static int num_kfds = 0;
862 static void add_kqueue_watch(const char *dir);
864 static void add_kqueue_watches(void) {
868 c = config.use_chroot;
871 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
872 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
875 static void add_kqueue_watch(const char *dir) {
882 if (num_kfds >= NUM_WATCHES)
885 fd = open(dir, O_RDONLY);
888 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
889 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
891 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
896 kfds[num_kfds++] = fd;
901 static void reload_config(void) {
904 /* Refresh in case the config dirs have been removed */
905 add_inotify_watches();
909 add_kqueue_watches();
913 static_service_load(config.use_chroot);
914 static_hosts_load(config.use_chroot);
916 static_service_load(0);
917 static_hosts_load(0);
919 static_service_add_to_server();
920 static_hosts_add_to_server();
922 if (resolv_conf_entry_group)
923 avahi_s_entry_group_reset(resolv_conf_entry_group);
927 update_wide_area_servers();
928 update_browse_domains();
930 if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
931 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
936 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
940 assert(fd == inotify_fd);
943 ioctl(inotify_fd, FIONREAD, &n);
947 buffer = avahi_malloc(n);
948 if (read(inotify_fd, buffer, n) < 0 ) {
950 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
955 avahi_log_info("Files changed, reloading.");
963 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
965 struct timespec nullts = { 0, 0 };
971 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
974 /* Sleep for a half-second to avoid potential races
975 * during install/uninstall. */
977 avahi_log_info("Files changed, reloading.");
980 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
986 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
988 const AvahiPoll *poll_api;
991 assert(simple_poll_api);
993 poll_api = avahi_simple_poll_get(simple_poll_api);
995 if ((sig = daemon_signal_next()) <= 0) {
996 avahi_log_error("daemon_signal_next() failed");
997 poll_api->watch_free(watch);
1006 "Got %s, quitting.",
1007 sig == SIGINT ? "SIGINT" :
1008 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
1009 avahi_simple_poll_quit(simple_poll_api);
1013 avahi_log_info("Got SIGHUP, reloading.");
1019 avahi_log_info("Got SIGUSR1, dumping record data.");
1020 avahi_server_dump(avahi_server, dump, NULL);
1024 avahi_log_warn("Got spurious signal, ignoring.");
1029 /* Imported from ../avahi-client/nss-check.c */
1030 int avahi_nss_support(void);
1032 static int run_server(DaemonConfig *c) {
1035 const AvahiPoll *poll_api = NULL;
1036 AvahiWatch *sig_watch = NULL;
1037 int retval_is_sent = 0;
1039 AvahiWatch *inotify_watch = NULL;
1043 AvahiWatch *kqueue_watch = NULL;
1048 if (!(nss_support = avahi_nss_support()))
1049 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
1051 if (!(simple_poll_api = avahi_simple_poll_new())) {
1052 avahi_log_error("Failed to create main loop object.");
1056 poll_api = avahi_simple_poll_get(simple_poll_api);
1058 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
1059 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
1063 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
1064 avahi_log_error( "Failed to create signal watcher");
1068 if (simple_protocol_setup(poll_api) < 0)
1072 if (c->enable_dbus) {
1073 if (dbus_protocol_setup(poll_api,
1074 config.disable_user_service_publishing,
1075 config.n_clients_max,
1076 config.n_objects_per_client_max,
1077 config.n_entries_per_entry_group_max,
1078 !c->fail_on_missing_dbus
1079 #ifdef ENABLE_CHROOT
1080 && !config.use_chroot
1084 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
1086 if (c->fail_on_missing_dbus)
1092 #ifdef ENABLE_CHROOT
1094 if (config.drop_root && config.use_chroot) {
1095 if (chroot(AVAHI_CONFIG_DIR) < 0) {
1096 avahi_log_error("Failed to chroot(): %s", strerror(errno));
1100 avahi_log_info("Successfully called chroot().");
1103 if (avahi_caps_drop_all() < 0) {
1104 avahi_log_error("Failed to drop capabilities.");
1107 avahi_log_info("Successfully dropped remaining capabilities.");
1113 if ((inotify_fd = inotify_init()) < 0)
1114 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
1116 add_inotify_watches();
1118 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
1119 avahi_log_error( "Failed to create inotify watcher");
1126 if ((kq = kqueue()) < 0)
1127 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
1129 add_kqueue_watches();
1131 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
1132 avahi_log_error( "Failed to create kqueue watcher");
1139 #ifdef ENABLE_CHROOT
1140 static_service_load(config.use_chroot);
1141 static_hosts_load(config.use_chroot);
1143 static_service_load(0);
1144 static_hosts_load(0);
1147 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
1148 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
1152 update_wide_area_servers();
1153 update_browse_domains();
1156 daemon_retval_send(0);
1161 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1163 /* We handle signals through an FD, so let's continue */
1167 avahi_log_error("poll(): %s", strerror(errno));
1177 static_service_remove_from_server();
1178 static_service_free_all();
1180 static_hosts_remove_from_server();
1181 static_hosts_free_all();
1183 remove_dns_server_entry_groups();
1185 simple_protocol_shutdown();
1189 dbus_protocol_shutdown();
1193 avahi_server_free(avahi_server);
1194 avahi_server = NULL;
1197 daemon_signal_done();
1200 poll_api->watch_free(sig_watch);
1204 poll_api->watch_free(inotify_watch);
1205 if (inotify_fd >= 0)
1211 poll_api->watch_free(kqueue_watch);
1214 for (i = 0; i < num_kfds; i++) {
1220 if (simple_poll_api) {
1221 avahi_simple_poll_free(simple_poll_api);
1222 simple_poll_api = NULL;
1225 if (!retval_is_sent && c->daemonize)
1226 daemon_retval_send(1);
1231 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1233 static int drop_root(void) {
1238 if (!(pw = getpwnam(AVAHI_USER))) {
1239 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1243 if (!(gr = getgrnam(AVAHI_GROUP))) {
1244 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1248 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);
1250 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1251 avahi_log_error("Failed to change group list: %s", strerror(errno));
1255 #if defined(HAVE_SETRESGID)
1256 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1257 #elif defined(HAVE_SETEGID)
1258 if ((r = setgid(gr->gr_gid)) >= 0)
1259 r = setegid(gr->gr_gid);
1260 #elif defined(HAVE_SETREGID)
1261 r = setregid(gr->gr_gid, gr->gr_gid);
1263 #error "No API to drop privileges"
1267 avahi_log_error("Failed to change GID: %s", strerror(errno));
1271 #if defined(HAVE_SETRESUID)
1272 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1273 #elif defined(HAVE_SETEUID)
1274 if ((r = setuid(pw->pw_uid)) >= 0)
1275 r = seteuid(pw->pw_uid);
1276 #elif defined(HAVE_SETREUID)
1277 r = setreuid(pw->pw_uid, pw->pw_uid);
1279 #error "No API to drop privileges"
1283 avahi_log_error("Failed to change UID: %s", strerror(errno));
1287 set_env("USER", pw->pw_name);
1288 set_env("LOGNAME", pw->pw_name);
1289 set_env("HOME", pw->pw_dir);
1291 avahi_log_info("Successfully dropped root privileges.");
1296 static const char* pid_file_proc(void) {
1297 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1300 static int make_runtime_dir(void) {
1303 int reset_umask = 0;
1308 if (!(pw = getpwnam(AVAHI_USER))) {
1309 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1313 if (!(gr = getgrnam(AVAHI_GROUP))) {
1314 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1321 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1322 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1326 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1328 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1329 avahi_log_error("stat(): %s\n", strerror(errno));
1333 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1334 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1346 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1348 rl.rlim_cur = rl.rlim_max = limit;
1350 if (setrlimit(resource, &rl) < 0)
1351 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1354 static void enforce_rlimits(void) {
1356 if (config.rlimit_as_set)
1357 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1359 if (config.rlimit_core_set)
1360 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1361 if (config.rlimit_data_set)
1362 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1363 if (config.rlimit_fsize_set)
1364 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1365 if (config.rlimit_nofile_set)
1366 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1367 if (config.rlimit_stack_set)
1368 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1370 if (config.rlimit_nproc_set)
1371 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1374 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1375 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
1376 /* We don't need locked memory */
1377 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1381 #define RANDOM_DEVICE "/dev/urandom"
1383 static void init_rand_seed(void) {
1387 /* Try to initialize seed from /dev/urandom, to make it a little
1388 * less predictable, and to make sure that multiple machines
1389 * booted at the same time choose different random seeds. */
1390 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1391 read(fd, &seed, sizeof(seed));
1395 /* If the initialization failed by some reason, we add the time to the seed*/
1396 seed ^= (unsigned) time(NULL);
1401 int main(int argc, char *argv[]) {
1403 int wrote_pid_file = 0;
1405 avahi_set_log_function(log_function);
1409 avahi_server_config_init(&config.server_config);
1410 config.command = DAEMON_RUN;
1411 config.daemonize = 0;
1412 config.config_file = NULL;
1414 config.enable_dbus = 1;
1415 config.fail_on_missing_dbus = 1;
1416 config.n_clients_max = 0;
1417 config.n_objects_per_client_max = 0;
1418 config.n_entries_per_entry_group_max = 0;
1421 config.drop_root = 1;
1422 config.set_rlimits = 1;
1423 #ifdef ENABLE_CHROOT
1424 config.use_chroot = 1;
1426 config.modify_proc_title = 1;
1428 config.disable_user_service_publishing = 0;
1429 config.publish_dns_servers = NULL;
1430 config.publish_resolv_conf = 0;
1431 config.use_syslog = 0;
1433 config.rlimit_as_set = 0;
1434 config.rlimit_core_set = 0;
1435 config.rlimit_data_set = 0;
1436 config.rlimit_fsize_set = 0;
1437 config.rlimit_nofile_set = 0;
1438 config.rlimit_stack_set = 0;
1440 config.rlimit_nproc_set = 0;
1443 if ((argv0 = strrchr(argv[0], '/')))
1444 argv0 = avahi_strdup(argv0 + 1);
1446 argv0 = avahi_strdup(argv[0]);
1448 daemon_pid_file_ident = (const char *) argv0;
1449 daemon_log_ident = (char*) argv0;
1450 daemon_pid_file_proc = pid_file_proc;
1452 if (parse_command_line(&config, argc, argv) < 0)
1455 if (config.modify_proc_title)
1456 avahi_init_proc_title(argc, argv);
1458 #ifdef ENABLE_CHROOT
1459 config.use_chroot = config.use_chroot && config.drop_root;
1462 if (config.command == DAEMON_HELP) {
1465 } else if (config.command == DAEMON_VERSION) {
1466 printf("%s "PACKAGE_VERSION"\n", argv0);
1468 } else if (config.command == DAEMON_KILL) {
1469 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1470 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1476 } else if (config.command == DAEMON_RELOAD) {
1477 if (daemon_pid_file_kill(SIGHUP) < 0) {
1478 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1484 } else if (config.command == DAEMON_CHECK)
1485 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1486 else if (config.command == DAEMON_RUN) {
1489 if (getuid() != 0 && config.drop_root) {
1490 avahi_log_error("This program is intended to be run as root.");
1494 if ((pid = daemon_pid_file_is_running()) >= 0) {
1495 avahi_log_error("Daemon already running on PID %u", pid);
1499 if (load_config_file(&config) < 0)
1502 if (config.daemonize) {
1503 daemon_retval_init();
1505 if ((pid = daemon_fork()) < 0)
1507 else if (pid != 0) {
1511 if ((ret = daemon_retval_wait(20)) < 0) {
1512 avahi_log_error("Could not receive return value from daemon process.");
1523 if (config.use_syslog || config.daemonize)
1524 daemon_log_use = DAEMON_LOG_SYSLOG;
1526 if (sd_listen_fds(0) <= 0)
1527 if (daemon_close_all(-1) < 0) {
1528 avahi_log_error("Failed to close remaining file descriptors: %s", strerror(errno));
1532 if (make_runtime_dir() < 0)
1535 if (config.drop_root) {
1536 #ifdef ENABLE_CHROOT
1537 if (config.use_chroot)
1538 if (avahi_caps_reduce() < 0)
1542 if (drop_root() < 0)
1545 #ifdef ENABLE_CHROOT
1546 if (config.use_chroot)
1547 if (avahi_caps_reduce2() < 0)
1552 if (daemon_pid_file_create() < 0) {
1553 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1555 if (config.daemonize)
1556 daemon_retval_send(1);
1561 if (config.set_rlimits)
1566 #ifdef ENABLE_CHROOT
1567 if (config.drop_root && config.use_chroot)
1568 if (avahi_chroot_helper_start(argv0) < 0) {
1569 avahi_log_error("failed to start chroot() helper daemon.");
1573 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1575 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1577 if (run_server(&config) == 0)
1583 if (config.daemonize)
1584 daemon_retval_done();
1586 avahi_server_config_free(&config.server_config);
1587 avahi_free(config.config_file);
1588 avahi_strfreev(config.publish_dns_servers);
1589 avahi_strfreev(resolv_conf_name_servers);
1590 avahi_strfreev(resolv_conf_search_domains);
1592 if (wrote_pid_file) {
1593 #ifdef ENABLE_CHROOT
1594 avahi_chroot_helper_unlink(pid_file_proc());
1596 daemon_pid_file_remove();
1600 #ifdef ENABLE_CHROOT
1601 avahi_chroot_helper_shutdown();