main.c revision 6979ebf549b9c0ccd115bbf8c0d905600086f292
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington/*
499b34cea04a46823d003d4c0520c8b03e8513cbBrian Wellington * Copyright (C) 2004-2014 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * Copyright (C) 1999-2003 Internet Software Consortium.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington *
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * Permission to use, copy, modify, and/or distribute this software for any
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * purpose with or without fee is hereby granted, provided that the above
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence * copyright notice and this permission notice appear in all copies.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
15a44745412679c30a6d022733925af70a38b715David Lawrence * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
15a44745412679c30a6d022733925af70a38b715David Lawrence * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
15a44745412679c30a6d022733925af70a38b715David Lawrence * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15a44745412679c30a6d022733925af70a38b715David Lawrence * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15a44745412679c30a6d022733925af70a38b715David Lawrence * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PERFORMANCE OF THIS SOFTWARE.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington/*! \file */
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <config.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <ctype.h>
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington#include <stdlib.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <string.h>
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/app.h>
6734ac097f78601a8194b06360e62e043dcffdb8Brian Wellington#include <isc/backtrace.h>
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington#include <isc/commandline.h>
6028d1ce0380d0ba7f6c6ecd1ad20b31ddd1becbDavid Lawrence#include <isc/dir.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <isc/entropy.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/file.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/hash.h>
a5e73f3e9d909e86baca18713c5697f52aca87caBrian Wellington#include <isc/os.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/platform.h>
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington#include <isc/print.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/resource.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/stdio.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/string.h>
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington#include <isc/task.h>
09f22ac5b09e70bc526015f37168ba33e21ea91fDavid Lawrence#include <isc/timer.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isc/util.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <isccc/result.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
92ef1a9b9dbd48ecb507b42ac62c15afefdaf838David Lawrence#include <dns/dispatch.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <dns/name.h>
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#include <dns/result.h>
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington#include <dns/view.h>
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington#include <dst/result.h>
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington#ifdef PKCS11CRYPTO
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington#include <pk11/result.h>
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington#endif
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington#include <dlz/dlz_dlopen_driver.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington/*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Defining NS_MAIN provides storage declarations (rather than extern)
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * for variables in named/globals.h.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#define NS_MAIN 1
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/builtin.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/control.h>
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington#include <named/globals.h> /* Explicit, though named/log.h includes it. */
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington#include <named/interfacemgr.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/log.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/os.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/server.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/lwresd.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/main.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/seccomp.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#ifdef HAVE_LIBSCF
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <named/ns_smf_globals.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#endif
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#ifdef OPENSSL
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <openssl/opensslv.h>
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington#endif
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington#ifdef HAVE_LIBXML2
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <libxml/xmlversion.h>
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington#endif
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington/*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Include header files for database drivers here.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson/* #include "xxdb.h" */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#ifdef CONTRIB_DLZ
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson/*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * Include contributed DLZ drivers if appropriate.
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson */
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#include <dlz/dlz_drivers.h>
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson#endif
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington/*
4755b174df8221dff7e872f21d42b3572a74bf2fAndreas Gustafsson * The maximum number of stack frames to dump on assertion failure.
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington */
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington#ifndef BACKTRACE_MAXFRAME
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington#define BACKTRACE_MAXFRAME 128
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssonextern int isc_dscp_check_value;
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafsson
76c8294c81fb48b1da6e1fc5b83322a4cedb8e58Andreas Gustafssonstatic isc_boolean_t want_stats = ISC_FALSE;
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic char program_name[ISC_DIR_NAMEMAX] = "named";
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic char absolute_conffile[ISC_DIR_PATHMAX];
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic char saved_command_line[512];
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic char version[512];
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic unsigned int maxsocks = 0;
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic int maxudp = 0;
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonvoid
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonns_main_earlywarning(const char *format, ...) {
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington va_list args;
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington va_start(args, format);
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington if (ns_g_lctx != NULL) {
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington format, args);
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington } else {
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington fprintf(stderr, "%s: ", program_name);
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington vfprintf(stderr, format, args);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington fprintf(stderr, "\n");
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington fflush(stderr);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington }
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington va_end(args);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington}
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonvoid
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellingtonns_main_earlyfatal(const char *format, ...) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington va_list args;
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington va_start(args, format);
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington if (ns_g_lctx != NULL) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington format, args);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington "exiting (due to early fatal error)");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington } else {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington fprintf(stderr, "%s: ", program_name);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington vfprintf(stderr, format, args);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington fprintf(stderr, "\n");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington fflush(stderr);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
d2524f38d22998efb4196410643280b14f8b6febBob Halley va_end(args);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington exit(1);
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington}
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian WellingtonISC_PLATFORM_NORETURN_PRE static void
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellingtonassertion_failed(const char *file, int line, isc_assertiontype_t type,
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington const char *cond) ISC_PLATFORM_NORETURN_POST;
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellingtonstatic void
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellingtonassertion_failed(const char *file, int line, isc_assertiontype_t type,
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington const char *cond)
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington{
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington void *tracebuf[BACKTRACE_MAXFRAME];
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington int i, nframes;
0e5d6900bdfcbeef8919e6fb453ca6c44f62ccd8Brian Wellington isc_result_t result;
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington const char *logsuffix = "";
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington const char *fname;
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington /*
ff7b9eede951083d1f8a1ad919611659c3e20b34Brian Wellington * Handle assertion failures.
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington */
7863e6bd4396e99a82805feccb59275530670829Andreas Gustafsson
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington if (ns_g_lctx != NULL) {
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington /*
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington * Reset the assertion callback in case it is the log
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington * routines causing the assertion.
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington */
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington isc_assertion_setcallback(NULL);
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME,
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington &nframes);
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington if (result == ISC_R_SUCCESS && nframes > 0)
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington logsuffix = ", back trace";
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "%s:%d: %s(%s) failed%s", file, line,
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington isc_assertion_typetotext(type), cond, logsuffix);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington if (result == ISC_R_SUCCESS) {
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington for (i = 0; i < nframes; i++) {
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington unsigned long offset;
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington fname = NULL;
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington result = isc_backtrace_getsymbol(tracebuf[i],
4be64854b4a3da0465bf962caa8488699e0e0681Brian Wellington &fname,
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington &offset);
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington if (result == ISC_R_SUCCESS) {
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington isc_log_write(ns_g_lctx,
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington NS_LOGCATEGORY_GENERAL,
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington NS_LOGMODULE_MAIN,
4a2f65ad8a26261e758d9682d88eb29395422fb9Brian Wellington ISC_LOG_CRITICAL,
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington "#%d %p in %s()+0x%lx", i,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington tracebuf[i], fname,
49c8a96fba8d85810d470fdc7dd3388f0c767c9eBrian Wellington offset);
49c8a96fba8d85810d470fdc7dd3388f0c767c9eBrian Wellington } else {
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington isc_log_write(ns_g_lctx,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington NS_LOGCATEGORY_GENERAL,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington NS_LOGMODULE_MAIN,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington ISC_LOG_CRITICAL,
49c8a96fba8d85810d470fdc7dd3388f0c767c9eBrian Wellington "#%d %p in ??", i,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington tracebuf[i]);
1a2c699f0eb89fbd776a2dfabb6e197fe36a8c20Brian Wellington }
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington }
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington "exiting (due to assertion failure)");
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington } else {
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington fprintf(stderr, "%s:%d: %s(%s) failed\n",
ac335315cddc0a42b9235001197dcf719ae737b6Brian Wellington file, line, isc_assertion_typetotext(type), cond);
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington fflush(stderr);
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence if (ns_g_coreok)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington abort();
b6666e61dc9b91f4ac6af3aa1172bfd8a5f2d6ffBrian Wellington exit(1);
8447ecf6fd002e264c4746a10193916aa64dd8c7Brian Wellington}
70d950d16e9623fec1bc89b158047de507071ce3Brian Wellington
70d950d16e9623fec1bc89b158047de507071ce3Brian WellingtonISC_PLATFORM_NORETURN_PRE static void
70d950d16e9623fec1bc89b158047de507071ce3Brian Wellingtonlibrary_fatal_error(const char *file, int line, const char *format,
70d950d16e9623fec1bc89b158047de507071ce3Brian Wellington va_list args)
26a5f97dd8770ced729025488091b77d8beb0ab6Brian WellingtonISC_FORMAT_PRINTF(3, 0) ISC_PLATFORM_NORETURN_POST;
26a5f97dd8770ced729025488091b77d8beb0ab6Brian Wellington
70d950d16e9623fec1bc89b158047de507071ce3Brian Wellingtonstatic void
b6666e61dc9b91f4ac6af3aa1172bfd8a5f2d6ffBrian Wellingtonlibrary_fatal_error(const char *file, int line, const char *format,
b6666e61dc9b91f4ac6af3aa1172bfd8a5f2d6ffBrian Wellington va_list args)
b6666e61dc9b91f4ac6af3aa1172bfd8a5f2d6ffBrian Wellington{
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /*
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * Handle isc_error_fatal() calls from our libraries.
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington */
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington if (ns_g_lctx != NULL) {
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington /*
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington * Reset the error callback in case it is the log
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington * routines causing the assertion.
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_error_setfatal(NULL);
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington
ddbc279e7b6a2d6ba682e60ca12956406030054bBrian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "%s:%d: fatal error:", file, line);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington format, args);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington "exiting (due to fatal error in library)");
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington } else {
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington fprintf(stderr, "%s:%d: fatal error: ", file, line);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington vfprintf(stderr, format, args);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington fprintf(stderr, "\n");
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington fflush(stderr);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington }
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington if (ns_g_coreok)
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington abort();
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington exit(1);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington}
7863e6bd4396e99a82805feccb59275530670829Andreas Gustafsson
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellingtonstatic void
9d967288c374ad4988334cd575ba1cca2b69f70eBrian Wellingtonlibrary_unexpected_error(const char *file, int line, const char *format,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington va_list args) ISC_FORMAT_PRINTF(3, 0);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellingtonstatic void
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellingtonlibrary_unexpected_error(const char *file, int line, const char *format,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington va_list args)
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington{
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington /*
5c29047792191d6141f69b2684314d0b762fedebBrian Wellington * Handle isc_error_unexpected() calls from our libraries.
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington */
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington if (ns_g_lctx != NULL) {
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington "%s:%d: unexpected error:", file, line);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington format, args);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington } else {
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington fprintf(stderr, "%s:%d: fatal error: ", file, line);
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington vfprintf(stderr, format, args);
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington fprintf(stderr, "\n");
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington fflush(stderr);
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington }
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington}
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellingtonstatic void
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellingtonlwresd_usage(void) {
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington fprintf(stderr,
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
d0345e08f26267c1d11e02af57a6555868068415Brian Wellington "[-d debuglevel]\n"
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington " [-f|-g] [-n number_of_cpus] [-p port] "
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington "[-P listen-port] [-s]\n"
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington " [-t chrootdir] [-u username] [-i pidfile]\n"
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington " [-m {usage|trace|record|size|mctx}]\n");
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington}
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellingtonstatic void
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellingtonusage(void) {
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington if (ns_g_lwresdonly) {
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington lwresd_usage();
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington return;
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington }
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington fprintf(stderr,
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington "usage: named [-4|-6] [-c conffile] [-d debuglevel] "
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington "[-E engine] [-f|-g]\n"
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington " [-n number_of_cpus] [-p port] [-s] "
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington "[-t chrootdir] [-u username]\n"
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington " [-m {usage|trace|record|size|mctx}]\n");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
d0345e08f26267c1d11e02af57a6555868068415Brian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellingtonstatic void
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellingtonsave_command_line(int argc, char *argv[]) {
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington int i;
d0345e08f26267c1d11e02af57a6555868068415Brian Wellington char *src;
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington char *dst;
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington char *eob;
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington const char truncated[] = "...";
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington isc_boolean_t quoted = ISC_FALSE;
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington
d0345e08f26267c1d11e02af57a6555868068415Brian Wellington dst = saved_command_line;
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington eob = saved_command_line + sizeof(saved_command_line);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington for (i = 1; i < argc && dst < eob; i++) {
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington *dst++ = ' ';
d0345e08f26267c1d11e02af57a6555868068415Brian Wellington
d0345e08f26267c1d11e02af57a6555868068415Brian Wellington src = argv[i];
949d406b57fe80fabc6a60d36a0dcee927c780b3Brian Wellington while (*src != '\0' && dst < eob) {
949d406b57fe80fabc6a60d36a0dcee927c780b3Brian Wellington /*
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington * This won't perfectly produce a shell-independent
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington * pastable command line in all circumstances, but
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington * comes close, and for practical purposes will
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington * nearly always be fine.
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington */
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (quoted || isalnum(*src & 0xff) ||
949d406b57fe80fabc6a60d36a0dcee927c780b3Brian Wellington *src == '-' || *src == '_' ||
949d406b57fe80fabc6a60d36a0dcee927c780b3Brian Wellington *src == '.' || *src == '/') {
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington *dst++ = *src++;
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington quoted = ISC_FALSE;
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington } else {
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington *dst++ = '\\';
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington quoted = ISC_TRUE;
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington }
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington }
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington }
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington INSIST(sizeof(saved_command_line) >= sizeof(truncated));
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (dst == eob)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington strcpy(eob - sizeof(truncated), truncated);
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellington else
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington *dst = '\0';
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
3b2b306f47867d0037fb851623fb5a5736d64348Michael Graff
4556681e191b7c1654639895ce719d98f2822ee2Michael Graffstatic int
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonparse_int(char *arg, const char *desc) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington char *endp;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington int tmp;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington long int ltmp;
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington
d692d9991a731d60b63e6389da1ebf2b2839cfabBrian Wellington ltmp = strtol(arg, &endp, 10);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington tmp = (int) ltmp;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (*endp != '\0')
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington if (tmp < 0 || tmp != ltmp)
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington ns_main_earlyfatal("%s '%s' out of range", desc, arg);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return (tmp);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellingtonstatic struct flag_def {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence const char *name;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int value;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence} mem_debug_flags[] = {
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington { "trace", ISC_MEM_DEBUGTRACE },
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington { "record", ISC_MEM_DEBUGRECORD },
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington { "usage", ISC_MEM_DEBUGUSAGE },
c3a4d8072ccd3b33aa3fc84cdeadd1a6fac87e08Michael Graff { "size", ISC_MEM_DEBUGSIZE },
c3a4d8072ccd3b33aa3fc84cdeadd1a6fac87e08Michael Graff { "mctx", ISC_MEM_DEBUGCTX },
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington { NULL, 0 }
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington};
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellingtonstatic void
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellingtonset_flags(const char *arg, struct flag_def *defs, unsigned int *ret) {
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington for (;;) {
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington const struct flag_def *def;
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington const char *end = strchr(arg, ',');
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington int arglen;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (end == NULL)
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graff end = arg + strlen(arg);
19c7cce8555ccc0c95455a0c35dedd017d420d05Mark Andrews arglen = (int)(end - arg);
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington for (def = defs; def->name != NULL; def++) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (arglen == (int)strlen(def->name) &&
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington memcmp(arg, def->name, arglen) == 0) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington *ret |= def->value;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence goto found;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington }
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington found:
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington if (*end == '\0')
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington break;
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington arg = end + 1;
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington }
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington}
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic void
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrenceparse_command_line(int argc, char *argv[]) {
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington int ch;
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington int port;
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington const char *p;
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington save_command_line(argc, argv);
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington /* PLEASE keep options synchronized when main is hooked! */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#define CMDLINE_FLAGS "46c:C:d:D:E:fFgi:lL:m:n:N:p:P:sS:t:T:U:u:vVx:"
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_commandline_errprint = ISC_FALSE;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington switch (ch) {
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington case '4':
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington if (ns_g_disable4)
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington ns_main_earlyfatal("cannot specify -4 and -6");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (isc_net_probeipv4() != ISC_R_SUCCESS)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_main_earlyfatal("IPv4 not supported by OS");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_net_disableipv6();
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington ns_g_disable6 = ISC_TRUE;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews break;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington case '6':
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington if (ns_g_disable6)
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_main_earlyfatal("cannot specify -4 and -6");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (isc_net_probeipv6() != ISC_R_SUCCESS)
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington ns_main_earlyfatal("IPv6 not supported by OS");
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington isc_net_disableipv4();
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_g_disable4 = ISC_TRUE;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington break;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington case 'c':
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_g_conffile = isc_commandline_argument;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington lwresd_g_conffile = isc_commandline_argument;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington if (lwresd_g_useresolvconf)
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_main_earlyfatal("cannot specify -c and -C");
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_g_conffileset = ISC_TRUE;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington break;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington case 'C':
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington lwresd_g_resolvconffile = isc_commandline_argument;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington if (ns_g_conffileset)
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ns_main_earlyfatal("cannot specify -c and -C");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington lwresd_g_useresolvconf = ISC_TRUE;
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington break;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington case 'd':
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington ns_g_debuglevel = parse_int(isc_commandline_argument,
d84ce5d5c69a7e144fb90fd4b3c349e88e4dcdddBrian Wellington "debug level");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence break;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'D':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /* Descriptive comment for 'ps'. */
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence break;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case 'E':
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence ns_g_engine = isc_commandline_argument;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington break;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case 'f':
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ns_g_foreground = ISC_TRUE;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington break;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'g':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_g_foreground = ISC_TRUE;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_g_logstderr = ISC_TRUE;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence break;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /* XXXBEW -i should be removed */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington case 'i':
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington lwresd_g_defaultpidfile = isc_commandline_argument;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington break;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case 'l':
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_g_lwresdonly = ISC_TRUE;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence break;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'L':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_g_logfile = isc_commandline_argument;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington break;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington case 'm':
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington set_flags(isc_commandline_argument, mem_debug_flags,
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington &isc_mem_debugging);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington break;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington case 'N': /* Deprecated. */
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington case 'n':
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_g_cpus = parse_int(isc_commandline_argument,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence "number of cpus");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (ns_g_cpus == 0)
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_g_cpus = 1;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington break;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington case 'p':
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington port = parse_int(isc_commandline_argument, "port");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (port < 1 || port > 65535)
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_main_earlyfatal("port '%s' out of range",
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_commandline_argument);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_port = port;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington break;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington /* XXXBEW Should -P be removed? */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case 'P':
0c70ab306505d89983186e9f8bb8647de55b5d04Mark Andrews port = parse_int(isc_commandline_argument, "port");
0c70ab306505d89983186e9f8bb8647de55b5d04Mark Andrews if (port < 1 || port > 65535)
0c70ab306505d89983186e9f8bb8647de55b5d04Mark Andrews ns_main_earlyfatal("port '%s' out of range",
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington isc_commandline_argument);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence lwresd_g_listenport = port;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington break;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case 's':
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /* XXXRTH temporary syntax */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington want_stats = ISC_TRUE;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington break;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence case 'S':
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence maxsocks = parse_int(isc_commandline_argument,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "max number of sockets");
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington break;
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington case 't':
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington /* XXXJAB should we make a copy? */
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_g_chrootdir = isc_commandline_argument;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence break;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington case 'T': /* NOT DOCUMENTED */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * force the server to behave (or misbehave) in
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * specified ways for testing purposes.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence *
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * clienttest: make clients single shot with their
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * own memory context.
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington * delay=xxxx: delay client responses by xxxx ms to
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington * simulate remote servers.
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington * dscp=x: check that dscp values are as
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * expected and assert otherwise.
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (!strcmp(isc_commandline_argument, "clienttest"))
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_g_clienttest = ISC_TRUE;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else if (!strcmp(isc_commandline_argument, "nosoa"))
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_nosoa = ISC_TRUE;
81ab85fd39c3f6c781ecac725d830364a311479dBrian Wellington else if (!strcmp(isc_commandline_argument, "noaa"))
d5334bc18380d25e8a7ee804f68f22dc746b9c20Brian Wellington ns_g_noaa = ISC_TRUE;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington else if (!strcmp(isc_commandline_argument, "maxudp512"))
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington maxudp = 512;
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington else if (!strcmp(isc_commandline_argument, "maxudp1460"))
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington maxudp = 1460;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington else if (!strcmp(isc_commandline_argument, "dropedns"))
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_dropedns = ISC_TRUE;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else if (!strcmp(isc_commandline_argument, "noedns"))
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington ns_g_noedns = ISC_TRUE;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington else if (!strncmp(isc_commandline_argument,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "maxudp=", 7))
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington maxudp = atoi(isc_commandline_argument + 7);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington else if (!strncmp(isc_commandline_argument,
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington "delay=", 6))
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington ns_g_delay = atoi(isc_commandline_argument + 6);
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington else if (!strcmp(isc_commandline_argument, "nosyslog"))
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington ns_g_nosyslog = ISC_TRUE;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else if (!strcmp(isc_commandline_argument, "nonearest"))
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_nonearest = ISC_TRUE;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else if (!strncmp(isc_commandline_argument, "dscp=", 5))
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_dscp_check_value =
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington atoi(isc_commandline_argument + 5);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence fprintf(stderr, "unknown -T flag '%s\n",
0dc4e6a6aef01175b8cdd71cb757f09ba1e69c49Brian Wellington isc_commandline_argument);
2c12fc4d63f1d5f9d55fc0ecb198d582da6fd7d3Brian Wellington break;
c3a4d8072ccd3b33aa3fc84cdeadd1a6fac87e08Michael Graff case 'U':
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington ns_g_udpdisp = parse_int(isc_commandline_argument,
0dc4e6a6aef01175b8cdd71cb757f09ba1e69c49Brian Wellington "number of UDP listeners "
2c12fc4d63f1d5f9d55fc0ecb198d582da6fd7d3Brian Wellington "per interface");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington break;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington case 'u':
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_g_username = isc_commandline_argument;
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington break;
a00c5e2151cc03c06bae5cdd3b40a5de05664059Brian Wellington case 'v':
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington printf("%s %s", ns_g_product, ns_g_version);
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington if (*ns_g_description != 0)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington printf(" %s", ns_g_description);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington printf("\n");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington exit(0);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington case 'V':
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington printf("%s %s", ns_g_product, ns_g_version);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (*ns_g_description != 0)
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington printf(" %s", ns_g_description);
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington printf(" <id:%s> built by %s with %s\n", ns_g_srcid,
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington ns_g_builder, ns_g_configargs);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#ifdef __clang__
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington printf("compiled by CLANG %s\n", __VERSION__);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#else
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#if defined(__ICC) || defined(__INTEL_COMPILER)
8c56932f9a3dc252ab13a529a75f75966468a7c1Andreas Gustafsson printf("compiled by ICC %s\n", __VERSION__);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#else
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#ifdef __GNUC__
0dc4e6a6aef01175b8cdd71cb757f09ba1e69c49Brian Wellington printf("compiled by GCC %s\n", __VERSION__);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#ifdef _MSC_VER
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington printf("compiled by MSVC %d\n", _MSC_VER);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#ifdef __SUNPRO_C
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington printf("compiled by Solaris Studio %x\n", __SUNPRO_C);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington#ifdef OPENSSL
5caab9f99d19ab9ebb0a0ba64c09c8de80e89e29Brian Wellington printf("using OpenSSL version: %s\n",
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington OPENSSL_VERSION_TEXT);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#ifdef HAVE_LIBXML2
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington printf("using libxml2 version: %s\n",
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington LIBXML_DOTTED_VERSION);
d1cbf714097e900ed1703529584d3e1a50e8a4a8Brian Wellington#endif
2c12fc4d63f1d5f9d55fc0ecb198d582da6fd7d3Brian Wellington exit(0);
2c12fc4d63f1d5f9d55fc0ecb198d582da6fd7d3Brian Wellington case 'F':
2c12fc4d63f1d5f9d55fc0ecb198d582da6fd7d3Brian Wellington /* Reserved for FIPS mode */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /* FALLTHROUGH */
7e8dd00fce7057d1da8158b65395a09ced43a892Brian Wellington case '?':
6e5fa98df021f2b1eeb0ca6193b2f9f36e82ff36Brian Wellington usage();
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (isc_commandline_option == '?')
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington exit(0);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington p = strchr(CMDLINE_FLAGS, isc_commandline_option);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (p == NULL || *++p != ':')
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_main_earlyfatal("unknown option '-%c'",
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_commandline_option);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington ns_main_earlyfatal("option '-%c' requires "
3b2efab212088496d5588ca5ed2d802c72d21a08Brian Wellington "an argument",
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington isc_commandline_option);
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington /* FALLTHROUGH */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington default:
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_main_earlyfatal("parsing options returned %d", ch);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
d692d9991a731d60b63e6389da1ebf2b2839cfabBrian Wellington argc -= isc_commandline_index;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington argv += isc_commandline_index;
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington POST(argv);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (argc > 0) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington usage();
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_main_earlyfatal("extra command line arguments");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
a523752c602dc6bdb69a8d9c6267a1e4188a9782Andreas Gustafssonstatic isc_result_t
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellingtoncreate_managers(void) {
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington isc_result_t result;
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington unsigned int socks;
f7fbd68b1cd96c733140fce938a61faf8b459b6fBrian Wellington
f7fbd68b1cd96c733140fce938a61faf8b459b6fBrian Wellington#ifdef ISC_PLATFORM_USETHREADS
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington if (ns_g_cpus == 0)
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington ns_g_cpus = ns_g_cpus_detected;
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington#else
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington ns_g_cpus = 1;
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington#endif
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington#ifdef WIN32
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington ns_g_udpdisp = 1;
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington#else
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington if (ns_g_udpdisp == 0) {
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington if (ns_g_cpus_detected == 1)
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington ns_g_udpdisp = 1;
05b6b2e6802d503a9e131415b4720f35ab9f08d1Brian Wellington else if (ns_g_cpus_detected < 4)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_udpdisp = 2;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington else
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_udpdisp = ns_g_cpus_detected / 2;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (ns_g_udpdisp > ns_g_cpus)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_udpdisp = ns_g_cpus;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
5caab9f99d19ab9ebb0a0ba64c09c8de80e89e29Brian Wellington ISC_LOG_INFO, "using %u UDP listener%s per interface",
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington ns_g_udpdisp, ns_g_udpdisp == 1 ? "" : "s");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington if (result != ISC_R_SUCCESS) {
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "isc_taskmgr_create() failed: %s",
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington isc_result_totext(result));
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews return (ISC_R_UNEXPECTED);
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington }
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington if (result != ISC_R_SUCCESS) {
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington "isc_timermgr_create() failed: %s",
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington isc_result_totext(result));
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington return (ISC_R_UNEXPECTED);
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington }
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (result != ISC_R_SUCCESS) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "isc_socketmgr_create() failed: %s",
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington isc_result_totext(result));
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington return (ISC_R_UNEXPECTED);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc__socketmgr_maxudp(ns_g_socketmgr, maxudp);
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington if (result == ISC_R_SUCCESS) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_SERVER,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ISC_LOG_INFO, "using up to %u sockets", socks);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington if (result != ISC_R_SUCCESS) {
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "isc_entropy_create() failed: %s",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_result_totext(result));
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence return (ISC_R_UNEXPECTED);
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington }
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington
3b2efab212088496d5588ca5ed2d802c72d21a08Brian Wellington result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington if (result != ISC_R_SUCCESS) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
3b2efab212088496d5588ca5ed2d802c72d21a08Brian Wellington "isc_hash_create() failed: %s",
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington isc_result_totext(result));
3b2efab212088496d5588ca5ed2d802c72d21a08Brian Wellington return (ISC_R_UNEXPECTED);
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellington }
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellington
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington return (ISC_R_SUCCESS);
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellington}
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington
49c8a96fba8d85810d470fdc7dd3388f0c767c9eBrian Wellingtonstatic void
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellingtondestroy_managers(void) {
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_lwresd_shutdown();
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellington isc_entropy_detach(&ns_g_entropy);
0f0162e6297ddf5e4abe848d27f3bcdb373189daBrian Wellington if (ns_g_fallbackentropy != NULL)
7077846f3bf941f626a8623bd29a56a5ce7a1e11Brian Wellington isc_entropy_detach(&ns_g_fallbackentropy);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /*
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * isc_taskmgr_destroy() will block until all tasks have exited,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_taskmgr_destroy(&ns_g_taskmgr);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_timermgr_destroy(&ns_g_timermgr);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_socketmgr_destroy(&ns_g_socketmgr);
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /*
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington * isc_hash_destroy() cannot be called as long as a resolver may be
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington * running. Calling this after isc_taskmgr_destroy() ensures the
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington * call is safe.
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington */
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington isc_hash_destroy();
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington}
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonstatic void
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellingtondump_symboltable(void) {
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington int i;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington isc_result_t result;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington const char *fname;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington const void *addr;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington if (isc__backtrace_nsymbols == 0)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99)))
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington return;
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ISC_LOG_DEBUG(99), "Symbol table:");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) {
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington addr = NULL;
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington fname = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington result = isc_backtrace_getsymbolfromindex(i, &addr, &fname);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (result == ISC_R_SUCCESS) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_DEBUG(99),
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "[%d] %p %s", i, addr, fname);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence#ifdef HAVE_LIBSECCOMP
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellingtonstatic void
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellingtonsetup_seccomp() {
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington scmp_filter_ctx ctx;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence unsigned int i;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence int ret;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /* Make sure the lists are in sync */
db1bfc315197de36919f211bb6e4e98c5ce197caDanny Mayer INSIST((sizeof(scmp_syscalls) / sizeof(int)) ==
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington (sizeof(scmp_syscall_names) / sizeof(const char *)));
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ctx = seccomp_init(SCMP_ACT_KILL);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (ctx == NULL) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "libseccomp activation failed");
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington return;
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence for (i = 0 ; i < sizeof(scmp_syscalls)/sizeof(*(scmp_syscalls)); i++) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ret = seccomp_rule_add(ctx, SCMP_ACT_ALLOW,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington scmp_syscalls[i], 0);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (ret < 0)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "libseccomp rule failed: %s",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence scmp_syscall_names[i]);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence else
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_DEBUG(9),
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington "added libseccomp rule: %s",
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington scmp_syscall_names[i]);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ret = seccomp_load(ctx);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (ret < 0) {
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "libseccomp unable to load filter");
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington } else {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "libseccomp sandboxing active");
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington /*
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * Release filter in ctx. Filters already loaded are not
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * affected.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington seccomp_release(ctx);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington}
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#endif /* HAVE_LIBSECCOMP */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrencestatic void
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellingtonsetup(void) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington isc_result_t result;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_resourcevalue_t old_openfiles;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington#ifdef HAVE_LIBSCF
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington char *instance = NULL;
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington /*
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * Get the user and group information before changing the root
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * directory, so the administrator does not need to keep a copy
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence * of the user and group databases in the chroot'ed environment.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ns_os_inituserinfo(ns_g_username);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /*
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * Initialize time conversion information
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington */
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ns_os_tzset();
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_os_opendevnull();
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#ifdef HAVE_LIBSCF
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington /* Check if named is under smf control, before chroot. */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /* We don't care about instance, just check if we got one. */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (result == ISC_R_SUCCESS)
3aec811249fc1bde4476ced9432d3ebea0ad4dc4Brian Wellington ns_smf_got_instance = 1;
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington else
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington ns_smf_got_instance = 0;
3aec811249fc1bde4476ced9432d3ebea0ad4dc4Brian Wellington if (instance != NULL)
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington isc_mem_free(ns_g_mctx, instance);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington#endif /* HAVE_LIBSCF */
b70f6ae9d0be6bcf11bddc42f5e066ab6ac16517Brian Wellington
b70f6ae9d0be6bcf11bddc42f5e066ab6ac16517Brian Wellington#ifdef PATH_RANDOMDEV
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington /*
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington * Initialize system's random device as fallback entropy source
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * if running chroot'ed.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington if (ns_g_chrootdir != NULL) {
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (result != ISC_R_SUCCESS)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_main_earlyfatal("isc_entropy_create() failed: %s",
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_result_totext(result));
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington result = isc_entropy_createfilesource(ns_g_fallbackentropy,
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington PATH_RANDOMDEV);
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington if (result != ISC_R_SUCCESS) {
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington ns_main_earlywarning("could not open pre-chroot "
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington "entropy source %s: %s",
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington PATH_RANDOMDEV,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isc_result_totext(result));
f7fbd68b1cd96c733140fce938a61faf8b459b6fBrian Wellington isc_entropy_detach(&ns_g_fallbackentropy);
f7fbd68b1cd96c733140fce938a61faf8b459b6fBrian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington#ifdef ISC_PLATFORM_USETHREADS
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /*
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * Check for the number of cpu's before ns_os_chroot().
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington ns_g_cpus_detected = isc_os_ncpus();
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington#endif
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington ns_os_chroot(ns_g_chrootdir);
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington /*
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * For operating systems which have a capability mechanism, now
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * is the time to switch to minimal privs and change our user id.
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * On traditional UNIX systems, this call will be a no-op, and we
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews * will change the user ID after reading the config file the first
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * time. (We need to read the config file to know which possibly
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * privileged ports to bind() to.)
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington */
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_os_minprivs();
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
ac335315cddc0a42b9235001197dcf719ae737b6Brian Wellington result = ns_log_init(ISC_TF(ns_g_username != NULL));
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington if (result != ISC_R_SUCCESS)
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ns_main_earlyfatal("ns_log_init() failed: %s",
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington isc_result_totext(result));
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington /*
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington * Now is the time to daemonize (if we're not running in the
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * foreground). We waited until now because we wanted to get
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * a valid logging context setup. We cannot daemonize any later,
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington * because calling create_managers() will create threads, which
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * would be lost after fork().
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (!ns_g_foreground)
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington ns_os_daemonize();
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington /*
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington * We call isc_app_start() here as some versions of FreeBSD's fork()
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington * destroys all the signal handling it sets up.
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington */
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington result = isc_app_start();
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington if (result != ISC_R_SUCCESS)
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington ns_main_earlyfatal("isc_app_start() failed: %s",
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington isc_result_totext(result));
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
368b37b616234fce3d23099eb180f1dd38e1fb62Mark Andrews ISC_LOG_NOTICE, "starting %s %s%s", ns_g_product,
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington ns_g_version, saved_command_line);
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ISC_LOG_NOTICE,
5caab9f99d19ab9ebb0a0ba64c09c8de80e89e29Brian Wellington "----------------------------------------------------");
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ISC_LOG_NOTICE,
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington "BIND 9 is maintained by Internet Systems Consortium,");
fe0e3c7707580da885bb6819e4f307986eb60cd0Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington ISC_LOG_NOTICE,
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington "Inc. (ISC), a non-profit 501(c)(3) public-benefit ");
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence ISC_LOG_NOTICE,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence "corporation. Support and training for BIND 9 are ");
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ISC_LOG_NOTICE,
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington "available at https://www.isc.org/support");
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ISC_LOG_NOTICE,
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington "----------------------------------------------------");
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington dump_symboltable();
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington /*
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * Get the initial resource limits.
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence (void)isc_resource_getlimit(isc_resource_stacksize,
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence &ns_g_initstacksize);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence (void)isc_resource_getlimit(isc_resource_datasize,
58aaab3687aac838542ee4ef65a9c094a5d34ab0Michael Graff &ns_g_initdatasize);
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington (void)isc_resource_getlimit(isc_resource_coresize,
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington &ns_g_initcoresize);
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington (void)isc_resource_getlimit(isc_resource_openfiles,
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington &ns_g_initopenfiles);
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington /*
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington * System resources cannot effectively be tuned on some systems.
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington * Raise the limit in such cases for safety.
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington */
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington old_openfiles = ns_g_initopenfiles;
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington ns_os_adjustnofile();
564c1b8f42d33a66c0f5c67d66bf0cd063c8b047Brian Wellington (void)isc_resource_getlimit(isc_resource_openfiles,
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington &ns_g_initopenfiles);
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington if (old_openfiles != ns_g_initopenfiles) {
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington "adjusted limit on open files from "
af602636644fdfaabc331bd926b0aabb9432e152Brian Wellington "%" ISC_PRINT_QUADFORMAT "u to "
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington "%" ISC_PRINT_QUADFORMAT "u",
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington old_openfiles, ns_g_initopenfiles);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington }
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington /*
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * If the named configuration filename is relative, prepend the current
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington * directory's name before possibly changing to another directory.
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington */
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington if (! isc_file_isabsolute(ns_g_conffile)) {
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence result = isc_file_absolutepath(ns_g_conffile,
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington absolute_conffile,
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence sizeof(absolute_conffile));
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (result != ISC_R_SUCCESS)
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_main_earlyfatal("could not construct absolute path "
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington "of configuration file: %s",
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington isc_result_totext(result));
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington ns_g_conffile = absolute_conffile;
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington }
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington /*
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * Record the server's startup time.
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington result = isc_time_now(&ns_g_boottime);
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington if (result != ISC_R_SUCCESS)
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington ns_main_earlyfatal("isc_time_now() failed: %s",
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington isc_result_totext(result));
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington result = create_managers();
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (result != ISC_R_SUCCESS)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_main_earlyfatal("create_managers() failed: %s",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_result_totext(result));
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_builtin_init();
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Add calls to register sdb drivers here.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /* xxdb_init(); */
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington#ifdef ISC_DLZ_DLOPEN
db1bfc315197de36919f211bb6e4e98c5ce197caDanny Mayer /*
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington * Register the DLZ "dlopen" driver.
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington */
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington result = dlz_dlopen_init(ns_g_mctx);
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (result != ISC_R_SUCCESS)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_main_earlyfatal("dlz_dlopen_init() failed: %s",
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence isc_result_totext(result));
dc3fc5830a90b170c0a2fbf2e8fe057aad209678Brian Wellington#endif
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington#if CONTRIB_DLZ
dc3fc5830a90b170c0a2fbf2e8fe057aad209678Brian Wellington /*
dc3fc5830a90b170c0a2fbf2e8fe057aad209678Brian Wellington * Register any other contributed DLZ drivers.
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence result = dlz_drivers_init();
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence if (result != ISC_R_SUCCESS)
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence ns_main_earlyfatal("dlz_drivers_init() failed: %s",
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington isc_result_totext(result));
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington#endif
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_server_create(ns_g_mctx, &ns_g_server);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington#ifdef HAVE_LIBSECCOMP
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence setup_seccomp();
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#endif /* HAVE_LIBSECCOMP */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence}
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellingtonstatic void
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellingtoncleanup(void) {
d598338952797df77417e69fcb8782b73651f9a9Brian Wellington destroy_managers();
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_server_destroy(&ns_g_server);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington ns_builtin_deinit();
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington /*
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence * Add calls to unregister sdb drivers here.
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence */
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence /* xxdb_clear(); */
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence#ifdef CONTRIB_DLZ
1e2749dba8aae3233b8962f1efe15385e92a77d9Brian Wellington /*
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * Unregister contributed DLZ drivers.
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence */
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington dlz_drivers_clear();
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington#endif
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington#ifdef ISC_DLZ_DLOPEN
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington /*
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington * Unregister "dlopen" DLZ driver.
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington */
0f80bfec687db08a6e6ce945ef1d818da06c7ca9Brian Wellington dlz_dlopen_clear();
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington#endif
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington dns_name_destroy();
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
5ea0d11ca45bfd1ea9db8db07f18fbb02f500661Brian Wellington ISC_LOG_NOTICE, "exiting");
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington ns_log_shutdown();
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington}
5766ea1011051b4a9f7560041a03d9a562722df3Brian Wellington
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellingtonstatic char *memstats = NULL;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
5766ea1011051b4a9f7560041a03d9a562722df3Brian Wellingtonvoid
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellingtonns_main_setmemstats(const char *filename) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington /*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Caller has to ensure locking.
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington */
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington
020ebf119089ef68070d6a0df2def3142f1eeff4Brian Wellington if (memstats != NULL) {
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington free(memstats);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington memstats = NULL;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington }
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington if (filename == NULL)
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington return;
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington memstats = malloc(strlen(filename) + 1);
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington if (memstats)
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington strcpy(memstats, filename);
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington}
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington#ifdef HAVE_LIBSCF
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington/*
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington * Get FMRI for the named process.
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington */
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellingtonisc_result_t
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellingtonns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington scf_handle_t *h = NULL;
809fdfba224a83fcc5a8ae4394cf2a477d13dc91Brian Wellington int namelen;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington char *instance;
4fe8755480c108a1232b7189fd5434ab35a6b623Brian Wellington
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington REQUIRE(ins_name != NULL && *ins_name == NULL);
c7f13217d11f26739a79f0dab391ec372b49b96bBrian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellington if (debug)
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington "scf_handle_create() failed: %s",
3f6dc1703f76a24b34ed3bc839447291c33ca837Brian Wellington scf_strerror(scf_error()));
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington return (ISC_R_FAILURE);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington if (scf_handle_bind(h) == -1) {
949d406b57fe80fabc6a60d36a0dcee927c780b3Brian Wellington if (debug)
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington "scf_handle_bind() failed: %s",
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington scf_strerror(scf_error()));
fb01226bcd598c36b5edc566489c890c39f03ed3Brian Wellington scf_handle_destroy(h);
49c8a96fba8d85810d470fdc7dd3388f0c767c9eBrian Wellington return (ISC_R_FAILURE);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington }
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if ((namelen = scf_myname(h, NULL, 0)) == -1) {
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (debug)
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington "scf_myname() failed: %s",
f2338a0d6aa0327372eb20ab5dc29502bc8c71efBrian Wellington scf_strerror(scf_error()));
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington scf_handle_destroy(h);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington return (ISC_R_FAILURE);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington }
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington "ns_smf_get_instance memory "
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington "allocation failed: %s",
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington isc_result_totext(ISC_R_NOMEMORY));
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington scf_handle_destroy(h);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington return (ISC_R_FAILURE);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington }
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (scf_myname(h, instance, namelen + 1) == -1) {
100d0d2ec64ab1a85f8c0d2da9b47ae411a10b21Brian Wellington if (debug)
49c8a96fba8d85810d470fdc7dd3388f0c767c9eBrian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington "scf_myname() failed: %s",
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington scf_strerror(scf_error()));
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington scf_handle_destroy(h);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington isc_mem_free(mctx, instance);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington return (ISC_R_FAILURE);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington }
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington scf_handle_destroy(h);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington *ins_name = instance;
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington return (ISC_R_SUCCESS);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington}
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington#endif /* HAVE_LIBSCF */
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington/* main entry point, possibly hooked */
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonint
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellingtonmain(int argc, char *argv[]) {
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington isc_result_t result;
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington#ifdef HAVE_LIBSCF
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington char *instance = NULL;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence#endif
ffdcf33647eb0345dfe84be2c0e7b28264377436Brian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington /*
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington * Record version in core image.
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington * strings named.core | grep "named version:"
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington */
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington strlcat(version,
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington#if defined(NO_VERSION_DATE) || !defined(__DATE__)
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence "named version: BIND " VERSION " <" SRCID ">",
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington#else
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington "named version: BIND " VERSION " <" SRCID "> (" __DATE__ ")",
8dd915daf3f75ac9d04395e61157fdea825f3ebaBrian Wellington#endif
8dd915daf3f75ac9d04395e61157fdea825f3ebaBrian Wellington sizeof(version));
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington result = isc_file_progname(*argv, program_name, sizeof(program_name));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff if (result != ISC_R_SUCCESS)
8dd915daf3f75ac9d04395e61157fdea825f3ebaBrian Wellington ns_main_earlyfatal("program name too long");
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (strcmp(program_name, "lwresd") == 0)
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington ns_g_lwresdonly = ISC_TRUE;
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (result != ISC_R_SUCCESS)
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington ns_main_earlyfatal("failed to build internal symbol table");
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington isc_assertion_setcallback(assertion_failed);
e552b980379e3a7ffce1411a939c62e27f953133Brian Wellington isc_error_setfatal(library_fatal_error);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington isc_error_setunexpected(library_unexpected_error);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington ns_os_init(program_name);
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dns_result_register();
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington dst_result_register();
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington isccc_result_register();
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington#ifdef PKCS11CRYPTO
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington pk11_result_register();
8dd915daf3f75ac9d04395e61157fdea825f3ebaBrian Wellington#endif
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington parse_command_line(argc, argv);
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington /*
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington * Warn about common configuration error.
b984520acca2532d048eae929dc0682dd334c7a3Brian Wellington */
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (ns_g_chrootdir != NULL) {
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington int len = strlen(ns_g_chrootdir);
a49acbf201a411a47e18d136b38bbea8cf283adaBrian Wellington if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
6d4886fa7430889a96dbf9b88a2a4eb6f9d04674Brian Wellington (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
ns_main_earlywarning("config filename (-c %s) contains "
"chroot path (-t %s)",
ns_g_conffile, ns_g_chrootdir);
}
result = isc_mem_create(0, 0, &ns_g_mctx);
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_mem_create() failed: %s",
isc_result_totext(result));
isc_mem_setname(ns_g_mctx, "main", NULL);
setup();
/*
* Start things running and then wait for a shutdown request
* or reload.
*/
do {
result = isc_app_run();
if (result == ISC_R_RELOAD) {
ns_server_reloadwanted(ns_g_server);
} else if (result != ISC_R_SUCCESS) {
UNEXPECTED_ERROR(__FILE__, __LINE__,
"isc_app_run(): %s",
isc_result_totext(result));
/*
* Force exit.
*/
result = ISC_R_SUCCESS;
}
} while (result != ISC_R_SUCCESS);
#ifdef HAVE_LIBSCF
if (ns_smf_want_disable == 1) {
result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
if (result == ISC_R_SUCCESS && instance != NULL) {
if (smf_disable_instance(instance, 0) != 0)
UNEXPECTED_ERROR(__FILE__, __LINE__,
"smf_disable_instance() "
"failed for %s : %s",
instance,
scf_strerror(scf_error()));
}
if (instance != NULL)
isc_mem_free(ns_g_mctx, instance);
}
#endif /* HAVE_LIBSCF */
cleanup();
if (want_stats) {
isc_mem_stats(ns_g_mctx, stdout);
isc_mutex_stats(stdout);
}
if (ns_g_memstatistics && memstats != NULL) {
FILE *fp = NULL;
result = isc_stdio_open(memstats, "w", &fp);
if (result == ISC_R_SUCCESS) {
isc_mem_stats(ns_g_mctx, fp);
isc_mutex_stats(fp);
isc_stdio_close(fp);
}
}
isc_mem_destroy(&ns_g_mctx);
isc_mem_checkdestroyed(stderr);
ns_main_setmemstats(NULL);
isc_app_finish();
ns_os_closedevnull();
ns_os_shutdown();
return (0);
}