]> git.meshlink.io Git - catta/blob - avahi-daemon/main.c
480753e59a14cbb036aed066a6fbd9ab2eda3296
[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                 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
544     r = 0;
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     if (avahi_server)
562         avahi_server_free(avahi_server);
563
564     daemon_signal_done();
565
566     if (watch_id != (guint) -1)
567         g_source_remove(watch_id);
568     
569     if (io)
570         g_io_channel_unref(io);
571
572         
573     if (loop)
574         g_main_loop_unref(loop);
575
576     if (r != 0 && c->daemonize)
577         daemon_retval_send(1);
578     
579     return r;
580 }
581
582 static gint drop_root(void) {
583     struct passwd *pw;
584     struct group * gr;
585     gint r;
586     
587     if (!(pw = getpwnam(AVAHI_USER))) {
588         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
589         return -1;
590     }
591
592     if (!(gr = getgrnam(AVAHI_GROUP))) {
593         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
594         return -1;
595     }
596
597     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);
598
599     if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
600         avahi_log_error("Failed to change group list: %s", strerror(errno));
601         return -1;
602     }
603
604 #if defined(HAVE_SETRESGID)
605     r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
606 #elif defined(HAVE_SETREGID)
607     r = setregid(gr->gr_gid, gr->gr_gid);
608 #else
609     if ((r = setgid(gr->gr_gid)) >= 0)
610         r = setegid(gr->gr_gid);
611 #endif
612
613     if (r < 0) {
614         avahi_log_error("Failed to change GID: %s", strerror(errno));
615         return -1;
616     }
617
618 #if defined(HAVE_SETRESUID)
619     r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
620 #elif defined(HAVE_SETREUID)
621     r = setreuid(pw->pw_uid, pw->pw_uid);
622 #else
623     if ((r = setuid(pw->pw_uid)) >= 0)
624         r = seteuid(pw->pw_uid);
625 #endif
626
627     if (r < 0) {
628         avahi_log_error("Failed to change UID: %s", strerror(errno));
629         return -1;
630     }
631
632     g_setenv("USER", pw->pw_name, 1);
633     g_setenv("LOGNAME", pw->pw_name, 1);
634     g_setenv("HOME", pw->pw_dir, 1);
635     
636     avahi_log_info("Successfully dropped root privileges.");
637
638     return 0;
639 }
640
641 static const char* pid_file_proc(void) {
642     return AVAHI_DAEMON_RUNTIME_DIR"/pid";
643 }
644
645 static gint make_runtime_dir(void) {
646     gint r = -1;
647     mode_t u;
648     gboolean reset_umask = FALSE;
649     struct passwd *pw;
650     struct group * gr;
651     struct stat st;
652
653     if (!(pw = getpwnam(AVAHI_USER))) {
654         avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
655         goto fail;
656     }
657
658     if (!(gr = getgrnam(AVAHI_GROUP))) {
659         avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
660         goto fail;
661     }
662
663     u = umask(0000);
664     reset_umask = TRUE;
665     
666     if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
667         avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
668         goto fail;
669     }
670     
671     chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
672
673     if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
674         avahi_log_error("stat(): %s\n", strerror(errno));
675         goto fail;
676     }
677
678     if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
679         avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
680         goto fail;
681     }
682
683     r = 0;
684
685 fail:
686     if (reset_umask)
687         umask(u);
688     return r;
689
690 }
691
692 int main(int argc, char *argv[]) {
693     gint r = 255;
694     const gchar *argv0;
695     gboolean wrote_pid_file = FALSE;
696
697     avahi_set_log_function(log_function);
698     
699     avahi_server_config_init(&config.server_config);
700     config.command = DAEMON_RUN;
701     config.daemonize = FALSE;
702     config.config_file = NULL;
703     config.enable_dbus = TRUE;
704     config.drop_root = TRUE;
705     config.publish_dns_servers = NULL;
706     config.publish_resolv_conf = FALSE;
707
708     if ((argv0 = strrchr(argv[0], '/')))
709         argv0++;
710     else
711         argv0 = argv[0];
712
713     daemon_pid_file_ident = (const char *) argv0;
714     daemon_log_ident = (char*) argv0;
715     daemon_pid_file_proc = pid_file_proc;
716     
717     if (parse_command_line(&config, argc, argv) < 0)
718         goto finish;
719
720     if (config.command == DAEMON_HELP) {
721         help(stdout, argv0);
722         r = 0;
723     } else if (config.command == DAEMON_VERSION) {
724         printf("%s "PACKAGE_VERSION"\n", argv0);
725         r = 0;
726     } else if (config.command == DAEMON_KILL) {
727         if (daemon_pid_file_kill_wait(SIGTERM, 5) < 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_RELOAD) {
735         if (daemon_pid_file_kill(SIGHUP) < 0) {
736             avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
737             goto finish;
738         }
739
740         r = 0;
741         
742     } else if (config.command == DAEMON_CHECK)
743         r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
744     else if (config.command == DAEMON_RUN) {
745         pid_t pid;
746
747         if (getuid() != 0) {
748             avahi_log_error("This program is intended to be run as root.");
749             goto finish;
750         }
751         
752         if ((pid = daemon_pid_file_is_running()) >= 0) {
753             avahi_log_error("Daemon already running on PID %u", pid);
754             goto finish;
755         }
756
757         if (load_config_file(&config) < 0)
758             goto finish;
759         
760         if (config.daemonize) {
761             daemon_retval_init();
762             
763             if ((pid = daemon_fork()) < 0)
764                 goto finish;
765             else if (pid != 0) {
766                 int ret;
767                 /** Parent **/
768
769                 if ((ret = daemon_retval_wait(20)) < 0) {
770                     avahi_log_error("Could not recieve return value from daemon process.");
771                     goto finish;
772                 }
773
774                 r = ret;
775                 goto finish;
776             }
777
778             /* Child */
779         }
780
781
782         printf("%s "PACKAGE_VERSION" starting up.\n", argv0);
783         
784         chdir("/");
785
786         if (make_runtime_dir() < 0)
787             goto finish;
788
789         if (config.drop_root) {
790             if (drop_root() < 0)
791                 goto finish;
792         }
793
794         if (daemon_pid_file_create() < 0) {
795             avahi_log_error("Failed to create PID file: %s", strerror(errno));
796
797             if (config.daemonize)
798                 daemon_retval_send(1);
799             goto finish;
800         } else
801             wrote_pid_file = TRUE;
802
803         if (run_server(&config) == 0)
804             r = 0;
805     }
806         
807 finish:
808
809     if (config.daemonize)
810         daemon_retval_done();
811
812     avahi_server_config_free(&config.server_config);
813     g_free(config.config_file);
814     g_strfreev(config.publish_dns_servers);
815     g_strfreev(resolv_conf);
816
817     if (wrote_pid_file)
818         daemon_pid_file_remove();
819     
820     return r;
821 }