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