main.c revision 76c8294c81fb48b1da6e1fc5b83322a4cedb8e58
/*
* Copyright (C) 1999-2001 Internet Software Consortium.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* $Id: main.c,v 1.119 2001/08/08 22:54:20 gson Exp $ */
#include <config.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <isc/commandline.h>
#include <isc/platform.h>
#include <isc/resource.h>
#include <dns/dispatch.h>
/*
* Defining NS_MAIN provides storage declarations (rather than extern)
*/
#define NS_MAIN 1
#include <named/interfacemgr.h>
/*
* Include header files for database drivers here.
*/
/* #include "xxdb.h" */
static char absolute_conffile[ISC_DIR_PATHMAX];
static char saved_command_line[512];
void
ns_main_earlyfatal(const char *format, ...) {
"exiting (due to early fatal error)");
} else {
}
exit(1);
}
static void
const char *cond)
{
/*
* Handle assertion failures.
*/
/*
* Reset the assetion callback in case it is the log
* routines causing the assertion.
*/
"exiting (due to assertion failure)");
} else {
}
if (ns_g_coreok)
abort();
exit(1);
}
static void
static void
{
/*
* Handle isc_error_fatal() calls from our libraries.
*/
/*
* Reset the error callback in case it is the log
* routines causing the assertion.
*/
"exiting (due to fatal error in library)");
} else {
}
if (ns_g_coreok)
abort();
exit(1);
}
static void
static void
{
/*
* Handle isc_error_unexpected() calls from our libraries.
*/
} else {
}
}
static void
lwresd_usage(void) {
"usage: lwresd [-c conffile | -C resolvconffile] "
"[-d debuglevel] [-f|-g]\n"
" [-n number_of_cpus] [-p port]"
"[-P listen-port] [-s]\n"
" [-t chrootdir] [-u username] [-i pidfile]\n");
}
static void
usage(void) {
if (ns_g_lwresdonly) {
lwresd_usage();
return;
}
"usage: named [-c conffile] [-d debuglevel] "
"[-f|-g] [-n number_of_cpus]\n"
" [-p port] [-s] [-t chrootdir] [-u username]\n");
}
static void
int i;
char *src;
char *dst;
char *eob;
const char truncated[] = "...";
*dst++ = ' ';
/*
* This won't perfectly produce a shell-independent
* pastable command line in all circumstances, but
* comes close, and for practical purposes will
* nearly always be fine.
*/
} else {
*dst++ = '\\';
}
}
}
else
*dst = '\0';
}
static int
char *endp;
int tmp;
long int ltmp;
if (*endp != '\0')
return (tmp);
}
static void
int ch;
int port;
"c:C:d:fgi:ln:N:p:P:st:u:vx:")) !=
-1) {
switch (ch) {
case 'c':
ns_main_earlyfatal("cannot specify -c and -C");
break;
case 'C':
if (ns_g_conffileset)
ns_main_earlyfatal("cannot specify -c and -C");
break;
case 'd':
"debug level");
break;
case 'f':
break;
case 'g':
break;
/* XXXBEW -i should be removed */
case 'i':
break;
case 'l':
break;
case 'N': /* Deprecated. */
case 'n':
"number of cpus");
if (ns_g_cpus == 0)
ns_g_cpus = 1;
break;
case 'p':
ns_main_earlyfatal("port '%s' out of range",
break;
/* XXXBEW Should -P be removed? */
case 'P':
ns_main_earlyfatal("port '%s' out of range",
break;
case 's':
/* XXXRTH temporary syntax */
break;
case 't':
/* XXXJAB should we make a copy? */
break;
case 'u':
break;
case 'v':
exit(0);
case '?':
usage();
ns_main_earlyfatal("unknown option '-%c'",
default:
}
}
if (argc > 0) {
usage();
ns_main_earlyfatal("extra command line arguments");
}
}
static isc_result_t
create_managers(void) {
#ifdef ISC_PLATFORM_USETHREADS
if (ns_g_cpus == 0)
ns_g_cpus = isc_os_ncpus();
#else
ns_g_cpus = 1;
#endif
ISC_LOG_INFO, "using %u CPU%s",
if (result != ISC_R_SUCCESS) {
"ns_taskmgr_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"ns_timermgr_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_socketmgr_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
if (result != ISC_R_SUCCESS) {
"isc_entropy_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
static void
destroy_managers(void) {
/*
* isc_taskmgr_destroy() will block until all tasks have exited,
*/
}
static void
setup(void) {
/*
* Get the user and group information before changing the root
* directory, so the administrator does not need to keep a copy
* of the user and group databases in the chroot'ed environment.
*/
/*
* For operating systems which have a capability mechanism, now
* is the time to switch to minimal privs and change our user id.
* On traditional UNIX systems, this call will be a no-op, and we
* will change the user ID after reading the config file the first
* time. (We need to read the config file to know which possibly
* privileged ports to bind() to.)
*/
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("ns_log_init() failed: %s",
/*
* Now is the time to daemonize (if we're not running in the
* foreground). We waited until now because we wanted to get
* a valid logging context setup. We cannot daemonize any later,
* because calling create_managers() will create threads, which
* would be lost after fork().
*/
if (!ns_g_foreground)
/*
* Get the initial resource limits.
*/
/*
* If the named configuration filename is relative, prepend the current
* directory's name before possibly changing to another directory.
*/
if (! isc_file_isabsolute(ns_g_conffile)) {
sizeof(absolute_conffile));
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("could not construct absolute path of "
"configuration file: %s",
}
result = create_managers();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("create_managers() failed: %s",
/*
* Add calls to register sdb drivers here.
*/
/* xxdb_init(); */
}
static void
cleanup(void) {
/*
* Add calls to unregister sdb drivers here.
*/
/* xxdb_clear(); */
ISC_LOG_NOTICE, "exiting");
}
int
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("program name too long");
result = isc_app_start();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_app_start() failed: %s",
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_mem_create() failed: %s",
setup();
/*
* Start things running and then wait for a shutdown request
* or reload.
*/
do {
result = isc_app_run();
if (result == ISC_R_RELOAD) {
} else if (result != ISC_R_SUCCESS) {
"isc_app_run(): %s",
/*
* Force exit.
*/
}
} while (result != ISC_R_SUCCESS);
cleanup();
if (want_stats) {
}
return (0);
}