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
30 #include <libdaemon/dfork.h>
31 #include <libdaemon/dsignal.h>
32 #include <libdaemon/dlog.h>
33 #include <libdaemon/dpid.h>
35 #include <avahi-core/core.h>
36 #include <avahi-core/log.h>
39 #include "simple-protocol.h"
40 #include "dbus-protocol.h"
41 #include "static-services.h"
43 AvahiServer *avahi_server = NULL;
53 AvahiServerConfig server_config;
54 DaemonCommand command;
60 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
63 if (state == AVAHI_SERVER_RUNNING) {
64 avahi_log_info("Server startup complete. Host name is <%s>", avahi_server_get_host_name_fqdn(s));
65 static_service_add_to_server();
66 } else if (state == AVAHI_SERVER_COLLISION) {
69 static_service_remove_from_server();
71 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
72 avahi_log_warn("Host name conflict, retrying with <%s>", n);
73 avahi_server_set_host_name(s, n);
78 static void help(FILE *f, const gchar *argv0) {
81 " -h --help Show this help\n"
82 " -D --daemonize Daemonize after startup\n"
83 " -k --kill Kill a running daemon\n"
84 " -V --version Show version\n"
85 " -f --file=FILE Load the specified configuration file instead of the default\n",
89 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
92 static const struct option const long_options[] = {
93 { "help", no_argument, NULL, 'h' },
94 { "daemonize", no_argument, NULL, 'D' },
95 { "kill", no_argument, NULL, 'k' },
96 { "version", no_argument, NULL, 'V' },
97 { "file", required_argument, NULL, 'f' },
103 while ((c = getopt_long(argc, argv, "hDkVf:", long_options, NULL)) >= 0) {
107 config->command = DAEMON_HELP;
110 config->daemonize = TRUE;
113 config->command = DAEMON_KILL;
116 config->command = DAEMON_VERSION;
119 g_free(config->config_file);
120 config->config_file = g_strdup(optarg);
123 fprintf(stderr, "Invalid command line argument: %c\n", c);
129 fprintf(stderr, "Too many arguments\n");
136 static gboolean is_yes(const gchar *s) {
139 return *s == 'y' || *s == 'Y';
142 static gint load_config_file(DaemonConfig *config) {
146 gchar **groups = NULL, **g, **keys = NULL;
150 f = g_key_file_new();
152 if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
153 fprintf(stderr, "Unable to read config file: %s\n", err->message);
157 groups = g_key_file_get_groups(f, NULL);
159 for (g = groups; *g; g++) {
160 if (g_strcasecmp(*g, "server") == 0) {
163 keys = g_key_file_get_keys(f, *g, NULL, NULL);
165 for (k = keys; *k; k++) {
166 if (g_strcasecmp(*k, "host-name") == 0) {
167 g_free(config->server_config.host_name);
168 config->server_config.host_name = g_strdup(g_key_file_get_string(f, *g, *k, NULL));
169 } else if (g_strcasecmp(*k, "domain-name") == 0) {
170 g_free(config->server_config.domain_name);
171 config->server_config.domain_name = g_strdup(g_key_file_get_string(f, *g, *k, NULL));
172 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
173 config->server_config.use_ipv4 = is_yes(g_key_file_get_string(f, *g, *k, NULL));
174 else if (g_strcasecmp(*k, "use-ipv6") == 0)
175 config->server_config.use_ipv6 = is_yes(g_key_file_get_string(f, *g, *k, NULL));
176 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
177 config->server_config.check_response_ttl = is_yes(g_key_file_get_string(f, *g, *k, NULL));
178 else if (g_strcasecmp(*k, "use-iff-running") == 0)
179 config->server_config.use_iff_running = is_yes(g_key_file_get_string(f, *g, *k, NULL));
180 else if (g_strcasecmp(*k, "enable-dbus") == 0)
181 config->enable_dbus = is_yes(g_key_file_get_string(f, *g, *k, NULL));
183 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
188 } else if (g_strcasecmp(*g, "publish") == 0) {
191 keys = g_key_file_get_keys(f, *g, NULL, NULL);
193 for (k = keys; *k; k++) {
194 if (g_strcasecmp(*k, "publish-addresses") == 0)
195 config->server_config.publish_addresses = is_yes(g_key_file_get_string(f, *g, *k, NULL));
196 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
197 config->server_config.publish_hinfo = is_yes(g_key_file_get_string(f, *g, *k, NULL));
198 else if (g_strcasecmp(*k, "publish-workstation") == 0)
199 config->server_config.publish_workstation = is_yes(g_key_file_get_string(f, *g, *k, NULL));
200 else if (g_strcasecmp(*k, "publish-domain") == 0)
201 config->server_config.publish_domain = is_yes(g_key_file_get_string(f, *g, *k, NULL));
203 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
208 } else if (g_strcasecmp(*g, "reflector") == 0) {
211 keys = g_key_file_get_keys(f, *g, NULL, NULL);
213 for (k = keys; *k; k++) {
214 if (g_strcasecmp(*k, "enable-reflector") == 0)
215 config->server_config.enable_reflector = is_yes(g_key_file_get_string(f, *g, *k, NULL));
216 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
217 config->server_config.reflect_ipv = is_yes(g_key_file_get_string(f, *g, *k, NULL));
219 fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
225 fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
246 static void log_function(AvahiLogLevel level, const gchar *txt) {
248 static const int const log_level_map[] = {
256 g_assert(level < AVAHI_LOG_LEVEL_MAX);
259 daemon_log(log_level_map[level], "%s", txt);
262 static gint run_server(DaemonConfig *config) {
263 GMainLoop *loop = NULL;
268 loop = g_main_loop_new(NULL, FALSE);
270 if (simple_protocol_setup(NULL) < 0)
274 if (config->enable_dbus)
275 if (dbus_protocol_setup(loop) < 0)
279 if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, NULL)))
282 static_service_load();
284 if (config->daemonize) {
285 daemon_retval_send(0);
289 g_main_loop_run(loop);
294 static_service_remove_from_server();
295 static_service_free_all();
297 simple_protocol_shutdown();
300 if (config->enable_dbus)
301 dbus_protocol_shutdown();
305 avahi_server_free(avahi_server);
308 g_main_loop_unref(loop);
310 if (r != 0 && config->daemonize)
311 daemon_retval_send(0);
316 int main(int argc, char *argv[]) {
320 gboolean wrote_pid_file = FALSE;
322 avahi_set_log_function(log_function);
324 avahi_server_config_init(&config.server_config);
325 config.command = DAEMON_RUN;
326 config.daemonize = FALSE;
327 config.config_file = NULL;
328 config.enable_dbus = TRUE;
330 if ((argv0 = strrchr(argv[0], '/')))
335 daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
337 if (parse_command_line(&config, argc, argv) < 0)
340 if (load_config_file(&config) < 0)
343 if (config.command == DAEMON_HELP) {
346 } else if (config.command == DAEMON_VERSION) {
347 printf("%s "PACKAGE_VERSION"\n", argv0);
349 } else if (config.command == DAEMON_KILL) {
350 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
351 avahi_log_warn("Failed to kill daemon");
357 } else if (config.command == DAEMON_RUN) {
360 if ((pid = daemon_pid_file_is_running()) >= 0) {
361 avahi_log_error("Daemon already running on PID %u", pid);
365 if (config.daemonize) {
367 daemon_retval_init();
369 if ((pid = daemon_fork()) < 0)
375 if ((ret = daemon_retval_wait(20)) < 0) {
376 avahi_log_error("Could not recieve return value from daemon process.");
387 if (daemon_pid_file_create() < 0) {
388 avahi_log_error("Failed to create PID file.");
389 daemon_retval_send(1);
392 wrote_pid_file = TRUE;
394 if (run_server(&config) == 0)
400 if (config.daemonize)
401 daemon_retval_done();
403 avahi_server_config_free(&config.server_config);
404 g_free(config.config_file);
407 daemon_pid_file_remove();