2 * Copyright (C) 2004-2010 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.166.34.7 2010-09-15 12:16:49 marka Exp $ */
29 #include <isc/commandline.h>
31 #include <isc/entropy.h>
35 #include <isc/platform.h>
36 #include <isc/print.h>
37 #include <isc/resource.h>
38 #include <isc/stdio.h>
39 #include <isc/string.h>
41 #include <isc/timer.h>
44 #include <isccc/result.h>
46 #include <dns/dispatch.h>
48 #include <dns/result.h>
51 #include <dst/result.h>
54 * Defining NS_MAIN provides storage declarations (rather than extern)
55 * for variables in named/globals.h.
59 #include <named/builtin.h>
60 #include <named/control.h>
61 #include <named/globals.h> /* Explicit, though named/log.h includes it. */
62 #include <named/interfacemgr.h>
63 #include <named/log.h>
65 #include <named/server.h>
66 #include <named/lwresd.h>
67 #include <named/main.h>
69 #include <named/ns_smf_globals.h>
73 * Include header files for database drivers here.
75 /* #include "xxdb.h" */
78 * Include DLZ drivers if appropriate.
81 #include <dlz/dlz_drivers.h>
84 static isc_boolean_t want_stats = ISC_FALSE;
85 static char program_name[ISC_DIR_NAMEMAX] = "named";
86 static char absolute_conffile[ISC_DIR_PATHMAX];
87 static char saved_command_line[512];
88 static char version[512];
89 static unsigned int maxsocks = 0;
92 ns_main_earlywarning(const char *format, ...) {
95 va_start(args, format);
96 if (ns_g_lctx != NULL) {
97 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
98 NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
101 fprintf(stderr, "%s: ", program_name);
102 vfprintf(stderr, format, args);
103 fprintf(stderr, "\n");
110 ns_main_earlyfatal(const char *format, ...) {
113 va_start(args, format);
114 if (ns_g_lctx != NULL) {
115 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
116 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
118 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
119 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
120 "exiting (due to early fatal error)");
122 fprintf(stderr, "%s: ", program_name);
123 vfprintf(stderr, format, args);
124 fprintf(stderr, "\n");
133 assertion_failed(const char *file, int line, isc_assertiontype_t type,
137 * Handle assertion failures.
140 if (ns_g_lctx != NULL) {
142 * Reset the assertion callback in case it is the log
143 * routines causing the assertion.
145 isc_assertion_setcallback(NULL);
147 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
148 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
149 "%s:%d: %s(%s) failed", file, line,
150 isc_assertion_typetotext(type), cond);
151 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
152 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
153 "exiting (due to assertion failure)");
155 fprintf(stderr, "%s:%d: %s(%s) failed\n",
156 file, line, isc_assertion_typetotext(type), cond);
166 library_fatal_error(const char *file, int line, const char *format,
167 va_list args) ISC_FORMAT_PRINTF(3, 0);
170 library_fatal_error(const char *file, int line, const char *format,
174 * Handle isc_error_fatal() calls from our libraries.
177 if (ns_g_lctx != NULL) {
179 * Reset the error callback in case it is the log
180 * routines causing the assertion.
182 isc_error_setfatal(NULL);
184 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
185 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
186 "%s:%d: fatal error:", file, line);
187 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
188 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
190 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
191 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
192 "exiting (due to fatal error in library)");
194 fprintf(stderr, "%s:%d: fatal error: ", file, line);
195 vfprintf(stderr, format, args);
196 fprintf(stderr, "\n");
206 library_unexpected_error(const char *file, int line, const char *format,
207 va_list args) ISC_FORMAT_PRINTF(3, 0);
210 library_unexpected_error(const char *file, int line, const char *format,
214 * Handle isc_error_unexpected() calls from our libraries.
217 if (ns_g_lctx != NULL) {
218 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
219 NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
220 "%s:%d: unexpected error:", file, line);
221 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
222 NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
225 fprintf(stderr, "%s:%d: fatal error: ", file, line);
226 vfprintf(stderr, format, args);
227 fprintf(stderr, "\n");
235 "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
237 " [-f|-g] [-n number_of_cpus] [-p port] "
238 "[-P listen-port] [-s]\n"
239 " [-t chrootdir] [-u username] [-i pidfile]\n"
240 " [-m {usage|trace|record|size|mctx}]\n");
245 if (ns_g_lwresdonly) {
250 "usage: named [-4|-6] [-c conffile] [-d debuglevel] "
251 "[-f|-g] [-n number_of_cpus]\n"
252 " [-p port] [-s] [-t chrootdir] [-u username]\n"
253 " [-m {usage|trace|record|size|mctx}]\n");
257 save_command_line(int argc, char *argv[]) {
262 const char truncated[] = "...";
263 isc_boolean_t quoted = ISC_FALSE;
265 dst = saved_command_line;
266 eob = saved_command_line + sizeof(saved_command_line);
268 for (i = 1; i < argc && dst < eob; i++) {
272 while (*src != '\0' && dst < eob) {
274 * This won't perfectly produce a shell-independent
275 * pastable command line in all circumstances, but
276 * comes close, and for practical purposes will
277 * nearly always be fine.
279 if (quoted || isalnum(*src & 0xff) ||
280 *src == '-' || *src == '_' ||
281 *src == '.' || *src == '/') {
291 INSIST(sizeof(saved_command_line) >= sizeof(truncated));
294 strcpy(eob - sizeof(truncated), truncated);
300 parse_int(char *arg, const char *desc) {
305 ltmp = strtol(arg, &endp, 10);
308 ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
309 if (tmp < 0 || tmp != ltmp)
310 ns_main_earlyfatal("%s '%s' out of range", desc, arg);
314 static struct flag_def {
317 } mem_debug_flags[] = {
318 { "trace", ISC_MEM_DEBUGTRACE },
319 { "record", ISC_MEM_DEBUGRECORD },
320 { "usage", ISC_MEM_DEBUGUSAGE },
321 { "size", ISC_MEM_DEBUGSIZE },
322 { "mctx", ISC_MEM_DEBUGCTX },
327 set_flags(const char *arg, struct flag_def *defs, unsigned int *ret) {
329 const struct flag_def *def;
330 const char *end = strchr(arg, ',');
333 end = arg + strlen(arg);
335 for (def = defs; def->name != NULL; def++) {
336 if (arglen == (int)strlen(def->name) &&
337 memcmp(arg, def->name, arglen) == 0) {
342 ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg);
351 parse_command_line(int argc, char *argv[]) {
354 isc_boolean_t disable6 = ISC_FALSE;
355 isc_boolean_t disable4 = ISC_FALSE;
357 save_command_line(argc, argv);
359 isc_commandline_errprint = ISC_FALSE;
360 while ((ch = isc_commandline_parse(argc, argv,
361 "46c:C:d:fgi:lm:n:N:p:P:"
362 "sS:t:T:u:vVx:")) != -1) {
366 ns_main_earlyfatal("cannot specify -4 and -6");
367 if (isc_net_probeipv4() != ISC_R_SUCCESS)
368 ns_main_earlyfatal("IPv4 not supported by OS");
369 isc_net_disableipv6();
374 ns_main_earlyfatal("cannot specify -4 and -6");
375 if (isc_net_probeipv6() != ISC_R_SUCCESS)
376 ns_main_earlyfatal("IPv6 not supported by OS");
377 isc_net_disableipv4();
381 ns_g_conffile = isc_commandline_argument;
382 lwresd_g_conffile = isc_commandline_argument;
383 if (lwresd_g_useresolvconf)
384 ns_main_earlyfatal("cannot specify -c and -C");
385 ns_g_conffileset = ISC_TRUE;
388 lwresd_g_resolvconffile = isc_commandline_argument;
389 if (ns_g_conffileset)
390 ns_main_earlyfatal("cannot specify -c and -C");
391 lwresd_g_useresolvconf = ISC_TRUE;
394 ns_g_debuglevel = parse_int(isc_commandline_argument,
398 ns_g_foreground = ISC_TRUE;
401 ns_g_foreground = ISC_TRUE;
402 ns_g_logstderr = ISC_TRUE;
404 /* XXXBEW -i should be removed */
406 lwresd_g_defaultpidfile = isc_commandline_argument;
409 ns_g_lwresdonly = ISC_TRUE;
412 set_flags(isc_commandline_argument, mem_debug_flags,
415 case 'N': /* Deprecated. */
417 ns_g_cpus = parse_int(isc_commandline_argument,
423 port = parse_int(isc_commandline_argument, "port");
424 if (port < 1 || port > 65535)
425 ns_main_earlyfatal("port '%s' out of range",
426 isc_commandline_argument);
429 /* XXXBEW Should -P be removed? */
431 port = parse_int(isc_commandline_argument, "port");
432 if (port < 1 || port > 65535)
433 ns_main_earlyfatal("port '%s' out of range",
434 isc_commandline_argument);
435 lwresd_g_listenport = port;
438 /* XXXRTH temporary syntax */
439 want_stats = ISC_TRUE;
442 maxsocks = parse_int(isc_commandline_argument,
443 "max number of sockets");
446 /* XXXJAB should we make a copy? */
447 ns_g_chrootdir = isc_commandline_argument;
449 case 'T': /* NOT DOCUMENTED */
451 * clienttest: make clients single shot with their
452 * own memory context.
454 if (strcmp(isc_commandline_argument, "clienttest") == 0)
455 ns_g_clienttest = ISC_TRUE;
456 else if (!strcmp(isc_commandline_argument, "nosoa"))
457 ns_g_nosoa = ISC_TRUE;
458 else if (!strcmp(isc_commandline_argument, "noaa"))
459 ns_g_noaa = ISC_TRUE;
461 fprintf(stderr, "unknown -T flag '%s\n",
462 isc_commandline_argument);
465 ns_g_username = isc_commandline_argument;
468 printf("BIND %s\n", ns_g_version);
471 printf("BIND %s built with %s\n", ns_g_version,
476 if (isc_commandline_option == '?')
478 ns_main_earlyfatal("unknown option '-%c'",
479 isc_commandline_option);
481 ns_main_earlyfatal("parsing options returned %d", ch);
485 argc -= isc_commandline_index;
486 argv += isc_commandline_index;
490 ns_main_earlyfatal("extra command line arguments");
495 create_managers(void) {
499 #ifdef ISC_PLATFORM_USETHREADS
501 ns_g_cpus = ns_g_cpus_detected;
502 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
503 ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
504 ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
505 ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
509 result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
510 if (result != ISC_R_SUCCESS) {
511 UNEXPECTED_ERROR(__FILE__, __LINE__,
512 "isc_taskmgr_create() failed: %s",
513 isc_result_totext(result));
514 return (ISC_R_UNEXPECTED);
517 result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
518 if (result != ISC_R_SUCCESS) {
519 UNEXPECTED_ERROR(__FILE__, __LINE__,
520 "isc_timermgr_create() failed: %s",
521 isc_result_totext(result));
522 return (ISC_R_UNEXPECTED);
525 result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
526 if (result != ISC_R_SUCCESS) {
527 UNEXPECTED_ERROR(__FILE__, __LINE__,
528 "isc_socketmgr_create() failed: %s",
529 isc_result_totext(result));
530 return (ISC_R_UNEXPECTED);
532 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
533 if (result == ISC_R_SUCCESS) {
534 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
536 ISC_LOG_INFO, "using up to %u sockets", socks);
539 result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
540 if (result != ISC_R_SUCCESS) {
541 UNEXPECTED_ERROR(__FILE__, __LINE__,
542 "isc_entropy_create() failed: %s",
543 isc_result_totext(result));
544 return (ISC_R_UNEXPECTED);
547 result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
548 if (result != ISC_R_SUCCESS) {
549 UNEXPECTED_ERROR(__FILE__, __LINE__,
550 "isc_hash_create() failed: %s",
551 isc_result_totext(result));
552 return (ISC_R_UNEXPECTED);
555 return (ISC_R_SUCCESS);
559 destroy_managers(void) {
560 ns_lwresd_shutdown();
562 isc_entropy_detach(&ns_g_entropy);
563 if (ns_g_fallbackentropy != NULL)
564 isc_entropy_detach(&ns_g_fallbackentropy);
567 * isc_taskmgr_destroy() will block until all tasks have exited,
569 isc_taskmgr_destroy(&ns_g_taskmgr);
570 isc_timermgr_destroy(&ns_g_timermgr);
571 isc_socketmgr_destroy(&ns_g_socketmgr);
574 * isc_hash_destroy() cannot be called as long as a resolver may be
575 * running. Calling this after isc_taskmgr_destroy() ensures the
584 isc_resourcevalue_t old_openfiles;
586 char *instance = NULL;
590 * Get the user and group information before changing the root
591 * directory, so the administrator does not need to keep a copy
592 * of the user and group databases in the chroot'ed environment.
594 ns_os_inituserinfo(ns_g_username);
597 * Initialize time conversion information
604 /* Check if named is under smf control, before chroot. */
605 result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
606 /* We don't care about instance, just check if we got one. */
607 if (result == ISC_R_SUCCESS)
608 ns_smf_got_instance = 1;
610 ns_smf_got_instance = 0;
611 if (instance != NULL)
612 isc_mem_free(ns_g_mctx, instance);
613 #endif /* HAVE_LIBSCF */
615 #ifdef PATH_RANDOMDEV
617 * Initialize system's random device as fallback entropy source
618 * if running chroot'ed.
620 if (ns_g_chrootdir != NULL) {
621 result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
622 if (result != ISC_R_SUCCESS)
623 ns_main_earlyfatal("isc_entropy_create() failed: %s",
624 isc_result_totext(result));
626 result = isc_entropy_createfilesource(ns_g_fallbackentropy,
628 if (result != ISC_R_SUCCESS) {
629 ns_main_earlywarning("could not open pre-chroot "
630 "entropy source %s: %s",
632 isc_result_totext(result));
633 isc_entropy_detach(&ns_g_fallbackentropy);
638 #ifdef ISC_PLATFORM_USETHREADS
640 * Check for the number of cpu's before ns_os_chroot().
642 ns_g_cpus_detected = isc_os_ncpus();
645 ns_os_chroot(ns_g_chrootdir);
648 * For operating systems which have a capability mechanism, now
649 * is the time to switch to minimal privs and change our user id.
650 * On traditional UNIX systems, this call will be a no-op, and we
651 * will change the user ID after reading the config file the first
652 * time. (We need to read the config file to know which possibly
653 * privileged ports to bind() to.)
657 result = ns_log_init(ISC_TF(ns_g_username != NULL));
658 if (result != ISC_R_SUCCESS)
659 ns_main_earlyfatal("ns_log_init() failed: %s",
660 isc_result_totext(result));
663 * Now is the time to daemonize (if we're not running in the
664 * foreground). We waited until now because we wanted to get
665 * a valid logging context setup. We cannot daemonize any later,
666 * because calling create_managers() will create threads, which
667 * would be lost after fork().
669 if (!ns_g_foreground)
673 * We call isc_app_start() here as some versions of FreeBSD's fork()
674 * destroys all the signal handling it sets up.
676 result = isc_app_start();
677 if (result != ISC_R_SUCCESS)
678 ns_main_earlyfatal("isc_app_start() failed: %s",
679 isc_result_totext(result));
681 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
682 ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
685 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
686 ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
689 * Get the initial resource limits.
691 (void)isc_resource_getlimit(isc_resource_stacksize,
692 &ns_g_initstacksize);
693 (void)isc_resource_getlimit(isc_resource_datasize,
695 (void)isc_resource_getlimit(isc_resource_coresize,
697 (void)isc_resource_getlimit(isc_resource_openfiles,
698 &ns_g_initopenfiles);
701 * System resources cannot effectively be tuned on some systems.
702 * Raise the limit in such cases for safety.
704 old_openfiles = ns_g_initopenfiles;
705 ns_os_adjustnofile();
706 (void)isc_resource_getlimit(isc_resource_openfiles,
707 &ns_g_initopenfiles);
708 if (old_openfiles != ns_g_initopenfiles) {
709 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
710 NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
711 "adjusted limit on open files from "
712 "%" ISC_PRINT_QUADFORMAT "u to "
713 "%" ISC_PRINT_QUADFORMAT "u",
714 old_openfiles, ns_g_initopenfiles);
718 * If the named configuration filename is relative, prepend the current
719 * directory's name before possibly changing to another directory.
721 if (! isc_file_isabsolute(ns_g_conffile)) {
722 result = isc_file_absolutepath(ns_g_conffile,
724 sizeof(absolute_conffile));
725 if (result != ISC_R_SUCCESS)
726 ns_main_earlyfatal("could not construct absolute path of "
727 "configuration file: %s",
728 isc_result_totext(result));
729 ns_g_conffile = absolute_conffile;
733 * Record the server's startup time.
735 result = isc_time_now(&ns_g_boottime);
736 if (result != ISC_R_SUCCESS)
737 ns_main_earlyfatal("isc_time_now() failed: %s",
738 isc_result_totext(result));
740 result = create_managers();
741 if (result != ISC_R_SUCCESS)
742 ns_main_earlyfatal("create_managers() failed: %s",
743 isc_result_totext(result));
748 * Add calls to register sdb drivers here.
754 * Register any DLZ drivers.
756 result = dlz_drivers_init();
757 if (result != ISC_R_SUCCESS)
758 ns_main_earlyfatal("dlz_drivers_init() failed: %s",
759 isc_result_totext(result));
762 ns_server_create(ns_g_mctx, &ns_g_server);
769 ns_server_destroy(&ns_g_server);
774 * Add calls to unregister sdb drivers here.
780 * Unregister any DLZ drivers.
787 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
788 ISC_LOG_NOTICE, "exiting");
792 static char *memstats = NULL;
795 ns_main_setmemstats(const char *filename) {
797 * Caller has to ensure locking.
800 if (memstats != NULL) {
804 if (filename == NULL)
806 memstats = malloc(strlen(filename) + 1);
808 strcpy(memstats, filename);
813 * Get FMRI for the named process.
816 ns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
817 scf_handle_t *h = NULL;
821 REQUIRE(ins_name != NULL && *ins_name == NULL);
823 if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
825 UNEXPECTED_ERROR(__FILE__, __LINE__,
826 "scf_handle_create() failed: %s",
827 scf_strerror(scf_error()));
828 return (ISC_R_FAILURE);
831 if (scf_handle_bind(h) == -1) {
833 UNEXPECTED_ERROR(__FILE__, __LINE__,
834 "scf_handle_bind() failed: %s",
835 scf_strerror(scf_error()));
836 scf_handle_destroy(h);
837 return (ISC_R_FAILURE);
840 if ((namelen = scf_myname(h, NULL, 0)) == -1) {
842 UNEXPECTED_ERROR(__FILE__, __LINE__,
843 "scf_myname() failed: %s",
844 scf_strerror(scf_error()));
845 scf_handle_destroy(h);
846 return (ISC_R_FAILURE);
849 if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
850 UNEXPECTED_ERROR(__FILE__, __LINE__,
851 "ns_smf_get_instance memory "
852 "allocation failed: %s",
853 isc_result_totext(ISC_R_NOMEMORY));
854 scf_handle_destroy(h);
855 return (ISC_R_FAILURE);
858 if (scf_myname(h, instance, namelen + 1) == -1) {
860 UNEXPECTED_ERROR(__FILE__, __LINE__,
861 "scf_myname() failed: %s",
862 scf_strerror(scf_error()));
863 scf_handle_destroy(h);
864 isc_mem_free(mctx, instance);
865 return (ISC_R_FAILURE);
868 scf_handle_destroy(h);
869 *ins_name = instance;
870 return (ISC_R_SUCCESS);
872 #endif /* HAVE_LIBSCF */
875 main(int argc, char *argv[]) {
878 char *instance = NULL;
882 * Record version in core image.
883 * strings named.core | grep "named version:"
886 #if defined(NO_VERSION_DATE) || !defined(__DATE__)
887 "named version: BIND " VERSION,
889 "named version: BIND " VERSION " (" __DATE__ ")",
892 result = isc_file_progname(*argv, program_name, sizeof(program_name));
893 if (result != ISC_R_SUCCESS)
894 ns_main_earlyfatal("program name too long");
896 if (strcmp(program_name, "lwresd") == 0)
897 ns_g_lwresdonly = ISC_TRUE;
899 isc_assertion_setcallback(assertion_failed);
900 isc_error_setfatal(library_fatal_error);
901 isc_error_setunexpected(library_unexpected_error);
903 ns_os_init(program_name);
905 dns_result_register();
906 dst_result_register();
907 isccc_result_register();
909 parse_command_line(argc, argv);
912 * Warn about common configuration error.
914 if (ns_g_chrootdir != NULL) {
915 int len = strlen(ns_g_chrootdir);
916 if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
917 (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
918 ns_main_earlywarning("config filename (-c %s) contains "
919 "chroot path (-t %s)",
920 ns_g_conffile, ns_g_chrootdir);
923 result = isc_mem_create(0, 0, &ns_g_mctx);
924 if (result != ISC_R_SUCCESS)
925 ns_main_earlyfatal("isc_mem_create() failed: %s",
926 isc_result_totext(result));
927 isc_mem_setname(ns_g_mctx, "main", NULL);
932 * Start things running and then wait for a shutdown request
936 result = isc_app_run();
938 if (result == ISC_R_RELOAD) {
939 ns_server_reloadwanted(ns_g_server);
940 } else if (result != ISC_R_SUCCESS) {
941 UNEXPECTED_ERROR(__FILE__, __LINE__,
943 isc_result_totext(result));
947 result = ISC_R_SUCCESS;
949 } while (result != ISC_R_SUCCESS);
952 if (ns_smf_want_disable == 1) {
953 result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
954 if (result == ISC_R_SUCCESS && instance != NULL) {
955 if (smf_disable_instance(instance, 0) != 0)
956 UNEXPECTED_ERROR(__FILE__, __LINE__,
957 "smf_disable_instance() "
958 "failed for %s : %s",
960 scf_strerror(scf_error()));
962 if (instance != NULL)
963 isc_mem_free(ns_g_mctx, instance);
965 #endif /* HAVE_LIBSCF */
970 isc_mem_stats(ns_g_mctx, stdout);
971 isc_mutex_stats(stdout);
974 if (ns_g_memstatistics && memstats != NULL) {
976 result = isc_stdio_open(memstats, "w", &fp);
977 if (result == ISC_R_SUCCESS) {
978 isc_mem_stats(ns_g_mctx, fp);
983 isc_mem_destroy(&ns_g_mctx);
984 isc_mem_checkdestroyed(stderr);
986 ns_main_setmemstats(NULL);
990 ns_os_closedevnull();