]> git.meshlink.io Git - catta/blob - avahi-daemon/main.c
* recreate DNS query in simple protocol on host or domain name changes
[catta] / avahi-daemon / main.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <getopt.h>
27 #include <string.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <grp.h>
33 #include <pwd.h>
34 #include <sys/stat.h>
35
36 #include <libdaemon/dfork.h>
37 #include <libdaemon/dsignal.h>
38 #include <libdaemon/dlog.h>
39 #include <libdaemon/dpid.h>
40
41 #include <avahi-core/core.h>
42 #include <avahi-core/log.h>
43
44 #include "main.h"
45 #include "simple-protocol.h"
46 #include "dbus-protocol.h"
47 #include "static-services.h"
48
49 AvahiServer *avahi_server = NULL;
50
51 typedef enum {
52     DAEMON_RUN,
53     DAEMON_KILL,
54     DAEMON_VERSION,
55     DAEMON_HELP,
56     DAEMON_RELOAD
57 } DaemonCommand;
58
59 typedef struct {
60     AvahiServerConfig server_config;
61     DaemonCommand command;
62     gboolean daemonize;
63     gchar *config_file;
64     gboolean enable_dbus;
65     gboolean drop_root;
66     gboolean publish_resolv_conf;
67     gchar ** publish_dns_servers;
68 } DaemonConfig;
69
70 #define RESOLV_CONF "/etc/resolv.conf"
71
72 static AvahiEntryGroup *dns_servers_entry_group = NULL;
73 static AvahiEntryGroup *resolv_conf_entry_group = NULL;
74
75 static gchar **resolv_conf = NULL;
76
77 static DaemonConfig config;
78
79 #define MAX_NAME_SERVERS 10
80
81 static gint load_resolv_conf(const DaemonConfig *config) {
82     gint ret = -1;
83     FILE *f;
84     gint i = 0;
85     
86     g_strfreev(resolv_conf);
87     resolv_conf = NULL;
88
89     if (!config->publish_resolv_conf)
90         return 0;
91
92     if (!(f = fopen(RESOLV_CONF, "r"))) {
93         avahi_log_warn("Failed to open "RESOLV_CONF".");
94         goto finish;
95     }
96
97     resolv_conf = g_new0(gchar*, MAX_NAME_SERVERS+1);
98
99     while (!feof(f) && i < MAX_NAME_SERVERS) {
100         char ln[128];
101         gchar *p;
102
103         if (!(fgets(ln, sizeof(ln), f)))
104             break;
105
106         ln[strcspn(ln, "\r\n#")] = 0;
107         p = ln + strspn(ln, "\t ");
108
109         if (g_str_has_prefix(p, "nameserver")) {
110             p += 10;
111             p += strspn(p, "\t ");
112             p[strcspn(p, "\t ")] = 0;
113             resolv_conf[i++] = strdup(p);
114         }
115     }
116
117     ret = 0;
118
119 finish:
120
121     if (ret != 0) {
122         g_strfreev(resolv_conf);
123         resolv_conf = NULL;
124     }
125         
126     if (f)
127         fclose(f);
128
129     return ret;
130 }
131
132 static AvahiEntryGroup* add_dns_servers(AvahiServer *s, gchar **l) {
133     gchar **p;
134     AvahiEntryGroup *g;
135
136     g_assert(s);
137     g_assert(l);
138
139     g = avahi_entry_group_new(s, NULL, NULL);
140
141     for (p = l; *p; p++) {
142         AvahiAddress a;
143         
144         if (!avahi_address_parse(*p, AF_UNSPEC, &a))
145             avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
146         else
147             if (avahi_server_add_dns_server_address(s, g, -1, AF_UNSPEC, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
148                 avahi_entry_group_free(g);
149                 return NULL;
150             }
151     }
152
153     avahi_entry_group_commit(g);
154
155     return g;
156     
157 }
158
159 static void remove_dns_server_entry_groups(void) {
160     if (resolv_conf_entry_group) {
161         avahi_entry_group_free(resolv_conf_entry_group);
162         resolv_conf_entry_group = NULL;
163     }
164     
165     if (dns_servers_entry_group) {
166         avahi_entry_group_free(dns_servers_entry_group);
167         dns_servers_entry_group = NULL;
168     }
169 }
170
171 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
172     DaemonConfig *config = userdata;
173     
174     g_assert(s);
175     g_assert(config);
176
177     if (state == AVAHI_SERVER_RUNNING) {
178         avahi_log_info("Server startup complete.  Host name is <%s>", avahi_server_get_host_name_fqdn(s));
179         static_service_add_to_server();
180
181         remove_dns_server_entry_groups();
182
183         if (resolv_conf && resolv_conf[0])
184             resolv_conf_entry_group = add_dns_servers(s, resolv_conf);
185
186         if (config->publish_dns_servers && config->publish_dns_servers[0])
187             dns_servers_entry_group = add_dns_servers(s, config->publish_dns_servers);
188
189         simple_protocol_restart_queries();
190         
191     } else if (state == AVAHI_SERVER_COLLISION) {
192         gchar *n;
193
194         static_service_remove_from_server();
195
196         remove_dns_server_entry_groups();
197
198         n = avahi_alternative_host_name(avahi_server_get_host_name(s));
199         avahi_log_warn("Host name conflict, retrying with <%s>", n);
200         avahi_server_set_host_name(s, n);
201         g_free(n);
202     }
203 }
204
205 static void help(FILE *f, const gchar *argv0) {
206     fprintf(f,
207             "%s [options]\n"
208             "    -h --help        Show this help\n"
209             "    -D --daemonize   Daemonize after startup\n"
210             "    -k --kill        Kill a running daemon\n"
211             "    -r --reload      Request a running daemon to reload static services\n"
212             "    -V --version     Show version\n"
213             "    -f --file=FILE   Load the specified configuration file instead of\n"
214             "                     "AVAHI_CONFIG_FILE"\n",
215             argv0);
216 }
217
218 static gint parse_command_line(DaemonConfig *config, int argc, char *argv[]) {
219     gint c;
220     
221     static const struct option const long_options[] = {
222         { "help",      no_argument,       NULL, 'h' },
223         { "daemonize", no_argument,       NULL, 'D' },
224         { "kill",      no_argument,       NULL, 'k' },
225         { "version",   no_argument,       NULL, 'V' },
226         { "file",      required_argument, NULL, 'f' },
227         { "reload",    no_argument,       NULL, 'r' },
228     };
229
230     g_assert(config);
231
232     opterr = 0;
233     while ((c = getopt_long(argc, argv, "hDkVf:r", long_options, NULL)) >= 0) {
234
235         switch(c) {
236             case 'h':
237                 config->command = DAEMON_HELP;
238                 break;
239             case 'D':
240                 config->daemonize = TRUE;
241                 break;
242             case 'k':
243                 config->command = DAEMON_KILL;
244                 break;
245             case 'V':
246                 config->command = DAEMON_VERSION;
247                 break;
248             case 'f':
249                 g_free(config->config_file);
250                 config->config_file = g_strdup(optarg);
251                 break;
252             case 'r':
253                 config->command = DAEMON_RELOAD;
254                 break;
255             default:
256                 fprintf(stderr, "Invalid command line argument: %c\n", c);
257                 return -1;
258         }
259     }
260
261     if (optind < argc) {
262         fprintf(stderr, "Too many arguments\n");
263         return -1;
264     }
265         
266     return 0;
267 }
268
269 static gboolean is_yes(const gchar *s) {
270     g_assert(s);
271     
272     return *s == 'y' || *s == 'Y';
273 }
274
275 static gint load_config_file(DaemonConfig *config) {
276     int r = -1;
277     GKeyFile *f = NULL;
278     GError *err = NULL;
279     gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
280
281     g_assert(config);
282     
283     f = g_key_file_new();
284     g_key_file_set_list_separator(f, ',');
285     
286     if (!g_key_file_load_from_file(f, config->config_file ? config->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
287         fprintf(stderr, "Unable to read config file: %s\n", err->message);
288         goto finish;
289     }
290
291     groups = g_key_file_get_groups(f, NULL);
292
293     for (g = groups; *g; g++) {
294         if (g_strcasecmp(*g, "server") == 0) {
295             gchar **k;
296             
297             keys = g_key_file_get_keys(f, *g, NULL, NULL);
298
299             for (k = keys; *k; k++) {
300
301                 v = g_key_file_get_value(f, *g, *k, NULL);
302                 
303                 if (g_strcasecmp(*k, "host-name") == 0) {
304                     g_free(config->server_config.host_name);
305                     config->server_config.host_name = v;
306                     v = NULL;
307                 } else if (g_strcasecmp(*k, "domain-name") == 0) {
308                     g_free(config->server_config.domain_name);
309                     config->server_config.domain_name = v;
310                     v = NULL;
311                 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
312                     config->server_config.use_ipv4 = is_yes(v);
313                 else if (g_strcasecmp(*k, "use-ipv6") == 0)
314                     config->server_config.use_ipv6 = is_yes(v);
315                 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
316                     config->server_config.check_response_ttl = is_yes(v);
317                 else if (g_strcasecmp(*k, "use-iff-running") == 0)
318                     config->server_config.use_iff_running = is_yes(v);
319                 else if (g_strcasecmp(*k, "enable-dbus") == 0)
320                     config->enable_dbus = is_yes(v);
321                 else if (g_strcasecmp(*k, "drop-root") == 0)
322                     config->drop_root = is_yes(v);
323                 else {
324                     fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
325                     goto finish;
326                 }
327
328                 g_free(v);
329                 v = NULL;
330             }
331         
332             g_strfreev(keys);
333             keys = NULL;
334             
335         } else if (g_strcasecmp(*g, "publish") == 0) {
336             gchar **k;
337             
338             keys = g_key_file_get_keys(f, *g, NULL, NULL);
339
340             for (k = keys; *k; k++) {
341
342                 v = g_key_file_get_string(f, *g, *k, NULL);
343                 
344                 if (g_strcasecmp(*k, "publish-addresses") == 0)
345                     config->server_config.publish_addresses = is_yes(v);
346                 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
347                     config->server_config.publish_hinfo = is_yes(v);
348                 else if (g_strcasecmp(*k, "publish-workstation") == 0)
349                     config->server_config.publish_workstation = is_yes(v);
350                 else if (g_strcasecmp(*k, "publish-domain") == 0)
351                     config->server_config.publish_domain = is_yes(v);
352                 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
353                     config->publish_resolv_conf = is_yes(v);
354                 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
355                     g_strfreev(config->publish_dns_servers);
356                     config->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
357                 } else {
358                     fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
359                     goto finish;
360                 }
361
362                 g_free(v);
363                 v = NULL;
364             }
365
366             g_strfreev(keys);
367             keys = NULL;
368
369         } else if (g_strcasecmp(*g, "reflector") == 0) {
370             gchar **k;
371             
372             keys = g_key_file_get_keys(f, *g, NULL, NULL);
373
374             for (k = keys; *k; k++) {
375
376                 v = g_key_file_get_string(f, *g, *k, NULL);
377                 
378                 if (g_strcasecmp(*k, "enable-reflector") == 0)
379                     config->server_config.enable_reflector = is_yes(v);
380                 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
381                     config->server_config.reflect_ipv = is_yes(v);
382                 else {
383                     fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
384                     goto finish;
385                 }
386
387                 g_free(v);
388                 v = NULL;
389             }
390     
391             g_strfreev(keys);
392             keys = NULL;
393             
394         } else {
395             fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
396             goto finish;
397         }
398     }
399
400     r = 0;
401
402 finish:
403
404     g_strfreev(groups);
405     g_strfreev(keys);
406     g_free(v);
407
408     if (err)
409         g_error_free (err);
410
411     if (f)
412         g_key_file_free(f);
413     
414     return r;
415 }
416
417 static void log_function(AvahiLogLevel level, const gchar *txt) {
418
419     static const int const log_level_map[] = {
420         LOG_ERR,
421         LOG_WARNING,
422         LOG_NOTICE,
423         LOG_INFO,
424         LOG_DEBUG
425     };
426     
427     g_assert(level < AVAHI_LOG_LEVEL_MAX);
428     g_assert(txt);
429
430     daemon_log(log_level_map[level], "%s", txt);
431 }
432
433 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
434     gint sig;
435     GMainLoop *loop = data;
436     
437     g_assert(source);
438     g_assert(loop);
439
440     if ((sig = daemon_signal_next()) <= 0) {
441         avahi_log_error("daemon_signal_next() failed");
442         return FALSE;
443     }
444
445     switch (sig) {
446         case SIGINT:
447         case SIGQUIT:
448         case SIGTERM:
449             avahi_log_info(
450                 "Got %s, quitting.",
451                 sig == SIGINT ? "SIGINT" :
452                 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
453             g_main_loop_quit(loop);
454             break;
455
456         case SIGHUP:
457             avahi_log_info("Got SIGHUP, reloading.");
458             static_service_load();
459             static_service_add_to_server();
460
461             if (resolv_conf_entry_group) {
462                 avahi_entry_group_free(resolv_conf_entry_group);
463                 resolv_conf_entry_group = NULL;
464             }
465
466             load_resolv_conf(&config);
467             
468             if (resolv_conf && resolv_conf[0])
469                 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
470
471             break;
472
473         default:
474             avahi_log_warn("Got spurious signal, ignoring.");
475             break;
476     }
477
478     return TRUE;
479 }
480
481 static gint run_server(DaemonConfig *config) {
482     GMainLoop *loop = NULL;
483     gint r = -1;
484     GIOChannel *io = NULL;
485     guint watch_id = (guint) -1;
486
487     g_assert(config);
488     
489     loop = g_main_loop_new(NULL, FALSE);
490
491     if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, 0) < 0) {
492         avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
493         goto finish;
494     }
495
496     if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
497         avahi_log_error( "Failed to create signal io channel.");
498         goto finish;
499     }
500
501     g_io_channel_set_close_on_unref(io, FALSE);
502     g_io_add_watch(io, G_IO_IN, signal_callback, loop);
503     
504     if (simple_protocol_setup(NULL) < 0)
505         goto finish;
506     
507 #ifdef ENABLE_DBUS
508     if (config->enable_dbus)
509         if (dbus_protocol_setup(loop) < 0)
510             goto finish;
511 #endif
512     
513     if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
514         goto finish;
515
516     load_resolv_conf(config);
517     
518     static_service_load();
519
520     if (config->daemonize) {
521         daemon_retval_send(0);
522         r = 0;
523     }
524
525     g_main_loop_run(loop);
526
527 finish:
528     
529     static_service_remove_from_server();
530     static_service_free_all();
531     remove_dns_server_entry_groups();
532     
533     simple_protocol_shutdown();
534
535 #ifdef ENABLE_DBUS
536     if (config->enable_dbus)
537         dbus_protocol_shutdown();
538 #endif
539
540
541     if (avahi_server)
542         avahi_server_free(avahi_server);
543
544     daemon_signal_done();
545
546     if (watch_id != (guint) -1)
547         g_source_remove(watch_id);
548     
549     if (io)
550         g_io_channel_unref(io);
551
552         
553     if (loop)
554         g_main_loop_unref(loop);
555
556     if (r != 0 && config->daemonize)
557         daemon_retval_send(1);
558     
559     return r;
560 }
561
562 static gint drop_root(void) {
563     struct passwd *pw;
564     struct group * gr;
565     gint r;
566     
567     if (!(pw = getpwnam(AVAHI_USER))) {
568         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
569         return -1;
570     }
571
572     if (!(gr = getgrnam(AVAHI_GROUP))) {
573         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
574         return -1;
575     }
576
577     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);
578
579     if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
580         avahi_log_error("Failed to change group list: %s", strerror(errno));
581         return -1;
582     }
583
584 #if defined(HAVE_SETRESGID)
585     r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
586 #elif defined(HAVE_SETREGID)
587     r = setregid(gr->gr_gid, gr->gr_gid);
588 #else
589     if ((r = setgid(gr->gr_gid)) >= 0)
590         r = setegid(gr->gr_gid);
591 #endif
592
593     if (r < 0) {
594         avahi_log_error("Failed to change GID: %s", strerror(errno));
595         return -1;
596     }
597
598 #if defined(HAVE_SETRESUID)
599     r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
600 #elif defined(HAVE_SETREUID)
601     r = setreuid(pw->pw_uid, pw->pw_uid);
602 #else
603     if ((r = setuid(pw->pw_uid)) >= 0)
604         r = seteuid(pw->pw_uid);
605 #endif
606
607     if (r < 0) {
608         avahi_log_error("Failed to change UID: %s", strerror(errno));
609         return -1;
610     }
611
612     g_setenv("USER", pw->pw_name, 1);
613     g_setenv("LOGNAME", pw->pw_name, 1);
614     g_setenv("HOME", pw->pw_dir, 1);
615     
616     avahi_log_info("Successfully dropped root privileges.");
617
618     return 0;
619 }
620
621 static const char* pid_file_proc(void) {
622     return AVAHI_RUNTIME_DIR"/avahi-daemon.pid";
623 }
624
625 static gint make_runtime_dir(void) {
626     gint r = -1;
627     mode_t u;
628     gboolean reset_umask = FALSE;
629     struct passwd *pw;
630     struct group * gr;
631     struct stat st;
632
633     if (!(pw = getpwnam(AVAHI_USER))) {
634         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
635         goto fail;
636     }
637
638     if (!(gr = getgrnam(AVAHI_GROUP))) {
639         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
640         goto fail;
641     }
642
643     u = umask(0000);
644     reset_umask = TRUE;
645     
646     if (mkdir(AVAHI_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
647         avahi_log_error("mkdir(\""AVAHI_RUNTIME_DIR"\"): %s", strerror(errno));
648         goto fail;
649     }
650     
651     chown(AVAHI_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
652
653     if (stat(AVAHI_RUNTIME_DIR, &st) < 0) {
654         avahi_log_error("stat(): %s\n", strerror(errno));
655         goto fail;
656     }
657
658     if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
659         avahi_log_error("Failed to create runtime directory "AVAHI_RUNTIME_DIR".");
660         goto fail;
661     }
662
663     r = 0;
664
665 fail:
666     if (reset_umask)
667         umask(u);
668     return r;
669
670 }
671
672 int main(int argc, char *argv[]) {
673     gint r = 255;
674     const gchar *argv0;
675     gboolean wrote_pid_file = FALSE;
676
677     avahi_set_log_function(log_function);
678     
679     avahi_server_config_init(&config.server_config);
680     config.command = DAEMON_RUN;
681     config.daemonize = FALSE;
682     config.config_file = NULL;
683     config.enable_dbus = TRUE;
684     config.drop_root = TRUE;
685     config.publish_dns_servers = NULL;
686     config.publish_resolv_conf = FALSE;
687
688     if ((argv0 = strrchr(argv[0], '/')))
689         argv0++;
690     else
691         argv0 = argv[0];
692
693     daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
694     daemon_pid_file_proc = pid_file_proc;
695     
696     if (parse_command_line(&config, argc, argv) < 0)
697         goto finish;
698
699     if (config.command == DAEMON_HELP) {
700         help(stdout, argv0);
701         r = 0;
702     } else if (config.command == DAEMON_VERSION) {
703         printf("%s "PACKAGE_VERSION"\n", argv0);
704         r = 0;
705     } else if (config.command == DAEMON_KILL) {
706         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
707             avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
708             goto finish;
709         }
710
711         r = 0;
712         
713     } else if (config.command == DAEMON_RELOAD) {
714         if (daemon_pid_file_kill(SIGHUP) < 0) {
715             avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
716             goto finish;
717         }
718
719         r = 0;
720         
721     } else if (config.command == DAEMON_RUN) {
722         pid_t pid;
723
724         if (getuid() != 0) {
725             avahi_log_error("This program is intended to be run as root.");
726             goto finish;
727         }
728         
729         if ((pid = daemon_pid_file_is_running()) >= 0) {
730             avahi_log_error("Daemon already running on PID %u", pid);
731             goto finish;
732         }
733
734         if (load_config_file(&config) < 0)
735             goto finish;
736         
737         if (config.daemonize) {
738             daemon_retval_init();
739             
740             if ((pid = daemon_fork()) < 0)
741                 goto finish;
742             else if (pid != 0) {
743                 int ret;
744                 /** Parent **/
745
746                 if ((ret = daemon_retval_wait(20)) < 0) {
747                     avahi_log_error("Could not recieve return value from daemon process.");
748                     goto finish;
749                 }
750
751                 r = ret;
752                 goto finish;
753             }
754
755             /* Child */
756         }
757
758         chdir("/");
759
760         if (make_runtime_dir() < 0)
761             goto finish;
762
763         if (config.drop_root) {
764             if (drop_root() < 0)
765                 goto finish;
766         }
767
768         if (daemon_pid_file_create() < 0) {
769             avahi_log_error("Failed to create PID file: %s", strerror(errno));
770
771             if (config.daemonize)
772                 daemon_retval_send(1);
773             goto finish;
774         } else
775             wrote_pid_file = TRUE;
776
777         if (run_server(&config) == 0)
778             r = 0;
779     }
780         
781 finish:
782
783     if (config.daemonize)
784         daemon_retval_done();
785
786     avahi_server_config_free(&config.server_config);
787     g_free(config.config_file);
788     g_strfreev(config.publish_dns_servers);
789     g_strfreev(resolv_conf);
790
791     if (wrote_pid_file)
792         daemon_pid_file_remove();
793     
794     return r;
795 }