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>
54 #include <avahi-core/core.h>
55 #include <avahi-core/publish.h>
56 #include <avahi-core/dns-srv-rr.h>
57 #include <avahi-core/log.h>
64 #include "setproctitle.h"
66 #include "simple-protocol.h"
67 #include "static-services.h"
68 #include "ini-file-parser.h"
71 #include "dbus-protocol.h"
74 AvahiServer *avahi_server = NULL;
75 AvahiSimplePoll *simple_poll_api = NULL;
76 static char *argv0 = NULL;
88 AvahiServerConfig server_config;
89 DaemonCommand command;
95 int fail_on_missing_dbus;
102 int modify_proc_title;
104 int publish_resolv_conf;
105 char ** publish_dns_servers;
108 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
109 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
112 int rlimit_nproc_set;
117 #define RESOLV_CONF "/etc/resolv.conf"
119 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
120 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
122 static char **resolv_conf = NULL;
124 static DaemonConfig config;
126 static int has_prefix(const char *s, const char *prefix) {
131 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
134 static int load_resolv_conf(void) {
139 avahi_strfreev(resolv_conf);
143 f = avahi_chroot_helper_get_file(RESOLV_CONF);
145 f = fopen(RESOLV_CONF, "r");
149 avahi_log_warn("Failed to open "RESOLV_CONF": %s", strerror(errno));
153 resolv_conf = avahi_new0(char*, AVAHI_WIDE_AREA_SERVERS_MAX+1);
155 while (!feof(f) && i < AVAHI_WIDE_AREA_SERVERS_MAX) {
159 if (!(fgets(ln, sizeof(ln), f)))
162 ln[strcspn(ln, "\r\n#")] = 0;
163 p = ln + strspn(ln, "\t ");
165 if (has_prefix(p, "nameserver")) {
167 p += strspn(p, "\t ");
168 p[strcspn(p, "\t ")] = 0;
169 resolv_conf[i++] = avahi_strdup(p);
178 avahi_strfreev(resolv_conf);
188 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
195 g = avahi_s_entry_group_new(s, NULL, NULL);
197 assert(avahi_s_entry_group_is_empty(g));
199 for (p = l; *p; p++) {
202 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
203 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
205 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
206 avahi_s_entry_group_free(g);
207 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
212 avahi_s_entry_group_commit(g);
217 static void remove_dns_server_entry_groups(void) {
219 if (resolv_conf_entry_group)
220 avahi_s_entry_group_reset(resolv_conf_entry_group);
222 if (dns_servers_entry_group)
223 avahi_s_entry_group_reset(dns_servers_entry_group);
226 static void update_wide_area_servers(void) {
227 AvahiAddress a[AVAHI_WIDE_AREA_SERVERS_MAX];
232 avahi_server_set_wide_area_servers(avahi_server, NULL, 0);
236 for (p = resolv_conf; *p && n < AVAHI_WIDE_AREA_SERVERS_MAX; p++) {
237 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
238 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
243 avahi_server_set_wide_area_servers(avahi_server, a, n);
246 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
247 DaemonConfig *c = userdata;
252 /* This function is possibly called before the global variable
253 * avahi_server has been set, therefore we do it explicitly */
258 if (c->enable_dbus && state != AVAHI_SERVER_INVALID && state != AVAHI_SERVER_FAILURE)
259 dbus_protocol_server_state_changed(state);
263 case AVAHI_SERVER_RUNNING:
264 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));
266 avahi_set_proc_title("%s: running [%s]", argv0, avahi_server_get_host_name_fqdn(s));
268 static_service_add_to_server();
270 remove_dns_server_entry_groups();
272 if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
273 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
275 if (c->publish_dns_servers && c->publish_dns_servers[0])
276 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
278 simple_protocol_restart_queries();
281 case AVAHI_SERVER_COLLISION: {
284 static_service_remove_from_server();
286 remove_dns_server_entry_groups();
288 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
289 avahi_log_warn("Host name conflict, retrying with <%s>", n);
290 avahi_server_set_host_name(s, n);
293 avahi_set_proc_title("%s: collision", argv0);
298 case AVAHI_SERVER_FAILURE:
300 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
301 avahi_simple_poll_quit(simple_poll_api);
304 case AVAHI_SERVER_REGISTERING:
305 avahi_set_proc_title("%s: registering [%s]", argv0, avahi_server_get_host_name_fqdn(s));
308 case AVAHI_SERVER_INVALID:
314 static void help(FILE *f) {
317 " -h --help Show this help\n"
318 " -D --daemonize Daemonize after startup (implies -s)\n"
319 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
320 " -k --kill Kill a running daemon\n"
321 " -r --reload Request a running daemon to reload static services\n"
322 " -c --check Return 0 if a daemon is already running\n"
323 " -V --version Show version\n"
324 " -f --file=FILE Load the specified configuration file instead of\n"
325 " "AVAHI_CONFIG_FILE"\n"
326 " --no-rlimits Don't enforce resource limits\n"
327 " --no-drop-root Don't drop privileges\n"
329 " --no-chroot Don't chroot()\n"
331 " --no-proc-title Don't modify process title\n"
332 " --debug Increase verbosity\n",
337 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
341 OPTION_NO_RLIMITS = 256,
346 OPTION_NO_PROC_TITLE,
350 static const struct option long_options[] = {
351 { "help", no_argument, NULL, 'h' },
352 { "daemonize", no_argument, NULL, 'D' },
353 { "kill", no_argument, NULL, 'k' },
354 { "version", no_argument, NULL, 'V' },
355 { "file", required_argument, NULL, 'f' },
356 { "reload", no_argument, NULL, 'r' },
357 { "check", no_argument, NULL, 'c' },
358 { "syslog", no_argument, NULL, 's' },
359 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
360 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
362 { "no-chroot", no_argument, NULL, OPTION_NO_CHROOT },
364 { "no-proc-title", no_argument, NULL, OPTION_NO_PROC_TITLE },
365 { "debug", no_argument, NULL, OPTION_DEBUG },
372 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
379 c->command = DAEMON_HELP;
385 c->command = DAEMON_KILL;
388 c->command = DAEMON_VERSION;
391 avahi_free(c->config_file);
392 c->config_file = avahi_strdup(optarg);
395 c->command = DAEMON_RELOAD;
398 c->command = DAEMON_CHECK;
400 case OPTION_NO_RLIMITS:
403 case OPTION_NO_DROP_ROOT:
407 case OPTION_NO_CHROOT:
411 case OPTION_NO_PROC_TITLE:
412 c->modify_proc_title = 0;
418 fprintf(stderr, "Invalid command line argument: %c\n", o);
424 fprintf(stderr, "Too many arguments\n");
431 static int is_yes(const char *s) {
434 return *s == 'y' || *s == 'Y';
437 static int load_config_file(DaemonConfig *c) {
440 AvahiIniFileGroup *g;
444 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
447 for (g = f->groups; g; g = g->groups_next) {
449 if (strcasecmp(g->name, "server") == 0) {
452 for (p = g->pairs; p; p = p->pairs_next) {
454 if (strcasecmp(p->key, "host-name") == 0) {
455 avahi_free(c->server_config.host_name);
456 c->server_config.host_name = avahi_strdup(p->value);
457 } else if (strcasecmp(p->key, "domain-name") == 0) {
458 avahi_free(c->server_config.domain_name);
459 c->server_config.domain_name = avahi_strdup(p->value);
460 } else if (strcasecmp(p->key, "use-ipv4") == 0)
461 c->server_config.use_ipv4 = is_yes(p->value);
462 else if (strcasecmp(p->key, "use-ipv6") == 0)
463 c->server_config.use_ipv6 = is_yes(p->value);
464 else if (strcasecmp(p->key, "check-response-ttl") == 0)
465 c->server_config.check_response_ttl = is_yes(p->value);
466 else if (strcasecmp(p->key, "use-iff-running") == 0)
467 c->server_config.use_iff_running = is_yes(p->value);
468 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
469 c->server_config.disallow_other_stacks = is_yes(p->value);
470 else if (strcasecmp(p->key, "enable-dbus") == 0) {
472 if (*(p->value) == 'w' || *(p->value) == 'W') {
473 c->fail_on_missing_dbus = 0;
475 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
476 c->fail_on_missing_dbus = 1;
481 } else if (strcasecmp(p->key, "drop-root") == 0)
482 c->drop_root = is_yes(p->value);
483 else if (strcasecmp(p->key, "add-service-cookie") == 0)
484 c->server_config.add_service_cookie = is_yes(p->value);
486 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
491 } else if (strcasecmp(g->name, "publish") == 0) {
494 for (p = g->pairs; p; p = p->pairs_next) {
496 if (strcasecmp(p->key, "publish-addresses") == 0)
497 c->server_config.publish_addresses = is_yes(p->value);
498 else if (strcasecmp(p->key, "publish-hinfo") == 0)
499 c->server_config.publish_hinfo = is_yes(p->value);
500 else if (strcasecmp(p->key, "publish-workstation") == 0)
501 c->server_config.publish_workstation = is_yes(p->value);
502 else if (strcasecmp(p->key, "publish-domain") == 0)
503 c->server_config.publish_domain = is_yes(p->value);
504 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
505 c->publish_resolv_conf = is_yes(p->value);
506 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
507 avahi_strfreev(c->publish_dns_servers);
508 c->publish_dns_servers = avahi_split_csv(p->value);
510 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
515 } else if (strcasecmp(g->name, "wide-area") == 0) {
518 for (p = g->pairs; p; p = p->pairs_next) {
520 if (strcasecmp(p->key, "enable-wide-area") == 0)
521 c->server_config.enable_wide_area = is_yes(p->value);
523 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
528 } else if (strcasecmp(g->name, "reflector") == 0) {
531 for (p = g->pairs; p; p = p->pairs_next) {
533 if (strcasecmp(p->key, "enable-reflector") == 0)
534 c->server_config.enable_reflector = is_yes(p->value);
535 else if (strcasecmp(p->key, "reflect-ipv") == 0)
536 c->server_config.reflect_ipv = is_yes(p->value);
538 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
543 } else if (strcasecmp(g->name, "rlimits") == 0) {
546 for (p = g->pairs; p; p = p->pairs_next) {
548 if (strcasecmp(p->key, "rlimit-as") == 0) {
549 c->rlimit_as_set = 1;
550 c->rlimit_as = atoi(p->value);
551 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
552 c->rlimit_core_set = 1;
553 c->rlimit_core = atoi(p->value);
554 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
555 c->rlimit_data_set = 1;
556 c->rlimit_data = atoi(p->value);
557 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
558 c->rlimit_fsize_set = 1;
559 c->rlimit_fsize = atoi(p->value);
560 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
561 c->rlimit_nofile_set = 1;
562 c->rlimit_nofile = atoi(p->value);
563 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
564 c->rlimit_stack_set = 1;
565 c->rlimit_stack = atoi(p->value);
567 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
568 c->rlimit_nproc_set = 1;
569 c->rlimit_nproc = atoi(p->value);
572 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
579 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
589 avahi_ini_file_free(f);
594 static void log_function(AvahiLogLevel level, const char *txt) {
596 static const int log_level_map[] = {
604 assert(level < AVAHI_LOG_LEVEL_MAX);
607 if (!config.debug && level == AVAHI_LOG_DEBUG)
610 daemon_log(log_level_map[level], "%s", txt);
613 static void dump(const char *text, AVAHI_GCC_UNUSED void* userdata) {
614 avahi_log_info("%s", text);
617 static void signal_callback(AvahiWatch *watch, AVAHI_GCC_UNUSED int fd, AVAHI_GCC_UNUSED AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
619 const AvahiPoll *poll_api;
622 assert(simple_poll_api);
624 poll_api = avahi_simple_poll_get(simple_poll_api);
626 if ((sig = daemon_signal_next()) <= 0) {
627 avahi_log_error("daemon_signal_next() failed");
628 poll_api->watch_free(watch);
638 sig == SIGINT ? "SIGINT" :
639 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
640 avahi_simple_poll_quit(simple_poll_api);
644 avahi_log_info("Got SIGHUP, reloading.");
646 static_service_load(config.use_chroot);
648 static_service_load(0);
650 static_service_add_to_server();
652 if (resolv_conf_entry_group)
653 avahi_s_entry_group_reset(resolv_conf_entry_group);
657 update_wide_area_servers();
659 if (config.publish_resolv_conf && resolv_conf && resolv_conf[0])
660 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
665 avahi_log_info("Got SIGUSR1, dumping record data.");
666 avahi_server_dump(avahi_server, dump, NULL);
670 avahi_log_warn("Got spurious signal, ignoring.");
676 static int run_server(DaemonConfig *c) {
679 const AvahiPoll *poll_api = NULL;
680 AvahiWatch *sig_watch = NULL;
684 if (!(simple_poll_api = avahi_simple_poll_new())) {
685 avahi_log_error("Failed to create main loop object.");
689 poll_api = avahi_simple_poll_get(simple_poll_api);
691 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
692 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
696 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
697 avahi_log_error( "Failed to create signal watcher");
701 if (simple_protocol_setup(poll_api) < 0)
705 if (c->enable_dbus) {
706 if (dbus_protocol_setup(poll_api) < 0) {
708 if (c->fail_on_missing_dbus)
711 avahi_log_warn("WARNING: Failed to contact D-BUS daemon, disabling D-BUS support.");
719 if (config.drop_root && config.use_chroot) {
720 if (chroot(AVAHI_CONFIG_DIR) < 0) {
721 avahi_log_error("Failed to chroot(): %s", strerror(errno));
727 if (avahi_caps_drop_all() < 0) {
728 avahi_log_error("Failed to drop capabilities.");
732 avahi_log_info("chroot() successful.");
739 static_service_load(config.use_chroot);
741 static_service_load(0);
744 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
745 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
749 update_wide_area_servers();
752 daemon_retval_send(0);
755 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
757 /* We handle signals through an FD, so let's continue */
761 avahi_log_error("poll(): %s", strerror(errno));
771 static_service_remove_from_server();
772 static_service_free_all();
773 remove_dns_server_entry_groups();
775 simple_protocol_shutdown();
779 dbus_protocol_shutdown();
783 avahi_server_free(avahi_server);
787 daemon_signal_done();
790 poll_api->watch_free(sig_watch);
792 if (simple_poll_api) {
793 avahi_simple_poll_free(simple_poll_api);
794 simple_poll_api = NULL;
797 if (r != 0 && c->daemonize)
798 daemon_retval_send(1);
803 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
805 static int drop_root(void) {
810 if (!(pw = getpwnam(AVAHI_USER))) {
811 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
815 if (!(gr = getgrnam(AVAHI_GROUP))) {
816 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
820 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);
822 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
823 avahi_log_error("Failed to change group list: %s", strerror(errno));
827 #if defined(HAVE_SETRESGID)
828 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
829 #elif defined(HAVE_SETEGID)
830 if ((r = setgid(gr->gr_gid)) >= 0)
831 r = setegid(gr->gr_gid);
832 #elif defined(HAVE_SETREGID)
833 r = setregid(gr->gr_gid, gr->gr_gid);
835 #error "No API to drop priviliges"
839 avahi_log_error("Failed to change GID: %s", strerror(errno));
843 #if defined(HAVE_SETRESUID)
844 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
845 #elif defined(HAVE_SETEUID)
846 if ((r = setuid(pw->pw_uid)) >= 0)
847 r = seteuid(pw->pw_uid);
848 #elif defined(HAVE_SETREUID)
849 r = setreuid(pw->pw_uid, pw->pw_uid);
851 #error "No API to drop priviliges"
855 avahi_log_error("Failed to change UID: %s", strerror(errno));
859 set_env("USER", pw->pw_name);
860 set_env("LOGNAME", pw->pw_name);
861 set_env("HOME", pw->pw_dir);
863 avahi_log_info("Successfully dropped root privileges.");
868 static const char* pid_file_proc(void) {
869 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
872 static int make_runtime_dir(void) {
880 if (!(pw = getpwnam(AVAHI_USER))) {
881 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
885 if (!(gr = getgrnam(AVAHI_GROUP))) {
886 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
893 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
894 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
898 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
900 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
901 avahi_log_error("stat(): %s\n", strerror(errno));
905 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
906 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
918 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
920 rl.rlim_cur = rl.rlim_max = limit;
922 if (setrlimit(resource, &rl) < 0)
923 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
926 static void enforce_rlimits(void) {
928 if (config.rlimit_as_set)
929 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
931 if (config.rlimit_core_set)
932 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
933 if (config.rlimit_data_set)
934 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
935 if (config.rlimit_fsize_set)
936 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
937 if (config.rlimit_nofile_set)
938 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
939 if (config.rlimit_stack_set)
940 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
942 if (config.rlimit_nproc_set)
943 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
946 #ifdef RLIMIT_MEMLOCK
947 /* We don't need locked memory */
948 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
952 #define RANDOM_DEVICE "/dev/urandom"
954 static void init_rand_seed(void) {
958 /* Try to initialize seed from /dev/urandom, to make it a little
959 * less predictable, and to make sure that multiple machines
960 * booted at the same time choose different random seeds. */
961 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
962 read(fd, &seed, sizeof(seed));
966 /* If the initialization failed by some reason, we add the time to the seed*/
967 seed |= (unsigned) time(NULL);
972 int main(int argc, char *argv[]) {
974 int wrote_pid_file = 0;
976 avahi_set_log_function(log_function);
980 avahi_server_config_init(&config.server_config);
981 config.command = DAEMON_RUN;
982 config.daemonize = 0;
983 config.config_file = NULL;
985 config.enable_dbus = 1;
986 config.fail_on_missing_dbus = 1;
989 config.drop_root = 1;
990 config.set_rlimits = 1;
992 config.use_chroot = 1;
994 config.modify_proc_title = 1;
996 config.publish_dns_servers = NULL;
997 config.publish_resolv_conf = 0;
998 config.use_syslog = 0;
1000 config.rlimit_as_set = 0;
1001 config.rlimit_core_set = 0;
1002 config.rlimit_data_set = 0;
1003 config.rlimit_fsize_set = 0;
1004 config.rlimit_nofile_set = 0;
1005 config.rlimit_stack_set = 0;
1007 config.rlimit_nproc_set = 0;
1010 if ((argv0 = strrchr(argv[0], '/')))
1011 argv0 = avahi_strdup(argv0 + 1);
1013 argv0 = avahi_strdup(argv[0]);
1015 daemon_pid_file_ident = (const char *) argv0;
1016 daemon_log_ident = (char*) argv0;
1017 daemon_pid_file_proc = pid_file_proc;
1019 if (parse_command_line(&config, argc, argv) < 0)
1022 if (config.modify_proc_title)
1023 avahi_init_proc_title(argc, argv);
1025 #ifdef ENABLE_CHROOT
1026 config.use_chroot = config.use_chroot && config.drop_root;
1029 if (config.command == DAEMON_HELP) {
1032 } else if (config.command == DAEMON_VERSION) {
1033 printf("%s "PACKAGE_VERSION"\n", argv0);
1035 } else if (config.command == DAEMON_KILL) {
1036 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
1037 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1043 } else if (config.command == DAEMON_RELOAD) {
1044 if (daemon_pid_file_kill(SIGHUP) < 0) {
1045 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
1051 } else if (config.command == DAEMON_CHECK)
1052 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
1053 else if (config.command == DAEMON_RUN) {
1056 if (getuid() != 0 && config.drop_root) {
1057 avahi_log_error("This program is intended to be run as root.");
1061 if ((pid = daemon_pid_file_is_running()) >= 0) {
1062 avahi_log_error("Daemon already running on PID %u", pid);
1066 if (load_config_file(&config) < 0)
1069 if (config.daemonize) {
1070 daemon_retval_init();
1072 if ((pid = daemon_fork()) < 0)
1074 else if (pid != 0) {
1078 if ((ret = daemon_retval_wait(20)) < 0) {
1079 avahi_log_error("Could not receive return value from daemon process.");
1090 if (config.use_syslog || config.daemonize)
1091 daemon_log_use = DAEMON_LOG_SYSLOG;
1093 if (make_runtime_dir() < 0)
1096 if (config.drop_root) {
1097 #ifdef ENABLE_CHROOT
1098 if (config.use_chroot)
1099 if (avahi_caps_reduce() < 0)
1103 if (drop_root() < 0)
1106 #ifdef ENABLE_CHROOT
1107 if (config.use_chroot)
1108 if (avahi_caps_reduce2() < 0)
1113 if (daemon_pid_file_create() < 0) {
1114 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1116 if (config.daemonize)
1117 daemon_retval_send(1);
1122 if (config.set_rlimits)
1127 #ifdef ENABLE_CHROOT
1128 if (config.drop_root && config.use_chroot)
1129 if (avahi_chroot_helper_start(argv0) < 0) {
1130 avahi_log_error("failed to start chroot() helper daemon.");
1134 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1136 avahi_set_proc_title("%s: starting up", argv0);
1138 if (run_server(&config) == 0)
1144 if (config.daemonize)
1145 daemon_retval_done();
1147 avahi_server_config_free(&config.server_config);
1148 avahi_free(config.config_file);
1149 avahi_strfreev(config.publish_dns_servers);
1150 avahi_strfreev(resolv_conf);
1152 if (wrote_pid_file) {
1153 #ifdef ENABLE_CHROOT
1154 avahi_chroot_helper_unlink(pid_file_proc());
1156 daemon_pid_file_remove();
1161 avahi_chroot_helper_shutdown();