xref: /haiku/src/libs/compat/freebsd_network/sysinit.c (revision da60a673b6910a4d9c856f3ec8af3bd9683c7016)
1*da60a673SAugustin Cavalier /*
2*da60a673SAugustin Cavalier  * Copyright 2018, Haiku, Inc. All rights reserved.
3*da60a673SAugustin Cavalier  * Distributed under the terms of the MIT License.
4*da60a673SAugustin Cavalier  */
5*da60a673SAugustin Cavalier #include "sysinit.h"
6*da60a673SAugustin Cavalier 
7*da60a673SAugustin Cavalier #include <sys/cdefs.h>
8*da60a673SAugustin Cavalier #include <sys/kernel.h>
9*da60a673SAugustin Cavalier 
10*da60a673SAugustin Cavalier 
11*da60a673SAugustin Cavalier //#define TRACE_SYSINIT
12*da60a673SAugustin Cavalier #ifdef TRACE_SYSINIT
13*da60a673SAugustin Cavalier #	define TRACE(x...) dprintf(x)
14*da60a673SAugustin Cavalier #else
15*da60a673SAugustin Cavalier #	define TRACE(x...)
16*da60a673SAugustin Cavalier #endif
17*da60a673SAugustin Cavalier 
18*da60a673SAugustin Cavalier 
19*da60a673SAugustin Cavalier /* linker sets */
20*da60a673SAugustin Cavalier SET_DECLARE(__freebsd_sysinit, struct sysinit);
21*da60a673SAugustin Cavalier SET_DECLARE(__freebsd_sysuninit, struct sysinit);
22*da60a673SAugustin Cavalier 
23*da60a673SAugustin Cavalier /* make sure there's something in both linker sets, so we can link */
24*da60a673SAugustin Cavalier SYSINIT(__dummy, 0, 0, NULL, NULL);
25*da60a673SAugustin Cavalier SYSUNINIT(__dummy, 0, 0, NULL, NULL);
26*da60a673SAugustin Cavalier 
27*da60a673SAugustin Cavalier 
28*da60a673SAugustin Cavalier void
29*da60a673SAugustin Cavalier init_sysinit()
30*da60a673SAugustin Cavalier {
31*da60a673SAugustin Cavalier 	struct sysinit* const* initee;
32*da60a673SAugustin Cavalier 	const enum sysinit_elem_order orders[6] = {
33*da60a673SAugustin Cavalier 		SI_ORDER_FIRST, SI_ORDER_SECOND, SI_ORDER_THIRD, SI_ORDER_FOURTH,
34*da60a673SAugustin Cavalier 		SI_ORDER_MIDDLE, SI_ORDER_ANY,
35*da60a673SAugustin Cavalier 	};
36*da60a673SAugustin Cavalier 	uint32 i;
37*da60a673SAugustin Cavalier 
38*da60a673SAugustin Cavalier 	for (i = 0; i < 6; i++) {
39*da60a673SAugustin Cavalier 		SET_FOREACH(initee, __freebsd_sysinit) {
40*da60a673SAugustin Cavalier 			if ((*initee)->order != orders[i] || (*initee)->func == NULL)
41*da60a673SAugustin Cavalier 				continue;
42*da60a673SAugustin Cavalier 			TRACE("sysinit: %d, %d, %s\n", orders[i], (*initee)->order,
43*da60a673SAugustin Cavalier 				(*initee)->name);
44*da60a673SAugustin Cavalier 			(*initee)->func((*initee)->arg);
45*da60a673SAugustin Cavalier 		}
46*da60a673SAugustin Cavalier 	}
47*da60a673SAugustin Cavalier }
48*da60a673SAugustin Cavalier 
49*da60a673SAugustin Cavalier 
50*da60a673SAugustin Cavalier void
51*da60a673SAugustin Cavalier uninit_sysinit()
52*da60a673SAugustin Cavalier {
53*da60a673SAugustin Cavalier 	struct sysinit* const* initee;
54*da60a673SAugustin Cavalier 	const enum sysinit_elem_order orders[6] = {
55*da60a673SAugustin Cavalier 		SI_ORDER_FIRST, SI_ORDER_SECOND, SI_ORDER_THIRD, SI_ORDER_FOURTH,
56*da60a673SAugustin Cavalier 		SI_ORDER_MIDDLE, SI_ORDER_ANY,
57*da60a673SAugustin Cavalier 	};
58*da60a673SAugustin Cavalier 	uint32 i;
59*da60a673SAugustin Cavalier 
60*da60a673SAugustin Cavalier 	for (i = 5; i >= 0; i--) {
61*da60a673SAugustin Cavalier 		SET_FOREACH(initee, __freebsd_sysuninit) {
62*da60a673SAugustin Cavalier 			if ((*initee)->order != orders[i] || (*initee)->func == NULL)
63*da60a673SAugustin Cavalier 				continue;
64*da60a673SAugustin Cavalier 			TRACE("sysinit: de-initializing %s\n", (*initee)->name);
65*da60a673SAugustin Cavalier 			(*initee)->func((*initee)->arg);
66*da60a673SAugustin Cavalier 		}
67*da60a673SAugustin Cavalier 	}
68*da60a673SAugustin Cavalier }
69