2 * Copyright (C) 2004-2013 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1999-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
29 #include <isc/backtrace.h>
30 #include <isc/commandline.h>
32 #include <isc/entropy.h>
36 #include <isc/platform.h>
37 #include <isc/print.h>
38 #include <isc/resource.h>
39 #include <isc/stdio.h>
40 #include <isc/string.h>
42 #include <isc/timer.h>
45 #include <isccc/result.h>
47 #include <dns/dispatch.h>
49 #include <dns/result.h>
52 #include <dst/result.h>
54 #include <dlz/dlz_dlopen_driver.h>
57 * Defining NS_MAIN provides storage declarations (rather than extern)
58 * for variables in named/globals.h.
62 #include <named/builtin.h>
63 #include <named/control.h>
64 #include <named/globals.h> /* Explicit, though named/log.h includes it. */
65 #include <named/interfacemgr.h>
66 #include <named/log.h>
68 #include <named/server.h>
69 #include <named/lwresd.h>
70 #include <named/main.h>
72 #include <named/ns_smf_globals.h>
76 #include <openssl/opensslv.h>
79 #include <libxml/xmlversion.h>
82 * Include header files for database drivers here.
84 /* #include "xxdb.h" */
88 * Include contributed DLZ drivers if appropriate.
90 #include <dlz/dlz_drivers.h>
94 * The maximum number of stack frames to dump on assertion failure.
96 #ifndef BACKTRACE_MAXFRAME
97 #define BACKTRACE_MAXFRAME 128
100 static isc_boolean_t want_stats = ISC_FALSE;
101 static char program_name[ISC_DIR_NAMEMAX] = "named";
102 static char absolute_conffile[ISC_DIR_PATHMAX];
103 static char saved_command_line[512];
104 static char version[512];
105 static unsigned int maxsocks = 0;
106 static int maxudp = 0;
109 ns_main_earlywarning(const char *format, ...) {
112 va_start(args, format);
113 if (ns_g_lctx != NULL) {
114 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
115 NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
118 fprintf(stderr, "%s: ", program_name);
119 vfprintf(stderr, format, args);
120 fprintf(stderr, "\n");
127 ns_main_earlyfatal(const char *format, ...) {
130 va_start(args, format);
131 if (ns_g_lctx != NULL) {
132 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
133 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
135 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
136 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
137 "exiting (due to early fatal error)");
139 fprintf(stderr, "%s: ", program_name);
140 vfprintf(stderr, format, args);
141 fprintf(stderr, "\n");
149 ISC_PLATFORM_NORETURN_PRE static void
150 assertion_failed(const char *file, int line, isc_assertiontype_t type,
151 const char *cond) ISC_PLATFORM_NORETURN_POST;
154 assertion_failed(const char *file, int line, isc_assertiontype_t type,
157 void *tracebuf[BACKTRACE_MAXFRAME];
160 const char *logsuffix = "";
164 * Handle assertion failures.
167 if (ns_g_lctx != NULL) {
169 * Reset the assertion callback in case it is the log
170 * routines causing the assertion.
172 isc_assertion_setcallback(NULL);
174 result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME,
176 if (result == ISC_R_SUCCESS && nframes > 0)
177 logsuffix = ", back trace";
178 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
179 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
180 "%s:%d: %s(%s) failed%s", file, line,
181 isc_assertion_typetotext(type), cond, logsuffix);
182 if (result == ISC_R_SUCCESS) {
183 for (i = 0; i < nframes; i++) {
184 unsigned long offset;
187 result = isc_backtrace_getsymbol(tracebuf[i],
190 if (result == ISC_R_SUCCESS) {
191 isc_log_write(ns_g_lctx,
192 NS_LOGCATEGORY_GENERAL,
195 "#%d %p in %s()+0x%lx", i,
199 isc_log_write(ns_g_lctx,
200 NS_LOGCATEGORY_GENERAL,
208 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
209 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
210 "exiting (due to assertion failure)");
212 fprintf(stderr, "%s:%d: %s(%s) failed\n",
213 file, line, isc_assertion_typetotext(type), cond);
222 ISC_PLATFORM_NORETURN_PRE static void
223 library_fatal_error(const char *file, int line, const char *format,
225 ISC_FORMAT_PRINTF(3, 0) ISC_PLATFORM_NORETURN_POST;
228 library_fatal_error(const char *file, int line, const char *format,
232 * Handle isc_error_fatal() calls from our libraries.
235 if (ns_g_lctx != NULL) {
237 * Reset the error callback in case it is the log
238 * routines causing the assertion.
240 isc_error_setfatal(NULL);
242 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
243 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
244 "%s:%d: fatal error:", file, line);
245 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
246 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
248 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
249 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
250 "exiting (due to fatal error in library)");
252 fprintf(stderr, "%s:%d: fatal error: ", file, line);
253 vfprintf(stderr, format, args);
254 fprintf(stderr, "\n");
264 library_unexpected_error(const char *file, int line, const char *format,
265 va_list args) ISC_FORMAT_PRINTF(3, 0);
268 library_unexpected_error(const char *file, int line, const char *format,
272 * Handle isc_error_unexpected() calls from our libraries.
275 if (ns_g_lctx != NULL) {
276 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
277 NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
278 "%s:%d: unexpected error:", file, line);
279 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
280 NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
283 fprintf(stderr, "%s:%d: fatal error: ", file, line);
284 vfprintf(stderr, format, args);
285 fprintf(stderr, "\n");
293 "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
295 " [-f|-g] [-n number_of_cpus] [-p port] "
296 "[-P listen-port] [-s]\n"
297 " [-t chrootdir] [-u username] [-i pidfile]\n"
298 " [-m {usage|trace|record|size|mctx}]\n");
303 if (ns_g_lwresdonly) {
308 "usage: named [-4|-6] [-c conffile] [-d debuglevel] "
309 "[-E engine] [-f|-g]\n"
310 " [-n number_of_cpus] [-p port] [-s] "
311 "[-t chrootdir] [-u username]\n"
312 " [-m {usage|trace|record|size|mctx}]\n");
316 save_command_line(int argc, char *argv[]) {
321 const char truncated[] = "...";
322 isc_boolean_t quoted = ISC_FALSE;
324 dst = saved_command_line;
325 eob = saved_command_line + sizeof(saved_command_line);
327 for (i = 1; i < argc && dst < eob; i++) {
331 while (*src != '\0' && dst < eob) {
333 * This won't perfectly produce a shell-independent
334 * pastable command line in all circumstances, but
335 * comes close, and for practical purposes will
336 * nearly always be fine.
338 if (quoted || isalnum(*src & 0xff) ||
339 *src == '-' || *src == '_' ||
340 *src == '.' || *src == '/') {
350 INSIST(sizeof(saved_command_line) >= sizeof(truncated));
353 strcpy(eob - sizeof(truncated), truncated);
359 parse_int(char *arg, const char *desc) {
364 ltmp = strtol(arg, &endp, 10);
367 ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
368 if (tmp < 0 || tmp != ltmp)
369 ns_main_earlyfatal("%s '%s' out of range", desc, arg);
373 static struct flag_def {
376 } mem_debug_flags[] = {
377 { "trace", ISC_MEM_DEBUGTRACE },
378 { "record", ISC_MEM_DEBUGRECORD },
379 { "usage", ISC_MEM_DEBUGUSAGE },
380 { "size", ISC_MEM_DEBUGSIZE },
381 { "mctx", ISC_MEM_DEBUGCTX },
386 set_flags(const char *arg, struct flag_def *defs, unsigned int *ret) {
388 const struct flag_def *def;
389 const char *end = strchr(arg, ',');
392 end = arg + strlen(arg);
393 arglen = (int)(end - arg);
394 for (def = defs; def->name != NULL; def++) {
395 if (arglen == (int)strlen(def->name) &&
396 memcmp(arg, def->name, arglen) == 0) {
401 ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg);
410 parse_command_line(int argc, char *argv[]) {
413 isc_boolean_t disable6 = ISC_FALSE;
414 isc_boolean_t disable4 = ISC_FALSE;
416 save_command_line(argc, argv);
418 /* PLEASE keep options synchronized when main is hooked! */
419 isc_commandline_errprint = ISC_FALSE;
420 while ((ch = isc_commandline_parse(argc, argv,
421 "46c:C:d:E:fFgi:lm:n:N:p:P:"
422 "sS:t:T:U:u:vVx:")) != -1) {
426 ns_main_earlyfatal("cannot specify -4 and -6");
427 if (isc_net_probeipv4() != ISC_R_SUCCESS)
428 ns_main_earlyfatal("IPv4 not supported by OS");
429 isc_net_disableipv6();
434 ns_main_earlyfatal("cannot specify -4 and -6");
435 if (isc_net_probeipv6() != ISC_R_SUCCESS)
436 ns_main_earlyfatal("IPv6 not supported by OS");
437 isc_net_disableipv4();
441 ns_g_conffile = isc_commandline_argument;
442 lwresd_g_conffile = isc_commandline_argument;
443 if (lwresd_g_useresolvconf)
444 ns_main_earlyfatal("cannot specify -c and -C");
445 ns_g_conffileset = ISC_TRUE;
448 lwresd_g_resolvconffile = isc_commandline_argument;
449 if (ns_g_conffileset)
450 ns_main_earlyfatal("cannot specify -c and -C");
451 lwresd_g_useresolvconf = ISC_TRUE;
454 ns_g_debuglevel = parse_int(isc_commandline_argument,
458 ns_g_engine = isc_commandline_argument;
461 ns_g_foreground = ISC_TRUE;
464 ns_g_foreground = ISC_TRUE;
465 ns_g_logstderr = ISC_TRUE;
467 /* XXXBEW -i should be removed */
469 lwresd_g_defaultpidfile = isc_commandline_argument;
472 ns_g_lwresdonly = ISC_TRUE;
475 set_flags(isc_commandline_argument, mem_debug_flags,
478 case 'N': /* Deprecated. */
480 ns_g_cpus = parse_int(isc_commandline_argument,
486 port = parse_int(isc_commandline_argument, "port");
487 if (port < 1 || port > 65535)
488 ns_main_earlyfatal("port '%s' out of range",
489 isc_commandline_argument);
492 /* XXXBEW Should -P be removed? */
494 port = parse_int(isc_commandline_argument, "port");
495 if (port < 1 || port > 65535)
496 ns_main_earlyfatal("port '%s' out of range",
497 isc_commandline_argument);
498 lwresd_g_listenport = port;
501 /* XXXRTH temporary syntax */
502 want_stats = ISC_TRUE;
505 maxsocks = parse_int(isc_commandline_argument,
506 "max number of sockets");
509 /* XXXJAB should we make a copy? */
510 ns_g_chrootdir = isc_commandline_argument;
512 case 'T': /* NOT DOCUMENTED */
514 * clienttest: make clients single shot with their
515 * own memory context.
517 if (!strcmp(isc_commandline_argument, "clienttest"))
518 ns_g_clienttest = ISC_TRUE;
519 else if (!strcmp(isc_commandline_argument, "nosoa"))
520 ns_g_nosoa = ISC_TRUE;
521 else if (!strcmp(isc_commandline_argument, "noaa"))
522 ns_g_noaa = ISC_TRUE;
523 else if (!strcmp(isc_commandline_argument, "maxudp512"))
525 else if (!strcmp(isc_commandline_argument, "maxudp1460"))
527 else if (!strcmp(isc_commandline_argument, "nosyslog"))
528 ns_g_nosyslog = ISC_TRUE;
529 else if (!strcmp(isc_commandline_argument, "nonearest"))
530 ns_g_nonearest = ISC_TRUE;
532 fprintf(stderr, "unknown -T flag '%s\n",
533 isc_commandline_argument);
536 ns_g_udpdisp = parse_int(isc_commandline_argument,
537 "number of UDP listeners "
541 ns_g_username = isc_commandline_argument;
544 printf("%s %s", ns_g_product, ns_g_version);
545 if (*ns_g_description != 0)
546 printf(" %s", ns_g_description);
550 printf("%s %s", ns_g_product, ns_g_version);
551 if (*ns_g_description != 0)
552 printf(" %s", ns_g_description);
553 printf(" <id:%s> built by %s with %s\n", ns_g_srcid,
554 ns_g_builder, ns_g_configargs);
556 printf("compiled by CLANG %s\n", __VERSION__);
558 #if defined(__ICC) || defined(__INTEL_COMPILER)
559 printf("compiled by ICC %s\n", __VERSION__);
562 printf("compiled by GCC %s\n", __VERSION__);
567 printf("compiled by MSVC %d\n", _MSC_VER);
570 printf("compiled by Solaris Studio %x\n", __SUNPRO_C);
573 printf("using OpenSSL version: %s\n",
574 OPENSSL_VERSION_TEXT);
577 printf("using libxml2 version: %s\n",
578 LIBXML_DOTTED_VERSION);
582 /* Reserved for FIPS mode */
586 if (isc_commandline_option == '?')
588 ns_main_earlyfatal("unknown option '-%c'",
589 isc_commandline_option);
592 ns_main_earlyfatal("parsing options returned %d", ch);
596 argc -= isc_commandline_index;
597 argv += isc_commandline_index;
602 ns_main_earlyfatal("extra command line arguments");
607 create_managers(void) {
611 #ifdef ISC_PLATFORM_USETHREADS
613 ns_g_cpus = ns_g_cpus_detected;
614 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
615 ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
616 ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
617 ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
624 if (ns_g_udpdisp == 0)
625 ns_g_udpdisp = ns_g_cpus_detected;
626 if (ns_g_udpdisp > ns_g_cpus)
627 ns_g_udpdisp = ns_g_cpus;
629 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
630 ISC_LOG_INFO, "using %u UDP listener%s per interface",
631 ns_g_udpdisp, ns_g_udpdisp == 1 ? "" : "s");
633 result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
634 if (result != ISC_R_SUCCESS) {
635 UNEXPECTED_ERROR(__FILE__, __LINE__,
636 "isc_taskmgr_create() failed: %s",
637 isc_result_totext(result));
638 return (ISC_R_UNEXPECTED);
641 result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
642 if (result != ISC_R_SUCCESS) {
643 UNEXPECTED_ERROR(__FILE__, __LINE__,
644 "isc_timermgr_create() failed: %s",
645 isc_result_totext(result));
646 return (ISC_R_UNEXPECTED);
649 result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
650 if (result != ISC_R_SUCCESS) {
651 UNEXPECTED_ERROR(__FILE__, __LINE__,
652 "isc_socketmgr_create() failed: %s",
653 isc_result_totext(result));
654 return (ISC_R_UNEXPECTED);
656 isc__socketmgr_maxudp(ns_g_socketmgr, maxudp);
657 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
658 if (result == ISC_R_SUCCESS) {
659 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
661 ISC_LOG_INFO, "using up to %u sockets", socks);
664 result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
665 if (result != ISC_R_SUCCESS) {
666 UNEXPECTED_ERROR(__FILE__, __LINE__,
667 "isc_entropy_create() failed: %s",
668 isc_result_totext(result));
669 return (ISC_R_UNEXPECTED);
672 result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
673 if (result != ISC_R_SUCCESS) {
674 UNEXPECTED_ERROR(__FILE__, __LINE__,
675 "isc_hash_create() failed: %s",
676 isc_result_totext(result));
677 return (ISC_R_UNEXPECTED);
680 return (ISC_R_SUCCESS);
684 destroy_managers(void) {
685 ns_lwresd_shutdown();
687 isc_entropy_detach(&ns_g_entropy);
688 if (ns_g_fallbackentropy != NULL)
689 isc_entropy_detach(&ns_g_fallbackentropy);
692 * isc_taskmgr_destroy() will block until all tasks have exited,
694 isc_taskmgr_destroy(&ns_g_taskmgr);
695 isc_timermgr_destroy(&ns_g_timermgr);
696 isc_socketmgr_destroy(&ns_g_socketmgr);
699 * isc_hash_destroy() cannot be called as long as a resolver may be
700 * running. Calling this after isc_taskmgr_destroy() ensures the
713 if (isc__backtrace_nsymbols == 0)
716 if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99)))
719 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
720 ISC_LOG_DEBUG(99), "Symbol table:");
722 for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) {
725 result = isc_backtrace_getsymbolfromindex(i, &addr, &fname);
726 if (result == ISC_R_SUCCESS) {
727 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
728 NS_LOGMODULE_MAIN, ISC_LOG_DEBUG(99),
729 "[%d] %p %s", i, addr, fname);
737 isc_resourcevalue_t old_openfiles;
739 char *instance = NULL;
743 * Get the user and group information before changing the root
744 * directory, so the administrator does not need to keep a copy
745 * of the user and group databases in the chroot'ed environment.
747 ns_os_inituserinfo(ns_g_username);
750 * Initialize time conversion information
757 /* Check if named is under smf control, before chroot. */
758 result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
759 /* We don't care about instance, just check if we got one. */
760 if (result == ISC_R_SUCCESS)
761 ns_smf_got_instance = 1;
763 ns_smf_got_instance = 0;
764 if (instance != NULL)
765 isc_mem_free(ns_g_mctx, instance);
766 #endif /* HAVE_LIBSCF */
768 #ifdef PATH_RANDOMDEV
770 * Initialize system's random device as fallback entropy source
771 * if running chroot'ed.
773 if (ns_g_chrootdir != NULL) {
774 result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
775 if (result != ISC_R_SUCCESS)
776 ns_main_earlyfatal("isc_entropy_create() failed: %s",
777 isc_result_totext(result));
779 result = isc_entropy_createfilesource(ns_g_fallbackentropy,
781 if (result != ISC_R_SUCCESS) {
782 ns_main_earlywarning("could not open pre-chroot "
783 "entropy source %s: %s",
785 isc_result_totext(result));
786 isc_entropy_detach(&ns_g_fallbackentropy);
791 #ifdef ISC_PLATFORM_USETHREADS
793 * Check for the number of cpu's before ns_os_chroot().
795 ns_g_cpus_detected = isc_os_ncpus();
798 ns_os_chroot(ns_g_chrootdir);
801 * For operating systems which have a capability mechanism, now
802 * is the time to switch to minimal privs and change our user id.
803 * On traditional UNIX systems, this call will be a no-op, and we
804 * will change the user ID after reading the config file the first
805 * time. (We need to read the config file to know which possibly
806 * privileged ports to bind() to.)
810 result = ns_log_init(ISC_TF(ns_g_username != NULL));
811 if (result != ISC_R_SUCCESS)
812 ns_main_earlyfatal("ns_log_init() failed: %s",
813 isc_result_totext(result));
816 * Now is the time to daemonize (if we're not running in the
817 * foreground). We waited until now because we wanted to get
818 * a valid logging context setup. We cannot daemonize any later,
819 * because calling create_managers() will create threads, which
820 * would be lost after fork().
822 if (!ns_g_foreground)
826 * We call isc_app_start() here as some versions of FreeBSD's fork()
827 * destroys all the signal handling it sets up.
829 result = isc_app_start();
830 if (result != ISC_R_SUCCESS)
831 ns_main_earlyfatal("isc_app_start() failed: %s",
832 isc_result_totext(result));
834 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
835 ISC_LOG_NOTICE, "starting %s %s%s", ns_g_product,
836 ns_g_version, saved_command_line);
838 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
839 ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
841 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
843 "----------------------------------------------------");
844 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
846 "BIND 9 is maintained by Internet Systems Consortium,");
847 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
849 "Inc. (ISC), a non-profit 501(c)(3) public-benefit ");
850 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
852 "corporation. Support and training for BIND 9 are ");
853 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
855 "available at https://www.isc.org/support");
856 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
858 "----------------------------------------------------");
863 * Get the initial resource limits.
865 (void)isc_resource_getlimit(isc_resource_stacksize,
866 &ns_g_initstacksize);
867 (void)isc_resource_getlimit(isc_resource_datasize,
869 (void)isc_resource_getlimit(isc_resource_coresize,
871 (void)isc_resource_getlimit(isc_resource_openfiles,
872 &ns_g_initopenfiles);
875 * System resources cannot effectively be tuned on some systems.
876 * Raise the limit in such cases for safety.
878 old_openfiles = ns_g_initopenfiles;
879 ns_os_adjustnofile();
880 (void)isc_resource_getlimit(isc_resource_openfiles,
881 &ns_g_initopenfiles);
882 if (old_openfiles != ns_g_initopenfiles) {
883 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
884 NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
885 "adjusted limit on open files from "
886 "%" ISC_PRINT_QUADFORMAT "u to "
887 "%" ISC_PRINT_QUADFORMAT "u",
888 old_openfiles, ns_g_initopenfiles);
892 * If the named configuration filename is relative, prepend the current
893 * directory's name before possibly changing to another directory.
895 if (! isc_file_isabsolute(ns_g_conffile)) {
896 result = isc_file_absolutepath(ns_g_conffile,
898 sizeof(absolute_conffile));
899 if (result != ISC_R_SUCCESS)
900 ns_main_earlyfatal("could not construct absolute path "
901 "of configuration file: %s",
902 isc_result_totext(result));
903 ns_g_conffile = absolute_conffile;
907 * Record the server's startup time.
909 result = isc_time_now(&ns_g_boottime);
910 if (result != ISC_R_SUCCESS)
911 ns_main_earlyfatal("isc_time_now() failed: %s",
912 isc_result_totext(result));
914 result = create_managers();
915 if (result != ISC_R_SUCCESS)
916 ns_main_earlyfatal("create_managers() failed: %s",
917 isc_result_totext(result));
922 * Add calls to register sdb drivers here.
926 #ifdef ISC_DLZ_DLOPEN
928 * Register the DLZ "dlopen" driver.
930 result = dlz_dlopen_init(ns_g_mctx);
931 if (result != ISC_R_SUCCESS)
932 ns_main_earlyfatal("dlz_dlopen_init() failed: %s",
933 isc_result_totext(result));
938 * Register any other contributed DLZ drivers.
940 result = dlz_drivers_init();
941 if (result != ISC_R_SUCCESS)
942 ns_main_earlyfatal("dlz_drivers_init() failed: %s",
943 isc_result_totext(result));
946 ns_server_create(ns_g_mctx, &ns_g_server);
953 ns_server_destroy(&ns_g_server);
958 * Add calls to unregister sdb drivers here.
964 * Unregister contributed DLZ drivers.
968 #ifdef ISC_DLZ_DLOPEN
970 * Unregister "dlopen" DLZ driver.
977 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
978 ISC_LOG_NOTICE, "exiting");
982 static char *memstats = NULL;
985 ns_main_setmemstats(const char *filename) {
987 * Caller has to ensure locking.
990 if (memstats != NULL) {
994 if (filename == NULL)
996 memstats = malloc(strlen(filename) + 1);
998 strcpy(memstats, filename);
1003 * Get FMRI for the named process.
1006 ns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
1007 scf_handle_t *h = NULL;
1011 REQUIRE(ins_name != NULL && *ins_name == NULL);
1013 if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
1015 UNEXPECTED_ERROR(__FILE__, __LINE__,
1016 "scf_handle_create() failed: %s",
1017 scf_strerror(scf_error()));
1018 return (ISC_R_FAILURE);
1021 if (scf_handle_bind(h) == -1) {
1023 UNEXPECTED_ERROR(__FILE__, __LINE__,
1024 "scf_handle_bind() failed: %s",
1025 scf_strerror(scf_error()));
1026 scf_handle_destroy(h);
1027 return (ISC_R_FAILURE);
1030 if ((namelen = scf_myname(h, NULL, 0)) == -1) {
1032 UNEXPECTED_ERROR(__FILE__, __LINE__,
1033 "scf_myname() failed: %s",
1034 scf_strerror(scf_error()));
1035 scf_handle_destroy(h);
1036 return (ISC_R_FAILURE);
1039 if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
1040 UNEXPECTED_ERROR(__FILE__, __LINE__,
1041 "ns_smf_get_instance memory "
1042 "allocation failed: %s",
1043 isc_result_totext(ISC_R_NOMEMORY));
1044 scf_handle_destroy(h);
1045 return (ISC_R_FAILURE);
1048 if (scf_myname(h, instance, namelen + 1) == -1) {
1050 UNEXPECTED_ERROR(__FILE__, __LINE__,
1051 "scf_myname() failed: %s",
1052 scf_strerror(scf_error()));
1053 scf_handle_destroy(h);
1054 isc_mem_free(mctx, instance);
1055 return (ISC_R_FAILURE);
1058 scf_handle_destroy(h);
1059 *ins_name = instance;
1060 return (ISC_R_SUCCESS);
1062 #endif /* HAVE_LIBSCF */
1064 /* main entry point, possibly hooked */
1067 main(int argc, char *argv[]) {
1068 isc_result_t result;
1070 char *instance = NULL;
1074 * Record version in core image.
1075 * strings named.core | grep "named version:"
1078 #if defined(NO_VERSION_DATE) || !defined(__DATE__)
1079 "named version: BIND " VERSION " <" SRCID ">",
1081 "named version: BIND " VERSION " <" SRCID "> (" __DATE__ ")",
1084 result = isc_file_progname(*argv, program_name, sizeof(program_name));
1085 if (result != ISC_R_SUCCESS)
1086 ns_main_earlyfatal("program name too long");
1088 if (strcmp(program_name, "lwresd") == 0)
1089 ns_g_lwresdonly = ISC_TRUE;
1091 if (result != ISC_R_SUCCESS)
1092 ns_main_earlyfatal("failed to build internal symbol table");
1094 isc_assertion_setcallback(assertion_failed);
1095 isc_error_setfatal(library_fatal_error);
1096 isc_error_setunexpected(library_unexpected_error);
1098 ns_os_init(program_name);
1100 dns_result_register();
1101 dst_result_register();
1102 isccc_result_register();
1104 parse_command_line(argc, argv);
1107 * Warn about common configuration error.
1109 if (ns_g_chrootdir != NULL) {
1110 int len = strlen(ns_g_chrootdir);
1111 if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
1112 (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
1113 ns_main_earlywarning("config filename (-c %s) contains "
1114 "chroot path (-t %s)",
1115 ns_g_conffile, ns_g_chrootdir);
1118 result = isc_mem_create(0, 0, &ns_g_mctx);
1119 if (result != ISC_R_SUCCESS)
1120 ns_main_earlyfatal("isc_mem_create() failed: %s",
1121 isc_result_totext(result));
1122 isc_mem_setname(ns_g_mctx, "main", NULL);
1127 * Start things running and then wait for a shutdown request
1131 result = isc_app_run();
1133 if (result == ISC_R_RELOAD) {
1134 ns_server_reloadwanted(ns_g_server);
1135 } else if (result != ISC_R_SUCCESS) {
1136 UNEXPECTED_ERROR(__FILE__, __LINE__,
1137 "isc_app_run(): %s",
1138 isc_result_totext(result));
1142 result = ISC_R_SUCCESS;
1144 } while (result != ISC_R_SUCCESS);
1147 if (ns_smf_want_disable == 1) {
1148 result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
1149 if (result == ISC_R_SUCCESS && instance != NULL) {
1150 if (smf_disable_instance(instance, 0) != 0)
1151 UNEXPECTED_ERROR(__FILE__, __LINE__,
1152 "smf_disable_instance() "
1153 "failed for %s : %s",
1155 scf_strerror(scf_error()));
1157 if (instance != NULL)
1158 isc_mem_free(ns_g_mctx, instance);
1160 #endif /* HAVE_LIBSCF */
1165 isc_mem_stats(ns_g_mctx, stdout);
1166 isc_mutex_stats(stdout);
1169 if (ns_g_memstatistics && memstats != NULL) {
1171 result = isc_stdio_open(memstats, "w", &fp);
1172 if (result == ISC_R_SUCCESS) {
1173 isc_mem_stats(ns_g_mctx, fp);
1174 isc_mutex_stats(fp);
1175 isc_stdio_close(fp);
1178 isc_mem_destroy(&ns_g_mctx);
1179 isc_mem_checkdestroyed(stderr);
1181 ns_main_setmemstats(NULL);
1185 ns_os_closedevnull();