Debian Codegraph @

tests/libtest/isc.c

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