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
37 #include <libdaemon/dfork.h>
38 #include <libdaemon/dsignal.h>
39 #include <libdaemon/dlog.h>
40 #include <libdaemon/dpid.h>
42 #include <avahi-core/core.h>
43 #include <avahi-core/log.h>
46 #include "simple-protocol.h"
47 #include "dbus-protocol.h"
48 #include "static-services.h"
50 AvahiServer *avahi_server = NULL;
62 AvahiServerConfig server_config;
63 DaemonCommand command;
68 gboolean publish_resolv_conf;
69 gchar ** publish_dns_servers;
72 #define RESOLV_CONF "/etc/resolv.conf"
74 static AvahiEntryGroup *dns_servers_entry_group = NULL;
75 static AvahiEntryGroup *resolv_conf_entry_group = NULL;
77 static gchar **resolv_conf = NULL;
79 static DaemonConfig config;
81 #define MAX_NAME_SERVERS 10
83 static gint load_resolv_conf(const DaemonConfig *c) {
88 g_strfreev(resolv_conf);
91 if (!c->publish_resolv_conf)
94 if (!(f = fopen(RESOLV_CONF, "r"))) {
95 avahi_log_warn("Failed to open "RESOLV_CONF".");
99 resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
101 while (!feof(f) && i < MAX_NAME_SERVERS) {
105 if (!(fgets(ln, sizeof(ln), f)))
108 ln[strcspn(ln, "\r\n#")] = 0;
109 p = ln + strspn(ln, "\t ");
111 if (g_str_has_prefix(p, "nameserver")) {
113 p += strspn(p, "\t ");
114 p[strcspn(p, "\t ")] = 0;
115 resolv_conf[i++] = strdup(p);
124 g_strfreev(resolv_conf);
134 static AvahiEntryGroup* add_dns_servers(AvahiServer *s, AvahiEntryGroup* g, gchar **l) {
141 g = avahi_entry_group_new(s, NULL, NULL);
143 g_assert(avahi_entry_group_is_empty(g));
145 for (p = l; *p; p++) {
148 if (!avahi_address_parse(*p, AF_UNSPEC, &a))
149 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
151 if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
152 avahi_entry_group_free(g);
153 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
158 avahi_entry_group_commit(g);
163 static void remove_dns_server_entry_groups(void) {
165 if (resolv_conf_entry_group)
166 avahi_entry_group_reset(resolv_conf_entry_group);
168 if (dns_servers_entry_group)
169 avahi_entry_group_reset(dns_servers_entry_group);
172 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
173 DaemonConfig *c = userdata;
180 dbus_protocol_server_state_changed(state);
183 if (state == AVAHI_SERVER_RUNNING) {
184 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
185 static_service_add_to_server();
187 remove_dns_server_entry_groups();
189 if (resolv_conf && resolv_conf[0])
190 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
192 if (c->publish_dns_servers && c->publish_dns_servers[0])
193 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
195 simple_protocol_restart_queries();
197 } else if (state == AVAHI_SERVER_COLLISION) {
200 static_service_remove_from_server();
202 remove_dns_server_entry_groups();
204 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
205 avahi_log_warn("Host name conflict, retrying with <%s>", n);
206 avahi_server_set_host_name(s, n);
211 static void help(FILE *f, const gchar *argv0) {
214 " -h --help Show this help\n"
215 " -D --daemonize Daemonize after startup\n"
216 " -k --kill Kill a running daemon\n"
217 " -r --reload Request a running daemon to reload static services\n"
218 " -c --check Return 0 if a daemon is already running\n"
219 " -V --version Show version\n"
220 " -f --file=FILE Load the specified configuration file instead of\n"
221 " "AVAHI_CONFIG_FILE"\n",
225 static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
228 static const struct option const long_options[] = {
229 { "help", no_argument, NULL, 'h' },
230 { "daemonize", no_argument, NULL, 'D' },
231 { "kill", no_argument, NULL, 'k' },
232 { "version", no_argument, NULL, 'V' },
233 { "file", required_argument, NULL, 'f' },
234 { "reload", no_argument, NULL, 'r' },
235 { "check", no_argument, NULL, 'c' },
241 while ((o = getopt_long(argc, argv, "hDkVf:rc", long_options, NULL)) >= 0) {
245 c->command = DAEMON_HELP;
251 c->command = DAEMON_KILL;
254 c->command = DAEMON_VERSION;
257 g_free(c->config_file);
258 c->config_file = g_strdup(optarg);
261 c->command = DAEMON_RELOAD;
264 c->command = DAEMON_CHECK;
267 fprintf(stderr, "Invalid command line argument: %c\n", o);
273 fprintf(stderr, "Too many arguments\n");
280 static gboolean is_yes(const gchar *s) {
283 return *s == 'y' || *s == 'Y';
286 static gint load_config_file(DaemonConfig *c) {
290 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
294 f = g_key_file_new();
295 g_key_file_set_list_separator(f, ',');
297 if (!g_key_file_load_from_file(f, c->config_file ? c->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
298 fprintf(stderr, "Unable to read config file: %s\n", err->message);
302 groups = g_key_file_get_groups(f, NULL);
304 for (g = groups; *g; g++) {
305 if (g_strcasecmp(*g, "server") == 0) {
308 keys = g_key_file_get_keys(f, *g, NULL, NULL);
310 for (k = keys; *k; k++) {
312 v = g_key_file_get_value(f, *g, *k, NULL);
314 if (g_strcasecmp(*k, "host-name") == 0) {
315 g_free(c->server_config.host_name);
316 c->server_config.host_name = v;
318 } else if (g_strcasecmp(*k, "domain-name") == 0) {
319 g_free(c->server_config.domain_name);
320 c->server_config.domain_name = v;
322 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
323 c->server_config.use_ipv4 = is_yes(v);
324 else if (g_strcasecmp(*k, "use-ipv6") == 0)
325 c->server_config.use_ipv6 = is_yes(v);
326 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
327 c->server_config.check_response_ttl = is_yes(v);
328 else if (g_strcasecmp(*k, "use-iff-running") == 0)
329 c->server_config.use_iff_running = is_yes(v);
330 else if (g_strcasecmp(*k, "enable-dbus") == 0)
331 c->enable_dbus = is_yes(v);
332 else if (g_strcasecmp(*k, "drop-root") == 0)
333 c->drop_root = is_yes(v);
335 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
346 } else if (g_strcasecmp(*g, "publish") == 0) {
349 keys = g_key_file_get_keys(f, *g, NULL, NULL);
351 for (k = keys; *k; k++) {
353 v = g_key_file_get_string(f, *g, *k, NULL);
355 if (g_strcasecmp(*k, "publish-addresses") == 0)
356 c->server_config.publish_addresses = is_yes(v);
357 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
358 c->server_config.publish_hinfo = is_yes(v);
359 else if (g_strcasecmp(*k, "publish-workstation") == 0)
360 c->server_config.publish_workstation = is_yes(v);
361 else if (g_strcasecmp(*k, "publish-domain") == 0)
362 c->server_config.publish_domain = is_yes(v);
363 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
364 c->publish_resolv_conf = is_yes(v);
365 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
366 g_strfreev(c->publish_dns_servers);
367 c->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
369 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
380 } else if (g_strcasecmp(*g, "reflector") == 0) {
383 keys = g_key_file_get_keys(f, *g, NULL, NULL);
385 for (k = keys; *k; k++) {
387 v = g_key_file_get_string(f, *g, *k, NULL);
389 if (g_strcasecmp(*k, "enable-reflector") == 0)
390 c->server_config.enable_reflector = is_yes(v);
391 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
392 c->server_config.reflect_ipv = is_yes(v);
394 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
406 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
428 static void log_function(AvahiLogLevel level, const gchar *txt) {
430 static const int const log_level_map[] = {
438 g_assert(level < AVAHI_LOG_LEVEL_MAX);
441 daemon_log(log_level_map[level], "%s", txt);
444 static void dump(const gchar *text, gpointer userdata) {
445 avahi_log_info("%s", text);
448 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
450 GMainLoop *loop = data;
455 if ((sig = daemon_signal_next()) <= 0) {
456 avahi_log_error("daemon_signal_next() failed");
466 sig == SIGINT ? "SIGINT" :
467 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
468 g_main_loop_quit(loop);
472 avahi_log_info("Got SIGHUP, reloading.");
473 static_service_load();
474 static_service_add_to_server();
476 if (resolv_conf_entry_group)
477 avahi_entry_group_reset(resolv_conf_entry_group);
479 load_resolv_conf(&config);
481 if (resolv_conf && resolv_conf[0])
482 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
487 avahi_log_info("Got SIGUSR1, dumping record data.");
488 avahi_server_dump(avahi_server, dump, NULL);
492 avahi_log_warn("Got spurious signal, ignoring.");
499 static gint run_server(DaemonConfig *c) {
500 GMainLoop *loop = NULL;
502 GIOChannel *io = NULL;
503 guint watch_id = (guint) -1;
508 loop = g_main_loop_new(NULL, FALSE);
510 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
511 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
515 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
516 avahi_log_error( "Failed to create signal io channel.");
520 g_io_channel_set_close_on_unref(io, FALSE);
521 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
523 if (simple_protocol_setup(NULL) < 0)
528 if (dbus_protocol_setup(loop) < 0)
532 if (!(avahi_server = avahi_server_new(NULL, &c->server_config, server_callback, c, &error))) {
533 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
539 static_service_load();
542 daemon_retval_send(0);
546 g_main_loop_run(loop);
550 static_service_remove_from_server();
551 static_service_free_all();
552 remove_dns_server_entry_groups();
554 simple_protocol_shutdown();
558 dbus_protocol_shutdown();
562 avahi_server_free(avahi_server);
564 daemon_signal_done();
566 if (watch_id != (guint) -1)
567 g_source_remove(watch_id);
570 g_io_channel_unref(io);
574 g_main_loop_unref(loop);
576 if (r != 0 && c->daemonize)
577 daemon_retval_send(1);
582 static gint drop_root(void) {
587 if (!(pw = getpwnam(AVAHI_USER))) {
588 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
592 if (!(gr = getgrnam(AVAHI_GROUP))) {
593 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
597 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);
599 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
600 avahi_log_error("Failed to change group list: %s", strerror(errno));
604 #if defined(HAVE_SETRESGID)
605 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
606 #elif defined(HAVE_SETREGID)
607 r = setregid(gr->gr_gid, gr->gr_gid);
609 if ((r = setgid(gr->gr_gid)) >= 0)
610 r = setegid(gr->gr_gid);
614 avahi_log_error("Failed to change GID: %s", strerror(errno));
618 #if defined(HAVE_SETRESUID)
619 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
620 #elif defined(HAVE_SETREUID)
621 r = setreuid(pw->pw_uid, pw->pw_uid);
623 if ((r = setuid(pw->pw_uid)) >= 0)
624 r = seteuid(pw->pw_uid);
628 avahi_log_error("Failed to change UID: %s", strerror(errno));
632 g_setenv("USER", pw->pw_name, 1);
633 g_setenv("LOGNAME", pw->pw_name, 1);
634 g_setenv("HOME", pw->pw_dir, 1);
636 avahi_log_info("Successfully dropped root privileges.");
641 static const char* pid_file_proc(void) {
642 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
645 static gint make_runtime_dir(void) {
648 gboolean reset_umask = FALSE;
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"'.");
666 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
667 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
671 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
673 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
674 avahi_log_error("stat(): %s\n", strerror(errno));
678 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
679 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
692 int main(int argc, char *argv[]) {
695 gboolean wrote_pid_file = FALSE;
697 avahi_set_log_function(log_function);
699 avahi_server_config_init(&config.server_config);
700 config.command = DAEMON_RUN;
701 config.daemonize = FALSE;
702 config.config_file = NULL;
703 config.enable_dbus = TRUE;
704 config.drop_root = TRUE;
705 config.publish_dns_servers = NULL;
706 config.publish_resolv_conf = FALSE;
708 if ((argv0 = strrchr(argv[0], '/')))
713 daemon_pid_file_ident = (const char *) argv0;
714 daemon_log_ident = (char*) argv0;
715 daemon_pid_file_proc = pid_file_proc;
717 if (parse_command_line(&config, argc, argv) < 0)
720 if (config.command == DAEMON_HELP) {
723 } else if (config.command == DAEMON_VERSION) {
724 printf("%s "PACKAGE_VERSION"\n", argv0);
726 } else if (config.command == DAEMON_KILL) {
727 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
728 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
734 } else if (config.command == DAEMON_RELOAD) {
735 if (daemon_pid_file_kill(SIGHUP) < 0) {
736 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
742 } else if (config.command == DAEMON_CHECK)
743 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
744 else if (config.command == DAEMON_RUN) {
748 avahi_log_error("This program is intended to be run as root.");
752 if ((pid = daemon_pid_file_is_running()) >= 0) {
753 avahi_log_error("Daemon already running on PID %u", pid);
757 if (load_config_file(&config) < 0)
760 if (config.daemonize) {
761 daemon_retval_init();
763 if ((pid = daemon_fork()) < 0)
769 if ((ret = daemon_retval_wait(20)) < 0) {
770 avahi_log_error("Could not recieve return value from daemon process.");
782 printf("%s "PACKAGE_VERSION" starting up.\n", argv0);
786 if (make_runtime_dir() < 0)
789 if (config.drop_root) {
794 if (daemon_pid_file_create() < 0) {
795 avahi_log_error("Failed to create PID file: %s", strerror(errno));
797 if (config.daemonize)
798 daemon_retval_send(1);
801 wrote_pid_file = TRUE;
803 if (run_server(&config) == 0)
809 if (config.daemonize)
810 daemon_retval_done();
812 avahi_server_config_free(&config.server_config);
813 g_free(config.config_file);
814 g_strfreev(config.publish_dns_servers);
815 g_strfreev(resolv_conf);
818 daemon_pid_file_remove();