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