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