main.c revision e9733bc599f30033ffe7af69f5fca254fb59d46e
/*
* Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
* Copyright (C) 1999-2003 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 ISC DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL ISC 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.150 2006/01/04 03:16:47 marka Exp $ */
/*! \file */
#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>
#ifdef HAVE_LIBSCF
#include <named/ns_smf_globals.h>
#endif
/*
* Include header files for database drivers here.
*/
/* #include "xxdb.h" */
/*
* Include DLZ drivers if appropriate.
*/
#ifdef DLZ
#include <dlz/dlz_drivers.h>
#endif
static char absolute_conffile[ISC_DIR_PATHMAX];
static char saved_command_line[512];
static char version[512];
void
ns_main_earlywarning(const char *format, ...) {
} else {
}
}
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 [-4|-6] [-c conffile | -C resolvconffile] "
"[-d debuglevel]\n"
" [-f|-g] [-n number_of_cpus] [-p port] "
"[-P listen-port] [-s]\n"
" [-t chrootdir] [-u username] [-i pidfile]\n"
" [-m {usage|trace|record|size|mctx}]\n");
}
static void
usage(void) {
if (ns_g_lwresdonly) {
lwresd_usage();
return;
}
"usage: named [-4|-6] [-c conffile] [-d debuglevel] "
"[-f|-g] [-n number_of_cpus]\n"
" [-p port] [-s] [-t chrootdir] [-u username]\n"
" [-m {usage|trace|record|size|mctx}]\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 struct flag_def {
const char *name;
unsigned int value;
} mem_debug_flags[] = {
{ "trace", ISC_MEM_DEBUGTRACE },
{ "record", ISC_MEM_DEBUGRECORD },
{ "usage", ISC_MEM_DEBUGUSAGE },
{ "size", ISC_MEM_DEBUGSIZE },
{ "mctx", ISC_MEM_DEBUGCTX },
{ NULL, 0 }
};
static void
for (;;) {
int arglen;
goto found;
}
}
if (*end == '\0')
break;
}
}
static void
int ch;
int port;
"46c:C:d:fgi:lm:n:N:p:P:st:u:vx:")) != -1) {
switch (ch) {
case '4':
if (disable4)
ns_main_earlyfatal("cannot specify -4 and -6");
if (isc_net_probeipv4() != ISC_R_SUCCESS)
ns_main_earlyfatal("IPv4 not supported by OS");
break;
case '6':
if (disable6)
ns_main_earlyfatal("cannot specify -4 and -6");
if (isc_net_probeipv6() != ISC_R_SUCCESS)
ns_main_earlyfatal("IPv6 not supported by OS");
break;
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 'm':
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
unsigned int cpus_detected;
#endif
#ifdef ISC_PLATFORM_USETHREADS
if (ns_g_cpus == 0)
ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
#else
ns_g_cpus = 1;
#endif
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);
}
if (result != ISC_R_SUCCESS) {
"isc_hash_create() failed: %s",
return (ISC_R_UNEXPECTED);
}
return (ISC_R_SUCCESS);
}
static void
destroy_managers(void) {
if (ns_g_fallbackentropy != NULL)
/*
* isc_taskmgr_destroy() will block until all tasks have exited,
*/
/*
* isc_hash_destroy() cannot be called as long as a resolver may be
* running. Calling this after isc_taskmgr_destroy() ensures the
* call is safe.
*/
}
static void
setup(void) {
#ifdef HAVE_LIBSCF
#endif
/*
* 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.
*/
/*
* Initialize time conversion information
*/
ns_os_tzset();
#ifdef HAVE_LIBSCF
/* Check if named is under smf control, before chroot. */
/* We don't care about instance, just check if we got one. */
if (result == ISC_R_SUCCESS)
ns_smf_got_instance = 1;
else
ns_smf_got_instance = 0;
#endif /* HAVE_LIBSCF */
#ifdef PATH_RANDOMDEV
/*
* Initialize system's random device as fallback entropy source
* if running chroot'ed.
*/
if (ns_g_chrootdir != NULL) {
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_entropy_create() failed: %s",
if (result != ISC_R_SUCCESS) {
ns_main_earlywarning("could not open pre-chroot "
"entropy source %s: %s",
}
}
#endif
/*
* 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)
/*
* We call isc_app_start() here as some versions of FreeBSD's fork()
* destroys all the signal handling it sets up.
*/
result = isc_app_start();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("isc_app_start() failed: %s",
/*
* 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(); */
#ifdef DLZ
/*
* Registyer any DLZ drivers.
*/
result = dlz_drivers_init();
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("dlz_drivers_init() failed: %s",
#endif
}
static void
cleanup(void) {
/*
* Add calls to unregister sdb drivers here.
*/
/* xxdb_clear(); */
#ifdef DLZ
/*
* Unregister any DLZ drivers.
*/
#endif
ISC_LOG_NOTICE, "exiting");
}
void
ns_main_setmemstats(const char *filename) {
/*
* Caller has to ensure locking.
*/
}
return;
if (memstats)
}
#ifdef HAVE_LIBSCF
/*
* Get FMRI for the named process.
*/
scf_handle_t *h = NULL;
int namelen;
char *instance;
if (debug)
"scf_handle_create() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
if (scf_handle_bind(h) == -1) {
if (debug)
"scf_handle_bind() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
if (debug)
"scf_myname() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
"ns_smf_get_instance memory "
"allocation failed: %s",
return (ISC_R_FAILURE);
}
if (debug)
"scf_myname() failed: %s",
scf_strerror(scf_error()));
return (ISC_R_FAILURE);
}
return (ISC_R_SUCCESS);
}
#endif /* HAVE_LIBSCF */
int
#ifdef HAVE_LIBSCF
#endif
/*
* Record version in core image.
* strings named.core | grep "named version:"
*/
#ifdef __DATE__
#else
"named version: BIND " VERSION,
#endif
sizeof(version));
if (result != ISC_R_SUCCESS)
ns_main_earlyfatal("program name too long");
/*
* Warn about common configuration error.
*/
if (ns_g_chrootdir != NULL) {
ns_main_earlywarning("config filename (-c %s) contains "
"chroot path (-t %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);
#ifdef HAVE_LIBSCF
if (ns_smf_want_disable == 1) {
if (smf_disable_instance(instance, 0) != 0)
"smf_disable_instance() ",
"failed for %s : %s",
scf_strerror(scf_error()));
}
}
#endif /* HAVE_LIBSCF */
cleanup();
if (want_stats) {
}
if (result == ISC_R_SUCCESS) {
}
}
return (0);
}