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);
157 avahi_entry_group_commit(g);
162 static void remove_dns_server_entry_groups(void) {
164 if (resolv_conf_entry_group)
165 avahi_entry_group_reset(resolv_conf_entry_group);
167 if (dns_servers_entry_group)
168 avahi_entry_group_reset(dns_servers_entry_group);
171 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
172 DaemonConfig *c = userdata;
179 dbus_protocol_server_state_changed(state);
182 if (state == AVAHI_SERVER_RUNNING) {
183 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
184 static_service_add_to_server();
186 remove_dns_server_entry_groups();
188 if (resolv_conf && resolv_conf[0])
189 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
191 if (c->publish_dns_servers && c->publish_dns_servers[0])
192 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
194 simple_protocol_restart_queries();
196 } else if (state == AVAHI_SERVER_COLLISION) {
199 static_service_remove_from_server();
201 remove_dns_server_entry_groups();
203 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
204 avahi_log_warn("Host name conflict, retrying with <%s>", n);
205 avahi_server_set_host_name(s, n);
210 static void help(FILE *f, const gchar *argv0) {
213 " -h --help Show this help\n"
214 " -D --daemonize Daemonize after startup\n"
215 " -k --kill Kill a running daemon\n"
216 " -r --reload Request a running daemon to reload static services\n"
217 " -c --check Return 0 if a daemon is already running\n"
218 " -V --version Show version\n"
219 " -f --file=FILE Load the specified configuration file instead of\n"
220 " "AVAHI_CONFIG_FILE"\n",
224 static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
227 static const struct option const long_options[] = {
228 { "help", no_argument, NULL, 'h' },
229 { "daemonize", no_argument, NULL, 'D' },
230 { "kill", no_argument, NULL, 'k' },
231 { "version", no_argument, NULL, 'V' },
232 { "file", required_argument, NULL, 'f' },
233 { "reload", no_argument, NULL, 'r' },
234 { "check", no_argument, NULL, 'c' },
240 while ((o = getopt_long(argc, argv, "hDkVf:rc", long_options, NULL)) >= 0) {
244 c->command = DAEMON_HELP;
250 c->command = DAEMON_KILL;
253 c->command = DAEMON_VERSION;
256 g_free(c->config_file);
257 c->config_file = g_strdup(optarg);
260 c->command = DAEMON_RELOAD;
263 c->command = DAEMON_CHECK;
266 fprintf(stderr, "Invalid command line argument: %c\n", o);
272 fprintf(stderr, "Too many arguments\n");
279 static gboolean is_yes(const gchar *s) {
282 return *s == 'y' || *s == 'Y';
285 static gint load_config_file(DaemonConfig *c) {
289 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
293 f = g_key_file_new();
294 g_key_file_set_list_separator(f, ',');
296 if (!g_key_file_load_from_file(f, c->config_file ? c->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
297 fprintf(stderr, "Unable to read config file: %s\n", err->message);
301 groups = g_key_file_get_groups(f, NULL);
303 for (g = groups; *g; g++) {
304 if (g_strcasecmp(*g, "server") == 0) {
307 keys = g_key_file_get_keys(f, *g, NULL, NULL);
309 for (k = keys; *k; k++) {
311 v = g_key_file_get_value(f, *g, *k, NULL);
313 if (g_strcasecmp(*k, "host-name") == 0) {
314 g_free(c->server_config.host_name);
315 c->server_config.host_name = v;
317 } else if (g_strcasecmp(*k, "domain-name") == 0) {
318 g_free(c->server_config.domain_name);
319 c->server_config.domain_name = v;
321 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
322 c->server_config.use_ipv4 = is_yes(v);
323 else if (g_strcasecmp(*k, "use-ipv6") == 0)
324 c->server_config.use_ipv6 = is_yes(v);
325 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
326 c->server_config.check_response_ttl = is_yes(v);
327 else if (g_strcasecmp(*k, "use-iff-running") == 0)
328 c->server_config.use_iff_running = is_yes(v);
329 else if (g_strcasecmp(*k, "enable-dbus") == 0)
330 c->enable_dbus = is_yes(v);
331 else if (g_strcasecmp(*k, "drop-root") == 0)
332 c->drop_root = is_yes(v);
334 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
345 } else if (g_strcasecmp(*g, "publish") == 0) {
348 keys = g_key_file_get_keys(f, *g, NULL, NULL);
350 for (k = keys; *k; k++) {
352 v = g_key_file_get_string(f, *g, *k, NULL);
354 if (g_strcasecmp(*k, "publish-addresses") == 0)
355 c->server_config.publish_addresses = is_yes(v);
356 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
357 c->server_config.publish_hinfo = is_yes(v);
358 else if (g_strcasecmp(*k, "publish-workstation") == 0)
359 c->server_config.publish_workstation = is_yes(v);
360 else if (g_strcasecmp(*k, "publish-domain") == 0)
361 c->server_config.publish_domain = is_yes(v);
362 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
363 c->publish_resolv_conf = is_yes(v);
364 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
365 g_strfreev(c->publish_dns_servers);
366 c->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
368 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
379 } else if (g_strcasecmp(*g, "reflector") == 0) {
382 keys = g_key_file_get_keys(f, *g, NULL, NULL);
384 for (k = keys; *k; k++) {
386 v = g_key_file_get_string(f, *g, *k, NULL);
388 if (g_strcasecmp(*k, "enable-reflector") == 0)
389 c->server_config.enable_reflector = is_yes(v);
390 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
391 c->server_config.reflect_ipv = is_yes(v);
393 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
405 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
427 static void log_function(AvahiLogLevel level, const gchar *txt) {
429 static const int const log_level_map[] = {
437 g_assert(level < AVAHI_LOG_LEVEL_MAX);
440 daemon_log(log_level_map[level], "%s", txt);
443 static void dump(const gchar *text, gpointer userdata) {
444 avahi_log_info("%s", text);
447 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
449 GMainLoop *loop = data;
454 if ((sig = daemon_signal_next()) <= 0) {
455 avahi_log_error("daemon_signal_next() failed");
465 sig == SIGINT ? "SIGINT" :
466 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
467 g_main_loop_quit(loop);
471 avahi_log_info("Got SIGHUP, reloading.");
472 static_service_load();
473 static_service_add_to_server();
475 if (resolv_conf_entry_group)
476 avahi_entry_group_reset(resolv_conf_entry_group);
478 load_resolv_conf(&config);
480 if (resolv_conf && resolv_conf[0])
481 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
486 avahi_log_info("Got SIGUSR1, dumping record data.");
487 avahi_server_dump(avahi_server, dump, NULL);
491 avahi_log_warn("Got spurious signal, ignoring.");
498 static gint run_server(DaemonConfig *c) {
499 GMainLoop *loop = NULL;
501 GIOChannel *io = NULL;
502 guint watch_id = (guint) -1;
506 loop = g_main_loop_new(NULL, FALSE);
508 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
509 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
513 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
514 avahi_log_error( "Failed to create signal io channel.");
518 g_io_channel_set_close_on_unref(io, FALSE);
519 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
521 if (simple_protocol_setup(NULL) < 0)
526 if (dbus_protocol_setup(loop) < 0)
530 if (!(avahi_server = avahi_server_new(NULL, &c->server_config, server_callback, c)))
535 static_service_load();
538 daemon_retval_send(0);
542 g_main_loop_run(loop);
546 static_service_remove_from_server();
547 static_service_free_all();
548 remove_dns_server_entry_groups();
550 simple_protocol_shutdown();
554 dbus_protocol_shutdown();
559 avahi_server_free(avahi_server);
561 daemon_signal_done();
563 if (watch_id != (guint) -1)
564 g_source_remove(watch_id);
567 g_io_channel_unref(io);
571 g_main_loop_unref(loop);
573 if (r != 0 && c->daemonize)
574 daemon_retval_send(1);
579 static gint drop_root(void) {
584 if (!(pw = getpwnam(AVAHI_USER))) {
585 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
589 if (!(gr = getgrnam(AVAHI_GROUP))) {
590 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
594 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);
596 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
597 avahi_log_error("Failed to change group list: %s", strerror(errno));
601 #if defined(HAVE_SETRESGID)
602 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
603 #elif defined(HAVE_SETREGID)
604 r = setregid(gr->gr_gid, gr->gr_gid);
606 if ((r = setgid(gr->gr_gid)) >= 0)
607 r = setegid(gr->gr_gid);
611 avahi_log_error("Failed to change GID: %s", strerror(errno));
615 #if defined(HAVE_SETRESUID)
616 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
617 #elif defined(HAVE_SETREUID)
618 r = setreuid(pw->pw_uid, pw->pw_uid);
620 if ((r = setuid(pw->pw_uid)) >= 0)
621 r = seteuid(pw->pw_uid);
625 avahi_log_error("Failed to change UID: %s", strerror(errno));
629 g_setenv("USER", pw->pw_name, 1);
630 g_setenv("LOGNAME", pw->pw_name, 1);
631 g_setenv("HOME", pw->pw_dir, 1);
633 avahi_log_info("Successfully dropped root privileges.");
638 static const char* pid_file_proc(void) {
639 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
642 static gint make_runtime_dir(void) {
645 gboolean reset_umask = FALSE;
650 if (!(pw = getpwnam(AVAHI_USER))) {
651 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
655 if (!(gr = getgrnam(AVAHI_GROUP))) {
656 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
663 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
664 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
668 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
670 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
671 avahi_log_error("stat(): %s\n", strerror(errno));
675 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
676 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
689 int main(int argc, char *argv[]) {
692 gboolean wrote_pid_file = FALSE;
694 avahi_set_log_function(log_function);
696 avahi_server_config_init(&config.server_config);
697 config.command = DAEMON_RUN;
698 config.daemonize = FALSE;
699 config.config_file = NULL;
700 config.enable_dbus = TRUE;
701 config.drop_root = TRUE;
702 config.publish_dns_servers = NULL;
703 config.publish_resolv_conf = FALSE;
705 if ((argv0 = strrchr(argv[0], '/')))
710 daemon_pid_file_ident = (const char *) argv0;
711 daemon_log_ident = (char*) argv0;
712 daemon_pid_file_proc = pid_file_proc;
714 if (parse_command_line(&config, argc, argv) < 0)
717 if (config.command == DAEMON_HELP) {
720 } else if (config.command == DAEMON_VERSION) {
721 printf("%s "PACKAGE_VERSION"\n", argv0);
723 } else if (config.command == DAEMON_KILL) {
724 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
725 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
731 } else if (config.command == DAEMON_RELOAD) {
732 if (daemon_pid_file_kill(SIGHUP) < 0) {
733 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
739 } else if (config.command == DAEMON_CHECK)
740 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
741 else if (config.command == DAEMON_RUN) {
745 avahi_log_error("This program is intended to be run as root.");
749 if ((pid = daemon_pid_file_is_running()) >= 0) {
750 avahi_log_error("Daemon already running on PID %u", pid);
754 if (load_config_file(&config) < 0)
757 if (config.daemonize) {
758 daemon_retval_init();
760 if ((pid = daemon_fork()) < 0)
766 if ((ret = daemon_retval_wait(20)) < 0) {
767 avahi_log_error("Could not recieve return value from daemon process.");
779 printf("%s "PACKAGE_VERSION" starting up.\n", argv0);
783 if (make_runtime_dir() < 0)
786 if (config.drop_root) {
791 if (daemon_pid_file_create() < 0) {
792 avahi_log_error("Failed to create PID file: %s", strerror(errno));
794 if (config.daemonize)
795 daemon_retval_send(1);
798 wrote_pid_file = TRUE;
800 if (run_server(&config) == 0)
806 if (config.daemonize)
807 daemon_retval_done();
809 avahi_server_config_free(&config.server_config);
810 g_free(config.config_file);
811 g_strfreev(config.publish_dns_servers);
812 g_strfreev(resolv_conf);
815 daemon_pid_file_remove();