1290001Sglebius/* 2290001Sglebius * Copyright (C) 2011, 2012 Internet Systems Consortium, Inc. ("ISC") 3290001Sglebius * 4290001Sglebius * Permission to use, copy, modify, and/or distribute this software for any 5290001Sglebius * purpose with or without fee is hereby granted, provided that the above 6290001Sglebius * copyright notice and this permission notice appear in all copies. 7290001Sglebius * 8290001Sglebius * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 9290001Sglebius * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 10290001Sglebius * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 11290001Sglebius * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 12290001Sglebius * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 13290001Sglebius * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 14290001Sglebius * PERFORMANCE OF THIS SOFTWARE. 15290001Sglebius */ 16290001Sglebius 17290001Sglebius/* $Id$ */ 18290001Sglebius 19290001Sglebius/*! \file */ 20290001Sglebius 21290001Sglebius#include <config.h> 22290001Sglebius 23290001Sglebius#include <time.h> 24290001Sglebius 25290001Sglebius#include <isc/app.h> 26290001Sglebius#include <isc/buffer.h> 27290001Sglebius#include <isc/entropy.h> 28290001Sglebius#include <isc/hash.h> 29290001Sglebius#include <isc/mem.h> 30290001Sglebius#include <isc/os.h> 31290001Sglebius#include <isc/socket.h> 32290001Sglebius#include <isc/string.h> 33290001Sglebius#include <isc/task.h> 34290001Sglebius#include <isc/timer.h> 35290001Sglebius#include <isc/util.h> 36290001Sglebius 37290001Sglebius#include "isctest.h" 38290001Sglebius 39290001Sglebiusisc_mem_t *mctx = NULL; 40290001Sglebiusisc_entropy_t *ectx = NULL; 41290001Sglebiusisc_log_t *lctx = NULL; 42290001Sglebiusisc_taskmgr_t *taskmgr = NULL; 43290001Sglebiusisc_timermgr_t *timermgr = NULL; 44290001Sglebiusisc_socketmgr_t *socketmgr = NULL; 45290001Sglebiusint ncpus; 46290001Sglebius 47290001Sglebiusstatic isc_boolean_t hash_active = ISC_FALSE; 48290001Sglebius 49290001Sglebius/* 50290001Sglebius * Logging categories: this needs to match the list in bin/named/log.c. 51290001Sglebius */ 52290001Sglebiusstatic isc_logcategory_t categories[] = { 53290001Sglebius { "", 0 }, 54290001Sglebius { "client", 0 }, 55290001Sglebius { "network", 0 }, 56290001Sglebius { "update", 0 }, 57290001Sglebius { "queries", 0 }, 58290001Sglebius { "unmatched", 0 }, 59290001Sglebius { "update-security", 0 }, 60290001Sglebius { "query-errors", 0 }, 61290001Sglebius { NULL, 0 } 62290001Sglebius}; 63290001Sglebius 64290001Sglebiusstatic void 65290001Sglebiuscleanup_managers() { 66290001Sglebius if (socketmgr != NULL) 67290001Sglebius isc_socketmgr_destroy(&socketmgr); 68290001Sglebius if (taskmgr != NULL) 69290001Sglebius isc_taskmgr_destroy(&taskmgr); 70290001Sglebius if (timermgr != NULL) 71290001Sglebius isc_timermgr_destroy(&timermgr); 72290001Sglebius} 73290001Sglebius 74290001Sglebiusstatic isc_result_t 75290001Sglebiuscreate_managers() { 76290001Sglebius isc_result_t result; 77290001Sglebius#ifdef ISC_PLATFORM_USETHREADS 78290001Sglebius ncpus = isc_os_ncpus(); 79290001Sglebius#else 80290001Sglebius ncpus = 1; 81290001Sglebius#endif 82290001Sglebius 83290001Sglebius CHECK(isc_taskmgr_create(mctx, ncpus, 0, &taskmgr)); 84290001Sglebius CHECK(isc_timermgr_create(mctx, &timermgr)); 85290001Sglebius CHECK(isc_socketmgr_create(mctx, &socketmgr)); 86290001Sglebius return (ISC_R_SUCCESS); 87290001Sglebius 88290001Sglebius cleanup: 89290001Sglebius cleanup_managers(); 90290001Sglebius return (result); 91290001Sglebius} 92290001Sglebius 93290001Sglebiusisc_result_t 94290001Sglebiusisc_test_begin(FILE *logfile, isc_boolean_t start_managers) { 95290001Sglebius isc_result_t result; 96290001Sglebius 97290001Sglebius isc_mem_debugging |= ISC_MEM_DEBUGRECORD; 98290001Sglebius CHECK(isc_mem_create(0, 0, &mctx)); 99290001Sglebius CHECK(isc_entropy_create(mctx, &ectx)); 100290001Sglebius 101290001Sglebius CHECK(isc_hash_create(mctx, ectx, 255)); 102290001Sglebius hash_active = ISC_TRUE; 103290001Sglebius 104290001Sglebius if (logfile != NULL) { 105290001Sglebius isc_logdestination_t destination; 106290001Sglebius isc_logconfig_t *logconfig = NULL; 107290001Sglebius 108290001Sglebius CHECK(isc_log_create(mctx, &lctx, &logconfig)); 109290001Sglebius isc_log_registercategories(lctx, categories); 110290001Sglebius isc_log_setcontext(lctx); 111290001Sglebius 112290001Sglebius destination.file.stream = logfile; 113290001Sglebius destination.file.name = NULL; 114290001Sglebius destination.file.versions = ISC_LOG_ROLLNEVER; 115290001Sglebius destination.file.maximum_size = 0; 116290001Sglebius CHECK(isc_log_createchannel(logconfig, "stderr", 117290001Sglebius ISC_LOG_TOFILEDESC, 118290001Sglebius ISC_LOG_DYNAMIC, 119290001Sglebius &destination, 0)); 120290001Sglebius CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL)); 121290001Sglebius } 122290001Sglebius 123290001Sglebius#ifdef ISC_PLATFORM_USETHREADS 124290001Sglebius ncpus = isc_os_ncpus(); 125290001Sglebius#else 126290001Sglebius ncpus = 1; 127290001Sglebius#endif 128290001Sglebius 129290001Sglebius if (start_managers) 130290001Sglebius CHECK(create_managers()); 131290001Sglebius 132290001Sglebius return (ISC_R_SUCCESS); 133290001Sglebius 134290001Sglebius cleanup: 135290001Sglebius isc_test_end(); 136290001Sglebius return (result); 137290001Sglebius} 138290001Sglebius 139290001Sglebiusvoid 140290001Sglebiusisc_test_end() { 141290001Sglebius if (taskmgr != NULL) 142290001Sglebius isc_taskmgr_destroy(&taskmgr); 143290001Sglebius if (lctx != NULL) 144290001Sglebius isc_log_destroy(&lctx); 145290001Sglebius if (hash_active) { 146290001Sglebius isc_hash_destroy(); 147290001Sglebius hash_active = ISC_FALSE; 148290001Sglebius } 149290001Sglebius if (ectx != NULL) 150290001Sglebius isc_entropy_detach(&ectx); 151290001Sglebius 152290001Sglebius cleanup_managers(); 153290001Sglebius 154290001Sglebius if (mctx != NULL) 155290001Sglebius isc_mem_destroy(&mctx); 156290001Sglebius} 157290001Sglebius 158290001Sglebius/* 159290001Sglebius * Sleep for 'usec' microseconds. 160290001Sglebius */ 161290001Sglebiusvoid 162290001Sglebiusisc_test_nap(isc_uint32_t usec) { 163290001Sglebius#ifdef HAVE_NANOSLEEP 164290001Sglebius struct timespec ts; 165290001Sglebius 166290001Sglebius ts.tv_sec = usec / 1000000; 167290001Sglebius ts.tv_nsec = (usec % 1000000) * 1000; 168290001Sglebius nanosleep(&ts, NULL); 169290001Sglebius#elif HAVE_USLEEP 170290001Sglebius usleep(usec); 171290001Sglebius#else 172290001Sglebius /* 173290001Sglebius * No fractional-second sleep function is available, so we 174290001Sglebius * round up to the nearest second and sleep instead 175290001Sglebius */ 176290001Sglebius sleep((usec / 1000000) + 1); 177290001Sglebius#endif 178290001Sglebius} 179