+++ /dev/null
-/*
- * daemon/daemon.h - collection of workers that handles requests.
- *
- * Copyright (c) 2007, NLnet Labs. All rights reserved.
- *
- * This software is open source.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * Neither the name of the NLNET LABS nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * \file
- *
- * The daemon consists of global settings and a number of workers.
- */
-
-#ifndef DAEMON_H
-#define DAEMON_H
-
-#include "util/locks.h"
-#include "util/alloc.h"
-#include "services/modstack.h"
-struct config_file;
-struct worker;
-struct listen_port;
-struct slabhash;
-struct module_env;
-struct rrset_cache;
-struct acl_list;
-struct local_zones;
-struct views;
-struct ub_randstate;
-struct daemon_remote;
-struct respip_set;
-struct shm_main_info;
-
-#include "dnstap/dnstap_config.h"
-#ifdef USE_DNSTAP
-struct dt_env;
-#endif
-
-#include "dnscrypt/dnscrypt_config.h"
-#ifdef USE_DNSCRYPT
-struct dnsc_env;
-#endif
-
-/**
- * Structure holding worker list.
- * Holds globally visible information.
- */
-struct daemon {
- /** The config settings */
- struct config_file* cfg;
- /** the chroot dir in use, NULL if none */
- char* chroot;
- /** pidfile that is used */
- char* pidfile;
- /** port number that has ports opened. */
- int listening_port;
- /** array of listening ports, opened. Listening ports per worker,
- * or just one element[0] shared by the worker threads. */
- struct listen_port** ports;
- /** size of ports array */
- size_t num_ports;
- /** reuseport is enabled if true */
- int reuseport;
- /** port number for remote that has ports opened. */
- int rc_port;
- /** listening ports for remote control */
- struct listen_port* rc_ports;
- /** remote control connections management (for first worker) */
- struct daemon_remote* rc;
- /** ssl context for listening to dnstcp over ssl, and connecting ssl */
- void* listen_sslctx, *connect_sslctx;
- /** num threads allocated */
- int num;
- /** the worker entries */
- struct worker** workers;
- /** do we need to exit unbound (or is it only a reload?) */
- int need_to_exit;
- /** master random table ; used for port div between threads on reload*/
- struct ub_randstate* rand;
- /** master allocation cache */
- struct alloc_cache superalloc;
- /** the module environment master value, copied and changed by threads*/
- struct module_env* env;
- /** stack of module callbacks */
- struct module_stack mods;
- /** access control, which client IPs are allowed to connect */
- struct acl_list* acl;
- /** TCP connection limit, limit connections from client IPs */
- struct tcl_list* tcl;
- /** local authority zones */
- struct local_zones* local_zones;
- /** last time of statistics printout */
- struct timeval time_last_stat;
- /** time when daemon started */
- struct timeval time_boot;
- /** views structure containing view tree */
- struct views* views;
-#ifdef USE_DNSTAP
- /** the dnstap environment master value, copied and changed by threads*/
- struct dt_env* dtenv;
-#endif
- struct shm_main_info* shm_info;
- /** response-ip set with associated actions and tags. */
- struct respip_set* respip_set;
- /** some response-ip tags or actions are configured if true */
- int use_response_ip;
- /** some RPZ policies are configured */
- int use_rpz;
-#ifdef USE_DNSCRYPT
- /** the dnscrypt environment */
- struct dnsc_env* dnscenv;
-#endif
-};
-
-/**
- * Initialize daemon structure.
- * @return: The daemon structure, or NULL on error.
- */
-struct daemon* daemon_init(void);
-
-/**
- * Open shared listening ports (if needed).
- * The cfg member pointer must have been set for the daemon.
- * @param daemon: the daemon.
- * @return: false on error.
- */
-int daemon_open_shared_ports(struct daemon* daemon);
-
-/**
- * Fork workers and start service.
- * When the routine exits, it is no longer forked.
- * @param daemon: the daemon.
- */
-void daemon_fork(struct daemon* daemon);
-
-/**
- * Close off the worker thread information.
- * Bring the daemon back into state ready for daemon_fork again.
- * @param daemon: the daemon.
- */
-void daemon_cleanup(struct daemon* daemon);
-
-/**
- * Delete workers, close listening ports.
- * @param daemon: the daemon.
- */
-void daemon_delete(struct daemon* daemon);
-
-/**
- * Apply config settings.
- * @param daemon: the daemon.
- * @param cfg: new config settings.
- */
-void daemon_apply_cfg(struct daemon* daemon, struct config_file* cfg);
-
-#endif /* DAEMON_H */
+++ /dev/null
-/*
- * util/shm_side/shm_main.c - SHM for statistics transport
- *
- * Copyright (c) 2017, NLnet Labs. All rights reserved.
- *
- * This software is open source.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * Neither the name of the NLNET LABS nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * \file
- *
- * This file contains functions for the SHM implementation.
- */
-
-#include "config.h"
-#include <ctype.h>
-#include <stdarg.h>
-#ifdef HAVE_SYS_IPC_H
-#include <sys/ipc.h>
-#endif
-#ifdef HAVE_SYS_SHM_H
-#include <sys/shm.h>
-#endif
-#include <sys/time.h>
-#include <errno.h>
-#include "shm_main.h"
-#include "daemon/daemon.h"
-#include "daemon/worker.h"
-#include "daemon/stats.h"
-#include "services/mesh.h"
-#include "services/cache/rrset.h"
-#include "services/cache/infra.h"
-#include "validator/validator.h"
-#include "util/config_file.h"
-#include "util/fptr_wlist.h"
-#include "util/log.h"
-
-#ifdef HAVE_SHMGET
-/** subtract timers and the values do not overflow or become negative */
-static void
-stat_timeval_subtract(long long *d_sec, long long *d_usec, const struct timeval* end,
- const struct timeval* start)
-{
-#ifndef S_SPLINT_S
- time_t end_usec = end->tv_usec;
- *d_sec = end->tv_sec - start->tv_sec;
- if(end_usec < start->tv_usec) {
- end_usec += 1000000;
- (*d_sec)--;
- }
- *d_usec = end_usec - start->tv_usec;
-#endif
-}
-#endif /* HAVE_SHMGET */
-
-int shm_main_init(struct daemon* daemon)
-{
-#ifdef HAVE_SHMGET
- struct ub_shm_stat_info *shm_stat;
- size_t shm_size;
-
- /* sanitize */
- if(!daemon)
- return 0;
- if(!daemon->cfg->shm_enable)
- return 1;
- if(daemon->cfg->stat_interval == 0)
- log_warn("shm-enable is yes but statistics-interval is 0");
-
- /* Statistics to maintain the number of thread + total */
- shm_size = (sizeof(struct ub_stats_info) * (daemon->num + 1));
-
- /* Allocation of needed memory */
- daemon->shm_info = (struct shm_main_info*)calloc(1, shm_size);
-
- /* Sanitize */
- if(!daemon->shm_info) {
- log_err("shm fail: malloc failure");
- return 0;
- }
-
- daemon->shm_info->key = daemon->cfg->shm_key;
-
- /* Check for previous create SHM */
- daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(int), SHM_R);
- daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, sizeof(int), SHM_R);
-
- /* Destroy previous SHM */
- if (daemon->shm_info->id_ctl >= 0)
- shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
-
- /* Destroy previous SHM */
- if (daemon->shm_info->id_arr >= 0)
- shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
-
- /* SHM: Create the segment */
- daemon->shm_info->id_ctl = shmget(daemon->shm_info->key, sizeof(struct ub_shm_stat_info), IPC_CREAT | 0644);
-
- if (daemon->shm_info->id_ctl < 0)
- {
- log_err("SHM failed(id_ctl) cannot shmget(key %d) %s",
- daemon->shm_info->key, strerror(errno));
-
- /* Just release memory unused */
- free(daemon->shm_info);
- daemon->shm_info = NULL;
-
- return 0;
- }
-
- daemon->shm_info->id_arr = shmget(daemon->shm_info->key + 1, shm_size, IPC_CREAT | 0644);
-
- if (daemon->shm_info->id_arr < 0)
- {
- log_err("SHM failed(id_arr) cannot shmget(key %d + 1) %s",
- daemon->shm_info->key, strerror(errno));
-
- /* Just release memory unused */
- free(daemon->shm_info);
- daemon->shm_info = NULL;
-
- return 0;
- }
-
- /* SHM: attach the segment */
- daemon->shm_info->ptr_ctl = (struct ub_shm_stat_info*)
- shmat(daemon->shm_info->id_ctl, NULL, 0);
- if(daemon->shm_info->ptr_ctl == (void *) -1) {
- log_err("SHM failed(ctl) cannot shmat(%d) %s",
- daemon->shm_info->id_ctl, strerror(errno));
-
- /* Just release memory unused */
- free(daemon->shm_info);
- daemon->shm_info = NULL;
-
- return 0;
- }
-
- daemon->shm_info->ptr_arr = (struct ub_stats_info*)
- shmat(daemon->shm_info->id_arr, NULL, 0);
-
- if (daemon->shm_info->ptr_arr == (void *) -1)
- {
- log_err("SHM failed(arr) cannot shmat(%d) %s",
- daemon->shm_info->id_arr, strerror(errno));
-
- /* Just release memory unused */
- free(daemon->shm_info);
- daemon->shm_info = NULL;
-
- return 0;
- }
-
- /* Zero fill SHM to stand clean while is not filled by other events */
- memset(daemon->shm_info->ptr_ctl, 0, sizeof(struct ub_shm_stat_info));
- memset(daemon->shm_info->ptr_arr, 0, shm_size);
-
- shm_stat = daemon->shm_info->ptr_ctl;
- shm_stat->num_threads = daemon->num;
-
-#else
- (void)daemon;
-#endif /* HAVE_SHMGET */
- return 1;
-}
-
-void shm_main_shutdown(struct daemon* daemon)
-{
-#ifdef HAVE_SHMGET
- /* web are OK, just disabled */
- if(!daemon->cfg->shm_enable)
- return;
-
- verbose(VERB_DETAIL, "SHM shutdown - KEY [%d] - ID CTL [%d] ARR [%d] - PTR CTL [%p] ARR [%p]",
- daemon->shm_info->key, daemon->shm_info->id_ctl, daemon->shm_info->id_arr, daemon->shm_info->ptr_ctl, daemon->shm_info->ptr_arr);
-
- /* Destroy previous SHM */
- if (daemon->shm_info->id_ctl >= 0)
- shmctl(daemon->shm_info->id_ctl, IPC_RMID, NULL);
-
- if (daemon->shm_info->id_arr >= 0)
- shmctl(daemon->shm_info->id_arr, IPC_RMID, NULL);
-
- if (daemon->shm_info->ptr_ctl)
- shmdt(daemon->shm_info->ptr_ctl);
-
- if (daemon->shm_info->ptr_arr)
- shmdt(daemon->shm_info->ptr_arr);
-
- free(daemon->shm_info);
- daemon->shm_info = NULL;
-#else
- (void)daemon;
-#endif /* HAVE_SHMGET */
-}
-
-void shm_main_run(struct worker *worker)
-{
-#ifdef HAVE_SHMGET
- struct ub_shm_stat_info *shm_stat;
- struct ub_stats_info *stat_total;
- struct ub_stats_info *stat_info;
- int offset;
-
-#ifndef S_SPLINT_S
- verbose(VERB_DETAIL, "SHM run - worker [%d] - daemon [%p] - timenow(%u) - timeboot(%u)",
- worker->thread_num, worker->daemon, (unsigned)worker->env.now_tv->tv_sec, (unsigned)worker->daemon->time_boot.tv_sec);
-#endif
-
- offset = worker->thread_num + 1;
- stat_total = worker->daemon->shm_info->ptr_arr;
- stat_info = worker->daemon->shm_info->ptr_arr + offset;
-
- /* Copy data to the current position */
- server_stats_compile(worker, stat_info, 0);
-
- /* First thread, zero fill total, and copy general info */
- if (worker->thread_num == 0) {
-
- /* Copy data to the current position */
- memset(stat_total, 0, sizeof(struct ub_stats_info));
-
- /* Point to data into SHM */
-#ifndef S_SPLINT_S
- shm_stat = worker->daemon->shm_info->ptr_ctl;
- shm_stat->time.now_sec = (long long)worker->env.now_tv->tv_sec;
- shm_stat->time.now_usec = (long long)worker->env.now_tv->tv_usec;
-#endif
-
- stat_timeval_subtract(&shm_stat->time.up_sec, &shm_stat->time.up_usec, worker->env.now_tv, &worker->daemon->time_boot);
- stat_timeval_subtract(&shm_stat->time.elapsed_sec, &shm_stat->time.elapsed_usec, worker->env.now_tv, &worker->daemon->time_last_stat);
-
- shm_stat->mem.msg = (long long)slabhash_get_mem(worker->env.msg_cache);
- shm_stat->mem.rrset = (long long)slabhash_get_mem(&worker->env.rrset_cache->table);
- shm_stat->mem.dnscrypt_shared_secret = 0;
-#ifdef USE_DNSCRYPT
- if(worker->daemon->dnscenv) {
- shm_stat->mem.dnscrypt_shared_secret = (long long)slabhash_get_mem(
- worker->daemon->dnscenv->shared_secrets_cache);
- shm_stat->mem.dnscrypt_nonce = (long long)slabhash_get_mem(
- worker->daemon->dnscenv->nonces_cache);
- }
-#endif
- shm_stat->mem.val = (long long)mod_get_mem(&worker->env,
- "validator");
- shm_stat->mem.iter = (long long)mod_get_mem(&worker->env,
- "iterator");
- shm_stat->mem.respip = (long long)mod_get_mem(&worker->env,
- "respip");
-
- /* subnet mem value is available in shm, also when not enabled,
- * to make the struct easier to memmap by other applications,
- * independent of the configuration of unbound */
- shm_stat->mem.subnet = 0;
-#ifdef CLIENT_SUBNET
- shm_stat->mem.subnet = (long long)mod_get_mem(&worker->env,
- "subnetcache");
-#endif
- /* ipsecmod mem value is available in shm, also when not enabled,
- * to make the struct easier to memmap by other applications,
- * independent of the configuration of unbound */
- shm_stat->mem.ipsecmod = 0;
-#ifdef USE_IPSECMOD
- shm_stat->mem.ipsecmod = (long long)mod_get_mem(&worker->env,
- "ipsecmod");
-#endif
-#ifdef WITH_DYNLIBMODULE
- shm_stat->mem.dynlib = (long long)mod_get_mem(&worker->env,
- "dynlib");
-#endif
- }
-
- server_stats_add(stat_total, stat_info);
-
- /* print the thread statistics */
- stat_total->mesh_time_median /= (double)worker->daemon->num;
-
-#else
- (void)worker;
-#endif /* HAVE_SHMGET */
-}
+++ /dev/null
-/*
- * util/shm_side/shm_main.h - control the shared memory for unbound.
- *
- * Copyright (c) 2007, NLnet Labs. All rights reserved.
- *
- * This software is open source.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- *
- * Neither the name of the NLNET LABS nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
- * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-/**
- * \file
- *
- * This file contains functions for the SHM side.
- */
-
-#ifndef UTIL_SHM_SIDE_MAIN_H
-#define UTIL_SHM_SIDE_MAIN_H
-struct daemon;
-struct worker;
-
-/* get struct ub_shm_stat_info */
-#include "libunbound/unbound.h"
-
-/**
- * The SHM info.
- */
-struct shm_main_info {
- /** stats_info array, shared memory segment.
- * [0] is totals, [1..thread_num] are per-thread stats */
- struct ub_stats_info* ptr_arr;
- /** the global stats block, shared memory segment */
- struct ub_shm_stat_info* ptr_ctl;
- int key;
- int id_ctl;
- int id_arr;
-};
-
-int shm_main_init(struct daemon* daemon);
-void shm_main_shutdown(struct daemon* daemon);
-void shm_main_run(struct worker *worker);
-
-#endif /* UTIL_SHM_SIDE_MAIN_H */