main.c revision 25b95d31ce658326510bef979cf2909c6eddc428
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Copyright (C) 1999-2003 Internet Software Consortium.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * Permission to use, copy, modify, and/or distribute this software for any
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * copyright notice and this permission notice appear in all copies.
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
68a8db1efb0cd13e0e3d59362ab9db1a4a8bc496Andreas Gustafsson * PERFORMANCE OF THIS SOFTWARE.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/* $Id: main.c,v 1.187 2012/02/06 23:46:44 tbox Exp $ */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Defining NS_MAIN provides storage declarations (rather than extern)
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * for variables in named/globals.h.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#include <named/globals.h> /* Explicit, though named/log.h includes it. */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Include header files for database drivers here.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson/* #include "xxdb.h" */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Include contributed DLZ drivers if appropriate.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * The maximum number of stack frames to dump on assertion failure.
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrewsstatic char program_name[ISC_DIR_NAMEMAX] = "named";
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrewsstatic unsigned int maxsocks = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonstatic int maxudp = 0;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonns_main_earlywarning(const char *format, ...) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonns_main_earlyfatal(const char *format, ...) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "exiting (due to early fatal error)");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonassertion_failed(const char *file, int line, isc_assertiontype_t type,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson const char *cond) ISC_PLATFORM_NORETURN_POST;
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrewsassertion_failed(const char *file, int line, isc_assertiontype_t type,
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews const char *cond)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Handle assertion failures.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Reset the assertion callback in case it is the log
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * routines causing the assertion.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (result == ISC_R_SUCCESS && nframes > 0)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_assertion_typetotext(type), cond, logsuffix);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 for (i = 0; i < nframes; i++) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 unsigned long offset;
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 result = isc_backtrace_getsymbol(tracebuf[i],
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "#%d %p in %s()+0x%lx", i,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "#%d %p in ??", i,
e2cf0e8ff9ff37121518af5b34b9e4de7abbb47cMark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "exiting (due to assertion failure)");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "%s:%d: %s(%s) failed\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson file, line, isc_assertion_typetotext(type), cond);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonlibrary_fatal_error(const char *file, int line, const char *format,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas GustafssonISC_FORMAT_PRINTF(3, 0) ISC_PLATFORM_NORETURN_POST;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonlibrary_fatal_error(const char *file, int line, const char *format,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Handle isc_error_fatal() calls from our libraries.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Reset the error callback in case it is the log
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * routines causing the assertion.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "exiting (due to fatal error in library)");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "%s:%d: fatal error: ", file, line);
66f51d8678c8fcc7256354b599317f67455120fdMark Andrewslibrary_unexpected_error(const char *file, int line, const char *format,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonlibrary_unexpected_error(const char *file, int line, const char *format,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Handle isc_error_unexpected() calls from our libraries.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson fprintf(stderr, "%s:%d: fatal error: ", file, line);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "[-d debuglevel]\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson " [-f|-g] [-n number_of_cpus] [-p port] "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "[-P listen-port] [-s]\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson " [-t chrootdir] [-u username] [-i pidfile]\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson " [-m {usage|trace|record|size|mctx}]\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "usage: named [-4|-6] [-c conffile] [-d debuglevel] "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "[-E engine] [-f|-g]\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson " [-n number_of_cpus] [-p port] [-s] "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "[-t chrootdir] [-u username]\n"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson " [-m {usage|trace|record|size|mctx}]\n");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonsave_command_line(int argc, char *argv[]) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson eob = saved_command_line + sizeof(saved_command_line);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson for (i = 1; i < argc && dst < eob; i++) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * This won't perfectly produce a shell-independent
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * pastable command line in all circumstances, but
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * comes close, and for practical purposes will
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * nearly always be fine.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson INSIST(sizeof(saved_command_line) >= sizeof(truncated));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson strcpy(eob - sizeof(truncated), truncated);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ns_main_earlyfatal("%s '%s' out of range", desc, arg);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonset_flags(const char *arg, struct flag_def *defs, unsigned int *ret) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 for (def = defs; def->name != NULL; def++) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉parse_command_line(int argc, char *argv[]) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson while ((ch = isc_commandline_parse(argc, argv,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "46c:C:d:E:fFgi:lm:n:N:p:P:"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("cannot specify -4 and -6");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (isc_net_probeipv4() != ISC_R_SUCCESS)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("IPv4 not supported by OS");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("cannot specify -4 and -6");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (isc_net_probeipv6() != ISC_R_SUCCESS)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("IPv6 not supported by OS");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_conffile = isc_commandline_argument;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson lwresd_g_conffile = isc_commandline_argument;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("cannot specify -c and -C");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson lwresd_g_resolvconffile = isc_commandline_argument;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("cannot specify -c and -C");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_debuglevel = parse_int(isc_commandline_argument,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "debug level");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* XXXBEW -i should be removed */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson lwresd_g_defaultpidfile = isc_commandline_argument;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson set_flags(isc_commandline_argument, mem_debug_flags,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_cpus = parse_int(isc_commandline_argument,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "number of cpus");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson port = parse_int(isc_commandline_argument, "port");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("port '%s' out of range",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* XXXBEW Should -P be removed? */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson port = parse_int(isc_commandline_argument, "port");
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson ns_main_earlyfatal("port '%s' out of range",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* XXXRTH temporary syntax */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson maxsocks = parse_int(isc_commandline_argument,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "max number of sockets");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* XXXJAB should we make a copy? */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_chrootdir = isc_commandline_argument;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * clienttest: make clients single shot with their
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * own memory context.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * delay=xxxx: delay client responses by xxxx ms to
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * simulate remote servers.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (!strcmp(isc_commandline_argument, "clienttest"))
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews else if (!strcmp(isc_commandline_argument, "nosoa"))
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strcmp(isc_commandline_argument, "noaa"))
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strcmp(isc_commandline_argument, "maxudp512"))
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strcmp(isc_commandline_argument, "maxudp1460"))
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strcmp(isc_commandline_argument, "dropedns"))
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strcmp(isc_commandline_argument, "noedns"))
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strncmp(isc_commandline_argument,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 maxudp = atoi(isc_commandline_argument + 7);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 else if (!strncmp(isc_commandline_argument,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_delay = atoi(isc_commandline_argument + 6);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson else if (!strcmp(isc_commandline_argument, "nosyslog"))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_udpdisp = parse_int(isc_commandline_argument,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "number of UDP listeners "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "per interface");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_username = isc_commandline_argument;
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson printf("BIND %s <id:%s> built with %s\n",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_version, ns_g_srcid, ns_g_configargs);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* Reserved for FIPS mode */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* FALLTHROUGH */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("unknown option '-%c'",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* FALLTHROUGH */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("parsing options returned %d", ch);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("extra command line arguments");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ns_g_udpdisp == 0 || ns_g_udpdisp > ns_g_cpus)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ISC_LOG_INFO, "using %u UDP listener%s per interface",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_g_udpdisp, ns_g_udpdisp == 1 ? "" : "s");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "isc_taskmgr_create() failed: %s",
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
8ac1acc30d0f405222ffa7b2b93131d9d4e18599Mark Andrews "isc_timermgr_create() failed: %s",
81e805888bcd4bd2a6d5c986fade6ffdf7d88a54Andreas Gustafsson result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "isc_socketmgr_create() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 isc__socketmgr_maxudp(ns_g_socketmgr, maxudp);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
66f51d8678c8fcc7256354b599317f67455120fdMark Andrews ISC_LOG_INFO, "using up to %u sockets", socks);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "isc_entropy_create() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "isc_hash_create() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 isc_entropy_detach(&ns_g_fallbackentropy);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * isc_taskmgr_destroy() will block until all tasks have exited,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * isc_hash_destroy() cannot be called as long as a resolver may be
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * running. Calling this after isc_taskmgr_destroy() ensures the
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * call is safe.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99)))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = isc_backtrace_getsymbolfromindex(i, &addr, &fname);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Get the user and group information before changing the root
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * directory, so the administrator does not need to keep a copy
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * of the user and group databases in the chroot'ed environment.
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Initialize time conversion information
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* Check if named is under smf control, before chroot. */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* We don't care about instance, just check if we got one. */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉#endif /* HAVE_LIBSCF */
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Initialize system's random device as fallback entropy source
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * if running chroot'ed.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ns_main_earlyfatal("isc_entropy_create() failed: %s",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 result = isc_entropy_createfilesource(ns_g_fallbackentropy,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ns_main_earlywarning("could not open pre-chroot "
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "entropy source %s: %s",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_entropy_detach(&ns_g_fallbackentropy);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Check for the number of cpu's before ns_os_chroot().
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * For operating systems which have a capability mechanism, now
1123872a47604644f6d74d7d8929472201db96f3Andreas Gustafsson * is the time to switch to minimal privs and change our user id.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * On traditional UNIX systems, this call will be a no-op, and we
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * will change the user ID after reading the config file the first
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * time. (We need to read the config file to know which possibly
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * privileged ports to bind() to.)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = ns_log_init(ISC_TF(ns_g_username != NULL));
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ns_main_earlyfatal("ns_log_init() failed: %s",
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Now is the time to daemonize (if we're not running in the
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * foreground). We waited until now because we wanted to get
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * a valid logging context setup. We cannot daemonize any later,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * because calling create_managers() will create threads, which
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * would be lost after fork().
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * We call isc_app_start() here as some versions of FreeBSD's fork()
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * destroys all the signal handling it sets up.
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ns_main_earlyfatal("isc_app_start() failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "----------------------------------------------------");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "BIND 9 is maintained by Internet Systems Consortium,");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "Inc. (ISC), a non-profit 501(c)(3) public-benefit ");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "corporation. Support and training for BIND 9 are ");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "available at https://www.isc.org/support");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "----------------------------------------------------");
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Get the initial resource limits.
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 (void)isc_resource_getlimit(isc_resource_stacksize,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 (void)isc_resource_getlimit(isc_resource_datasize,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 (void)isc_resource_getlimit(isc_resource_coresize,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 (void)isc_resource_getlimit(isc_resource_openfiles,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * System resources cannot effectively be tuned on some systems.
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 * Raise the limit in such cases for safety.
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 (void)isc_resource_getlimit(isc_resource_openfiles,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 if (old_openfiles != ns_g_initopenfiles) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "adjusted limit on open files from "
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * If the named configuration filename is relative, prepend the current
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * directory's name before possibly changing to another directory.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if (! isc_file_isabsolute(ns_g_conffile)) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 result = isc_file_absolutepath(ns_g_conffile,
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ns_main_earlyfatal("could not construct absolute path "
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "of configuration file: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Record the server's startup time.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ns_main_earlyfatal("isc_time_now() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ns_main_earlyfatal("create_managers() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Add calls to register sdb drivers here.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 /* xxdb_init(); */
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Register the DLZ "dlopen" driver.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ns_main_earlyfatal("dlz_dlopen_init() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Register any other contributed DLZ drivers.
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 ns_main_earlyfatal("dlz_drivers_init() failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_server_create(ns_g_mctx, &ns_g_server);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Add calls to unregister sdb drivers here.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson /* xxdb_clear(); */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Unregister contributed DLZ drivers.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Unregister "dlopen" DLZ driver.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉ns_main_setmemstats(const char *filename) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Caller has to ensure locking.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Get FMRI for the named process.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafssonns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 REQUIRE(ins_name != NULL && *ins_name == NULL);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "scf_handle_create() failed: %s",
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 "scf_handle_bind() failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((namelen = scf_myname(h, NULL, 0)) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "scf_myname() failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
f620c5e527746a2ec3d90a11d21abd8a114746dfTatuya JINMEI 神明達哉 "ns_smf_get_instance memory "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "allocation failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (scf_myname(h, instance, namelen + 1) == -1) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "scf_myname() failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#endif /* HAVE_LIBSCF */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Record version in core image.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * strings named.core | grep "named version:"
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#if defined(NO_VERSION_DATE) || !defined(__DATE__)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "named version: BIND " VERSION " <" SRCID ">",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "named version: BIND " VERSION " <" SRCID "> (" __DATE__ ")",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = isc_file_progname(*argv, program_name, sizeof(program_name));
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("program name too long");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("failed to build internal symbol table");
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_assertion_setcallback(assertion_failed);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_error_setunexpected(library_unexpected_error);
b89095ba15e9163f2bacc46239d8f69d1a2b9ad3Tatuya JINMEI 神明達哉 * Warn about common configuration error.
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlywarning("config filename (-c %s) contains "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "chroot path (-t %s)",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = isc_mem_create(0, 0, &ns_g_mctx);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson ns_main_earlyfatal("isc_mem_create() failed: %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson isc_mem_setname(ns_g_mctx, "main", NULL);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson * Start things running and then wait for a shutdown request
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "isc_app_run(): %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (result == ISC_R_SUCCESS && instance != NULL) {
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (smf_disable_instance(instance, 0) != 0)
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "smf_disable_instance() "
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson "failed for %s : %s",
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson#endif /* HAVE_LIBSCF */
4848fe4ad2c0ba6e2e69e4a2617727f8556d79a0Andreas Gustafsson if (ns_g_memstatistics && memstats != NULL) {