4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
36 #include <sys/ioctl.h>
42 #include <sys/resource.h>
43 #include <sys/socket.h>
46 #include <sys/inotify.h>
50 #include <sys/types.h>
51 #include <sys/event.h>
55 #include <libdaemon/dfork.h>
56 #include <libdaemon/dsignal.h>
57 #include <libdaemon/dlog.h>
58 #include <libdaemon/dpid.h>
60 #include <avahi-common/malloc.h>
61 #include <avahi-common/simple-watch.h>
62 #include <avahi-common/error.h>
63 #include <avahi-common/alternative.h>
64 #include <avahi-common/domain.h>
66 #include <avahi-core/core.h>
67 #include <avahi-core/publish.h>
68 #include <avahi-core/dns-srv-rr.h>
69 #include <avahi-core/log.h>
76 #include "setproctitle.h"
78 #include "simple-protocol.h"
79 #include "static-services.h"
80 #include "static-hosts.h"
81 #include "ini-file-parser.h"
84 #include "dbus-protocol.h"
87 AvahiServer *avahi_server = NULL;
88 AvahiSimplePoll *simple_poll_api = NULL;
89 static char *argv0 = NULL;
102 AvahiServerConfig server_config;
103 DaemonCommand command;
109 int fail_on_missing_dbus;
116 int modify_proc_title;
118 int disable_user_service_publishing;
119 int publish_resolv_conf;
120 char ** publish_dns_servers;
123 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
124 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
127 int rlimit_nproc_set;
132 #define RESOLV_CONF "/etc/resolv.conf"
133 #define BROWSE_DOMAINS_MAX 16
135 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
136 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
138 static char **resolv_conf_name_servers = NULL;
139 static char **resolv_conf_search_domains = NULL;
141 static DaemonConfig config;
143 static int has_prefix(const char *s, const char *prefix) {
148 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
151 static int load_resolv_conf(void) {
156 avahi_strfreev(resolv_conf_name_servers);
157 resolv_conf_name_servers = NULL;
159 avahi_strfreev(resolv_conf_search_domains);
160 resolv_conf_search_domains = NULL;
163 f = avahi_chroot_helper_get_file(RESOLV_CONF);
165 f = fopen(RESOLV_CONF, "r");
169 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
173 resolv_conf_name_servers = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
174 resolv_conf_search_domains = avahi_new0(char*, BROWSE_DOMAINS_MAX+1);
180 if (!(fgets(ln, sizeof(ln), f)))
183 ln[strcspn(ln, "\r\n#")] = 0;
184 p = ln + strspn(ln, "\t ");
186 if ((has_prefix(p, "nameserver ") || has_prefix(p, "nameserver\t")) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
188 p += strspn(p, "\t ");
189 p[strcspn(p, "\t ")] = 0;
190 resolv_conf_name_servers[i++] = avahi_strdup(p);
193 if ((has_prefix(p, "search ") || has_prefix(p, "search\t") ||
194 has_prefix(p, "domain ") || has_prefix(p, "domain\t"))) {
198 while (j < BROWSE_DOMAINS_MAX) {
201 p += strspn(p, "\t ");
202 k = strcspn(p, "\t ");
205 resolv_conf_search_domains[j++] = avahi_strndup(p, k);
220 avahi_strfreev(resolv_conf_name_servers);
221 resolv_conf_name_servers = NULL;
223 avahi_strfreev(resolv_conf_search_domains);
224 resolv_conf_search_domains = NULL;
233 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
240 g = avahi_s_entry_group_new(s, NULL, NULL);
242 assert(avahi_s_entry_group_is_empty(g));
244 for (p = l; *p; p++) {
247 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
248 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
250 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
251 avahi_s_entry_group_free(g);
252 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
257 avahi_s_entry_group_commit(g);
262 static void remove_dns_server_entry_groups(void) {
264 if (resolv_conf_entry_group)
265 avahi_s_entry_group_reset(resolv_conf_entry_group);
267 if (dns_servers_entry_group)
268 avahi_s_entry_group_reset(dns_servers_entry_group);
271 static void update_wide_area_servers(void) {
272 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
276 if (!resolv_conf_name_servers) {
277 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
281 for (p = resolv_conf_name_servers; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
282 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
283 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
288 avahi_server_set_wide_area_servers(avahi_server, a, n);
291 static AvahiStringList *filter_duplicate_domains(AvahiStringList *l) {
292 AvahiStringList *e, *n, *p;
297 for (p = l, e = l->next; e; e = n) {
300 if (avahi_domain_equal((char*) e->text, (char*) l->text)) {
307 l->next = filter_duplicate_domains(l->next);
311 static void update_browse_domains(void) {
316 if (!resolv_conf_search_domains) {
317 avahi_server_set_browse_domains(avahi_server, NULL);
321 l = avahi_string_list_copy(config.server_config.browse_domains);
323 for (p = resolv_conf_search_domains, n = 0; *p && n < BROWSE_DOMAINS_MAX; p++, n++) {
324 if (!avahi_is_valid_domain_name(*p))
325 avahi_log_warn("'%s' is no valid domain name, ignoring.", *p);
327 l = avahi_string_list_add(l, *p);
330 l = filter_duplicate_domains(l);
332 avahi_server_set_browse_domains(avahi_server, l);
335 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
336 DaemonConfig *c = userdata;
341 /* This function is possibly called before the global variable
342 * avahi_server has been set, therefore we do it explicitly */
347 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
348 dbus_protocol_server_state_changed(state);
352 case AVAHI_SERVER_RUNNING:
353 avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
355 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
357 static_service_add_to_server();
358 static_hosts_add_to_server();
360 remove_dns_server_entry_groups();
362 if (c->publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
363 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf_name_servers);
365 if (c->publish_dns_servers && c->publish_dns_servers[0])
366 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
368 simple_protocol_restart_queries();
371 case AVAHI_SERVER_COLLISION: {
374 avahi_set_proc_title(argv0, "%s: collision", argv0);
376 static_service_remove_from_server();
377 static_hosts_remove_from_server();
378 remove_dns_server_entry_groups();
380 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
381 avahi_log_warn("Host name conflict, retrying with <%s>", n);
382 avahi_server_set_host_name(s, n);
388 case AVAHI_SERVER_FAILURE:
390 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
391 avahi_simple_poll_quit(simple_poll_api);
394 case AVAHI_SERVER_REGISTERING:
396 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
398 static_service_remove_from_server();
399 static_hosts_remove_from_server();
400 remove_dns_server_entry_groups();
404 case AVAHI_SERVER_INVALID:
410 static void help(FILE *f) {
413 " -h --help Show this help\n"
414 " -D --daemonize Daemonize after startup (implies -s)\n"
415 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
416 " -k --kill Kill a running daemon\n"
417 " -r --reload Request a running daemon to reload static services\n"
418 " -c --check Return 0 if a daemon is already running\n"
419 " -V --version Show version\n"
420 " -f --file=FILE Load the specified configuration file instead of\n"
421 " "AVAHI_CONFIG_FILE"\n"
422 " --no-rlimits Don't enforce resource limits\n"
423 " --no-drop-root Don't drop privileges\n"
425 " --no-chroot Don't chroot()\n"
427 " --no-proc-title Don't modify process title\n"
428 " --debug Increase verbosity\n",
433 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
437 OPTION_NO_RLIMITS = 256,
442 OPTION_NO_PROC_TITLE,
446 static const struct option long_options[] = {
447 { "help", no_argument, NULL, 'h' },
448 { "daemonize", no_argument, NULL, 'D' },
449 { "kill", no_argument, NULL, 'k' },
450 { "version", no_argument, NULL, 'V' },
451 { "file", required_argument, NULL, 'f' },
452 { "reload", no_argument, NULL, 'r' },
453 { "check", no_argument, NULL, 'c' },
454 { "syslog", no_argument, NULL, 's' },
455 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
456 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
458 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
460 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
461 { "debug", no_argument, NULL, OPTION_DEBUG },
467 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
474 c->command = DAEMON_HELP;
480 c->command = DAEMON_KILL;
483 c->command = DAEMON_VERSION;
486 avahi_free(c->config_file);
487 c->config_file = avahi_strdup(optarg);
490 c->command = DAEMON_RELOAD;
493 c->command = DAEMON_CHECK;
495 case OPTION_NO_RLIMITS:
498 case OPTION_NO_DROP_ROOT:
502 case OPTION_NO_CHROOT:
506 case OPTION_NO_PROC_TITLE:
507 c->modify_proc_title = 0;
518 fprintf(stderr, "Too many arguments\n");
525 static int is_yes(const char *s) {
528 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
531 static int load_config_file(DaemonConfig *c) {
534 AvahiIniFileGroup *g;
538 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
541 for (g = f->groups; g; g = g->groups_next) {
543 if (strcasecmp(g->name, "server") == 0) {
546 for (p = g->pairs; p; p = p->pairs_next) {
548 if (strcasecmp(p->key, "host-name") == 0) {
549 avahi_free(c->server_config.host_name);
550 c->server_config.host_name = avahi_strdup(p->value);
551 } else if (strcasecmp(p->key, "domain-name") == 0) {
552 avahi_free(c->server_config.domain_name);
553 c->server_config.domain_name = avahi_strdup(p->value);
554 } else if (strcasecmp(p->key, "browse-domains") == 0) {
557 e = avahi_split_csv(p->value);
559 for (t = e; *t; t++) {
560 char cleaned[AVAHI_DOMAIN_NAME_MAX];
562 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
563 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
568 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
573 c->server_config.browse_domains = filter_duplicate_domains(c->server_config.browse_domains);
574 } else if (strcasecmp(p->key, "use-ipv4") == 0)
575 c->server_config.use_ipv4 = is_yes(p->value);
576 else if (strcasecmp(p->key, "use-ipv6") == 0)
577 c->server_config.use_ipv6 = is_yes(p->value);
578 else if (strcasecmp(p->key, "check-response-ttl") == 0)
579 c->server_config.check_response_ttl = is_yes(p->value);
580 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
581 c->server_config.allow_point_to_point = is_yes(p->value);
582 else if (strcasecmp(p->key, "use-iff-running") == 0)
583 c->server_config.use_iff_running = is_yes(p->value);
584 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
585 c->server_config.disallow_other_stacks = is_yes(p->value);
587 else if (strcasecmp(p->key, "enable-dbus") == 0) {
589 if (*(p->value) == 'w' || *(p->value) == 'W') {
590 c->fail_on_missing_dbus = 0;
592 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
593 c->fail_on_missing_dbus = 1;
601 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
606 } else if (strcasecmp(g->name, "publish") == 0) {
609 for (p = g->pairs; p; p = p->pairs_next) {
611 if (strcasecmp(p->key, "publish-addresses") == 0)
612 c->server_config.publish_addresses = is_yes(p->value);
613 else if (strcasecmp(p->key, "publish-hinfo") == 0)
614 c->server_config.publish_hinfo = is_yes(p->value);
615 else if (strcasecmp(p->key, "publish-workstation") == 0)
616 c->server_config.publish_workstation = is_yes(p->value);
617 else if (strcasecmp(p->key, "publish-domain") == 0)
618 c->server_config.publish_domain = is_yes(p->value);
619 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
620 c->publish_resolv_conf = is_yes(p->value);
621 else if (strcasecmp(p->key, "disable-publishing") == 0)
622 c->server_config.disable_publishing = is_yes(p->value);
623 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
624 c->disable_user_service_publishing = is_yes(p->value);
625 else if (strcasecmp(p->key, "add-service-cookie") == 0)
626 c->server_config.add_service_cookie = is_yes(p->value);
627 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
628 avahi_strfreev(c->publish_dns_servers);
629 c->publish_dns_servers = avahi_split_csv(p->value);
630 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
631 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
632 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
633 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
635 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
640 } else if (strcasecmp(g->name, "wide-area") == 0) {
643 for (p = g->pairs; p; p = p->pairs_next) {
645 if (strcasecmp(p->key, "enable-wide-area") == 0)
646 c->server_config.enable_wide_area = is_yes(p->value);
648 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
653 } else if (strcasecmp(g->name, "reflector") == 0) {
656 for (p = g->pairs; p; p = p->pairs_next) {
658 if (strcasecmp(p->key, "enable-reflector") == 0)
659 c->server_config.enable_reflector = is_yes(p->value);
660 else if (strcasecmp(p->key, "reflect-ipv") == 0)
661 c->server_config.reflect_ipv = is_yes(p->value);
663 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
668 } else if (strcasecmp(g->name, "rlimits") == 0) {
671 for (p = g->pairs; p; p = p->pairs_next) {
673 if (strcasecmp(p->key, "rlimit-as") == 0) {
674 c->rlimit_as_set = 1;
675 c->rlimit_as = atoi(p->value);
676 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
677 c->rlimit_core_set = 1;
678 c->rlimit_core = atoi(p->value);
679 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
680 c->rlimit_data_set = 1;
681 c->rlimit_data = atoi(p->value);
682 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
683 c->rlimit_fsize_set = 1;
684 c->rlimit_fsize = atoi(p->value);
685 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
686 c->rlimit_nofile_set = 1;
687 c->rlimit_nofile = atoi(p->value);
688 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
689 c->rlimit_stack_set = 1;
690 c->rlimit_stack = atoi(p->value);
691 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
693 c->rlimit_nproc_set = 1;
694 c->rlimit_nproc = atoi(p->value);
696 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
699 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
706 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
716 avahi_ini_file_free(f);
721 static void log_function(AvahiLogLevel level, const char *txt) {
723 static const int log_level_map[] = {
731 assert(level < AVAHI_LOG_LEVEL_MAX);
734 if (!config.debug && level == AVAHI_LOG_DEBUG)
737 daemon_log(log_level_map[level], "%s", txt);
740 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
741 avahi_log_info("%s", text);
746 static int inotify_fd = -1;
748 static void add_inotify_watches(void) {
750 /* We ignore the return values, because one or more of these files
751 * might not exist and we're OK with that. In addition we never
752 * want to remove these watches, hence we keep their ids? */
755 c = config.use_chroot;
758 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
763 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
774 #define NUM_WATCHES 2
777 static int kfds[NUM_WATCHES];
778 static int num_kfds = 0;
780 static void add_kqueue_watch(const char *dir);
782 static void add_kqueue_watches(void) {
786 c = config.use_chroot;
789 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
790 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
793 static void add_kqueue_watch(const char *dir) {
800 if (num_kfds >= NUM_WATCHES)
803 fd = open(dir, O_RDONLY);
806 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
807 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
809 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
814 kfds[num_kfds++] = fd;
819 static void reload_config(void) {
822 /* Refresh in case the config dirs have been removed */
823 add_inotify_watches();
827 add_kqueue_watches();
831 static_service_load(config.use_chroot);
832 static_hosts_load(config.use_chroot);
834 static_service_load(0);
835 static_hosts_load(0);
837 static_service_add_to_server();
838 static_hosts_add_to_server();
840 if (resolv_conf_entry_group)
841 avahi_s_entry_group_reset(resolv_conf_entry_group);
845 update_wide_area_servers();
846 update_browse_domains();
848 if (config.publish_resolv_conf && resolv_conf_name_servers && resolv_conf_name_servers[0])
849 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf_name_servers);
854 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
858 assert(fd == inotify_fd);
861 ioctl(inotify_fd, FIONREAD, &n);
865 buffer = avahi_malloc(n);
866 if (read(inotify_fd, buffer, n) < 0 ) {
868 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
873 avahi_log_info("Files changed, reloading.");
881 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
883 struct timespec nullts = { 0, 0 };
889 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
892 /* Sleep for a half-second to avoid potential races
893 * during install/uninstall. */
895 avahi_log_info("Files changed, reloading.");
898 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
904 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
906 const AvahiPoll *poll_api;
909 assert(simple_poll_api);
911 poll_api = avahi_simple_poll_get(simple_poll_api);
913 if ((sig = daemon_signal_next()) <= 0) {
914 avahi_log_error("daemon_signal_next() failed");
915 poll_api->watch_free(watch);
925 sig == SIGINT ? "SIGINT" :
926 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
927 avahi_simple_poll_quit(simple_poll_api);
931 avahi_log_info("Got SIGHUP, reloading.");
937 avahi_log_info("Got SIGUSR1, dumping record data.");
938 avahi_server_dump(avahi_server, dump, NULL);
942 avahi_log_warn("Got spurious signal, ignoring.");
947 /* Imported from ../avahi-client/nss-check.c */
948 int avahi_nss_support(void);
950 static int run_server(DaemonConfig *c) {
953 const AvahiPoll *poll_api = NULL;
954 AvahiWatch *sig_watch = NULL;
955 int retval_is_sent = 0;
957 AvahiWatch *inotify_watch = NULL;
961 AvahiWatch *kqueue_watch = NULL;
966 if (!(nss_support = avahi_nss_support()))
967 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
969 if (!(simple_poll_api = avahi_simple_poll_new())) {
970 avahi_log_error("Failed to create main loop object.");
974 poll_api = avahi_simple_poll_get(simple_poll_api);
976 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
977 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
981 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
982 avahi_log_error( "Failed to create signal watcher");
986 if (simple_protocol_setup(poll_api) < 0)
990 if (c->enable_dbus) {
991 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, !c->fail_on_missing_dbus
993 && !config.use_chroot
997 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
999 if (c->fail_on_missing_dbus)
1005 #ifdef ENABLE_CHROOT
1007 if (config.drop_root && config.use_chroot) {
1008 if (chroot(AVAHI_CONFIG_DIR) < 0) {
1009 avahi_log_error("Failed to chroot(): %s", strerror(errno));
1013 avahi_log_info("Successfully called chroot().");
1016 if (avahi_caps_drop_all() < 0) {
1017 avahi_log_error("Failed to drop capabilities.");
1020 avahi_log_info("Successfully dropped remaining capabilities.");
1026 if ((inotify_fd = inotify_init()) < 0)
1027 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
1029 add_inotify_watches();
1031 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
1032 avahi_log_error( "Failed to create inotify watcher");
1039 if ((kq = kqueue()) < 0)
1040 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
1042 add_kqueue_watches();
1044 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
1045 avahi_log_error( "Failed to create kqueue watcher");
1052 #ifdef ENABLE_CHROOT
1053 static_service_load(config.use_chroot);
1054 static_hosts_load(config.use_chroot);
1056 static_service_load(0);
1057 static_hosts_load(0);
1060 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
1061 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
1065 update_wide_area_servers();
1066 update_browse_domains();
1069 daemon_retval_send(0);
1074 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1076 /* We handle signals through an FD, so let's continue */
1080 avahi_log_error("poll(): %s", strerror(errno));
1090 static_service_remove_from_server();
1091 static_service_free_all();
1093 static_hosts_remove_from_server();
1094 static_hosts_free_all();
1096 remove_dns_server_entry_groups();
1098 simple_protocol_shutdown();
1102 dbus_protocol_shutdown();
1106 avahi_server_free(avahi_server);
1107 avahi_server = NULL;
1110 daemon_signal_done();
1113 poll_api->watch_free(sig_watch);
1117 poll_api->watch_free(inotify_watch);
1118 if (inotify_fd >= 0)
1124 poll_api->watch_free(kqueue_watch);
1127 for (i = 0; i < num_kfds; i++) {
1133 if (simple_poll_api) {
1134 avahi_simple_poll_free(simple_poll_api);
1135 simple_poll_api = NULL;
1138 if (!retval_is_sent && c->daemonize)
1139 daemon_retval_send(1);
1144 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1146 static int drop_root(void) {
1151 if (!(pw = getpwnam(AVAHI_USER))) {
1152 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1156 if (!(gr = getgrnam(AVAHI_GROUP))) {
1157 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1161 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);
1163 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1164 avahi_log_error("Failed to change group list: %s", strerror(errno));
1168 #if defined(HAVE_SETRESGID)
1169 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1170 #elif defined(HAVE_SETEGID)
1171 if ((r = setgid(gr->gr_gid)) >= 0)
1172 r = setegid(gr->gr_gid);
1173 #elif defined(HAVE_SETREGID)
1174 r = setregid(gr->gr_gid, gr->gr_gid);
1176 #error "No API to drop privileges"
1180 avahi_log_error("Failed to change GID: %s", strerror(errno));
1184 #if defined(HAVE_SETRESUID)
1185 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1186 #elif defined(HAVE_SETEUID)
1187 if ((r = setuid(pw->pw_uid)) >= 0)
1188 r = seteuid(pw->pw_uid);
1189 #elif defined(HAVE_SETREUID)
1190 r = setreuid(pw->pw_uid, pw->pw_uid);
1192 #error "No API to drop privileges"
1196 avahi_log_error("Failed to change UID: %s", strerror(errno));
1200 set_env("USER", pw->pw_name);
1201 set_env("LOGNAME", pw->pw_name);
1202 set_env("HOME", pw->pw_dir);
1204 avahi_log_info("Successfully dropped root privileges.");
1209 static const char* pid_file_proc(void) {
1210 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1213 static int make_runtime_dir(void) {
1216 int reset_umask = 0;
1221 if (!(pw = getpwnam(AVAHI_USER))) {
1222 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1226 if (!(gr = getgrnam(AVAHI_GROUP))) {
1227 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1234 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1235 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1239 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1241 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1242 avahi_log_error("stat(): %s\n", strerror(errno));
1246 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1247 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1259 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1261 rl.rlim_cur = rl.rlim_max = limit;
1263 if (setrlimit(resource, &rl) < 0)
1264 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1267 static void enforce_rlimits(void) {
1269 if (config.rlimit_as_set)
1270 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1272 if (config.rlimit_core_set)
1273 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1274 if (config.rlimit_data_set)
1275 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1276 if (config.rlimit_fsize_set)
1277 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1278 if (config.rlimit_nofile_set)
1279 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1280 if (config.rlimit_stack_set)
1281 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1283 if (config.rlimit_nproc_set)
1284 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1287 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1288 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
1289 /* We don't need locked memory */
1290 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1294 #define RANDOM_DEVICE "/dev/urandom"
1296 static void init_rand_seed(void) {
1300 /* Try to initialize seed from /dev/urandom, to make it a little
1301 * less predictable, and to make sure that multiple machines
1302 * booted at the same time choose different random seeds. */
1303 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1304 read(fd, &seed, sizeof(seed));
1308 /* If the initialization failed by some reason, we add the time to the seed*/
1309 seed ^= (unsigned) time(NULL);
1314 int main(int argc, char *argv[]) {
1316 int wrote_pid_file = 0;
1318 avahi_set_log_function(log_function);
1322 avahi_server_config_init(&config.server_config);
1323 config.command = DAEMON_RUN;
1324 config.daemonize = 0;
1325 config.config_file = NULL;
1327 config.enable_dbus = 1;
1328 config.fail_on_missing_dbus = 1;
1331 config.drop_root = 1;
1332 config.set_rlimits = 1;
1333 #ifdef ENABLE_CHROOT
1334 config.use_chroot = 1;
1336 config.modify_proc_title = 1;
1338 config.disable_user_service_publishing = 0;
1339 config.publish_dns_servers = NULL;
1340 config.publish_resolv_conf = 0;
1341 config.use_syslog = 0;
1343 config.rlimit_as_set = 0;
1344 config.rlimit_core_set = 0;
1345 config.rlimit_data_set = 0;
1346 config.rlimit_fsize_set = 0;
1347 config.rlimit_nofile_set = 0;
1348 config.rlimit_stack_set = 0;
1350 config.rlimit_nproc_set = 0;
1353 if ((argv0 = strrchr(argv[0], '/')))
1354 argv0 = avahi_strdup(argv0 + 1);
1356 argv0 = avahi_strdup(argv[0]);
1358 daemon_pid_file_ident = (const char *) argv0;
1359 daemon_log_ident = (char*) argv0;
1360 daemon_pid_file_proc = pid_file_proc;
1362 if (parse_command_line(&config, argc, argv) < 0)
1365 if (config.modify_proc_title)
1366 avahi_init_proc_title(argc, argv);
1368 #ifdef ENABLE_CHROOT
1369 config.use_chroot = config.use_chroot && config.drop_root;
1372 if (config.command == DAEMON_HELP) {
1375 } else if (config.command == DAEMON_VERSION) {
1376 printf("%s "PACKAGE_VERSION"\n", argv0);
1378 } else if (config.command == DAEMON_KILL) {
1379 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1380 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1386 } else if (config.command == DAEMON_RELOAD) {
1387 if (daemon_pid_file_kill(SIGHUP) < 0) {
1388 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1394 } else if (config.command == DAEMON_CHECK)
1395 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1396 else if (config.command == DAEMON_RUN) {
1399 if (getuid() != 0 && config.drop_root) {
1400 avahi_log_error("This program is intended to be run as root.");
1404 if ((pid = daemon_pid_file_is_running()) >= 0) {
1405 avahi_log_error("Daemon already running on PID %u", pid);
1409 if (load_config_file(&config) < 0)
1412 if (config.daemonize) {
1413 daemon_retval_init();
1415 if ((pid = daemon_fork()) < 0)
1417 else if (pid != 0) {
1421 if ((ret = daemon_retval_wait(20)) < 0) {
1422 avahi_log_error("Could not receive return value from daemon process.");
1433 if (config.use_syslog || config.daemonize)
1434 daemon_log_use = DAEMON_LOG_SYSLOG;
1436 if (daemon_close_all(-1) < 0) {
1437 avahi_log_error("Failed to close remaining file descriptors: %s", strerror(errno));
1441 if (make_runtime_dir() < 0)
1444 if (config.drop_root) {
1445 #ifdef ENABLE_CHROOT
1446 if (config.use_chroot)
1447 if (avahi_caps_reduce() < 0)
1451 if (drop_root() < 0)
1454 #ifdef ENABLE_CHROOT
1455 if (config.use_chroot)
1456 if (avahi_caps_reduce2() < 0)
1461 if (daemon_pid_file_create() < 0) {
1462 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1464 if (config.daemonize)
1465 daemon_retval_send(1);
1470 if (config.set_rlimits)
1475 #ifdef ENABLE_CHROOT
1476 if (config.drop_root && config.use_chroot)
1477 if (avahi_chroot_helper_start(argv0) < 0) {
1478 avahi_log_error("failed to start chroot() helper daemon.");
1482 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1484 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1486 if (run_server(&config) == 0)
1492 if (config.daemonize)
1493 daemon_retval_done();
1495 avahi_server_config_free(&config.server_config);
1496 avahi_free(config.config_file);
1497 avahi_strfreev(config.publish_dns_servers);
1498 avahi_strfreev(resolv_conf_name_servers);
1499 avahi_strfreev(resolv_conf_search_domains);
1501 if (wrote_pid_file) {
1502 #ifdef ENABLE_CHROOT
1503 avahi_chroot_helper_unlink(pid_file_proc());
1505 daemon_pid_file_remove();
1509 #ifdef ENABLE_CHROOT
1510 avahi_chroot_helper_shutdown();