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
41 #include <sys/resource.h>
42 #include <sys/socket.h>
44 #include <libdaemon/dfork.h>
45 #include <libdaemon/dsignal.h>
46 #include <libdaemon/dlog.h>
47 #include <libdaemon/dpid.h>
49 #include <avahi-common/malloc.h>
50 #include <avahi-common/simple-watch.h>
51 #include <avahi-common/error.h>
52 #include <avahi-common/alternative.h>
53 #include <avahi-common/domain.h>
55 #include <avahi-core/core.h>
56 #include <avahi-core/publish.h>
57 #include <avahi-core/dns-srv-rr.h>
58 #include <avahi-core/log.h>
65 #include "setproctitle.h"
67 #include "simple-protocol.h"
68 #include "static-services.h"
69 #include "static-hosts.h"
70 #include "ini-file-parser.h"
73 #include "dbus-protocol.h"
76 AvahiServer *avahi_server = NULL;
77 AvahiSimplePoll *simple_poll_api = NULL;
78 static char *argv0 = NULL;
91 AvahiServerConfig server_config;
92 DaemonCommand command;
98 int fail_on_missing_dbus;
105 int modify_proc_title;
107 int disable_user_service_publishing;
108 int publish_resolv_conf;
109 char ** publish_dns_servers;
112 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
113 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
116 int rlimit_nproc_set;
121 #define RESOLV_CONF "/etc/resolv.conf"
123 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
124 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
126 static char **resolv_conf = NULL;
128 static DaemonConfig config;
130 static int has_prefix(const char *s, const char *prefix) {
135 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
138 static int load_resolv_conf(void) {
143 avahi_strfreev(resolv_conf);
147 f = avahi_chroot_helper_get_file(RESOLV_CONF);
149 f = fopen(RESOLV_CONF, "r");
153 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
157 resolv_conf = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
159 while (!feof(f) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
163 if (!(fgets(ln, sizeof(ln), f)))
166 ln[strcspn(ln, "\r\n#")] = 0;
167 p = ln + strspn(ln, "\t ");
169 if (has_prefix(p, "nameserver")) {
171 p += strspn(p, "\t ");
172 p[strcspn(p, "\t ")] = 0;
173 resolv_conf[i++] = avahi_strdup(p);
182 avahi_strfreev(resolv_conf);
192 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
199 g = avahi_s_entry_group_new(s, NULL, NULL);
201 assert(avahi_s_entry_group_is_empty(g));
203 for (p = l; *p; p++) {
206 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
207 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
209 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
210 avahi_s_entry_group_free(g);
211 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
216 avahi_s_entry_group_commit(g);
221 static void remove_dns_server_entry_groups(void) {
223 if (resolv_conf_entry_group)
224 avahi_s_entry_group_reset(resolv_conf_entry_group);
226 if (dns_servers_entry_group)
227 avahi_s_entry_group_reset(dns_servers_entry_group);
230 static void update_wide_area_servers(void) {
231 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
236 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
240 for (p = resolv_conf; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
241 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
242 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
247 avahi_server_set_wide_area_servers(avahi_server, a, n);
250 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
251 DaemonConfig *c = userdata;
256 /* This function is possibly called before the global variable
257 * avahi_server has been set, therefore we do it explicitly */
262 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
263 dbus_protocol_server_state_changed(state);
267 case AVAHI_SERVER_RUNNING:
268 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));
270 avahi_set_proc_title("%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
272 static_service_add_to_server();
273 static_hosts_add_to_server();
275 remove_dns_server_entry_groups();
277 if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
278 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
280 if (c->publish_dns_servers && c->publish_dns_servers[0])
281 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
283 simple_protocol_restart_queries();
286 case AVAHI_SERVER_COLLISION: {
289 static_service_remove_from_server();
290 static_hosts_remove_from_server();
292 remove_dns_server_entry_groups();
294 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
295 avahi_log_warn("Host name conflict, retrying with <%s>", n);
296 avahi_server_set_host_name(s, n);
299 avahi_set_proc_title("%s: collision", argv0);
304 case AVAHI_SERVER_FAILURE:
306 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
307 avahi_simple_poll_quit(simple_poll_api);
310 case AVAHI_SERVER_REGISTERING:
311 avahi_set_proc_title("%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
314 case AVAHI_SERVER_INVALID:
320 static void help(FILE *f) {
323 " -h --help Show this help\n"
324 " -D --daemonize Daemonize after startup (implies -s)\n"
325 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
326 " -k --kill Kill a running daemon\n"
327 " -r --reload Request a running daemon to reload static services\n"
328 " -c --check Return 0 if a daemon is already running\n"
329 " -V --version Show version\n"
330 " -f --file=FILE Load the specified configuration file instead of\n"
331 " "AVAHI_CONFIG_FILE"\n"
332 " --no-rlimits Don't enforce resource limits\n"
333 " --no-drop-root Don't drop privileges\n"
335 " --no-chroot Don't chroot()\n"
337 " --no-proc-title Don't modify process title\n"
338 " --debug Increase verbosity\n",
343 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
347 OPTION_NO_RLIMITS = 256,
352 OPTION_NO_PROC_TITLE,
356 static const struct option long_options[] = {
357 { "help", no_argument, NULL, 'h' },
358 { "daemonize", no_argument, NULL, 'D' },
359 { "kill", no_argument, NULL, 'k' },
360 { "version", no_argument, NULL, 'V' },
361 { "file", required_argument, NULL, 'f' },
362 { "reload", no_argument, NULL, 'r' },
363 { "check", no_argument, NULL, 'c' },
364 { "syslog", no_argument, NULL, 's' },
365 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
366 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
368 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
370 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
371 { "debug", no_argument, NULL, OPTION_DEBUG },
378 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
385 c->command = DAEMON_HELP;
391 c->command = DAEMON_KILL;
394 c->command = DAEMON_VERSION;
397 avahi_free(c->config_file);
398 c->config_file = avahi_strdup(optarg);
401 c->command = DAEMON_RELOAD;
404 c->command = DAEMON_CHECK;
406 case OPTION_NO_RLIMITS:
409 case OPTION_NO_DROP_ROOT:
413 case OPTION_NO_CHROOT:
417 case OPTION_NO_PROC_TITLE:
418 c->modify_proc_title = 0;
424 fprintf(stderr, "Invalid command line argument: %c\n", o);
430 fprintf(stderr, "Too many arguments\n");
437 static int is_yes(const char *s) {
440 return *s == 'y' || *s == 'Y';
443 static int load_config_file(DaemonConfig *c) {
446 AvahiIniFileGroup *g;
450 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
453 for (g = f->groups; g; g = g->groups_next) {
455 if (strcasecmp(g->name, "server") == 0) {
458 for (p = g->pairs; p; p = p->pairs_next) {
460 if (strcasecmp(p->key, "host-name") == 0) {
461 avahi_free(c->server_config.host_name);
462 c->server_config.host_name = avahi_strdup(p->value);
463 } else if (strcasecmp(p->key, "domain-name") == 0) {
464 avahi_free(c->server_config.domain_name);
465 c->server_config.domain_name = avahi_strdup(p->value);
466 } else if (strcasecmp(p->key, "browse-domains") == 0) {
469 e = avahi_split_csv(p->value);
471 for (t = e; *t; t++) {
472 char cleaned[AVAHI_DOMAIN_NAME_MAX];
474 if (!avahi_normalize_name(*t, cleaned, sizeof(cleaned))) {
475 avahi_log_error("Invalid domain name \"%s\" for key \"%s\" in group \"%s\"\n", *t, p->key, g->name);
480 c->server_config.browse_domains = avahi_string_list_add(c->server_config.browse_domains, cleaned);
484 } else if (strcasecmp(p->key, "use-ipv4") == 0)
485 c->server_config.use_ipv4 = is_yes(p->value);
486 else if (strcasecmp(p->key, "use-ipv6") == 0)
487 c->server_config.use_ipv6 = is_yes(p->value);
488 else if (strcasecmp(p->key, "check-response-ttl") == 0)
489 c->server_config.check_response_ttl = is_yes(p->value);
490 else if (strcasecmp(p->key, "use-iff-running") == 0)
491 c->server_config.use_iff_running = is_yes(p->value);
492 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
493 c->server_config.disallow_other_stacks = is_yes(p->value);
495 else if (strcasecmp(p->key, "enable-dbus") == 0) {
497 if (*(p->value) == 'w' || *(p->value) == 'W') {
498 c->fail_on_missing_dbus = 0;
500 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
501 c->fail_on_missing_dbus = 1;
509 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
514 } else if (strcasecmp(g->name, "publish") == 0) {
517 for (p = g->pairs; p; p = p->pairs_next) {
519 if (strcasecmp(p->key, "publish-addresses") == 0)
520 c->server_config.publish_addresses = is_yes(p->value);
521 else if (strcasecmp(p->key, "publish-hinfo") == 0)
522 c->server_config.publish_hinfo = is_yes(p->value);
523 else if (strcasecmp(p->key, "publish-workstation") == 0)
524 c->server_config.publish_workstation = is_yes(p->value);
525 else if (strcasecmp(p->key, "publish-domain") == 0)
526 c->server_config.publish_domain = is_yes(p->value);
527 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
528 c->publish_resolv_conf = is_yes(p->value);
529 else if (strcasecmp(p->key, "disable-publishing") == 0)
530 c->server_config.disable_publishing = is_yes(p->value);
531 else if (strcasecmp(p->key, "disable-user-service-publishing") == 0)
532 c->disable_user_service_publishing = is_yes(p->value);
533 else if (strcasecmp(p->key, "add-service-cookie") == 0)
534 c->server_config.add_service_cookie = is_yes(p->value);
535 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
536 avahi_strfreev(c->publish_dns_servers);
537 c->publish_dns_servers = avahi_split_csv(p->value);
539 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
544 } else if (strcasecmp(g->name, "wide-area") == 0) {
547 for (p = g->pairs; p; p = p->pairs_next) {
549 if (strcasecmp(p->key, "enable-wide-area") == 0)
550 c->server_config.enable_wide_area = is_yes(p->value);
552 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
557 } else if (strcasecmp(g->name, "reflector") == 0) {
560 for (p = g->pairs; p; p = p->pairs_next) {
562 if (strcasecmp(p->key, "enable-reflector") == 0)
563 c->server_config.enable_reflector = is_yes(p->value);
564 else if (strcasecmp(p->key, "reflect-ipv") == 0)
565 c->server_config.reflect_ipv = is_yes(p->value);
567 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
572 } else if (strcasecmp(g->name, "rlimits") == 0) {
575 for (p = g->pairs; p; p = p->pairs_next) {
577 if (strcasecmp(p->key, "rlimit-as") == 0) {
578 c->rlimit_as_set = 1;
579 c->rlimit_as = atoi(p->value);
580 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
581 c->rlimit_core_set = 1;
582 c->rlimit_core = atoi(p->value);
583 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
584 c->rlimit_data_set = 1;
585 c->rlimit_data = atoi(p->value);
586 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
587 c->rlimit_fsize_set = 1;
588 c->rlimit_fsize = atoi(p->value);
589 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
590 c->rlimit_nofile_set = 1;
591 c->rlimit_nofile = atoi(p->value);
592 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
593 c->rlimit_stack_set = 1;
594 c->rlimit_stack = atoi(p->value);
596 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
597 c->rlimit_nproc_set = 1;
598 c->rlimit_nproc = atoi(p->value);
601 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
608 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
618 avahi_ini_file_free(f);
623 static void log_function(AvahiLogLevel level, const char *txt) {
625 static const int log_level_map[] = {
633 assert(level < AVAHI_LOG_LEVEL_MAX);
636 if (!config.debug && level == AVAHI_LOG_DEBUG)
639 daemon_log(log_level_map[level], "%s", txt);
642 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
643 avahi_log_info("%s", text);
646 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
648 const AvahiPoll *poll_api;
651 assert(simple_poll_api);
653 poll_api = avahi_simple_poll_get(simple_poll_api);
655 if ((sig = daemon_signal_next()) <= 0) {
656 avahi_log_error("daemon_signal_next() failed");
657 poll_api->watch_free(watch);
667 sig == SIGINT ? "SIGINT" :
668 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
669 avahi_simple_poll_quit(simple_poll_api);
673 avahi_log_info("Got SIGHUP, reloading.");
675 static_service_load(config.use_chroot);
676 static_hosts_load(config.use_chroot);
678 static_service_load(0);
679 static_hosts_load(0);
681 static_service_add_to_server();
682 static_service_remove_from_server();
684 if (resolv_conf_entry_group)
685 avahi_s_entry_group_reset(resolv_conf_entry_group);
689 update_wide_area_servers();
691 if (config.publish_resolv_conf && resolv_conf && resolv_conf[0])
692 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
697 avahi_log_info("Got SIGUSR1, dumping record data.");
698 avahi_server_dump(avahi_server, dump, NULL);
702 avahi_log_warn("Got spurious signal, ignoring.");
707 /* Imported from ../avahi-client/nss-check.c */
708 int avahi_nss_support(void);
710 static int run_server(DaemonConfig *c) {
713 const AvahiPoll *poll_api = NULL;
714 AvahiWatch *sig_watch = NULL;
715 int retval_is_sent = 0;
719 if (!(nss_support = avahi_nss_support()))
720 avahi_log_warn("WARNING: No NSS support for mDNS detected, consider installing nss-mdns!");
722 if (!(simple_poll_api = avahi_simple_poll_new())) {
723 avahi_log_error("Failed to create main loop object.");
727 poll_api = avahi_simple_poll_get(simple_poll_api);
729 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
730 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
734 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
735 avahi_log_error( "Failed to create signal watcher");
739 if (simple_protocol_setup(poll_api) < 0)
743 if (c->enable_dbus) {
744 if (dbus_protocol_setup(poll_api, config.disable_user_service_publishing) < 0) {
746 if (c->fail_on_missing_dbus)
749 avahi_log_warn("WARNING: Failed to contact D-BUS daemon, disabling D-BUS support.");
757 if (config.drop_root && config.use_chroot) {
758 if (chroot(AVAHI_CONFIG_DIR) < 0) {
759 avahi_log_error("Failed to chroot(): %s", strerror(errno));
763 avahi_log_info("Successfully called chroot().");
766 if (avahi_caps_drop_all() < 0) {
767 avahi_log_error("Failed to drop capabilities.");
770 avahi_log_info("Successfully dropped remaining capabilities.");
777 static_service_load(config.use_chroot);
778 static_hosts_load(config.use_chroot);
780 static_service_load(0);
781 static_hosts_load(0);
784 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
785 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
789 update_wide_area_servers();
792 daemon_retval_send(0);
797 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
799 /* We handle signals through an FD, so let's continue */
803 avahi_log_error("poll(): %s", strerror(errno));
813 static_service_remove_from_server();
814 static_service_free_all();
816 static_hosts_remove_from_server();
817 static_hosts_free_all();
819 remove_dns_server_entry_groups();
821 simple_protocol_shutdown();
825 dbus_protocol_shutdown();
829 avahi_server_free(avahi_server);
833 daemon_signal_done();
836 poll_api->watch_free(sig_watch);
838 if (simple_poll_api) {
839 avahi_simple_poll_free(simple_poll_api);
840 simple_poll_api = NULL;
843 if (!retval_is_sent && c->daemonize)
844 daemon_retval_send(1);
849 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
851 static int drop_root(void) {
856 if (!(pw = getpwnam(AVAHI_USER))) {
857 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
861 if (!(gr = getgrnam(AVAHI_GROUP))) {
862 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
866 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);
868 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
869 avahi_log_error("Failed to change group list: %s", strerror(errno));
873 #if defined(HAVE_SETRESGID)
874 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
875 #elif defined(HAVE_SETEGID)
876 if ((r = setgid(gr->gr_gid)) >= 0)
877 r = setegid(gr->gr_gid);
878 #elif defined(HAVE_SETREGID)
879 r = setregid(gr->gr_gid, gr->gr_gid);
881 #error "No API to drop priviliges"
885 avahi_log_error("Failed to change GID: %s", strerror(errno));
889 #if defined(HAVE_SETRESUID)
890 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
891 #elif defined(HAVE_SETEUID)
892 if ((r = setuid(pw->pw_uid)) >= 0)
893 r = seteuid(pw->pw_uid);
894 #elif defined(HAVE_SETREUID)
895 r = setreuid(pw->pw_uid, pw->pw_uid);
897 #error "No API to drop priviliges"
901 avahi_log_error("Failed to change UID: %s", strerror(errno));
905 set_env("USER", pw->pw_name);
906 set_env("LOGNAME", pw->pw_name);
907 set_env("HOME", pw->pw_dir);
909 avahi_log_info("Successfully dropped root privileges.");
914 static const char* pid_file_proc(void) {
915 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
918 static int make_runtime_dir(void) {
926 if (!(pw = getpwnam(AVAHI_USER))) {
927 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
931 if (!(gr = getgrnam(AVAHI_GROUP))) {
932 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
939 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
940 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
944 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
946 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
947 avahi_log_error("stat(): %s\n", strerror(errno));
951 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
952 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
964 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
966 rl.rlim_cur = rl.rlim_max = limit;
968 if (setrlimit(resource, &rl) < 0)
969 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
972 static void enforce_rlimits(void) {
974 if (config.rlimit_as_set)
975 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
977 if (config.rlimit_core_set)
978 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
979 if (config.rlimit_data_set)
980 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
981 if (config.rlimit_fsize_set)
982 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
983 if (config.rlimit_nofile_set)
984 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
985 if (config.rlimit_stack_set)
986 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
988 if (config.rlimit_nproc_set)
989 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
992 /* the sysctl() call from iface-pfroute.c needs locked memory on FreeBSD */
993 #if defined(RLIMIT_MEMLOCK) && !defined(__FreeBSD__)
994 /* We don't need locked memory */
995 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
999 #define RANDOM_DEVICE "/dev/urandom"
1001 static void init_rand_seed(void) {
1005 /* Try to initialize seed from /dev/urandom, to make it a little
1006 * less predictable, and to make sure that multiple machines
1007 * booted at the same time choose different random seeds. */
1008 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
1009 read(fd, &seed, sizeof(seed));
1013 /* If the initialization failed by some reason, we add the time to the seed*/
1014 seed |= (unsigned) time(NULL);
1019 int main(int argc, char *argv[]) {
1021 int wrote_pid_file = 0;
1023 avahi_set_log_function(log_function);
1027 avahi_server_config_init(&config.server_config);
1028 config.command = DAEMON_RUN;
1029 config.daemonize = 0;
1030 config.config_file = NULL;
1032 config.enable_dbus = 1;
1033 config.fail_on_missing_dbus = 1;
1036 config.drop_root = 1;
1037 config.set_rlimits = 1;
1038 #ifdef ENABLE_CHROOT
1039 config.use_chroot = 1;
1041 config.modify_proc_title = 1;
1043 config.disable_user_service_publishing = 0;
1044 config.publish_dns_servers = NULL;
1045 config.publish_resolv_conf = 0;
1046 config.use_syslog = 0;
1048 config.rlimit_as_set = 0;
1049 config.rlimit_core_set = 0;
1050 config.rlimit_data_set = 0;
1051 config.rlimit_fsize_set = 0;
1052 config.rlimit_nofile_set = 0;
1053 config.rlimit_stack_set = 0;
1055 config.rlimit_nproc_set = 0;
1058 if ((argv0 = strrchr(argv[0], '/')))
1059 argv0 = avahi_strdup(argv0 + 1);
1061 argv0 = avahi_strdup(argv[0]);
1063 daemon_pid_file_ident = (const char *) argv0;
1064 daemon_log_ident = (char*) argv0;
1065 daemon_pid_file_proc = pid_file_proc;
1067 if (parse_command_line(&config, argc, argv) < 0)
1070 if (config.modify_proc_title)
1071 avahi_init_proc_title(argc, argv);
1073 #ifdef ENABLE_CHROOT
1074 config.use_chroot = config.use_chroot && config.drop_root;
1077 if (config.command == DAEMON_HELP) {
1080 } else if (config.command == DAEMON_VERSION) {
1081 printf("%s "PACKAGE_VERSION"\n", argv0);
1083 } else if (config.command == DAEMON_KILL) {
1084 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1085 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1091 } else if (config.command == DAEMON_RELOAD) {
1092 if (daemon_pid_file_kill(SIGHUP) < 0) {
1093 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1099 } else if (config.command == DAEMON_CHECK)
1100 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1101 else if (config.command == DAEMON_RUN) {
1104 if (getuid() != 0 && config.drop_root) {
1105 avahi_log_error("This program is intended to be run as root.");
1109 if ((pid = daemon_pid_file_is_running()) >= 0) {
1110 avahi_log_error("Daemon already running on PID %u", pid);
1114 if (load_config_file(&config) < 0)
1117 if (config.daemonize) {
1118 daemon_retval_init();
1120 if ((pid = daemon_fork()) < 0)
1122 else if (pid != 0) {
1126 if ((ret = daemon_retval_wait(20)) < 0) {
1127 avahi_log_error("Could not receive return value from daemon process.");
1138 if (config.use_syslog || config.daemonize)
1139 daemon_log_use = DAEMON_LOG_SYSLOG;
1141 if (make_runtime_dir() < 0)
1144 if (config.drop_root) {
1145 #ifdef ENABLE_CHROOT
1146 if (config.use_chroot)
1147 if (avahi_caps_reduce() < 0)
1151 if (drop_root() < 0)
1154 #ifdef ENABLE_CHROOT
1155 if (config.use_chroot)
1156 if (avahi_caps_reduce2() < 0)
1161 if (daemon_pid_file_create() < 0) {
1162 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1164 if (config.daemonize)
1165 daemon_retval_send(1);
1170 if (config.set_rlimits)
1175 #ifdef ENABLE_CHROOT
1176 if (config.drop_root && config.use_chroot)
1177 if (avahi_chroot_helper_start(argv0) < 0) {
1178 avahi_log_error("failed to start chroot() helper daemon.");
1182 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1184 avahi_set_proc_title("%s: starting up", argv0);
1186 if (run_server(&config) == 0)
1192 if (config.daemonize)
1193 daemon_retval_done();
1195 avahi_server_config_free(&config.server_config);
1196 avahi_free(config.config_file);
1197 avahi_strfreev(config.publish_dns_servers);
1198 avahi_strfreev(resolv_conf);
1200 if (wrote_pid_file) {
1201 #ifdef ENABLE_CHROOT
1202 avahi_chroot_helper_unlink(pid_file_proc());
1204 daemon_pid_file_remove();
1208 #ifdef ENABLE_CHROOT
1209 avahi_chroot_helper_shutdown();