isc_result_t isc_httpdmgr_addurl2(isc_httpdmgr_t *httpdmgr, const char *url, isc_boolean_t isstatic, isc_httpdaction_t *func, void *arg) { isc_httpdurl_t *item; if (url == NULL) { httpdmgr->render_404 = func; return (ISC_R_SUCCESS); } item = isc_mem_get(httpdmgr->mctx, sizeof(isc_httpdurl_t)); if (item == NULL) return (ISC_R_NOMEMORY); item->url = isc_mem_strdup(httpdmgr->mctx, url); if (item->url == NULL) { isc_mem_put(httpdmgr->mctx, item, sizeof(isc_httpdurl_t)); return (ISC_R_NOMEMORY); } item->action = func; item->action_arg = arg; item->isstatic = isstatic; isc_time_now(&item->loadtime); ISC_LINK_INIT(item, link); ISC_LIST_APPEND(httpdmgr->urls, item, link); return (ISC_R_SUCCESS); }
ATF_TC_BODY(isc_time_parsehttptimestamp, tc) { isc_result_t result; isc_time_t t, x; char buf[ISC_FORMATHTTPTIMESTAMP_SIZE]; setenv("TZ", "PST8PDT", 1); result = isc_time_now(&t); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); isc_time_formathttptimestamp(&t, buf, sizeof(buf)); result = isc_time_parsehttptimestamp(buf, &x); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); ATF_REQUIRE_EQ(isc_time_seconds(&t), isc_time_seconds(&x)); }
isc_result_t isc_condition_waituntil(isc_condition_t *cond, isc_mutex_t *mutex, isc_time_t *t) { DWORD milliseconds; isc_uint64_t microseconds; isc_time_t now; if (isc_time_now(&now) != ISC_R_SUCCESS) { /* XXX */ return (ISC_R_UNEXPECTED); } microseconds = isc_time_microdiff(t, &now); if (microseconds > 0xFFFFFFFFi64 * 1000) milliseconds = 0xFFFFFFFF; else milliseconds = (DWORD)(microseconds / 1000); return (wait(cond, mutex, milliseconds)); }
void dns_badcache_flushtree(dns_badcache_t *bc, dns_name_t *name) { dns_bcentry_t *bad, *prev, *next; unsigned int i; int n; isc_time_t now; isc_result_t result; REQUIRE(VALID_BADCACHE(bc)); REQUIRE(name != NULL); LOCK(&bc->lock); result = isc_time_now(&now); if (result != ISC_R_SUCCESS) isc_time_settoepoch(&now); for (i = 0; bc->count > 0 && i < bc->size; i++) { prev = NULL; for (bad = bc->table[i]; bad != NULL; bad = next) { next = bad->next; n = isc_time_compare(&bad->expire, &now); if (n < 0 || dns_name_issubdomain(&bad->name, name)) { if (prev == NULL) bc->table[i] = bad->next; else prev->next = bad->next; isc_mem_put(bc->mctx, bad, sizeof(*bad) + bad->name.length); bc->count--; } else prev = bad; } } UNLOCK(&bc->lock); }
void dns_badcache_flushname(dns_badcache_t *bc, dns_name_t *name) { dns_bcentry_t *bad, *prev, *next; isc_result_t result; isc_time_t now; unsigned int i; REQUIRE(VALID_BADCACHE(bc)); REQUIRE(name != NULL); LOCK(&bc->lock); result = isc_time_now(&now); if (result != ISC_R_SUCCESS) isc_time_settoepoch(&now); i = dns_name_hash(name, ISC_FALSE) % bc->size; prev = NULL; for (bad = bc->table[i]; bad != NULL; bad = next) { int n; next = bad->next; n = isc_time_compare(&bad->expire, &now); if (n < 0 || dns_name_equal(name, &bad->name)) { if (prev == NULL) bc->table[i] = bad->next; else prev->next = bad->next; isc_mem_put(bc->mctx, bad, sizeof(*bad) + bad->name.length); bc->count--; } else prev = bad; } UNLOCK(&bc->lock); }
static void isc_httpd_recvdone(isc_task_t *task, isc_event_t *ev) { isc_region_t r; isc_result_t result; isc_httpd_t *httpd = ev->ev_arg; isc_socketevent_t *sev = (isc_socketevent_t *)ev; isc_httpdurl_t *url; isc_time_t now; char datebuf[32]; /* Only need 30, but safety first */ ENTER("recv"); INSIST(ISC_HTTPD_ISRECV(httpd)); if (sev->result != ISC_R_SUCCESS) { NOTICE("recv destroying client"); destroy_client(&httpd); goto out; } result = process_request(httpd, sev->n); if (result == ISC_R_NOTFOUND) { if (httpd->recvlen >= HTTP_RECVLEN - 1) { destroy_client(&httpd); goto out; } r.base = (unsigned char *)httpd->recvbuf + httpd->recvlen; r.length = HTTP_RECVLEN - httpd->recvlen - 1; /* check return code? */ (void)isc_socket_recv(httpd->sock, &r, 1, task, isc_httpd_recvdone, httpd); goto out; } else if (result != ISC_R_SUCCESS) { destroy_client(&httpd); goto out; } ISC_HTTPD_SETSEND(httpd); /* * XXXMLG Call function here. Provide an add-header function * which will append the common headers to a response we generate. */ isc_buffer_initnull(&httpd->bodybuffer); isc_time_now(&now); isc_time_formathttptimestamp(&now, datebuf, sizeof(datebuf)); url = ISC_LIST_HEAD(httpd->mgr->urls); while (url != NULL) { if (strcmp(httpd->url, url->url) == 0) break; url = ISC_LIST_NEXT(url, link); } if (url == NULL) result = httpd->mgr->render_404(httpd->url, NULL, httpd->querystring, NULL, NULL, &httpd->retcode, &httpd->retmsg, &httpd->mimetype, &httpd->bodybuffer, &httpd->freecb, &httpd->freecb_arg); else result = url->action(httpd->url, url, httpd->querystring, httpd->headers, url->action_arg, &httpd->retcode, &httpd->retmsg, &httpd->mimetype, &httpd->bodybuffer, &httpd->freecb, &httpd->freecb_arg); if (result != ISC_R_SUCCESS) { result = httpd->mgr->render_500(httpd->url, url, httpd->querystring, NULL, NULL, &httpd->retcode, &httpd->retmsg, &httpd->mimetype, &httpd->bodybuffer, &httpd->freecb, &httpd->freecb_arg); RUNTIME_CHECK(result == ISC_R_SUCCESS); } isc_httpd_response(httpd); isc_httpd_addheader(httpd, "Content-Type", httpd->mimetype); isc_httpd_addheader(httpd, "Date", datebuf); isc_httpd_addheader(httpd, "Expires", datebuf); if (url != NULL && url->isstatic) { char loadbuf[32]; isc_time_formathttptimestamp(&url->loadtime, loadbuf, sizeof(loadbuf)); isc_httpd_addheader(httpd, "Last-Modified", loadbuf); isc_httpd_addheader(httpd, "Cache-Control: public", NULL); } else { isc_httpd_addheader(httpd, "Last-Modified", datebuf); isc_httpd_addheader(httpd, "Pragma: no-cache", NULL); isc_httpd_addheader(httpd, "Cache-Control: no-cache", NULL); } isc_httpd_addheader(httpd, "Server: libisc", NULL); isc_httpd_addheaderuint(httpd, "Content-Length", isc_buffer_usedlength(&httpd->bodybuffer)); isc_httpd_endheaders(httpd); /* done */ ISC_LIST_APPEND(httpd->bufflist, &httpd->headerbuffer, link); /* * Link the data buffer into our send queue, should we have any data * rendered into it. If no data is present, we won't do anything * with the buffer. */ if (isc_buffer_length(&httpd->bodybuffer) > 0) ISC_LIST_APPEND(httpd->bufflist, &httpd->bodybuffer, link); /* check return code? */ (void)isc_socket_sendv(httpd->sock, &httpd->bufflist, task, isc_httpd_senddone, httpd); out: isc_event_free(&ev); EXIT("recv"); }
static void setup(void) { isc_result_t result; isc_resourcevalue_t old_openfiles; #ifdef HAVE_LIBSCF char *instance = NULL; #endif /* * Get the user and group information before changing the root * directory, so the administrator does not need to keep a copy * of the user and group databases in the chroot'ed environment. */ ns_os_inituserinfo(ns_g_username); /* * Initialize time conversion information */ ns_os_tzset(); ns_os_opendevnull(); #ifdef HAVE_LIBSCF /* Check if named is under smf control, before chroot. */ result = ns_smf_get_instance(&instance, 0, ns_g_mctx); /* We don't care about instance, just check if we got one. */ if (result == ISC_R_SUCCESS) ns_smf_got_instance = 1; else ns_smf_got_instance = 0; if (instance != NULL) isc_mem_free(ns_g_mctx, instance); #endif /* HAVE_LIBSCF */ #ifdef PATH_RANDOMDEV /* * Initialize system's random device as fallback entropy source * if running chroot'ed. */ if (ns_g_chrootdir != NULL) { result = isc_entropy_create(ns_g_mctx, &ns_g_fallbackentropy); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("isc_entropy_create() failed: %s", isc_result_totext(result)); result = isc_entropy_createfilesource(ns_g_fallbackentropy, PATH_RANDOMDEV); if (result != ISC_R_SUCCESS) { ns_main_earlywarning("could not open pre-chroot " "entropy source %s: %s", PATH_RANDOMDEV, isc_result_totext(result)); isc_entropy_detach(&ns_g_fallbackentropy); } } #endif #ifdef ISC_PLATFORM_USETHREADS /* * Check for the number of cpu's before ns_os_chroot(). */ ns_g_cpus_detected = isc_os_ncpus(); #endif ns_os_chroot(ns_g_chrootdir); /* * For operating systems which have a capability mechanism, now * is the time to switch to minimal privs and change our user id. * On traditional UNIX systems, this call will be a no-op, and we * will change the user ID after reading the config file the first * time. (We need to read the config file to know which possibly * privileged ports to bind() to.) */ ns_os_minprivs(); result = ns_log_init(ISC_TF(ns_g_username != NULL)); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("ns_log_init() failed: %s", isc_result_totext(result)); /* * Now is the time to daemonize (if we're not running in the * foreground). We waited until now because we wanted to get * a valid logging context setup. We cannot daemonize any later, * because calling create_managers() will create threads, which * would be lost after fork(). */ if (!ns_g_foreground) ns_os_daemonize(); /* * We call isc_app_start() here as some versions of FreeBSD's fork() * destroys all the signal handling it sets up. */ result = isc_app_start(); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("isc_app_start() failed: %s", isc_result_totext(result)); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "starting %s %s%s%s <id:%s>%s", ns_g_product, ns_g_version, *ns_g_description ? " " : "", ns_g_description, ns_g_srcid, saved_command_line); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "running on %s", ns_os_uname()); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "built with %s", ns_g_configargs); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "----------------------------------------------------"); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "BIND 9 is maintained by Internet Systems Consortium,"); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "Inc. (ISC), a non-profit 501(c)(3) public-benefit "); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "corporation. Support and training for BIND 9 are "); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "available at https://www.isc.org/support"); isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "----------------------------------------------------"); dump_symboltable(); /* * Get the initial resource limits. */ (void)isc_resource_getlimit(isc_resource_stacksize, &ns_g_initstacksize); (void)isc_resource_getlimit(isc_resource_datasize, &ns_g_initdatasize); (void)isc_resource_getlimit(isc_resource_coresize, &ns_g_initcoresize); (void)isc_resource_getlimit(isc_resource_openfiles, &ns_g_initopenfiles); /* * System resources cannot effectively be tuned on some systems. * Raise the limit in such cases for safety. */ old_openfiles = ns_g_initopenfiles; ns_os_adjustnofile(); (void)isc_resource_getlimit(isc_resource_openfiles, &ns_g_initopenfiles); if (old_openfiles != ns_g_initopenfiles) { isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN, ISC_LOG_NOTICE, "adjusted limit on open files from " "%" ISC_PRINT_QUADFORMAT "u to " "%" ISC_PRINT_QUADFORMAT "u", old_openfiles, ns_g_initopenfiles); } /* * If the named configuration filename is relative, prepend the current * directory's name before possibly changing to another directory. */ if (! isc_file_isabsolute(ns_g_conffile)) { result = isc_file_absolutepath(ns_g_conffile, absolute_conffile, sizeof(absolute_conffile)); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("could not construct absolute path " "of configuration file: %s", isc_result_totext(result)); ns_g_conffile = absolute_conffile; } /* * Record the server's startup time. */ result = isc_time_now(&ns_g_boottime); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("isc_time_now() failed: %s", isc_result_totext(result)); result = create_managers(); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("create_managers() failed: %s", isc_result_totext(result)); ns_builtin_init(); /* * Add calls to register sdb drivers here. */ /* xxdb_init(); */ #ifdef ISC_DLZ_DLOPEN /* * Register the DLZ "dlopen" driver. */ result = dlz_dlopen_init(ns_g_mctx); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("dlz_dlopen_init() failed: %s", isc_result_totext(result)); #endif #if CONTRIB_DLZ /* * Register any other contributed DLZ drivers. */ result = dlz_drivers_init(); if (result != ISC_R_SUCCESS) ns_main_earlyfatal("dlz_drivers_init() failed: %s", isc_result_totext(result)); #endif ns_server_create(ns_g_mctx, &ns_g_server); #ifdef HAVE_LIBSECCOMP setup_seccomp(); #endif /* HAVE_LIBSECCOMP */ }
void dns_badcache_add(dns_badcache_t *bc, dns_name_t *name, dns_rdatatype_t type, isc_boolean_t update, isc_uint32_t flags, isc_time_t *expire) { isc_result_t result; unsigned int i, hashval; dns_bcentry_t *bad, *prev, *next; isc_time_t now; REQUIRE(VALID_BADCACHE(bc)); REQUIRE(name != NULL); REQUIRE(expire != NULL); LOCK(&bc->lock); result = isc_time_now(&now); if (result != ISC_R_SUCCESS) isc_time_settoepoch(&now); hashval = dns_name_hash(name, ISC_FALSE); i = hashval % bc->size; prev = NULL; for (bad = bc->table[i]; bad != NULL; bad = next) { next = bad->next; if (bad->type == type && dns_name_equal(name, &bad->name)) { if (update) { bad->expire = *expire; bad->flags = flags; } break; } if (isc_time_compare(&bad->expire, &now) < 0) { if (prev == NULL) bc->table[i] = bad->next; else prev->next = bad->next; isc_mem_put(bc->mctx, bad, sizeof(*bad) + bad->name.length); bc->count--; } else prev = bad; } if (bad == NULL) { isc_buffer_t buffer; bad = isc_mem_get(bc->mctx, sizeof(*bad) + name->length); if (bad == NULL) goto cleanup; bad->type = type; bad->hashval = hashval; bad->expire = *expire; bad->flags = flags; isc_buffer_init(&buffer, bad + 1, name->length); dns_name_init(&bad->name, NULL); dns_name_copy(name, &bad->name, &buffer); bad->next = bc->table[i]; bc->table[i] = bad; bc->count++; if (bc->count > bc->size * 8) badcache_resize(bc, &now, ISC_TRUE); if (bc->count < bc->size * 2 && bc->size > bc->minsize) badcache_resize(bc, &now, ISC_FALSE); } else bad->expire = *expire; cleanup: UNLOCK(&bc->lock); }
static void tx_te(isc_task_t *task, isc_event_t *event) { isc_result_t isc_result; isc_time_t now; isc_time_t base; isc_time_t ulim; isc_time_t llim; isc_interval_t interval; isc_eventtype_t expected_event_type; ++Tx_eventcnt; t_info("tick %d\n", Tx_eventcnt); expected_event_type = ISC_TIMEREVENT_LIFE; if ((isc_timertype_t) event->ev_arg == isc_timertype_ticker) expected_event_type = ISC_TIMEREVENT_TICK; if (event->ev_type != expected_event_type) { t_info("expected event type %d, got %d\n", expected_event_type, (int) event->ev_type); ++Tx_nfails; } isc_result = isc_time_now(&now); if (isc_result == ISC_R_SUCCESS) { interval.seconds = Tx_seconds; interval.nanoseconds = Tx_nanoseconds; isc_result = isc_time_add(&Tx_lasttime, &interval, &base); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_add failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } else { t_info("isc_time_now failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } if (isc_result == ISC_R_SUCCESS) { interval.seconds = Tx_FUDGE_SECONDS; interval.nanoseconds = Tx_FUDGE_NANOSECONDS; isc_result = isc_time_add(&base, &interval, &ulim); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_add failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } if (isc_result == ISC_R_SUCCESS) { isc_result = isc_time_subtract(&base, &interval, &llim); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_subtract failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } if (isc_result == ISC_R_SUCCESS) { if (isc_time_compare(&llim, &now) > 0) { t_info("timer range error: early by " "%lu microseconds\n", (unsigned long)isc_time_microdiff(&base, &now)); ++Tx_nfails; } else if (isc_time_compare(&ulim, &now) < 0) { t_info("timer range error: late by " "%lu microseconds\n", (unsigned long)isc_time_microdiff(&now, &base)); ++Tx_nfails; } Tx_lasttime = now; } if (Tx_eventcnt == Tx_nevents) { isc_result = isc_time_now(&Tx_endtime); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_now failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } isc_timer_detach(&Tx_timer); isc_task_shutdown(task); } isc_event_free(&event); }
static void t4_te(isc_task_t *task, isc_event_t *event) { isc_result_t isc_result; isc_time_t now; isc_time_t base; isc_time_t ulim; isc_time_t llim; isc_time_t expires; isc_interval_t interval; ++Tx_eventcnt; t_info("tick %d\n", Tx_eventcnt); /* * Check expired time. */ isc_result = isc_time_now(&now); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_now failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } if (isc_result == ISC_R_SUCCESS) { interval.seconds = Tx_seconds; interval.nanoseconds = Tx_nanoseconds; isc_result = isc_time_add(&Tx_lasttime, &interval, &base); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_add failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } if (isc_result == ISC_R_SUCCESS) { interval.seconds = Tx_FUDGE_SECONDS; interval.nanoseconds = Tx_FUDGE_NANOSECONDS; isc_result = isc_time_add(&base, &interval, &ulim); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_add failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } if (isc_result == ISC_R_SUCCESS) { isc_result = isc_time_subtract(&base, &interval, &llim); if (isc_result != ISC_R_SUCCESS) { t_info("isc_time_subtract failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } if (isc_result == ISC_R_SUCCESS) { if (isc_time_compare(&llim, &now) > 0) { t_info("timer range error: early by " "%lu microseconds\n", (unsigned long)isc_time_microdiff(&base, &now)); ++Tx_nfails; } else if (isc_time_compare(&ulim, &now) < 0) { t_info("timer range error: late by " "%lu microseconds\n", (unsigned long)isc_time_microdiff(&now, &base)); ++Tx_nfails; } Tx_lasttime = now; } if (Tx_eventcnt < 3) { if (event->ev_type != ISC_TIMEREVENT_TICK) { t_info("received event type %d, expected type %d\n", event->ev_type, ISC_TIMEREVENT_IDLE); ++Tx_nfails; } if (Tx_eventcnt == 2) { isc_interval_set(&interval, T4_SECONDS, T4_NANOSECONDS); isc_result = isc_time_nowplusinterval(&expires, &interval); if (isc_result == ISC_R_SUCCESS) { isc_interval_set(&interval, 0, 0); isc_result = isc_timer_reset(Tx_timer, isc_timertype_once, &expires, &interval, ISC_FALSE); if (isc_result != ISC_R_SUCCESS) { t_info("isc_timer_reset failed %s\n", isc_result_totext(isc_result)); ++Tx_nfails; } } else { t_info("isc_time_nowplusinterval failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } } else { if (event->ev_type != ISC_TIMEREVENT_LIFE) { t_info("received event type %d, expected type %d\n", event->ev_type, ISC_TIMEREVENT_IDLE); ++Tx_nfails; } isc_timer_detach(&Tx_timer); isc_task_shutdown(task); } isc_event_free(&event); }
static void t_timers_x(isc_timertype_t timertype, isc_time_t *expires, isc_interval_t *interval, void (*action)(isc_task_t *, isc_event_t *)) { char *p; isc_mem_t *mctx; isc_taskmgr_t *tmgr; isc_task_t *task; unsigned int workers; isc_result_t isc_result; isc_timermgr_t *timermgr; Tx_eventcnt = 0; isc_time_settoepoch(&Tx_endtime); workers = 2; p = t_getenv("ISC_TASK_WORKERS"); if (p != NULL) workers = atoi(p); mctx = NULL; isc_result = isc_mem_create(0, 0, &mctx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mem_create failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; return; } isc_result = isc_mutex_init(&Tx_mx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mutex_init failed %s\n", isc_result_totext(isc_result)); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } isc_result = isc_condition_init(&Tx_cv); if (isc_result != ISC_R_SUCCESS) { t_info("isc_condition_init failed %s\n", isc_result_totext(isc_result)); DESTROYLOCK(&Tx_mx); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } tmgr = NULL; isc_result = isc_taskmgr_create(mctx, workers, 0, &tmgr); if (isc_result != ISC_R_SUCCESS) { t_info("isc_taskmgr_create failed %s\n", isc_result_totext(isc_result)); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } timermgr = NULL; isc_result = isc_timermgr_create(mctx, &timermgr); if (isc_result != ISC_R_SUCCESS) { t_info("isc_timermgr_create failed %s\n", isc_result_totext(isc_result)); isc_taskmgr_destroy(&tmgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } isc_result = isc_mutex_lock(&Tx_mx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mutex_lock failed %s\n", isc_result_totext(isc_result)); isc_timermgr_destroy(&timermgr); isc_taskmgr_destroy(&tmgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } task = NULL; isc_result = isc_task_create(tmgr, 0, &task); if (isc_result != ISC_R_SUCCESS) { t_info("isc_task_create failed %s\n", isc_result_totext(isc_result)); isc_timermgr_destroy(&timermgr); isc_taskmgr_destroy(&tmgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } isc_result = isc_task_onshutdown(task, tx_sde, NULL); if (isc_result != ISC_R_SUCCESS) { t_info("isc_task_onshutdown failed %s\n", isc_result_totext(isc_result)); isc_timermgr_destroy(&timermgr); isc_task_destroy(&task); isc_taskmgr_destroy(&tmgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } isc_result = isc_time_now(&Tx_lasttime); if (isc_result != ISC_R_SUCCESS) { isc_timermgr_destroy(&timermgr); isc_task_destroy(&task); isc_taskmgr_destroy(&tmgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } Tx_timer = NULL; isc_result = isc_timer_create(timermgr, timertype, expires, interval, task, action, (void *)timertype, &Tx_timer); if (isc_result != ISC_R_SUCCESS) { isc_timermgr_destroy(&timermgr); isc_task_destroy(&task); isc_taskmgr_destroy(&tmgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); ++Tx_nprobs; return; } /* * Wait for shutdown processing to complete. */ while (Tx_eventcnt != Tx_nevents) { isc_result = isc_condition_wait(&Tx_cv, &Tx_mx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_condition_waituntil failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } } isc_result = isc_mutex_unlock(&Tx_mx); if (isc_result != ISC_R_SUCCESS) { t_info("isc_mutex_unlock failed %s\n", isc_result_totext(isc_result)); ++Tx_nprobs; } isc_task_detach(&task); isc_taskmgr_destroy(&tmgr); isc_timermgr_destroy(&timermgr); DESTROYLOCK(&Tx_mx); (void) isc_condition_destroy(&Tx_cv); isc_mem_destroy(&mctx); }
static isc_result_t generatexml(ns_server_t *server, int *buflen, xmlChar **buf) { char boottime[sizeof "yyyy-mm-ddThh:mm:ssZ"]; char nowstr[sizeof "yyyy-mm-ddThh:mm:ssZ"]; isc_time_t now; xmlTextWriterPtr writer = NULL; xmlDocPtr doc = NULL; int xmlrc; dns_view_t *view; stats_dumparg_t dumparg; dns_stats_t *cachestats; isc_uint64_t nsstat_values[dns_nsstatscounter_max]; isc_uint64_t resstat_values[dns_resstatscounter_max]; isc_uint64_t zonestat_values[dns_zonestatscounter_max]; isc_uint64_t sockstat_values[isc_sockstatscounter_max]; isc_result_t result; isc_time_now(&now); isc_time_formatISO8601(&ns_g_boottime, boottime, sizeof boottime); isc_time_formatISO8601(&now, nowstr, sizeof nowstr); writer = xmlNewTextWriterDoc(&doc, 0); if (writer == NULL) goto error; TRY0(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL)); TRY0(xmlTextWriterWritePI(writer, ISC_XMLCHAR "xml-stylesheet", ISC_XMLCHAR "type=\"text/xsl\" href=\"/bind9.xsl\"")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "isc")); TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version", ISC_XMLCHAR "1.0")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "bind")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "statistics")); TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version", ISC_XMLCHAR "2.2")); /* Set common fields for statistics dump */ dumparg.type = statsformat_xml; dumparg.arg = writer; /* * Start by rendering the views we know of here. For each view we * know of, call its rendering function. */ view = ISC_LIST_HEAD(server->viewlist); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "views")); while (view != NULL) { TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "view")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "name")); TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR view->name)); TRY0(xmlTextWriterEndElement(writer)); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "zones")); result = dns_zt_apply(view->zonetable, ISC_TRUE, zone_xmlrender, writer); if (result != ISC_R_SUCCESS) goto error; TRY0(xmlTextWriterEndElement(writer)); if (view->resquerystats != NULL) { dumparg.result = ISC_R_SUCCESS; dns_rdatatypestats_dump(view->resquerystats, rdtypestat_dump, &dumparg, 0); if (dumparg.result != ISC_R_SUCCESS) goto error; } if (view->resstats != NULL) { result = dump_counters(view->resstats, statsformat_xml, writer, "resstat", resstats_xmldesc, dns_resstatscounter_max, resstats_index, resstat_values, ISC_STATSDUMP_VERBOSE); if (result != ISC_R_SUCCESS) goto error; } cachestats = dns_db_getrrsetstats(view->cachedb); if (cachestats != NULL) { TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "cache")); TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name", ISC_XMLCHAR dns_cache_getname(view->cache))); dumparg.result = ISC_R_SUCCESS; dns_rdatasetstats_dump(cachestats, rdatasetstats_dump, &dumparg, 0); if (dumparg.result != ISC_R_SUCCESS) goto error; TRY0(xmlTextWriterEndElement(writer)); /* cache */ } TRY0(xmlTextWriterEndElement(writer)); /* view */ view = ISC_LIST_NEXT(view, link); } TRY0(xmlTextWriterEndElement(writer)); /* views */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "socketmgr")); isc_socketmgr_renderxml(ns_g_socketmgr, writer); TRY0(xmlTextWriterEndElement(writer)); /* socketmgr */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "taskmgr")); isc_taskmgr_renderxml(ns_g_taskmgr, writer); TRY0(xmlTextWriterEndElement(writer)); /* taskmgr */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "server")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "boot-time")); TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR boottime)); TRY0(xmlTextWriterEndElement(writer)); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "current-time")); TRY0(xmlTextWriterWriteString(writer, ISC_XMLCHAR nowstr)); TRY0(xmlTextWriterEndElement(writer)); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "requests")); dumparg.result = ISC_R_SUCCESS; dns_opcodestats_dump(server->opcodestats, opcodestat_dump, &dumparg, 0); if (dumparg.result != ISC_R_SUCCESS) goto error; TRY0(xmlTextWriterEndElement(writer)); /* requests */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "queries-in")); dumparg.result = ISC_R_SUCCESS; dns_rdatatypestats_dump(server->rcvquerystats, rdtypestat_dump, &dumparg, 0); if (dumparg.result != ISC_R_SUCCESS) goto error; TRY0(xmlTextWriterEndElement(writer)); /* queries-in */ result = dump_counters(server->nsstats, statsformat_xml, writer, "nsstat", nsstats_xmldesc, dns_nsstatscounter_max, nsstats_index, nsstat_values, ISC_STATSDUMP_VERBOSE); if (result != ISC_R_SUCCESS) goto error; result = dump_counters(server->zonestats, statsformat_xml, writer, "zonestat", zonestats_xmldesc, dns_zonestatscounter_max, zonestats_index, zonestat_values, ISC_STATSDUMP_VERBOSE); if (result != ISC_R_SUCCESS) goto error; /* * Most of the common resolver statistics entries are 0, so we don't * use the verbose dump here. */ result = dump_counters(server->resolverstats, statsformat_xml, writer, "resstat", resstats_xmldesc, dns_resstatscounter_max, resstats_index, resstat_values, 0); if (result != ISC_R_SUCCESS) goto error; result = dump_counters(server->sockstats, statsformat_xml, writer, "sockstat", sockstats_xmldesc, isc_sockstatscounter_max, sockstats_index, sockstat_values, ISC_STATSDUMP_VERBOSE); if (result != ISC_R_SUCCESS) goto error; TRY0(xmlTextWriterEndElement(writer)); /* server */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "memory")); isc_mem_renderxml(writer); TRY0(xmlTextWriterEndElement(writer)); /* memory */ TRY0(xmlTextWriterEndElement(writer)); /* statistics */ TRY0(xmlTextWriterEndElement(writer)); /* bind */ TRY0(xmlTextWriterEndElement(writer)); /* isc */ TRY0(xmlTextWriterEndDocument(writer)); xmlFreeTextWriter(writer); xmlDocDumpFormatMemoryEnc(doc, buf, buflen, "UTF-8", 1); xmlFreeDoc(doc); return (ISC_R_SUCCESS); error: if (writer != NULL) xmlFreeTextWriter(writer); if (doc != NULL) xmlFreeDoc(doc); return (ISC_R_FAILURE); }
ATF_TC_BODY(benchmark, tc) { isc_result_t result; char namestr[sizeof("name18446744073709551616.example.org.")]; unsigned int r; dns_rbt_t *mytree; dns_rbtnode_t *node; unsigned int i; unsigned int maxvalue = 1000000; isc_time_t ts1, ts2; double t; unsigned int nthreads; isc_thread_t threads[32]; UNUSED(tc); srandom(time(NULL)); debug_mem_record = ISC_FALSE; result = dns_test_begin(NULL, ISC_TRUE); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); fnames = (dns_fixedname_t *) malloc(4000000 * sizeof(dns_fixedname_t)); names = (dns_name_t **) malloc(4000000 * sizeof(dns_name_t *)); values = (int *) malloc(4000000 * sizeof(int)); for (i = 0; i < 4000000; i++) { r = ((unsigned long) random()) % maxvalue; snprintf(namestr, sizeof(namestr), "name%u.example.org.", r); build_name_from_str(namestr, &fnames[i]); names[i] = dns_fixedname_name(&fnames[i]); values[i] = r; } /* Create a tree. */ mytree = NULL; result = dns_rbt_create(mctx, NULL, NULL, &mytree); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); /* Insert test data into the tree. */ for (i = 0; i < maxvalue; i++) { snprintf(namestr, sizeof(namestr), "name%u.example.org.", i); node = NULL; result = insert_helper(mytree, namestr, &node); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); node->data = (void *) (intptr_t) i; } result = isc_time_now(&ts1); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); nthreads = ISC_MIN(isc_os_ncpus(), 32); nthreads = ISC_MAX(nthreads, 1); for (i = 0; i < nthreads; i++) { result = isc_thread_create(find_thread, mytree, &threads[i]); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); } for (i = 0; i < nthreads; i++) { result = isc_thread_join(threads[i], NULL); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); } result = isc_time_now(&ts2); ATF_REQUIRE_EQ(result, ISC_R_SUCCESS); t = isc_time_microdiff(&ts2, &ts1); printf("%u findnode calls, %f seconds, %f calls/second\n", nthreads * 8 * 4000000, t / 1000000.0, (nthreads * 8 * 4000000) / (t / 1000000.0)); free(values); free(names); free(fnames); dns_rbt_destroy(&mytree); dns_test_end(); }
isc_result_t cached_ldap_rdatalist_get(isc_mem_t *mctx, ldap_cache_t *cache, ldap_db_t *ldap_db, dns_name_t *name, dns_name_t *origin, ldapdb_rdatalist_t *rdatalist) { isc_result_t result; ldapdb_rdatalist_t rdlist; cache_node_t *node = NULL; int in_cache = 0; int is_locked = 0; REQUIRE(cache != NULL); if (cache->rbt == NULL) return ldapdb_rdatalist_get(mctx, ldap_db, name, origin, rdatalist); CONTROLED_LOCK(&cache->mutex); result = dns_rbt_findname(cache->rbt, name, 0, NULL, (void *)&node); if (result == ISC_R_SUCCESS) { isc_time_t now; CHECK(isc_time_now(&now)); /* Check if the record is still valid. */ if (isc_time_compare(&now, &node->valid_until) > 0) { CHECK(dns_rbt_deletename(cache->rbt, name, ISC_FALSE)); in_cache = 0; } else { rdlist = node->rdatalist; in_cache = 1; } } else if (result != ISC_R_NOTFOUND && result != DNS_R_PARTIALMATCH) { goto cleanup; } CONTROLED_UNLOCK(&cache->mutex); if (!in_cache) { INIT_LIST(rdlist); result = ldapdb_rdatalist_get(mctx, ldap_db, name, origin, &rdlist); /* TODO: Cache entries that are not found. */ if (result != ISC_R_SUCCESS) goto cleanup; CONTROLED_LOCK(&cache->mutex); /* Check again to make sure. */ node = NULL; result = dns_rbt_findname(cache->rbt, name, 0, NULL, (void *)&node); if (result == ISC_R_NOTFOUND || result == DNS_R_PARTIALMATCH) { node = NULL; CHECK(cache_node_create(cache, rdlist, &node)); CHECK(dns_rbt_addname(cache->rbt, name, (void *)node)); } CONTROLED_UNLOCK(&cache->mutex); } CHECK(ldap_rdatalist_copy(mctx, rdlist, rdatalist)); if (EMPTY(*rdatalist)) result = ISC_R_NOTFOUND; cleanup: CONTROLED_UNLOCK(&cache->mutex); return result; }
static void generatexml(ns_server_t *server, int *buflen, xmlChar **buf) { char boottime[sizeof "yyyy-mm-ddThh:mm:ssZ"]; char nowstr[sizeof "yyyy-mm-ddThh:mm:ssZ"]; isc_time_t now; xmlTextWriterPtr writer; xmlDocPtr doc; int xmlrc; dns_view_t *view; stats_dumparg_t dumparg; dns_stats_t *cachestats; isc_time_now(&now); isc_time_formatISO8601(&ns_g_boottime, boottime, sizeof boottime); isc_time_formatISO8601(&now, nowstr, sizeof nowstr); writer = xmlNewTextWriterDoc(&doc, 0); TRY0(xmlTextWriterStartDocument(writer, NULL, "UTF-8", NULL)); TRY0(xmlTextWriterWritePI(writer, ISC_XMLCHAR "xml-stylesheet", ISC_XMLCHAR "type=\"text/xsl\" href=\"/bind9.xsl\"")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "isc")); TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version", ISC_XMLCHAR "1.0")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "bind")); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "statistics")); TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "version", ISC_XMLCHAR "1.0")); /* Set common fields for statistics dump */ dumparg.type = statsformat_xml; dumparg.arg = writer; /* * Start by rendering the views we know of here. For each view we * know of, call its rendering function. */ view = ISC_LIST_HEAD(server->viewlist); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "views")); while (view != NULL) { xmlTextWriterStartElement(writer, ISC_XMLCHAR "view"); xmlTextWriterStartElement(writer, ISC_XMLCHAR "name"); xmlTextWriterWriteString(writer, ISC_XMLCHAR view->name); xmlTextWriterEndElement(writer); xmlTextWriterStartElement(writer, ISC_XMLCHAR "zones"); dns_zt_apply(view->zonetable, ISC_FALSE, zone_xmlrender, writer); xmlTextWriterEndElement(writer); if (view->resquerystats != NULL) { dns_rdatatypestats_dump(view->resquerystats, rdtypestat_dump, &dumparg, 0); } if (view->resstats != NULL) { xmlTextWriterStartElement(writer, ISC_XMLCHAR "resstats"); dumparg.ncounters = dns_resstatscounter_max; dumparg.desc = resstats_xmldesc; /* auto-generated */ dns_generalstats_dump(view->resstats, generalstat_dump, &dumparg, DNS_STATSDUMP_VERBOSE); xmlTextWriterEndElement(writer); /* resstats */ } cachestats = dns_db_getrrsetstats(view->cachedb); if (cachestats != NULL) { xmlTextWriterStartElement(writer, ISC_XMLCHAR "cache"); dns_rdatasetstats_dump(cachestats, rdatasetstats_dump, &dumparg, 0); xmlTextWriterEndElement(writer); /* cache */ } xmlTextWriterEndElement(writer); /* view */ view = ISC_LIST_NEXT(view, link); } TRY0(xmlTextWriterEndElement(writer)); /* views */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "socketmgr")); isc_socketmgr_renderxml(ns_g_socketmgr, writer); TRY0(xmlTextWriterEndElement(writer)); /* socketmgr */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "taskmgr")); isc_taskmgr_renderxml(ns_g_taskmgr, writer); TRY0(xmlTextWriterEndElement(writer)); /* taskmgr */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "server")); xmlTextWriterStartElement(writer, ISC_XMLCHAR "boot-time"); xmlTextWriterWriteString(writer, ISC_XMLCHAR boottime); xmlTextWriterEndElement(writer); xmlTextWriterStartElement(writer, ISC_XMLCHAR "current-time"); xmlTextWriterWriteString(writer, ISC_XMLCHAR nowstr); xmlTextWriterEndElement(writer); TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "requests")); dns_opcodestats_dump(server->opcodestats, opcodestat_dump, &dumparg, 0); xmlTextWriterEndElement(writer); /* requests */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "queries-in")); dns_rdatatypestats_dump(server->rcvquerystats, rdtypestat_dump, &dumparg, 0); xmlTextWriterEndElement(writer); /* queries-in */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "nsstats")); dumparg.desc = nsstats_xmldesc; /* auto-generated in bind9.xsl.h */ dumparg.ncounters = dns_nsstatscounter_max; dns_generalstats_dump(server->nsstats, generalstat_dump, &dumparg, DNS_STATSDUMP_VERBOSE); xmlTextWriterEndElement(writer); /* nsstats */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "zonestats")); dumparg.desc = zonestats_xmldesc; /* auto-generated in bind9.xsl.h */ dumparg.ncounters = dns_zonestatscounter_max; dns_generalstats_dump(server->zonestats, generalstat_dump, &dumparg, DNS_STATSDUMP_VERBOSE); xmlTextWriterEndElement(writer); /* zonestats */ xmlTextWriterStartElement(writer, ISC_XMLCHAR "resstats"); dumparg.ncounters = dns_resstatscounter_max; dumparg.desc = resstats_xmldesc; dns_generalstats_dump(server->resolverstats, generalstat_dump, &dumparg, DNS_STATSDUMP_VERBOSE); xmlTextWriterEndElement(writer); /* resstats */ xmlTextWriterEndElement(writer); /* server */ TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "memory")); isc_mem_renderxml(writer); TRY0(xmlTextWriterEndElement(writer)); /* memory */ TRY0(xmlTextWriterEndElement(writer)); /* statistics */ TRY0(xmlTextWriterEndElement(writer)); /* bind */ TRY0(xmlTextWriterEndElement(writer)); /* isc */ TRY0(xmlTextWriterEndDocument(writer)); xmlFreeTextWriter(writer); xmlDocDumpFormatMemoryEnc(doc, buf, buflen, "UTF-8", 1); xmlFreeDoc(doc); }