1/* 2 * Copyright (C) 2004-2013 Internet Systems Consortium, Inc. ("ISC") 3 * Copyright (C) 1999-2003 Internet Software Consortium. 4 * 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. 8 * 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. 16 */ 17 18/* $Id$ */ 19 20/*! \file */ 21 22#include <config.h> 23 24#include <ctype.h> 25#include <stdlib.h> 26#include <string.h> 27 28#include <isc/app.h> 29#include <isc/backtrace.h> 30#include <isc/commandline.h> 31#include <isc/dir.h> 32#include <isc/entropy.h> 33#include <isc/file.h> 34#include <isc/hash.h> 35#include <isc/os.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> 41#include <isc/task.h> 42#include <isc/timer.h> 43#include <isc/util.h> 44 45#include <isccc/result.h> 46 47#include <dns/dispatch.h> 48#include <dns/name.h> 49#include <dns/result.h> 50#include <dns/view.h> 51 52#include <dst/result.h> 53 54#include <dlz/dlz_dlopen_driver.h> 55 56/* 57 * Defining NS_MAIN provides storage declarations (rather than extern) 58 * for variables in named/globals.h. 59 */ 60#define NS_MAIN 1 61 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> 67#include <named/os.h> 68#include <named/server.h> 69#include <named/lwresd.h> 70#include <named/main.h> 71#ifdef HAVE_LIBSCF 72#include <named/ns_smf_globals.h> 73#endif 74 75#ifdef OPENSSL 76#include <openssl/opensslv.h> 77#endif 78#ifdef HAVE_LIBXML2 79#include <libxml/xmlversion.h> 80#endif 81/* 82 * Include header files for database drivers here. 83 */ 84/* #include "xxdb.h" */ 85 86#ifdef CONTRIB_DLZ 87/* 88 * Include contributed DLZ drivers if appropriate. 89 */ 90#include <dlz/dlz_drivers.h> 91#endif 92 93/* 94 * The maximum number of stack frames to dump on assertion failure. 95 */ 96#ifndef BACKTRACE_MAXFRAME 97#define BACKTRACE_MAXFRAME 128 98#endif 99 100static isc_boolean_t want_stats = ISC_FALSE; 101static char program_name[ISC_DIR_NAMEMAX] = "named"; 102static char absolute_conffile[ISC_DIR_PATHMAX]; 103static char saved_command_line[512]; 104static char version[512]; 105static unsigned int maxsocks = 0; 106static int maxudp = 0; 107 108void 109ns_main_earlywarning(const char *format, ...) { 110 va_list args; 111 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, 116 format, args); 117 } else { 118 fprintf(stderr, "%s: ", program_name); 119 vfprintf(stderr, format, args); 120 fprintf(stderr, "\n"); 121 fflush(stderr); 122 } 123 va_end(args); 124} 125 126void 127ns_main_earlyfatal(const char *format, ...) { 128 va_list args; 129 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, 134 format, args); 135 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, 136 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL, 137 "exiting (due to early fatal error)"); 138 } else { 139 fprintf(stderr, "%s: ", program_name); 140 vfprintf(stderr, format, args); 141 fprintf(stderr, "\n"); 142 fflush(stderr); 143 } 144 va_end(args); 145 146 exit(1); 147} 148 149ISC_PLATFORM_NORETURN_PRE static void 150assertion_failed(const char *file, int line, isc_assertiontype_t type, 151 const char *cond) ISC_PLATFORM_NORETURN_POST; 152 153static void 154assertion_failed(const char *file, int line, isc_assertiontype_t type, 155 const char *cond) 156{ 157 void *tracebuf[BACKTRACE_MAXFRAME]; 158 int i, nframes; 159 isc_result_t result; 160 const char *logsuffix = ""; 161 const char *fname; 162 163 /* 164 * Handle assertion failures. 165 */ 166 167 if (ns_g_lctx != NULL) { 168 /* 169 * Reset the assertion callback in case it is the log 170 * routines causing the assertion. 171 */ 172 isc_assertion_setcallback(NULL); 173 174 result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME, 175 &nframes); 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; 185 186 fname = NULL; 187 result = isc_backtrace_getsymbol(tracebuf[i], 188 &fname, 189 &offset); 190 if (result == ISC_R_SUCCESS) { 191 isc_log_write(ns_g_lctx, 192 NS_LOGCATEGORY_GENERAL, 193 NS_LOGMODULE_MAIN, 194 ISC_LOG_CRITICAL, 195 "#%d %p in %s()+0x%lx", i, 196 tracebuf[i], fname, 197 offset); 198 } else { 199 isc_log_write(ns_g_lctx, 200 NS_LOGCATEGORY_GENERAL, 201 NS_LOGMODULE_MAIN, 202 ISC_LOG_CRITICAL, 203 "#%d %p in ??", i, 204 tracebuf[i]); 205 } 206 } 207 } 208 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, 209 NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL, 210 "exiting (due to assertion failure)"); 211 } else { 212 fprintf(stderr, "%s:%d: %s(%s) failed\n", 213 file, line, isc_assertion_typetotext(type), cond); 214 fflush(stderr); 215 } 216 217 if (ns_g_coreok) 218 abort(); 219 exit(1); 220} 221 222ISC_PLATFORM_NORETURN_PRE static void 223library_fatal_error(const char *file, int line, const char *format, 224 va_list args) 225ISC_FORMAT_PRINTF(3, 0) ISC_PLATFORM_NORETURN_POST; 226 227static void 228library_fatal_error(const char *file, int line, const char *format, 229 va_list args) 230{ 231 /* 232 * Handle isc_error_fatal() calls from our libraries. 233 */ 234 235 if (ns_g_lctx != NULL) { 236 /* 237 * Reset the error callback in case it is the log 238 * routines causing the assertion. 239 */ 240 isc_error_setfatal(NULL); 241 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, 247 format, args); 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)"); 251 } else { 252 fprintf(stderr, "%s:%d: fatal error: ", file, line); 253 vfprintf(stderr, format, args); 254 fprintf(stderr, "\n"); 255 fflush(stderr); 256 } 257 258 if (ns_g_coreok) 259 abort(); 260 exit(1); 261} 262 263static void 264library_unexpected_error(const char *file, int line, const char *format, 265 va_list args) ISC_FORMAT_PRINTF(3, 0); 266 267static void 268library_unexpected_error(const char *file, int line, const char *format, 269 va_list args) 270{ 271 /* 272 * Handle isc_error_unexpected() calls from our libraries. 273 */ 274 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, 281 format, args); 282 } else { 283 fprintf(stderr, "%s:%d: fatal error: ", file, line); 284 vfprintf(stderr, format, args); 285 fprintf(stderr, "\n"); 286 fflush(stderr); 287 } 288} 289 290static void 291lwresd_usage(void) { 292 fprintf(stderr, 293 "usage: lwresd [-4|-6] [-c conffile | -C resolvconffile] " 294 "[-d debuglevel]\n" 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"); 299} 300 301static void 302usage(void) { 303 if (ns_g_lwresdonly) { 304 lwresd_usage(); 305 return; 306 } 307 fprintf(stderr, 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"); 313} 314 315static void 316save_command_line(int argc, char *argv[]) { 317 int i; 318 char *src; 319 char *dst; 320 char *eob; 321 const char truncated[] = "..."; 322 isc_boolean_t quoted = ISC_FALSE; 323 324 dst = saved_command_line; 325 eob = saved_command_line + sizeof(saved_command_line); 326 327 for (i = 1; i < argc && dst < eob; i++) { 328 *dst++ = ' '; 329 330 src = argv[i]; 331 while (*src != '\0' && dst < eob) { 332 /* 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. 337 */ 338 if (quoted || isalnum(*src & 0xff) || 339 *src == '-' || *src == '_' || 340 *src == '.' || *src == '/') { 341 *dst++ = *src++; 342 quoted = ISC_FALSE; 343 } else { 344 *dst++ = '\\'; 345 quoted = ISC_TRUE; 346 } 347 } 348 } 349 350 INSIST(sizeof(saved_command_line) >= sizeof(truncated)); 351 352 if (dst == eob) 353 strcpy(eob - sizeof(truncated), truncated); 354 else 355 *dst = '\0'; 356} 357 358static int 359parse_int(char *arg, const char *desc) { 360 char *endp; 361 int tmp; 362 long int ltmp; 363 364 ltmp = strtol(arg, &endp, 10); 365 tmp = (int) ltmp; 366 if (*endp != '\0') 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); 370 return (tmp); 371} 372 373static struct flag_def { 374 const char *name; 375 unsigned int value; 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 }, 382 { NULL, 0 } 383}; 384 385static void 386set_flags(const char *arg, struct flag_def *defs, unsigned int *ret) { 387 for (;;) { 388 const struct flag_def *def; 389 const char *end = strchr(arg, ','); 390 int arglen; 391 if (end == NULL) 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) { 397 *ret |= def->value; 398 goto found; 399 } 400 } 401 ns_main_earlyfatal("unrecognized flag '%.*s'", arglen, arg); 402 found: 403 if (*end == '\0') 404 break; 405 arg = end + 1; 406 } 407} 408 409static void 410parse_command_line(int argc, char *argv[]) { 411 int ch; 412 int port; 413 isc_boolean_t disable6 = ISC_FALSE; 414 isc_boolean_t disable4 = ISC_FALSE; 415 416 save_command_line(argc, argv); 417 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) { 423 switch (ch) { 424 case '4': 425 if (disable4) 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(); 430 disable6 = ISC_TRUE; 431 break; 432 case '6': 433 if (disable6) 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(); 438 disable4 = ISC_TRUE; 439 break; 440 case 'c': 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; 446 break; 447 case 'C': 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; 452 break; 453 case 'd': 454 ns_g_debuglevel = parse_int(isc_commandline_argument, 455 "debug level"); 456 break; 457 case 'E': 458 ns_g_engine = isc_commandline_argument; 459 break; 460 case 'f': 461 ns_g_foreground = ISC_TRUE; 462 break; 463 case 'g': 464 ns_g_foreground = ISC_TRUE; 465 ns_g_logstderr = ISC_TRUE; 466 break; 467 /* XXXBEW -i should be removed */ 468 case 'i': 469 lwresd_g_defaultpidfile = isc_commandline_argument; 470 break; 471 case 'l': 472 ns_g_lwresdonly = ISC_TRUE; 473 break; 474 case 'm': 475 set_flags(isc_commandline_argument, mem_debug_flags, 476 &isc_mem_debugging); 477 break; 478 case 'N': /* Deprecated. */ 479 case 'n': 480 ns_g_cpus = parse_int(isc_commandline_argument, 481 "number of cpus"); 482 if (ns_g_cpus == 0) 483 ns_g_cpus = 1; 484 break; 485 case 'p': 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); 490 ns_g_port = port; 491 break; 492 /* XXXBEW Should -P be removed? */ 493 case 'P': 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; 499 break; 500 case 's': 501 /* XXXRTH temporary syntax */ 502 want_stats = ISC_TRUE; 503 break; 504 case 'S': 505 maxsocks = parse_int(isc_commandline_argument, 506 "max number of sockets"); 507 break; 508 case 't': 509 /* XXXJAB should we make a copy? */ 510 ns_g_chrootdir = isc_commandline_argument; 511 break; 512 case 'T': /* NOT DOCUMENTED */ 513 /* 514 * clienttest: make clients single shot with their 515 * own memory context. 516 */ 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")) 524 maxudp = 512; 525 else if (!strcmp(isc_commandline_argument, "maxudp1460")) 526 maxudp = 1460; 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; 531 else 532 fprintf(stderr, "unknown -T flag '%s\n", 533 isc_commandline_argument); 534 break; 535 case 'U': 536 ns_g_udpdisp = parse_int(isc_commandline_argument, 537 "number of UDP listeners " 538 "per interface"); 539 break; 540 case 'u': 541 ns_g_username = isc_commandline_argument; 542 break; 543 case 'v': 544 printf("%s %s", ns_g_product, ns_g_version); 545 if (*ns_g_description != 0) 546 printf(" %s", ns_g_description); 547 printf("\n"); 548 exit(0); 549 case 'V': 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); 555#ifdef __clang__ 556 printf("compiled by CLANG %s\n", __VERSION__); 557#else 558#if defined(__ICC) || defined(__INTEL_COMPILER) 559 printf("compiled by ICC %s\n", __VERSION__); 560#else 561#ifdef __GNUC__ 562 printf("compiled by GCC %s\n", __VERSION__); 563#endif 564#endif 565#endif 566#ifdef _MSC_VER 567 printf("compiled by MSVC %d\n", _MSC_VER); 568#endif 569#ifdef __SUNPRO_C 570 printf("compiled by Solaris Studio %x\n", __SUNPRO_C); 571#endif 572#ifdef OPENSSL 573 printf("using OpenSSL version: %s\n", 574 OPENSSL_VERSION_TEXT); 575#endif 576#ifdef HAVE_LIBXML2 577 printf("using libxml2 version: %s\n", 578 LIBXML_DOTTED_VERSION); 579#endif 580 exit(0); 581 case 'F': 582 /* Reserved for FIPS mode */ 583 /* FALLTHROUGH */ 584 case '?': 585 usage(); 586 if (isc_commandline_option == '?') 587 exit(0); 588 ns_main_earlyfatal("unknown option '-%c'", 589 isc_commandline_option); 590 /* FALLTHROUGH */ 591 default: 592 ns_main_earlyfatal("parsing options returned %d", ch); 593 } 594 } 595 596 argc -= isc_commandline_index; 597 argv += isc_commandline_index; 598 POST(argv); 599 600 if (argc > 0) { 601 usage(); 602 ns_main_earlyfatal("extra command line arguments"); 603 } 604} 605 606static isc_result_t 607create_managers(void) { 608 isc_result_t result; 609 unsigned int socks; 610 611#ifdef ISC_PLATFORM_USETHREADS 612 if (ns_g_cpus == 0) 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"); 618#else 619 ns_g_cpus = 1; 620#endif 621#ifdef WIN32 622 ns_g_udpdisp = 1; 623#else 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; 628#endif 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"); 632 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); 639 } 640 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); 647 } 648 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); 655 } 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, 660 NS_LOGMODULE_SERVER, 661 ISC_LOG_INFO, "using up to %u sockets", socks); 662 } 663 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); 670 } 671 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); 678 } 679 680 return (ISC_R_SUCCESS); 681} 682 683static void 684destroy_managers(void) { 685 ns_lwresd_shutdown(); 686 687 isc_entropy_detach(&ns_g_entropy); 688 if (ns_g_fallbackentropy != NULL) 689 isc_entropy_detach(&ns_g_fallbackentropy); 690 691 /* 692 * isc_taskmgr_destroy() will block until all tasks have exited, 693 */ 694 isc_taskmgr_destroy(&ns_g_taskmgr); 695 isc_timermgr_destroy(&ns_g_timermgr); 696 isc_socketmgr_destroy(&ns_g_socketmgr); 697 698 /* 699 * isc_hash_destroy() cannot be called as long as a resolver may be 700 * running. Calling this after isc_taskmgr_destroy() ensures the 701 * call is safe. 702 */ 703 isc_hash_destroy(); 704} 705 706static void 707dump_symboltable() { 708 int i; 709 isc_result_t result; 710 const char *fname; 711 const void *addr; 712 713 if (isc__backtrace_nsymbols == 0) 714 return; 715 716 if (!isc_log_wouldlog(ns_g_lctx, ISC_LOG_DEBUG(99))) 717 return; 718 719 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 720 ISC_LOG_DEBUG(99), "Symbol table:"); 721 722 for (i = 0, result = ISC_R_SUCCESS; result == ISC_R_SUCCESS; i++) { 723 addr = NULL; 724 fname = NULL; 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); 730 } 731 } 732} 733 734static void 735setup(void) { 736 isc_result_t result; 737 isc_resourcevalue_t old_openfiles; 738#ifdef HAVE_LIBSCF 739 char *instance = NULL; 740#endif 741 742 /* 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. 746 */ 747 ns_os_inituserinfo(ns_g_username); 748 749 /* 750 * Initialize time conversion information 751 */ 752 ns_os_tzset(); 753 754 ns_os_opendevnull(); 755 756#ifdef HAVE_LIBSCF 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; 762 else 763 ns_smf_got_instance = 0; 764 if (instance != NULL) 765 isc_mem_free(ns_g_mctx, instance); 766#endif /* HAVE_LIBSCF */ 767 768#ifdef PATH_RANDOMDEV 769 /* 770 * Initialize system's random device as fallback entropy source 771 * if running chroot'ed. 772 */ 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)); 778 779 result = isc_entropy_createfilesource(ns_g_fallbackentropy, 780 PATH_RANDOMDEV); 781 if (result != ISC_R_SUCCESS) { 782 ns_main_earlywarning("could not open pre-chroot " 783 "entropy source %s: %s", 784 PATH_RANDOMDEV, 785 isc_result_totext(result)); 786 isc_entropy_detach(&ns_g_fallbackentropy); 787 } 788 } 789#endif 790 791#ifdef ISC_PLATFORM_USETHREADS 792 /* 793 * Check for the number of cpu's before ns_os_chroot(). 794 */ 795 ns_g_cpus_detected = isc_os_ncpus(); 796#endif 797 798 ns_os_chroot(ns_g_chrootdir); 799 800 /* 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.) 807 */ 808 ns_os_minprivs(); 809 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)); 814 815 /* 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(). 821 */ 822 if (!ns_g_foreground) 823 ns_os_daemonize(); 824 825 /* 826 * We call isc_app_start() here as some versions of FreeBSD's fork() 827 * destroys all the signal handling it sets up. 828 */ 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)); 833 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); 837 838 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 839 ISC_LOG_NOTICE, "built with %s", ns_g_configargs); 840 841 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 842 ISC_LOG_NOTICE, 843 "----------------------------------------------------"); 844 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 845 ISC_LOG_NOTICE, 846 "BIND 9 is maintained by Internet Systems Consortium,"); 847 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 848 ISC_LOG_NOTICE, 849 "Inc. (ISC), a non-profit 501(c)(3) public-benefit "); 850 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 851 ISC_LOG_NOTICE, 852 "corporation. Support and training for BIND 9 are "); 853 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 854 ISC_LOG_NOTICE, 855 "available at https://www.isc.org/support"); 856 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 857 ISC_LOG_NOTICE, 858 "----------------------------------------------------"); 859 860 dump_symboltable(); 861 862 /* 863 * Get the initial resource limits. 864 */ 865 (void)isc_resource_getlimit(isc_resource_stacksize, 866 &ns_g_initstacksize); 867 (void)isc_resource_getlimit(isc_resource_datasize, 868 &ns_g_initdatasize); 869 (void)isc_resource_getlimit(isc_resource_coresize, 870 &ns_g_initcoresize); 871 (void)isc_resource_getlimit(isc_resource_openfiles, 872 &ns_g_initopenfiles); 873 874 /* 875 * System resources cannot effectively be tuned on some systems. 876 * Raise the limit in such cases for safety. 877 */ 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); 889 } 890 891 /* 892 * If the named configuration filename is relative, prepend the current 893 * directory's name before possibly changing to another directory. 894 */ 895 if (! isc_file_isabsolute(ns_g_conffile)) { 896 result = isc_file_absolutepath(ns_g_conffile, 897 absolute_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; 904 } 905 906 /* 907 * Record the server's startup time. 908 */ 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)); 913 914 result = create_managers(); 915 if (result != ISC_R_SUCCESS) 916 ns_main_earlyfatal("create_managers() failed: %s", 917 isc_result_totext(result)); 918 919 ns_builtin_init(); 920 921 /* 922 * Add calls to register sdb drivers here. 923 */ 924 /* xxdb_init(); */ 925 926#ifdef ISC_DLZ_DLOPEN 927 /* 928 * Register the DLZ "dlopen" driver. 929 */ 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)); 934#endif 935 936#if CONTRIB_DLZ 937 /* 938 * Register any other contributed DLZ drivers. 939 */ 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)); 944#endif 945 946 ns_server_create(ns_g_mctx, &ns_g_server); 947} 948 949static void 950cleanup(void) { 951 destroy_managers(); 952 953 ns_server_destroy(&ns_g_server); 954 955 ns_builtin_deinit(); 956 957 /* 958 * Add calls to unregister sdb drivers here. 959 */ 960 /* xxdb_clear(); */ 961 962#ifdef CONTRIB_DLZ 963 /* 964 * Unregister contributed DLZ drivers. 965 */ 966 dlz_drivers_clear(); 967#endif 968#ifdef ISC_DLZ_DLOPEN 969 /* 970 * Unregister "dlopen" DLZ driver. 971 */ 972 dlz_dlopen_clear(); 973#endif 974 975 dns_name_destroy(); 976 977 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, 978 ISC_LOG_NOTICE, "exiting"); 979 ns_log_shutdown(); 980} 981 982static char *memstats = NULL; 983 984void 985ns_main_setmemstats(const char *filename) { 986 /* 987 * Caller has to ensure locking. 988 */ 989 990 if (memstats != NULL) { 991 free(memstats); 992 memstats = NULL; 993 } 994 if (filename == NULL) 995 return; 996 memstats = malloc(strlen(filename) + 1); 997 if (memstats) 998 strcpy(memstats, filename); 999} 1000 1001#ifdef HAVE_LIBSCF 1002/* 1003 * Get FMRI for the named process. 1004 */ 1005isc_result_t 1006ns_smf_get_instance(char **ins_name, int debug, isc_mem_t *mctx) { 1007 scf_handle_t *h = NULL; 1008 int namelen; 1009 char *instance; 1010 1011 REQUIRE(ins_name != NULL && *ins_name == NULL); 1012 1013 if ((h = scf_handle_create(SCF_VERSION)) == NULL) { 1014 if (debug) 1015 UNEXPECTED_ERROR(__FILE__, __LINE__, 1016 "scf_handle_create() failed: %s", 1017 scf_strerror(scf_error())); 1018 return (ISC_R_FAILURE); 1019 } 1020 1021 if (scf_handle_bind(h) == -1) { 1022 if (debug) 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); 1028 } 1029 1030 if ((namelen = scf_myname(h, NULL, 0)) == -1) { 1031 if (debug) 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); 1037 } 1038 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); 1046 } 1047 1048 if (scf_myname(h, instance, namelen + 1) == -1) { 1049 if (debug) 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); 1056 } 1057 1058 scf_handle_destroy(h); 1059 *ins_name = instance; 1060 return (ISC_R_SUCCESS); 1061} 1062#endif /* HAVE_LIBSCF */ 1063 1064/* main entry point, possibly hooked */ 1065 1066int 1067main(int argc, char *argv[]) { 1068 isc_result_t result; 1069#ifdef HAVE_LIBSCF 1070 char *instance = NULL; 1071#endif 1072 1073 /* 1074 * Record version in core image. 1075 * strings named.core | grep "named version:" 1076 */ 1077 strlcat(version, 1078#if defined(NO_VERSION_DATE) || !defined(__DATE__) 1079 "named version: BIND " VERSION " <" SRCID ">", 1080#else 1081 "named version: BIND " VERSION " <" SRCID "> (" __DATE__ ")", 1082#endif 1083 sizeof(version)); 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"); 1087 1088 if (strcmp(program_name, "lwresd") == 0) 1089 ns_g_lwresdonly = ISC_TRUE; 1090 1091 if (result != ISC_R_SUCCESS) 1092 ns_main_earlyfatal("failed to build internal symbol table"); 1093 1094 isc_assertion_setcallback(assertion_failed); 1095 isc_error_setfatal(library_fatal_error); 1096 isc_error_setunexpected(library_unexpected_error); 1097 1098 ns_os_init(program_name); 1099 1100 dns_result_register(); 1101 dst_result_register(); 1102 isccc_result_register(); 1103 1104 parse_command_line(argc, argv); 1105 1106 /* 1107 * Warn about common configuration error. 1108 */ 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); 1116 } 1117 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); 1123 1124 setup(); 1125 1126 /* 1127 * Start things running and then wait for a shutdown request 1128 * or reload. 1129 */ 1130 do { 1131 result = isc_app_run(); 1132 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)); 1139 /* 1140 * Force exit. 1141 */ 1142 result = ISC_R_SUCCESS; 1143 } 1144 } while (result != ISC_R_SUCCESS); 1145 1146#ifdef HAVE_LIBSCF 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", 1154 instance, 1155 scf_strerror(scf_error())); 1156 } 1157 if (instance != NULL) 1158 isc_mem_free(ns_g_mctx, instance); 1159 } 1160#endif /* HAVE_LIBSCF */ 1161 1162 cleanup(); 1163 1164 if (want_stats) { 1165 isc_mem_stats(ns_g_mctx, stdout); 1166 isc_mutex_stats(stdout); 1167 } 1168 1169 if (ns_g_memstatistics && memstats != NULL) { 1170 FILE *fp = 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); 1176 } 1177 } 1178 isc_mem_destroy(&ns_g_mctx); 1179 isc_mem_checkdestroyed(stderr); 1180 1181 ns_main_setmemstats(NULL); 1182 1183 isc_app_finish(); 1184 1185 ns_os_closedevnull(); 1186 1187 ns_os_shutdown(); 1188 1189 return (0); 1190} 1191