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);
335 avahi_string_list_free(l);
338 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
339 DaemonConfig *c = userdata;
344 /* This function is possibly called before the global variable
345 * avahi_server has been set, therefore we do it explicitly */
350 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
351 dbus_protocol_server_state_changed(state);
355 case AVAHI_SERVER_RUNNING:
356 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 sd_notifyf(0, "STATUS=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));
358 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
360 static_service_add_to_server();
361 static_hosts_add_to_server();
363 remove_dns_server_entry_groups();
365 if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
366 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
368 if (c->publish_dns_servers && c->publish_dns_servers[0])
369 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
371 simple_protocol_restart_queries();
374 case AVAHI_SERVER_COLLISION: {
377 static_service_remove_from_server();
378 static_hosts_remove_from_server();
379 remove_dns_server_entry_groups();
381 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
383 avahi_log_warn("Host name conflict, retrying with %s", n);
384 sd_notifyf(0, "STATUS=Host name conflict, retrying with %s", n);
385 avahi_set_proc_title(argv0, "%s: collision [%s]", argv0, n);
387 avahi_server_set_host_name(s, n);
393 case AVAHI_SERVER_FAILURE:
395 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
396 sd_notifyf(0, "STATUS=Server error: %s", avahi_strerror(avahi_server_errno(s)));
398 avahi_simple_poll_quit(simple_poll_api);
401 case AVAHI_SERVER_REGISTERING:
403 sd_notifyf(0, "STATUS=Registering host name %s", avahi_server_get_host_name_fqdn(s));
404 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
406 static_service_remove_from_server();
407 static_hosts_remove_from_server();
408 remove_dns_server_entry_groups();
412 case AVAHI_SERVER_INVALID:
418 static void help(FILE *f) {
421 " -h --help Show this help\n"
422 " -D --daemonize Daemonize after startup (implies -s)\n"
423 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
424 " -k --kill Kill a running daemon\n"
425 " -r --reload Request a running daemon to reload static services\n"
426 " -c --check Return 0 if a daemon is already running\n"
427 " -V --version Show version\n"
428 " -f --file=FILE Load the specified configuration file instead of\n"
429 " "AVAHI_CONFIG_FILE"\n"
430 " --no-rlimits Don't enforce resource limits\n"
431 " --no-drop-root Don't drop privileges\n"
433 " --no-chroot Don't chroot()\n"
435 " --no-proc-title Don't modify process title\n"
436 " --debug Increase verbosity\n",
441 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
445 OPTION_NO_RLIMITS = 256,
450 OPTION_NO_PROC_TITLE,
454 static const struct option long_options[] = {
455 { "help", no_argument, NULL, 'h' },
456 { "daemonize", no_argument, NULL, 'D' },
457 { "kill", no_argument, NULL, 'k' },
458 { "version", no_argument, NULL, 'V' },
459 { "file", required_argument, NULL, 'f' },
460 { "reload", no_argument, NULL, 'r' },
461 { "check", no_argument, NULL, 'c' },
462 { "syslog", no_argument, NULL, 's' },
463 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
464 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
466 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
468 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
469 { "debug", no_argument, NULL, OPTION_DEBUG },
475 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
482 c->command = DAEMON_HELP;
488 c->command = DAEMON_KILL;
491 c->command = DAEMON_VERSION;
494 avahi_free(c->config_file);
495 c->config_file = avahi_strdup(optarg);
498 c->command = DAEMON_RELOAD;
501 c->command = DAEMON_CHECK;
503 case OPTION_NO_RLIMITS:
506 case OPTION_NO_DROP_ROOT:
510 case OPTION_NO_CHROOT:
514 case OPTION_NO_PROC_TITLE:
515 c->modify_proc_title = 0;
526 fprintf(stderr, "Too many arguments\n");
533 static int is_yes(const char *s) {
536 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
539 static int parse_unsigned(const char *s, unsigned *u) {
545 ul = strtoul(s, &e, 0);
547 if (!e || *e || errno != 0)
552 if ((unsigned long) k != ul)
559 static int parse_usec(const char *s, AvahiUsec *u) {
561 unsigned long long ull;
565 ull = strtoull(s, &e, 0);
567 if (!e || *e || errno != 0)
572 if ((unsigned long long) k != ull)
579 static int load_config_file(DaemonConfig *c) {
582 AvahiIniFileGroup *g;
586 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
589 for (g = f->groups; g; g = g->groups_next) {
591 if (strcasecmp(g->name, "server") == 0) {
594 for (p = g->pairs; p; p = p->pairs_next) {
596 if (strcasecmp(p->key, "host-name") == 0) {
597 avahi_free(c->server_config.host_name);
598 c->server_config.host_name = avahi_strdup(p->value);
599 } else if (strcasecmp(p->key, "domain-name") == 0) {
600 avahi_free(c->server_config.domain_name);
601 c->server_config.domain_name = avahi_strdup(p->value);
602 } else if (strcasecmp(p->key, "browse-domains") == 0) {
605 e = avahi_split_csv(p->value);
607 for (t = e; *t; t++) {
608 char cleaned[AVAHI_DOMAIN_NAME_MAX];
610 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
611 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
616 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
621 c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
622 } else if (strcasecmp(p->key, "use-ipv4") == 0)
623 c->server_config.use_ipv4 = is_yes(p->value);
624 else if (strcasecmp(p->key, "use-ipv6") == 0)
625 c->server_config.use_ipv6 = is_yes(p->value);
626 else if (strcasecmp(p->key, "check-response-ttl") == 0)
627 c->server_config.check_response_ttl = is_yes(p->value);
628 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
629 c->server_config.allow_point_to_point = is_yes(p->value);
630 else if (strcasecmp(p->key, "use-iff-running") == 0)
631 c->server_config.use_iff_running = is_yes(p->value);
632 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
633 c->server_config.disallow_other_stacks = is_yes(p->value);
635 else if (strcasecmp(p->key, "enable-dbus") == 0) {
637 if (*(p->value) == 'w' || *(p->value) == 'W') {
638 c->fail_on_missing_dbus = 0;
640 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
641 c->fail_on_missing_dbus = 1;
648 else if (strcasecmp(p->key, "allow-interfaces") == 0) {
651 avahi_string_list_free(c->server_config.allow_interfaces);
652 c->server_config.allow_interfaces = NULL;
653 e = avahi_split_csv(p->value);
656 c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);
659 } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
662 avahi_string_list_free(c->server_config.deny_interfaces);
663 c->server_config.deny_interfaces = NULL;
664 e = avahi_split_csv(p->value);
667 c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);
670 } else if (strcasecmp(p->key, "ratelimit-interval-usec") == 0) {
673 if (parse_usec(p->value, &k) < 0) {
674 avahi_log_error("Invalid ratelimit-interval-usec setting %s", p->value);
678 c->server_config.ratelimit_interval = k;
680 } else if (strcasecmp(p->key, "ratelimit-burst") == 0) {
683 if (parse_unsigned(p->value, &k) < 0) {
684 avahi_log_error("Invalid ratelimit-burst setting %s", p->value);
688 c->server_config.ratelimit_burst = k;
690 } else if (strcasecmp(p->key, "cache-entries-max") == 0) {
693 if (parse_unsigned(p->value, &k) < 0) {
694 avahi_log_error("Invalid cache-entries-max setting %s", p->value);
698 c->server_config.n_cache_entries_max = k;
700 } else if (strcasecmp(p->key, "clients-max") == 0) {
703 if (parse_unsigned(p->value, &k) < 0) {
704 avahi_log_error("Invalid clients-max setting %s", p->value);
708 c->n_clients_max = k;
709 } else if (strcasecmp(p->key, "objects-per-client-max") == 0) {
712 if (parse_unsigned(p->value, &k) < 0) {
713 avahi_log_error("Invalid objects-per-client-max setting %s", p->value);
717 c->n_objects_per_client_max = k;
718 } else if (strcasecmp(p->key, "entries-per-entry-group-max") == 0) {
721 if (parse_unsigned(p->value, &k) < 0) {
722 avahi_log_error("Invalid entries-per-entry-group-max setting %s", p->value);
726 c->n_entries_per_entry_group_max = k;
729 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
734 } else if (strcasecmp(g->name, "publish") == 0) {
737 for (p = g->pairs; p; p = p->pairs_next) {
739 if (strcasecmp(p->key, "publish-addresses") == 0)
740 c->server_config.publish_addresses = is_yes(p->value);
741 else if (strcasecmp(p->key, "publish-hinfo") == 0)
742 c->server_config.publish_hinfo = is_yes(p->value);
743 else if (strcasecmp(p->key, "publish-workstation") == 0)
744 c->server_config.publish_workstation = is_yes(p->value);
745 else if (strcasecmp(p->key, "publish-domain") == 0)
746 c->server_config.publish_domain = is_yes(p->value);
747 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
748 c->publish_resolv_conf = is_yes(p->value);
749 else if (strcasecmp(p->key, "disable-publishing") == 0)
750 c->server_config.disable_publishing = is_yes(p->value);
751 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
752 c->disable_user_service_publishing = is_yes(p->value);
753 else if (strcasecmp(p->key, "add-service-cookie") == 0)
754 c->server_config.add_service_cookie = is_yes(p->value);
755 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
756 avahi_strfreev(c->publish_dns_servers);
757 c->publish_dns_servers = avahi_split_csv(p->value);
758 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
759 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
760 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
761 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
763 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
768 } else if (strcasecmp(g->name, "wide-area") == 0) {
771 for (p = g->pairs; p; p = p->pairs_next) {
773 if (strcasecmp(p->key, "enable-wide-area") == 0)
774 c->server_config.enable_wide_area = is_yes(p->value);
776 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
781 } else if (strcasecmp(g->name, "reflector") == 0) {
784 for (p = g->pairs; p; p = p->pairs_next) {
786 if (strcasecmp(p->key, "enable-reflector") == 0)
787 c->server_config.enable_reflector = is_yes(p->value);
788 else if (strcasecmp(p->key, "reflect-ipv") == 0)
789 c->server_config.reflect_ipv = is_yes(p->value);
791 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
796 } else if (strcasecmp(g->name, "rlimits") == 0) {
799 for (p = g->pairs; p; p = p->pairs_next) {
801 if (strcasecmp(p->key, "rlimit-as") == 0) {
802 c->rlimit_as_set = 1;
803 c->rlimit_as = atoi(p->value);
804 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
805 c->rlimit_core_set = 1;
806 c->rlimit_core = atoi(p->value);
807 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
808 c->rlimit_data_set = 1;
809 c->rlimit_data = atoi(p->value);
810 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
811 c->rlimit_fsize_set = 1;
812 c->rlimit_fsize = atoi(p->value);
813 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
814 c->rlimit_nofile_set = 1;
815 c->rlimit_nofile = atoi(p->value);
816 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
817 c->rlimit_stack_set = 1;
818 c->rlimit_stack = atoi(p->value);
819 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
821 c->rlimit_nproc_set = 1;
822 c->rlimit_nproc = atoi(p->value);
824 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
827 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
834 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
844 avahi_ini_file_free(f);
849 static void log_function(AvahiLogLevel level, const char *txt) {
851 static const int log_level_map[] = {
859 assert(level < AVAHI_LOG_LEVEL_MAX);
862 if (!config.debug && level == AVAHI_LOG_DEBUG)
865 daemon_log(log_level_map[level], "%s", txt);
868 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
869 avahi_log_info("%s", text);
874 static int inotify_fd = -1;
876 static void add_inotify_watches(void) {
878 /* We ignore the return values, because one or more of these files
879 * might not exist and we're OK with that. In addition we never
880 * want to remove these watches, hence we keep their ids? */
883 c = config.use_chroot;
886 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
891 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
902 #define NUM_WATCHES 2
905 static int kfds[NUM_WATCHES];
906 static int num_kfds = 0;
908 static void add_kqueue_watch(const char *dir);
910 static void add_kqueue_watches(void) {
914 c = config.use_chroot;
917 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
918 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
921 static void add_kqueue_watch(const char *dir) {
928 if (num_kfds >= NUM_WATCHES)
931 fd = open(dir, O_RDONLY);
934 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
935 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
937 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
942 kfds[num_kfds++] = fd;
947 static void reload_config(void) {
950 /* Refresh in case the config dirs have been removed */
951 add_inotify_watches();
955 add_kqueue_watches();
959 static_service_load(config.use_chroot);
960 static_hosts_load(config.use_chroot);
962 static_service_load(0);
963 static_hosts_load(0);
965 static_service_add_to_server();
966 static_hosts_add_to_server();
968 if (resolv_conf_entry_group)
969 avahi_s_entry_group_reset(resolv_conf_entry_group);
973 update_wide_area_servers();
974 update_browse_domains();
976 if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
977 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
982 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
986 assert(fd == inotify_fd);
989 ioctl(inotify_fd, FIONREAD, &n);
993 buffer = avahi_malloc(n);
994 if (read(inotify_fd, buffer, n) < 0 ) {
996 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
1001 avahi_log_info("Files changed, reloading.");
1009 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
1011 struct timespec nullts = { 0, 0 };
1017 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
1020 /* Sleep for a half-second to avoid potential races
1021 * during install/uninstall. */
1023 avahi_log_info("Files changed, reloading.");
1026 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
1032 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
1034 const AvahiPoll *poll_api;
1037 assert(simple_poll_api);
1039 poll_api = avahi_simple_poll_get(simple_poll_api);
1041 if ((sig = daemon_signal_next()) <= 0) {
1042 avahi_log_error("daemon_signal_next() failed");
1043 poll_api->watch_free(watch);
1052 "Got %s, quitting.",
1053 sig == SIGINT ? "SIGINT" :
1054 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
1055 avahi_simple_poll_quit(simple_poll_api);
1059 avahi_log_info("Got SIGHUP, reloading.");
1065 avahi_log_info("Got SIGUSR1, dumping record data.");
1066 avahi_server_dump(avahi_server, dump, NULL);
1070 avahi_log_warn("Got spurious signal, ignoring.");
1075 /* Imported from ../avahi-client/nss-check.c */
1076 int avahi_nss_support(void);
1078 static void ignore_signal(int sig) {
1079 struct sigaction sa;
1081 memset(&sa, 0, sizeof(sa));
1082 sa.sa_handler = SIG_IGN;
1083 sa.sa_flags = SA_RESTART;
1085 sigaction(sig, &sa, NULL);
1088 static int run_server(DaemonConfig *c) {
1091 const AvahiPoll *poll_api = NULL;
1092 AvahiWatch *sig_watch = NULL;
1093 int retval_is_sent = 0;
1095 AvahiWatch *inotify_watch = NULL;
1099 AvahiWatch *kqueue_watch = NULL;
1104 ignore_signal(SIGPIPE);
1106 if (!(nss_support = avahi_nss_support()))
1107 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
1109 if (!(simple_poll_api = avahi_simple_poll_new())) {
1110 avahi_log_error("Failed to create main loop object.");
1114 poll_api = avahi_simple_poll_get(simple_poll_api);
1116 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
1117 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
1121 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
1122 avahi_log_error( "Failed to create signal watcher");
1126 if (simple_protocol_setup(poll_api) < 0)
1130 if (c->enable_dbus) {
1131 if (dbus_protocol_setup(poll_api,
1132 config.disable_user_service_publishing,
1133 config.n_clients_max,
1134 config.n_objects_per_client_max,
1135 config.n_entries_per_entry_group_max,
1136 !c->fail_on_missing_dbus
1137 #ifdef ENABLE_CHROOT
1138 && !config.use_chroot
1142 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
1144 if (c->fail_on_missing_dbus)
1150 #ifdef ENABLE_CHROOT
1152 if (config.drop_root && config.use_chroot) {
1153 if (chroot(AVAHI_CONFIG_DIR) < 0) {
1154 avahi_log_error("Failed to chroot(): %s", strerror(errno));
1158 avahi_log_info("Successfully called chroot().");
1161 if (avahi_caps_drop_all() < 0) {
1162 avahi_log_error("Failed to drop capabilities.");
1165 avahi_log_info("Successfully dropped remaining capabilities.");
1171 if ((inotify_fd = inotify_init()) < 0)
1172 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
1174 add_inotify_watches();
1176 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
1177 avahi_log_error( "Failed to create inotify watcher");
1184 if ((kq = kqueue()) < 0)
1185 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
1187 add_kqueue_watches();
1189 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
1190 avahi_log_error( "Failed to create kqueue watcher");
1197 #ifdef ENABLE_CHROOT
1198 static_service_load(config.use_chroot);
1199 static_hosts_load(config.use_chroot);
1201 static_service_load(0);
1202 static_hosts_load(0);
1205 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
1206 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
1210 update_wide_area_servers();
1211 update_browse_domains();
1214 daemon_retval_send(0);
1219 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1221 /* We handle signals through an FD, so let's continue */
1225 avahi_log_error("poll(): %s", strerror(errno));
1236 static_service_remove_from_server();
1237 static_service_free_all();
1239 static_hosts_remove_from_server();
1240 static_hosts_free_all();
1242 remove_dns_server_entry_groups();
1244 simple_protocol_shutdown();
1248 dbus_protocol_shutdown();
1252 avahi_server_free(avahi_server);
1253 avahi_server = NULL;
1256 daemon_signal_done();
1259 poll_api->watch_free(sig_watch);
1263 poll_api->watch_free(inotify_watch);
1264 if (inotify_fd >= 0)
1270 poll_api->watch_free(kqueue_watch);
1273 for (i = 0; i < num_kfds; i++) {
1279 if (simple_poll_api) {
1280 avahi_simple_poll_free(simple_poll_api);
1281 simple_poll_api = NULL;
1284 if (!retval_is_sent && c->daemonize)
1285 daemon_retval_send(1);
1290 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1292 static int drop_root(void) {
1297 if (!(pw = getpwnam(AVAHI_USER))) {
1298 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1302 if (!(gr = getgrnam(AVAHI_GROUP))) {
1303 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1307 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);
1309 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1310 avahi_log_error("Failed to change group list: %s", strerror(errno));
1314 #if defined(HAVE_SETRESGID)
1315 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1316 #elif defined(HAVE_SETEGID)
1317 if ((r = setgid(gr->gr_gid)) >= 0)
1318 r = setegid(gr->gr_gid);
1319 #elif defined(HAVE_SETREGID)
1320 r = setregid(gr->gr_gid, gr->gr_gid);
1322 #error "No API to drop privileges"
1326 avahi_log_error("Failed to change GID: %s", strerror(errno));
1330 #if defined(HAVE_SETRESUID)
1331 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1332 #elif defined(HAVE_SETEUID)
1333 if ((r = setuid(pw->pw_uid)) >= 0)
1334 r = seteuid(pw->pw_uid);
1335 #elif defined(HAVE_SETREUID)
1336 r = setreuid(pw->pw_uid, pw->pw_uid);
1338 #error "No API to drop privileges"
1342 avahi_log_error("Failed to change UID: %s", strerror(errno));
1346 set_env("USER", pw->pw_name);
1347 set_env("LOGNAME", pw->pw_name);
1348 set_env("HOME", pw->pw_dir);
1350 avahi_log_info("Successfully dropped root privileges.");
1355 static const char* pid_file_proc(void) {
1356 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1359 static int make_runtime_dir(void) {
1362 int reset_umask = 0;
1367 if (!(pw = getpwnam(AVAHI_USER))) {
1368 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1372 if (!(gr = getgrnam(AVAHI_GROUP))) {
1373 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1380 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1381 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1385 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1387 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1388 avahi_log_error("stat(): %s\n", strerror(errno));
1392 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1393 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1405 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1407 rl.rlim_cur = rl.rlim_max = limit;
1409 if (setrlimit(resource, &rl) < 0)
1410 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1413 static void enforce_rlimits(void) {
1415 if (config.rlimit_as_set)
1416 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1418 if (config.rlimit_core_set)
1419 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1420 if (config.rlimit_data_set)
1421 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1422 if (config.rlimit_fsize_set)
1423 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1424 if (config.rlimit_nofile_set)
1425 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1426 if (config.rlimit_stack_set)
1427 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1429 if (config.rlimit_nproc_set)
1430 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1433 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1434 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__) && !defined(__FreeBSD_kernel__)
1435 /* We don't need locked memory */
1436 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1440 #define RANDOM_DEVICE "/dev/urandom"
1442 static void init_rand_seed(void) {
1446 /* Try to initialize seed from /dev/urandom, to make it a little
1447 * less predictable, and to make sure that multiple machines
1448 * booted at the same time choose different random seeds. */
1449 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1450 read(fd, &seed, sizeof(seed));
1454 /* If the initialization failed by some reason, we add the time to the seed*/
1455 seed ^= (unsigned) time(NULL);
1460 int main(int argc, char *argv[]) {
1462 int wrote_pid_file = 0;
1464 avahi_set_log_function(log_function);
1468 avahi_server_config_init(&config.server_config);
1469 config.command = DAEMON_RUN;
1470 config.daemonize = 0;
1471 config.config_file = NULL;
1473 config.enable_dbus = 1;
1474 config.fail_on_missing_dbus = 1;
1475 config.n_clients_max = 0;
1476 config.n_objects_per_client_max = 0;
1477 config.n_entries_per_entry_group_max = 0;
1480 config.drop_root = 1;
1481 config.set_rlimits = 1;
1482 #ifdef ENABLE_CHROOT
1483 config.use_chroot = 1;
1485 config.modify_proc_title = 1;
1487 config.disable_user_service_publishing = 0;
1488 config.publish_dns_servers = NULL;
1489 config.publish_resolv_conf = 0;
1490 config.use_syslog = 0;
1492 config.rlimit_as_set = 0;
1493 config.rlimit_core_set = 0;
1494 config.rlimit_data_set = 0;
1495 config.rlimit_fsize_set = 0;
1496 config.rlimit_nofile_set = 0;
1497 config.rlimit_stack_set = 0;
1499 config.rlimit_nproc_set = 0;
1502 if ((argv0 = strrchr(argv[0], '/')))
1503 argv0 = avahi_strdup(argv0 + 1);
1505 argv0 = avahi_strdup(argv[0]);
1507 daemon_pid_file_ident = (const char *) argv0;
1508 daemon_log_ident = (char*) argv0;
1509 daemon_pid_file_proc = pid_file_proc;
1511 if (parse_command_line(&config, argc, argv) < 0)
1514 if (config.modify_proc_title)
1515 avahi_init_proc_title(argc, argv);
1517 #ifdef ENABLE_CHROOT
1518 config.use_chroot = config.use_chroot && config.drop_root;
1521 if (config.command == DAEMON_HELP) {
1524 } else if (config.command == DAEMON_VERSION) {
1525 printf("%s "PACKAGE_VERSION"\n", argv0);
1527 } else if (config.command == DAEMON_KILL) {
1528 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1529 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1535 } else if (config.command == DAEMON_RELOAD) {
1536 if (daemon_pid_file_kill(SIGHUP) < 0) {
1537 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1543 } else if (config.command == DAEMON_CHECK)
1544 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1545 else if (config.command == DAEMON_RUN) {
1548 if (getuid() != 0 && config.drop_root) {
1549 avahi_log_error("This program is intended to be run as root.");
1553 if ((pid = daemon_pid_file_is_running()) >= 0) {
1554 avahi_log_error("Daemon already running on PID %u", pid);
1558 if (load_config_file(&config) < 0)
1561 if (config.daemonize) {
1562 daemon_retval_init();
1564 if ((pid = daemon_fork()) < 0)
1566 else if (pid != 0) {
1570 if ((ret = daemon_retval_wait(20)) < 0) {
1571 avahi_log_error("Could not receive return value from daemon process.");
1582 if (config.use_syslog || config.daemonize)
1583 daemon_log_use = DAEMON_LOG_SYSLOG;
1585 if (sd_listen_fds(0) <= 0)
1586 if (daemon_close_all(-1) < 0)
1587 avahi_log_warn("Failed to close all remaining file descriptors: %s", strerror(errno));
1589 daemon_reset_sigs(-1);
1590 daemon_unblock_sigs(-1);
1592 if (make_runtime_dir() < 0)
1595 if (config.drop_root) {
1596 #ifdef ENABLE_CHROOT
1597 if (config.use_chroot)
1598 if (avahi_caps_reduce() < 0)
1602 if (drop_root() < 0)
1605 #ifdef ENABLE_CHROOT
1606 if (config.use_chroot)
1607 if (avahi_caps_reduce2() < 0)
1612 if (daemon_pid_file_create() < 0) {
1613 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1615 if (config.daemonize)
1616 daemon_retval_send(1);
1621 if (config.set_rlimits)
1626 #ifdef ENABLE_CHROOT
1627 if (config.drop_root && config.use_chroot)
1628 if (avahi_chroot_helper_start(argv0) < 0) {
1629 avahi_log_error("failed to start chroot() helper daemon.");
1633 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1634 sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" starting up.", argv0);
1635 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1637 if (run_server(&config) == 0)
1640 avahi_log_info("%s "PACKAGE_VERSION" exiting.", argv0);
1641 sd_notifyf(0, "STATUS=%s "PACKAGE_VERSION" exiting.", argv0);
1646 if (config.daemonize)
1647 daemon_retval_done();
1649 avahi_server_config_free(&config.server_config);
1650 avahi_free(config.config_file);
1651 avahi_strfreev(config.publish_dns_servers);
1652 avahi_strfreev(resolv_conf_name_servers);
1653 avahi_strfreev(resolv_conf_search_domains);
1655 if (wrote_pid_file) {
1656 #ifdef ENABLE_CHROOT
1657 avahi_chroot_helper_unlink(pid_file_proc());
1659 daemon_pid_file_remove();
1663 #ifdef ENABLE_CHROOT
1664 avahi_chroot_helper_shutdown();