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>
45 #ifdef HAVE_SYS_INOTIFY_H
46 #include <sys/inotify.h>
48 #include "inotify-nosys.h"
51 #include <libdaemon/dfork.h>
52 #include <libdaemon/dsignal.h>
53 #include <libdaemon/dlog.h>
54 #include <libdaemon/dpid.h>
56 #include <avahi-common/malloc.h>
57 #include <avahi-common/simple-watch.h>
58 #include <avahi-common/error.h>
59 #include <avahi-common/alternative.h>
60 #include <avahi-common/domain.h>
62 #include <avahi-core/core.h>
63 #include <avahi-core/publish.h>
64 #include <avahi-core/dns-srv-rr.h>
65 #include <avahi-core/log.h>
72 #include "setproctitle.h"
74 #include "simple-protocol.h"
75 #include "static-services.h"
76 #include "static-hosts.h"
77 #include "ini-file-parser.h"
80 #include "dbus-protocol.h"
83 AvahiServer *avahi_server = NULL;
84 AvahiSimplePoll *simple_poll_api = NULL;
85 static char *argv0 = NULL;
98 AvahiServerConfig server_config;
99 DaemonCommand command;
105 int fail_on_missing_dbus;
112 int modify_proc_title;
114 int disable_user_service_publishing;
115 int publish_resolv_conf;
116 char ** publish_dns_servers;
119 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
120 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
123 int rlimit_nproc_set;
128 #define RESOLV_CONF "/etc/resolv.conf"
130 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
131 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
133 static char **resolv_conf = NULL;
135 static DaemonConfig config;
137 static int has_prefix(const char *s, const char *prefix) {
142 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
145 static int load_resolv_conf(void) {
150 avahi_strfreev(resolv_conf);
154 f = avahi_chroot_helper_get_file(RESOLV_CONF);
156 f = fopen(RESOLV_CONF, "r");
160 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
164 resolv_conf = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
166 while (!feof(f) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
170 if (!(fgets(ln, sizeof(ln), f)))
173 ln[strcspn(ln, "\r\n#")] = 0;
174 p = ln + strspn(ln, "\t ");
176 if (has_prefix(p, "nameserver")) {
178 p += strspn(p, "\t ");
179 p[strcspn(p, "\t ")] = 0;
180 resolv_conf[i++] = avahi_strdup(p);
189 avahi_strfreev(resolv_conf);
199 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
206 g = avahi_s_entry_group_new(s, NULL, NULL);
208 assert(avahi_s_entry_group_is_empty(g));
210 for (p = l; *p; p++) {
213 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
214 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
216 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
217 avahi_s_entry_group_free(g);
218 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
223 avahi_s_entry_group_commit(g);
228 static void remove_dns_server_entry_groups(void) {
230 if (resolv_conf_entry_group)
231 avahi_s_entry_group_reset(resolv_conf_entry_group);
233 if (dns_servers_entry_group)
234 avahi_s_entry_group_reset(dns_servers_entry_group);
237 static void update_wide_area_servers(void) {
238 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
243 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
247 for (p = resolv_conf; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
248 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
249 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
254 avahi_server_set_wide_area_servers(avahi_server, a, n);
257 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
258 DaemonConfig *c = userdata;
263 /* This function is possibly called before the global variable
264 * avahi_server has been set, therefore we do it explicitly */
269 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
270 dbus_protocol_server_state_changed(state);
274 case AVAHI_SERVER_RUNNING:
275 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));
277 avahi_set_proc_title(argv0, "%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
279 static_service_add_to_server();
280 static_hosts_add_to_server();
282 remove_dns_server_entry_groups();
284 if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
285 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
287 if (c->publish_dns_servers && c->publish_dns_servers[0])
288 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
290 simple_protocol_restart_queries();
293 case AVAHI_SERVER_COLLISION: {
296 avahi_set_proc_title(argv0, "%s: collision", argv0);
298 static_service_remove_from_server();
299 static_hosts_remove_from_server();
300 remove_dns_server_entry_groups();
302 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
303 avahi_log_warn("Host name conflict, retrying with <%s>", n);
304 avahi_server_set_host_name(s, n);
310 case AVAHI_SERVER_FAILURE:
312 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
313 avahi_simple_poll_quit(simple_poll_api);
316 case AVAHI_SERVER_REGISTERING:
318 avahi_set_proc_title(argv0, "%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
320 static_service_remove_from_server();
321 static_hosts_remove_from_server();
322 remove_dns_server_entry_groups();
326 case AVAHI_SERVER_INVALID:
332 static void help(FILE *f) {
335 " -h --help Show this help\n"
336 " -D --daemonize Daemonize after startup (implies -s)\n"
337 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
338 " -k --kill Kill a running daemon\n"
339 " -r --reload Request a running daemon to reload static services\n"
340 " -c --check Return 0 if a daemon is already running\n"
341 " -V --version Show version\n"
342 " -f --file=FILE Load the specified configuration file instead of\n"
343 " "AVAHI_CONFIG_FILE"\n"
344 " --no-rlimits Don't enforce resource limits\n"
345 " --no-drop-root Don't drop privileges\n"
347 " --no-chroot Don't chroot()\n"
349 " --no-proc-title Don't modify process title\n"
350 " --debug Increase verbosity\n",
355 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
359 OPTION_NO_RLIMITS = 256,
364 OPTION_NO_PROC_TITLE,
368 static const struct option long_options[] = {
369 { "help", no_argument, NULL, 'h' },
370 { "daemonize", no_argument, NULL, 'D' },
371 { "kill", no_argument, NULL, 'k' },
372 { "version", no_argument, NULL, 'V' },
373 { "file", required_argument, NULL, 'f' },
374 { "reload", no_argument, NULL, 'r' },
375 { "check", no_argument, NULL, 'c' },
376 { "syslog", no_argument, NULL, 's' },
377 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
378 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
380 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
382 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
383 { "debug", no_argument, NULL, OPTION_DEBUG },
389 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
396 c->command = DAEMON_HELP;
402 c->command = DAEMON_KILL;
405 c->command = DAEMON_VERSION;
408 avahi_free(c->config_file);
409 c->config_file = avahi_strdup(optarg);
412 c->command = DAEMON_RELOAD;
415 c->command = DAEMON_CHECK;
417 case OPTION_NO_RLIMITS:
420 case OPTION_NO_DROP_ROOT:
424 case OPTION_NO_CHROOT:
428 case OPTION_NO_PROC_TITLE:
429 c->modify_proc_title = 0;
440 fprintf(stderr, "Too many arguments\n");
447 static int is_yes(const char *s) {
450 return *s == 'y' || *s == 'Y' || *s == '1' || *s == 't' || *s == 'T';
453 static int load_config_file(DaemonConfig *c) {
456 AvahiIniFileGroup *g;
460 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
463 for (g = f->groups; g; g = g->groups_next) {
465 if (strcasecmp(g->name, "server") == 0) {
468 for (p = g->pairs; p; p = p->pairs_next) {
470 if (strcasecmp(p->key, "host-name") == 0) {
471 avahi_free(c->server_config.host_name);
472 c->server_config.host_name = avahi_strdup(p->value);
473 } else if (strcasecmp(p->key, "domain-name") == 0) {
474 avahi_free(c->server_config.domain_name);
475 c->server_config.domain_name = avahi_strdup(p->value);
476 } else if (strcasecmp(p->key, "browse-domains") == 0) {
479 e = avahi_split_csv(p->value);
481 for (t = e; *t; t++) {
482 char cleaned[AVAHI_DOMAIN_NAME_MAX];
484 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
485 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
490 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
494 } else if (strcasecmp(p->key, "use-ipv4") == 0)
495 c->server_config.use_ipv4 = is_yes(p->value);
496 else if (strcasecmp(p->key, "use-ipv6") == 0)
497 c->server_config.use_ipv6 = is_yes(p->value);
498 else if (strcasecmp(p->key, "check-response-ttl") == 0)
499 c->server_config.check_response_ttl = is_yes(p->value);
500 else if (strcasecmp(p->key, "allow-point-to-point") == 0)
501 c->server_config.allow_point_to_point = is_yes(p->value);
502 else if (strcasecmp(p->key, "use-iff-running") == 0)
503 c->server_config.use_iff_running = is_yes(p->value);
504 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
505 c->server_config.disallow_other_stacks = is_yes(p->value);
507 else if (strcasecmp(p->key, "enable-dbus") == 0) {
509 if (*(p->value) == 'w' || *(p->value) == 'W') {
510 c->fail_on_missing_dbus = 0;
512 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
513 c->fail_on_missing_dbus = 1;
521 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
526 } else if (strcasecmp(g->name, "publish") == 0) {
529 for (p = g->pairs; p; p = p->pairs_next) {
531 if (strcasecmp(p->key, "publish-addresses") == 0)
532 c->server_config.publish_addresses = is_yes(p->value);
533 else if (strcasecmp(p->key, "publish-hinfo") == 0)
534 c->server_config.publish_hinfo = is_yes(p->value);
535 else if (strcasecmp(p->key, "publish-workstation") == 0)
536 c->server_config.publish_workstation = is_yes(p->value);
537 else if (strcasecmp(p->key, "publish-domain") == 0)
538 c->server_config.publish_domain = is_yes(p->value);
539 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
540 c->publish_resolv_conf = is_yes(p->value);
541 else if (strcasecmp(p->key, "disable-publishing") == 0)
542 c->server_config.disable_publishing = is_yes(p->value);
543 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
544 c->disable_user_service_publishing = is_yes(p->value);
545 else if (strcasecmp(p->key, "add-service-cookie") == 0)
546 c->server_config.add_service_cookie = is_yes(p->value);
547 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
548 avahi_strfreev(c->publish_dns_servers);
549 c->publish_dns_servers = avahi_split_csv(p->value);
550 } else if (strcasecmp(p->key, "publish-a-on-ipv6") == 0)
551 c->server_config.publish_a_on_ipv6 = is_yes(p->value);
552 else if (strcasecmp(p->key, "publish-aaaa-on-ipv4") == 0)
553 c->server_config.publish_aaaa_on_ipv4 = is_yes(p->value);
555 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
560 } else if (strcasecmp(g->name, "wide-area") == 0) {
563 for (p = g->pairs; p; p = p->pairs_next) {
565 if (strcasecmp(p->key, "enable-wide-area") == 0)
566 c->server_config.enable_wide_area = is_yes(p->value);
568 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
573 } else if (strcasecmp(g->name, "reflector") == 0) {
576 for (p = g->pairs; p; p = p->pairs_next) {
578 if (strcasecmp(p->key, "enable-reflector") == 0)
579 c->server_config.enable_reflector = is_yes(p->value);
580 else if (strcasecmp(p->key, "reflect-ipv") == 0)
581 c->server_config.reflect_ipv = is_yes(p->value);
583 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
588 } else if (strcasecmp(g->name, "rlimits") == 0) {
591 for (p = g->pairs; p; p = p->pairs_next) {
593 if (strcasecmp(p->key, "rlimit-as") == 0) {
594 c->rlimit_as_set = 1;
595 c->rlimit_as = atoi(p->value);
596 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
597 c->rlimit_core_set = 1;
598 c->rlimit_core = atoi(p->value);
599 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
600 c->rlimit_data_set = 1;
601 c->rlimit_data = atoi(p->value);
602 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
603 c->rlimit_fsize_set = 1;
604 c->rlimit_fsize = atoi(p->value);
605 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
606 c->rlimit_nofile_set = 1;
607 c->rlimit_nofile = atoi(p->value);
608 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
609 c->rlimit_stack_set = 1;
610 c->rlimit_stack = atoi(p->value);
611 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
613 c->rlimit_nproc_set = 1;
614 c->rlimit_nproc = atoi(p->value);
616 avahi_log_error("Ignoring configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
619 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
626 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
636 avahi_ini_file_free(f);
641 static void log_function(AvahiLogLevel level, const char *txt) {
643 static const int log_level_map[] = {
651 assert(level < AVAHI_LOG_LEVEL_MAX);
654 if (!config.debug && level == AVAHI_LOG_DEBUG)
657 daemon_log(log_level_map[level], "%s", txt);
660 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
661 avahi_log_info("%s", text);
666 static int inotify_fd = -1;
668 static void add_inotify_watches(void) {
670 /* We ignore the return values, because one or more of these files
671 * might not exist and we're OK with that. In addition we never
672 * want to remove these watches, hence we keep their ids? */
675 c = config.use_chroot;
678 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|IN_ONLYDIR);
679 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|IN_ONLYDIR);
684 static void reload_config(void) {
687 /* Refresh in case the config dirs have been removed */
688 add_inotify_watches();
692 static_service_load(config.use_chroot);
693 static_hosts_load(config.use_chroot);
695 static_service_load(0);
696 static_hosts_load(0);
698 static_service_add_to_server();
699 static_hosts_add_to_server();
701 if (resolv_conf_entry_group)
702 avahi_s_entry_group_reset(resolv_conf_entry_group);
706 update_wide_area_servers();
708 if (config.publish_resolv_conf && resolv_conf && resolv_conf[0])
709 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
714 static void inotify_callback(AvahiWatch *watch, int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
718 assert(fd == inotify_fd);
721 ioctl(inotify_fd, FIONREAD, &n);
725 buffer = avahi_malloc(n);
726 if (read(inotify_fd, buffer, n) < 0 ) {
728 avahi_log_error("Failed to read inotify event: %s", avahi_strerror(errno));
733 avahi_log_info("Files changed, reloading.");
739 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
741 const AvahiPoll *poll_api;
744 assert(simple_poll_api);
746 poll_api = avahi_simple_poll_get(simple_poll_api);
748 if ((sig = daemon_signal_next()) <= 0) {
749 avahi_log_error("daemon_signal_next() failed");
750 poll_api->watch_free(watch);
760 sig == SIGINT ? "SIGINT" :
761 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
762 avahi_simple_poll_quit(simple_poll_api);
766 avahi_log_info("Got SIGHUP, reloading.");
772 avahi_log_info("Got SIGUSR1, dumping record data.");
773 avahi_server_dump(avahi_server, dump, NULL);
777 avahi_log_warn("Got spurious signal, ignoring.");
782 /* Imported from ../avahi-client/nss-check.c */
783 int avahi_nss_support(void);
785 static int run_server(DaemonConfig *c) {
788 const AvahiPoll *poll_api = NULL;
789 AvahiWatch *sig_watch = NULL;
790 int retval_is_sent = 0;
792 AvahiWatch *inotify_watch = NULL;
797 if (!(nss_support = avahi_nss_support()))
798 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
800 if (!(simple_poll_api = avahi_simple_poll_new())) {
801 avahi_log_error("Failed to create main loop object.");
805 poll_api = avahi_simple_poll_get(simple_poll_api);
807 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
808 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
812 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
813 avahi_log_error( "Failed to create signal watcher");
817 if (simple_protocol_setup(poll_api) < 0)
821 if (c->enable_dbus) {
822 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing, !c->fail_on_missing_dbus
824 && !config.use_chroot
828 avahi_log_warn("WARNING: Failed to contact D-Bus daemon.");
830 if (c->fail_on_missing_dbus)
838 if (config.drop_root && config.use_chroot) {
839 if (chroot(AVAHI_CONFIG_DIR) < 0) {
840 avahi_log_error("Failed to chroot(): %s", strerror(errno));
844 avahi_log_info("Successfully called chroot().");
847 if (avahi_caps_drop_all() < 0) {
848 avahi_log_error("Failed to drop capabilities.");
851 avahi_log_info("Successfully dropped remaining capabilities.");
857 if ((inotify_fd = inotify_init()) < 0)
858 avahi_log_warn( "Failed to initialize inotify: %s", strerror(errno));
860 add_inotify_watches();
862 if (!(inotify_watch = poll_api->watch_new(poll_api, inotify_fd, AVAHI_WATCH_IN, inotify_callback, NULL))) {
863 avahi_log_error( "Failed to create inotify watcher");
871 static_service_load(config.use_chroot);
872 static_hosts_load(config.use_chroot);
874 static_service_load(0);
875 static_hosts_load(0);
878 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
879 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
883 update_wide_area_servers();
886 daemon_retval_send(0);
891 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
893 /* We handle signals through an FD, so let's continue */
897 avahi_log_error("poll(): %s", strerror(errno));
907 static_service_remove_from_server();
908 static_service_free_all();
910 static_hosts_remove_from_server();
911 static_hosts_free_all();
913 remove_dns_server_entry_groups();
915 simple_protocol_shutdown();
919 dbus_protocol_shutdown();
923 avahi_server_free(avahi_server);
927 daemon_signal_done();
930 poll_api->watch_free(sig_watch);
934 poll_api->watch_free(inotify_watch);
939 if (simple_poll_api) {
940 avahi_simple_poll_free(simple_poll_api);
941 simple_poll_api = NULL;
944 if (!retval_is_sent && c->daemonize)
945 daemon_retval_send(1);
950 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
952 static int drop_root(void) {
957 if (!(pw = getpwnam(AVAHI_USER))) {
958 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
962 if (!(gr = getgrnam(AVAHI_GROUP))) {
963 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
967 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);
969 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
970 avahi_log_error("Failed to change group list: %s", strerror(errno));
974 #if defined(HAVE_SETRESGID)
975 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
976 #elif defined(HAVE_SETEGID)
977 if ((r = setgid(gr->gr_gid)) >= 0)
978 r = setegid(gr->gr_gid);
979 #elif defined(HAVE_SETREGID)
980 r = setregid(gr->gr_gid, gr->gr_gid);
982 #error "No API to drop priviliges"
986 avahi_log_error("Failed to change GID: %s", strerror(errno));
990 #if defined(HAVE_SETRESUID)
991 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
992 #elif defined(HAVE_SETEUID)
993 if ((r = setuid(pw->pw_uid)) >= 0)
994 r = seteuid(pw->pw_uid);
995 #elif defined(HAVE_SETREUID)
996 r = setreuid(pw->pw_uid, pw->pw_uid);
998 #error "No API to drop priviliges"
1002 avahi_log_error("Failed to change UID: %s", strerror(errno));
1006 set_env("USER", pw->pw_name);
1007 set_env("LOGNAME", pw->pw_name);
1008 set_env("HOME", pw->pw_dir);
1010 avahi_log_info("Successfully dropped root privileges.");
1015 static const char* pid_file_proc(void) {
1016 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
1019 static int make_runtime_dir(void) {
1022 int reset_umask = 0;
1027 if (!(pw = getpwnam(AVAHI_USER))) {
1028 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
1032 if (!(gr = getgrnam(AVAHI_GROUP))) {
1033 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
1040 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
1041 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
1045 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
1047 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
1048 avahi_log_error("stat(): %s\n", strerror(errno));
1052 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
1053 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
1065 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
1067 rl.rlim_cur = rl.rlim_max = limit;
1069 if (setrlimit(resource, &rl) < 0)
1070 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
1073 static void enforce_rlimits(void) {
1075 if (config.rlimit_as_set)
1076 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
1078 if (config.rlimit_core_set)
1079 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
1080 if (config.rlimit_data_set)
1081 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
1082 if (config.rlimit_fsize_set)
1083 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
1084 if (config.rlimit_nofile_set)
1085 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
1086 if (config.rlimit_stack_set)
1087 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
1089 if (config.rlimit_nproc_set)
1090 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
1093 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
1094 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
1095 /* We don't need locked memory */
1096 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
1100 #define RANDOM_DEVICE "/dev/urandom"
1102 static void init_rand_seed(void) {
1106 /* Try to initialize seed from /dev/urandom, to make it a little
1107 * less predictable, and to make sure that multiple machines
1108 * booted at the same time choose different random seeds. */
1109 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1110 read(fd, &seed, sizeof(seed));
1114 /* If the initialization failed by some reason, we add the time to the seed*/
1115 seed ^= (unsigned) time(NULL);
1120 int main(int argc, char *argv[]) {
1122 int wrote_pid_file = 0;
1124 avahi_set_log_function(log_function);
1128 avahi_server_config_init(&config.server_config);
1129 config.command = DAEMON_RUN;
1130 config.daemonize = 0;
1131 config.config_file = NULL;
1133 config.enable_dbus = 1;
1134 config.fail_on_missing_dbus = 1;
1137 config.drop_root = 1;
1138 config.set_rlimits = 1;
1139 #ifdef ENABLE_CHROOT
1140 config.use_chroot = 1;
1142 config.modify_proc_title = 1;
1144 config.disable_user_service_publishing = 0;
1145 config.publish_dns_servers = NULL;
1146 config.publish_resolv_conf = 0;
1147 config.use_syslog = 0;
1149 config.rlimit_as_set = 0;
1150 config.rlimit_core_set = 0;
1151 config.rlimit_data_set = 0;
1152 config.rlimit_fsize_set = 0;
1153 config.rlimit_nofile_set = 0;
1154 config.rlimit_stack_set = 0;
1156 config.rlimit_nproc_set = 0;
1159 if ((argv0 = strrchr(argv[0], '/')))
1160 argv0 = avahi_strdup(argv0 + 1);
1162 argv0 = avahi_strdup(argv[0]);
1164 daemon_pid_file_ident = (const char *) argv0;
1165 daemon_log_ident = (char*) argv0;
1166 daemon_pid_file_proc = pid_file_proc;
1168 if (parse_command_line(&config, argc, argv) < 0)
1171 if (config.modify_proc_title)
1172 avahi_init_proc_title(argc, argv);
1174 #ifdef ENABLE_CHROOT
1175 config.use_chroot = config.use_chroot && config.drop_root;
1178 if (config.command == DAEMON_HELP) {
1181 } else if (config.command == DAEMON_VERSION) {
1182 printf("%s "PACKAGE_VERSION"\n", argv0);
1184 } else if (config.command == DAEMON_KILL) {
1185 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1186 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1192 } else if (config.command == DAEMON_RELOAD) {
1193 if (daemon_pid_file_kill(SIGHUP) < 0) {
1194 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1200 } else if (config.command == DAEMON_CHECK)
1201 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1202 else if (config.command == DAEMON_RUN) {
1205 if (getuid() != 0 && config.drop_root) {
1206 avahi_log_error("This program is intended to be run as root.");
1210 if ((pid = daemon_pid_file_is_running()) >= 0) {
1211 avahi_log_error("Daemon already running on PID %u", pid);
1215 if (load_config_file(&config) < 0)
1218 if (config.daemonize) {
1219 daemon_retval_init();
1221 if ((pid = daemon_fork()) < 0)
1223 else if (pid != 0) {
1227 if ((ret = daemon_retval_wait(20)) < 0) {
1228 avahi_log_error("Could not receive return value from daemon process.");
1239 if (config.use_syslog || config.daemonize)
1240 daemon_log_use = DAEMON_LOG_SYSLOG;
1242 if (make_runtime_dir() < 0)
1245 if (config.drop_root) {
1246 #ifdef ENABLE_CHROOT
1247 if (config.use_chroot)
1248 if (avahi_caps_reduce() < 0)
1252 if (drop_root() < 0)
1255 #ifdef ENABLE_CHROOT
1256 if (config.use_chroot)
1257 if (avahi_caps_reduce2() < 0)
1262 if (daemon_pid_file_create() < 0) {
1263 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1265 if (config.daemonize)
1266 daemon_retval_send(1);
1271 if (config.set_rlimits)
1276 #ifdef ENABLE_CHROOT
1277 if (config.drop_root && config.use_chroot)
1278 if (avahi_chroot_helper_start(argv0) < 0) {
1279 avahi_log_error("failed to start chroot() helper daemon.");
1283 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1285 avahi_set_proc_title(argv0, "%s: starting up", argv0);
1287 if (run_server(&config) == 0)
1293 if (config.daemonize)
1294 daemon_retval_done();
1296 avahi_server_config_free(&config.server_config);
1297 avahi_free(config.config_file);
1298 avahi_strfreev(config.publish_dns_servers);
1299 avahi_strfreev(resolv_conf);
1301 if (wrote_pid_file) {
1302 #ifdef ENABLE_CHROOT
1303 avahi_chroot_helper_unlink(pid_file_proc());
1305 daemon_pid_file_remove();
1309 #ifdef ENABLE_CHROOT
1310 avahi_chroot_helper_shutdown();