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>
43 #include <libdaemon/dfork.h>
44 #include <libdaemon/dsignal.h>
45 #include <libdaemon/dlog.h>
46 #include <libdaemon/dpid.h>
48 #include <avahi-common/malloc.h>
49 #include <avahi-common/simple-watch.h>
50 #include <avahi-core/core.h>
51 #include <avahi-core/log.h>
54 #include "simple-protocol.h"
55 #include "dbus-protocol.h"
56 #include "static-services.h"
57 #include "ini-file-parser.h"
59 AvahiServer *avahi_server = NULL;
71 AvahiServerConfig server_config;
72 DaemonCommand command;
78 int publish_resolv_conf;
79 char ** publish_dns_servers;
83 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
84 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
92 #define RESOLV_CONF "/etc/resolv.conf"
94 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
95 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
97 static char **resolv_conf = NULL;
99 static DaemonConfig config;
101 #define MAX_NAME_SERVERS 10
103 static int has_prefix(const char *s, const char *prefix) {
108 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
111 static int load_resolv_conf(const DaemonConfig *c) {
116 avahi_strfreev(resolv_conf);
119 if (!c->publish_resolv_conf)
122 if (!(f = fopen(RESOLV_CONF, "r"))) {
123 avahi_log_warn("Failed to open "RESOLV_CONF".");
127 resolv_conf = avahi_new0(char*, MAX_NAME_SERVERS+1);
129 while (!feof(f) && i < MAX_NAME_SERVERS) {
133 if (!(fgets(ln, sizeof(ln), f)))
136 ln[strcspn(ln, "\r\n#")] = 0;
137 p = ln + strspn(ln, "\t ");
139 if (has_prefix(p, "nameserver")) {
141 p += strspn(p, "\t ");
142 p[strcspn(p, "\t ")] = 0;
143 resolv_conf[i++] = avahi_strdup(p);
152 avahi_strfreev(resolv_conf);
162 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
169 g = avahi_s_entry_group_new(s, NULL, NULL);
171 assert(avahi_s_entry_group_is_empty(g));
173 for (p = l; *p; p++) {
176 if (!avahi_address_parse(*p, AF_UNSPEC, &a))
177 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
179 if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
180 avahi_s_entry_group_free(g);
181 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
186 avahi_s_entry_group_commit(g);
191 static void remove_dns_server_entry_groups(void) {
193 if (resolv_conf_entry_group)
194 avahi_s_entry_group_reset(resolv_conf_entry_group);
196 if (dns_servers_entry_group)
197 avahi_s_entry_group_reset(dns_servers_entry_group);
200 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
201 DaemonConfig *c = userdata;
206 /** This function is possibly called before the global variable
207 * avahi_server has been set, therefore we do it explicitly */
213 dbus_protocol_server_state_changed(state);
216 if (state == AVAHI_SERVER_RUNNING) {
217 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
218 static_service_add_to_server();
220 remove_dns_server_entry_groups();
222 if (resolv_conf && resolv_conf[0])
223 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
225 if (c->publish_dns_servers && c->publish_dns_servers[0])
226 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
228 simple_protocol_restart_queries();
230 } else if (state == AVAHI_SERVER_COLLISION) {
233 static_service_remove_from_server();
235 remove_dns_server_entry_groups();
237 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
238 avahi_log_warn("Host name conflict, retrying with <%s>", n);
239 avahi_server_set_host_name(s, n);
244 static void help(FILE *f, const char *argv0) {
247 " -h --help Show this help\n"
248 " -D --daemonize Daemonize after startup (implies -s)\n"
249 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
250 " -k --kill Kill a running daemon\n"
251 " -r --reload Request a running daemon to reload static services\n"
252 " -c --check Return 0 if a daemon is already running\n"
253 " -V --version Show version\n"
254 " -f --file=FILE Load the specified configuration file instead of\n"
255 " "AVAHI_CONFIG_FILE"\n"
256 " --no-rlimits Don't enforce resource limits\n"
257 " --no-drop-root Don't drop priviliges\n"
258 " --debug Increase verbosity\n",
263 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
267 OPTION_NO_RLIMITS = 256,
272 static const struct option const long_options[] = {
273 { "help", no_argument, NULL, 'h' },
274 { "daemonize", no_argument, NULL, 'D' },
275 { "kill", no_argument, NULL, 'k' },
276 { "version", no_argument, NULL, 'V' },
277 { "file", required_argument, NULL, 'f' },
278 { "reload", no_argument, NULL, 'r' },
279 { "check", no_argument, NULL, 'c' },
280 { "syslog", no_argument, NULL, 's' },
281 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
282 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
283 { "debug", no_argument, NULL, OPTION_DEBUG },
290 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
297 c->command = DAEMON_HELP;
303 c->command = DAEMON_KILL;
306 c->command = DAEMON_VERSION;
309 avahi_free(c->config_file);
310 c->config_file = avahi_strdup(optarg);
313 c->command = DAEMON_RELOAD;
316 c->command = DAEMON_CHECK;
318 case OPTION_NO_RLIMITS:
321 case OPTION_NO_DROP_ROOT:
328 fprintf(stderr, "Invalid command line argument: %c\n", o);
334 fprintf(stderr, "Too many arguments\n");
341 static int is_yes(const char *s) {
344 return *s == 'y' || *s == 'Y';
347 static int load_config_file(DaemonConfig *c) {
350 AvahiIniFileGroup *g;
354 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
357 for (g = f->groups; g; g = g->groups_next) {
359 if (strcasecmp(g->name, "server") == 0) {
362 for (p = g->pairs; p; p = p->pairs_next) {
364 if (strcasecmp(p->key, "host-name") == 0) {
365 avahi_free(c->server_config.host_name);
366 c->server_config.host_name = avahi_strdup(p->value);
367 } else if (strcasecmp(p->key, "domain-name") == 0) {
368 avahi_free(c->server_config.domain_name);
369 c->server_config.domain_name = avahi_strdup(p->value);
370 } else if (strcasecmp(p->key, "use-ipv4") == 0)
371 c->server_config.use_ipv4 = is_yes(p->value);
372 else if (strcasecmp(p->key, "use-ipv6") == 0)
373 c->server_config.use_ipv6 = is_yes(p->value);
374 else if (strcasecmp(p->key, "check-response-ttl") == 0)
375 c->server_config.check_response_ttl = is_yes(p->value);
376 else if (strcasecmp(p->key, "use-iff-running") == 0)
377 c->server_config.use_iff_running = is_yes(p->value);
378 else if (strcasecmp(p->key, "enable-dbus") == 0)
379 c->enable_dbus = is_yes(p->value);
380 else if (strcasecmp(p->key, "drop-root") == 0)
381 c->drop_root = is_yes(p->value);
383 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
388 } else if (strcasecmp(g->name, "publish") == 0) {
391 for (p = g->pairs; p; p = p->pairs_next) {
393 if (strcasecmp(p->key, "publish-addresses") == 0)
394 c->server_config.publish_addresses = is_yes(p->value);
395 else if (strcasecmp(p->key, "publish-hinfo") == 0)
396 c->server_config.publish_hinfo = is_yes(p->value);
397 else if (strcasecmp(p->key, "publish-workstation") == 0)
398 c->server_config.publish_workstation = is_yes(p->value);
399 else if (strcasecmp(p->key, "publish-domain") == 0)
400 c->server_config.publish_domain = is_yes(p->value);
401 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
402 c->publish_resolv_conf = is_yes(p->value);
403 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
404 avahi_strfreev(c->publish_dns_servers);
405 c->publish_dns_servers = avahi_split_csv(p->value);
407 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
412 } else if (strcasecmp(g->name, "reflector") == 0) {
415 for (p = g->pairs; p; p = p->pairs_next) {
417 if (strcasecmp(p->key, "enable-reflector") == 0)
418 c->server_config.enable_reflector = is_yes(p->value);
419 else if (strcasecmp(p->key, "reflect-ipv") == 0)
420 c->server_config.reflect_ipv = is_yes(p->value);
422 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
427 } else if (strcasecmp(g->name, "rlimits") == 0) {
430 for (p = g->pairs; p; p = p->pairs_next) {
432 if (strcasecmp(p->key, "rlimit-as") == 0) {
433 c->rlimit_as_set = 1;
434 c->rlimit_as = atoi(p->value);
435 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
436 c->rlimit_core_set = 1;
437 c->rlimit_core = atoi(p->value);
438 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
439 c->rlimit_data_set = 1;
440 c->rlimit_data = atoi(p->value);
441 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
442 c->rlimit_fsize_set = 1;
443 c->rlimit_fsize = atoi(p->value);
444 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
445 c->rlimit_nofile_set = 1;
446 c->rlimit_nofile = atoi(p->value);
447 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
448 c->rlimit_stack_set = 1;
449 c->rlimit_stack = atoi(p->value);
451 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
452 c->rlimit_nproc_set = 1;
453 c->rlimit_nproc = atoi(p->value);
456 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
463 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
473 avahi_ini_file_free(f);
478 static void log_function(AvahiLogLevel level, const char *txt) {
480 static const int const log_level_map[] = {
488 assert(level < AVAHI_LOG_LEVEL_MAX);
491 if (!config.debug && level == AVAHI_LOG_DEBUG)
494 daemon_log(log_level_map[level], "%s", txt);
497 static void dump(const char *text, void* userdata) {
498 avahi_log_info("%s", text);
501 static void signal_callback(AvahiWatch *watch, int fd, AvahiWatchEvent event, void *userdata) {
503 AvahiSimplePoll *simple_poll_api = userdata;
504 const AvahiPoll *poll_api;
507 assert(simple_poll_api);
509 poll_api = avahi_simple_poll_get(simple_poll_api);
511 if ((sig = daemon_signal_next()) <= 0) {
512 avahi_log_error("daemon_signal_next() failed");
513 poll_api->watch_free(watch);
523 sig == SIGINT ? "SIGINT" :
524 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
525 avahi_simple_poll_quit(simple_poll_api);
529 avahi_log_info("Got SIGHUP, reloading.");
530 static_service_load();
531 static_service_add_to_server();
533 if (resolv_conf_entry_group)
534 avahi_s_entry_group_reset(resolv_conf_entry_group);
536 load_resolv_conf(&config);
538 if (resolv_conf && resolv_conf[0])
539 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
544 avahi_log_info("Got SIGUSR1, dumping record data.");
545 avahi_server_dump(avahi_server, dump, NULL);
549 avahi_log_warn("Got spurious signal, ignoring.");
554 static int run_server(DaemonConfig *c) {
557 AvahiSimplePoll *simple_poll_api;
558 const AvahiPoll *poll_api;
559 AvahiWatch *sig_watch;
563 if (!(simple_poll_api = avahi_simple_poll_new())) {
564 avahi_log_error("Failed to create main loop object.");
568 poll_api = avahi_simple_poll_get(simple_poll_api);
570 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
571 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
575 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
576 avahi_log_error( "Failed to create signal watcher");
580 if (simple_protocol_setup(poll_api) < 0)
585 if (dbus_protocol_setup(poll_api) < 0)
590 static_service_load();
592 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
593 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
599 daemon_retval_send(0);
602 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
604 /* We handle signals through an FD, so let's continue */
608 avahi_log_error("poll(): %s", strerror(errno));
618 static_service_remove_from_server();
619 static_service_free_all();
620 remove_dns_server_entry_groups();
622 simple_protocol_shutdown();
626 dbus_protocol_shutdown();
630 avahi_server_free(avahi_server);
632 daemon_signal_done();
635 poll_api->watch_free(sig_watch);
638 avahi_simple_poll_free(simple_poll_api);
640 if (r != 0 && c->daemonize)
641 daemon_retval_send(1);
646 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
648 static int drop_root(void) {
653 if (!(pw = getpwnam(AVAHI_USER))) {
654 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
658 if (!(gr = getgrnam(AVAHI_GROUP))) {
659 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
663 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);
665 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
666 avahi_log_error("Failed to change group list: %s", strerror(errno));
670 #if defined(HAVE_SETRESGID)
671 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
672 #elif defined(HAVE_SETREGID)
673 r = setregid(gr->gr_gid, gr->gr_gid);
675 if ((r = setgid(gr->gr_gid)) >= 0)
676 r = setegid(gr->gr_gid);
680 avahi_log_error("Failed to change GID: %s", strerror(errno));
684 #if defined(HAVE_SETRESUID)
685 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
686 #elif defined(HAVE_SETREUID)
687 r = setreuid(pw->pw_uid, pw->pw_uid);
689 if ((r = setuid(pw->pw_uid)) >= 0)
690 r = seteuid(pw->pw_uid);
694 avahi_log_error("Failed to change UID: %s", strerror(errno));
698 set_env("USER", pw->pw_name);
699 set_env("LOGNAME", pw->pw_name);
700 set_env("HOME", pw->pw_dir);
702 avahi_log_info("Successfully dropped root privileges.");
707 static const char* pid_file_proc(void) {
708 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
711 static int make_runtime_dir(void) {
719 if (!(pw = getpwnam(AVAHI_USER))) {
720 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
724 if (!(gr = getgrnam(AVAHI_GROUP))) {
725 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
732 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
733 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
737 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
739 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
740 avahi_log_error("stat(): %s\n", strerror(errno));
744 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
745 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
757 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
759 rl.rlim_cur = rl.rlim_max = limit;
761 if (setrlimit(resource, &rl) < 0)
762 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
765 static void enforce_rlimits(void) {
767 if (config.rlimit_as_set)
768 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
769 if (config.rlimit_core_set)
770 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
771 if (config.rlimit_data_set)
772 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
773 if (config.rlimit_fsize_set)
774 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
775 if (config.rlimit_nofile_set)
776 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
777 if (config.rlimit_stack_set)
778 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
780 if (config.rlimit_nproc_set)
781 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
784 #ifdef RLIMIT_MEMLOCK
785 /* We don't need locked memory */
786 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
790 #define RANDOM_DEVICE "/dev/urandom"
792 static void init_rand_seed(void) {
796 /* Try to initialize seed from /dev/urandom, to make it a little
797 * less predictable, and to make sure that multiple machines
798 * booted at the same time choose different random seeds. */
799 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
800 read(fd, &seed, sizeof(seed));
804 /* If the initialization failed by some reason, we add the time to the seed*/
805 seed |= (unsigned) time(NULL);
810 int main(int argc, char *argv[]) {
813 int wrote_pid_file = 0;
815 avahi_set_log_function(log_function);
819 avahi_server_config_init(&config.server_config);
820 config.command = DAEMON_RUN;
821 config.daemonize = 0;
822 config.config_file = NULL;
823 config.enable_dbus = 1;
824 config.drop_root = 1;
825 config.publish_dns_servers = NULL;
826 config.publish_resolv_conf = 0;
827 config.use_syslog = 0;
828 config.no_rlimits = 0;
831 config.rlimit_as_set = 0;
832 config.rlimit_core_set = 0;
833 config.rlimit_data_set = 0;
834 config.rlimit_fsize_set = 0;
835 config.rlimit_nofile_set = 0;
836 config.rlimit_stack_set = 0;
838 config.rlimit_nproc_set = 0;
841 if ((argv0 = strrchr(argv[0], '/')))
846 daemon_pid_file_ident = (const char *) argv0;
847 daemon_log_ident = (char*) argv0;
848 daemon_pid_file_proc = pid_file_proc;
850 if (parse_command_line(&config, argc, argv) < 0)
853 if (config.command == DAEMON_HELP) {
856 } else if (config.command == DAEMON_VERSION) {
857 printf("%s "PACKAGE_VERSION"\n", argv0);
859 } else if (config.command == DAEMON_KILL) {
860 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
861 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
867 } else if (config.command == DAEMON_RELOAD) {
868 if (daemon_pid_file_kill(SIGHUP) < 0) {
869 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
875 } else if (config.command == DAEMON_CHECK)
876 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
877 else if (config.command == DAEMON_RUN) {
880 if (getuid() != 0 && config.drop_root) {
881 avahi_log_error("This program is intended to be run as root.");
885 if ((pid = daemon_pid_file_is_running()) >= 0) {
886 avahi_log_error("Daemon already running on PID %u", pid);
890 if (load_config_file(&config) < 0)
893 if (config.daemonize) {
894 daemon_retval_init();
896 if ((pid = daemon_fork()) < 0)
902 if ((ret = daemon_retval_wait(20)) < 0) {
903 avahi_log_error("Could not recieve return value from daemon process.");
914 if (config.use_syslog || config.daemonize)
915 daemon_log_use = DAEMON_LOG_SYSLOG;
917 if (make_runtime_dir() < 0)
920 if (config.drop_root) {
925 if (daemon_pid_file_create() < 0) {
926 avahi_log_error("Failed to create PID file: %s", strerror(errno));
928 if (config.daemonize)
929 daemon_retval_send(1);
934 if (!config.no_rlimits)
939 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
941 if (run_server(&config) == 0)
947 if (config.daemonize)
948 daemon_retval_done();
950 avahi_server_config_free(&config.server_config);
951 avahi_free(config.config_file);
952 avahi_strfreev(config.publish_dns_servers);
953 avahi_strfreev(resolv_conf);
956 daemon_pid_file_remove();