main.c revision 6b8ab03dc8f53a9cbf0d932e5c2eef558d41005a
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1999-2001 Internet Software Consortium.
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.
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.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence/* $Id: main.c,v 1.103 2001/01/11 23:46:10 bwelling Exp $ */
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews * Defining NS_MAIN provides storage declarations (rather than extern)
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * for variables in named/globals.h.
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews#include <named/globals.h> /* Explicit, though named/log.h includes it. */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Include header files for database drivers here.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence/* #include "xxdb.h" */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "exiting (due to early fatal error)");
63430de3450a99b7ae0cb05a95a26cff8cc66358David Lawrenceassertion_failed(const char *file, int line, isc_assertiontype_t type,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews const char *cond)
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Handle assertion failures.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Reset the assetion callback in case it is the log
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * routines causing the assertion.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "exiting (due to assertion failure)");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews file, line, isc_assertion_typetotext(type), cond);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafssonlibrary_fatal_error(const char *file, int line, const char *format,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Handle isc_error_fatal() calls from our libraries.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * Reset the error callback in case it is the log
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews * routines causing the assertion.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews "exiting (due to fatal error in library)");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence fprintf(stderr, "%s:%d: fatal error: ", file, line);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrewslibrary_unexpected_error(const char *file, int line, const char *format,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Handle isc_error_unexpected() calls from our libraries.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews fprintf(stderr, "%s:%d: fatal error: ", file, line);
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");
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");
fad44a20eede1bbc66716241dede225500c91caaAndreas Gustafssonsave_command_line(int argc, char *argv[]) {
a3c9e34301218c7092cd8b6add6eec0957ab5483Andreas Gustafsson eob = saved_command_line + sizeof(saved_command_line);
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.
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews INSIST(sizeof(saved_command_line) >= sizeof(truncated));
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_main_earlyfatal("%s '%s' out of range", desc, arg);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * See if we should run as lwresd.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence while ((ch = isc_commandline_parse(argc, argv,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "c:C:d:fgi:ln:N:p:P:st:u:vx:")) !=
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews ns_main_earlyfatal("cannot specify -c and -C");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews lwresd_g_resolvconffile = isc_commandline_argument;
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews ns_main_earlyfatal("cannot specify -c and -C");
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson ns_g_debuglevel = parse_int(isc_commandline_argument,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "debug level");
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /* XXXBEW -i should be removed */
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews lwresd_g_defaultpidfile = isc_commandline_argument;
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson ns_g_cpus = parse_int(isc_commandline_argument,
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews "number of cpus");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence port = parse_int(isc_commandline_argument, "port");
8d3e74b1683f714a484bbcf73249e8ee470e36d7Mark Andrews /* XXXBEW Should -P be removed? */
5901928ef856543882f38b2318e8991ce2644d2bMark Andrews port = parse_int(isc_commandline_argument, "port");
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson ns_main_earlyfatal("port '%s' out of range",
f427e7850928d15ffc37b1f68c60588995c9b318Mark Andrews /* XXXRTH temporary syntax */
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington /* XXXJAB should we make a copy? */
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("parsing options returned %d", ch);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("extra command line arguments");
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "ns_taskmgr_create() failed: %s",
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington "ns_timermgr_create() failed: %s",
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington result = isc_socketmgr_create(ns_g_mctx, &ns_g_socketmgr);
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence "isc_socketmgr_create() failed: %s",
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington "isc_entropy_create() failed: %s",
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews * The omapi listeners need to be stopped here so that
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington * isc_taskmgr_destroy() won't block on the omapi task.
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * isc_taskmgr_destroy() will block until all tasks have exited,
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 * 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.)
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington result = ns_log_init(ISC_TF(ns_g_username != NULL));
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence ns_main_earlyfatal("ns_log_init() failed: %s",
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 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence * Get the initial resource limits.
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington (void)isc_resource_getlimit(isc_resource_stacksize,
cf3f14106d082e4676431c10c54b60b9a0e9b127Brian Wellington (void)isc_resource_getlimit(isc_resource_datasize,
652c80435a97ca558a47e2f320047b73b3626cd1David Lawrence (void)isc_resource_getlimit(isc_resource_coresize,
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews (void)isc_resource_getlimit(isc_resource_openfiles,
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ns_main_earlyfatal("create_managers() failed: %s",
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews * Add calls to register sdb drivers here.
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews /* xxdb_init(); */
d10099d8663f4a4b21219f0c4bcc4f2b1ca1d877Bob Halley * Add calls to unregister sdb drivers here.
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews /* xxdb_clear(); */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
d981ca645597116d227a48bf37cc5edc061c854dBob Halley isc_error_setunexpected(library_unexpected_error);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ns_main_earlyfatal("isc_mem_create() failed: %s",
setup();
cleanup();
if (want_stats) {