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
33 #include <libdaemon/dfork.h>
34 #include <libdaemon/dsignal.h>
35 #include <libdaemon/dlog.h>
36 #include <libdaemon/dpid.h>
38 #include <avahi-core/core.h>
39 #include <avahi-core/log.h>
42 #include "simple-protocol.h"
43 #include "dbus-protocol.h"
44 #include "static-services.h"
46 AvahiServer *avahi_server = NULL;
57 AvahiServerConfig server_config;
58 DaemonCommand command;
64 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
67 if (state == AVAHI_SERVER_RUNNING) {
68 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
69 static_service_add_to_server();
70 } else if (state == AVAHI_SERVER_COLLISION) {
73 static_service_remove_from_server();
75 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
76 avahi_log_warn("Host name conflict, retrying with <%s>", n);
77 avahi_server_set_host_name(s, n);
82 static void help(FILE *f, const gchar *argv0) {
85 " -h --help Show this help\n"
86 " -D --daemonize Daemonize after startup\n"
87 " -k --kill Kill a running daemon\n"
88 " -r --reload Request a running daemon to reload static services\n"
89 " -V --version Show version\n"
90 " -f --file=FILE Load the specified configuration file instead of\n"
91 " "AVAHI_CONFIG_FILE"\n",
95 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
98 static const struct option const long_options[] = {
99 { "help", no_argument, NULL, 'h' },
100 { "daemonize", no_argument, NULL, 'D' },
101 { "kill", no_argument, NULL, 'k' },
102 { "version", no_argument, NULL, 'V' },
103 { "file", required_argument, NULL, 'f' },
104 { "reload", no_argument, NULL, 'r' },
110 while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
114 config->command = DAEMON_HELP;
117 config->daemonize = TRUE;
120 config->command = DAEMON_KILL;
123 config->command = DAEMON_VERSION;
126 g_free(config->config_file);
127 config->config_file = g_strdup(optarg);
130 config->command = DAEMON_RELOAD;
133 fprintf(stderr, "Invalid command line argument: %c\n", c);
139 fprintf(stderr, "Too many arguments\n");
146 static gboolean is_yes(const gchar *s) {
149 return *s == 'y' || *s == 'Y';
152 static gint load_config_file(DaemonConfig *config) {
156 gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
160 f = g_key_file_new();
162 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
163 fprintf(stderr, "Unable to read config file: %s\n", err->message);
167 groups = g_key_file_get_groups(f, NULL);
169 for (g = groups; *g; g++) {
170 if (g_strcasecmp(*g, "server") == 0) {
173 keys = g_key_file_get_keys(f, *g, NULL, NULL);
175 for (k = keys; *k; k++) {
177 v = g_key_file_get_value(f, *g, *k, NULL);
179 if (g_strcasecmp(*k, "host-name") == 0) {
180 g_free(config->server_config.host_name);
181 config->server_config.host_name = v;
183 } else if (g_strcasecmp(*k, "domain-name") == 0) {
184 g_free(config->server_config.domain_name);
185 config->server_config.domain_name = v;
187 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
188 config->server_config.use_ipv4 = is_yes(v);
189 else if (g_strcasecmp(*k, "use-ipv6") == 0)
190 config->server_config.use_ipv6 = is_yes(v);
191 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
192 config->server_config.check_response_ttl = is_yes(v);
193 else if (g_strcasecmp(*k, "use-iff-running") == 0)
194 config->server_config.use_iff_running = is_yes(v);
195 else if (g_strcasecmp(*k, "enable-dbus") == 0)
196 config->enable_dbus = is_yes(v);
198 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
209 } else if (g_strcasecmp(*g, "publish") == 0) {
212 keys = g_key_file_get_keys(f, *g, NULL, NULL);
214 for (k = keys; *k; k++) {
216 v = g_key_file_get_string(f, *g, *k, NULL);
218 if (g_strcasecmp(*k, "publish-addresses") == 0)
219 config->server_config.publish_addresses = is_yes(v);
220 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
221 config->server_config.publish_hinfo = is_yes(v);
222 else if (g_strcasecmp(*k, "publish-workstation") == 0)
223 config->server_config.publish_workstation = is_yes(v);
224 else if (g_strcasecmp(*k, "publish-domain") == 0)
225 config->server_config.publish_domain = is_yes(v);
227 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
238 } else if (g_strcasecmp(*g, "reflector") == 0) {
241 keys = g_key_file_get_keys(f, *g, NULL, NULL);
243 for (k = keys; *k; k++) {
245 v = g_key_file_get_string(f, *g, *k, NULL);
247 if (g_strcasecmp(*k, "enable-reflector") == 0)
248 config->server_config.enable_reflector = is_yes(v);
249 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
250 config->server_config.reflect_ipv = is_yes(v);
252 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
264 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
286 static void log_function(AvahiLogLevel level, const gchar *txt) {
288 static const int const log_level_map[] = {
296 g_assert(level < AVAHI_LOG_LEVEL_MAX);
299 daemon_log(log_level_map[level], "%s", txt);
302 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
306 if ((sig = daemon_signal_next()) <= 0) {
307 avahi_log_error("daemon_signal_next() failed");
317 sig == SIGINT ? "SIGINT" :
318 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
319 g_main_loop_quit((GMainLoop*) data);
323 avahi_log_info("Got SIGHUP, reloading.");
324 static_service_load();
325 static_service_add_to_server();
329 avahi_log_warn("Got spurious signal, ignoring.");
336 static gint run_server(DaemonConfig *config) {
337 GMainLoop *loop = NULL;
339 GIOChannel *io = NULL;
340 guint watch_id = (guint) -1;
344 loop = g_main_loop_new(NULL, FALSE);
346 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
347 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
351 if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
352 avahi_log_error( "Failed to create signal io channel.");
356 g_io_channel_set_close_on_unref(io, FALSE);
357 g_io_add_watch(io, G_IO_IN, signal_callback, loop);
359 if (simple_protocol_setup(NULL) < 0)
363 if (config->enable_dbus)
364 if (dbus_protocol_setup(loop) < 0)
368 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, NULL)))
371 static_service_load();
373 if (config->daemonize) {
374 daemon_retval_send(0);
378 g_main_loop_run(loop);
382 static_service_remove_from_server();
383 static_service_free_all();
385 simple_protocol_shutdown();
388 if (config->enable_dbus)
389 dbus_protocol_shutdown();
393 avahi_server_free(avahi_server);
395 daemon_signal_done();
397 if (watch_id != (guint) -1)
398 g_source_remove(watch_id);
401 g_io_channel_unref(io);
405 g_main_loop_unref(loop);
407 if (r != 0 && config->daemonize)
408 daemon_retval_send(1);
413 int main(int argc, char *argv[]) {
417 gboolean wrote_pid_file = FALSE;
419 avahi_set_log_function(log_function);
421 avahi_server_config_init(&config.server_config);
422 config.command = DAEMON_RUN;
423 config.daemonize = FALSE;
424 config.config_file = NULL;
425 config.enable_dbus = TRUE;
427 if ((argv0 = strrchr(argv[0], '/')))
432 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
434 if (parse_command_line(&config, argc, argv) < 0)
437 if (config.command == DAEMON_HELP) {
440 } else if (config.command == DAEMON_VERSION) {
441 printf("%s "PACKAGE_VERSION"\n", argv0);
443 } else if (config.command == DAEMON_KILL) {
444 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
445 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
451 } else if (config.command == DAEMON_RELOAD) {
452 if (daemon_pid_file_kill(SIGHUP) < 0) {
453 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
459 } else if (config.command == DAEMON_RUN) {
463 avahi_log_error("This program is intended to be run as root.");
467 if ((pid = daemon_pid_file_is_running()) >= 0) {
468 avahi_log_error("Daemon already running on PID %u", pid);
472 if (load_config_file(&config) < 0)
475 if (config.daemonize) {
477 daemon_retval_init();
479 if ((pid = daemon_fork()) < 0)
485 if ((ret = daemon_retval_wait(20)) < 0) {
486 avahi_log_error("Could not recieve return value from daemon process.");
497 if (daemon_pid_file_create() < 0) {
498 avahi_log_error("Failed to create PID file: %s", strerror(errno));
500 if (config.daemonize)
501 daemon_retval_send(1);
504 wrote_pid_file = TRUE;
506 if (run_server(&config) == 0)
512 if (config.daemonize)
513 daemon_retval_done();
515 avahi_server_config_free(&config.server_config);
516 g_free(config.config_file);
519 daemon_pid_file_remove();