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