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