* add proper error codes and patch everything to make use of it
[platform/upstream/avahi.git] / 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                 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
154                 return NULL;
155             }
156     }
157
158     avahi_entry_group_commit(g);
159
160     return g;
161 }
162
163 static void remove_dns_server_entry_groups(void) {
164
165     if (resolv_conf_entry_group)
166         avahi_entry_group_reset(resolv_conf_entry_group);
167     
168     if (dns_servers_entry_group) 
169         avahi_entry_group_reset(dns_servers_entry_group);
170 }
171
172 static void server_callback(AvahiServer *s, AvahiServerState state, gpointer userdata) {
173     DaemonConfig *c = userdata;
174     
175     g_assert(s);
176     g_assert(c);
177
178 #ifdef ENABLE_DBUS
179     if (c->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_entry_group, resolv_conf);
191
192         if (c->publish_dns_servers && c->publish_dns_servers[0])
193             dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->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 static void help(FILE *f, const gchar *argv0) {
212     fprintf(f,
213             "%s [options]\n"
214             "    -h --help        Show this help\n"
215             "    -D --daemonize   Daemonize after startup\n"
216             "    -k --kill        Kill a running daemon\n"
217             "    -r --reload      Request a running daemon to reload static services\n"
218             "    -c --check       Return 0 if a daemon is already running\n"
219             "    -V --version     Show version\n"
220             "    -f --file=FILE   Load the specified configuration file instead of\n"
221             "                     "AVAHI_CONFIG_FILE"\n",
222             argv0);
223 }
224
225 static gint parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
226     gint o;
227     
228     static const struct option const long_options[] = {
229         { "help",      no_argument,       NULL, 'h' },
230         { "daemonize", no_argument,       NULL, 'D' },
231         { "kill",      no_argument,       NULL, 'k' },
232         { "version",   no_argument,       NULL, 'V' },
233         { "file",      required_argument, NULL, 'f' },
234         { "reload",    no_argument,       NULL, 'r' },
235         { "check",     no_argument,       NULL, 'c' },
236     };
237
238     g_assert(c);
239
240     opterr = 0;
241     while ((o = getopt_long(argc, argv, "hDkVf:rc", long_options, NULL)) >= 0) {
242
243         switch(o) {
244             case 'h':
245                 c->command = DAEMON_HELP;
246                 break;
247             case 'D':
248                 c->daemonize = TRUE;
249                 break;
250             case 'k':
251                 c->command = DAEMON_KILL;
252                 break;
253             case 'V':
254                 c->command = DAEMON_VERSION;
255                 break;
256             case 'f':
257                 g_free(c->config_file);
258                 c->config_file = g_strdup(optarg);
259                 break;
260             case 'r':
261                 c->command = DAEMON_RELOAD;
262                 break;
263             case 'c':
264                 c->command = DAEMON_CHECK;
265                 break;
266             default:
267                 fprintf(stderr, "Invalid command line argument: %c\n", o);
268                 return -1;
269         }
270     }
271
272     if (optind < argc) {
273         fprintf(stderr, "Too many arguments\n");
274         return -1;
275     }
276         
277     return 0;
278 }
279
280 static gboolean is_yes(const gchar *s) {
281     g_assert(s);
282     
283     return *s == 'y' || *s == 'Y';
284 }
285
286 static gint load_config_file(DaemonConfig *c) {
287     int r = -1;
288     GKeyFile *f = NULL;
289     GError *err = NULL;
290     gchar **groups = NULL, **g, **keys = NULL, *v = NULL;
291
292     g_assert(c);
293     
294     f = g_key_file_new();
295     g_key_file_set_list_separator(f, ',');
296     
297     if (!g_key_file_load_from_file(f, c->config_file ? c->config_file : AVAHI_CONFIG_FILE, G_KEY_FILE_NONE, &err)) {
298         fprintf(stderr, "Unable to read config file: %s\n", err->message);
299         goto finish;
300     }
301
302     groups = g_key_file_get_groups(f, NULL);
303
304     for (g = groups; *g; g++) {
305         if (g_strcasecmp(*g, "server") == 0) {
306             gchar **k;
307             
308             keys = g_key_file_get_keys(f, *g, NULL, NULL);
309
310             for (k = keys; *k; k++) {
311
312                 v = g_key_file_get_value(f, *g, *k, NULL);
313                 
314                 if (g_strcasecmp(*k, "host-name") == 0) {
315                     g_free(c->server_config.host_name);
316                     c->server_config.host_name = v;
317                     v = NULL;
318                 } else if (g_strcasecmp(*k, "domain-name") == 0) {
319                     g_free(c->server_config.domain_name);
320                     c->server_config.domain_name = v;
321                     v = NULL;
322                 } else if (g_strcasecmp(*k, "use-ipv4") == 0)
323                     c->server_config.use_ipv4 = is_yes(v);
324                 else if (g_strcasecmp(*k, "use-ipv6") == 0)
325                     c->server_config.use_ipv6 = is_yes(v);
326                 else if (g_strcasecmp(*k, "check-response-ttl") == 0)
327                     c->server_config.check_response_ttl = is_yes(v);
328                 else if (g_strcasecmp(*k, "use-iff-running") == 0)
329                     c->server_config.use_iff_running = is_yes(v);
330                 else if (g_strcasecmp(*k, "enable-dbus") == 0)
331                     c->enable_dbus = is_yes(v);
332                 else if (g_strcasecmp(*k, "drop-root") == 0)
333                     c->drop_root = is_yes(v);
334                 else {
335                     fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
336                     goto finish;
337                 }
338
339                 g_free(v);
340                 v = NULL;
341             }
342         
343             g_strfreev(keys);
344             keys = NULL;
345             
346         } else if (g_strcasecmp(*g, "publish") == 0) {
347             gchar **k;
348             
349             keys = g_key_file_get_keys(f, *g, NULL, NULL);
350
351             for (k = keys; *k; k++) {
352
353                 v = g_key_file_get_string(f, *g, *k, NULL);
354                 
355                 if (g_strcasecmp(*k, "publish-addresses") == 0)
356                     c->server_config.publish_addresses = is_yes(v);
357                 else if (g_strcasecmp(*k, "publish-hinfo") == 0)
358                     c->server_config.publish_hinfo = is_yes(v);
359                 else if (g_strcasecmp(*k, "publish-workstation") == 0)
360                     c->server_config.publish_workstation = is_yes(v);
361                 else if (g_strcasecmp(*k, "publish-domain") == 0)
362                     c->server_config.publish_domain = is_yes(v);
363                 else if (g_strcasecmp(*k, "publish-resolv-conf-dns-servers") == 0)
364                     c->publish_resolv_conf = is_yes(v);
365                 else if (g_strcasecmp(*k, "publish-dns-servers") == 0) {
366                     g_strfreev(c->publish_dns_servers);
367                     c->publish_dns_servers = g_key_file_get_string_list(f, *g, *k, NULL, NULL);
368                 } else {
369                     fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
370                     goto finish;
371                 }
372
373                 g_free(v);
374                 v = NULL;
375             }
376
377             g_strfreev(keys);
378             keys = NULL;
379
380         } else if (g_strcasecmp(*g, "reflector") == 0) {
381             gchar **k;
382             
383             keys = g_key_file_get_keys(f, *g, NULL, NULL);
384
385             for (k = keys; *k; k++) {
386
387                 v = g_key_file_get_string(f, *g, *k, NULL);
388                 
389                 if (g_strcasecmp(*k, "enable-reflector") == 0)
390                     c->server_config.enable_reflector = is_yes(v);
391                 else if (g_strcasecmp(*k, "reflect-ipv") == 0)
392                     c->server_config.reflect_ipv = is_yes(v);
393                 else {
394                     fprintf(stderr, "Invalid configuration key \"%s\" in group \"%s\"\n", *k, *g);
395                     goto finish;
396                 }
397
398                 g_free(v);
399                 v = NULL;
400             }
401     
402             g_strfreev(keys);
403             keys = NULL;
404             
405         } else {
406             fprintf(stderr, "Invalid configuration file group \"%s\".\n", *g);
407             goto finish;
408         }
409     }
410
411     r = 0;
412
413 finish:
414
415     g_strfreev(groups);
416     g_strfreev(keys);
417     g_free(v);
418
419     if (err)
420         g_error_free (err);
421
422     if (f)
423         g_key_file_free(f);
424     
425     return r;
426 }
427
428 static void log_function(AvahiLogLevel level, const gchar *txt) {
429
430     static const int const log_level_map[] = {
431         LOG_ERR,
432         LOG_WARNING,
433         LOG_NOTICE,
434         LOG_INFO,
435         LOG_DEBUG
436     };
437     
438     g_assert(level < AVAHI_LOG_LEVEL_MAX);
439     g_assert(txt);
440
441     daemon_log(log_level_map[level], "%s", txt);
442 }
443
444 static void dump(const gchar *text, gpointer userdata) {
445     avahi_log_info("%s", text);
446 }
447
448 static gboolean signal_callback(GIOChannel *source, GIOCondition condition, gpointer data) {
449     gint sig;
450     GMainLoop *loop = data;
451     
452     g_assert(source);
453     g_assert(loop);
454
455     if ((sig = daemon_signal_next()) <= 0) {
456         avahi_log_error("daemon_signal_next() failed");
457         return FALSE;
458     }
459
460     switch (sig) {
461         case SIGINT:
462         case SIGQUIT:
463         case SIGTERM:
464             avahi_log_info(
465                 "Got %s, quitting.",
466                 sig == SIGINT ? "SIGINT" :
467                 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
468             g_main_loop_quit(loop);
469             break;
470
471         case SIGHUP:
472             avahi_log_info("Got SIGHUP, reloading.");
473             static_service_load();
474             static_service_add_to_server();
475
476             if (resolv_conf_entry_group)
477                 avahi_entry_group_reset(resolv_conf_entry_group);
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_entry_group, resolv_conf);
483
484             break;
485
486         case SIGUSR1:
487             avahi_log_info("Got SIGUSR1, dumping record data.");
488             avahi_server_dump(avahi_server, dump, NULL);
489             break;
490
491         default:
492             avahi_log_warn("Got spurious signal, ignoring.");
493             break;
494     }
495
496     return TRUE;
497 }
498
499 static gint run_server(DaemonConfig *c) {
500     GMainLoop *loop = NULL;
501     gint r = -1;
502     GIOChannel *io = NULL;
503     guint watch_id = (guint) -1;
504     gint error;
505
506     g_assert(c);
507     
508     loop = g_main_loop_new(NULL, FALSE);
509
510     if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
511         avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
512         goto finish;
513     }
514
515     if (!(io = g_io_channel_unix_new(daemon_signal_fd()))) {
516         avahi_log_error( "Failed to create signal io channel.");
517         goto finish;
518     }
519
520     g_io_channel_set_close_on_unref(io, FALSE);
521     g_io_add_watch(io, G_IO_IN, signal_callback, loop);
522     
523     if (simple_protocol_setup(NULL) < 0)
524         goto finish;
525     
526 #ifdef ENABLE_DBUS
527     if (c->enable_dbus)
528         if (dbus_protocol_setup(loop) < 0)
529             goto finish;
530 #endif
531     
532     if (!(avahi_server = avahi_server_new(NULL, &c->server_config, server_callback, c, &error))) {
533         avahi_log_error("Failed to create server: %s", avahi_strerror(error));
534         goto finish;
535     }
536
537     load_resolv_conf(c);
538     
539     static_service_load();
540
541     if (c->daemonize) {
542         daemon_retval_send(0);
543         r = 0;
544     }
545
546     g_main_loop_run(loop);
547
548 finish:
549     
550     static_service_remove_from_server();
551     static_service_free_all();
552     remove_dns_server_entry_groups();
553     
554     simple_protocol_shutdown();
555
556 #ifdef ENABLE_DBUS
557     if (c->enable_dbus)
558         dbus_protocol_shutdown();
559 #endif
560
561
562     if (avahi_server)
563         avahi_server_free(avahi_server);
564
565     daemon_signal_done();
566
567     if (watch_id != (guint) -1)
568         g_source_remove(watch_id);
569     
570     if (io)
571         g_io_channel_unref(io);
572
573         
574     if (loop)
575         g_main_loop_unref(loop);
576
577     if (r != 0 && c->daemonize)
578         daemon_retval_send(1);
579     
580     return r;
581 }
582
583 static gint drop_root(void) {
584     struct passwd *pw;
585     struct group * gr;
586     gint r;
587     
588     if (!(pw = getpwnam(AVAHI_USER))) {
589         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
590         return -1;
591     }
592
593     if (!(gr = getgrnam(AVAHI_GROUP))) {
594         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
595         return -1;
596     }
597
598     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);
599
600     if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
601         avahi_log_error("Failed to change group list: %s", strerror(errno));
602         return -1;
603     }
604
605 #if defined(HAVE_SETRESGID)
606     r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
607 #elif defined(HAVE_SETREGID)
608     r = setregid(gr->gr_gid, gr->gr_gid);
609 #else
610     if ((r = setgid(gr->gr_gid)) >= 0)
611         r = setegid(gr->gr_gid);
612 #endif
613
614     if (r < 0) {
615         avahi_log_error("Failed to change GID: %s", strerror(errno));
616         return -1;
617     }
618
619 #if defined(HAVE_SETRESUID)
620     r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
621 #elif defined(HAVE_SETREUID)
622     r = setreuid(pw->pw_uid, pw->pw_uid);
623 #else
624     if ((r = setuid(pw->pw_uid)) >= 0)
625         r = seteuid(pw->pw_uid);
626 #endif
627
628     if (r < 0) {
629         avahi_log_error("Failed to change UID: %s", strerror(errno));
630         return -1;
631     }
632
633     g_setenv("USER", pw->pw_name, 1);
634     g_setenv("LOGNAME", pw->pw_name, 1);
635     g_setenv("HOME", pw->pw_dir, 1);
636     
637     avahi_log_info("Successfully dropped root privileges.");
638
639     return 0;
640 }
641
642 static const char* pid_file_proc(void) {
643     return AVAHI_DAEMON_RUNTIME_DIR"/pid";
644 }
645
646 static gint make_runtime_dir(void) {
647     gint r = -1;
648     mode_t u;
649     gboolean reset_umask = FALSE;
650     struct passwd *pw;
651     struct group * gr;
652     struct stat st;
653
654     if (!(pw = getpwnam(AVAHI_USER))) {
655         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
656         goto fail;
657     }
658
659     if (!(gr = getgrnam(AVAHI_GROUP))) {
660         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
661         goto fail;
662     }
663
664     u = umask(0000);
665     reset_umask = TRUE;
666     
667     if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
668         avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
669         goto fail;
670     }
671     
672     chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
673
674     if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
675         avahi_log_error("stat(): %s\n", strerror(errno));
676         goto fail;
677     }
678
679     if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
680         avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
681         goto fail;
682     }
683
684     r = 0;
685
686 fail:
687     if (reset_umask)
688         umask(u);
689     return r;
690
691 }
692
693 int main(int argc, char *argv[]) {
694     gint r = 255;
695     const gchar *argv0;
696     gboolean wrote_pid_file = FALSE;
697
698     avahi_set_log_function(log_function);
699     
700     avahi_server_config_init(&config.server_config);
701     config.command = DAEMON_RUN;
702     config.daemonize = FALSE;
703     config.config_file = NULL;
704     config.enable_dbus = TRUE;
705     config.drop_root = TRUE;
706     config.publish_dns_servers = NULL;
707     config.publish_resolv_conf = FALSE;
708
709     if ((argv0 = strrchr(argv[0], '/')))
710         argv0++;
711     else
712         argv0 = argv[0];
713
714     daemon_pid_file_ident = (const char *) argv0;
715     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
783         printf("%s "PACKAGE_VERSION" starting up.\n", argv0);
784         
785         chdir("/");
786
787         if (make_runtime_dir() < 0)
788             goto finish;
789
790         if (config.drop_root) {
791             if (drop_root() < 0)
792                 goto finish;
793         }
794
795         if (daemon_pid_file_create() < 0) {
796             avahi_log_error("Failed to create PID file: %s", strerror(errno));
797
798             if (config.daemonize)
799                 daemon_retval_send(1);
800             goto finish;
801         } else
802             wrote_pid_file = TRUE;
803
804         if (run_server(&config) == 0)
805             r = 0;
806     }
807         
808 finish:
809
810     if (config.daemonize)
811         daemon_retval_done();
812
813     avahi_server_config_free(&config.server_config);
814     g_free(config.config_file);
815     g_strfreev(config.publish_dns_servers);
816     g_strfreev(resolv_conf);
817
818     if (wrote_pid_file)
819         daemon_pid_file_remove();
820     
821     return r;
822 }