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 #ifdef HAVE_SYS_INOTIFY_H
47 #include <sys/inotify.h>
49 #include "inotify-nosys.h"
54 #include <sys/types.h>
55 #include <sys/event.h>
59 #include <libdaemon/dfork.h>
60 #include <libdaemon/dsignal.h>
61 #include <libdaemon/dlog.h>
62 #include <libdaemon/dpid.h>
64 #include <avahi-common/malloc.h>
65 #include <avahi-common/simple-watch.h>
66 #include <avahi-common/error.h>
67 #include <avahi-common/alternative.h>
68 #include <avahi-common/domain.h>
70 #include <avahi-core/core.h>
71 #include <avahi-core/publish.h>
72 #include <avahi-core/dns-srv-rr.h>
73 #include <avahi-core/log.h>
80 #include "setproctitle.h"
82 #include "simple-protocol.h"
83 #include "static-services.h"
84 #include "static-hosts.h"
85 #include "ini-file-parser.h"
88 #include "dbus-protocol.h"
91 AvahiServer *avahi_server = NULL;
92 AvahiSimplePoll *simple_poll_api = NULL;
93 static char *argv0 = NULL;
106 AvahiServerConfig server_config;
107 DaemonCommand command;
113 int fail_on_missing_dbus;
120 int modify_proc_title;
122 int disable_user_service_publishing;
123 int publish_resolv_conf;
124 char ** publish_dns_servers;
127 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
128 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
131 int rlimit_nproc_set;
136 #define RESOLV_CONF "/etc/resolv.conf"
137 #define BROWSE_DOMAINS_MAX 16
139 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
140 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
142 static char **resolv_conf_name_servers = NULL;
143 static char **resolv_conf_search_domains = NULL;
145 static DaemonConfig config;
147 static int has_prefix(const char *s, const char *prefix) {
152 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
155 static int load_resolv_conf(void) {
160 avahi_strfreev(resolv_conf_name_servers);
161 resolv_conf_name_servers = NULL;
163 avahi_strfreev(resolv_conf_search_domains);
164 resolv_conf_search_domains = NULL;
167 f = avahi_chroot_helper_get_file(RESOLV_CONF);
169 f = fopen(RESOLV_CONF, "r");
173 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
177 resolv_conf_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
178 resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);
184 if (!(fgets(ln, sizeof(ln), f)))
187 ln[strcspn(ln, "\r\n#")] = 0;
188 p = ln + strspn(ln, "\t ");
190 if ((has_prefix(p, "nameserver ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
192 p += strspn(p, "\t ");
193 p[strcspn(p, "\t ")] = 0;
194 resolv_conf_name_servers[i++] = avahi_strdup(p);
197 if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
198 has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {
202 while (j < BROWSE_DOMAINS_MAX) {
205 p += strspn(p, "\t ");
206 k = strcspn(p, "\t ");
209 resolv_conf_search_domains[j++] = avahi_strndup(p, k);
224 avahi_strfreev(resolv_conf_name_servers);
225 resolv_conf_name_servers = NULL;
227 avahi_strfreev(resolv_conf_search_domains);
228 resolv_conf_search_domains = NULL;
237 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
244 g = avahi_s_entry_group_new(s, NULL, NULL);
246 assert(avahi_s_entry_group_is_empty(g));
248 for (p = l; *p; p++) {
251 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
252 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
254 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
255 avahi_s_entry_group_free(g);
256 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
261 avahi_s_entry_group_commit(g);
266 static void remove_dns_server_entry_groups(void) {
268 if (resolv_conf_entry_group)
269 avahi_s_entry_group_reset(resolv_conf_entry_group);
271 if (dns_servers_entry_group)
272 avahi_s_entry_group_reset(dns_servers_entry_group);
275 static void update_wide_area_servers(void) {
276 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
280 if (!resolv_conf_name_servers) {
281 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
285 for (p = resolv_conf_name_servers; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
286 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
287 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
292 avahi_server_set_wide_area_servers(avahi_server, a, n);
295 static AvahiStringList *filter_duplicate_domains(AvahiStringList *l) {
296 AvahiStringList *e, *n, *p;
301 for (p = l, e = l->next; e; e = n) {
304 if (avahi_domain_equal((char*) e->text, (char*) l->text)) {
311 l->next = filter_duplicate_domains(l->next);
315 static void update_browse_domains(void) {
320 if (!resolv_conf_search_domains) {
321 avahi_server_set_browse_domains(avahi_server, NULL);
325 l = avahi_string_list_copy(config.server_config.browse_domains);
327 for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
328 if (!avahi_is_valid_domain_name(*p))
329 avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
331 l = avahi_string_list_add(l, *p);
334 l = filter_duplicate_domains(l);
336 avahi_server_set_browse_domains(avahi_server, l);
339 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
340 DaemonConfig *c = userdata;
345 /* This function is possibly called before the global variable
346 * avahi_server has been set, therefore we do it explicitly */
351 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
352 dbus_protocol_server_state_changed(state);
356 case AVAHI_SERVER_RUNNING:
357 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));
359 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
361 static_service_add_to_server();
362 static_hosts_add_to_server();
364 remove_dns_server_entry_groups();
366 if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
367 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
369 if (c->publish_dns_servers && c->publish_dns_servers[0])
370 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
372 simple_protocol_restart_queries();
375 case AVAHI_SERVER_COLLISION: {
378 avahi_set_proc_title(argv0, "%s: collision", argv0);
380 static_service_remove_from_server();
381 static_hosts_remove_from_server();
382 remove_dns_server_entry_groups();
384 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
385 avahi_log_warn("Host name conflict, retrying with <%s>", n);
386 avahi_server_set_host_name(s, n);
392 case AVAHI_SERVER_FAILURE:
394 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
395 avahi_simple_poll_quit(simple_poll_api);
398 case AVAHI_SERVER_REGISTERING:
400 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
402 static_service_remove_from_server();
403 static_hosts_remove_from_server();
404 remove_dns_server_entry_groups();
408 case AVAHI_SERVER_INVALID:
414 static void help(FILE *f) {
417 " -h --help Show this help\n"
418 " -D --daemonize Daemonize after startup (implies -s)\n"
419 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
420 " -k --kill Kill a running daemon\n"
421 " -r --reload Request a running daemon to reload static services\n"
422 " -c --check Return 0 if a daemon is already running\n"
423 " -V --version Show version\n"
424 " -f --file=FILE Load the specified configuration file instead of\n"
425 " "AVAHI_CONFIG_FILE"\n"
426 " --no-rlimits Don't enforce resource limits\n"
427 " --no-drop-root Don't drop privileges\n"
429 " --no-chroot Don't chroot()\n"
431 " --no-proc-title Don't modify process title\n"
432 " --debug Increase verbosity\n",
437 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
441 OPTION_NO_RLIMITS = 256,
446 OPTION_NO_PROC_TITLE,
450 static const struct option long_options[] = {
451 { "help", no_argument, NULL, 'h' },
452 { "daemonize", no_argument, NULL, 'D' },
453 { "kill", no_argument, NULL, 'k' },
454 { "version", no_argument, NULL, 'V' },
455 { "file", required_argument, NULL, 'f' },
456 { "reload", no_argument, NULL, 'r' },
457 { "check", no_argument, NULL, 'c' },
458 { "syslog", no_argument, NULL, 's' },
459 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
460 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
462 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
464 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
465 { "debug", no_argument, NULL, OPTION_DEBUG },
471 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
478 c->command = DAEMON_HELP;
484 c->command = DAEMON_KILL;
487 c->command = DAEMON_VERSION;
490 avahi_free(c->config_file);
491 c->config_file = avahi_strdup(optarg);
494 c->command = DAEMON_RELOAD;
497 c->command = DAEMON_CHECK;
499 case OPTION_NO_RLIMITS:
502 case OPTION_NO_DROP_ROOT:
506 case OPTION_NO_CHROOT:
510 case OPTION_NO_PROC_TITLE:
511 c->modify_proc_title = 0;
522 fprintf(stderr, "Too many arguments\n");
529 static int is_yes(const char *s) {
532 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
535 static int load_config_file(DaemonConfig *c) {
538 AvahiIniFileGroup *g;
542 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
545 for (g = f->groups; g; g = g->groups_next) {
547 if (strcasecmp(g->name, "server") == 0) {
550 for (p = g->pairs; p; p = p->pairs_next) {
552 if (strcasecmp(p->key, "host-name") == 0) {
553 avahi_free(c->server_config.host_name);
554 c->server_config.host_name = avahi_strdup(p->value);
555 } else if (strcasecmp(p->key, "domain-name") == 0) {
556 avahi_free(c->server_config.domain_name);
557 c->server_config.domain_name = avahi_strdup(p->value);
558 } else if (strcasecmp(p->key, "browse-domains") == 0) {
561 e = avahi_split_csv(p->value);
563 for (t = e; *t; t++) {
564 char cleaned[AVAHI_DOMAIN_NAME_MAX];
566 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
567 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
572 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
577 c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
578 } else if (strcasecmp(p->key, "use-ipv4") == 0)
579 c->server_config.use_ipv4 = is_yes(p->value);
580 else if (strcasecmp(p->key, "use-ipv6") == 0)
581 c->server_config.use_ipv6 = is_yes(p->value);
582 else if (strcasecmp(p->key, "check-response-ttl") == 0)
583 c->server_config.check_response_ttl = is_yes(p->value);
584 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
585 c->server_config.allow_point_to_point = is_yes(p->value);
586 else if (strcasecmp(p->key, "use-iff-running") == 0)
587 c->server_config.use_iff_running = is_yes(p->value);
588 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
589 c->server_config.disallow_other_stacks = is_yes(p->value);
591 else if (strcasecmp(p->key, "enable-dbus") == 0) {
593 if (*(p->value) == 'w' || *(p->value) == 'W') {
594 c->fail_on_missing_dbus = 0;
596 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
597 c->fail_on_missing_dbus = 1;
605 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
610 } else if (strcasecmp(g->name, "publish") == 0) {
613 for (p = g->pairs; p; p = p->pairs_next) {
615 if (strcasecmp(p->key, "publish-addresses") == 0)
616 c->server_config.publish_addresses = is_yes(p->value);
617 else if (strcasecmp(p->key, "publish-hinfo") == 0)
618 c->server_config.publish_hinfo = is_yes(p->value);
619 else if (strcasecmp(p->key, "publish-workstation") == 0)
620 c->server_config.publish_workstation = is_yes(p->value);
621 else if (strcasecmp(p->key, "publish-domain") == 0)
622 c->server_config.publish_domain = is_yes(p->value);
623 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
624 c->publish_resolv_conf = is_yes(p->value);
625 else if (strcasecmp(p->key, "disable-publishing") == 0)
626 c->server_config.disable_publishing = is_yes(p->value);
627 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
628 c->disable_user_service_publishing = is_yes(p->value);
629 else if (strcasecmp(p->key, "add-service-cookie") == 0)
630 c->server_config.add_service_cookie = is_yes(p->value);
631 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
632 avahi_strfreev(c->publish_dns_servers);
633 c->publish_dns_servers = avahi_split_csv(p->value);
634 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
635 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
636 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
637 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
639 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
644 } else if (strcasecmp(g->name, "wide-area") == 0) {
647 for (p = g->pairs; p; p = p->pairs_next) {
649 if (strcasecmp(p->key, "enable-wide-area") == 0)
650 c->server_config.enable_wide_area = is_yes(p->value);
652 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
657 } else if (strcasecmp(g->name, "reflector") == 0) {
660 for (p = g->pairs; p; p = p->pairs_next) {
662 if (strcasecmp(p->key, "enable-reflector") == 0)
663 c->server_config.enable_reflector = is_yes(p->value);
664 else if (strcasecmp(p->key, "reflect-ipv") == 0)
665 c->server_config.reflect_ipv = is_yes(p->value);
667 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
672 } else if (strcasecmp(g->name, "rlimits") == 0) {
675 for (p = g->pairs; p; p = p->pairs_next) {
677 if (strcasecmp(p->key, "rlimit-as") == 0) {
678 c->rlimit_as_set = 1;
679 c->rlimit_as = atoi(p->value);
680 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
681 c->rlimit_core_set = 1;
682 c->rlimit_core = atoi(p->value);
683 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
684 c->rlimit_data_set = 1;
685 c->rlimit_data = atoi(p->value);
686 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
687 c->rlimit_fsize_set = 1;
688 c->rlimit_fsize = atoi(p->value);
689 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
690 c->rlimit_nofile_set = 1;
691 c->rlimit_nofile = atoi(p->value);
692 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
693 c->rlimit_stack_set = 1;
694 c->rlimit_stack = atoi(p->value);
695 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
697 c->rlimit_nproc_set = 1;
698 c->rlimit_nproc = atoi(p->value);
700 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
703 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
710 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
720 avahi_ini_file_free(f);
725 static void log_function(AvahiLogLevel level, const char *txt) {
727 static const int log_level_map[] = {
735 assert(level < AVAHI_LOG_LEVEL_MAX);
738 if (!config.debug && level == AVAHI_LOG_DEBUG)
741 daemon_log(log_level_map[level], "%s", txt);
744 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
745 avahi_log_info("%s", text);
750 static int inotify_fd = -1;
752 static void add_inotify_watches(void) {
754 /* We ignore the return values, because one or more of these files
755 * might not exist and we're OK with that. In addition we never
756 * want to remove these watches, hence we keep their ids? */
759 c = config.use_chroot;
762 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
767 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
778 #define NUM_WATCHES 2
781 static int kfds[NUM_WATCHES];
782 static int num_kfds = 0;
784 static void add_kqueue_watch(const char *dir);
786 static void add_kqueue_watches(void) {
790 c = config.use_chroot;
793 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
794 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
797 static void add_kqueue_watch(const char *dir) {
804 if (num_kfds >= NUM_WATCHES)
807 fd = open(dir, O_RDONLY);
810 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
811 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
813 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
818 kfds[num_kfds++] = fd;
823 static void reload_config(void) {
826 /* Refresh in case the config dirs have been removed */
827 add_inotify_watches();
831 add_kqueue_watches();
835 static_service_load(config.use_chroot);
836 static_hosts_load(config.use_chroot);
838 static_service_load(0);
839 static_hosts_load(0);
841 static_service_add_to_server();
842 static_hosts_add_to_server();
844 if (resolv_conf_entry_group)
845 avahi_s_entry_group_reset(resolv_conf_entry_group);
849 update_wide_area_servers();
850 update_browse_domains();
852 if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
853 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
858 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
862 assert(fd == inotify_fd);
865 ioctl(inotify_fd, FIONREAD, &n);
869 buffer = avahi_malloc(n);
870 if (read(inotify_fd, buffer, n) < 0 ) {
872 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
877 avahi_log_info("Files changed, reloading.");
885 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
887 struct timespec nullts = { 0, 0 };
893 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
896 /* Sleep for a half-second to avoid potential races
897 * during install/uninstall. */
899 avahi_log_info("Files changed, reloading.");
902 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
908 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
910 const AvahiPoll *poll_api;
913 assert(simple_poll_api);
915 poll_api = avahi_simple_poll_get(simple_poll_api);
917 if ((sig = daemon_signal_next()) <= 0) {
918 avahi_log_error("daemon_signal_next() failed");
919 poll_api->watch_free(watch);
929 sig == SIGINT ? "SIGINT" :
930 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
931 avahi_simple_poll_quit(simple_poll_api);
935 avahi_log_info("Got SIGHUP, reloading.");
941 avahi_log_info("Got SIGUSR1, dumping record data.");
942 avahi_server_dump(avahi_server, dump, NULL);
946 avahi_log_warn("Got spurious signal, ignoring.");
951 /* Imported from ../avahi-client/nss-check.c */
952 int avahi_nss_support(void);
954 static int run_server(DaemonConfig *c) {
957 const AvahiPoll *poll_api = NULL;
958 AvahiWatch *sig_watch = NULL;
959 int retval_is_sent = 0;
961 AvahiWatch *inotify_watch = NULL;
965 AvahiWatch *kqueue_watch = NULL;
970 if (!(nss_support = avahi_nss_support()))
971 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
973 if (!(simple_poll_api = avahi_simple_poll_new())) {
974 avahi_log_error("Failed to create main loop object.");
978 poll_api = avahi_simple_poll_get(simple_poll_api);
980 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
981 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
985 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
986 avahi_log_error( "Failed to create signal watcher");
990 if (simple_protocol_setup(poll_api) < 0)
994 if (c->enable_dbus) {
995 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, !c->fail_on_missing_dbus
997 && !config.use_chroot
1001 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
1003 if (c->fail_on_missing_dbus)
1009 #ifdef ENABLE_CHROOT
1011 if (config.drop_root && config.use_chroot) {
1012 if (chroot(AVAHI_CONFIG_DIR) < 0) {
1013 avahi_log_error("Failed to chroot(): %s", strerror(errno));
1017 avahi_log_info("Successfully called chroot().");
1020 if (avahi_caps_drop_all() < 0) {
1021 avahi_log_error("Failed to drop capabilities.");
1024 avahi_log_info("Successfully dropped remaining capabilities.");
1030 if ((inotify_fd = inotify_init()) < 0)
1031 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
1033 add_inotify_watches();
1035 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
1036 avahi_log_error( "Failed to create inotify watcher");
1043 if ((kq = kqueue()) < 0)
1044 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
1046 add_kqueue_watches();
1048 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
1049 avahi_log_error( "Failed to create kqueue watcher");
1056 #ifdef ENABLE_CHROOT
1057 static_service_load(config.use_chroot);
1058 static_hosts_load(config.use_chroot);
1060 static_service_load(0);
1061 static_hosts_load(0);
1064 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
1065 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
1069 update_wide_area_servers();
1070 update_browse_domains();
1073 daemon_retval_send(0);
1078 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1080 /* We handle signals through an FD, so let's continue */
1084 avahi_log_error("poll(): %s", strerror(errno));
1094 static_service_remove_from_server();
1095 static_service_free_all();
1097 static_hosts_remove_from_server();
1098 static_hosts_free_all();
1100 remove_dns_server_entry_groups();
1102 simple_protocol_shutdown();
1106 dbus_protocol_shutdown();
1110 avahi_server_free(avahi_server);
1111 avahi_server = NULL;
1114 daemon_signal_done();
1117 poll_api->watch_free(sig_watch);
1121 poll_api->watch_free(inotify_watch);
1122 if (inotify_fd >= 0)
1128 poll_api->watch_free(kqueue_watch);
1131 for (i = 0; i < num_kfds; i++) {
1137 if (simple_poll_api) {
1138 avahi_simple_poll_free(simple_poll_api);
1139 simple_poll_api = NULL;
1142 if (!retval_is_sent && c->daemonize)
1143 daemon_retval_send(1);
1148 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1150 static int drop_root(void) {
1155 if (!(pw = getpwnam(AVAHI_USER))) {
1156 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1160 if (!(gr = getgrnam(AVAHI_GROUP))) {
1161 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1165 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);
1167 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1168 avahi_log_error("Failed to change group list: %s", strerror(errno));
1172 #if defined(HAVE_SETRESGID)
1173 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1174 #elif defined(HAVE_SETEGID)
1175 if ((r = setgid(gr->gr_gid)) >= 0)
1176 r = setegid(gr->gr_gid);
1177 #elif defined(HAVE_SETREGID)
1178 r = setregid(gr->gr_gid, gr->gr_gid);
1180 #error "No API to drop privileges"
1184 avahi_log_error("Failed to change GID: %s", strerror(errno));
1188 #if defined(HAVE_SETRESUID)
1189 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1190 #elif defined(HAVE_SETEUID)
1191 if ((r = setuid(pw->pw_uid)) >= 0)
1192 r = seteuid(pw->pw_uid);
1193 #elif defined(HAVE_SETREUID)
1194 r = setreuid(pw->pw_uid, pw->pw_uid);
1196 #error "No API to drop privileges"
1200 avahi_log_error("Failed to change UID: %s", strerror(errno));
1204 set_env("USER", pw->pw_name);
1205 set_env("LOGNAME", pw->pw_name);
1206 set_env("HOME", pw->pw_dir);
1208 avahi_log_info("Successfully dropped root privileges.");
1213 static const char* pid_file_proc(void) {
1214 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1217 static int make_runtime_dir(void) {
1220 int reset_umask = 0;
1225 if (!(pw = getpwnam(AVAHI_USER))) {
1226 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1230 if (!(gr = getgrnam(AVAHI_GROUP))) {
1231 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1238 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1239 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1243 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1245 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1246 avahi_log_error("stat(): %s\n", strerror(errno));
1250 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1251 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1263 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1265 rl.rlim_cur = rl.rlim_max = limit;
1267 if (setrlimit(resource, &rl) < 0)
1268 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1271 static void enforce_rlimits(void) {
1273 if (config.rlimit_as_set)
1274 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1276 if (config.rlimit_core_set)
1277 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1278 if (config.rlimit_data_set)
1279 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1280 if (config.rlimit_fsize_set)
1281 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1282 if (config.rlimit_nofile_set)
1283 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1284 if (config.rlimit_stack_set)
1285 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1287 if (config.rlimit_nproc_set)
1288 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1291 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1292 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
1293 /* We don't need locked memory */
1294 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1298 #define RANDOM_DEVICE "/dev/urandom"
1300 static void init_rand_seed(void) {
1304 /* Try to initialize seed from /dev/urandom, to make it a little
1305 * less predictable, and to make sure that multiple machines
1306 * booted at the same time choose different random seeds. */
1307 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1308 read(fd, &seed, sizeof(seed));
1312 /* If the initialization failed by some reason, we add the time to the seed*/
1313 seed ^= (unsigned) time(NULL);
1318 int main(int argc, char *argv[]) {
1320 int wrote_pid_file = 0;
1322 avahi_set_log_function(log_function);
1326 avahi_server_config_init(&config.server_config);
1327 config.command = DAEMON_RUN;
1328 config.daemonize = 0;
1329 config.config_file = NULL;
1331 config.enable_dbus = 1;
1332 config.fail_on_missing_dbus = 1;
1335 config.drop_root = 1;
1336 config.set_rlimits = 1;
1337 #ifdef ENABLE_CHROOT
1338 config.use_chroot = 1;
1340 config.modify_proc_title = 1;
1342 config.disable_user_service_publishing = 0;
1343 config.publish_dns_servers = NULL;
1344 config.publish_resolv_conf = 0;
1345 config.use_syslog = 0;
1347 config.rlimit_as_set = 0;
1348 config.rlimit_core_set = 0;
1349 config.rlimit_data_set = 0;
1350 config.rlimit_fsize_set = 0;
1351 config.rlimit_nofile_set = 0;
1352 config.rlimit_stack_set = 0;
1354 config.rlimit_nproc_set = 0;
1357 if ((argv0 = strrchr(argv[0], '/')))
1358 argv0 = avahi_strdup(argv0 + 1);
1360 argv0 = avahi_strdup(argv[0]);
1362 daemon_pid_file_ident = (const char *) argv0;
1363 daemon_log_ident = (char*) argv0;
1364 daemon_pid_file_proc = pid_file_proc;
1366 if (parse_command_line(&config, argc, argv) < 0)
1369 if (config.modify_proc_title)
1370 avahi_init_proc_title(argc, argv);
1372 #ifdef ENABLE_CHROOT
1373 config.use_chroot = config.use_chroot && config.drop_root;
1376 if (config.command == DAEMON_HELP) {
1379 } else if (config.command == DAEMON_VERSION) {
1380 printf("%s "PACKAGE_VERSION"\n", argv0);
1382 } else if (config.command == DAEMON_KILL) {
1383 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1384 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1390 } else if (config.command == DAEMON_RELOAD) {
1391 if (daemon_pid_file_kill(SIGHUP) < 0) {
1392 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1398 } else if (config.command == DAEMON_CHECK)
1399 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1400 else if (config.command == DAEMON_RUN) {
1403 if (getuid() != 0 && config.drop_root) {
1404 avahi_log_error("This program is intended to be run as root.");
1408 if ((pid = daemon_pid_file_is_running()) >= 0) {
1409 avahi_log_error("Daemon already running on PID %u", pid);
1413 if (load_config_file(&config) < 0)
1416 if (config.daemonize) {
1417 daemon_retval_init();
1419 if ((pid = daemon_fork()) < 0)
1421 else if (pid != 0) {
1425 if ((ret = daemon_retval_wait(20)) < 0) {
1426 avahi_log_error("Could not receive return value from daemon process.");
1437 if (config.use_syslog || config.daemonize)
1438 daemon_log_use = DAEMON_LOG_SYSLOG;
1440 if (daemon_close_all(-1) < 0) {
1441 avahi_log_error("Failed to close remaining file descriptors: %s", strerror(errno));
1445 if (make_runtime_dir() < 0)
1448 if (config.drop_root) {
1449 #ifdef ENABLE_CHROOT
1450 if (config.use_chroot)
1451 if (avahi_caps_reduce() < 0)
1455 if (drop_root() < 0)
1458 #ifdef ENABLE_CHROOT
1459 if (config.use_chroot)
1460 if (avahi_caps_reduce2() < 0)
1465 if (daemon_pid_file_create() < 0) {
1466 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1468 if (config.daemonize)
1469 daemon_retval_send(1);
1474 if (config.set_rlimits)
1479 #ifdef ENABLE_CHROOT
1480 if (config.drop_root && config.use_chroot)
1481 if (avahi_chroot_helper_start(argv0) < 0) {
1482 avahi_log_error("failed to start chroot() helper daemon.");
1486 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1488 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1490 if (run_server(&config) == 0)
1496 if (config.daemonize)
1497 daemon_retval_done();
1499 avahi_server_config_free(&config.server_config);
1500 avahi_free(config.config_file);
1501 avahi_strfreev(config.publish_dns_servers);
1502 avahi_strfreev(resolv_conf_name_servers);
1503 avahi_strfreev(resolv_conf_search_domains);
1505 if (wrote_pid_file) {
1506 #ifdef ENABLE_CHROOT
1507 avahi_chroot_helper_unlink(pid_file_proc());
1509 daemon_pid_file_remove();
1513 #ifdef ENABLE_CHROOT
1514 avahi_chroot_helper_shutdown();