main.c revision bcf966e6146943ebcac1603a52468f5292612b18
63b0524b961b6dce1aa18488d7175ca4ee17778fTinderbox User/*
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * Copyright (C) 2004-2012 Internet Systems Consortium, Inc. ("ISC")
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * Copyright (C) 1999-2003 Internet Software Consortium.
7bd455641455950eff7d21be652c8142b134d32fTinderbox User *
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * Permission to use, copy, modify, and/or distribute this software for any
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * purpose with or without fee is hereby granted, provided that the above
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * copyright notice and this permission notice appear in all copies.
7bd455641455950eff7d21be652c8142b134d32fTinderbox User *
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
7bd455641455950eff7d21be652c8142b134d32fTinderbox User * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt * PERFORMANCE OF THIS SOFTWARE.
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews */
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews/* $Id: main.c,v 1.187 2012/02/06 23:46:44 tbox Exp $ */
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews/*! \file */
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#include <config.h>
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#include <ctype.h>
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#include <stdlib.h>
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#include <string.h>
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews
bf8267aa453e5d2a735ed732a043b77a0b355b20Mark Andrews#include <isc/app.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence#include <isc/backtrace.h>
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <isc/commandline.h>
74cb99072c4b0ebd2ccafcfa284288fa760f7a1aMark Andrews#include <isc/dir.h>
bff64bf12b58a6f80e740e94f2e42a32df18113aEvan Hunt#include <isc/entropy.h>
866d106459313499d0ca7bfccb4b2d23d5e4377cDavid Lawrence#include <isc/file.h>
7c74e180c206e6ed99e8beb820da5f399d845c3eDavid Lawrence#include <isc/hash.h>
92b796c963e7ba0217debfa27a0709f94934f5d5Mark Andrews#include <isc/os.h>
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews#include <isc/platform.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <isc/print.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <isc/resource.h>
63dd46733010bb9622810faa17d88c3e3c28b730Mark Andrews#include <isc/stdio.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <isc/string.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <isc/task.h>
a5d43b72413db3edd6b36a58f9bdf2cf6ff692f2Bob Halley#include <isc/timer.h>
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#include <isc/util.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <isccc/result.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <dns/dispatch.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <dns/name.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <dns/result.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <dns/view.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <dst/result.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews#include <dlz/dlz_dlopen_driver.h>
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews
f4ea363e3acc321b24ffe95a64a583e8041d6fd5Mark Andrews/*
914eeb33149a0008e26741d9e7d89dcd6f8b6d0bMark Andrews * Defining NS_MAIN provides storage declarations (rather than extern)
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff * for variables in named/globals.h.
a903095bf4512dae561c7f6fc7854a51bebf334aMark Andrews */
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#define NS_MAIN 1
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#include <named/builtin.h>
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#include <named/control.h>
3d776d762914d1b675b4fd49728ce353ccf6f77eBrian Wellington#include <named/globals.h> /* Explicit, though named/log.h includes it. */
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt#include <named/interfacemgr.h>
6b66ee9147e940572a0e873ecbd67456ccb85c39Mark Andrews#include <named/log.h>
ccdac53c027e8964753b36c4d8c7b0e98af501c2Michael Graff#include <named/os.h>
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#include <named/server.h>
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#include <named/lwresd.h>
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#include <named/main.h>
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#ifdef HAVE_LIBSCF
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#include <named/ns_smf_globals.h>
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#endif
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#ifdef OPENSSL
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#include <openssl/opensslv.h>
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#endif
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#ifdef HAVE_LIBXML2
11dbf2fc38eea8c5d3fe7123718bf197a8bb2e6bMark Andrews#include <libxml/xmlversion.h>
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt#endif
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt/*
1c1290afabb3c8f4dd498170ac9592e5be450161Mark Andrews * Include header files for database drivers here.
1c1290afabb3c8f4dd498170ac9592e5be450161Mark Andrews */
b123b265e3a3d9b72a14230b6517e0f6fdb5c5b5Mark Andrews/* #include "xxdb.h" */
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews#ifdef CONTRIB_DLZ
740e7340c55e9f0cf80c6fbbf7e8d3c1bdeaa255Mark Andrews/*
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews * Include contributed DLZ drivers if appropriate.
84f95ddb2572641022619950a211aff49e331c98Mukund Sivaraman */
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews#include <dlz/dlz_drivers.h>
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews#endif
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews
71697fd082b1c76562dc80fa91787af3860146bfEvan Hunt/*
1c1290afabb3c8f4dd498170ac9592e5be450161Mark Andrews * The maximum number of stack frames to dump on assertion failure.
11463c0ac24692e229ec87f307f5e7df3c0a7e10Evan Hunt */
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews#ifndef BACKTRACE_MAXFRAME
32ebb157b1d9409a186c86002827f8a1f4931f5aMark Andrews#define BACKTRACE_MAXFRAME 128
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt#endif
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntstatic isc_boolean_t want_stats = ISC_FALSE;
1831311ac6179951c8fcca75aa29dc2f5c0218b9Francis Dupontstatic char program_name[ISC_DIR_NAMEMAX] = "named";
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntstatic char absolute_conffile[ISC_DIR_PATHMAX];
206e697f24e47b8868bd68a5b6ef42f5f62e39d5Evan Huntstatic char saved_command_line[512];
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntstatic char version[512];
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntstatic unsigned int maxsocks = 0;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntstatic int maxudp = 0;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntvoid
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntns_main_earlywarning(const char *format, ...) {
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt va_list args;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt va_start(args, format);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt if (ns_g_lctx != NULL) {
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt format, args);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt } else {
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt fprintf(stderr, "%s: ", program_name);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt vfprintf(stderr, format, args);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt fprintf(stderr, "\n");
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt fflush(stderr);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt }
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt va_end(args);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt}
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntvoid
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntns_main_earlyfatal(const char *format, ...) {
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt va_list args;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt va_start(args, format);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt if (ns_g_lctx != NULL) {
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt format, args);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt "exiting (due to early fatal error)");
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt } else {
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt fprintf(stderr, "%s: ", program_name);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt vfprintf(stderr, format, args);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt fprintf(stderr, "\n");
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt fflush(stderr);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt }
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt va_end(args);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt exit(1);
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt}
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan HuntISC_PLATFORM_NORETURN_PRE static void
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntassertion_failed(const char *file, int line, isc_assertiontype_t type,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt const char *cond) ISC_PLATFORM_NORETURN_POST;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntstatic void
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Huntassertion_failed(const char *file, int line, isc_assertiontype_t type,
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt const char *cond)
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt{
206e697f24e47b8868bd68a5b6ef42f5f62e39d5Evan Hunt void *tracebuf[BACKTRACE_MAXFRAME];
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt int i, nframes;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt isc_result_t result;
6fa84a3e255ef9e6233f0a8d134fc6d273f04599Evan Hunt const char *logsuffix = "";
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews const char *fname;
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews /*
323a9f3430abf186f8f84d795549391a8ed7f274Francis Dupont * Handle assertion failures.
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews */
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews
03e200df5dc283f24a6a349f0b31d3eab26da893Mark Andrews if (ns_g_lctx != NULL) {
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence /*
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence * Reset the assertion callback in case it is the log
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence * routines causing the assertion.
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence */
75a4dd0d377dca2f85cea44e28bf110314c1fe8cDavid Lawrence isc_assertion_setcallback(NULL);
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME,
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson &nframes);
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson if (result == ISC_R_SUCCESS && nframes > 0)
91306d962f9d147d94b82fb14edb28f8d907cae7Andreas Gustafsson logsuffix = ", back trace";
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence "%s:%d: %s(%s) failed%s", file, line,
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence isc_assertion_typetotext(type), cond, logsuffix);
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence if (result == ISC_R_SUCCESS) {
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence for (i = 0; i < nframes; i++) {
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence unsigned long offset;
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence fname = NULL;
e893dce91279d7313a579f72caae3941f6dc5a27David Lawrence result = isc_backtrace_getsymbol(tracebuf[i],
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley &fname,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley &offset);
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley if (result == ISC_R_SUCCESS) {
8e06cea14c857429ab7e7299af2dce5eeeaa5ff0Michael Graff isc_log_write(ns_g_lctx,
ce8c568e0d6106bb87069453505e09bc66754b40Andreas Gustafsson NS_LOGCATEGORY_GENERAL,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley NS_LOGMODULE_MAIN,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley ISC_LOG_CRITICAL,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley "#%d %p in %s()+0x%lx", i,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley tracebuf[i], fname,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley offset);
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley } else {
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley isc_log_write(ns_g_lctx,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley NS_LOGCATEGORY_GENERAL,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley NS_LOGMODULE_MAIN,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley ISC_LOG_CRITICAL,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley "#%d %p in ??", i,
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley tracebuf[i]);
3b77946b751f39bd4db5a7d1fe48a81e6b1e7a28Bob Halley }
8e06cea14c857429ab7e7299af2dce5eeeaa5ff0Michael Graff }
8e06cea14c857429ab7e7299af2dce5eeeaa5ff0Michael Graff }
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
a27fe4c990f96bd792f2a07ca4d38c78d5b9df2cTatuya JINMEI 神明達哉 "exiting (due to assertion failure)");
a27fe4c990f96bd792f2a07ca4d38c78d5b9df2cTatuya JINMEI 神明達哉 } else {
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson fprintf(stderr, "%s:%d: %s(%s) failed\n",
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson file, line, isc_assertion_typetotext(type), cond);
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson fflush(stderr);
3ecf3394e37dc2848a09ffc643565d454e9e6974Andreas Gustafsson }
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews if (ns_g_coreok)
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews abort();
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews exit(1);
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews}
b5f6271f4daf1e54501af2cb7dd278d7e8003d65Mark Andrews
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark AndrewsISC_PLATFORM_NORETURN_PRE static void
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewslibrary_fatal_error(const char *file, int line, const char *format,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt va_list args)
523f3d630243211ddfda852f5224f7eff681d3a5Evan HuntISC_FORMAT_PRINTF(3, 0) ISC_PLATFORM_NORETURN_POST;
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewsstatic void
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewslibrary_fatal_error(const char *file, int line, const char *format,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt va_list args)
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews{
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews /*
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews * Handle isc_error_fatal() calls from our libraries.
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews */
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt if (ns_g_lctx != NULL) {
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt /*
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt * Reset the error callback in case it is the log
1f27b7873c640771df00a47ee3be188c4079479cFrancis Dupont * routines causing the assertion.
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt */
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt isc_error_setfatal(NULL);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt "%s:%d: fatal error:", file, line);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt format, args);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
1f27b7873c640771df00a47ee3be188c4079479cFrancis Dupont "exiting (due to fatal error in library)");
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt } else {
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt fprintf(stderr, "%s:%d: fatal error: ", file, line);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt vfprintf(stderr, format, args);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt fprintf(stderr, "\n");
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt fflush(stderr);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt }
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt if (ns_g_coreok)
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt abort();
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt exit(1);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt}
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewsstatic void
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewslibrary_unexpected_error(const char *file, int line, const char *format,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt va_list args) ISC_FORMAT_PRINTF(3, 0);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewsstatic void
523f3d630243211ddfda852f5224f7eff681d3a5Evan Huntlibrary_unexpected_error(const char *file, int line, const char *format,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt va_list args)
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews{
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews /*
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt * Handle isc_error_unexpected() calls from our libraries.
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt */
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt if (ns_g_lctx != NULL) {
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt "%s:%d: unexpected error:", file, line);
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
523f3d630243211ddfda852f5224f7eff681d3a5Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews format, args);
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews } else {
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews fprintf(stderr, "%s:%d: fatal error: ", file, line);
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews vfprintf(stderr, format, args);
66dddd906ada6035d65bbbad2ecbcd74037759a8Mark Andrews fprintf(stderr, "\n");
831f59eb43b56642b00f82e07722836d2f9593abEvan Hunt fflush(stderr);
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews }
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews}
66dddd906ada6035d65bbbad2ecbcd74037759a8Mark Andrews
831f59eb43b56642b00f82e07722836d2f9593abEvan Huntstatic void
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrewslwresd_usage(void) {
66dddd906ada6035d65bbbad2ecbcd74037759a8Mark Andrews fprintf(stderr,
831f59eb43b56642b00f82e07722836d2f9593abEvan Hunt "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews "[-d debuglevel]\n"
1cefb9df3fa34d08734f29005cfafa6be5cf3e93Mark Andrews " [-f|-g] [-n number_of_cpus] [-p port] "
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence "[-P listen-port] [-s]\n"
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence " [-t chrootdir] [-u username] [-i pidfile]\n"
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence " [-m {usage|trace|record|size|mctx}]\n");
b587e1d83f007ce68a9ae93097c461d8eb7aa373Mark Andrews}
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrencestatic void
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrenceusage(void) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence if (ns_g_lwresdonly) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence lwresd_usage();
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence return;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence }
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence fprintf(stderr,
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence "usage: named [-4|-6] [-c conffile] [-d debuglevel] "
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence "[-E engine] [-f|-g]\n"
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence " [-n number_of_cpus] [-p port] [-s] "
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence "[-t chrootdir] [-u username]\n"
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence " [-m {usage|trace|record|size|mctx}]\n");
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence}
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrencestatic void
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrencesave_command_line(int argc, char *argv[]) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews int i;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews char *src;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews char *dst;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews char *eob;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews const char truncated[] = "...";
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews isc_boolean_t quoted = ISC_FALSE;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews dst = saved_command_line;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews eob = saved_command_line + sizeof(saved_command_line);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews for (i = 1; i < argc && dst < eob; i++) {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews *dst++ = ' ';
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence
ae4cbb69eef32ced103fe4561e8d2031ee4c3497David Lawrence src = argv[i];
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence while (*src != '\0' && dst < eob) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence /*
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence * This won't perfectly produce a shell-independent
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence * pastable command line in all circumstances, but
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence * comes close, and for practical purposes will
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence * nearly always be fine.
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence */
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence if (quoted || isalnum(*src & 0xff) ||
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence *src == '-' || *src == '_' ||
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence *src == '.' || *src == '/') {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews *dst++ = *src++;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews quoted = ISC_FALSE;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews } else {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews *dst++ = '\\';
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews quoted = ISC_TRUE;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews }
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews INSIST(sizeof(saved_command_line) >= sizeof(truncated));
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews if (dst == eob)
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews strcpy(eob - sizeof(truncated), truncated);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews else
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence *dst = '\0';
0293ad13207aa29bd5844cdc87d085ffc009d749David Lawrence}
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrencestatic int
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrenceparse_int(char *arg, const char *desc) {
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence char *endp;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence int tmp;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence long int ltmp;
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence
df3c4c7988b9bae7d121a8ac9ed17a23366a948dDavid Lawrence ltmp = strtol(arg, &endp, 10);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff tmp = (int) ltmp;
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff if (*endp != '\0')
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff if (tmp < 0 || tmp != ltmp)
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff ns_main_earlyfatal("%s '%s' out of range", desc, arg);
1ce985ab3c6670662d555c108b35fed84a6a1001David Lawrence return (tmp);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews}
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrewsstatic struct flag_def {
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews const char *name;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews unsigned int value;
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews} mem_debug_flags[] = {
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence { "trace", ISC_MEM_DEBUGTRACE },
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence { "record", ISC_MEM_DEBUGRECORD },
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence { "usage", ISC_MEM_DEBUGUSAGE },
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence { "size", ISC_MEM_DEBUGSIZE },
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence { "mctx", ISC_MEM_DEBUGCTX },
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence { NULL, 0 }
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence};
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrencestatic void
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrenceset_flags(const char *arg, struct flag_def *defs, unsigned int *ret) {
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence for (;;) {
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence const struct flag_def *def;
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence const char *end = strchr(arg, ',');
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence int arglen;
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence if (end == NULL)
4bcaefbcd3ced942139fdc830e007c6ea2b8d2feDavid Lawrence end = arg + strlen(arg);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff arglen = end - arg;
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff for (def = defs; def->name != NULL; def++) {
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff if (arglen == (int)strlen(def->name) &&
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff memcmp(arg, def->name, arglen) == 0) {
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff *ret |= def->value;
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff goto found;
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff }
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff }
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg);
657ce0b9d84fbd66514df53d61a087e8f1161187Michael Graff found:
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson if (*end == '\0')
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson break;
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews arg = end + 1;
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews }
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews}
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrewsstatic void
80badf38c74c326a694e24281ee258aa26984171Mark Andrewsparse_command_line(int argc, char *argv[]) {
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews int ch;
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews int port;
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews isc_boolean_t disable6 = ISC_FALSE;
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews isc_boolean_t disable4 = ISC_FALSE;
641da3ca1184d9951d5cf91538524a345bf5f271Mark Andrews
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews save_command_line(argc, argv);
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews isc_commandline_errprint = ISC_FALSE;
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews while ((ch = isc_commandline_parse(argc, argv,
323a9f3430abf186f8f84d795549391a8ed7f274Francis Dupont "46c:C:d:E:fFgi:lm:n:N:p:P:"
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews "sS:t:T:U:u:vVx:")) != -1) {
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews switch (ch) {
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews case '4':
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews if (disable4)
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews ns_main_earlyfatal("cannot specify -4 and -6");
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews if (isc_net_probeipv4() != ISC_R_SUCCESS)
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews ns_main_earlyfatal("IPv4 not supported by OS");
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews isc_net_disableipv6();
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews disable6 = ISC_TRUE;
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews break;
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews case '6':
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews if (disable6)
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews ns_main_earlyfatal("cannot specify -4 and -6");
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews if (isc_net_probeipv6() != ISC_R_SUCCESS)
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews ns_main_earlyfatal("IPv6 not supported by OS");
774c3a62d9adca187b44fe90919bb409a43a2f2aMark Andrews isc_net_disableipv4();
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews disable4 = ISC_TRUE;
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews break;
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews case 'c':
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews ns_g_conffile = isc_commandline_argument;
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews lwresd_g_conffile = isc_commandline_argument;
89119e3cafff373426858f6cec7c09539f53e209Mark Andrews if (lwresd_g_useresolvconf)
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews ns_main_earlyfatal("cannot specify -c and -C");
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews ns_g_conffileset = ISC_TRUE;
d7896edb4e93c4785a9281ea86afba86b758e813Mark Andrews break;
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff case 'C':
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff lwresd_g_resolvconffile = isc_commandline_argument;
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff if (ns_g_conffileset)
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff ns_main_earlyfatal("cannot specify -c and -C");
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff lwresd_g_useresolvconf = ISC_TRUE;
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff break;
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff case 'd':
ab3aeba682460fd39deb7901aa69f976583c9f47Michael Graff ns_g_debuglevel = parse_int(isc_commandline_argument,
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews "debug level");
9fe28a624c659e380d47dbf45527637dab03b998Mark Andrews break;
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt case 'E':
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt ns_g_engine = isc_commandline_argument;
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt break;
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt case 'f':
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt ns_g_foreground = ISC_TRUE;
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt break;
5b7abbef511cea0b568be0bc8d5b3120a0b9034dEvan Hunt case 'g':
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson ns_g_foreground = ISC_TRUE;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson ns_g_logstderr = ISC_TRUE;
850b5e80930907e4747347201dc41e4d04e036f8Mark Andrews break;
6342df69b05f2f62d060fd4affdf536e51504084Mark Andrews /* XXXBEW -i should be removed */
6342df69b05f2f62d060fd4affdf536e51504084Mark Andrews case 'i':
6342df69b05f2f62d060fd4affdf536e51504084Mark Andrews lwresd_g_defaultpidfile = isc_commandline_argument;
6342df69b05f2f62d060fd4affdf536e51504084Mark Andrews break;
62ec9fd1681ffae7d6b0d54618599ecf650e3100Mark Andrews case 'l':
62ec9fd1681ffae7d6b0d54618599ecf650e3100Mark Andrews ns_g_lwresdonly = ISC_TRUE;
62ec9fd1681ffae7d6b0d54618599ecf650e3100Mark Andrews break;
6342df69b05f2f62d060fd4affdf536e51504084Mark Andrews case 'm':
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson set_flags(isc_commandline_argument, mem_debug_flags,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson &isc_mem_debugging);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson break;
8486ce1efa5deded85415d21d5696e5a51c63357Mark Andrews case 'N': /* Deprecated. */
c654449ccf403ccd2b81be2038b1013d6fbb06ccMark Andrews case 'n':
5b02fc32d693bb811199308a40143df0adf818c1Mark Andrews ns_g_cpus = parse_int(isc_commandline_argument,
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson "number of cpus");
5506903c9215faf42586307c2288942fd804c579Evan Hunt if (ns_g_cpus == 0)
5506903c9215faf42586307c2288942fd804c579Evan Hunt ns_g_cpus = 1;
5506903c9215faf42586307c2288942fd804c579Evan Hunt break;
5506903c9215faf42586307c2288942fd804c579Evan Hunt case 'p':
5506903c9215faf42586307c2288942fd804c579Evan Hunt port = parse_int(isc_commandline_argument, "port");
38cd4d14cc341c2663e574035074788bb6f0fce2Evan Hunt if (port < 1 || port > 65535)
38cd4d14cc341c2663e574035074788bb6f0fce2Evan Hunt ns_main_earlyfatal("port '%s' out of range",
38cd4d14cc341c2663e574035074788bb6f0fce2Evan Hunt isc_commandline_argument);
38cd4d14cc341c2663e574035074788bb6f0fce2Evan Hunt ns_g_port = port;
38cd4d14cc341c2663e574035074788bb6f0fce2Evan Hunt break;
38cd4d14cc341c2663e574035074788bb6f0fce2Evan Hunt /* XXXBEW Should -P be removed? */
d8f2dd46cba3a16c2433e85657a5b15543013ca6Mark Andrews case 'P':
d8f2dd46cba3a16c2433e85657a5b15543013ca6Mark Andrews port = parse_int(isc_commandline_argument, "port");
d8f2dd46cba3a16c2433e85657a5b15543013ca6Mark Andrews if (port < 1 || port > 65535)
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson ns_main_earlyfatal("port '%s' out of range",
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson isc_commandline_argument);
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson lwresd_g_listenport = port;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson break;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson case 's':
cd63e943104ab4f7f8b37da8d49738e91a8db1ddEvan Hunt /* XXXRTH temporary syntax */
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington want_stats = ISC_TRUE;
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington break;
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington case 'S':
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington maxsocks = parse_int(isc_commandline_argument,
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington "max number of sockets");
47fd46791da765e3dbedd987e9b263b3bee25986Brian Wellington break;
cd63e943104ab4f7f8b37da8d49738e91a8db1ddEvan Hunt case 't':
cd63e943104ab4f7f8b37da8d49738e91a8db1ddEvan Hunt /* XXXJAB should we make a copy? */
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson ns_g_chrootdir = isc_commandline_argument;
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson break;
87708bde16713bc02ff2598f4a82f98c699a2f2dMark Andrews case 'T': /* NOT DOCUMENTED */
6fcb2f0faad67a6d2cb2e30ec57157d75fbfe58fAndreas Gustafsson /*
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson * clienttest: make clients single shot with their
8f3dd8f8e73e4465221a5297819db70e6b383138Mark Andrews * own memory context.
6e9efadbea9febb0494e713e54dfea6f7ef70383Mark Andrews * delay=xxxx: delay client responses by xxxx ms to
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews * simulate remote servers.
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews */
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews if (!strcmp(isc_commandline_argument, "clienttest"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews ns_g_clienttest = ISC_TRUE;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strcmp(isc_commandline_argument, "nosoa"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews ns_g_nosoa = ISC_TRUE;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strcmp(isc_commandline_argument, "noaa"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews ns_g_noaa = ISC_TRUE;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strcmp(isc_commandline_argument, "maxudp512"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews maxudp = 512;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strcmp(isc_commandline_argument, "maxudp1460"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews maxudp = 1460;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strcmp(isc_commandline_argument, "dropedns"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews ns_g_dropedns = ISC_TRUE;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strcmp(isc_commandline_argument, "noedns"))
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews ns_g_noedns = ISC_TRUE;
43fe2897fc80bbec2115310ca79d432a252f3ea4Mark Andrews else if (!strncmp(isc_commandline_argument,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson "maxudp=", 7))
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews maxudp = atoi(isc_commandline_argument + 7);
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews else if (!strncmp(isc_commandline_argument,
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews "delay=", 6))
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews ns_g_delay = atoi(isc_commandline_argument + 6);
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews else
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews fprintf(stderr, "unknown -T flag '%s\n",
754cca729dd82ae8363917dc00ad44f9d900635bMark Andrews isc_commandline_argument);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson break;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont case 'U':
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont ns_g_udpdisp = parse_int(isc_commandline_argument,
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont "number of UDP listeners "
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont "per interface");
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont break;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont case 'u':
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont ns_g_username = isc_commandline_argument;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont break;
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont case 'v':
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont printf("BIND %s\n", ns_g_version);
339c130823ff00cdfa70d1f0a8922199aa37f33cMark Andrews exit(0);
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont case 'V':
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont printf("BIND %s <id:%s> built with %s\n",
debd489a44363870f96f75818e89ec27d3cab736Francis Dupont ns_g_version, ns_g_srcid, ns_g_configargs);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews#ifdef OPENSSL
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews printf("using OpenSSL version: %s\n",
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews OPENSSL_VERSION_TEXT);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews#endif
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews#ifdef HAVE_LIBXML2
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews printf("using libxml2 version: %s\n",
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews LIBXML_DOTTED_VERSION);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews#endif
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews exit(0);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews case 'F':
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews /* Reserved for FIPS mode */
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews /* FALLTHROUGH */
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews case '?':
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews usage();
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews if (isc_commandline_option == '?')
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews exit(0);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews ns_main_earlyfatal("unknown option '-%c'",
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews isc_commandline_option);
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews /* FALLTHROUGH */
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews default:
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_main_earlyfatal("parsing options returned %d", ch);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews argc -= isc_commandline_index;
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews argv += isc_commandline_index;
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews POST(argv);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews if (argc > 0) {
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews usage();
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews ns_main_earlyfatal("extra command line arguments");
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews }
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews}
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrewsstatic isc_result_t
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrewscreate_managers(void) {
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews isc_result_t result;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 unsigned int socks;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉#ifdef ISC_PLATFORM_USETHREADS
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (ns_g_cpus == 0)
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_g_cpus = ns_g_cpus_detected;
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews#else
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 ns_g_cpus = 1;
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews#endif
ddb35cf2f301ae1c3fa601792034f6d349efc8c5Mark Andrews#ifdef WIN32
ddb35cf2f301ae1c3fa601792034f6d349efc8c5Mark Andrews ns_g_udpdisp = 1;
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews#else
ddb35cf2f301ae1c3fa601792034f6d349efc8c5Mark Andrews if (ns_g_udpdisp == 0 || ns_g_udpdisp > ns_g_cpus)
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews ns_g_udpdisp = ns_g_cpus;
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews#endif
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews ISC_LOG_INFO, "using %u UDP listener%s per interface",
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews ns_g_udpdisp, ns_g_udpdisp == 1 ? "" : "s");
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews if (result != ISC_R_SUCCESS) {
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews "isc_taskmgr_create() failed: %s",
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews isc_result_totext(result));
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews return (ISC_R_UNEXPECTED);
b123b265e3a3d9b72a14230b6517e0f6fdb5c5b5Mark Andrews }
b123b265e3a3d9b72a14230b6517e0f6fdb5c5b5Mark Andrews
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
ddb35cf2f301ae1c3fa601792034f6d349efc8c5Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
ddb35cf2f301ae1c3fa601792034f6d349efc8c5Mark Andrews "isc_timermgr_create() failed: %s",
ddb35cf2f301ae1c3fa601792034f6d349efc8c5Mark Andrews isc_result_totext(result));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_UNEXPECTED);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 if (result != ISC_R_SUCCESS) {
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 UNEXPECTED_ERROR(__FILE__, __LINE__,
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 "isc_socketmgr_create() failed: %s",
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 isc_result_totext(result));
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 return (ISC_R_UNEXPECTED);
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews }
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews isc__socketmgr_maxudp(ns_g_socketmgr, maxudp);
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews if (result == ISC_R_SUCCESS) {
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
c0a76b3c0b42a110e14eb56103973944900400c4Mark Andrews NS_LOGMODULE_SERVER,
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews ISC_LOG_INFO, "using up to %u sockets", socks);
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews }
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews if (result != ISC_R_SUCCESS) {
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews UNEXPECTED_ERROR(__FILE__, __LINE__,
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews "isc_entropy_create() failed: %s",
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews isc_result_totext(result));
3dfa202e4fea6b985bcf8761e2d11c176baa40d1Mark Andrews return (ISC_R_UNEXPECTED);
386d3a99c190bad55edf44d076e6bd087e230ab8Tatuya JINMEI 神明達哉 }
40dd9cb8cc240c33d820fe79f176ed51e4c06a1aMark Andrews
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson if (result != ISC_R_SUCCESS) {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson UNEXPECTED_ERROR(__FILE__, __LINE__,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson "isc_hash_create() failed: %s",
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_result_totext(result));
963c48ba4d06a112c70d50328e827749e95f58dbMark Andrews return (ISC_R_UNEXPECTED);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson }
963c48ba4d06a112c70d50328e827749e95f58dbMark Andrews
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson return (ISC_R_SUCCESS);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson}
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafssonstatic void
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafssondestroy_managers(void) {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson ns_lwresd_shutdown();
a1898260ad19d02e88ab76c1855d33c67add9defMark Andrews
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_entropy_detach(&ns_g_entropy);
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews if (ns_g_fallbackentropy != NULL)
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_entropy_detach(&ns_g_fallbackentropy);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson /*
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson * isc_taskmgr_destroy() will block until all tasks have exited,
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson */
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_taskmgr_destroy(&ns_g_taskmgr);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_timermgr_destroy(&ns_g_timermgr);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_socketmgr_destroy(&ns_g_socketmgr);
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
a1898260ad19d02e88ab76c1855d33c67add9defMark Andrews /*
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson * isc_hash_destroy() cannot be called as long as a resolver may be
289ae548d52bc8f982d9823af64cafda7bd92232Mark Andrews * running. Calling this after isc_taskmgr_destroy() ensures the
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson * call is safe.
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson */
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_hash_destroy();
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson}
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafssonstatic void
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafssondump_symboltable() {
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson int i;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson isc_result_t result;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson const char *fname;
5ff133b82082d82f0ba89b7c999c6b62b6298e46Andreas Gustafsson const void *addr;
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff if (isc__backtrace_nsymbols == 0)
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff return;
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff
90407942d3afe50f04ccea361de3b164a5a1702dMichael Graff if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99)))
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff return;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_DEBUG(99), "Symbol table:");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt addr = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt fname = NULL;
13faa8b6a2d0d45e0659049983928366252ab3faMichael Graff result = isc_backtrace_getsymbolfromindex(i, &addr, &fname);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS) {
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
d0803df3310ad09447c34b972e7594d576f5cbb5Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_DEBUG(99),
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrews "[%d] %p %s", i, addr, fname);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrews }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrews
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrewsstatic void
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrewssetup(void) {
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrews isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_resourcevalue_t old_openfiles;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef HAVE_LIBSCF
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt char *instance = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
a53259c4cc558f86dd008eccc60cc89b6734a03cMark Andrews /*
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington * Get the user and group information before changing the root
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * directory, so the administrator does not need to keep a copy
c5826852e6c789f59b301f8197e65a1dd4e09a44Mark Andrews * of the user and group databases in the chroot'ed environment.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_inituserinfo(ns_g_username);
568de8123acb1a94e2d7bfe9cc5eb5d099f6c1f5Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Initialize time conversion information
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_tzset();
c5826852e6c789f59b301f8197e65a1dd4e09a44Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_opendevnull();
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef HAVE_LIBSCF
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* Check if named is under smf control, before chroot. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
c5826852e6c789f59b301f8197e65a1dd4e09a44Mark Andrews /* We don't care about instance, just check if we got one. */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_smf_got_instance = 1;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt else
bb70b45c1f3869ec81678fc857e3dfad60da5de4Evan Hunt ns_smf_got_instance = 0;
bb70b45c1f3869ec81678fc857e3dfad60da5de4Evan Hunt if (instance != NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_free(ns_g_mctx, instance);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* HAVE_LIBSCF */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef PATH_RANDOMDEV
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Initialize system's random device as fallback entropy source
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * if running chroot'ed.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (ns_g_chrootdir != NULL) {
5989aea4bbe79e09290792f04aeb557e2b2da02eAndreas Gustafsson result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("isc_entropy_create() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_entropy_createfilesource(ns_g_fallbackentropy,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt PATH_RANDOMDEV);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlywarning("could not open pre-chroot "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "entropy source %s: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt PATH_RANDOMDEV,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews isc_entropy_detach(&ns_g_fallbackentropy);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
d73de275987d29627dc11d5bd4a22874a29f7874Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef ISC_PLATFORM_USETHREADS
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Check for the number of cpu's before ns_os_chroot().
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_g_cpus_detected = isc_os_ncpus();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_chroot(ns_g_chrootdir);
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * For operating systems which have a capability mechanism, now
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * is the time to switch to minimal privs and change our user id.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * On traditional UNIX systems, this call will be a no-op, and we
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews * will change the user ID after reading the config file the first
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * time. (We need to read the config file to know which possibly
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * privileged ports to bind() to.)
d73de275987d29627dc11d5bd4a22874a29f7874Mark Andrews */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_minprivs();
fda0a038810529d6e45b17822ddcc61d82964e83Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ns_log_init(ISC_TF(ns_g_username != NULL));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
fda0a038810529d6e45b17822ddcc61d82964e83Mark Andrews ns_main_earlyfatal("ns_log_init() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Now is the time to daemonize (if we're not running in the
aaaf8d4f4873d21e55c3ffb4f656203d08339865Mark Andrews * foreground). We waited until now because we wanted to get
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * a valid logging context setup. We cannot daemonize any later,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * because calling create_managers() will create threads, which
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * would be lost after fork().
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (!ns_g_foreground)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_daemonize();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * We call isc_app_start() here as some versions of FreeBSD's fork()
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * destroys all the signal handling it sets up.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_app_start();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("isc_app_start() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt saved_command_line);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
d7729155dff87d3c7a2b9103bf6e5164ea4d7dd7Mark Andrews ISC_LOG_NOTICE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "----------------------------------------------------");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "BIND 9 is maintained by Internet Systems Consortium,");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "Inc. (ISC), a non-profit 501(c)(3) public-benefit ");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE,
b123b265e3a3d9b72a14230b6517e0f6fdb5c5b5Mark Andrews "corporation. Support and training for BIND 9 are ");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "available at https://www.isc.org/support");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "----------------------------------------------------");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dump_symboltable();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Get the initial resource limits.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void)isc_resource_getlimit(isc_resource_stacksize,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &ns_g_initstacksize);
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt (void)isc_resource_getlimit(isc_resource_datasize,
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt &ns_g_initdatasize);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews (void)isc_resource_getlimit(isc_resource_coresize,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &ns_g_initcoresize);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (void)isc_resource_getlimit(isc_resource_openfiles,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &ns_g_initopenfiles);
37dee1ff94960a61243f611c0f87f8c316815c53Mark Andrews
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt * System resources cannot effectively be tuned on some systems.
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt * Raise the limit in such cases for safety.
e09cdbac087b88524ac40e943d040e2a032c48f2Mark Andrews */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt old_openfiles = ns_g_initopenfiles;
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt ns_os_adjustnofile();
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt (void)isc_resource_getlimit(isc_resource_openfiles,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt &ns_g_initopenfiles);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (old_openfiles != ns_g_initopenfiles) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "adjusted limit on open files from "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "%" ISC_PRINT_QUADFORMAT "u to "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "%" ISC_PRINT_QUADFORMAT "u",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt old_openfiles, ns_g_initopenfiles);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * If the named configuration filename is relative, prepend the current
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * directory's name before possibly changing to another directory.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (! isc_file_isabsolute(ns_g_conffile)) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_file_absolutepath(ns_g_conffile,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt absolute_conffile,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeof(absolute_conffile));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("could not construct absolute path "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "of configuration file: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_g_conffile = absolute_conffile;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Record the server's startup time.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_time_now(&ns_g_boottime);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("isc_time_now() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt result = create_managers();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("create_managers() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_builtin_init();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Add calls to register sdb drivers here.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* xxdb_init(); */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef ISC_DLZ_DLOPEN
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Register the DLZ "dlopen" driver.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt result = dlz_dlopen_init(ns_g_mctx);
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt if (result != ISC_R_SUCCESS)
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt ns_main_earlyfatal("dlz_dlopen_init() failed: %s",
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt isc_result_totext(result));
f0e9d6e905235482ff9b4be770ee35018fcd8234Evan Hunt#endif
0277def3cf21a0b52bdb50a9b449c37b2139b321Mark Andrews
0277def3cf21a0b52bdb50a9b449c37b2139b321Mark Andrews#if CONTRIB_DLZ
0277def3cf21a0b52bdb50a9b449c37b2139b321Mark Andrews /*
0277def3cf21a0b52bdb50a9b449c37b2139b321Mark Andrews * Register any other contributed DLZ drivers.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = dlz_drivers_init();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("dlz_drivers_init() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
55fff76a16c8c822bb7bb8bc63ed26ede9fbeb9cMark Andrews ns_server_create(ns_g_mctx, &ns_g_server);
55fff76a16c8c822bb7bb8bc63ed26ede9fbeb9cMark Andrews}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic void
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntcleanup(void) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt destroy_managers();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_server_destroy(&ns_g_server);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_builtin_deinit();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Add calls to unregister sdb drivers here.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /* xxdb_clear(); */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef CONTRIB_DLZ
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Unregister contributed DLZ drivers.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dlz_drivers_clear();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef ISC_DLZ_DLOPEN
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Unregister "dlopen" DLZ driver.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dlz_dlopen_clear();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_name_destroy();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ISC_LOG_NOTICE, "exiting");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_log_shutdown();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntstatic char *memstats = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntvoid
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_main_setmemstats(const char *filename) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Caller has to ensure locking.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (memstats != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt free(memstats);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt memstats = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (filename == NULL)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt memstats = malloc(strlen(filename) + 1);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (memstats)
5f9e583552f53de12062bfff12e47250abce378fBrian Wellington strcpy(memstats, filename);
08a768e82ad64ede97f640c88e02984b59122753Michael Graff}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef HAVE_LIBSCF
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt/*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Get FMRI for the named process.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntisc_result_t
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_handle_t *h = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt int namelen;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt char *instance;
08a768e82ad64ede97f640c88e02984b59122753Michael Graff
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt REQUIRE(ins_name != NULL && *ins_name == NULL);
08a768e82ad64ede97f640c88e02984b59122753Michael Graff
08a768e82ad64ede97f640c88e02984b59122753Michael Graff if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (debug)
08a768e82ad64ede97f640c88e02984b59122753Michael Graff UNEXPECTED_ERROR(__FILE__, __LINE__,
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont "scf_handle_create() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_strerror(scf_error()));
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont return (ISC_R_FAILURE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (scf_handle_bind(h) == -1) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (debug)
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont UNEXPECTED_ERROR(__FILE__, __LINE__,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "scf_handle_bind() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_strerror(scf_error()));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_handle_destroy(h);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_R_FAILURE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if ((namelen = scf_myname(h, NULL, 0)) == -1) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (debug)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "scf_myname() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_strerror(scf_error()));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_handle_destroy(h);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_R_FAILURE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "ns_smf_get_instance memory "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "allocation failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(ISC_R_NOMEMORY));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_handle_destroy(h);
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont return (ISC_R_FAILURE);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont if (scf_myname(h, instance, namelen + 1) == -1) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (debug)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "scf_myname() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_strerror(scf_error()));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_handle_destroy(h);
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont isc_mem_free(mctx, instance);
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont return (ISC_R_FAILURE);
c1d7e0562f6a72ecc07ab5140cf2b88183adbd08Francis Dupont }
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt scf_handle_destroy(h);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt *ins_name = instance;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (ISC_R_SUCCESS);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif /* HAVE_LIBSCF */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntint
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Huntmain(int argc, char *argv[]) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_t result;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef HAVE_LIBSCF
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt char *instance = NULL;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont * Record version in core image.
1ea6e09c376b1351c614474a88675b1a9bda6571Evan Hunt * strings named.core | grep "named version:"
1ea6e09c376b1351c614474a88675b1a9bda6571Evan Hunt */
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont strlcat(version,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#if defined(NO_VERSION_DATE) || !defined(__DATE__)
1ea6e09c376b1351c614474a88675b1a9bda6571Evan Hunt "named version: BIND " VERSION " <" SRCID ">",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#else
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "named version: BIND " VERSION " <" SRCID "> (" __DATE__ ")",
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont#endif
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt sizeof(version));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_file_progname(*argv, program_name, sizeof(program_name));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("program name too long");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (strcmp(program_name, "lwresd") == 0)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_g_lwresdonly = ISC_TRUE;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("failed to build internal symbol table");
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_assertion_setcallback(assertion_failed);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_error_setfatal(library_fatal_error);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_error_setunexpected(library_unexpected_error);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_init(program_name);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dns_result_register();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt dst_result_register();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isccc_result_register();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt parse_command_line(argc, argv);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Warn about common configuration error.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (ns_g_chrootdir != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt int len = strlen(ns_g_chrootdir);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlywarning("config filename (-c %s) contains "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "chroot path (-t %s)",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_g_conffile, ns_g_chrootdir);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_mem_create(0, 0, &ns_g_mctx);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result != ISC_R_SUCCESS)
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_main_earlyfatal("isc_mem_create() failed: %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_mem_setname(ns_g_mctx, "main", NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt setup();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Start things running and then wait for a shutdown request
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * or reload.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt do {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = isc_app_run();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_RELOAD) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_server_reloadwanted(ns_g_server);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } else if (result != ISC_R_SUCCESS) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt UNEXPECTED_ERROR(__FILE__, __LINE__,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "isc_app_run(): %s",
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt isc_result_totext(result));
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt /*
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt * Force exit.
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt */
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt result = ISC_R_SUCCESS;
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt }
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt } while (result != ISC_R_SUCCESS);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt#ifdef HAVE_LIBSCF
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (ns_smf_want_disable == 1) {
2a31bd531072824ef252c18303859d6af7451b00Francis Dupont result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (result == ISC_R_SUCCESS && instance != NULL) {
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt if (smf_disable_instance(instance, 0) != 0)
a631b30b1ddd8b2ea780371d0d99ba1c05bc7e42Francis Dupont UNEXPECTED_ERROR(__FILE__, __LINE__,
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "smf_disable_instance() "
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt "failed for %s : %s",
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt instance,
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt scf_strerror(scf_error()));
1831311ac6179951c8fcca75aa29dc2f5c0218b9Francis Dupont }
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt if (instance != NULL)
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mem_free(ns_g_mctx, instance);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt }
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt#endif /* HAVE_LIBSCF */
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt cleanup();
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt if (want_stats) {
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mem_stats(ns_g_mctx, stdout);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mutex_stats(stdout);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt }
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt if (ns_g_memstatistics && memstats != NULL) {
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt FILE *fp = NULL;
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt result = isc_stdio_open(memstats, "w", &fp);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt if (result == ISC_R_SUCCESS) {
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mem_stats(ns_g_mctx, fp);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mutex_stats(fp);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_stdio_close(fp);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt }
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt }
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mem_destroy(&ns_g_mctx);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt isc_mem_checkdestroyed(stderr);
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt
8cbf3b6fc35091abde426930f2eadb8f53476c98Evan Hunt ns_main_setmemstats(NULL);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt isc_app_finish();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt ns_os_closedevnull();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
501941f0b6cce74c2ff75b10aff3f230d5d37e4cEvan Hunt ns_os_shutdown();
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt return (0);
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt}
ba751492fcc4f161a18b983d4f018a1a52938cb9Evan Hunt