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
32 #include <libdaemon/dfork.h>
33 #include <libdaemon/dsignal.h>
34 #include <libdaemon/dlog.h>
35 #include <libdaemon/dpid.h>
37 #include <avahi-core/core.h>
38 #include <avahi-core/log.h>
41 #include "simple-protocol.h"
42 #include "dbus-protocol.h"
43 #include "static-services.h"
45 AvahiServer *avahi_server = NULL;
55 AvahiServerConfig server_config;
56 DaemonCommand command;
62 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
65 if (state == AVAHI_SERVER_RUNNING) {
66 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
67 static_service_add_to_server();
68 } else if (state == AVAHI_SERVER_COLLISION) {
71 static_service_remove_from_server();
73 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
74 avahi_log_warn("Host name conflict, retrying with <%s>", n);
75 avahi_server_set_host_name(s, n);
80 static void help(FILE *f, const gchar *argv0) {
83 " -h --help Show this help\n"
84 " -D --daemonize Daemonize after startup\n"
85 " -k --kill Kill a running daemon\n"
86 " -V --version Show version\n"
87 " -f --file=FILE Load the specified configuration file instead of the default\n",
91 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
94 static const struct option const long_options[] = {
95 { "help", no_argument, NULL, 'h' },
96 { "daemonize", no_argument, NULL, 'D' },
97 { "kill", no_argument, NULL, 'k' },
98 { "version", no_argument, NULL, 'V' },
99 { "file", required_argument, NULL, 'f' },
105 while ((c = getopt_long(argc, argv, "hDkVf:", long_options, NULL)) >= 0) {
109 config->command = DAEMON_HELP;
112 config->daemonize = TRUE;
115 config->command = DAEMON_KILL;
118 config->command = DAEMON_VERSION;
121 g_free(config->config_file);
122 config->config_file = g_strdup(optarg);
125 fprintf(stderr, "Invalid command line argument: %c\n", c);
131 fprintf(stderr, "Too many arguments\n");
138 static gboolean is_yes(const gchar *s) {
141 return *s == 'y' || *s == 'Y';
144 static gint load_config_file(DaemonConfig *config) {
148 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
152 f = g_key_file_new();
154 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
155 fprintf(stderr, "Unable to read config file: %s\n", err->message);
159 groups = g_key_file_get_groups(f, NULL);
161 for (g = groups; *g; g++) {
162 if (g_strcasecmp(*g, "server") == 0) {
165 keys = g_key_file_get_keys(f, *g, NULL, NULL);
167 for (k = keys; *k; k++) {
169 v = g_key_file_get_value(f, *g, *k, NULL);
171 if (g_strcasecmp(*k, "host-name") == 0) {
172 g_free(config->server_config.host_name);
173 config->server_config.host_name = v;
175 } else if (g_strcasecmp(*k, "domain-name") == 0) {
176 g_free(config->server_config.domain_name);
177 config->server_config.domain_name = v;
179 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
180 config->server_config.use_ipv4 = is_yes(v);
181 else if (g_strcasecmp(*k, "use-ipv6") == 0)
182 config->server_config.use_ipv6 = is_yes(v);
183 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
184 config->server_config.check_response_ttl = is_yes(v);
185 else if (g_strcasecmp(*k, "use-iff-running") == 0)
186 config->server_config.use_iff_running = is_yes(v);
187 else if (g_strcasecmp(*k, "enable-dbus") == 0)
188 config->enable_dbus = is_yes(v);
190 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
201 } else if (g_strcasecmp(*g, "publish") == 0) {
204 keys = g_key_file_get_keys(f, *g, NULL, NULL);
206 for (k = keys; *k; k++) {
208 v = g_key_file_get_string(f, *g, *k, NULL);
210 if (g_strcasecmp(*k, "publish-addresses") == 0)
211 config->server_config.publish_addresses = is_yes(v);
212 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
213 config->server_config.publish_hinfo = is_yes(v);
214 else if (g_strcasecmp(*k, "publish-workstation") == 0)
215 config->server_config.publish_workstation = is_yes(v);
216 else if (g_strcasecmp(*k, "publish-domain") == 0)
217 config->server_config.publish_domain = is_yes(v);
219 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
230 } else if (g_strcasecmp(*g, "reflector") == 0) {
233 keys = g_key_file_get_keys(f, *g, NULL, NULL);
235 for (k = keys; *k; k++) {
237 v = g_key_file_get_string(f, *g, *k, NULL);
239 if (g_strcasecmp(*k, "enable-reflector") == 0)
240 config->server_config.enable_reflector = is_yes(v);
241 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
242 config->server_config.reflect_ipv = is_yes(v);
244 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
256 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
278 static void log_function(AvahiLogLevel level, const gchar *txt) {
280 static const int const log_level_map[] = {
288 g_assert(level < AVAHI_LOG_LEVEL_MAX);
291 daemon_log(log_level_map[level], "%s", txt);
294 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
298 if ((sig = daemon_signal_next()) <= 0) {
299 avahi_log_error("daemon_signal_next() failed");
309 sig == SIGINT ? "SIGINT" :
310 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
311 g_main_loop_quit((GMainLoop*) data);
315 avahi_log_info("Got SIGHUP, reloading.");
316 static_service_load();
320 avahi_log_warn("Got spurious signal, ignoring.");
327 static gint run_server(DaemonConfig *config) {
328 GMainLoop *loop = NULL;
330 GIOChannel *io = NULL;
331 guint watch_id = (guint) -1;
335 loop = g_main_loop_new(NULL, FALSE);
337 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
338 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
342 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
343 avahi_log_error( "Failed to create signal io channel.");
347 g_io_channel_set_close_on_unref(io, FALSE);
348 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
350 if (simple_protocol_setup(NULL) < 0)
354 if (config->enable_dbus)
355 if (dbus_protocol_setup(loop) < 0)
359 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, NULL)))
362 static_service_load();
364 if (config->daemonize) {
365 daemon_retval_send(0);
369 g_main_loop_run(loop);
373 static_service_remove_from_server();
374 static_service_free_all();
376 simple_protocol_shutdown();
379 if (config->enable_dbus)
380 dbus_protocol_shutdown();
384 avahi_server_free(avahi_server);
386 daemon_signal_done();
388 if (watch_id != (guint) -1)
389 g_source_remove(watch_id);
392 g_io_channel_unref(io);
396 g_main_loop_unref(loop);
398 if (r != 0 && config->daemonize)
399 daemon_retval_send(1);
404 int main(int argc, char *argv[]) {
408 gboolean wrote_pid_file = FALSE;
410 avahi_set_log_function(log_function);
412 avahi_server_config_init(&config.server_config);
413 config.command = DAEMON_RUN;
414 config.daemonize = FALSE;
415 config.config_file = NULL;
416 config.enable_dbus = TRUE;
418 if ((argv0 = strrchr(argv[0], '/')))
423 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
425 if (parse_command_line(&config, argc, argv) < 0)
428 if (load_config_file(&config) < 0)
431 if (config.command == DAEMON_HELP) {
434 } else if (config.command == DAEMON_VERSION) {
435 printf("%s "PACKAGE_VERSION"\n", argv0);
437 } else if (config.command == DAEMON_KILL) {
438 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
439 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
445 } else if (config.command == DAEMON_RUN) {
448 if ((pid = daemon_pid_file_is_running()) >= 0) {
449 avahi_log_error("Daemon already running on PID %u", pid);
453 if (config.daemonize) {
455 daemon_retval_init();
457 if ((pid = daemon_fork()) < 0)
463 if ((ret = daemon_retval_wait(20)) < 0) {
464 avahi_log_error("Could not recieve return value from daemon process.");
475 if (daemon_pid_file_create() < 0) {
476 avahi_log_error("Failed to create PID file: %s", strerror(errno));
478 if (config.daemonize)
479 daemon_retval_send(1);
482 wrote_pid_file = TRUE;
484 if (run_server(&config) == 0)
490 if (config.daemonize)
491 daemon_retval_done();
493 avahi_server_config_free(&config.server_config);
494 g_free(config.config_file);
497 daemon_pid_file_remove();