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"
138 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
139 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
141 static char **resolv_conf = NULL;
143 static DaemonConfig config;
145 static int has_prefix(const char *s, const char *prefix) {
150 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
153 static int load_resolv_conf(void) {
158 avahi_strfreev(resolv_conf);
162 f = avahi_chroot_helper_get_file(RESOLV_CONF);
164 f = fopen(RESOLV_CONF, "r");
168 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
172 resolv_conf = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
174 while (!feof(f) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
178 if (!(fgets(ln, sizeof(ln), f)))
181 ln[strcspn(ln, "\r\n#")] = 0;
182 p = ln + strspn(ln, "\t ");
184 if (has_prefix(p, "nameserver")) {
186 p += strspn(p, "\t ");
187 p[strcspn(p, "\t ")] = 0;
188 resolv_conf[i++] = avahi_strdup(p);
197 avahi_strfreev(resolv_conf);
207 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
214 g = avahi_s_entry_group_new(s, NULL, NULL);
216 assert(avahi_s_entry_group_is_empty(g));
218 for (p = l; *p; p++) {
221 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
222 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
224 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
225 avahi_s_entry_group_free(g);
226 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
231 avahi_s_entry_group_commit(g);
236 static void remove_dns_server_entry_groups(void) {
238 if (resolv_conf_entry_group)
239 avahi_s_entry_group_reset(resolv_conf_entry_group);
241 if (dns_servers_entry_group)
242 avahi_s_entry_group_reset(dns_servers_entry_group);
245 static void update_wide_area_servers(void) {
246 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
251 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
255 for (p = resolv_conf; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
256 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
257 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
262 avahi_server_set_wide_area_servers(avahi_server, a, n);
265 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
266 DaemonConfig *c = userdata;
271 /* This function is possibly called before the global variable
272 * avahi_server has been set, therefore we do it explicitly */
277 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
278 dbus_protocol_server_state_changed(state);
282 case AVAHI_SERVER_RUNNING:
283 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));
285 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
287 static_service_add_to_server();
288 static_hosts_add_to_server();
290 remove_dns_server_entry_groups();
292 if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
293 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
295 if (c->publish_dns_servers && c->publish_dns_servers[0])
296 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
298 simple_protocol_restart_queries();
301 case AVAHI_SERVER_COLLISION: {
304 avahi_set_proc_title(argv0, "%s: collision", argv0);
306 static_service_remove_from_server();
307 static_hosts_remove_from_server();
308 remove_dns_server_entry_groups();
310 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
311 avahi_log_warn("Host name conflict, retrying with <%s>", n);
312 avahi_server_set_host_name(s, n);
318 case AVAHI_SERVER_FAILURE:
320 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
321 avahi_simple_poll_quit(simple_poll_api);
324 case AVAHI_SERVER_REGISTERING:
326 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
328 static_service_remove_from_server();
329 static_hosts_remove_from_server();
330 remove_dns_server_entry_groups();
334 case AVAHI_SERVER_INVALID:
340 static void help(FILE *f) {
343 " -h --help Show this help\n"
344 " -D --daemonize Daemonize after startup (implies -s)\n"
345 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
346 " -k --kill Kill a running daemon\n"
347 " -r --reload Request a running daemon to reload static services\n"
348 " -c --check Return 0 if a daemon is already running\n"
349 " -V --version Show version\n"
350 " -f --file=FILE Load the specified configuration file instead of\n"
351 " "AVAHI_CONFIG_FILE"\n"
352 " --no-rlimits Don't enforce resource limits\n"
353 " --no-drop-root Don't drop privileges\n"
355 " --no-chroot Don't chroot()\n"
357 " --no-proc-title Don't modify process title\n"
358 " --debug Increase verbosity\n",
363 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
367 OPTION_NO_RLIMITS = 256,
372 OPTION_NO_PROC_TITLE,
376 static const struct option long_options[] = {
377 { "help", no_argument, NULL, 'h' },
378 { "daemonize", no_argument, NULL, 'D' },
379 { "kill", no_argument, NULL, 'k' },
380 { "version", no_argument, NULL, 'V' },
381 { "file", required_argument, NULL, 'f' },
382 { "reload", no_argument, NULL, 'r' },
383 { "check", no_argument, NULL, 'c' },
384 { "syslog", no_argument, NULL, 's' },
385 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
386 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
388 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
390 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
391 { "debug", no_argument, NULL, OPTION_DEBUG },
397 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
404 c->command = DAEMON_HELP;
410 c->command = DAEMON_KILL;
413 c->command = DAEMON_VERSION;
416 avahi_free(c->config_file);
417 c->config_file = avahi_strdup(optarg);
420 c->command = DAEMON_RELOAD;
423 c->command = DAEMON_CHECK;
425 case OPTION_NO_RLIMITS:
428 case OPTION_NO_DROP_ROOT:
432 case OPTION_NO_CHROOT:
436 case OPTION_NO_PROC_TITLE:
437 c->modify_proc_title = 0;
448 fprintf(stderr, "Too many arguments\n");
455 static int is_yes(const char *s) {
458 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
461 static int load_config_file(DaemonConfig *c) {
464 AvahiIniFileGroup *g;
468 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
471 for (g = f->groups; g; g = g->groups_next) {
473 if (strcasecmp(g->name, "server") == 0) {
476 for (p = g->pairs; p; p = p->pairs_next) {
478 if (strcasecmp(p->key, "host-name") == 0) {
479 avahi_free(c->server_config.host_name);
480 c->server_config.host_name = avahi_strdup(p->value);
481 } else if (strcasecmp(p->key, "domain-name") == 0) {
482 avahi_free(c->server_config.domain_name);
483 c->server_config.domain_name = avahi_strdup(p->value);
484 } else if (strcasecmp(p->key, "browse-domains") == 0) {
487 e = avahi_split_csv(p->value);
489 for (t = e; *t; t++) {
490 char cleaned[AVAHI_DOMAIN_NAME_MAX];
492 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
493 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
498 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
502 } else if (strcasecmp(p->key, "use-ipv4") == 0)
503 c->server_config.use_ipv4 = is_yes(p->value);
504 else if (strcasecmp(p->key, "use-ipv6") == 0)
505 c->server_config.use_ipv6 = is_yes(p->value);
506 else if (strcasecmp(p->key, "check-response-ttl") == 0)
507 c->server_config.check_response_ttl = is_yes(p->value);
508 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
509 c->server_config.allow_point_to_point = is_yes(p->value);
510 else if (strcasecmp(p->key, "use-iff-running") == 0)
511 c->server_config.use_iff_running = is_yes(p->value);
512 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
513 c->server_config.disallow_other_stacks = is_yes(p->value);
515 else if (strcasecmp(p->key, "enable-dbus") == 0) {
517 if (*(p->value) == 'w' || *(p->value) == 'W') {
518 c->fail_on_missing_dbus = 0;
520 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
521 c->fail_on_missing_dbus = 1;
529 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
534 } else if (strcasecmp(g->name, "publish") == 0) {
537 for (p = g->pairs; p; p = p->pairs_next) {
539 if (strcasecmp(p->key, "publish-addresses") == 0)
540 c->server_config.publish_addresses = is_yes(p->value);
541 else if (strcasecmp(p->key, "publish-hinfo") == 0)
542 c->server_config.publish_hinfo = is_yes(p->value);
543 else if (strcasecmp(p->key, "publish-workstation") == 0)
544 c->server_config.publish_workstation = is_yes(p->value);
545 else if (strcasecmp(p->key, "publish-domain") == 0)
546 c->server_config.publish_domain = is_yes(p->value);
547 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
548 c->publish_resolv_conf = is_yes(p->value);
549 else if (strcasecmp(p->key, "disable-publishing") == 0)
550 c->server_config.disable_publishing = is_yes(p->value);
551 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
552 c->disable_user_service_publishing = is_yes(p->value);
553 else if (strcasecmp(p->key, "add-service-cookie") == 0)
554 c->server_config.add_service_cookie = is_yes(p->value);
555 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
556 avahi_strfreev(c->publish_dns_servers);
557 c->publish_dns_servers = avahi_split_csv(p->value);
558 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
559 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
560 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
561 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
563 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
568 } else if (strcasecmp(g->name, "wide-area") == 0) {
571 for (p = g->pairs; p; p = p->pairs_next) {
573 if (strcasecmp(p->key, "enable-wide-area") == 0)
574 c->server_config.enable_wide_area = is_yes(p->value);
576 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
581 } else if (strcasecmp(g->name, "reflector") == 0) {
584 for (p = g->pairs; p; p = p->pairs_next) {
586 if (strcasecmp(p->key, "enable-reflector") == 0)
587 c->server_config.enable_reflector = is_yes(p->value);
588 else if (strcasecmp(p->key, "reflect-ipv") == 0)
589 c->server_config.reflect_ipv = is_yes(p->value);
591 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
596 } else if (strcasecmp(g->name, "rlimits") == 0) {
599 for (p = g->pairs; p; p = p->pairs_next) {
601 if (strcasecmp(p->key, "rlimit-as") == 0) {
602 c->rlimit_as_set = 1;
603 c->rlimit_as = atoi(p->value);
604 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
605 c->rlimit_core_set = 1;
606 c->rlimit_core = atoi(p->value);
607 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
608 c->rlimit_data_set = 1;
609 c->rlimit_data = atoi(p->value);
610 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
611 c->rlimit_fsize_set = 1;
612 c->rlimit_fsize = atoi(p->value);
613 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
614 c->rlimit_nofile_set = 1;
615 c->rlimit_nofile = atoi(p->value);
616 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
617 c->rlimit_stack_set = 1;
618 c->rlimit_stack = atoi(p->value);
619 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
621 c->rlimit_nproc_set = 1;
622 c->rlimit_nproc = atoi(p->value);
624 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
627 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
634 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
644 avahi_ini_file_free(f);
649 static void log_function(AvahiLogLevel level, const char *txt) {
651 static const int log_level_map[] = {
659 assert(level < AVAHI_LOG_LEVEL_MAX);
662 if (!config.debug && level == AVAHI_LOG_DEBUG)
665 daemon_log(log_level_map[level], "%s", txt);
668 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
669 avahi_log_info("%s", text);
674 static int inotify_fd = -1;
676 static void add_inotify_watches(void) {
678 /* We ignore the return values, because one or more of these files
679 * might not exist and we're OK with that. In addition we never
680 * want to remove these watches, hence we keep their ids? */
683 c = config.use_chroot;
686 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
691 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
702 #define NUM_WATCHES 2
705 static int kfds[NUM_WATCHES];
706 static int num_kfds = 0;
708 static void add_kqueue_watch(const char *dir);
710 static void add_kqueue_watches(void) {
714 c = config.use_chroot;
717 add_kqueue_watch(c ? "/" : AVAHI_CONFIG_DIR);
718 add_kqueue_watch(c ? "/services" : AVAHI_SERVICE_DIR);
721 static void add_kqueue_watch(const char *dir) {
728 if (num_kfds >= NUM_WATCHES)
731 fd = open(dir, O_RDONLY);
734 EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
735 NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME,
737 if (kevent(kq, &ev, 1, NULL, 0, NULL) == -1) {
742 kfds[num_kfds++] = fd;
747 static void reload_config(void) {
750 /* Refresh in case the config dirs have been removed */
751 add_inotify_watches();
755 add_kqueue_watches();
759 static_service_load(config.use_chroot);
760 static_hosts_load(config.use_chroot);
762 static_service_load(0);
763 static_hosts_load(0);
765 static_service_add_to_server();
766 static_hosts_add_to_server();
768 if (resolv_conf_entry_group)
769 avahi_s_entry_group_reset(resolv_conf_entry_group);
773 update_wide_area_servers();
775 if (config.publish_resolv_conf && resolv_conf && resolv_conf[0])
776 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
781 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
785 assert(fd == inotify_fd);
788 ioctl(inotify_fd, FIONREAD, &n);
792 buffer = avahi_malloc(n);
793 if (read(inotify_fd, buffer, n) < 0 ) {
795 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
800 avahi_log_info("Files changed, reloading.");
808 static void kqueue_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
810 struct timespec nullts = { 0, 0 };
816 res = kevent(kq, NULL, 0, &ev, 1, &nullts);
819 /* Sleep for a half-second to avoid potential races
820 * during install/uninstall. */
822 avahi_log_info("Files changed, reloading.");
825 avahi_log_error("Failed to read kqueue event: %s", avahi_strerror(errno));
831 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
833 const AvahiPoll *poll_api;
836 assert(simple_poll_api);
838 poll_api = avahi_simple_poll_get(simple_poll_api);
840 if ((sig = daemon_signal_next()) <= 0) {
841 avahi_log_error("daemon_signal_next() failed");
842 poll_api->watch_free(watch);
852 sig == SIGINT ? "SIGINT" :
853 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
854 avahi_simple_poll_quit(simple_poll_api);
858 avahi_log_info("Got SIGHUP, reloading.");
864 avahi_log_info("Got SIGUSR1, dumping record data.");
865 avahi_server_dump(avahi_server, dump, NULL);
869 avahi_log_warn("Got spurious signal, ignoring.");
874 /* Imported from ../avahi-client/nss-check.c */
875 int avahi_nss_support(void);
877 static int run_server(DaemonConfig *c) {
880 const AvahiPoll *poll_api = NULL;
881 AvahiWatch *sig_watch = NULL;
882 int retval_is_sent = 0;
884 AvahiWatch *inotify_watch = NULL;
888 AvahiWatch *kqueue_watch = NULL;
893 if (!(nss_support = avahi_nss_support()))
894 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
896 if (!(simple_poll_api = avahi_simple_poll_new())) {
897 avahi_log_error("Failed to create main loop object.");
901 poll_api = avahi_simple_poll_get(simple_poll_api);
903 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
904 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
908 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
909 avahi_log_error( "Failed to create signal watcher");
913 if (simple_protocol_setup(poll_api) < 0)
917 if (c->enable_dbus) {
918 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, !c->fail_on_missing_dbus
920 && !config.use_chroot
924 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
926 if (c->fail_on_missing_dbus)
934 if (config.drop_root && config.use_chroot) {
935 if (chroot(AVAHI_CONFIG_DIR) < 0) {
936 avahi_log_error("Failed to chroot(): %s", strerror(errno));
940 avahi_log_info("Successfully called chroot().");
943 if (avahi_caps_drop_all() < 0) {
944 avahi_log_error("Failed to drop capabilities.");
947 avahi_log_info("Successfully dropped remaining capabilities.");
953 if ((inotify_fd = inotify_init()) < 0)
954 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
956 add_inotify_watches();
958 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
959 avahi_log_error( "Failed to create inotify watcher");
966 if ((kq = kqueue()) < 0)
967 avahi_log_warn( "Failed to initialize kqueue: %s", strerror(errno));
969 add_kqueue_watches();
971 if (!(kqueue_watch = poll_api->watch_new(poll_api, kq, AVAHI_WATCH_IN, kqueue_callback, NULL))) {
972 avahi_log_error( "Failed to create kqueue watcher");
980 static_service_load(config.use_chroot);
981 static_hosts_load(config.use_chroot);
983 static_service_load(0);
984 static_hosts_load(0);
987 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
988 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
992 update_wide_area_servers();
995 daemon_retval_send(0);
1000 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
1002 /* We handle signals through an FD, so let's continue */
1006 avahi_log_error("poll(): %s", strerror(errno));
1016 static_service_remove_from_server();
1017 static_service_free_all();
1019 static_hosts_remove_from_server();
1020 static_hosts_free_all();
1022 remove_dns_server_entry_groups();
1024 simple_protocol_shutdown();
1028 dbus_protocol_shutdown();
1032 avahi_server_free(avahi_server);
1033 avahi_server = NULL;
1036 daemon_signal_done();
1039 poll_api->watch_free(sig_watch);
1043 poll_api->watch_free(inotify_watch);
1044 if (inotify_fd >= 0)
1050 poll_api->watch_free(kqueue_watch);
1053 for (i = 0; i < num_kfds; i++) {
1059 if (simple_poll_api) {
1060 avahi_simple_poll_free(simple_poll_api);
1061 simple_poll_api = NULL;
1064 if (!retval_is_sent && c->daemonize)
1065 daemon_retval_send(1);
1070 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
1072 static int drop_root(void) {
1077 if (!(pw = getpwnam(AVAHI_USER))) {
1078 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1082 if (!(gr = getgrnam(AVAHI_GROUP))) {
1083 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1087 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);
1089 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
1090 avahi_log_error("Failed to change group list: %s", strerror(errno));
1094 #if defined(HAVE_SETRESGID)
1095 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
1096 #elif defined(HAVE_SETEGID)
1097 if ((r = setgid(gr->gr_gid)) >= 0)
1098 r = setegid(gr->gr_gid);
1099 #elif defined(HAVE_SETREGID)
1100 r = setregid(gr->gr_gid, gr->gr_gid);
1102 #error "No API to drop priviliges"
1106 avahi_log_error("Failed to change GID: %s", strerror(errno));
1110 #if defined(HAVE_SETRESUID)
1111 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
1112 #elif defined(HAVE_SETEUID)
1113 if ((r = setuid(pw->pw_uid)) >= 0)
1114 r = seteuid(pw->pw_uid);
1115 #elif defined(HAVE_SETREUID)
1116 r = setreuid(pw->pw_uid, pw->pw_uid);
1118 #error "No API to drop priviliges"
1122 avahi_log_error("Failed to change UID: %s", strerror(errno));
1126 set_env("USER", pw->pw_name);
1127 set_env("LOGNAME", pw->pw_name);
1128 set_env("HOME", pw->pw_dir);
1130 avahi_log_info("Successfully dropped root privileges.");
1135 static const char* pid_file_proc(void) {
1136 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1139 static int make_runtime_dir(void) {
1142 int reset_umask = 0;
1147 if (!(pw = getpwnam(AVAHI_USER))) {
1148 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1152 if (!(gr = getgrnam(AVAHI_GROUP))) {
1153 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1160 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1161 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1165 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1167 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1168 avahi_log_error("stat(): %s\n", strerror(errno));
1172 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1173 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1185 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1187 rl.rlim_cur = rl.rlim_max = limit;
1189 if (setrlimit(resource, &rl) < 0)
1190 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1193 static void enforce_rlimits(void) {
1195 if (config.rlimit_as_set)
1196 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1198 if (config.rlimit_core_set)
1199 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1200 if (config.rlimit_data_set)
1201 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1202 if (config.rlimit_fsize_set)
1203 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1204 if (config.rlimit_nofile_set)
1205 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1206 if (config.rlimit_stack_set)
1207 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1209 if (config.rlimit_nproc_set)
1210 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1213 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1214 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
1215 /* We don't need locked memory */
1216 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1220 #define RANDOM_DEVICE "/dev/urandom"
1222 static void init_rand_seed(void) {
1226 /* Try to initialize seed from /dev/urandom, to make it a little
1227 * less predictable, and to make sure that multiple machines
1228 * booted at the same time choose different random seeds. */
1229 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1230 read(fd, &seed, sizeof(seed));
1234 /* If the initialization failed by some reason, we add the time to the seed*/
1235 seed ^= (unsigned) time(NULL);
1240 int main(int argc, char *argv[]) {
1242 int wrote_pid_file = 0;
1244 avahi_set_log_function(log_function);
1248 avahi_server_config_init(&config.server_config);
1249 config.command = DAEMON_RUN;
1250 config.daemonize = 0;
1251 config.config_file = NULL;
1253 config.enable_dbus = 1;
1254 config.fail_on_missing_dbus = 1;
1257 config.drop_root = 1;
1258 config.set_rlimits = 1;
1259 #ifdef ENABLE_CHROOT
1260 config.use_chroot = 1;
1262 config.modify_proc_title = 1;
1264 config.disable_user_service_publishing = 0;
1265 config.publish_dns_servers = NULL;
1266 config.publish_resolv_conf = 0;
1267 config.use_syslog = 0;
1269 config.rlimit_as_set = 0;
1270 config.rlimit_core_set = 0;
1271 config.rlimit_data_set = 0;
1272 config.rlimit_fsize_set = 0;
1273 config.rlimit_nofile_set = 0;
1274 config.rlimit_stack_set = 0;
1276 config.rlimit_nproc_set = 0;
1279 if ((argv0 = strrchr(argv[0], '/')))
1280 argv0 = avahi_strdup(argv0 + 1);
1282 argv0 = avahi_strdup(argv[0]);
1284 daemon_pid_file_ident = (const char *) argv0;
1285 daemon_log_ident = (char*) argv0;
1286 daemon_pid_file_proc = pid_file_proc;
1288 if (parse_command_line(&config, argc, argv) < 0)
1291 if (config.modify_proc_title)
1292 avahi_init_proc_title(argc, argv);
1294 #ifdef ENABLE_CHROOT
1295 config.use_chroot = config.use_chroot && config.drop_root;
1298 if (config.command == DAEMON_HELP) {
1301 } else if (config.command == DAEMON_VERSION) {
1302 printf("%s "PACKAGE_VERSION"\n", argv0);
1304 } else if (config.command == DAEMON_KILL) {
1305 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1306 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1312 } else if (config.command == DAEMON_RELOAD) {
1313 if (daemon_pid_file_kill(SIGHUP) < 0) {
1314 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1320 } else if (config.command == DAEMON_CHECK)
1321 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1322 else if (config.command == DAEMON_RUN) {
1325 if (getuid() != 0 && config.drop_root) {
1326 avahi_log_error("This program is intended to be run as root.");
1330 if ((pid = daemon_pid_file_is_running()) >= 0) {
1331 avahi_log_error("Daemon already running on PID %u", pid);
1335 if (load_config_file(&config) < 0)
1338 if (config.daemonize) {
1339 daemon_retval_init();
1341 if ((pid = daemon_fork()) < 0)
1343 else if (pid != 0) {
1347 if ((ret = daemon_retval_wait(20)) < 0) {
1348 avahi_log_error("Could not receive return value from daemon process.");
1359 if (config.use_syslog || config.daemonize)
1360 daemon_log_use = DAEMON_LOG_SYSLOG;
1362 if (daemon_close_all(-1) < 0) {
1363 avahi_log_error("Failed to close remaining file descriptors: %s", strerror(errno));
1367 if (make_runtime_dir() < 0)
1370 if (config.drop_root) {
1371 #ifdef ENABLE_CHROOT
1372 if (config.use_chroot)
1373 if (avahi_caps_reduce() < 0)
1377 if (drop_root() < 0)
1380 #ifdef ENABLE_CHROOT
1381 if (config.use_chroot)
1382 if (avahi_caps_reduce2() < 0)
1387 if (daemon_pid_file_create() < 0) {
1388 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1390 if (config.daemonize)
1391 daemon_retval_send(1);
1396 if (config.set_rlimits)
1401 #ifdef ENABLE_CHROOT
1402 if (config.drop_root && config.use_chroot)
1403 if (avahi_chroot_helper_start(argv0) < 0) {
1404 avahi_log_error("failed to start chroot() helper daemon.");
1408 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1410 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1412 if (run_server(&config) == 0)
1418 if (config.daemonize)
1419 daemon_retval_done();
1421 avahi_server_config_free(&config.server_config);
1422 avahi_free(config.config_file);
1423 avahi_strfreev(config.publish_dns_servers);
1424 avahi_strfreev(resolv_conf);
1426 if (wrote_pid_file) {
1427 #ifdef ENABLE_CHROOT
1428 avahi_chroot_helper_unlink(pid_file_proc());
1430 daemon_pid_file_remove();
1434 #ifdef ENABLE_CHROOT
1435 avahi_chroot_helper_shutdown();