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