main.c revision 40f53fa8d9c6a4fc38c0014495e7a42b08f52481
/*
* 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.76 2000/08/01 01:11:51 tale Exp $ */
#include <config.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";
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 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] [-d debuglevel] "
"[-f|-g] [-n number_of_cpus]\n"
" [-p listen-port] [-P query-port] [-s] "
"[-t chrootdir] [-u username] [-i pidfile]\n");
}
static void
usage(void) {
"usage: named [-c conffile] [-d debuglevel] "
"[-f|-g] [-n number_of_cpus]\n"
" [-p port] [-s] [-t chrootdir] [-u username]\n");
}
static void
int ch;
unsigned int port;
"C:d:fgi:n:p:P:st:u:")) !=
-1) {
switch (ch) {
case 'C':
break;
case 'd':
break;
case 'f':
break;
case 'g':
break;
case 'i':
break;
case 'n':
if (ns_g_cpus == 0)
ns_g_cpus = 1;
break;
case 'p':
ns_main_earlyfatal("port '%s' out of range",
break;
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 '?':
lwresd_usage();
ns_main_earlyfatal("unknown option '-%c'",
default:
}
}
if (argc > 0) {
lwresd_usage();
ns_main_earlyfatal("extra command line arguments");
}
}
static void
int ch;
unsigned int port;
char *s;
if (s == NULL)
s = argv[0];
else
s++;
if (strcmp(s, "lwresd") == 0) {
return;
}
"c:d:fgn:N:p:st:u:x:")) !=
-1) {
switch (ch) {
case 'c':
break;
case 'd':
break;
case 'f':
break;
case 'g':
break;
case 'N': /* Deprecated. */
case 'n':
if (ns_g_cpus == 0)
ns_g_cpus = 1;
break;
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 '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();
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) {
if (!lwresd_only)
/*
* 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 (lwresd_only)
else
if (!lwresd_only) {
result = ns_omapi_init();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("ns_omapi_init() failed: %s",
}
}
static void
cleanup(void) {
if (lwresd_only)
else
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);
}