1 /* $NetBSD: isctest.c,v 1.1.1.7 2014/12/10 03:34:44 christos Exp $ */
4 * Copyright (C) 2011-2014 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
28 #include <isc/buffer.h>
29 #include <isc/entropy.h>
33 #include <isc/socket.h>
34 #include <isc/string.h>
36 #include <isc/timer.h>
41 isc_mem_t
*mctx
= NULL
;
42 isc_entropy_t
*ectx
= NULL
;
43 isc_log_t
*lctx
= NULL
;
44 isc_taskmgr_t
*taskmgr
= NULL
;
45 isc_timermgr_t
*timermgr
= NULL
;
46 isc_socketmgr_t
*socketmgr
= NULL
;
47 isc_task_t
*maintask
= NULL
;
50 static isc_boolean_t hash_active
= ISC_FALSE
;
53 * Logging categories: this needs to match the list in bin/named/log.c.
55 static isc_logcategory_t categories
[] = {
62 { "update-security", 0 },
63 { "query-errors", 0 },
68 cleanup_managers(void) {
70 isc_task_destroy(&maintask
);
71 if (socketmgr
!= NULL
)
72 isc_socketmgr_destroy(&socketmgr
);
74 isc_taskmgr_destroy(&taskmgr
);
76 isc_timermgr_destroy(&timermgr
);
80 create_managers(void) {
82 #ifdef ISC_PLATFORM_USETHREADS
83 ncpus
= isc_os_ncpus();
88 CHECK(isc_taskmgr_create(mctx
, ncpus
, 0, &taskmgr
));
89 CHECK(isc_task_create(taskmgr
, 0, &maintask
));
90 isc_taskmgr_setexcltask(taskmgr
, maintask
);
92 CHECK(isc_timermgr_create(mctx
, &timermgr
));
93 CHECK(isc_socketmgr_create(mctx
, &socketmgr
));
94 return (ISC_R_SUCCESS
);
102 isc_test_begin(FILE *logfile
, isc_boolean_t start_managers
) {
105 isc_mem_debugging
|= ISC_MEM_DEBUGRECORD
;
106 CHECK(isc_mem_create(0, 0, &mctx
));
107 CHECK(isc_entropy_create(mctx
, &ectx
));
109 CHECK(isc_hash_create(mctx
, ectx
, 255));
110 hash_active
= ISC_TRUE
;
112 if (logfile
!= NULL
) {
113 isc_logdestination_t destination
;
114 isc_logconfig_t
*logconfig
= NULL
;
116 CHECK(isc_log_create(mctx
, &lctx
, &logconfig
));
117 isc_log_registercategories(lctx
, categories
);
118 isc_log_setcontext(lctx
);
120 destination
.file
.stream
= logfile
;
121 destination
.file
.name
= NULL
;
122 destination
.file
.versions
= ISC_LOG_ROLLNEVER
;
123 destination
.file
.maximum_size
= 0;
124 CHECK(isc_log_createchannel(logconfig
, "stderr",
128 CHECK(isc_log_usechannel(logconfig
, "stderr", NULL
, NULL
));
131 #ifdef ISC_PLATFORM_USETHREADS
132 ncpus
= isc_os_ncpus();
138 CHECK(create_managers());
140 return (ISC_R_SUCCESS
);
149 if (maintask
!= NULL
)
150 isc_task_detach(&maintask
);
152 isc_taskmgr_destroy(&taskmgr
);
154 isc_log_destroy(&lctx
);
157 hash_active
= ISC_FALSE
;
160 isc_entropy_detach(&ectx
);
165 isc_mem_destroy(&mctx
);
169 * Sleep for 'usec' microseconds.
172 isc_test_nap(isc_uint32_t usec
) {
173 #ifdef HAVE_NANOSLEEP
176 ts
.tv_sec
= usec
/ 1000000;
177 ts
.tv_nsec
= (usec
% 1000000) * 1000;
178 nanosleep(&ts
, NULL
);
183 * No fractional-second sleep function is available, so we
184 * round up to the nearest second and sleep instead
186 sleep((usec
/ 1000000) + 1);