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