]> git.meshlink.io Git - catta/blob - avahi-daemon/main.c
* add new tool avahi-publish-address
[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 void dump(const gchar *text, gpointer userdata) {
447     avahi_log_info("%s", text);
448 }
449
450 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
451     gint sig;
452     GMainLoop *loop = data;
453     
454     g_assert(source);
455     g_assert(loop);
456
457     if ((sig = daemon_signal_next()) <= 0) {
458         avahi_log_error("daemon_signal_next() failed");
459         return FALSE;
460     }
461
462     switch (sig) {
463         case SIGINT:
464         case SIGQUIT:
465         case SIGTERM:
466             avahi_log_info(
467                 "Got %s, quitting.",
468                 sig == SIGINT ? "SIGINT" :
469                 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
470             g_main_loop_quit(loop);
471             break;
472
473         case SIGHUP:
474             avahi_log_info("Got SIGHUP, reloading.");
475             static_service_load();
476             static_service_add_to_server();
477
478             if (resolv_conf_entry_group) {
479                 avahi_entry_group_free(resolv_conf_entry_group);
480                 resolv_conf_entry_group = NULL;
481             }
482
483             load_resolv_conf(&config);
484             
485             if (resolv_conf && resolv_conf[0])
486                 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf);
487
488             break;
489
490         case SIGUSR1:
491             avahi_log_info("Got SIGUSR1, dumping record data.");
492             avahi_server_dump(avahi_server, dump, NULL);
493             break;
494
495         default:
496             avahi_log_warn("Got spurious signal, ignoring.");
497             break;
498     }
499
500     return TRUE;
501 }
502
503 static gint run_server(DaemonConfig *config) {
504     GMainLoop *loop = NULL;
505     gint r = -1;
506     GIOChannel *io = NULL;
507     guint watch_id = (guint) -1;
508
509     g_assert(config);
510     
511     loop = g_main_loop_new(NULL, FALSE);
512
513     if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
514         avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
515         goto finish;
516     }
517
518     if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
519         avahi_log_error( "Failed to create signal io channel.");
520         goto finish;
521     }
522
523     g_io_channel_set_close_on_unref(io, FALSE);
524     g_io_add_watch(io, G_IO_IN, signal_callback, loop);
525     
526     if (simple_protocol_setup(NULL) < 0)
527         goto finish;
528     
529 #ifdef ENABLE_DBUS
530     if (config->enable_dbus)
531         if (dbus_protocol_setup(loop) < 0)
532             goto finish;
533 #endif
534     
535     if (!(avahi_server = avahi_server_new(NULL, &config->server_config, server_callback, config)))
536         goto finish;
537
538     load_resolv_conf(config);
539     
540     static_service_load();
541
542     if (config->daemonize) {
543         daemon_retval_send(0);
544         r = 0;
545     }
546
547     g_main_loop_run(loop);
548
549 finish:
550     
551     static_service_remove_from_server();
552     static_service_free_all();
553     remove_dns_server_entry_groups();
554     
555     simple_protocol_shutdown();
556
557 #ifdef ENABLE_DBUS
558     if (config->enable_dbus)
559         dbus_protocol_shutdown();
560 #endif
561
562
563     if (avahi_server)
564         avahi_server_free(avahi_server);
565
566     daemon_signal_done();
567
568     if (watch_id != (guint) -1)
569         g_source_remove(watch_id);
570     
571     if (io)
572         g_io_channel_unref(io);
573
574         
575     if (loop)
576         g_main_loop_unref(loop);
577
578     if (r != 0 && config->daemonize)
579         daemon_retval_send(1);
580     
581     return r;
582 }
583
584 static gint drop_root(void) {
585     struct passwd *pw;
586     struct group * gr;
587     gint r;
588     
589     if (!(pw = getpwnam(AVAHI_USER))) {
590         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
591         return -1;
592     }
593
594     if (!(gr = getgrnam(AVAHI_GROUP))) {
595         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
596         return -1;
597     }
598
599     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);
600
601     if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
602         avahi_log_error("Failed to change group list: %s", strerror(errno));
603         return -1;
604     }
605
606 #if defined(HAVE_SETRESGID)
607     r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
608 #elif defined(HAVE_SETREGID)
609     r = setregid(gr->gr_gid, gr->gr_gid);
610 #else
611     if ((r = setgid(gr->gr_gid)) >= 0)
612         r = setegid(gr->gr_gid);
613 #endif
614
615     if (r < 0) {
616         avahi_log_error("Failed to change GID: %s", strerror(errno));
617         return -1;
618     }
619
620 #if defined(HAVE_SETRESUID)
621     r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
622 #elif defined(HAVE_SETREUID)
623     r = setreuid(pw->pw_uid, pw->pw_uid);
624 #else
625     if ((r = setuid(pw->pw_uid)) >= 0)
626         r = seteuid(pw->pw_uid);
627 #endif
628
629     if (r < 0) {
630         avahi_log_error("Failed to change UID: %s", strerror(errno));
631         return -1;
632     }
633
634     g_setenv("USER", pw->pw_name, 1);
635     g_setenv("LOGNAME", pw->pw_name, 1);
636     g_setenv("HOME", pw->pw_dir, 1);
637     
638     avahi_log_info("Successfully dropped root privileges.");
639
640     return 0;
641 }
642
643 static const char* pid_file_proc(void) {
644     return AVAHI_DAEMON_RUNTIME_DIR"/pid";
645 }
646
647 static gint make_runtime_dir(void) {
648     gint r = -1;
649     mode_t u;
650     gboolean reset_umask = FALSE;
651     struct passwd *pw;
652     struct group * gr;
653     struct stat st;
654
655     if (!(pw = getpwnam(AVAHI_USER))) {
656         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
657         goto fail;
658     }
659
660     if (!(gr = getgrnam(AVAHI_GROUP))) {
661         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
662         goto fail;
663     }
664
665     u = umask(0000);
666     reset_umask = TRUE;
667     
668     if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
669         avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
670         goto fail;
671     }
672     
673     chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
674
675     if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
676         avahi_log_error("stat(): %s\n", strerror(errno));
677         goto fail;
678     }
679
680     if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
681         avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
682         goto fail;
683     }
684
685     r = 0;
686
687 fail:
688     if (reset_umask)
689         umask(u);
690     return r;
691
692 }
693
694 int main(int argc, char *argv[]) {
695     gint r = 255;
696     const gchar *argv0;
697     gboolean wrote_pid_file = FALSE;
698
699     avahi_set_log_function(log_function);
700     
701     avahi_server_config_init(&config.server_config);
702     config.command = DAEMON_RUN;
703     config.daemonize = FALSE;
704     config.config_file = NULL;
705     config.enable_dbus = TRUE;
706     config.drop_root = TRUE;
707     config.publish_dns_servers = NULL;
708     config.publish_resolv_conf = FALSE;
709
710     if ((argv0 = strrchr(argv[0], '/')))
711         argv0++;
712     else
713         argv0 = argv[0];
714
715     daemon_pid_file_ident = daemon_log_ident = (char *) argv0;
716     daemon_pid_file_proc = pid_file_proc;
717     
718     if (parse_command_line(&config, argc, argv) < 0)
719         goto finish;
720
721     if (config.command == DAEMON_HELP) {
722         help(stdout, argv0);
723         r = 0;
724     } else if (config.command == DAEMON_VERSION) {
725         printf("%s "PACKAGE_VERSION"\n", argv0);
726         r = 0;
727     } else if (config.command == DAEMON_KILL) {
728         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
729             avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
730             goto finish;
731         }
732
733         r = 0;
734
735     } else if (config.command == DAEMON_RELOAD) {
736         if (daemon_pid_file_kill(SIGHUP) < 0) {
737             avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
738             goto finish;
739         }
740
741         r = 0;
742         
743     } else if (config.command == DAEMON_CHECK)
744         r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
745     else if (config.command == DAEMON_RUN) {
746         pid_t pid;
747
748         if (getuid() != 0) {
749             avahi_log_error("This program is intended to be run as root.");
750             goto finish;
751         }
752         
753         if ((pid = daemon_pid_file_is_running()) >= 0) {
754             avahi_log_error("Daemon already running on PID %u", pid);
755             goto finish;
756         }
757
758         if (load_config_file(&config) < 0)
759             goto finish;
760         
761         if (config.daemonize) {
762             daemon_retval_init();
763             
764             if ((pid = daemon_fork()) < 0)
765                 goto finish;
766             else if (pid != 0) {
767                 int ret;
768                 /** Parent **/
769
770                 if ((ret = daemon_retval_wait(20)) < 0) {
771                     avahi_log_error("Could not recieve return value from daemon process.");
772                     goto finish;
773                 }
774
775                 r = ret;
776                 goto finish;
777             }
778
779             /* Child */
780         }
781
782         chdir("/");
783
784         if (make_runtime_dir() < 0)
785             goto finish;
786
787         if (config.drop_root) {
788             if (drop_root() < 0)
789                 goto finish;
790         }
791
792         if (daemon_pid_file_create() < 0) {
793             avahi_log_error("Failed to create PID file: %s", strerror(errno));
794
795             if (config.daemonize)
796                 daemon_retval_send(1);
797             goto finish;
798         } else
799             wrote_pid_file = TRUE;
800
801         if (run_server(&config) == 0)
802             r = 0;
803     }
804         
805 finish:
806
807     if (config.daemonize)
808         daemon_retval_done();
809
810     avahi_server_config_free(&config.server_config);
811     g_free(config.config_file);
812     g_strfreev(config.publish_dns_servers);
813     g_strfreev(resolv_conf);
814
815     if (wrote_pid_file)
816         daemon_pid_file_remove();
817     
818     return r;
819 }