4 This file is part of avahi.
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.
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.
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
41 #include <sys/resource.h>
43 #include <libdaemon/dfork.h>
44 #include <libdaemon/dsignal.h>
45 #include <libdaemon/dlog.h>
46 #include <libdaemon/dpid.h>
48 #include <avahi-common/malloc.h>
49 #include <avahi-common/simple-watch.h>
50 #include <avahi-common/error.h>
51 #include <avahi-common/alternative.h>
52 #include <avahi-core/core.h>
53 #include <avahi-core/publish.h>
54 #include <avahi-core/log.h>
57 #include "simple-protocol.h"
58 #include "static-services.h"
59 #include "ini-file-parser.h"
62 #include "dbus-protocol.h"
65 AvahiServer *avahi_server = NULL;
77 AvahiServerConfig server_config;
78 DaemonCommand command;
83 int fail_on_missing_dbus;
85 int publish_resolv_conf;
86 char ** publish_dns_servers;
90 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
91 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
99 #define RESOLV_CONF "/etc/resolv.conf"
101 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
102 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
104 static char **resolv_conf = NULL;
106 static DaemonConfig config;
108 static int has_prefix(const char *s, const char *prefix) {
113 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
116 static int load_resolv_conf(const DaemonConfig *c) {
121 avahi_strfreev(resolv_conf);
124 if (!(f = fopen(RESOLV_CONF, "r"))) {
125 avahi_log_warn("Failed to open "RESOLV_CONF".");
129 resolv_conf = avahi_new0(char*, AVAHI_MAX_WIDE_AREA_SERVERS+1);
131 while (!feof(f) && i < AVAHI_MAX_WIDE_AREA_SERVERS) {
135 if (!(fgets(ln, sizeof(ln), f)))
138 ln[strcspn(ln, "\r\n#")] = 0;
139 p = ln + strspn(ln, "\t ");
141 if (has_prefix(p, "nameserver")) {
143 p += strspn(p, "\t ");
144 p[strcspn(p, "\t ")] = 0;
145 resolv_conf[i++] = avahi_strdup(p);
154 avahi_strfreev(resolv_conf);
164 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
171 g = avahi_s_entry_group_new(s, NULL, NULL);
173 assert(avahi_s_entry_group_is_empty(g));
175 for (p = l; *p; p++) {
178 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
179 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
181 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
182 avahi_s_entry_group_free(g);
183 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
188 avahi_s_entry_group_commit(g);
193 static void remove_dns_server_entry_groups(void) {
195 if (resolv_conf_entry_group)
196 avahi_s_entry_group_reset(resolv_conf_entry_group);
198 if (dns_servers_entry_group)
199 avahi_s_entry_group_reset(dns_servers_entry_group);
202 static void update_wide_area_servers(void) {
203 AvahiAddress a[AVAHI_MAX_WIDE_AREA_SERVERS];
207 for (p = resolv_conf; *p && n < AVAHI_MAX_WIDE_AREA_SERVERS; p++) {
208 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
209 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
214 avahi_server_set_wide_area_servers(avahi_server, a, n);
218 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
219 DaemonConfig *c = userdata;
224 /** This function is possibly called before the global variable
225 * avahi_server has been set, therefore we do it explicitly */
231 dbus_protocol_server_state_changed(state);
234 if (state == AVAHI_SERVER_RUNNING) {
235 avahi_log_info("Server startup complete. Host name is %s. Local service cookie is %u.", avahi_server_get_host_name_fqdn(s), avahi_server_get_local_service_cookie(s));
236 static_service_add_to_server();
238 remove_dns_server_entry_groups();
240 if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
241 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
243 if (c->publish_dns_servers && c->publish_dns_servers[0])
244 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
246 simple_protocol_restart_queries();
248 } else if (state == AVAHI_SERVER_COLLISION) {
251 static_service_remove_from_server();
253 remove_dns_server_entry_groups();
255 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
256 avahi_log_warn("Host name conflict, retrying with <%s>", n);
257 avahi_server_set_host_name(s, n);
262 static void help(FILE *f, const char *argv0) {
265 " -h --help Show this help\n"
266 " -D --daemonize Daemonize after startup (implies -s)\n"
267 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
268 " -k --kill Kill a running daemon\n"
269 " -r --reload Request a running daemon to reload static services\n"
270 " -c --check Return 0 if a daemon is already running\n"
271 " -V --version Show version\n"
272 " -f --file=FILE Load the specified configuration file instead of\n"
273 " "AVAHI_CONFIG_FILE"\n"
274 " --no-rlimits Don't enforce resource limits\n"
275 " --no-drop-root Don't drop privileges\n"
276 " --debug Increase verbosity\n",
281 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
285 OPTION_NO_RLIMITS = 256,
290 static const struct option long_options[] = {
291 { "help", no_argument, NULL, 'h' },
292 { "daemonize", no_argument, NULL, 'D' },
293 { "kill", no_argument, NULL, 'k' },
294 { "version", no_argument, NULL, 'V' },
295 { "file", required_argument, NULL, 'f' },
296 { "reload", no_argument, NULL, 'r' },
297 { "check", no_argument, NULL, 'c' },
298 { "syslog", no_argument, NULL, 's' },
299 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
300 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
301 { "debug", no_argument, NULL, OPTION_DEBUG },
308 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
315 c->command = DAEMON_HELP;
321 c->command = DAEMON_KILL;
324 c->command = DAEMON_VERSION;
327 avahi_free(c->config_file);
328 c->config_file = avahi_strdup(optarg);
331 c->command = DAEMON_RELOAD;
334 c->command = DAEMON_CHECK;
336 case OPTION_NO_RLIMITS:
339 case OPTION_NO_DROP_ROOT:
346 fprintf(stderr, "Invalid command line argument: %c\n", o);
352 fprintf(stderr, "Too many arguments\n");
359 static int is_yes(const char *s) {
362 return *s == 'y' || *s == 'Y';
365 static int load_config_file(DaemonConfig *c) {
368 AvahiIniFileGroup *g;
372 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
375 for (g = f->groups; g; g = g->groups_next) {
377 if (strcasecmp(g->name, "server") == 0) {
380 for (p = g->pairs; p; p = p->pairs_next) {
382 if (strcasecmp(p->key, "host-name") == 0) {
383 avahi_free(c->server_config.host_name);
384 c->server_config.host_name = avahi_strdup(p->value);
385 } else if (strcasecmp(p->key, "domain-name") == 0) {
386 avahi_free(c->server_config.domain_name);
387 c->server_config.domain_name = avahi_strdup(p->value);
388 } else if (strcasecmp(p->key, "use-ipv4") == 0)
389 c->server_config.use_ipv4 = is_yes(p->value);
390 else if (strcasecmp(p->key, "use-ipv6") == 0)
391 c->server_config.use_ipv6 = is_yes(p->value);
392 else if (strcasecmp(p->key, "check-response-ttl") == 0)
393 c->server_config.check_response_ttl = is_yes(p->value);
394 else if (strcasecmp(p->key, "use-iff-running") == 0)
395 c->server_config.use_iff_running = is_yes(p->value);
396 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
397 c->server_config.disallow_other_stacks = is_yes(p->value);
398 else if (strcasecmp(p->key, "enable-dbus") == 0) {
400 if (*(p->value) == 'w' || *(p->value) == 'W') {
401 c->fail_on_missing_dbus = 0;
403 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
404 c->fail_on_missing_dbus = 1;
409 } else if (strcasecmp(p->key, "drop-root") == 0)
410 c->drop_root = is_yes(p->value);
411 else if (strcasecmp(p->key, "add-service-cookie") == 0)
412 c->server_config.add_service_cookie = is_yes(p->value);
414 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
419 } else if (strcasecmp(g->name, "publish") == 0) {
422 for (p = g->pairs; p; p = p->pairs_next) {
424 if (strcasecmp(p->key, "publish-addresses") == 0)
425 c->server_config.publish_addresses = is_yes(p->value);
426 else if (strcasecmp(p->key, "publish-hinfo") == 0)
427 c->server_config.publish_hinfo = is_yes(p->value);
428 else if (strcasecmp(p->key, "publish-workstation") == 0)
429 c->server_config.publish_workstation = is_yes(p->value);
430 else if (strcasecmp(p->key, "publish-domain") == 0)
431 c->server_config.publish_domain = is_yes(p->value);
432 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
433 c->publish_resolv_conf = is_yes(p->value);
434 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
435 avahi_strfreev(c->publish_dns_servers);
436 c->publish_dns_servers = avahi_split_csv(p->value);
438 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
443 } else if (strcasecmp(g->name, "wide-area") == 0) {
446 for (p = g->pairs; p; p = p->pairs_next) {
448 if (strcasecmp(p->key, "enable-wide-area") == 0)
449 c->server_config.enable_wide_area = is_yes(p->value);
451 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
456 } else if (strcasecmp(g->name, "reflector") == 0) {
459 for (p = g->pairs; p; p = p->pairs_next) {
461 if (strcasecmp(p->key, "enable-reflector") == 0)
462 c->server_config.enable_reflector = is_yes(p->value);
463 else if (strcasecmp(p->key, "reflect-ipv") == 0)
464 c->server_config.reflect_ipv = is_yes(p->value);
466 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
471 } else if (strcasecmp(g->name, "rlimits") == 0) {
474 for (p = g->pairs; p; p = p->pairs_next) {
476 if (strcasecmp(p->key, "rlimit-as") == 0) {
477 c->rlimit_as_set = 1;
478 c->rlimit_as = atoi(p->value);
479 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
480 c->rlimit_core_set = 1;
481 c->rlimit_core = atoi(p->value);
482 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
483 c->rlimit_data_set = 1;
484 c->rlimit_data = atoi(p->value);
485 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
486 c->rlimit_fsize_set = 1;
487 c->rlimit_fsize = atoi(p->value);
488 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
489 c->rlimit_nofile_set = 1;
490 c->rlimit_nofile = atoi(p->value);
491 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
492 c->rlimit_stack_set = 1;
493 c->rlimit_stack = atoi(p->value);
495 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
496 c->rlimit_nproc_set = 1;
497 c->rlimit_nproc = atoi(p->value);
500 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
507 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
517 avahi_ini_file_free(f);
522 static void log_function(AvahiLogLevel level, const char *txt) {
524 static const int log_level_map[] = {
532 assert(level < AVAHI_LOG_LEVEL_MAX);
535 if (!config.debug && level == AVAHI_LOG_DEBUG)
538 daemon_log(log_level_map[level], "%s", txt);
541 static void dump(const char *text, void* userdata) {
542 avahi_log_info("%s", text);
545 static void signal_callback(AvahiWatch *watch, int fd, AvahiWatchEvent event, void *userdata) {
547 AvahiSimplePoll *simple_poll_api = userdata;
548 const AvahiPoll *poll_api;
551 assert(simple_poll_api);
553 poll_api = avahi_simple_poll_get(simple_poll_api);
555 if ((sig = daemon_signal_next()) <= 0) {
556 avahi_log_error("daemon_signal_next() failed");
557 poll_api->watch_free(watch);
567 sig == SIGINT ? "SIGINT" :
568 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
569 avahi_simple_poll_quit(simple_poll_api);
573 avahi_log_info("Got SIGHUP, reloading.");
574 static_service_load();
575 static_service_add_to_server();
577 if (resolv_conf_entry_group)
578 avahi_s_entry_group_reset(resolv_conf_entry_group);
580 load_resolv_conf(&config);
582 update_wide_area_servers();
584 if (config.publish_resolv_conf && resolv_conf && resolv_conf[0])
585 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
590 avahi_log_info("Got SIGUSR1, dumping record data.");
591 avahi_server_dump(avahi_server, dump, NULL);
595 avahi_log_warn("Got spurious signal, ignoring.");
600 static int run_server(DaemonConfig *c) {
603 AvahiSimplePoll *simple_poll_api;
604 const AvahiPoll *poll_api;
605 AvahiWatch *sig_watch;
609 if (!(simple_poll_api = avahi_simple_poll_new())) {
610 avahi_log_error("Failed to create main loop object.");
614 poll_api = avahi_simple_poll_get(simple_poll_api);
616 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
617 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
621 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
622 avahi_log_error( "Failed to create signal watcher");
626 if (simple_protocol_setup(poll_api) < 0)
628 if (c->enable_dbus) {
630 if (dbus_protocol_setup(poll_api) < 0) {
632 if (c->fail_on_missing_dbus)
635 avahi_log_warn("WARNING: Failed to contact D-BUS daemon, disabling D-BUS support.");
639 avahi_log_warn("WARNING: We are configured to enable D-BUS but it was not compiled in.");
645 static_service_load();
647 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
648 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
652 update_wide_area_servers();
655 daemon_retval_send(0);
658 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
660 /* We handle signals through an FD, so let's continue */
664 avahi_log_error("poll(): %s", strerror(errno));
674 static_service_remove_from_server();
675 static_service_free_all();
676 remove_dns_server_entry_groups();
678 simple_protocol_shutdown();
682 dbus_protocol_shutdown();
686 avahi_server_free(avahi_server);
688 daemon_signal_done();
691 poll_api->watch_free(sig_watch);
694 avahi_simple_poll_free(simple_poll_api);
696 if (r != 0 && c->daemonize)
697 daemon_retval_send(1);
702 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
704 static int drop_root(void) {
709 if (!(pw = getpwnam(AVAHI_USER))) {
710 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
714 if (!(gr = getgrnam(AVAHI_GROUP))) {
715 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
719 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);
721 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
722 avahi_log_error("Failed to change group list: %s", strerror(errno));
726 #if defined(HAVE_SETRESGID)
727 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
728 #elif defined(HAVE_SETREGID)
729 r = setregid(gr->gr_gid, gr->gr_gid);
731 if ((r = setgid(gr->gr_gid)) >= 0)
732 r = setegid(gr->gr_gid);
736 avahi_log_error("Failed to change GID: %s", strerror(errno));
740 #if defined(HAVE_SETRESUID)
741 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
742 #elif defined(HAVE_SETREUID)
743 r = setreuid(pw->pw_uid, pw->pw_uid);
745 if ((r = setuid(pw->pw_uid)) >= 0)
746 r = seteuid(pw->pw_uid);
750 avahi_log_error("Failed to change UID: %s", strerror(errno));
754 set_env("USER", pw->pw_name);
755 set_env("LOGNAME", pw->pw_name);
756 set_env("HOME", pw->pw_dir);
758 avahi_log_info("Successfully dropped root privileges.");
763 static const char* pid_file_proc(void) {
764 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
767 static int make_runtime_dir(void) {
775 if (!(pw = getpwnam(AVAHI_USER))) {
776 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
780 if (!(gr = getgrnam(AVAHI_GROUP))) {
781 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
788 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
789 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
793 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
795 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
796 avahi_log_error("stat(): %s\n", strerror(errno));
800 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
801 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
813 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
815 rl.rlim_cur = rl.rlim_max = limit;
817 if (setrlimit(resource, &rl) < 0)
818 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
821 static void enforce_rlimits(void) {
823 if (config.rlimit_as_set)
824 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
825 if (config.rlimit_core_set)
826 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
827 if (config.rlimit_data_set)
828 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
829 if (config.rlimit_fsize_set)
830 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
831 if (config.rlimit_nofile_set)
832 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
833 if (config.rlimit_stack_set)
834 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
836 if (config.rlimit_nproc_set)
837 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
840 #ifdef RLIMIT_MEMLOCK
841 /* We don't need locked memory */
842 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
846 #define RANDOM_DEVICE "/dev/urandom"
848 static void init_rand_seed(void) {
852 /* Try to initialize seed from /dev/urandom, to make it a little
853 * less predictable, and to make sure that multiple machines
854 * booted at the same time choose different random seeds. */
855 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
856 read(fd, &seed, sizeof(seed));
860 /* If the initialization failed by some reason, we add the time to the seed*/
861 seed |= (unsigned) time(NULL);
866 int main(int argc, char *argv[]) {
869 int wrote_pid_file = 0;
871 avahi_set_log_function(log_function);
875 avahi_server_config_init(&config.server_config);
876 config.command = DAEMON_RUN;
877 config.daemonize = 0;
878 config.config_file = NULL;
880 config.enable_dbus = 1;
881 config.fail_on_missing_dbus = 1;
883 config.enable_dbus = 0;
884 config.fail_on_missing_dbus = 0;
886 config.drop_root = 1;
887 config.publish_dns_servers = NULL;
888 config.publish_resolv_conf = 0;
889 config.use_syslog = 0;
890 config.no_rlimits = 0;
893 config.rlimit_as_set = 0;
894 config.rlimit_core_set = 0;
895 config.rlimit_data_set = 0;
896 config.rlimit_fsize_set = 0;
897 config.rlimit_nofile_set = 0;
898 config.rlimit_stack_set = 0;
900 config.rlimit_nproc_set = 0;
903 if ((argv0 = strrchr(argv[0], '/')))
908 daemon_pid_file_ident = (const char *) argv0;
909 daemon_log_ident = (char*) argv0;
910 daemon_pid_file_proc = pid_file_proc;
912 if (parse_command_line(&config, argc, argv) < 0)
915 if (config.command == DAEMON_HELP) {
918 } else if (config.command == DAEMON_VERSION) {
919 printf("%s "PACKAGE_VERSION"\n", argv0);
921 } else if (config.command == DAEMON_KILL) {
922 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
923 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
929 } else if (config.command == DAEMON_RELOAD) {
930 if (daemon_pid_file_kill(SIGHUP) < 0) {
931 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
937 } else if (config.command == DAEMON_CHECK)
938 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
939 else if (config.command == DAEMON_RUN) {
942 if (getuid() != 0 && config.drop_root) {
943 avahi_log_error("This program is intended to be run as root.");
947 if ((pid = daemon_pid_file_is_running()) >= 0) {
948 avahi_log_error("Daemon already running on PID %u", pid);
952 if (load_config_file(&config) < 0)
955 if (config.daemonize) {
956 daemon_retval_init();
958 if ((pid = daemon_fork()) < 0)
964 if ((ret = daemon_retval_wait(20)) < 0) {
965 avahi_log_error("Could not recieve return value from daemon process.");
976 if (config.use_syslog || config.daemonize)
977 daemon_log_use = DAEMON_LOG_SYSLOG;
979 if (make_runtime_dir() < 0)
982 if (config.drop_root) {
987 if (daemon_pid_file_create() < 0) {
988 avahi_log_error("Failed to create PID file: %s", strerror(errno));
990 if (config.daemonize)
991 daemon_retval_send(1);
996 if (!config.no_rlimits)
1001 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1003 if (run_server(&config) == 0)
1009 if (config.daemonize)
1010 daemon_retval_done();
1012 avahi_server_config_free(&config.server_config);
1013 avahi_free(config.config_file);
1014 avahi_strfreev(config.publish_dns_servers);
1015 avahi_strfreev(resolv_conf);
1018 daemon_pid_file_remove();