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.175.166.2 2010/06/26 23:46:39 tbox 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>
55 * Defining NS_MAIN provides storage declarations (rather than extern)
56 * for variables in named/globals.h.
60 #include <named/builtin.h>
61 #include <named/control.h>
62 #include <named/globals.h> /* Explicit, though named/log.h includes it. */
63 #include <named/interfacemgr.h>
64 #include <named/log.h>
66 #include <named/server.h>
67 #include <named/lwresd.h>
68 #include <named/main.h>
70 #include <named/ns_smf_globals.h>
74 * Include header files for database drivers here.
76 /* #include "xxdb.h" */
79 * Include DLZ drivers if appropriate.
82 #include <dlz/dlz_drivers.h>
86 * The maximum number of stack frames to dump on assertion failure.
88 #ifndef BACKTRACE_MAXFRAME
89 #define BACKTRACE_MAXFRAME 128
92 static isc_boolean_t want_stats = ISC_FALSE;
93 static char program_name[ISC_DIR_NAMEMAX] = "named";
94 static char absolute_conffile[ISC_DIR_PATHMAX];
95 static char saved_command_line[512];
96 static char version[512];
97 static unsigned int maxsocks = 0;
98 static int maxudp = 0;
101 ns_main_earlywarning(const char *format, ...) {
104 va_start(args, format);
105 if (ns_g_lctx != NULL) {
106 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
107 NS_LOGMODULE_MAIN, ISC_LOG_WARNING,
110 fprintf(stderr, "%s: ", program_name);
111 vfprintf(stderr, format, args);
112 fprintf(stderr, "\n");
119 ns_main_earlyfatal(const char *format, ...) {
122 va_start(args, format);
123 if (ns_g_lctx != NULL) {
124 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
125 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
127 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
128 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
129 "exiting (due to early fatal error)");
131 fprintf(stderr, "%s: ", program_name);
132 vfprintf(stderr, format, args);
133 fprintf(stderr, "\n");
141 ISC_PLATFORM_NORETURN_PRE static void
142 assertion_failed(const char *file, int line, isc_assertiontype_t type,
143 const char *cond) ISC_PLATFORM_NORETURN_POST;
146 assertion_failed(const char *file, int line, isc_assertiontype_t type,
149 void *tracebuf[BACKTRACE_MAXFRAME];
152 const char *logsuffix = "";
156 * Handle assertion failures.
159 if (ns_g_lctx != NULL) {
161 * Reset the assertion callback in case it is the log
162 * routines causing the assertion.
164 isc_assertion_setcallback(NULL);
166 result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME,
168 if (result == ISC_R_SUCCESS && nframes > 0)
169 logsuffix = ", back trace";
170 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
171 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
172 "%s:%d: %s(%s) failed%s", file, line,
173 isc_assertion_typetotext(type), cond, logsuffix);
174 if (result == ISC_R_SUCCESS) {
175 for (i = 0; i < nframes; i++) {
176 unsigned long offset;
179 result = isc_backtrace_getsymbol(tracebuf[i],
182 if (result == ISC_R_SUCCESS) {
183 isc_log_write(ns_g_lctx,
184 NS_LOGCATEGORY_GENERAL,
187 "#%d %p in %s()+0x%lx", i,
191 isc_log_write(ns_g_lctx,
192 NS_LOGCATEGORY_GENERAL,
200 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
201 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
202 "exiting (due to assertion failure)");
204 fprintf(stderr, "%s:%d: %s(%s) failed\n",
205 file, line, isc_assertion_typetotext(type), cond);
214 ISC_PLATFORM_NORETURN_PRE static void
215 library_fatal_error(const char *file, int line, const char *format,
217 ISC_FORMAT_PRINTF(3, 0) ISC_PLATFORM_NORETURN_POST;
220 library_fatal_error(const char *file, int line, const char *format,
224 * Handle isc_error_fatal() calls from our libraries.
227 if (ns_g_lctx != NULL) {
229 * Reset the error callback in case it is the log
230 * routines causing the assertion.
232 isc_error_setfatal(NULL);
234 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
235 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
236 "%s:%d: fatal error:", file, line);
237 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
238 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
240 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
241 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
242 "exiting (due to fatal error in library)");
244 fprintf(stderr, "%s:%d: fatal error: ", file, line);
245 vfprintf(stderr, format, args);
246 fprintf(stderr, "\n");
256 library_unexpected_error(const char *file, int line, const char *format,
257 va_list args) ISC_FORMAT_PRINTF(3, 0);
260 library_unexpected_error(const char *file, int line, const char *format,
264 * Handle isc_error_unexpected() calls from our libraries.
267 if (ns_g_lctx != NULL) {
268 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
269 NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
270 "%s:%d: unexpected error:", file, line);
271 isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
272 NS_LOGMODULE_MAIN, ISC_LOG_ERROR,
275 fprintf(stderr, "%s:%d: fatal error: ", file, line);
276 vfprintf(stderr, format, args);
277 fprintf(stderr, "\n");
285 "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] "
287 " [-f|-g] [-n number_of_cpus] [-p port] "
288 "[-P listen-port] [-s]\n"
289 " [-t chrootdir] [-u username] [-i pidfile]\n"
290 " [-m {usage|trace|record|size|mctx}]\n");
295 if (ns_g_lwresdonly) {
300 "usage: named [-4|-6] [-c conffile] [-d debuglevel] "
301 "[-E engine] [-f|-g]\n"
302 " [-n number_of_cpus] [-p port] [-s] "
303 "[-t chrootdir] [-u username]\n"
304 " [-m {usage|trace|record|size|mctx}]\n");
308 save_command_line(int argc, char *argv[]) {
313 const char truncated[] = "...";
314 isc_boolean_t quoted = ISC_FALSE;
316 dst = saved_command_line;
317 eob = saved_command_line + sizeof(saved_command_line);
319 for (i = 1; i < argc && dst < eob; i++) {
323 while (*src != '\0' && dst < eob) {
325 * This won't perfectly produce a shell-independent
326 * pastable command line in all circumstances, but
327 * comes close, and for practical purposes will
328 * nearly always be fine.
330 if (quoted || isalnum(*src & 0xff) ||
331 *src == '-' || *src == '_' ||
332 *src == '.' || *src == '/') {
342 INSIST(sizeof(saved_command_line) >= sizeof(truncated));
345 strcpy(eob - sizeof(truncated), truncated);
351 parse_int(char *arg, const char *desc) {
356 ltmp = strtol(arg, &endp, 10);
359 ns_main_earlyfatal("%s '%s' must be numeric", desc, arg);
360 if (tmp < 0 || tmp != ltmp)
361 ns_main_earlyfatal("%s '%s' out of range", desc, arg);
365 static struct flag_def {
368 } mem_debug_flags[] = {
369 { "trace", ISC_MEM_DEBUGTRACE },
370 { "record", ISC_MEM_DEBUGRECORD },
371 { "usage", ISC_MEM_DEBUGUSAGE },
372 { "size", ISC_MEM_DEBUGSIZE },
373 { "mctx", ISC_MEM_DEBUGCTX },
378 set_flags(const char *arg, struct flag_def *defs, unsigned int *ret) {
380 const struct flag_def *def;
381 const char *end = strchr(arg, ',');
384 end = arg + strlen(arg);
386 for (def = defs; def->name != NULL; def++) {
387 if (arglen == (int)strlen(def->name) &&
388 memcmp(arg, def->name, arglen) == 0) {
393 ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg);
402 parse_command_line(int argc, char *argv[]) {
405 isc_boolean_t disable6 = ISC_FALSE;
406 isc_boolean_t disable4 = ISC_FALSE;
408 save_command_line(argc, argv);
410 isc_commandline_errprint = ISC_FALSE;
411 while ((ch = isc_commandline_parse(argc, argv,
412 "46c:C:d:E:fFgi:lm:n:N:p:P:"
413 "sS:t:T:u:vVx:")) != -1) {
417 ns_main_earlyfatal("cannot specify -4 and -6");
418 if (isc_net_probeipv4() != ISC_R_SUCCESS)
419 ns_main_earlyfatal("IPv4 not supported by OS");
420 isc_net_disableipv6();
425 ns_main_earlyfatal("cannot specify -4 and -6");
426 if (isc_net_probeipv6() != ISC_R_SUCCESS)
427 ns_main_earlyfatal("IPv6 not supported by OS");
428 isc_net_disableipv4();
432 ns_g_conffile = isc_commandline_argument;
433 lwresd_g_conffile = isc_commandline_argument;
434 if (lwresd_g_useresolvconf)
435 ns_main_earlyfatal("cannot specify -c and -C");
436 ns_g_conffileset = ISC_TRUE;
439 lwresd_g_resolvconffile = isc_commandline_argument;
440 if (ns_g_conffileset)
441 ns_main_earlyfatal("cannot specify -c and -C");
442 lwresd_g_useresolvconf = ISC_TRUE;
445 ns_g_debuglevel = parse_int(isc_commandline_argument,
449 ns_g_engine = isc_commandline_argument;
452 ns_g_foreground = ISC_TRUE;
455 ns_g_foreground = ISC_TRUE;
456 ns_g_logstderr = ISC_TRUE;
458 /* XXXBEW -i should be removed */
460 lwresd_g_defaultpidfile = isc_commandline_argument;
463 ns_g_lwresdonly = ISC_TRUE;
466 set_flags(isc_commandline_argument, mem_debug_flags,
469 case 'N': /* Deprecated. */
471 ns_g_cpus = parse_int(isc_commandline_argument,
477 port = parse_int(isc_commandline_argument, "port");
478 if (port < 1 || port > 65535)
479 ns_main_earlyfatal("port '%s' out of range",
480 isc_commandline_argument);
483 /* XXXBEW Should -P be removed? */
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);
489 lwresd_g_listenport = port;
492 /* XXXRTH temporary syntax */
493 want_stats = ISC_TRUE;
496 maxsocks = parse_int(isc_commandline_argument,
497 "max number of sockets");
500 /* XXXJAB should we make a copy? */
501 ns_g_chrootdir = isc_commandline_argument;
503 case 'T': /* NOT DOCUMENTED */
505 * clienttest: make clients single shot with their
506 * own memory context.
508 if (!strcmp(isc_commandline_argument, "clienttest"))
509 ns_g_clienttest = ISC_TRUE;
510 else if (!strcmp(isc_commandline_argument, "nosoa"))
511 ns_g_nosoa = ISC_TRUE;
512 else if (!strcmp(isc_commandline_argument, "maxudp512"))
514 else if (!strcmp(isc_commandline_argument, "maxudp1460"))
517 fprintf(stderr, "unknown -T flag '%s\n",
518 isc_commandline_argument);
521 ns_g_username = isc_commandline_argument;
524 printf("BIND %s\n", ns_g_version);
527 printf("BIND %s built with %s\n", ns_g_version,
531 /* Reserved for FIPS mode */
535 if (isc_commandline_option == '?')
537 ns_main_earlyfatal("unknown option '-%c'",
538 isc_commandline_option);
541 ns_main_earlyfatal("parsing options returned %d", ch);
545 argc -= isc_commandline_index;
546 argv += isc_commandline_index;
550 ns_main_earlyfatal("extra command line arguments");
555 create_managers(void) {
559 #ifdef ISC_PLATFORM_USETHREADS
561 ns_g_cpus = ns_g_cpus_detected;
562 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
563 ISC_LOG_INFO, "found %u CPU%s, using %u worker thread%s",
564 ns_g_cpus_detected, ns_g_cpus_detected == 1 ? "" : "s",
565 ns_g_cpus, ns_g_cpus == 1 ? "" : "s");
569 result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
570 if (result != ISC_R_SUCCESS) {
571 UNEXPECTED_ERROR(__FILE__, __LINE__,
572 "isc_taskmgr_create() failed: %s",
573 isc_result_totext(result));
574 return (ISC_R_UNEXPECTED);
577 result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
578 if (result != ISC_R_SUCCESS) {
579 UNEXPECTED_ERROR(__FILE__, __LINE__,
580 "isc_timermgr_create() failed: %s",
581 isc_result_totext(result));
582 return (ISC_R_UNEXPECTED);
585 result = isc_socketmgr_create2(ns_g_mctx, &ns_g_socketmgr, maxsocks);
586 if (result != ISC_R_SUCCESS) {
587 UNEXPECTED_ERROR(__FILE__, __LINE__,
588 "isc_socketmgr_create() failed: %s",
589 isc_result_totext(result));
590 return (ISC_R_UNEXPECTED);
592 isc__socketmgr_maxudp(ns_g_socketmgr, maxudp);
593 result = isc_socketmgr_getmaxsockets(ns_g_socketmgr, &socks);
594 if (result == ISC_R_SUCCESS) {
595 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
597 ISC_LOG_INFO, "using up to %u sockets", socks);
600 result = isc_entropy_create(ns_g_mctx, &ns_g_entropy);
601 if (result != ISC_R_SUCCESS) {
602 UNEXPECTED_ERROR(__FILE__, __LINE__,
603 "isc_entropy_create() failed: %s",
604 isc_result_totext(result));
605 return (ISC_R_UNEXPECTED);
608 result = isc_hash_create(ns_g_mctx, ns_g_entropy, DNS_NAME_MAXWIRE);
609 if (result != ISC_R_SUCCESS) {
610 UNEXPECTED_ERROR(__FILE__, __LINE__,
611 "isc_hash_create() failed: %s",
612 isc_result_totext(result));
613 return (ISC_R_UNEXPECTED);
616 return (ISC_R_SUCCESS);
620 destroy_managers(void) {
621 ns_lwresd_shutdown();
623 isc_entropy_detach(&ns_g_entropy);
624 if (ns_g_fallbackentropy != NULL)
625 isc_entropy_detach(&ns_g_fallbackentropy);
628 * isc_taskmgr_destroy() will block until all tasks have exited,
630 isc_taskmgr_destroy(&ns_g_taskmgr);
631 isc_timermgr_destroy(&ns_g_timermgr);
632 isc_socketmgr_destroy(&ns_g_socketmgr);
635 * isc_hash_destroy() cannot be called as long as a resolver may be
636 * running. Calling this after isc_taskmgr_destroy() ensures the
649 if (isc__backtrace_nsymbols == 0)
652 if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99)))
655 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
656 ISC_LOG_DEBUG(99), "Symbol table:");
658 for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) {
661 result = isc_backtrace_getsymbolfromindex(i, &addr, &fname);
662 if (result == ISC_R_SUCCESS) {
663 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
664 NS_LOGMODULE_MAIN, ISC_LOG_DEBUG(99),
665 "[%d] %p %s", i, addr, fname);
673 isc_resourcevalue_t old_openfiles;
675 char *instance = NULL;
679 * Get the user and group information before changing the root
680 * directory, so the administrator does not need to keep a copy
681 * of the user and group databases in the chroot'ed environment.
683 ns_os_inituserinfo(ns_g_username);
686 * Initialize time conversion information
693 /* Check if named is under smf control, before chroot. */
694 result = ns_smf_get_instance(&instance, 0, ns_g_mctx);
695 /* We don't care about instance, just check if we got one. */
696 if (result == ISC_R_SUCCESS)
697 ns_smf_got_instance = 1;
699 ns_smf_got_instance = 0;
700 if (instance != NULL)
701 isc_mem_free(ns_g_mctx, instance);
702 #endif /* HAVE_LIBSCF */
704 #ifdef PATH_RANDOMDEV
706 * Initialize system's random device as fallback entropy source
707 * if running chroot'ed.
709 if (ns_g_chrootdir != NULL) {
710 result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy);
711 if (result != ISC_R_SUCCESS)
712 ns_main_earlyfatal("isc_entropy_create() failed: %s",
713 isc_result_totext(result));
715 result = isc_entropy_createfilesource(ns_g_fallbackentropy,
717 if (result != ISC_R_SUCCESS) {
718 ns_main_earlywarning("could not open pre-chroot "
719 "entropy source %s: %s",
721 isc_result_totext(result));
722 isc_entropy_detach(&ns_g_fallbackentropy);
727 #ifdef ISC_PLATFORM_USETHREADS
729 * Check for the number of cpu's before ns_os_chroot().
731 ns_g_cpus_detected = isc_os_ncpus();
734 ns_os_chroot(ns_g_chrootdir);
737 * For operating systems which have a capability mechanism, now
738 * is the time to switch to minimal privs and change our user id.
739 * On traditional UNIX systems, this call will be a no-op, and we
740 * will change the user ID after reading the config file the first
741 * time. (We need to read the config file to know which possibly
742 * privileged ports to bind() to.)
746 result = ns_log_init(ISC_TF(ns_g_username != NULL));
747 if (result != ISC_R_SUCCESS)
748 ns_main_earlyfatal("ns_log_init() failed: %s",
749 isc_result_totext(result));
752 * Now is the time to daemonize (if we're not running in the
753 * foreground). We waited until now because we wanted to get
754 * a valid logging context setup. We cannot daemonize any later,
755 * because calling create_managers() will create threads, which
756 * would be lost after fork().
758 if (!ns_g_foreground)
762 * We call isc_app_start() here as some versions of FreeBSD's fork()
763 * destroys all the signal handling it sets up.
765 result = isc_app_start();
766 if (result != ISC_R_SUCCESS)
767 ns_main_earlyfatal("isc_app_start() failed: %s",
768 isc_result_totext(result));
770 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
771 ISC_LOG_NOTICE, "starting BIND %s%s", ns_g_version,
774 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
775 ISC_LOG_NOTICE, "built with %s", ns_g_configargs);
780 * Get the initial resource limits.
782 (void)isc_resource_getlimit(isc_resource_stacksize,
783 &ns_g_initstacksize);
784 (void)isc_resource_getlimit(isc_resource_datasize,
786 (void)isc_resource_getlimit(isc_resource_coresize,
788 (void)isc_resource_getlimit(isc_resource_openfiles,
789 &ns_g_initopenfiles);
792 * System resources cannot effectively be tuned on some systems.
793 * Raise the limit in such cases for safety.
795 old_openfiles = ns_g_initopenfiles;
796 ns_os_adjustnofile();
797 (void)isc_resource_getlimit(isc_resource_openfiles,
798 &ns_g_initopenfiles);
799 if (old_openfiles != ns_g_initopenfiles) {
800 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
801 NS_LOGMODULE_MAIN, ISC_LOG_NOTICE,
802 "adjusted limit on open files from "
803 "%" ISC_PRINT_QUADFORMAT "u to "
804 "%" ISC_PRINT_QUADFORMAT "u",
805 old_openfiles, ns_g_initopenfiles);
809 * If the named configuration filename is relative, prepend the current
810 * directory's name before possibly changing to another directory.
812 if (! isc_file_isabsolute(ns_g_conffile)) {
813 result = isc_file_absolutepath(ns_g_conffile,
815 sizeof(absolute_conffile));
816 if (result != ISC_R_SUCCESS)
817 ns_main_earlyfatal("could not construct absolute path "
818 "of configuration file: %s",
819 isc_result_totext(result));
820 ns_g_conffile = absolute_conffile;
824 * Record the server's startup time.
826 result = isc_time_now(&ns_g_boottime);
827 if (result != ISC_R_SUCCESS)
828 ns_main_earlyfatal("isc_time_now() failed: %s",
829 isc_result_totext(result));
831 result = create_managers();
832 if (result != ISC_R_SUCCESS)
833 ns_main_earlyfatal("create_managers() failed: %s",
834 isc_result_totext(result));
839 * Add calls to register sdb drivers here.
845 * Register any DLZ drivers.
847 result = dlz_drivers_init();
848 if (result != ISC_R_SUCCESS)
849 ns_main_earlyfatal("dlz_drivers_init() failed: %s",
850 isc_result_totext(result));
853 ns_server_create(ns_g_mctx, &ns_g_server);
860 ns_server_destroy(&ns_g_server);
865 * Add calls to unregister sdb drivers here.
871 * Unregister any DLZ drivers.
878 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
879 ISC_LOG_NOTICE, "exiting");
883 static char *memstats = NULL;
886 ns_main_setmemstats(const char *filename) {
888 * Caller has to ensure locking.
891 if (memstats != NULL) {
895 if (filename == NULL)
897 memstats = malloc(strlen(filename) + 1);
899 strcpy(memstats, filename);
904 * Get FMRI for the named process.
907 ns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) {
908 scf_handle_t *h = NULL;
912 REQUIRE(ins_name != NULL && *ins_name == NULL);
914 if ((h = scf_handle_create(SCF_VERSION)) == NULL) {
916 UNEXPECTED_ERROR(__FILE__, __LINE__,
917 "scf_handle_create() failed: %s",
918 scf_strerror(scf_error()));
919 return (ISC_R_FAILURE);
922 if (scf_handle_bind(h) == -1) {
924 UNEXPECTED_ERROR(__FILE__, __LINE__,
925 "scf_handle_bind() failed: %s",
926 scf_strerror(scf_error()));
927 scf_handle_destroy(h);
928 return (ISC_R_FAILURE);
931 if ((namelen = scf_myname(h, NULL, 0)) == -1) {
933 UNEXPECTED_ERROR(__FILE__, __LINE__,
934 "scf_myname() failed: %s",
935 scf_strerror(scf_error()));
936 scf_handle_destroy(h);
937 return (ISC_R_FAILURE);
940 if ((instance = isc_mem_allocate(mctx, namelen + 1)) == NULL) {
941 UNEXPECTED_ERROR(__FILE__, __LINE__,
942 "ns_smf_get_instance memory "
943 "allocation failed: %s",
944 isc_result_totext(ISC_R_NOMEMORY));
945 scf_handle_destroy(h);
946 return (ISC_R_FAILURE);
949 if (scf_myname(h, instance, namelen + 1) == -1) {
951 UNEXPECTED_ERROR(__FILE__, __LINE__,
952 "scf_myname() failed: %s",
953 scf_strerror(scf_error()));
954 scf_handle_destroy(h);
955 isc_mem_free(mctx, instance);
956 return (ISC_R_FAILURE);
959 scf_handle_destroy(h);
960 *ins_name = instance;
961 return (ISC_R_SUCCESS);
963 #endif /* HAVE_LIBSCF */
966 main(int argc, char *argv[]) {
969 char *instance = NULL;
973 * Record version in core image.
974 * strings named.core | grep "named version:"
977 #if defined(NO_VERSION_DATE) || !defined(__DATE__)
978 "named version: BIND " VERSION,
980 "named version: BIND " VERSION " (" __DATE__ ")",
983 result = isc_file_progname(*argv, program_name, sizeof(program_name));
984 if (result != ISC_R_SUCCESS)
985 ns_main_earlyfatal("program name too long");
987 if (strcmp(program_name, "lwresd") == 0)
988 ns_g_lwresdonly = ISC_TRUE;
990 if (result != ISC_R_SUCCESS)
991 ns_main_earlyfatal("failed to build internal symbol table");
993 isc_assertion_setcallback(assertion_failed);
994 isc_error_setfatal(library_fatal_error);
995 isc_error_setunexpected(library_unexpected_error);
997 ns_os_init(program_name);
999 dns_result_register();
1000 dst_result_register();
1001 isccc_result_register();
1003 parse_command_line(argc, argv);
1006 * Warn about common configuration error.
1008 if (ns_g_chrootdir != NULL) {
1009 int len = strlen(ns_g_chrootdir);
1010 if (strncmp(ns_g_chrootdir, ns_g_conffile, len) == 0 &&
1011 (ns_g_conffile[len] == '/' || ns_g_conffile[len] == '\\'))
1012 ns_main_earlywarning("config filename (-c %s) contains "
1013 "chroot path (-t %s)",
1014 ns_g_conffile, ns_g_chrootdir);
1017 result = isc_mem_create(0, 0, &ns_g_mctx);
1018 if (result != ISC_R_SUCCESS)
1019 ns_main_earlyfatal("isc_mem_create() failed: %s",
1020 isc_result_totext(result));
1021 isc_mem_setname(ns_g_mctx, "main", NULL);
1026 * Start things running and then wait for a shutdown request
1030 result = isc_app_run();
1032 if (result == ISC_R_RELOAD) {
1033 ns_server_reloadwanted(ns_g_server);
1034 } else if (result != ISC_R_SUCCESS) {
1035 UNEXPECTED_ERROR(__FILE__, __LINE__,
1036 "isc_app_run(): %s",
1037 isc_result_totext(result));
1041 result = ISC_R_SUCCESS;
1043 } while (result != ISC_R_SUCCESS);
1046 if (ns_smf_want_disable == 1) {
1047 result = ns_smf_get_instance(&instance, 1, ns_g_mctx);
1048 if (result == ISC_R_SUCCESS && instance != NULL) {
1049 if (smf_disable_instance(instance, 0) != 0)
1050 UNEXPECTED_ERROR(__FILE__, __LINE__,
1051 "smf_disable_instance() "
1052 "failed for %s : %s",
1054 scf_strerror(scf_error()));
1056 if (instance != NULL)
1057 isc_mem_free(ns_g_mctx, instance);
1059 #endif /* HAVE_LIBSCF */
1064 isc_mem_stats(ns_g_mctx, stdout);
1065 isc_mutex_stats(stdout);
1068 if (ns_g_memstatistics && memstats != NULL) {
1070 result = isc_stdio_open(memstats, "w", &fp);
1071 if (result == ISC_R_SUCCESS) {
1072 isc_mem_stats(ns_g_mctx, fp);
1073 isc_mutex_stats(fp);
1074 isc_stdio_close(fp);
1077 isc_mem_destroy(&ns_g_mctx);
1078 isc_mem_checkdestroyed(stderr);
1080 ns_main_setmemstats(NULL);
1084 ns_os_closedevnull();