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
35 #include <libdaemon/dfork.h>
36 #include <libdaemon/dsignal.h>
37 #include <libdaemon/dlog.h>
38 #include <libdaemon/dpid.h>
40 #include <avahi-core/core.h>
41 #include <avahi-core/log.h>
44 #include "simple-protocol.h"
45 #include "dbus-protocol.h"
46 #include "static-services.h"
48 AvahiServer *avahi_server = NULL;
59 AvahiServerConfig server_config;
60 DaemonCommand command;
67 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
70 if (state == AVAHI_SERVER_RUNNING) {
71 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
72 static_service_add_to_server();
73 } else if (state == AVAHI_SERVER_COLLISION) {
76 static_service_remove_from_server();
78 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
79 avahi_log_warn("Host name conflict, retrying with <%s>", n);
80 avahi_server_set_host_name(s, n);
85 static void help(FILE *f, const gchar *argv0) {
88 " -h --help Show this help\n"
89 " -D --daemonize Daemonize after startup\n"
90 " -k --kill Kill a running daemon\n"
91 " -r --reload Request a running daemon to reload static services\n"
92 " -V --version Show version\n"
93 " -f --file=FILE Load the specified configuration file instead of\n"
94 " "AVAHI_CONFIG_FILE"\n",
98 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
101 static const struct option const long_options[] = {
102 { "help", no_argument, NULL, 'h' },
103 { "daemonize", no_argument, NULL, 'D' },
104 { "kill", no_argument, NULL, 'k' },
105 { "version", no_argument, NULL, 'V' },
106 { "file", required_argument, NULL, 'f' },
107 { "reload", no_argument, NULL, 'r' },
113 while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
117 config->command = DAEMON_HELP;
120 config->daemonize = TRUE;
123 config->command = DAEMON_KILL;
126 config->command = DAEMON_VERSION;
129 g_free(config->config_file);
130 config->config_file = g_strdup(optarg);
133 config->command = DAEMON_RELOAD;
136 fprintf(stderr, "Invalid command line argument: %c\n", c);
142 fprintf(stderr, "Too many arguments\n");
149 static gboolean is_yes(const gchar *s) {
152 return *s == 'y' || *s == 'Y';
155 static gint load_config_file(DaemonConfig *config) {
159 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
163 f = g_key_file_new();
165 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
166 fprintf(stderr, "Unable to read config file: %s\n", err->message);
170 groups = g_key_file_get_groups(f, NULL);
172 for (g = groups; *g; g++) {
173 if (g_strcasecmp(*g, "server") == 0) {
176 keys = g_key_file_get_keys(f, *g, NULL, NULL);
178 for (k = keys; *k; k++) {
180 v = g_key_file_get_value(f, *g, *k, NULL);
182 if (g_strcasecmp(*k, "host-name") == 0) {
183 g_free(config->server_config.host_name);
184 config->server_config.host_name = v;
186 } else if (g_strcasecmp(*k, "domain-name") == 0) {
187 g_free(config->server_config.domain_name);
188 config->server_config.domain_name = v;
190 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
191 config->server_config.use_ipv4 = is_yes(v);
192 else if (g_strcasecmp(*k, "use-ipv6") == 0)
193 config->server_config.use_ipv6 = is_yes(v);
194 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
195 config->server_config.check_response_ttl = is_yes(v);
196 else if (g_strcasecmp(*k, "use-iff-running") == 0)
197 config->server_config.use_iff_running = is_yes(v);
198 else if (g_strcasecmp(*k, "enable-dbus") == 0)
199 config->enable_dbus = is_yes(v);
200 else if (g_strcasecmp(*k, "drop-root") == 0)
201 config->drop_root = is_yes(v);
203 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
214 } else if (g_strcasecmp(*g, "publish") == 0) {
217 keys = g_key_file_get_keys(f, *g, NULL, NULL);
219 for (k = keys; *k; k++) {
221 v = g_key_file_get_string(f, *g, *k, NULL);
223 if (g_strcasecmp(*k, "publish-addresses") == 0)
224 config->server_config.publish_addresses = is_yes(v);
225 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
226 config->server_config.publish_hinfo = is_yes(v);
227 else if (g_strcasecmp(*k, "publish-workstation") == 0)
228 config->server_config.publish_workstation = is_yes(v);
229 else if (g_strcasecmp(*k, "publish-domain") == 0)
230 config->server_config.publish_domain = is_yes(v);
232 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
243 } else if (g_strcasecmp(*g, "reflector") == 0) {
246 keys = g_key_file_get_keys(f, *g, NULL, NULL);
248 for (k = keys; *k; k++) {
250 v = g_key_file_get_string(f, *g, *k, NULL);
252 if (g_strcasecmp(*k, "enable-reflector") == 0)
253 config->server_config.enable_reflector = is_yes(v);
254 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
255 config->server_config.reflect_ipv = is_yes(v);
257 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
269 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
291 static void log_function(AvahiLogLevel level, const gchar *txt) {
293 static const int const log_level_map[] = {
301 g_assert(level < AVAHI_LOG_LEVEL_MAX);
304 daemon_log(log_level_map[level], "%s", txt);
307 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
311 if ((sig = daemon_signal_next()) <= 0) {
312 avahi_log_error("daemon_signal_next() failed");
322 sig == SIGINT ? "SIGINT" :
323 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
324 g_main_loop_quit((GMainLoop*) data);
328 avahi_log_info("Got SIGHUP, reloading.");
329 static_service_load();
330 static_service_add_to_server();
334 avahi_log_warn("Got spurious signal, ignoring.");
341 static gint run_server(DaemonConfig *config) {
342 GMainLoop *loop = NULL;
344 GIOChannel *io = NULL;
345 guint watch_id = (guint) -1;
349 loop = g_main_loop_new(NULL, FALSE);
351 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
352 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
356 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
357 avahi_log_error( "Failed to create signal io channel.");
361 g_io_channel_set_close_on_unref(io, FALSE);
362 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
364 if (simple_protocol_setup(NULL) < 0)
368 if (config->enable_dbus)
369 if (dbus_protocol_setup(loop) < 0)
373 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, NULL)))
376 static_service_load();
378 if (config->daemonize) {
379 daemon_retval_send(0);
383 g_main_loop_run(loop);
387 static_service_remove_from_server();
388 static_service_free_all();
390 simple_protocol_shutdown();
393 if (config->enable_dbus)
394 dbus_protocol_shutdown();
398 avahi_server_free(avahi_server);
400 daemon_signal_done();
402 if (watch_id != (guint) -1)
403 g_source_remove(watch_id);
406 g_io_channel_unref(io);
410 g_main_loop_unref(loop);
412 if (r != 0 && config->daemonize)
413 daemon_retval_send(1);
418 static gint drop_root(void) {
423 if (!(pw = getpwnam(AVAHI_USER))) {
424 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
428 if (!(gr = getgrnam(AVAHI_GROUP))) {
429 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
433 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);
435 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
436 avahi_log_error("Failed to change group list: %s", strerror(errno));
440 #if defined(HAVE_SETRESGID)
441 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
442 #elif defined(HAVE_SETREGID)
443 r = setregid(gr->gr_gid, gr->gr_gid);
445 if ((r = setgid(gr->gr_gid)) >= 0)
446 r = setegid(gr->gr_gid);
450 avahi_log_error("Failed to change GID: %s", strerror(errno));
454 #if defined(HAVE_SETRESUID)
455 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
456 #elif defined(HAVE_SETREUID)
457 r = setreuid(pw->pw_uid, pw->pw_uid);
459 if ((r = setuid(pw->pw_uid)) >= 0)
460 r = seteuid(pw->pw_uid);
464 avahi_log_error("Failed to change UID: %s", strerror(errno));
468 g_setenv("USER", pw->pw_name, 1);
469 g_setenv("LOGNAME", pw->pw_name, 1);
470 g_setenv("HOME", pw->pw_dir, 1);
472 avahi_log_info("Successfully dropped root privileges.");
477 int main(int argc, char *argv[]) {
481 gboolean wrote_pid_file = FALSE;
483 avahi_set_log_function(log_function);
485 avahi_server_config_init(&config.server_config);
486 config.command = DAEMON_RUN;
487 config.daemonize = FALSE;
488 config.config_file = NULL;
489 config.enable_dbus = TRUE;
490 config.drop_root = TRUE;
492 if ((argv0 = strrchr(argv[0], '/')))
497 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
499 if (parse_command_line(&config, argc, argv) < 0)
502 if (config.command == DAEMON_HELP) {
505 } else if (config.command == DAEMON_VERSION) {
506 printf("%s "PACKAGE_VERSION"\n", argv0);
508 } else if (config.command == DAEMON_KILL) {
509 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
510 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
516 } else if (config.command == DAEMON_RELOAD) {
517 if (daemon_pid_file_kill(SIGHUP) < 0) {
518 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
524 } else if (config.command == DAEMON_RUN) {
528 avahi_log_error("This program is intended to be run as root.");
532 if ((pid = daemon_pid_file_is_running()) >= 0) {
533 avahi_log_error("Daemon already running on PID %u", pid);
537 if (load_config_file(&config) < 0)
540 if (config.daemonize) {
542 daemon_retval_init();
544 if ((pid = daemon_fork()) < 0)
550 if ((ret = daemon_retval_wait(20)) < 0) {
551 avahi_log_error("Could not recieve return value from daemon process.");
564 if (daemon_pid_file_create() < 0) {
565 avahi_log_error("Failed to create PID file: %s", strerror(errno));
567 if (config.daemonize)
568 daemon_retval_send(1);
571 wrote_pid_file = TRUE;
573 if (config.drop_root) {
578 if (run_server(&config) == 0)
584 if (config.daemonize)
585 daemon_retval_done();
587 avahi_server_config_free(&config.server_config);
588 g_free(config.config_file);
591 daemon_pid_file_remove();