2 * Copyright (C) 2004-2011 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.
18 /* $Id: main.c,v 1.180.14.4 2011/11/05 00:45:52 each Exp $ */
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);
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 isc_commandline_errprint = ISC_FALSE;
419 while ((ch = isc_commandline_parse(argc, argv,
420 "46c:C:d:E:fFgi:lm:n:N:p:P:"
421 "sS:t:T:u:vVx:")) != -1) {
425 ns_main_earlyfatal("cannot specify -4 and -6");
426 if (isc_net_probeipv4() != ISC_R_SUCCESS)
427 ns_main_earlyfatal("IPv4 not supported by OS");
428 isc_net_disableipv6();
433 ns_main_earlyfatal("cannot specify -4 and -6");
434 if (isc_net_probeipv6() != ISC_R_SUCCESS)
435 ns_main_earlyfatal("IPv6 not supported by OS");
436 isc_net_disableipv4();
440 ns_g_conffile = isc_commandline_argument;
441 lwresd_g_conffile = isc_commandline_argument;
442 if (lwresd_g_useresolvconf)
443 ns_main_earlyfatal("cannot specify -c and -C");
444 ns_g_conffileset = ISC_TRUE;
447 lwresd_g_resolvconffile = isc_commandline_argument;
448 if (ns_g_conffileset)
449 ns_main_earlyfatal("cannot specify -c and -C");
450 lwresd_g_useresolvconf = ISC_TRUE;
453 ns_g_debuglevel = parse_int(isc_commandline_argument,
457 ns_g_engine = isc_commandline_argument;
460 ns_g_foreground = ISC_TRUE;
463 ns_g_foreground = ISC_TRUE;
464 ns_g_logstderr = ISC_TRUE;
466 /* XXXBEW -i should be removed */
468 lwresd_g_defaultpidfile = isc_commandline_argument;
471 ns_g_lwresdonly = ISC_TRUE;
474 set_flags(isc_commandline_argument, mem_debug_flags,
477 case 'N': /* Deprecated. */
479 ns_g_cpus = parse_int(isc_commandline_argument,
485 port = parse_int(isc_commandline_argument, "port");
486 if (port < 1 || port > 65535)
487 ns_main_earlyfatal("port '%s' out of range",
488 isc_commandline_argument);
491 /* XXXBEW Should -P be removed? */
493 port = parse_int(isc_commandline_argument, "port");
494 if (port < 1 || port > 65535)
495 ns_main_earlyfatal("port '%s' out of range",
496 isc_commandline_argument);
497 lwresd_g_listenport = port;
500 /* XXXRTH temporary syntax */
501 want_stats = ISC_TRUE;
504 maxsocks = parse_int(isc_commandline_argument,
505 "max number of sockets");
508 /* XXXJAB should we make a copy? */
509 ns_g_chrootdir = isc_commandline_argument;
511 case 'T': /* NOT DOCUMENTED */
513 * clienttest: make clients single shot with their
514 * own memory context.
516 if (!strcmp(isc_commandline_argument, "clienttest"))
517 ns_g_clienttest = ISC_TRUE;
518 else if (!strcmp(isc_commandline_argument, "nosoa"))
519 ns_g_nosoa = ISC_TRUE;
520 else if (!strcmp(isc_commandline_argument, "noaa"))
521 ns_g_noaa = ISC_TRUE;
522 else if (!strcmp(isc_commandline_argument, "maxudp512"))
524 else if (!strcmp(isc_commandline_argument, "maxudp1460"))
527 fprintf(stderr, "unknown -T flag '%s\n",
528 isc_commandline_argument);
531 ns_g_username = isc_commandline_argument;
534 printf("BIND %s\n", ns_g_version);
537 printf("BIND %s built with %s\n", ns_g_version,
540 printf("using OpenSSL version: %s\n",
541 OPENSSL_VERSION_TEXT);
544 printf("using libxml2 version: %s\n",
545 LIBXML_DOTTED_VERSION);
549 /* Reserved for FIPS mode */
553 if (isc_commandline_option == '?')
555 ns_main_earlyfatal("unknown option '-%c'",
556 isc_commandline_option);
559 ns_main_earlyfatal("parsing options returned %d", ch);
563 argc -= isc_commandline_index;
564 argv += isc_commandline_index;
569 ns_main_earlyfatal("extra command line arguments");
574 create_managers(void) {
578 #ifdef ISC_PLATFORM_USETHREADS
580 ns_g_cpus = ns_g_cpus_detected;
581 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
582 ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
583 ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
584 ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
588 result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
589 if (result != ISC_R_SUCCESS) {
590 UNEXPECTED_ERROR(__FILE__, __LINE__,
591 "isc_taskmgr_create() failed: %s",
592 isc_result_totext(result));
593 return (ISC_R_UNEXPECTED);
596 result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
597 if (result != ISC_R_SUCCESS) {
598 UNEXPECTED_ERROR(__FILE__, __LINE__,
599 "isc_timermgr_create() failed: %s",
600 isc_result_totext(result));
601 return (ISC_R_UNEXPECTED);
604 result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
605 if (result != ISC_R_SUCCESS) {
606 UNEXPECTED_ERROR(__FILE__, __LINE__,
607 "isc_socketmgr_create() failed: %s",
608 isc_result_totext(result));
609 return (ISC_R_UNEXPECTED);
611 isc__socketmgr_maxudp(ns_g_socketmgr, maxudp);
612 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
613 if (result == ISC_R_SUCCESS) {
614 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
616 ISC_LOG_INFO, "using up to %u sockets", socks);
619 result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
620 if (result != ISC_R_SUCCESS) {
621 UNEXPECTED_ERROR(__FILE__, __LINE__,
622 "isc_entropy_create() failed: %s",
623 isc_result_totext(result));
624 return (ISC_R_UNEXPECTED);
627 result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
628 if (result != ISC_R_SUCCESS) {
629 UNEXPECTED_ERROR(__FILE__, __LINE__,
630 "isc_hash_create() failed: %s",
631 isc_result_totext(result));
632 return (ISC_R_UNEXPECTED);
635 return (ISC_R_SUCCESS);
639 destroy_managers(void) {
640 ns_lwresd_shutdown();
642 isc_entropy_detach(&ns_g_entropy);
643 if (ns_g_fallbackentropy != NULL)
644 isc_entropy_detach(&ns_g_fallbackentropy);
647 * isc_taskmgr_destroy() will block until all tasks have exited,
649 isc_taskmgr_destroy(&ns_g_taskmgr);
650 isc_timermgr_destroy(&ns_g_timermgr);
651 isc_socketmgr_destroy(&ns_g_socketmgr);
654 * isc_hash_destroy() cannot be called as long as a resolver may be
655 * running. Calling this after isc_taskmgr_destroy() ensures the
668 if (isc__backtrace_nsymbols == 0)
671 if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99)))
674 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
675 ISC_LOG_DEBUG(99), "Symbol table:");
677 for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) {
680 result = isc_backtrace_getsymbolfromindex(i, &addr, &fname);
681 if (result == ISC_R_SUCCESS) {
682 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
683 NS_LOGMODULE_MAIN, ISC_LOG_DEBUG(99),
684 "[%d] %p %s", i, addr, fname);
692 isc_resourcevalue_t old_openfiles;
694 char *instance = NULL;
698 * Get the user and group information before changing the root
699 * directory, so the administrator does not need to keep a copy
700 * of the user and group databases in the chroot'ed environment.
702 ns_os_inituserinfo(ns_g_username);
705 * Initialize time conversion information
712 /* Check if named is under smf control, before chroot. */
713 result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
714 /* We don't care about instance, just check if we got one. */
715 if (result == ISC_R_SUCCESS)
716 ns_smf_got_instance = 1;
718 ns_smf_got_instance = 0;
719 if (instance != NULL)
720 isc_mem_free(ns_g_mctx, instance);
721 #endif /* HAVE_LIBSCF */
723 #ifdef PATH_RANDOMDEV
725 * Initialize system's random device as fallback entropy source
726 * if running chroot'ed.
728 if (ns_g_chrootdir != NULL) {
729 result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
730 if (result != ISC_R_SUCCESS)
731 ns_main_earlyfatal("isc_entropy_create() failed: %s",
732 isc_result_totext(result));
734 result = isc_entropy_createfilesource(ns_g_fallbackentropy,
736 if (result != ISC_R_SUCCESS) {
737 ns_main_earlywarning("could not open pre-chroot "
738 "entropy source %s: %s",
740 isc_result_totext(result));
741 isc_entropy_detach(&ns_g_fallbackentropy);
746 #ifdef ISC_PLATFORM_USETHREADS
748 * Check for the number of cpu's before ns_os_chroot().
750 ns_g_cpus_detected = isc_os_ncpus();
753 ns_os_chroot(ns_g_chrootdir);
756 * For operating systems which have a capability mechanism, now
757 * is the time to switch to minimal privs and change our user id.
758 * On traditional UNIX systems, this call will be a no-op, and we
759 * will change the user ID after reading the config file the first
760 * time. (We need to read the config file to know which possibly
761 * privileged ports to bind() to.)
765 result = ns_log_init(ISC_TF(ns_g_username != NULL));
766 if (result != ISC_R_SUCCESS)
767 ns_main_earlyfatal("ns_log_init() failed: %s",
768 isc_result_totext(result));
771 * Now is the time to daemonize (if we're not running in the
772 * foreground). We waited until now because we wanted to get
773 * a valid logging context setup. We cannot daemonize any later,
774 * because calling create_managers() will create threads, which
775 * would be lost after fork().
777 if (!ns_g_foreground)
781 * We call isc_app_start() here as some versions of FreeBSD's fork()
782 * destroys all the signal handling it sets up.
784 result = isc_app_start();
785 if (result != ISC_R_SUCCESS)
786 ns_main_earlyfatal("isc_app_start() failed: %s",
787 isc_result_totext(result));
789 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
790 ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
793 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
794 ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
796 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
798 "----------------------------------------------------");
799 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
801 "BIND 9 is maintained by Internet Systems Consortium,");
802 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
804 "Inc. (ISC), a non-profit 501(c)(3) public-benefit ");
805 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
807 "corporation. Support and training for BIND 9 are ");
808 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
810 "available at https://www.isc.org/support");
811 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
813 "----------------------------------------------------");
818 * Get the initial resource limits.
820 (void)isc_resource_getlimit(isc_resource_stacksize,
821 &ns_g_initstacksize);
822 (void)isc_resource_getlimit(isc_resource_datasize,
824 (void)isc_resource_getlimit(isc_resource_coresize,
826 (void)isc_resource_getlimit(isc_resource_openfiles,
827 &ns_g_initopenfiles);
830 * System resources cannot effectively be tuned on some systems.
831 * Raise the limit in such cases for safety.
833 old_openfiles = ns_g_initopenfiles;
834 ns_os_adjustnofile();
835 (void)isc_resource_getlimit(isc_resource_openfiles,
836 &ns_g_initopenfiles);
837 if (old_openfiles != ns_g_initopenfiles) {
838 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
839 NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
840 "adjusted limit on open files from "
841 "%" ISC_PRINT_QUADFORMAT "u to "
842 "%" ISC_PRINT_QUADFORMAT "u",
843 old_openfiles, ns_g_initopenfiles);
847 * If the named configuration filename is relative, prepend the current
848 * directory's name before possibly changing to another directory.
850 if (! isc_file_isabsolute(ns_g_conffile)) {
851 result = isc_file_absolutepath(ns_g_conffile,
853 sizeof(absolute_conffile));
854 if (result != ISC_R_SUCCESS)
855 ns_main_earlyfatal("could not construct absolute path "
856 "of configuration file: %s",
857 isc_result_totext(result));
858 ns_g_conffile = absolute_conffile;
862 * Record the server's startup time.
864 result = isc_time_now(&ns_g_boottime);
865 if (result != ISC_R_SUCCESS)
866 ns_main_earlyfatal("isc_time_now() failed: %s",
867 isc_result_totext(result));
869 result = create_managers();
870 if (result != ISC_R_SUCCESS)
871 ns_main_earlyfatal("create_managers() failed: %s",
872 isc_result_totext(result));
877 * Add calls to register sdb drivers here.
881 #ifdef ISC_DLZ_DLOPEN
883 * Register the DLZ "dlopen" driver.
885 result = dlz_dlopen_init(ns_g_mctx);
886 if (result != ISC_R_SUCCESS)
887 ns_main_earlyfatal("dlz_dlopen_init() failed: %s",
888 isc_result_totext(result));
893 * Register any other contributed DLZ drivers.
895 result = dlz_drivers_init();
896 if (result != ISC_R_SUCCESS)
897 ns_main_earlyfatal("dlz_drivers_init() failed: %s",
898 isc_result_totext(result));
901 ns_server_create(ns_g_mctx, &ns_g_server);
908 ns_server_destroy(&ns_g_server);
913 * Add calls to unregister sdb drivers here.
919 * Unregister contributed DLZ drivers.
923 #ifdef ISC_DLZ_DLOPEN
925 * Unregister "dlopen" DLZ driver.
932 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
933 ISC_LOG_NOTICE, "exiting");
937 static char *memstats = NULL;
940 ns_main_setmemstats(const char *filename) {
942 * Caller has to ensure locking.
945 if (memstats != NULL) {
949 if (filename == NULL)
951 memstats = malloc(strlen(filename) + 1);
953 strcpy(memstats, filename);
958 * Get FMRI for the named process.
961 ns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
962 scf_handle_t *h = NULL;
966 REQUIRE(ins_name != NULL && *ins_name == NULL);
968 if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
970 UNEXPECTED_ERROR(__FILE__, __LINE__,
971 "scf_handle_create() failed: %s",
972 scf_strerror(scf_error()));
973 return (ISC_R_FAILURE);
976 if (scf_handle_bind(h) == -1) {
978 UNEXPECTED_ERROR(__FILE__, __LINE__,
979 "scf_handle_bind() failed: %s",
980 scf_strerror(scf_error()));
981 scf_handle_destroy(h);
982 return (ISC_R_FAILURE);
985 if ((namelen = scf_myname(h, NULL, 0)) == -1) {
987 UNEXPECTED_ERROR(__FILE__, __LINE__,
988 "scf_myname() failed: %s",
989 scf_strerror(scf_error()));
990 scf_handle_destroy(h);
991 return (ISC_R_FAILURE);
994 if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
995 UNEXPECTED_ERROR(__FILE__, __LINE__,
996 "ns_smf_get_instance memory "
997 "allocation failed: %s",
998 isc_result_totext(ISC_R_NOMEMORY));
999 scf_handle_destroy(h);
1000 return (ISC_R_FAILURE);
1003 if (scf_myname(h, instance, namelen + 1) == -1) {
1005 UNEXPECTED_ERROR(__FILE__, __LINE__,
1006 "scf_myname() failed: %s",
1007 scf_strerror(scf_error()));
1008 scf_handle_destroy(h);
1009 isc_mem_free(mctx, instance);
1010 return (ISC_R_FAILURE);
1013 scf_handle_destroy(h);
1014 *ins_name = instance;
1015 return (ISC_R_SUCCESS);
1017 #endif /* HAVE_LIBSCF */
1020 main(int argc, char *argv[]) {
1021 isc_result_t result;
1023 char *instance = NULL;
1027 * Record version in core image.
1028 * strings named.core | grep "named version:"
1031 #if defined(NO_VERSION_DATE) || !defined(__DATE__)
1032 "named version: BIND " VERSION,
1034 "named version: BIND " VERSION " (" __DATE__ ")",
1037 result = isc_file_progname(*argv, program_name, sizeof(program_name));
1038 if (result != ISC_R_SUCCESS)
1039 ns_main_earlyfatal("program name too long");
1041 if (strcmp(program_name, "lwresd") == 0)
1042 ns_g_lwresdonly = ISC_TRUE;
1044 if (result != ISC_R_SUCCESS)
1045 ns_main_earlyfatal("failed to build internal symbol table");
1047 isc_assertion_setcallback(assertion_failed);
1048 isc_error_setfatal(library_fatal_error);
1049 isc_error_setunexpected(library_unexpected_error);
1051 ns_os_init(program_name);
1053 dns_result_register();
1054 dst_result_register();
1055 isccc_result_register();
1057 parse_command_line(argc, argv);
1060 * Warn about common configuration error.
1062 if (ns_g_chrootdir != NULL) {
1063 int len = strlen(ns_g_chrootdir);
1064 if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
1065 (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
1066 ns_main_earlywarning("config filename (-c %s) contains "
1067 "chroot path (-t %s)",
1068 ns_g_conffile, ns_g_chrootdir);
1071 result = isc_mem_create(0, 0, &ns_g_mctx);
1072 if (result != ISC_R_SUCCESS)
1073 ns_main_earlyfatal("isc_mem_create() failed: %s",
1074 isc_result_totext(result));
1075 isc_mem_setname(ns_g_mctx, "main", NULL);
1080 * Start things running and then wait for a shutdown request
1084 result = isc_app_run();
1086 if (result == ISC_R_RELOAD) {
1087 ns_server_reloadwanted(ns_g_server);
1088 } else if (result != ISC_R_SUCCESS) {
1089 UNEXPECTED_ERROR(__FILE__, __LINE__,
1090 "isc_app_run(): %s",
1091 isc_result_totext(result));
1095 result = ISC_R_SUCCESS;
1097 } while (result != ISC_R_SUCCESS);
1100 if (ns_smf_want_disable == 1) {
1101 result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
1102 if (result == ISC_R_SUCCESS && instance != NULL) {
1103 if (smf_disable_instance(instance, 0) != 0)
1104 UNEXPECTED_ERROR(__FILE__, __LINE__,
1105 "smf_disable_instance() "
1106 "failed for %s : %s",
1108 scf_strerror(scf_error()));
1110 if (instance != NULL)
1111 isc_mem_free(ns_g_mctx, instance);
1113 #endif /* HAVE_LIBSCF */
1118 isc_mem_stats(ns_g_mctx, stdout);
1119 isc_mutex_stats(stdout);
1122 if (ns_g_memstatistics && memstats != NULL) {
1124 result = isc_stdio_open(memstats, "w", &fp);
1125 if (result == ISC_R_SUCCESS) {
1126 isc_mem_stats(ns_g_mctx, fp);
1127 isc_mutex_stats(fp);
1128 isc_stdio_close(fp);
1131 isc_mem_destroy(&ns_g_mctx);
1132 isc_mem_checkdestroyed(stderr);
1134 ns_main_setmemstats(NULL);
1138 ns_os_closedevnull();