1 /* $NetBSD: isc.c,v 1.4 2025/01/26 16:25:51 christos Exp $ */ 2 3 /* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * SPDX-License-Identifier: MPL-2.0 7 * 8 * This Source Code Form is subject to the terms of the Mozilla Public 9 * License, v. 2.0. If a copy of the MPL was not distributed with this 10 * file, you can obtain one at https://mozilla.org/MPL/2.0/. 11 * 12 * See the COPYRIGHT file distributed with this work for additional 13 * information regarding copyright ownership. 14 */ 15 16 /*! \file */ 17 18 #include <inttypes.h> 19 #include <signal.h> 20 #include <stdbool.h> 21 #include <stdlib.h> 22 #include <sys/resource.h> 23 #include <time.h> 24 25 #include <isc/buffer.h> 26 #include <isc/hash.h> 27 #include <isc/loop.h> 28 #include <isc/managers.h> 29 #include <isc/mem.h> 30 #include <isc/os.h> 31 #include <isc/string.h> 32 #include <isc/timer.h> 33 #include <isc/util.h> 34 35 #include <tests/isc.h> 36 37 isc_mem_t *mctx = NULL; 38 isc_log_t *lctx = NULL; 39 isc_loop_t *mainloop = NULL; 40 isc_loopmgr_t *loopmgr = NULL; 41 isc_nm_t *netmgr = NULL; 42 unsigned int workers = 0; 43 bool debug = false; 44 45 static void 46 adjustnofile(void) { 47 struct rlimit rl; 48 49 if (getrlimit(RLIMIT_NOFILE, &rl) == 0) { 50 if (rl.rlim_cur != rl.rlim_max) { 51 rl.rlim_cur = rl.rlim_max; 52 setrlimit(RLIMIT_NOFILE, &rl); 53 } 54 } 55 } 56 57 int 58 setup_workers(void **state ISC_ATTR_UNUSED) { 59 char *env_workers = getenv("ISC_TASK_WORKERS"); 60 if (env_workers != NULL) { 61 workers = atoi(env_workers); 62 } else { 63 workers = isc_os_ncpus(); 64 65 /* We always need at least two loops for some of the tests */ 66 if (workers < 2) { 67 workers = 2; 68 } 69 } 70 INSIST(workers != 0); 71 72 return 0; 73 } 74 75 int 76 setup_mctx(void **state ISC_ATTR_UNUSED) { 77 isc_mem_debugging |= ISC_MEM_DEBUGRECORD; 78 isc_mem_create(&mctx); 79 80 return 0; 81 } 82 83 int 84 teardown_mctx(void **state ISC_ATTR_UNUSED) { 85 isc_mem_destroy(&mctx); 86 87 return 0; 88 } 89 90 int 91 setup_loopmgr(void **state ISC_ATTR_UNUSED) { 92 REQUIRE(mctx != NULL); 93 94 setup_workers(state); 95 96 isc_loopmgr_create(mctx, workers, &loopmgr); 97 mainloop = isc_loop_main(loopmgr); 98 99 return 0; 100 } 101 102 int 103 teardown_loopmgr(void **state ISC_ATTR_UNUSED) { 104 REQUIRE(netmgr == NULL); 105 106 mainloop = NULL; 107 isc_loopmgr_destroy(&loopmgr); 108 109 return 0; 110 } 111 112 int 113 setup_netmgr(void **state ISC_ATTR_UNUSED) { 114 REQUIRE(loopmgr != NULL); 115 116 adjustnofile(); 117 118 isc_netmgr_create(mctx, loopmgr, &netmgr); 119 120 return 0; 121 } 122 123 int 124 teardown_netmgr(void **state ISC_ATTR_UNUSED) { 125 REQUIRE(loopmgr != NULL); 126 127 isc_netmgr_destroy(&netmgr); 128 129 return 0; 130 } 131 132 int 133 setup_managers(void **state) { 134 setup_loopmgr(state); 135 setup_netmgr(state); 136 137 return 0; 138 } 139 140 int 141 teardown_managers(void **state) { 142 teardown_netmgr(state); 143 teardown_loopmgr(state); 144 145 return 0; 146 } 147