/** set verbosity, check rlimits, cache settings */ static void apply_settings(struct daemon* daemon, struct config_file* cfg, int cmdline_verbose, int debug_mode, const char* log_default_identity) { /* apply if they have changed */ verbosity = cmdline_verbose + cfg->verbosity; if (debug_mode > 1) { cfg->use_syslog = 0; free(cfg->logfile); cfg->logfile = NULL; } daemon_apply_cfg(daemon, cfg); checkrlimits(cfg); log_ident_set_fromdefault(cfg, log_default_identity); }
/** set verbosity, check rlimits, cache settings */ static void apply_settings(struct daemon* daemon, struct config_file* cfg, int cmdline_verbose, int debug_mode, const char* log_default_identity) { /* apply if they have changed */ verbosity = cmdline_verbose + cfg->verbosity; if (debug_mode > 1) { cfg->use_syslog = 0; free(cfg->logfile); cfg->logfile = NULL; } daemon_apply_cfg(daemon, cfg); checkrlimits(cfg); if (cfg->use_systemd && cfg->do_daemonize) { log_warn("use-systemd and do-daemonize should not be enabled at the same time"); } log_ident_set_fromdefault(cfg, log_default_identity); }
/** * Init service. Keeps calling status pending to tell service control * manager that this process is not hanging. * @param r: restart, true on restart * @param d: daemon returned here. * @param c: config file returned here. * @return false if failed. */ static int service_init(int r, struct daemon** d, struct config_file** c) { struct config_file* cfg = NULL; struct daemon* daemon = NULL; if(!service_cfgfile) { char* newf = lookup_reg_str("Software\\Unbound", "ConfigFile"); if(newf) service_cfgfile = newf; else service_cfgfile = strdup(CONFIGFILE); if(!service_cfgfile) fatal_exit("out of memory"); } /* create daemon */ if(r) daemon = *d; else daemon = daemon_init(); if(!daemon) return 0; if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2800); /* read config */ cfg = config_create(); if(!cfg) return 0; if(!config_read(cfg, service_cfgfile, daemon->chroot)) { if(errno != ENOENT) { log_err("error in config file"); return 0; } log_warn("could not open config file, using defaults"); } if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2600); verbose(VERB_QUERY, "winservice - apply settings"); /* apply settings and init */ verbosity = cfg->verbosity + service_cmdline_verbose; if(cfg->directory && cfg->directory[0]) { if(chdir(cfg->directory)) { log_err("could not chdir to %s: %s", cfg->directory, strerror(errno)); if(errno != ENOENT) return 0; log_warn("could not change directory - continuing"); } else verbose(VERB_QUERY, "chdir to %s", cfg->directory); } log_init(cfg->logfile, cfg->use_syslog, cfg->chrootdir); if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2400); verbose(VERB_QUERY, "winservice - apply cfg"); daemon_apply_cfg(daemon, cfg); if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2300); if(!(daemon->rc = daemon_remote_create(cfg))) { log_err("could not set up remote-control"); daemon_delete(daemon); config_delete(cfg); return 0; } /* open ports */ /* keep reporting that we are busy starting */ if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2200); verbose(VERB_QUERY, "winservice - open ports"); if(!daemon_open_shared_ports(daemon)) return 0; verbose(VERB_QUERY, "winservice - ports opened"); if(!r) report_status(SERVICE_START_PENDING, NO_ERROR, 2000); *d = daemon; *c = cfg; return 1; }
/** check file descriptor count */ static void checkrlimits(struct config_file* cfg) { #ifdef HAVE_GETRLIMIT /* list has number of ports to listen to, ifs number addresses */ int list = ((cfg->do_udp?1:0) + (cfg->do_tcp?1 + (int)cfg->incoming_num_tcp:0)); size_t listen_ifs = (size_t)(cfg->num_ifs==0? ((cfg->do_ip4 && !cfg->if_automatic?1:0) + (cfg->do_ip6?1:0)):cfg->num_ifs); size_t listen_num = list*listen_ifs; size_t outudpnum = (size_t)cfg->outgoing_num_ports; size_t outtcpnum = cfg->outgoing_num_tcp; size_t misc = 4; /* logfile, pidfile, stdout... */ size_t perthread_noudp = listen_num + outtcpnum + 2/*cmdpipe*/ + 2/*libevent*/ + misc; size_t perthread = perthread_noudp + outudpnum; #if !defined(HAVE_PTHREAD) && !defined(HAVE_SOLARIS_THREADS) int numthread = 1; /* it forks */ #else int numthread = (cfg->num_threads?cfg->num_threads:1); #endif size_t total = numthread * perthread + misc; size_t avail; struct rlimit rlim; if(total > 1024 && strncmp(event_get_version(), "mini-event", 10) == 0) { log_warn("too many file descriptors requested. The builtin" "mini-event cannot handle more than 1024. Config " "for less fds or compile with libevent"); if(numthread*perthread_noudp+15 > 1024) fatal_exit("too much tcp. not enough fds."); cfg->outgoing_num_ports = (int)((1024 - numthread*perthread_noudp - 10 /* safety margin */) /numthread); log_warn("continuing with less udp ports: %u", cfg->outgoing_num_ports); total = 1024; } if(perthread > 64 && strncmp(event_get_version(), "winsock-event", 13) == 0) { log_err("too many file descriptors requested. The winsock" " event handler cannot handle more than 64 per " " thread. Config for less fds"); if(perthread_noudp+2 > 64) fatal_exit("too much tcp. not enough fds."); cfg->outgoing_num_ports = (int)((64 - perthread_noudp - 2/* safety margin */)); log_warn("continuing with less udp ports: %u", cfg->outgoing_num_ports); total = numthread*(perthread_noudp+ (size_t)cfg->outgoing_num_ports)+misc; } if(getrlimit(RLIMIT_NOFILE, &rlim) < 0) { log_warn("getrlimit: %s", strerror(errno)); return; } if(rlim.rlim_cur == (rlim_t)RLIM_INFINITY) return; if((size_t)rlim.rlim_cur < total) { avail = (size_t)rlim.rlim_cur; rlim.rlim_cur = (rlim_t)(total + 10); rlim.rlim_max = (rlim_t)(total + 10); #ifdef HAVE_SETRLIMIT if(setrlimit(RLIMIT_NOFILE, &rlim) < 0) { log_warn("setrlimit: %s", strerror(errno)); #else if(1) { #endif log_warn("cannot increase max open fds from %u to %u", (unsigned)avail, (unsigned)total+10); /* check that calculation below does not underflow, * with 15 as margin */ if(numthread*perthread_noudp+15 > avail) fatal_exit("too much tcp. not enough fds."); cfg->outgoing_num_ports = (int)((avail - numthread*perthread_noudp - 10 /* safety margin */) /numthread); log_warn("continuing with less udp ports: %u", cfg->outgoing_num_ports); log_warn("increase ulimit or decrease threads, " "ports in config to remove this warning"); return; } log_warn("increased limit(open files) from %u to %u", (unsigned)avail, (unsigned)total+10); } #else (void)cfg; #endif /* HAVE_GETRLIMIT */ } /** set verbosity, check rlimits, cache settings */ static void apply_settings(struct daemon* daemon, struct config_file* cfg, int cmdline_verbose) { /* apply if they have changed */ verbosity = cmdline_verbose + cfg->verbosity; daemon_apply_cfg(daemon, cfg); checkrlimits(cfg); }