main.c revision 4587a7c3df4a8921b2df5c0d65fcb76f29c7e032
/*
* Copyright (C) 1999, 2000 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.90 2000/11/01 20:59:19 bwelling Exp $ */
#include <config.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <isc/commandline.h>
#include <dns/dispatch.h>
/*
* Defining NS_MAIN provides storage declaratons (rather than extern)
*/
#define NS_MAIN 1
#include <named/interfacemgr.h>
static const char * program_name = "named";
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
{
/*
* 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
{
/*
* 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 number_of_cpus]\n"
" [-p port] [-P listen-port] [-s] "
"[-t chrootdir]\n"
" [-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;
char *s;
/*
* See if we should run as lwresd.
*/
if (s == NULL)
s = argv[0];
else
s++;
if (strcmp(s, "lwresd") == 0)
"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 'x':
/* XXXRTH temporary syntax */
break;
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) {
if (ns_g_cpus == 0)
ns_g_cpus = isc_os_ncpus();
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);
}
#ifdef PATH_RANDOMDEV
#endif
return (ISC_R_SUCCESS);
}
static void
destroy_managers(void) {
if (!ns_g_lwresdonly)
/*
* The omapi listeners need to be stopped here so that
* isc_taskmgr_destroy() won't block on the omapi task.
*/
/*
* 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)
result = create_managers();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("create_managers() failed: %s",
if (!ns_g_lwresdonly) {
result = ns_omapi_init();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("ns_omapi_init() failed: %s",
}
}
static void
cleanup(void) {
ISC_LOG_NOTICE, "exiting");
}
int
program_name = argv[0];
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);
}