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;
60 AvahiServerConfig server_config;
61 DaemonCommand command;
68 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
71 if (state == AVAHI_SERVER_RUNNING) {
72 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
73 static_service_add_to_server();
74 } else if (state == AVAHI_SERVER_COLLISION) {
77 static_service_remove_from_server();
79 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
80 avahi_log_warn("Host name conflict, retrying with <%s>", n);
81 avahi_server_set_host_name(s, n);
86 static void help(FILE *f, const gchar *argv0) {
89 " -h --help Show this help\n"
90 " -D --daemonize Daemonize after startup\n"
91 " -k --kill Kill a running daemon\n"
92 " -r --reload Request a running daemon to reload static services\n"
93 " -V --version Show version\n"
94 " -f --file=FILE Load the specified configuration file instead of\n"
95 " "AVAHI_CONFIG_FILE"\n",
99 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
102 static const struct option const long_options[] = {
103 { "help", no_argument, NULL, 'h' },
104 { "daemonize", no_argument, NULL, 'D' },
105 { "kill", no_argument, NULL, 'k' },
106 { "version", no_argument, NULL, 'V' },
107 { "file", required_argument, NULL, 'f' },
108 { "reload", no_argument, NULL, 'r' },
114 while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
118 config->command = DAEMON_HELP;
121 config->daemonize = TRUE;
124 config->command = DAEMON_KILL;
127 config->command = DAEMON_VERSION;
130 g_free(config->config_file);
131 config->config_file = g_strdup(optarg);
134 config->command = DAEMON_RELOAD;
137 fprintf(stderr, "Invalid command line argument: %c\n", c);
143 fprintf(stderr, "Too many arguments\n");
150 static gboolean is_yes(const gchar *s) {
153 return *s == 'y' || *s == 'Y';
156 static gint load_config_file(DaemonConfig *config) {
160 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
164 f = g_key_file_new();
166 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
167 fprintf(stderr, "Unable to read config file: %s\n", err->message);
171 groups = g_key_file_get_groups(f, NULL);
173 for (g = groups; *g; g++) {
174 if (g_strcasecmp(*g, "server") == 0) {
177 keys = g_key_file_get_keys(f, *g, NULL, NULL);
179 for (k = keys; *k; k++) {
181 v = g_key_file_get_value(f, *g, *k, NULL);
183 if (g_strcasecmp(*k, "host-name") == 0) {
184 g_free(config->server_config.host_name);
185 config->server_config.host_name = v;
187 } else if (g_strcasecmp(*k, "domain-name") == 0) {
188 g_free(config->server_config.domain_name);
189 config->server_config.domain_name = v;
191 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
192 config->server_config.use_ipv4 = is_yes(v);
193 else if (g_strcasecmp(*k, "use-ipv6") == 0)
194 config->server_config.use_ipv6 = is_yes(v);
195 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
196 config->server_config.check_response_ttl = is_yes(v);
197 else if (g_strcasecmp(*k, "use-iff-running") == 0)
198 config->server_config.use_iff_running = is_yes(v);
199 else if (g_strcasecmp(*k, "enable-dbus") == 0)
200 config->enable_dbus = is_yes(v);
201 else if (g_strcasecmp(*k, "drop-root") == 0)
202 config->drop_root = is_yes(v);
204 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
215 } else if (g_strcasecmp(*g, "publish") == 0) {
218 keys = g_key_file_get_keys(f, *g, NULL, NULL);
220 for (k = keys; *k; k++) {
222 v = g_key_file_get_string(f, *g, *k, NULL);
224 if (g_strcasecmp(*k, "publish-addresses") == 0)
225 config->server_config.publish_addresses = is_yes(v);
226 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
227 config->server_config.publish_hinfo = is_yes(v);
228 else if (g_strcasecmp(*k, "publish-workstation") == 0)
229 config->server_config.publish_workstation = is_yes(v);
230 else if (g_strcasecmp(*k, "publish-domain") == 0)
231 config->server_config.publish_domain = is_yes(v);
233 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
244 } else if (g_strcasecmp(*g, "reflector") == 0) {
247 keys = g_key_file_get_keys(f, *g, NULL, NULL);
249 for (k = keys; *k; k++) {
251 v = g_key_file_get_string(f, *g, *k, NULL);
253 if (g_strcasecmp(*k, "enable-reflector") == 0)
254 config->server_config.enable_reflector = is_yes(v);
255 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
256 config->server_config.reflect_ipv = is_yes(v);
258 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
270 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
292 static void log_function(AvahiLogLevel level, const gchar *txt) {
294 static const int const log_level_map[] = {
302 g_assert(level < AVAHI_LOG_LEVEL_MAX);
305 daemon_log(log_level_map[level], "%s", txt);
308 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
312 if ((sig = daemon_signal_next()) <= 0) {
313 avahi_log_error("daemon_signal_next() failed");
323 sig == SIGINT ? "SIGINT" :
324 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
325 g_main_loop_quit((GMainLoop*) data);
329 avahi_log_info("Got SIGHUP, reloading.");
330 static_service_load();
331 static_service_add_to_server();
335 avahi_log_warn("Got spurious signal, ignoring.");
342 static gint run_server(DaemonConfig *config) {
343 GMainLoop *loop = NULL;
345 GIOChannel *io = NULL;
346 guint watch_id = (guint) -1;
350 loop = g_main_loop_new(NULL, FALSE);
352 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
353 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
357 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
358 avahi_log_error( "Failed to create signal io channel.");
362 g_io_channel_set_close_on_unref(io, FALSE);
363 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
365 if (simple_protocol_setup(NULL) < 0)
369 if (config->enable_dbus)
370 if (dbus_protocol_setup(loop) < 0)
374 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, NULL)))
377 static_service_load();
379 if (config->daemonize) {
380 daemon_retval_send(0);
384 g_main_loop_run(loop);
388 static_service_remove_from_server();
389 static_service_free_all();
391 simple_protocol_shutdown();
394 if (config->enable_dbus)
395 dbus_protocol_shutdown();
399 avahi_server_free(avahi_server);
401 daemon_signal_done();
403 if (watch_id != (guint) -1)
404 g_source_remove(watch_id);
407 g_io_channel_unref(io);
411 g_main_loop_unref(loop);
413 if (r != 0 && config->daemonize)
414 daemon_retval_send(1);
419 static gint drop_root(void) {
424 if (!(pw = getpwnam(AVAHI_USER))) {
425 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
429 if (!(gr = getgrnam(AVAHI_GROUP))) {
430 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
434 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);
436 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
437 avahi_log_error("Failed to change group list: %s", strerror(errno));
441 #if defined(HAVE_SETRESGID)
442 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
443 #elif defined(HAVE_SETREGID)
444 r = setregid(gr->gr_gid, gr->gr_gid);
446 if ((r = setgid(gr->gr_gid)) >= 0)
447 r = setegid(gr->gr_gid);
451 avahi_log_error("Failed to change GID: %s", strerror(errno));
455 #if defined(HAVE_SETRESUID)
456 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
457 #elif defined(HAVE_SETREUID)
458 r = setreuid(pw->pw_uid, pw->pw_uid);
460 if ((r = setuid(pw->pw_uid)) >= 0)
461 r = seteuid(pw->pw_uid);
465 avahi_log_error("Failed to change UID: %s", strerror(errno));
469 g_setenv("USER", pw->pw_name, 1);
470 g_setenv("LOGNAME", pw->pw_name, 1);
471 g_setenv("HOME", pw->pw_dir, 1);
473 avahi_log_info("Successfully dropped root privileges.");
478 static const char* pid_file_proc(void) {
479 return AVAHI_RUNTIME_DIR"/pid";
482 static gint make_runtime_dir(void) {
485 gboolean reset_umask = FALSE;
490 if (!(pw = getpwnam(AVAHI_USER))) {
491 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
495 if (!(gr = getgrnam(AVAHI_GROUP))) {
496 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
503 if (mkdir(AVAHI_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
504 avahi_log_error("mkdir(\""AVAHI_RUNTIME_DIR"\"): %s", strerror(errno));
508 chown(AVAHI_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
510 if (stat(AVAHI_RUNTIME_DIR, &st) < 0) {
511 avahi_log_error("stat(): %s\n", strerror(errno));
515 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
516 avahi_log_error("Failed to create runtime directory "AVAHI_RUNTIME_DIR".");
531 int main(int argc, char *argv[]) {
535 gboolean wrote_pid_file = FALSE;
537 avahi_set_log_function(log_function);
539 avahi_server_config_init(&config.server_config);
540 config.command = DAEMON_RUN;
541 config.daemonize = FALSE;
542 config.config_file = NULL;
543 config.enable_dbus = TRUE;
544 config.drop_root = TRUE;
546 if ((argv0 = strrchr(argv[0], '/')))
551 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
553 daemon_pid_file_proc = pid_file_proc;
555 if (parse_command_line(&config, argc, argv) < 0)
558 if (config.command == DAEMON_HELP) {
561 } else if (config.command == DAEMON_VERSION) {
562 printf("%s "PACKAGE_VERSION"\n", argv0);
564 } else if (config.command == DAEMON_KILL) {
565 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
566 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
572 } else if (config.command == DAEMON_RELOAD) {
573 if (daemon_pid_file_kill(SIGHUP) < 0) {
574 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
580 } else if (config.command == DAEMON_RUN) {
584 avahi_log_error("This program is intended to be run as root.");
588 if ((pid = daemon_pid_file_is_running()) >= 0) {
589 avahi_log_error("Daemon already running on PID %u", pid);
593 if (load_config_file(&config) < 0)
596 if (config.daemonize) {
598 daemon_retval_init();
600 if ((pid = daemon_fork()) < 0)
606 if ((ret = daemon_retval_wait(20)) < 0) {
607 avahi_log_error("Could not recieve return value from daemon process.");
620 if (make_runtime_dir() < 0)
623 if (config.drop_root) {
628 if (daemon_pid_file_create() < 0) {
629 avahi_log_error("Failed to create PID file: %s", strerror(errno));
631 if (config.daemonize)
632 daemon_retval_send(1);
635 wrote_pid_file = TRUE;
637 if (run_server(&config) == 0)
643 if (config.daemonize)
644 daemon_retval_done();
646 avahi_server_config_free(&config.server_config);
647 g_free(config.config_file);
650 daemon_pid_file_remove();