1275970Scy/* 2275970Scy * Copyright (C) 2011, 2012 Internet Systems Consortium, Inc. ("ISC") 3275970Scy * 4275970Scy * Permission to use, copy, modify, and/or distribute this software for any 5275970Scy * purpose with or without fee is hereby granted, provided that the above 6275970Scy * copyright notice and this permission notice appear in all copies. 7275970Scy * 8275970Scy * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 9275970Scy * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10275970Scy * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 11275970Scy * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12275970Scy * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13275970Scy * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14275970Scy * PERFORMANCE OF THIS SOFTWARE. 15275970Scy */ 16275970Scy 17275970Scy/* $Id$ */ 18275970Scy 19275970Scy/*! \file */ 20275970Scy 21275970Scy#include <config.h> 22275970Scy 23275970Scy#include <time.h> 24275970Scy 25275970Scy#include <isc/app.h> 26275970Scy#include <isc/buffer.h> 27275970Scy#include <isc/entropy.h> 28275970Scy#include <isc/hash.h> 29275970Scy#include <isc/mem.h> 30275970Scy#include <isc/os.h> 31275970Scy#include <isc/socket.h> 32275970Scy#include <isc/string.h> 33275970Scy#include <isc/task.h> 34275970Scy#include <isc/timer.h> 35275970Scy#include <isc/util.h> 36275970Scy 37275970Scy#include "isctest.h" 38275970Scy 39275970Scyisc_mem_t *mctx = NULL; 40275970Scyisc_entropy_t *ectx = NULL; 41275970Scyisc_log_t *lctx = NULL; 42275970Scyisc_taskmgr_t *taskmgr = NULL; 43275970Scyisc_timermgr_t *timermgr = NULL; 44275970Scyisc_socketmgr_t *socketmgr = NULL; 45275970Scyint ncpus; 46275970Scy 47275970Scystatic isc_boolean_t hash_active = ISC_FALSE; 48275970Scy 49275970Scy/* 50275970Scy * Logging categories: this needs to match the list in bin/named/log.c. 51275970Scy */ 52275970Scystatic isc_logcategory_t categories[] = { 53275970Scy { "", 0 }, 54275970Scy { "client", 0 }, 55275970Scy { "network", 0 }, 56275970Scy { "update", 0 }, 57275970Scy { "queries", 0 }, 58275970Scy { "unmatched", 0 }, 59275970Scy { "update-security", 0 }, 60275970Scy { "query-errors", 0 }, 61275970Scy { NULL, 0 } 62275970Scy}; 63275970Scy 64275970Scystatic void 65275970Scycleanup_managers() { 66275970Scy if (socketmgr != NULL) 67275970Scy isc_socketmgr_destroy(&socketmgr); 68275970Scy if (taskmgr != NULL) 69275970Scy isc_taskmgr_destroy(&taskmgr); 70275970Scy if (timermgr != NULL) 71275970Scy isc_timermgr_destroy(&timermgr); 72275970Scy} 73275970Scy 74275970Scystatic isc_result_t 75275970Scycreate_managers() { 76275970Scy isc_result_t result; 77275970Scy#ifdef ISC_PLATFORM_USETHREADS 78275970Scy ncpus = isc_os_ncpus(); 79275970Scy#else 80275970Scy ncpus = 1; 81275970Scy#endif 82275970Scy 83275970Scy CHECK(isc_taskmgr_create(mctx, ncpus, 0, &taskmgr)); 84275970Scy CHECK(isc_timermgr_create(mctx, &timermgr)); 85275970Scy CHECK(isc_socketmgr_create(mctx, &socketmgr)); 86275970Scy return (ISC_R_SUCCESS); 87275970Scy 88275970Scy cleanup: 89275970Scy cleanup_managers(); 90275970Scy return (result); 91275970Scy} 92275970Scy 93275970Scyisc_result_t 94275970Scyisc_test_begin(FILE *logfile, isc_boolean_t start_managers) { 95275970Scy isc_result_t result; 96275970Scy 97275970Scy isc_mem_debugging |= ISC_MEM_DEBUGRECORD; 98275970Scy CHECK(isc_mem_create(0, 0, &mctx)); 99275970Scy CHECK(isc_entropy_create(mctx, &ectx)); 100275970Scy 101275970Scy CHECK(isc_hash_create(mctx, ectx, 255)); 102275970Scy hash_active = ISC_TRUE; 103275970Scy 104275970Scy if (logfile != NULL) { 105275970Scy isc_logdestination_t destination; 106275970Scy isc_logconfig_t *logconfig = NULL; 107275970Scy 108275970Scy CHECK(isc_log_create(mctx, &lctx, &logconfig)); 109275970Scy isc_log_registercategories(lctx, categories); 110275970Scy isc_log_setcontext(lctx); 111275970Scy 112275970Scy destination.file.stream = logfile; 113275970Scy destination.file.name = NULL; 114275970Scy destination.file.versions = ISC_LOG_ROLLNEVER; 115275970Scy destination.file.maximum_size = 0; 116275970Scy CHECK(isc_log_createchannel(logconfig, "stderr", 117275970Scy ISC_LOG_TOFILEDESC, 118275970Scy ISC_LOG_DYNAMIC, 119275970Scy &destination, 0)); 120275970Scy CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL)); 121275970Scy } 122275970Scy 123275970Scy#ifdef ISC_PLATFORM_USETHREADS 124275970Scy ncpus = isc_os_ncpus(); 125275970Scy#else 126275970Scy ncpus = 1; 127275970Scy#endif 128275970Scy 129275970Scy if (start_managers) 130275970Scy CHECK(create_managers()); 131275970Scy 132275970Scy return (ISC_R_SUCCESS); 133275970Scy 134275970Scy cleanup: 135275970Scy isc_test_end(); 136275970Scy return (result); 137275970Scy} 138275970Scy 139275970Scyvoid 140275970Scyisc_test_end() { 141275970Scy if (taskmgr != NULL) 142275970Scy isc_taskmgr_destroy(&taskmgr); 143275970Scy if (lctx != NULL) 144275970Scy isc_log_destroy(&lctx); 145275970Scy if (hash_active) { 146275970Scy isc_hash_destroy(); 147275970Scy hash_active = ISC_FALSE; 148275970Scy } 149275970Scy if (ectx != NULL) 150275970Scy isc_entropy_detach(&ectx); 151275970Scy 152275970Scy cleanup_managers(); 153275970Scy 154275970Scy if (mctx != NULL) 155275970Scy isc_mem_destroy(&mctx); 156275970Scy} 157275970Scy 158275970Scy/* 159275970Scy * Sleep for 'usec' microseconds. 160275970Scy */ 161275970Scyvoid 162275970Scyisc_test_nap(isc_uint32_t usec) { 163275970Scy#ifdef HAVE_NANOSLEEP 164275970Scy struct timespec ts; 165275970Scy 166275970Scy ts.tv_sec = usec / 1000000; 167275970Scy ts.tv_nsec = (usec % 1000000) * 1000; 168275970Scy nanosleep(&ts, NULL); 169275970Scy#elif HAVE_USLEEP 170275970Scy usleep(usec); 171275970Scy#else 172275970Scy /* 173275970Scy * No fractional-second sleep function is available, so we 174275970Scy * round up to the nearest second and sleep instead 175275970Scy */ 176275970Scy sleep((usec / 1000000) + 1); 177275970Scy#endif 178275970Scy} 179