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>
53 #include <avahi-core/core.h>
54 #include <avahi-core/publish.h>
55 #include <avahi-core/dns-srv-rr.h>
56 #include <avahi-core/log.h>
59 #include "simple-protocol.h"
60 #include "static-services.h"
61 #include "ini-file-parser.h"
64 #include "dbus-protocol.h"
67 AvahiServer *avahi_server = NULL;
68 AvahiSimplePoll *simple_poll_api = NULL;
80 AvahiServerConfig server_config;
81 DaemonCommand command;
86 int fail_on_missing_dbus;
88 int publish_resolv_conf;
89 char ** publish_dns_servers;
93 int rlimit_as_set, rlimit_core_set, rlimit_data_set, rlimit_fsize_set, rlimit_nofile_set, rlimit_stack_set;
94 rlim_t rlimit_as, rlimit_core, rlimit_data, rlimit_fsize, rlimit_nofile, rlimit_stack;
102 #define RESOLV_CONF "/etc/resolv.conf"
104 static AvahiSEntryGroup *dns_servers_entry_group = NULL;
105 static AvahiSEntryGroup *resolv_conf_entry_group = NULL;
107 static char **resolv_conf = NULL;
109 static DaemonConfig config;
111 static int has_prefix(const char *s, const char *prefix) {
116 return strlen(s) >= l && strncmp(s, prefix, l) == 0;
119 static int load_resolv_conf(const DaemonConfig *c) {
124 avahi_strfreev(resolv_conf);
127 if (!(f = fopen(RESOLV_CONF, "r"))) {
128 avahi_log_warn("Failed to open "RESOLV_CONF".");
132 resolv_conf = avahi_new0(char*, AVAHI_MAX_WIDE_AREA_SERVERS+1);
134 while (!feof(f) && i < AVAHI_MAX_WIDE_AREA_SERVERS) {
138 if (!(fgets(ln, sizeof(ln), f)))
141 ln[strcspn(ln, "\r\n#")] = 0;
142 p = ln + strspn(ln, "\t ");
144 if (has_prefix(p, "nameserver")) {
146 p += strspn(p, "\t ");
147 p[strcspn(p, "\t ")] = 0;
148 resolv_conf[i++] = avahi_strdup(p);
157 avahi_strfreev(resolv_conf);
167 static AvahiSEntryGroup* add_dns_servers(AvahiServer *s, AvahiSEntryGroup* g, char **l) {
174 g = avahi_s_entry_group_new(s, NULL, NULL);
176 assert(avahi_s_entry_group_is_empty(g));
178 for (p = l; *p; p++) {
181 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a))
182 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
184 if (avahi_server_add_dns_server_address(s, g, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, NULL, AVAHI_DNS_SERVER_RESOLVE, &a, 53) < 0) {
185 avahi_s_entry_group_free(g);
186 avahi_log_error("Failed to add DNS server address: %s", avahi_strerror(avahi_server_errno(s)));
191 avahi_s_entry_group_commit(g);
196 static void remove_dns_server_entry_groups(void) {
198 if (resolv_conf_entry_group)
199 avahi_s_entry_group_reset(resolv_conf_entry_group);
201 if (dns_servers_entry_group)
202 avahi_s_entry_group_reset(dns_servers_entry_group);
205 static void update_wide_area_servers(void) {
206 AvahiAddress a[AVAHI_MAX_WIDE_AREA_SERVERS];
210 for (p = resolv_conf; *p && n < AVAHI_MAX_WIDE_AREA_SERVERS; p++) {
211 if (!avahi_address_parse(*p, AVAHI_PROTO_UNSPEC, &a[n]))
212 avahi_log_warn("Failed to parse address '%s', ignoring.", *p);
217 avahi_server_set_wide_area_servers(avahi_server, a, n);
220 static void server_callback(AvahiServer *s, AvahiServerState state, void *userdata) {
221 DaemonConfig *c = userdata;
226 /* This function is possibly called before the global variable
227 * avahi_server has been set, therefore we do it explicitly */
233 dbus_protocol_server_state_changed(state);
237 case AVAHI_SERVER_RUNNING:
238 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));
239 static_service_add_to_server();
241 remove_dns_server_entry_groups();
243 if (c->publish_resolv_conf && resolv_conf && resolv_conf[0])
244 resolv_conf_entry_group = add_dns_servers(s, resolv_conf_entry_group, resolv_conf);
246 if (c->publish_dns_servers && c->publish_dns_servers[0])
247 dns_servers_entry_group = add_dns_servers(s, dns_servers_entry_group, c->publish_dns_servers);
249 simple_protocol_restart_queries();
252 case AVAHI_SERVER_COLLISION: {
255 static_service_remove_from_server();
257 remove_dns_server_entry_groups();
259 n = avahi_alternative_host_name(avahi_server_get_host_name(s));
260 avahi_log_warn("Host name conflict, retrying with <%s>", n);
261 avahi_server_set_host_name(s, n);
266 case AVAHI_SERVER_FAILURE:
268 avahi_log_error("Server error: %s", avahi_strerror(avahi_server_errno(s)));
269 avahi_simple_poll_quit(simple_poll_api);
272 case AVAHI_SERVER_REGISTERING:
273 case AVAHI_SERVER_INVALID:
279 static void help(FILE *f, const char *argv0) {
282 " -h --help Show this help\n"
283 " -D --daemonize Daemonize after startup (implies -s)\n"
284 " -s --syslog Write log messages to syslog(3) instead of STDERR\n"
285 " -k --kill Kill a running daemon\n"
286 " -r --reload Request a running daemon to reload static services\n"
287 " -c --check Return 0 if a daemon is already running\n"
288 " -V --version Show version\n"
289 " -f --file=FILE Load the specified configuration file instead of\n"
290 " "AVAHI_CONFIG_FILE"\n"
291 " --no-rlimits Don't enforce resource limits\n"
292 " --no-drop-root Don't drop privileges\n"
293 " --debug Increase verbosity\n",
298 static int parse_command_line(DaemonConfig *c, int argc, char *argv[]) {
302 OPTION_NO_RLIMITS = 256,
307 static const struct option long_options[] = {
308 { "help", no_argument, NULL, 'h' },
309 { "daemonize", no_argument, NULL, 'D' },
310 { "kill", no_argument, NULL, 'k' },
311 { "version", no_argument, NULL, 'V' },
312 { "file", required_argument, NULL, 'f' },
313 { "reload", no_argument, NULL, 'r' },
314 { "check", no_argument, NULL, 'c' },
315 { "syslog", no_argument, NULL, 's' },
316 { "no-rlimits", no_argument, NULL, OPTION_NO_RLIMITS },
317 { "no-drop-root", no_argument, NULL, OPTION_NO_DROP_ROOT },
318 { "debug", no_argument, NULL, OPTION_DEBUG },
325 while ((o = getopt_long(argc, argv, "hDkVf:rcs", long_options, NULL)) >= 0) {
332 c->command = DAEMON_HELP;
338 c->command = DAEMON_KILL;
341 c->command = DAEMON_VERSION;
344 avahi_free(c->config_file);
345 c->config_file = avahi_strdup(optarg);
348 c->command = DAEMON_RELOAD;
351 c->command = DAEMON_CHECK;
353 case OPTION_NO_RLIMITS:
356 case OPTION_NO_DROP_ROOT:
363 fprintf(stderr, "Invalid command line argument: %c\n", o);
369 fprintf(stderr, "Too many arguments\n");
376 static int is_yes(const char *s) {
379 return *s == 'y' || *s == 'Y';
382 static int load_config_file(DaemonConfig *c) {
385 AvahiIniFileGroup *g;
389 if (!(f = avahi_ini_file_load(c->config_file ? c->config_file : AVAHI_CONFIG_FILE)))
392 for (g = f->groups; g; g = g->groups_next) {
394 if (strcasecmp(g->name, "server") == 0) {
397 for (p = g->pairs; p; p = p->pairs_next) {
399 if (strcasecmp(p->key, "host-name") == 0) {
400 avahi_free(c->server_config.host_name);
401 c->server_config.host_name = avahi_strdup(p->value);
402 } else if (strcasecmp(p->key, "domain-name") == 0) {
403 avahi_free(c->server_config.domain_name);
404 c->server_config.domain_name = avahi_strdup(p->value);
405 } else if (strcasecmp(p->key, "use-ipv4") == 0)
406 c->server_config.use_ipv4 = is_yes(p->value);
407 else if (strcasecmp(p->key, "use-ipv6") == 0)
408 c->server_config.use_ipv6 = is_yes(p->value);
409 else if (strcasecmp(p->key, "check-response-ttl") == 0)
410 c->server_config.check_response_ttl = is_yes(p->value);
411 else if (strcasecmp(p->key, "use-iff-running") == 0)
412 c->server_config.use_iff_running = is_yes(p->value);
413 else if (strcasecmp(p->key, "disallow-other-stacks") == 0)
414 c->server_config.disallow_other_stacks = is_yes(p->value);
415 else if (strcasecmp(p->key, "enable-dbus") == 0) {
417 if (*(p->value) == 'w' || *(p->value) == 'W') {
418 c->fail_on_missing_dbus = 0;
420 } else if (*(p->value) == 'y' || *(p->value) == 'Y') {
421 c->fail_on_missing_dbus = 1;
426 } else if (strcasecmp(p->key, "drop-root") == 0)
427 c->drop_root = is_yes(p->value);
428 else if (strcasecmp(p->key, "add-service-cookie") == 0)
429 c->server_config.add_service_cookie = is_yes(p->value);
431 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
436 } else if (strcasecmp(g->name, "publish") == 0) {
439 for (p = g->pairs; p; p = p->pairs_next) {
441 if (strcasecmp(p->key, "publish-addresses") == 0)
442 c->server_config.publish_addresses = is_yes(p->value);
443 else if (strcasecmp(p->key, "publish-hinfo") == 0)
444 c->server_config.publish_hinfo = is_yes(p->value);
445 else if (strcasecmp(p->key, "publish-workstation") == 0)
446 c->server_config.publish_workstation = is_yes(p->value);
447 else if (strcasecmp(p->key, "publish-domain") == 0)
448 c->server_config.publish_domain = is_yes(p->value);
449 else if (strcasecmp(p->key, "publish-resolv-conf-dns-servers") == 0)
450 c->publish_resolv_conf = is_yes(p->value);
451 else if (strcasecmp(p->key, "publish-dns-servers") == 0) {
452 avahi_strfreev(c->publish_dns_servers);
453 c->publish_dns_servers = avahi_split_csv(p->value);
455 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
460 } else if (strcasecmp(g->name, "wide-area") == 0) {
463 for (p = g->pairs; p; p = p->pairs_next) {
465 if (strcasecmp(p->key, "enable-wide-area") == 0)
466 c->server_config.enable_wide_area = is_yes(p->value);
468 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
473 } else if (strcasecmp(g->name, "reflector") == 0) {
476 for (p = g->pairs; p; p = p->pairs_next) {
478 if (strcasecmp(p->key, "enable-reflector") == 0)
479 c->server_config.enable_reflector = is_yes(p->value);
480 else if (strcasecmp(p->key, "reflect-ipv") == 0)
481 c->server_config.reflect_ipv = is_yes(p->value);
483 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
488 } else if (strcasecmp(g->name, "rlimits") == 0) {
491 for (p = g->pairs; p; p = p->pairs_next) {
493 if (strcasecmp(p->key, "rlimit-as") == 0) {
494 c->rlimit_as_set = 1;
495 c->rlimit_as = atoi(p->value);
496 } else if (strcasecmp(p->key, "rlimit-core") == 0) {
497 c->rlimit_core_set = 1;
498 c->rlimit_core = atoi(p->value);
499 } else if (strcasecmp(p->key, "rlimit-data") == 0) {
500 c->rlimit_data_set = 1;
501 c->rlimit_data = atoi(p->value);
502 } else if (strcasecmp(p->key, "rlimit-fsize") == 0) {
503 c->rlimit_fsize_set = 1;
504 c->rlimit_fsize = atoi(p->value);
505 } else if (strcasecmp(p->key, "rlimit-nofile") == 0) {
506 c->rlimit_nofile_set = 1;
507 c->rlimit_nofile = atoi(p->value);
508 } else if (strcasecmp(p->key, "rlimit-stack") == 0) {
509 c->rlimit_stack_set = 1;
510 c->rlimit_stack = atoi(p->value);
512 } else if (strcasecmp(p->key, "rlimit-nproc") == 0) {
513 c->rlimit_nproc_set = 1;
514 c->rlimit_nproc = atoi(p->value);
517 avahi_log_error("Invalid configuration key \"%s\" in group \"%s\"\n", p->key, g->name);
524 avahi_log_error("Invalid configuration file group \"%s\".\n", g->name);
534 avahi_ini_file_free(f);
539 static void log_function(AvahiLogLevel level, const char *txt) {
541 static const int log_level_map[] = {
549 assert(level < AVAHI_LOG_LEVEL_MAX);
552 if (!config.debug && level == AVAHI_LOG_DEBUG)
555 daemon_log(log_level_map[level], "%s", txt);
558 static void dump(const char *text, void* userdata) {
559 avahi_log_info("%s", text);
562 static void signal_callback(AvahiWatch *watch, int fd, AvahiWatchEvent event, void *userdata) {
564 const AvahiPoll *poll_api;
567 assert(simple_poll_api);
569 poll_api = avahi_simple_poll_get(simple_poll_api);
571 if ((sig = daemon_signal_next()) <= 0) {
572 avahi_log_error("daemon_signal_next() failed");
573 poll_api->watch_free(watch);
583 sig == SIGINT ? "SIGINT" :
584 (sig == SIGQUIT ? "SIGQUIT" : "SIGTERM"));
585 avahi_simple_poll_quit(simple_poll_api);
589 avahi_log_info("Got SIGHUP, reloading.");
590 static_service_load();
591 static_service_add_to_server();
593 if (resolv_conf_entry_group)
594 avahi_s_entry_group_reset(resolv_conf_entry_group);
596 load_resolv_conf(&config);
598 update_wide_area_servers();
600 if (config.publish_resolv_conf && resolv_conf && resolv_conf[0])
601 resolv_conf_entry_group = add_dns_servers(avahi_server, resolv_conf_entry_group, resolv_conf);
606 avahi_log_info("Got SIGUSR1, dumping record data.");
607 avahi_server_dump(avahi_server, dump, NULL);
611 avahi_log_warn("Got spurious signal, ignoring.");
616 static int run_server(DaemonConfig *c) {
619 const AvahiPoll *poll_api;
620 AvahiWatch *sig_watch;
624 if (!(simple_poll_api = avahi_simple_poll_new())) {
625 avahi_log_error("Failed to create main loop object.");
629 poll_api = avahi_simple_poll_get(simple_poll_api);
631 if (daemon_signal_init(SIGINT, SIGQUIT, SIGHUP, SIGTERM, SIGUSR1, 0) < 0) {
632 avahi_log_error("Could not register signal handlers (%s).", strerror(errno));
636 if (!(sig_watch = poll_api->watch_new(poll_api, daemon_signal_fd(), AVAHI_WATCH_IN, signal_callback, simple_poll_api))) {
637 avahi_log_error( "Failed to create signal watcher");
641 if (simple_protocol_setup(poll_api) < 0)
643 if (c->enable_dbus) {
645 if (dbus_protocol_setup(poll_api) < 0) {
647 if (c->fail_on_missing_dbus)
650 avahi_log_warn("WARNING: Failed to contact D-BUS daemon, disabling D-BUS support.");
654 avahi_log_warn("WARNING: We are configured to enable D-BUS but it was not compiled in.");
660 static_service_load();
662 if (!(avahi_server = avahi_server_new(poll_api, &c->server_config, server_callback, c, &error))) {
663 avahi_log_error("Failed to create server: %s", avahi_strerror(error));
667 update_wide_area_servers();
670 daemon_retval_send(0);
673 if ((r = avahi_simple_poll_iterate(simple_poll_api, -1)) < 0) {
675 /* We handle signals through an FD, so let's continue */
679 avahi_log_error("poll(): %s", strerror(errno));
689 static_service_remove_from_server();
690 static_service_free_all();
691 remove_dns_server_entry_groups();
693 simple_protocol_shutdown();
697 dbus_protocol_shutdown();
701 avahi_server_free(avahi_server);
705 daemon_signal_done();
708 poll_api->watch_free(sig_watch);
710 if (simple_poll_api) {
711 avahi_simple_poll_free(simple_poll_api);
712 simple_poll_api = NULL;
715 if (r != 0 && c->daemonize)
716 daemon_retval_send(1);
721 #define set_env(key, value) putenv(avahi_strdup_printf("%s=%s", (key), (value)))
723 static int drop_root(void) {
728 if (!(pw = getpwnam(AVAHI_USER))) {
729 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
733 if (!(gr = getgrnam(AVAHI_GROUP))) {
734 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
738 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);
740 if (initgroups(AVAHI_USER, gr->gr_gid) != 0) {
741 avahi_log_error("Failed to change group list: %s", strerror(errno));
745 #if defined(HAVE_SETRESGID)
746 r = setresgid(gr->gr_gid, gr->gr_gid, gr->gr_gid);
747 #elif defined(HAVE_SETEGID)
748 if ((r = setgid(gr->gr_gid)) >= 0)
749 r = setegid(gr->gr_gid);
750 #elif defined(HAVE_SETREGID)
751 r = setregid(gr->gr_gid, gr->gr_gid);
753 #error "No API to drop priviliges"
757 avahi_log_error("Failed to change GID: %s", strerror(errno));
761 #if defined(HAVE_SETRESUID)
762 r = setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid);
763 #elif defined(HAVE_SETEUID)
764 if ((r = setuid(pw->pw_uid)) >= 0)
765 r = seteuid(pw->pw_uid);
766 #elif defined(HAVE_SETREUID)
767 r = setreuid(pw->pw_uid, pw->pw_uid);
769 #error "No API to drop priviliges"
773 avahi_log_error("Failed to change UID: %s", strerror(errno));
777 set_env("USER", pw->pw_name);
778 set_env("LOGNAME", pw->pw_name);
779 set_env("HOME", pw->pw_dir);
781 avahi_log_info("Successfully dropped root privileges.");
786 static const char* pid_file_proc(void) {
787 return AVAHI_DAEMON_RUNTIME_DIR"/pid";
790 static int make_runtime_dir(void) {
798 if (!(pw = getpwnam(AVAHI_USER))) {
799 avahi_log_error( "Failed to find user '"AVAHI_USER"'.");
803 if (!(gr = getgrnam(AVAHI_GROUP))) {
804 avahi_log_error( "Failed to find group '"AVAHI_GROUP"'.");
811 if (mkdir(AVAHI_DAEMON_RUNTIME_DIR, 0755) < 0 && errno != EEXIST) {
812 avahi_log_error("mkdir(\""AVAHI_DAEMON_RUNTIME_DIR"\"): %s", strerror(errno));
816 chown(AVAHI_DAEMON_RUNTIME_DIR, pw->pw_uid, gr->gr_gid);
818 if (stat(AVAHI_DAEMON_RUNTIME_DIR, &st) < 0) {
819 avahi_log_error("stat(): %s\n", strerror(errno));
823 if (!S_ISDIR(st.st_mode) || st.st_uid != pw->pw_uid || st.st_gid != gr->gr_gid) {
824 avahi_log_error("Failed to create runtime directory "AVAHI_DAEMON_RUNTIME_DIR".");
836 static void set_one_rlimit(int resource, rlim_t limit, const char *name) {
838 rl.rlim_cur = rl.rlim_max = limit;
840 if (setrlimit(resource, &rl) < 0)
841 avahi_log_warn("setrlimit(%s, {%u, %u}) failed: %s", name, (unsigned) limit, (unsigned) limit, strerror(errno));
844 static void enforce_rlimits(void) {
846 if (config.rlimit_as_set)
847 set_one_rlimit(RLIMIT_AS, config.rlimit_as, "RLIMIT_AS");
848 if (config.rlimit_core_set)
849 set_one_rlimit(RLIMIT_CORE, config.rlimit_core, "RLIMIT_CORE");
850 if (config.rlimit_data_set)
851 set_one_rlimit(RLIMIT_DATA, config.rlimit_data, "RLIMIT_DATA");
852 if (config.rlimit_fsize_set)
853 set_one_rlimit(RLIMIT_FSIZE, config.rlimit_fsize, "RLIMIT_FSIZE");
854 if (config.rlimit_nofile_set)
855 set_one_rlimit(RLIMIT_NOFILE, config.rlimit_nofile, "RLIMIT_NOFILE");
856 if (config.rlimit_stack_set)
857 set_one_rlimit(RLIMIT_STACK, config.rlimit_stack, "RLIMIT_STACK");
859 if (config.rlimit_nproc_set)
860 set_one_rlimit(RLIMIT_NPROC, config.rlimit_nproc, "RLIMIT_NPROC");
863 #ifdef RLIMIT_MEMLOCK
864 /* We don't need locked memory */
865 set_one_rlimit(RLIMIT_MEMLOCK, 0, "RLIMIT_MEMLOCK");
869 #define RANDOM_DEVICE "/dev/urandom"
871 static void init_rand_seed(void) {
875 /* Try to initialize seed from /dev/urandom, to make it a little
876 * less predictable, and to make sure that multiple machines
877 * booted at the same time choose different random seeds. */
878 if ((fd = open(RANDOM_DEVICE, O_RDONLY)) >= 0) {
879 read(fd, &seed, sizeof(seed));
883 /* If the initialization failed by some reason, we add the time to the seed*/
884 seed |= (unsigned) time(NULL);
889 int main(int argc, char *argv[]) {
892 int wrote_pid_file = 0;
894 avahi_set_log_function(log_function);
898 avahi_server_config_init(&config.server_config);
899 config.command = DAEMON_RUN;
900 config.daemonize = 0;
901 config.config_file = NULL;
903 config.enable_dbus = 1;
904 config.fail_on_missing_dbus = 1;
906 config.enable_dbus = 0;
907 config.fail_on_missing_dbus = 0;
909 config.drop_root = 1;
910 config.publish_dns_servers = NULL;
911 config.publish_resolv_conf = 0;
912 config.use_syslog = 0;
913 config.no_rlimits = 0;
916 config.rlimit_as_set = 0;
917 config.rlimit_core_set = 0;
918 config.rlimit_data_set = 0;
919 config.rlimit_fsize_set = 0;
920 config.rlimit_nofile_set = 0;
921 config.rlimit_stack_set = 0;
923 config.rlimit_nproc_set = 0;
926 if ((argv0 = strrchr(argv[0], '/')))
931 daemon_pid_file_ident = (const char *) argv0;
932 daemon_log_ident = (char*) argv0;
933 daemon_pid_file_proc = pid_file_proc;
935 if (parse_command_line(&config, argc, argv) < 0)
938 if (config.command == DAEMON_HELP) {
941 } else if (config.command == DAEMON_VERSION) {
942 printf("%s "PACKAGE_VERSION"\n", argv0);
944 } else if (config.command == DAEMON_KILL) {
945 if (daemon_pid_file_kill_wait(SIGTERM, 5) < 0) {
946 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
952 } else if (config.command == DAEMON_RELOAD) {
953 if (daemon_pid_file_kill(SIGHUP) < 0) {
954 avahi_log_warn("Failed to kill daemon: %s", strerror(errno));
960 } else if (config.command == DAEMON_CHECK)
961 r = (daemon_pid_file_is_running() >= 0) ? 0 : 1;
962 else if (config.command == DAEMON_RUN) {
965 if (getuid() != 0 && config.drop_root) {
966 avahi_log_error("This program is intended to be run as root.");
970 if ((pid = daemon_pid_file_is_running()) >= 0) {
971 avahi_log_error("Daemon already running on PID %u", pid);
975 if (load_config_file(&config) < 0)
978 if (config.daemonize) {
979 daemon_retval_init();
981 if ((pid = daemon_fork()) < 0)
987 if ((ret = daemon_retval_wait(20)) < 0) {
988 avahi_log_error("Could not recieve return value from daemon process.");
999 if (config.use_syslog || config.daemonize)
1000 daemon_log_use = DAEMON_LOG_SYSLOG;
1002 if (make_runtime_dir() < 0)
1005 if (config.drop_root) {
1006 if (drop_root() < 0)
1010 if (daemon_pid_file_create() < 0) {
1011 avahi_log_error("Failed to create PID file: %s", strerror(errno));
1013 if (config.daemonize)
1014 daemon_retval_send(1);
1019 if (!config.no_rlimits)
1024 avahi_log_info("%s "PACKAGE_VERSION" starting up.", argv0);
1026 if (run_server(&config) == 0)
1032 if (config.daemonize)
1033 daemon_retval_done();
1035 avahi_server_config_free(&config.server_config);
1036 avahi_free(config.config_file);
1037 avahi_strfreev(config.publish_dns_servers);
1038 avahi_strfreev(resolv_conf);
1041 daemon_pid_file_remove();