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"
82 #include "sd-daemon.h"
85 #include "dbus-protocol.h"
88 AvahiServer *avahi_server = NULL;
89 AvahiSimplePoll *simple_poll_api = NULL;
90 static char *argv0 = NULL;
103 AvahiServerConfig server_config;
104 DaemonCommand command;
110 int fail_on_missing_dbus;
117 int modify_proc_title;
119 int disable_user_service_publishing;
120 int publish_resolv_conf;
121 char ** publish_dns_servers;
124 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
125 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
128 int rlimit_nproc_set;
133 #define RESOLV_CONF "/etc/resolv.conf"
134 #define BROWSE_DOMAINS_MAX 16
136 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
137 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
139 static char **resolv_conf_name_servers = NULL;
140 static char **resolv_conf_search_domains = NULL;
142 static DaemonConfig config;
144 static int has_prefix(const char *s, const char *prefix) {
149 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
152 static int load_resolv_conf(void) {
157 avahi_strfreev(resolv_conf_name_servers);
158 resolv_conf_name_servers = NULL;
160 avahi_strfreev(resolv_conf_search_domains);
161 resolv_conf_search_domains = NULL;
164 f = avahi_chroot_helper_get_file(RESOLV_CONF);
166 f = fopen(RESOLV_CONF, "r");
170 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
174 resolv_conf_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
175 resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);
181 if (!(fgets(ln, sizeof(ln), f)))
184 ln[strcspn(ln, "\r\n#")] = 0;
185 p = ln + strspn(ln, "\t ");
187 if ((has_prefix(p, "nameserver ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
189 p += strspn(p, "\t ");
190 p[strcspn(p, "\t ")] = 0;
191 resolv_conf_name_servers[i++] = avahi_strdup(p);
194 if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
195 has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {
199 while (j < BROWSE_DOMAINS_MAX) {
202 p += strspn(p, "\t ");
203 k = strcspn(p, "\t ");
206 resolv_conf_search_domains[j++] = avahi_strndup(p, k);
221 avahi_strfreev(resolv_conf_name_servers);
222 resolv_conf_name_servers = NULL;
224 avahi_strfreev(resolv_conf_search_domains);
225 resolv_conf_search_domains = NULL;
234 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
241 g = avahi_s_entry_group_new(s, NULL, NULL);
243 assert(avahi_s_entry_group_is_empty(g));
245 for (p = l; *p; p++) {
248 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
249 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
251 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
252 avahi_s_entry_group_free(g);
253 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
258 avahi_s_entry_group_commit(g);
263 static void remove_dns_server_entry_groups(void) {
265 if (resolv_conf_entry_group)
266 avahi_s_entry_group_reset(resolv_conf_entry_group);
268 if (dns_servers_entry_group)
269 avahi_s_entry_group_reset(dns_servers_entry_group);
272 static void update_wide_area_servers(void) {
273 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
277 if (!resolv_conf_name_servers) {
278 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
282 for (p = resolv_conf_name_servers; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
283 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
284 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
289 avahi_server_set_wide_area_servers(avahi_server, a, n);
292 static AvahiStringList *filter_duplicate_domains(AvahiStringList *l) {
293 AvahiStringList *e, *n, *p;
298 for (p = l, e = l->next; e; e = n) {
301 if (avahi_domain_equal((char*) e->text, (char*) l->text)) {
308 l->next = filter_duplicate_domains(l->next);
312 static void update_browse_domains(void) {
317 if (!resolv_conf_search_domains) {
318 avahi_server_set_browse_domains(avahi_server, NULL);
322 l = avahi_string_list_copy(config.server_config.browse_domains);
324 for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
325 if (!avahi_is_valid_domain_name(*p))
326 avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
328 l = avahi_string_list_add(l, *p);
331 l = filter_duplicate_domains(l);
333 avahi_server_set_browse_domains(avahi_server, l);
336 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
337 DaemonConfig *c = userdata;
342 /* This function is possibly called before the global variable
343 * avahi_server has been set, therefore we do it explicitly */
348 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
349 dbus_protocol_server_state_changed(state);
353 case AVAHI_SERVER_RUNNING:
354 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));
356 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
358 static_service_add_to_server();
359 static_hosts_add_to_server();
361 remove_dns_server_entry_groups();
363 if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
364 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
366 if (c->publish_dns_servers && c->publish_dns_servers[0])
367 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
369 simple_protocol_restart_queries();
372 case AVAHI_SERVER_COLLISION: {
375 avahi_set_proc_title(argv0, "%s: collision", argv0);
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));
382 avahi_log_warn("Host name conflict, retrying with <%s>", n);
383 avahi_server_set_host_name(s, n);
389 case AVAHI_SERVER_FAILURE:
391 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
392 avahi_simple_poll_quit(simple_poll_api);
395 case AVAHI_SERVER_REGISTERING:
397 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
399 static_service_remove_from_server();
400 static_hosts_remove_from_server();
401 remove_dns_server_entry_groups();
405 case AVAHI_SERVER_INVALID:
411 static void help(FILE *f) {
414 " -h --help Show this help\n"
415 " -D --daemonize Daemonize after startup (implies -s)\n"
416 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
417 " -k --kill Kill a running daemon\n"
418 " -r --reload Request a running daemon to reload static services\n"
419 " -c --check Return 0 if a daemon is already running\n"
420 " -V --version Show version\n"
421 " -f --file=FILE Load the specified configuration file instead of\n"
422 " "AVAHI_CONFIG_FILE"\n"
423 " --no-rlimits Don't enforce resource limits\n"
424 " --no-drop-root Don't drop privileges\n"
426 " --no-chroot Don't chroot()\n"
428 " --no-proc-title Don't modify process title\n"
429 " --debug Increase verbosity\n",
434 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
438 OPTION_NO_RLIMITS = 256,
443 OPTION_NO_PROC_TITLE,
447 static const struct option long_options[] = {
448 { "help", no_argument, NULL, 'h' },
449 { "daemonize", no_argument, NULL, 'D' },
450 { "kill", no_argument, NULL, 'k' },
451 { "version", no_argument, NULL, 'V' },
452 { "file", required_argument, NULL, 'f' },
453 { "reload", no_argument, NULL, 'r' },
454 { "check", no_argument, NULL, 'c' },
455 { "syslog", no_argument, NULL, 's' },
456 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
457 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
459 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
461 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
462 { "debug", no_argument, NULL, OPTION_DEBUG },
468 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
475 c->command = DAEMON_HELP;
481 c->command = DAEMON_KILL;
484 c->command = DAEMON_VERSION;
487 avahi_free(c->config_file);
488 c->config_file = avahi_strdup(optarg);
491 c->command = DAEMON_RELOAD;
494 c->command = DAEMON_CHECK;
496 case OPTION_NO_RLIMITS:
499 case OPTION_NO_DROP_ROOT:
503 case OPTION_NO_CHROOT:
507 case OPTION_NO_PROC_TITLE:
508 c->modify_proc_title = 0;
519 fprintf(stderr, "Too many arguments\n");
526 static int is_yes(const char *s) {
529 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
532 static int load_config_file(DaemonConfig *c) {
535 AvahiIniFileGroup *g;
539 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
542 for (g = f->groups; g; g = g->groups_next) {
544 if (strcasecmp(g->name, "server") == 0) {
547 for (p = g->pairs; p; p = p->pairs_next) {
549 if (strcasecmp(p->key, "host-name") == 0) {
550 avahi_free(c->server_config.host_name);
551 c->server_config.host_name = avahi_strdup(p->value);
552 } else if (strcasecmp(p->key, "domain-name") == 0) {
553 avahi_free(c->server_config.domain_name);
554 c->server_config.domain_name = avahi_strdup(p->value);
555 } else if (strcasecmp(p->key, "browse-domains") == 0) {
558 e = avahi_split_csv(p->value);
560 for (t = e; *t; t++) {
561 char cleaned[AVAHI_DOMAIN_NAME_MAX];
563 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
564 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
569 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
574 c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
575 } else if (strcasecmp(p->key, "use-ipv4") == 0)
576 c->server_config.use_ipv4 = is_yes(p->value);
577 else if (strcasecmp(p->key, "use-ipv6") == 0)
578 c->server_config.use_ipv6 = is_yes(p->value);
579 else if (strcasecmp(p->key, "check-response-ttl") == 0)
580 c->server_config.check_response_ttl = is_yes(p->value);
581 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
582 c->server_config.allow_point_to_point = is_yes(p->value);
583 else if (strcasecmp(p->key, "use-iff-running") == 0)
584 c->server_config.use_iff_running = is_yes(p->value);
585 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
586 c->server_config.disallow_other_stacks = is_yes(p->value);
588 else if (strcasecmp(p->key, "enable-dbus") == 0) {
590 if (*(p->value) == 'w' || *(p->value) == 'W') {
591 c->fail_on_missing_dbus = 0;
593 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
594 c->fail_on_missing_dbus = 1;
601 else if (strcasecmp(p->key, "allow-interfaces") == 0) {
604 avahi_string_list_free(c->server_config.allow_interfaces);
605 c->server_config.allow_interfaces = NULL;
606 e = avahi_split_csv(p->value);
609 c->server_config.allow_interfaces = avahi_string_list_add(c->server_config.allow_interfaces, *t);
612 } else if (strcasecmp(p->key, "deny-interfaces") == 0) {
615 avahi_string_list_free(c->server_config.deny_interfaces);
616 c->server_config.deny_interfaces = NULL;
617 e = avahi_split_csv(p->value);
620 c->server_config.deny_interfaces = avahi_string_list_add(c->server_config.deny_interfaces, *t);
624 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
629 } else if (strcasecmp(g->name, "publish") == 0) {
632 for (p = g->pairs; p; p = p->pairs_next) {
634 if (strcasecmp(p->key, "publish-addresses") == 0)
635 c->server_config.publish_addresses = is_yes(p->value);
636 else if (strcasecmp(p->key, "publish-hinfo") == 0)
637 c->server_config.publish_hinfo = is_yes(p->value);
638 else if (strcasecmp(p->key, "publish-workstation") == 0)
639 c->server_config.publish_workstation = is_yes(p->value);
640 else if (strcasecmp(p->key, "publish-domain") == 0)
641 c->server_config.publish_domain = is_yes(p->value);
642 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
643 c->publish_resolv_conf = is_yes(p->value);
644 else if (strcasecmp(p->key, "disable-publishing") == 0)
645 c->server_config.disable_publishing = is_yes(p->value);
646 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
647 c->disable_user_service_publishing = is_yes(p->value);
648 else if (strcasecmp(p->key, "add-service-cookie") == 0)
649 c->server_config.add_service_cookie = is_yes(p->value);
650 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
651 avahi_strfreev(c->publish_dns_servers);
652 c->publish_dns_servers = avahi_split_csv(p->value);
653 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
654 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
655 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
656 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
658 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
663 } else if (strcasecmp(g->name, "wide-area") == 0) {
666 for (p = g->pairs; p; p = p->pairs_next) {
668 if (strcasecmp(p->key, "enable-wide-area") == 0)
669 c->server_config.enable_wide_area = is_yes(p->value);
671 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
676 } else if (strcasecmp(g->name, "reflector") == 0) {
679 for (p = g->pairs; p; p = p->pairs_next) {
681 if (strcasecmp(p->key, "enable-reflector") == 0)
682 c->server_config.enable_reflector = is_yes(p->value);
683 else if (strcasecmp(p->key, "reflect-ipv") == 0)
684 c->server_config.reflect_ipv = is_yes(p->value);
686 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
691 } else if (strcasecmp(g->name, "rlimits") == 0) {
694 for (p = g->pairs; p; p = p->pairs_next) {
696 if (strcasecmp(p->key, "rlimit-as") == 0) {
697 c->rlimit_as_set = 1;
698 c->rlimit_as = atoi(p->value);
699 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
700 c->rlimit_core_set = 1;
701 c->rlimit_core = atoi(p->value);
702 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
703 c->rlimit_data_set = 1;
704 c->rlimit_data = atoi(p->value);
705 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
706 c->rlimit_fsize_set = 1;
707 c->rlimit_fsize = atoi(p->value);
708 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
709 c->rlimit_nofile_set = 1;
710 c->rlimit_nofile = atoi(p->value);
711 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
712 c->rlimit_stack_set = 1;
713 c->rlimit_stack = atoi(p->value);
714 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
716 c->rlimit_nproc_set = 1;
717 c->rlimit_nproc = atoi(p->value);
719 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
722 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
729 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
739 avahi_ini_file_free(f);
744 static void log_function(AvahiLogLevel level, const char *txt) {
746 static const int log_level_map[] = {
754 assert(level < AVAHI_LOG_LEVEL_MAX);
757 if (!config.debug && level == AVAHI_LOG_DEBUG)
760 daemon_log(log_level_map[level], "%s", txt);
763 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
764 avahi_log_info("%s", text);
769 static int inotify_fd = -1;
771 static void add_inotify_watches(void) {
773 /* We ignore the return values, because one or more of these files
774 * might not exist and we're OK with that. In addition we never
775 * want to remove these watches, hence we keep their ids? */
778 c = config.use_chroot;
781 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
786 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
797 #define NUM_WATCHES 2
800 static int kfds[NUM_WATCHES];
801 static int num_kfds = 0;
803 static void add_kqueue_watch(const char *dir);
805 static void add_kqueue_watches(void) {
809 c = config.use_chroot;
812 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
813 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
816 static void add_kqueue_watch(const char *dir) {
823 if (num_kfds >= NUM_WATCHES)
826 fd = open(dir, O_RDONLY);
829 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
830 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
832 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
837 kfds[num_kfds++] = fd;
842 static void reload_config(void) {
845 /* Refresh in case the config dirs have been removed */
846 add_inotify_watches();
850 add_kqueue_watches();
854 static_service_load(config.use_chroot);
855 static_hosts_load(config.use_chroot);
857 static_service_load(0);
858 static_hosts_load(0);
860 static_service_add_to_server();
861 static_hosts_add_to_server();
863 if (resolv_conf_entry_group)
864 avahi_s_entry_group_reset(resolv_conf_entry_group);
868 update_wide_area_servers();
869 update_browse_domains();
871 if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
872 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
877 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
881 assert(fd == inotify_fd);
884 ioctl(inotify_fd, FIONREAD, &n);
888 buffer = avahi_malloc(n);
889 if (read(inotify_fd, buffer, n) < 0 ) {
891 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
896 avahi_log_info("Files changed, reloading.");
904 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
906 struct timespec nullts = { 0, 0 };
912 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
915 /* Sleep for a half-second to avoid potential races
916 * during install/uninstall. */
918 avahi_log_info("Files changed, reloading.");
921 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
927 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
929 const AvahiPoll *poll_api;
932 assert(simple_poll_api);
934 poll_api = avahi_simple_poll_get(simple_poll_api);
936 if ((sig = daemon_signal_next()) <= 0) {
937 avahi_log_error("daemon_signal_next() failed");
938 poll_api->watch_free(watch);
948 sig == SIGINT ? "SIGINT" :
949 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
950 avahi_simple_poll_quit(simple_poll_api);
954 avahi_log_info("Got SIGHUP, reloading.");
960 avahi_log_info("Got SIGUSR1, dumping record data.");
961 avahi_server_dump(avahi_server, dump, NULL);
965 avahi_log_warn("Got spurious signal, ignoring.");
970 /* Imported from ../avahi-client/nss-check.c */
971 int avahi_nss_support(void);
973 static int run_server(DaemonConfig *c) {
976 const AvahiPoll *poll_api = NULL;
977 AvahiWatch *sig_watch = NULL;
978 int retval_is_sent = 0;
980 AvahiWatch *inotify_watch = NULL;
984 AvahiWatch *kqueue_watch = NULL;
989 if (!(nss_support = avahi_nss_support()))
990 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
992 if (!(simple_poll_api = avahi_simple_poll_new())) {
993 avahi_log_error("Failed to create main loop object.");
997 poll_api = avahi_simple_poll_get(simple_poll_api);
999 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
1000 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
1004 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
1005 avahi_log_error( "Failed to create signal watcher");
1009 if (simple_protocol_setup(poll_api) < 0)
1013 if (c->enable_dbus) {
1014 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, !c->fail_on_missing_dbus
1015 #ifdef ENABLE_CHROOT
1016 && !config.use_chroot
1020 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
1022 if (c->fail_on_missing_dbus)
1028 #ifdef ENABLE_CHROOT
1030 if (config.drop_root && config.use_chroot) {
1031 if (chroot(AVAHI_CONFIG_DIR) < 0) {
1032 avahi_log_error("Failed to chroot(): %s", strerror(errno));
1036 avahi_log_info("Successfully called chroot().");
1039 if (avahi_caps_drop_all() < 0) {
1040 avahi_log_error("Failed to drop capabilities.");
1043 avahi_log_info("Successfully dropped remaining capabilities.");
1049 if ((inotify_fd = inotify_init()) < 0)
1050 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
1052 add_inotify_watches();
1054 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
1055 avahi_log_error( "Failed to create inotify watcher");
1062 if ((kq = kqueue()) < 0)
1063 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
1065 add_kqueue_watches();
1067 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
1068 avahi_log_error( "Failed to create kqueue watcher");
1075 #ifdef ENABLE_CHROOT
1076 static_service_load(config.use_chroot);
1077 static_hosts_load(config.use_chroot);
1079 static_service_load(0);
1080 static_hosts_load(0);
1083 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
1084 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
1088 update_wide_area_servers();
1089 update_browse_domains();
1092 daemon_retval_send(0);
1097 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1099 /* We handle signals through an FD, so let's continue */
1103 avahi_log_error("poll(): %s", strerror(errno));
1113 static_service_remove_from_server();
1114 static_service_free_all();
1116 static_hosts_remove_from_server();
1117 static_hosts_free_all();
1119 remove_dns_server_entry_groups();
1121 simple_protocol_shutdown();
1125 dbus_protocol_shutdown();
1129 avahi_server_free(avahi_server);
1130 avahi_server = NULL;
1133 daemon_signal_done();
1136 poll_api->watch_free(sig_watch);
1140 poll_api->watch_free(inotify_watch);
1141 if (inotify_fd >= 0)
1147 poll_api->watch_free(kqueue_watch);
1150 for (i = 0; i < num_kfds; i++) {
1156 if (simple_poll_api) {
1157 avahi_simple_poll_free(simple_poll_api);
1158 simple_poll_api = NULL;
1161 if (!retval_is_sent && c->daemonize)
1162 daemon_retval_send(1);
1167 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1169 static int drop_root(void) {
1174 if (!(pw = getpwnam(AVAHI_USER))) {
1175 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1179 if (!(gr = getgrnam(AVAHI_GROUP))) {
1180 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1184 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);
1186 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1187 avahi_log_error("Failed to change group list: %s", strerror(errno));
1191 #if defined(HAVE_SETRESGID)
1192 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1193 #elif defined(HAVE_SETEGID)
1194 if ((r = setgid(gr->gr_gid)) >= 0)
1195 r = setegid(gr->gr_gid);
1196 #elif defined(HAVE_SETREGID)
1197 r = setregid(gr->gr_gid, gr->gr_gid);
1199 #error "No API to drop privileges"
1203 avahi_log_error("Failed to change GID: %s", strerror(errno));
1207 #if defined(HAVE_SETRESUID)
1208 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1209 #elif defined(HAVE_SETEUID)
1210 if ((r = setuid(pw->pw_uid)) >= 0)
1211 r = seteuid(pw->pw_uid);
1212 #elif defined(HAVE_SETREUID)
1213 r = setreuid(pw->pw_uid, pw->pw_uid);
1215 #error "No API to drop privileges"
1219 avahi_log_error("Failed to change UID: %s", strerror(errno));
1223 set_env("USER", pw->pw_name);
1224 set_env("LOGNAME", pw->pw_name);
1225 set_env("HOME", pw->pw_dir);
1227 avahi_log_info("Successfully dropped root privileges.");
1232 static const char* pid_file_proc(void) {
1233 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1236 static int make_runtime_dir(void) {
1239 int reset_umask = 0;
1244 if (!(pw = getpwnam(AVAHI_USER))) {
1245 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1249 if (!(gr = getgrnam(AVAHI_GROUP))) {
1250 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1257 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1258 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1262 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1264 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1265 avahi_log_error("stat(): %s\n", strerror(errno));
1269 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1270 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1282 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1284 rl.rlim_cur = rl.rlim_max = limit;
1286 if (setrlimit(resource, &rl) < 0)
1287 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1290 static void enforce_rlimits(void) {
1292 if (config.rlimit_as_set)
1293 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1295 if (config.rlimit_core_set)
1296 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1297 if (config.rlimit_data_set)
1298 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1299 if (config.rlimit_fsize_set)
1300 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1301 if (config.rlimit_nofile_set)
1302 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1303 if (config.rlimit_stack_set)
1304 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1306 if (config.rlimit_nproc_set)
1307 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1310 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1311 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
1312 /* We don't need locked memory */
1313 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1317 #define RANDOM_DEVICE "/dev/urandom"
1319 static void init_rand_seed(void) {
1323 /* Try to initialize seed from /dev/urandom, to make it a little
1324 * less predictable, and to make sure that multiple machines
1325 * booted at the same time choose different random seeds. */
1326 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1327 read(fd, &seed, sizeof(seed));
1331 /* If the initialization failed by some reason, we add the time to the seed*/
1332 seed ^= (unsigned) time(NULL);
1337 int main(int argc, char *argv[]) {
1339 int wrote_pid_file = 0;
1341 avahi_set_log_function(log_function);
1345 avahi_server_config_init(&config.server_config);
1346 config.command = DAEMON_RUN;
1347 config.daemonize = 0;
1348 config.config_file = NULL;
1350 config.enable_dbus = 1;
1351 config.fail_on_missing_dbus = 1;
1354 config.drop_root = 1;
1355 config.set_rlimits = 1;
1356 #ifdef ENABLE_CHROOT
1357 config.use_chroot = 1;
1359 config.modify_proc_title = 1;
1361 config.disable_user_service_publishing = 0;
1362 config.publish_dns_servers = NULL;
1363 config.publish_resolv_conf = 0;
1364 config.use_syslog = 0;
1366 config.rlimit_as_set = 0;
1367 config.rlimit_core_set = 0;
1368 config.rlimit_data_set = 0;
1369 config.rlimit_fsize_set = 0;
1370 config.rlimit_nofile_set = 0;
1371 config.rlimit_stack_set = 0;
1373 config.rlimit_nproc_set = 0;
1376 if ((argv0 = strrchr(argv[0], '/')))
1377 argv0 = avahi_strdup(argv0 + 1);
1379 argv0 = avahi_strdup(argv[0]);
1381 daemon_pid_file_ident = (const char *) argv0;
1382 daemon_log_ident = (char*) argv0;
1383 daemon_pid_file_proc = pid_file_proc;
1385 if (parse_command_line(&config, argc, argv) < 0)
1388 if (config.modify_proc_title)
1389 avahi_init_proc_title(argc, argv);
1391 #ifdef ENABLE_CHROOT
1392 config.use_chroot = config.use_chroot && config.drop_root;
1395 if (config.command == DAEMON_HELP) {
1398 } else if (config.command == DAEMON_VERSION) {
1399 printf("%s "PACKAGE_VERSION"\n", argv0);
1401 } else if (config.command == DAEMON_KILL) {
1402 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1403 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1409 } else if (config.command == DAEMON_RELOAD) {
1410 if (daemon_pid_file_kill(SIGHUP) < 0) {
1411 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1417 } else if (config.command == DAEMON_CHECK)
1418 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1419 else if (config.command == DAEMON_RUN) {
1422 if (getuid() != 0 && config.drop_root) {
1423 avahi_log_error("This program is intended to be run as root.");
1427 if ((pid = daemon_pid_file_is_running()) >= 0) {
1428 avahi_log_error("Daemon already running on PID %u", pid);
1432 if (load_config_file(&config) < 0)
1435 if (config.daemonize) {
1436 daemon_retval_init();
1438 if ((pid = daemon_fork()) < 0)
1440 else if (pid != 0) {
1444 if ((ret = daemon_retval_wait(20)) < 0) {
1445 avahi_log_error("Could not receive return value from daemon process.");
1456 if (config.use_syslog || config.daemonize)
1457 daemon_log_use = DAEMON_LOG_SYSLOG;
1459 if (sd_listen_fds(0) <= 0)
1460 if (daemon_close_all(-1) < 0) {
1461 avahi_log_error("Failed to close remaining file descriptors: %s", strerror(errno));
1465 if (make_runtime_dir() < 0)
1468 if (config.drop_root) {
1469 #ifdef ENABLE_CHROOT
1470 if (config.use_chroot)
1471 if (avahi_caps_reduce() < 0)
1475 if (drop_root() < 0)
1478 #ifdef ENABLE_CHROOT
1479 if (config.use_chroot)
1480 if (avahi_caps_reduce2() < 0)
1485 if (daemon_pid_file_create() < 0) {
1486 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1488 if (config.daemonize)
1489 daemon_retval_send(1);
1494 if (config.set_rlimits)
1499 #ifdef ENABLE_CHROOT
1500 if (config.drop_root && config.use_chroot)
1501 if (avahi_chroot_helper_start(argv0) < 0) {
1502 avahi_log_error("failed to start chroot() helper daemon.");
1506 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1508 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1510 if (run_server(&config) == 0)
1516 if (config.daemonize)
1517 daemon_retval_done();
1519 avahi_server_config_free(&config.server_config);
1520 avahi_free(config.config_file);
1521 avahi_strfreev(config.publish_dns_servers);
1522 avahi_strfreev(resolv_conf_name_servers);
1523 avahi_strfreev(resolv_conf_search_domains);
1525 if (wrote_pid_file) {
1526 #ifdef ENABLE_CHROOT
1527 avahi_chroot_helper_unlink(pid_file_proc());
1529 daemon_pid_file_remove();
1533 #ifdef ENABLE_CHROOT
1534 avahi_chroot_helper_shutdown();