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 <libdaemon/dfork.h>
37 #include <libdaemon/dsignal.h>
38 #include <libdaemon/dlog.h>
39 #include <libdaemon/dpid.h>
41 #include <avahi-core/core.h>
42 #include <avahi-core/log.h>
45 #include "simple-protocol.h"
46 #include "dbus-protocol.h"
47 #include "static-services.h"
49 AvahiServer *avahi_server = NULL;
61 AvahiServerConfig server_config;
62 DaemonCommand command;
67 gboolean publish_resolv_conf;
68 gchar ** publish_dns_servers;
71 #define RESOLV_CONF "/etc/resolv.conf"
73 static AvahiEntryGroup *dns_servers_entry_group = NULL;
74 static AvahiEntryGroup *resolv_conf_entry_group = NULL;
76 static gchar **resolv_conf = NULL;
78 static DaemonConfig config;
80 #define MAX_NAME_SERVERS 10
82 static gint load_resolv_conf(const DaemonConfig *config) {
87 g_strfreev(resolv_conf);
90 if (!config->publish_resolv_conf)
93 if (!(f = fopen(RESOLV_CONF, "r"))) {
94 avahi_log_warn("Failed to open "RESOLV_CONF".");
98 resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
100 while (!feof(f) && i < MAX_NAME_SERVERS) {
104 if (!(fgets(ln, sizeof(ln), f)))
107 ln[strcspn(ln, "\r\n#")] = 0;
108 p = ln + strspn(ln, "\t ");
110 if (g_str_has_prefix(p, "nameserver")) {
112 p += strspn(p, "\t ");
113 p[strcspn(p, "\t ")] = 0;
114 resolv_conf[i++] = strdup(p);
123 g_strfreev(resolv_conf);
133 static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
140 g = avahi_entry_group_new(s, NULL, NULL);
142 for (p = l; *p; p++) {
145 if (!avahi_address_parse(*p, AF_UNSPEC, &a))
146 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
148 if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
149 avahi_entry_group_free(g);
154 avahi_entry_group_commit(g);
160 static void remove_dns_server_entry_groups(void) {
161 if (resolv_conf_entry_group) {
162 avahi_entry_group_free(resolv_conf_entry_group);
163 resolv_conf_entry_group = NULL;
166 if (dns_servers_entry_group) {
167 avahi_entry_group_free(dns_servers_entry_group);
168 dns_servers_entry_group = NULL;
172 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
173 DaemonConfig *config = userdata;
179 if (config->enable_dbus)
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);
192 if (config->publish_dns_servers && config->publish_dns_servers[0])
193 dns_servers_entry_group = add_dns_servers(s, config->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);
213 static void help(FILE *f, const gchar *argv0) {
216 " -h --help Show this help\n"
217 " -D --daemonize Daemonize after startup\n"
218 " -k --kill Kill a running daemon\n"
219 " -r --reload Request a running daemon to reload static services\n"
220 " -c --check Return 0 if a daemon is already running\n"
221 " -V --version Show version\n"
222 " -f --file=FILE Load the specified configuration file instead of\n"
223 " "AVAHI_CONFIG_FILE"\n",
227 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
230 static const struct option const long_options[] = {
231 { "help", no_argument, NULL, 'h' },
232 { "daemonize", no_argument, NULL, 'D' },
233 { "kill", no_argument, NULL, 'k' },
234 { "version", no_argument, NULL, 'V' },
235 { "file", required_argument, NULL, 'f' },
236 { "reload", no_argument, NULL, 'r' },
237 { "check", no_argument, NULL, 'c' },
243 while ((c = getopt_long(argc, argv, "hDkVf:rc", long_options, NULL)) >= 0) {
247 config->command = DAEMON_HELP;
250 config->daemonize = TRUE;
253 config->command = DAEMON_KILL;
256 config->command = DAEMON_VERSION;
259 g_free(config->config_file);
260 config->config_file = g_strdup(optarg);
263 config->command = DAEMON_RELOAD;
266 config->command = DAEMON_CHECK;
269 fprintf(stderr, "Invalid command line argument: %c\n", c);
275 fprintf(stderr, "Too many arguments\n");
282 static gboolean is_yes(const gchar *s) {
285 return *s == 'y' || *s == 'Y';
288 static gint load_config_file(DaemonConfig *config) {
292 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
296 f = g_key_file_new();
297 g_key_file_set_list_separator(f, ',');
299 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
300 fprintf(stderr, "Unable to read config file: %s\n", err->message);
304 groups = g_key_file_get_groups(f, NULL);
306 for (g = groups; *g; g++) {
307 if (g_strcasecmp(*g, "server") == 0) {
310 keys = g_key_file_get_keys(f, *g, NULL, NULL);
312 for (k = keys; *k; k++) {
314 v = g_key_file_get_value(f, *g, *k, NULL);
316 if (g_strcasecmp(*k, "host-name") == 0) {
317 g_free(config->server_config.host_name);
318 config->server_config.host_name = v;
320 } else if (g_strcasecmp(*k, "domain-name") == 0) {
321 g_free(config->server_config.domain_name);
322 config->server_config.domain_name = v;
324 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
325 config->server_config.use_ipv4 = is_yes(v);
326 else if (g_strcasecmp(*k, "use-ipv6") == 0)
327 config->server_config.use_ipv6 = is_yes(v);
328 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
329 config->server_config.check_response_ttl = is_yes(v);
330 else if (g_strcasecmp(*k, "use-iff-running") == 0)
331 config->server_config.use_iff_running = is_yes(v);
332 else if (g_strcasecmp(*k, "enable-dbus") == 0)
333 config->enable_dbus = is_yes(v);
334 else if (g_strcasecmp(*k, "drop-root") == 0)
335 config->drop_root = is_yes(v);
337 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
348 } else if (g_strcasecmp(*g, "publish") == 0) {
351 keys = g_key_file_get_keys(f, *g, NULL, NULL);
353 for (k = keys; *k; k++) {
355 v = g_key_file_get_string(f, *g, *k, NULL);
357 if (g_strcasecmp(*k, "publish-addresses") == 0)
358 config->server_config.publish_addresses = is_yes(v);
359 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
360 config->server_config.publish_hinfo = is_yes(v);
361 else if (g_strcasecmp(*k, "publish-workstation") == 0)
362 config->server_config.publish_workstation = is_yes(v);
363 else if (g_strcasecmp(*k, "publish-domain") == 0)
364 config->server_config.publish_domain = is_yes(v);
365 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
366 config->publish_resolv_conf = is_yes(v);
367 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
368 g_strfreev(config->publish_dns_servers);
369 config->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
371 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
382 } else if (g_strcasecmp(*g, "reflector") == 0) {
385 keys = g_key_file_get_keys(f, *g, NULL, NULL);
387 for (k = keys; *k; k++) {
389 v = g_key_file_get_string(f, *g, *k, NULL);
391 if (g_strcasecmp(*k, "enable-reflector") == 0)
392 config->server_config.enable_reflector = is_yes(v);
393 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
394 config->server_config.reflect_ipv = is_yes(v);
396 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
408 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
430 static void log_function(AvahiLogLevel level, const gchar *txt) {
432 static const int const log_level_map[] = {
440 g_assert(level < AVAHI_LOG_LEVEL_MAX);
443 daemon_log(log_level_map[level], "%s", txt);
446 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
448 GMainLoop *loop = data;
453 if ((sig = daemon_signal_next()) <= 0) {
454 avahi_log_error("daemon_signal_next() failed");
464 sig == SIGINT ? "SIGINT" :
465 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
466 g_main_loop_quit(loop);
470 avahi_log_info("Got SIGHUP, reloading.");
471 static_service_load();
472 static_service_add_to_server();
474 if (resolv_conf_entry_group) {
475 avahi_entry_group_free(resolv_conf_entry_group);
476 resolv_conf_entry_group = NULL;
479 load_resolv_conf(&config);
481 if (resolv_conf && resolv_conf[0])
482 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
487 avahi_log_warn("Got spurious signal, ignoring.");
494 static gint run_server(DaemonConfig *config) {
495 GMainLoop *loop = NULL;
497 GIOChannel *io = NULL;
498 guint watch_id = (guint) -1;
502 loop = g_main_loop_new(NULL, FALSE);
504 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
505 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
509 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
510 avahi_log_error( "Failed to create signal io channel.");
514 g_io_channel_set_close_on_unref(io, FALSE);
515 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
517 if (simple_protocol_setup(NULL) < 0)
521 if (config->enable_dbus)
522 if (dbus_protocol_setup(loop) < 0)
526 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
529 load_resolv_conf(config);
531 static_service_load();
533 if (config->daemonize) {
534 daemon_retval_send(0);
538 g_main_loop_run(loop);
542 static_service_remove_from_server();
543 static_service_free_all();
544 remove_dns_server_entry_groups();
546 simple_protocol_shutdown();
549 if (config->enable_dbus)
550 dbus_protocol_shutdown();
555 avahi_server_free(avahi_server);
557 daemon_signal_done();
559 if (watch_id != (guint) -1)
560 g_source_remove(watch_id);
563 g_io_channel_unref(io);
567 g_main_loop_unref(loop);
569 if (r != 0 && config->daemonize)
570 daemon_retval_send(1);
575 static gint drop_root(void) {
580 if (!(pw = getpwnam(AVAHI_USER))) {
581 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
585 if (!(gr = getgrnam(AVAHI_GROUP))) {
586 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
590 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);
592 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
593 avahi_log_error("Failed to change group list: %s", strerror(errno));
597 #if defined(HAVE_SETRESGID)
598 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
599 #elif defined(HAVE_SETREGID)
600 r = setregid(gr->gr_gid, gr->gr_gid);
602 if ((r = setgid(gr->gr_gid)) >= 0)
603 r = setegid(gr->gr_gid);
607 avahi_log_error("Failed to change GID: %s", strerror(errno));
611 #if defined(HAVE_SETRESUID)
612 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
613 #elif defined(HAVE_SETREUID)
614 r = setreuid(pw->pw_uid, pw->pw_uid);
616 if ((r = setuid(pw->pw_uid)) >= 0)
617 r = seteuid(pw->pw_uid);
621 avahi_log_error("Failed to change UID: %s", strerror(errno));
625 g_setenv("USER", pw->pw_name, 1);
626 g_setenv("LOGNAME", pw->pw_name, 1);
627 g_setenv("HOME", pw->pw_dir, 1);
629 avahi_log_info("Successfully dropped root privileges.");
634 static const char* pid_file_proc(void) {
635 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
638 static gint make_runtime_dir(void) {
641 gboolean reset_umask = FALSE;
646 if (!(pw = getpwnam(AVAHI_USER))) {
647 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
651 if (!(gr = getgrnam(AVAHI_GROUP))) {
652 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
659 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
660 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
664 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
666 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
667 avahi_log_error("stat(): %s\n", strerror(errno));
671 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
672 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
685 int main(int argc, char *argv[]) {
688 gboolean wrote_pid_file = FALSE;
690 avahi_set_log_function(log_function);
692 avahi_server_config_init(&config.server_config);
693 config.command = DAEMON_RUN;
694 config.daemonize = FALSE;
695 config.config_file = NULL;
696 config.enable_dbus = TRUE;
697 config.drop_root = TRUE;
698 config.publish_dns_servers = NULL;
699 config.publish_resolv_conf = FALSE;
701 if ((argv0 = strrchr(argv[0], '/')))
706 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
707 daemon_pid_file_proc = pid_file_proc;
709 if (parse_command_line(&config, argc, argv) < 0)
712 if (config.command == DAEMON_HELP) {
715 } else if (config.command == DAEMON_VERSION) {
716 printf("%s "PACKAGE_VERSION"\n", argv0);
718 } else if (config.command == DAEMON_KILL) {
719 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
720 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
726 } else if (config.command == DAEMON_RELOAD) {
727 if (daemon_pid_file_kill(SIGHUP) < 0) {
728 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
734 } else if (config.command == DAEMON_CHECK)
735 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
736 else if (config.command == DAEMON_RUN) {
740 avahi_log_error("This program is intended to be run as root.");
744 if ((pid = daemon_pid_file_is_running()) >= 0) {
745 avahi_log_error("Daemon already running on PID %u", pid);
749 if (load_config_file(&config) < 0)
752 if (config.daemonize) {
753 daemon_retval_init();
755 if ((pid = daemon_fork()) < 0)
761 if ((ret = daemon_retval_wait(20)) < 0) {
762 avahi_log_error("Could not recieve return value from daemon process.");
775 if (make_runtime_dir() < 0)
778 if (config.drop_root) {
783 if (daemon_pid_file_create() < 0) {
784 avahi_log_error("Failed to create PID file: %s", strerror(errno));
786 if (config.daemonize)
787 daemon_retval_send(1);
790 wrote_pid_file = TRUE;
792 if (run_server(&config) == 0)
798 if (config.daemonize)
799 daemon_retval_done();
801 avahi_server_config_free(&config.server_config);
802 g_free(config.config_file);
803 g_strfreev(config.publish_dns_servers);
804 g_strfreev(resolv_conf);
807 daemon_pid_file_remove();