main.c revision 6b8ab03dc8f53a9cbf0d932e5c2eef558d41005a
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1999-2001 Internet Software Consortium.
2f072c2982861ec2e86e97f8a3ed199fe45c1f70Michael Graff *
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Permission to use, copy, modify, and distribute this software for any
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * purpose with or without fee is hereby granted, provided that the above
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * copyright notice and this permission notice appear in all copies.
2f072c2982861ec2e86e97f8a3ed199fe45c1f70Michael Graff *
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence/* $Id: main.c,v 1.103 2001/01/11 23:46:10 bwelling Exp $ */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley#include <config.h>
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley#include <ctype.h>
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley#include <stdlib.h>
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley#include <string.h>
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#include <isc/app.h>
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#include <isc/commandline.h>
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews#include <isc/entropy.h>
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff#include <isc/os.h>
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff#include <isc/platform.h>
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff#include <isc/resource.h>
d981ca645597116d227a48bf37cc5edc061c854dBob Halley#include <isc/task.h>
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews#include <isc/timer.h>
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews#include <isc/util.h>
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews#include <dns/dispatch.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <dst/result.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <dns/view.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence/*
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews * Defining NS_MAIN provides storage declarations (rather than extern)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * for variables in named/globals.h.
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#define NS_MAIN 1
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <named/globals.h> /* Explicit, though named/log.h includes it. */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence#include <named/interfacemgr.h>
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence#include <named/log.h>
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence#include <named/omapi.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <named/os.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <named/server.h>
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence#include <named/lwresd.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <named/main.h>
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews/*
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Include header files for database drivers here.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence/* #include "xxdb.h" */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewsstatic isc_boolean_t want_stats = ISC_FALSE;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewsstatic const char * program_name = "named";
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewsstatic char saved_command_line[512];
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrencevoid
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrencens_main_earlyfatal(const char *format, ...) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews va_list args;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews va_start(args, format);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (ns_g_lctx != NULL) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews format, args);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "exiting (due to early fatal error)");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews } else {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fprintf(stderr, "%s: ", program_name);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence vfprintf(stderr, format, args);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fprintf(stderr, "\n");
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews fflush(stderr);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff va_end(args);
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews exit(1);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews}
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrence
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrewsstatic void
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrenceassertion_failed(const char *file, int line, isc_assertiontype_t type,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews const char *cond)
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews{
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /*
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Handle assertion failures.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (ns_g_lctx != NULL) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /*
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Reset the assetion callback in case it is the log
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * routines causing the assertion.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_assertion_setcallback(NULL);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "%s:%d: %s(%s) failed", file, line,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_assertion_typetotext(type), cond);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "exiting (due to assertion failure)");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews } else {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fprintf(stderr, "%s:%d: %s(%s) failed\n",
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews file, line, isc_assertion_typetotext(type), cond);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fflush(stderr);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (ns_g_coreok)
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence abort();
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews exit(1);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews}
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonstatic void
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonlibrary_fatal_error(const char *file, int line, const char *format,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson va_list args)
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley{
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /*
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Handle isc_error_fatal() calls from our libraries.
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews if (ns_g_lctx != NULL) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /*
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Reset the error callback in case it is the log
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * routines causing the assertion.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_error_setfatal(NULL);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews "%s:%d: fatal error:", file, line);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
5901928ef856543882f38b2318e8991ce2644d2bMark Andrews NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson format, args);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
2cd8a160b9e2c0c7a016b534652b5c909f36ed4aMark Andrews NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews "exiting (due to fatal error in library)");
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews } else {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fprintf(stderr, "%s:%d: fatal error: ", file, line);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence vfprintf(stderr, format, args);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fprintf(stderr, "\n");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fflush(stderr);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (ns_g_coreok)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews abort();
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence exit(1);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence}
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewsstatic void
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewslibrary_unexpected_error(const char *file, int line, const char *format,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews va_list args)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews{
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /*
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Handle isc_error_unexpected() calls from our libraries.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (ns_g_lctx != NULL) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "%s:%d: unexpected error:", file, line);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence format, args);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence } else {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fprintf(stderr, "%s:%d: fatal error: ", file, line);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews vfprintf(stderr, format, args);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff fprintf(stderr, "\n");
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews fflush(stderr);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews }
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews}
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrewsstatic void
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewslwresd_usage(void) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fprintf(stderr,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "usage: lwresd [-c conffile | -C resolvconffile] "
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "[-d debuglevel] [-f|-g]\n"
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews " [-n number_of_cpus] [-p port]"
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "[-P listen-port] [-s]\n"
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson " [-t chrootdir] [-u username] [-i pidfile]\n");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews}
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrencestatic void
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrenceusage(void) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (ns_g_lwresdonly) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews lwresd_usage();
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley return;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley }
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson fprintf(stderr,
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson "usage: named [-c conffile] [-d debuglevel] "
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson "[-f|-g] [-n number_of_cpus]\n"
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley " [-p port] [-s] [-t chrootdir] [-u username]\n");
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson}
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafssonstatic void
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafssonsave_command_line(int argc, char *argv[]) {
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafsson int i;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews char *src;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews char *dst;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence char *eob;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence const char truncated[] = "...";
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_boolean_t quoted = ISC_FALSE;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews dst = saved_command_line;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson eob = saved_command_line + sizeof(saved_command_line);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews for (i = 1; i < argc && dst < eob; i++) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence *dst++ = ' ';
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence src = argv[i];
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley while (*src != '\0' && dst < eob) {
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson /*
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson * This won't perfectly produce a shell-independent
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson * pastable command line in all circumstances, but
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson * comes close, and for practical purposes will
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson * nearly always be fine.
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson */
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson if (quoted || isalnum(*src & 0xff) ||
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson *src == '-' || *src == '_' ||
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson *src == '.' || *src == '/') {
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson *dst++ = *src++;
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson quoted = ISC_FALSE;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley } else {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews *dst++ = '\\';
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews quoted = ISC_TRUE;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews INSIST(sizeof(saved_command_line) >= sizeof(truncated));
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (dst == eob)
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews strcpy(eob - sizeof(truncated), truncated);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews else
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley *dst = '\0';
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley}
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrewsstatic int
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafssonparse_int(char *arg, const char *desc) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews char *endp;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence int tmp;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence long int ltmp;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews ltmp = strtol(arg, &endp, 10);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews tmp = (int) ltmp;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews if (*endp != '\0')
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (tmp < 0 || tmp != ltmp)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_main_earlyfatal("%s '%s' out of range", desc, arg);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews return (tmp);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews}
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrencestatic void
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewsparse_command_line(int argc, char *argv[]) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff int ch;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews int port;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews char *s;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews save_command_line(argc, argv);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence /*
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * See if we should run as lwresd.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews */
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff s = strrchr(argv[0], '/');
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (s == NULL)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews s = argv[0];
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff else
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews s++;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (strcmp(s, "lwresd") == 0)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_g_lwresdonly = ISC_TRUE;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_commandline_errprint = ISC_FALSE;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence while ((ch = isc_commandline_parse(argc, argv,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "c:C:d:fgi:ln:N:p:P:st:u:vx:")) !=
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews -1) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff switch (ch) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 'c':
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_g_conffile = isc_commandline_argument;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff lwresd_g_conffile = isc_commandline_argument;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (lwresd_g_useresolvconf)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_main_earlyfatal("cannot specify -c and -C");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_g_conffileset = ISC_TRUE;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews break;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff case 'C':
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews lwresd_g_resolvconffile = isc_commandline_argument;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (ns_g_conffileset)
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews ns_main_earlyfatal("cannot specify -c and -C");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews lwresd_g_useresolvconf = ISC_TRUE;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews break;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 'd':
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson ns_g_debuglevel = parse_int(isc_commandline_argument,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "debug level");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence break;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence case 'f':
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_g_foreground = ISC_TRUE;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews break;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews case 'g':
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews ns_g_foreground = ISC_TRUE;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews ns_g_logstderr = ISC_TRUE;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews break;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /* XXXBEW -i should be removed */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 'i':
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews lwresd_g_defaultpidfile = isc_commandline_argument;
4529cdaedaf1a0a5f8ff89aeca510b7a4475446cBob Halley break;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 'l':
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_g_lwresdonly = ISC_TRUE;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews break;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews case 'N': /* Deprecated. */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews case 'n':
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson ns_g_cpus = parse_int(isc_commandline_argument,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "number of cpus");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (ns_g_cpus == 0)
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ns_g_cpus = 1;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews break;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 'p':
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence port = parse_int(isc_commandline_argument, "port");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (port < 1 || port > 65535)
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("port '%s' out of range",
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_commandline_argument);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_g_port = port;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews break;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews /* XXXBEW Should -P be removed? */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews case 'P':
5901928ef856543882f38b2318e8991ce2644d2bMark Andrews port = parse_int(isc_commandline_argument, "port");
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson if (port < 1 || port > 65535)
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson ns_main_earlyfatal("port '%s' out of range",
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews isc_commandline_argument);
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews lwresd_g_listenport = port;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews break;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 's':
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /* XXXRTH temporary syntax */
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff want_stats = ISC_TRUE;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley break;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley case 't':
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington /* XXXJAB should we make a copy? */
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington ns_g_chrootdir = isc_commandline_argument;
b8dd48ecf83142f6ee7238cbd68fec455e527fc8Mark Andrews break;
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case 'u':
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley ns_g_username = isc_commandline_argument;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley break;
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley case 'v':
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley printf("BIND %s\n", ns_g_version);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews exit(0);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews case '?':
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson usage();
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington ns_main_earlyfatal("unknown option '-%c'",
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_commandline_option);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence default:
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("parsing options returned %d", ch);
5466ce3f279d9fa83ce826bcdc9482bc591152aeAndreas Gustafsson }
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence argc -= isc_commandline_index;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence argv += isc_commandline_index;
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington if (argc > 0) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence usage();
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("extra command line arguments");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington}
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrencestatic isc_result_t
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrencecreate_managers(void) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_result_t result;
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington#ifdef ISC_PLATFORM_USETHREADS
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence if (ns_g_cpus == 0)
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_g_cpus = isc_os_ncpus();
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence#else
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington ns_g_cpus = 1;
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington#endif
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ISC_LOG_INFO, "using %u CPU%s",
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington if (result != ISC_R_SUCCESS) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "ns_taskmgr_create() failed: %s",
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_result_totext(result));
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington return (ISC_R_UNEXPECTED);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington if (result != ISC_R_SUCCESS) {
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington "ns_timermgr_create() failed: %s",
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_result_totext(result));
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence return (ISC_R_UNEXPECTED);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence }
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington result = isc_socketmgr_create(ns_g_mctx, &ns_g_socketmgr);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington if (result != ISC_R_SUCCESS) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence UNEXPECTED_ERROR(__FILE__, __LINE__,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "isc_socketmgr_create() failed: %s",
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_result_totext(result));
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington return (ISC_R_UNEXPECTED);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence }
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington if (result != ISC_R_SUCCESS) {
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington UNEXPECTED_ERROR(__FILE__, __LINE__,
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington "isc_entropy_create() failed: %s",
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_result_totext(result));
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_UNEXPECTED);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews }
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff return (ISC_R_SUCCESS);
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews}
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellingtonstatic void
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellingtondestroy_managers(void) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews if (!ns_g_lwresdonly)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /*
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews * The omapi listeners need to be stopped here so that
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * isc_taskmgr_destroy() won't block on the omapi task.
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington */
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington ns_omapi_shutdown(ISC_TRUE);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ns_lwresd_shutdown();
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_entropy_detach(&ns_g_entropy);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence /*
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * isc_taskmgr_destroy() will block until all tasks have exited,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_taskmgr_destroy(&ns_g_taskmgr);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_timermgr_destroy(&ns_g_timermgr);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_socketmgr_destroy(&ns_g_socketmgr);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews}
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellingtonstatic void
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrencesetup(void) {
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_result_t result;
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington /*
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * Get the user and group information before changing the root
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * directory, so the administrator does not need to keep a copy
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * of the user and group databases in the chroot'ed environment.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_os_inituserinfo(ns_g_username);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington ns_os_chroot(ns_g_chrootdir);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence /*
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * For operating systems which have a capability mechanism, now
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * is the time to switch to minimal privs and change our user id.
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * On traditional UNIX systems, this call will be a no-op, and we
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * will change the user ID after reading the config file the first
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * time. (We need to read the config file to know which possibly
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * privileged ports to bind() to.)
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_os_minprivs();
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington result = ns_log_init(ISC_TF(ns_g_username != NULL));
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington if (result != ISC_R_SUCCESS)
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("ns_log_init() failed: %s",
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_result_totext(result));
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington /*
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * Now is the time to daemonize (if we're not running in the
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * foreground). We waited until now because we wanted to get
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * a valid logging context setup. We cannot daemonize any later,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * because calling create_managers() will create threads, which
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * would be lost after fork().
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews */
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (!ns_g_foreground)
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ns_os_daemonize();
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews saved_command_line);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence /*
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Get the initial resource limits.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence */
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington (void)isc_resource_getlimit(isc_resource_stacksize,
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington &ns_g_initstacksize);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington (void)isc_resource_getlimit(isc_resource_datasize,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence &ns_g_initdatasize);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence (void)isc_resource_getlimit(isc_resource_coresize,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence &ns_g_initcoresize);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews (void)isc_resource_getlimit(isc_resource_openfiles,
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews &ns_g_initopenfiles);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews result = create_managers();
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (result != ISC_R_SUCCESS)
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ns_main_earlyfatal("create_managers() failed: %s",
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington isc_result_totext(result));
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff /*
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews * Add calls to register sdb drivers here.
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews */
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews /* xxdb_init(); */
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ns_server_create(ns_g_mctx, &ns_g_server);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (!ns_g_lwresdonly) {
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews result = ns_omapi_init();
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews if (result != ISC_R_SUCCESS)
4529cdaedaf1a0a5f8ff89aeca510b7a4475446cBob Halley ns_main_earlyfatal("ns_omapi_init() failed: %s",
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews isc_result_totext(result));
b8dd48ecf83142f6ee7238cbd68fec455e527fc8Mark Andrews }
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington}
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellingtonstatic void
9281e7aa775026dc47c01745fdcc438645146877Mark Andrewscleanup(void) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews destroy_managers();
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington ns_server_destroy(&ns_g_server);
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews /*
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley * Add calls to unregister sdb drivers here.
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews */
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews /* xxdb_clear(); */
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff ISC_LOG_NOTICE, "exiting");
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ns_log_shutdown();
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafssonint
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halleymain(int argc, char *argv[]) {
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley isc_result_t result;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley program_name = argv[0];
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_assertion_setcallback(assertion_failed);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff isc_error_setfatal(library_fatal_error);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_error_setunexpected(library_unexpected_error);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff ns_os_init(program_name);
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley result = isc_app_start();
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson if (result != ISC_R_SUCCESS)
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley ns_main_earlyfatal("isc_app_start() failed: %s",
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley isc_result_totext(result));
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley result = isc_mem_create(0, 0, &ns_g_mctx);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley if (result != ISC_R_SUCCESS)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ns_main_earlyfatal("isc_mem_create() failed: %s",
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley isc_result_totext(result));
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews dns_result_register();
dst_result_register();
parse_command_line(argc, argv);
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);
cleanup();
if (want_stats) {
isc_mem_stats(ns_g_mctx, stdout);
isc_mutex_stats(stdout);
}
isc_mem_destroy(&ns_g_mctx);
isc_app_finish();
ns_os_shutdown();
return (0);
}