xref: /minix3/tests/modules/k_helper/k_helper.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: k_helper.c,v 1.6 2012/06/03 10:59:44 dsl Exp $	*/
2*11be35a1SLionel Sambuc /*
3*11be35a1SLionel Sambuc  * Copyright (c) 2008 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc  * All rights reserved.
5*11be35a1SLionel Sambuc  *
6*11be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
7*11be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
8*11be35a1SLionel Sambuc  * are met:
9*11be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
10*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
11*11be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
12*11be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
13*11be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
14*11be35a1SLionel Sambuc  *
15*11be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16*11be35a1SLionel Sambuc  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17*11be35a1SLionel Sambuc  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18*11be35a1SLionel Sambuc  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19*11be35a1SLionel Sambuc  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20*11be35a1SLionel Sambuc  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*11be35a1SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22*11be35a1SLionel Sambuc  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*11be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24*11be35a1SLionel Sambuc  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25*11be35a1SLionel Sambuc  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26*11be35a1SLionel Sambuc  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*11be35a1SLionel Sambuc  */
28*11be35a1SLionel Sambuc 
29*11be35a1SLionel Sambuc #include <sys/cdefs.h>
30*11be35a1SLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: k_helper.c,v 1.6 2012/06/03 10:59:44 dsl Exp $");
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <sys/param.h>
33*11be35a1SLionel Sambuc #include <sys/kernel.h>
34*11be35a1SLionel Sambuc #include <sys/module.h>
35*11be35a1SLionel Sambuc #include <sys/sysctl.h>
36*11be35a1SLionel Sambuc 
37*11be35a1SLionel Sambuc #include <prop/proplib.h>
38*11be35a1SLionel Sambuc 
39*11be35a1SLionel Sambuc MODULE(MODULE_CLASS_MISC, k_helper, NULL);
40*11be35a1SLionel Sambuc 
41*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
42*11be35a1SLionel Sambuc /* Sysctl interface to query information about the module.               */
43*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc /* TODO: Change the integer variables below that represent booleans to
46*11be35a1SLionel Sambuc  * bools, once sysctl(8) supports CTLTYPE_BOOL nodes. */
47*11be35a1SLionel Sambuc 
48*11be35a1SLionel Sambuc static struct sysctllog *clogp;
49*11be35a1SLionel Sambuc static int present = 1;
50*11be35a1SLionel Sambuc static int prop_str_ok;
51*11be35a1SLionel Sambuc static char prop_str_val[128];
52*11be35a1SLionel Sambuc static int prop_int_ok;
53*11be35a1SLionel Sambuc static int64_t prop_int_val;
54*11be35a1SLionel Sambuc static int prop_int_load;
55*11be35a1SLionel Sambuc 
56*11be35a1SLionel Sambuc #define K_HELPER 0x12345678
57*11be35a1SLionel Sambuc #define K_HELPER_PRESENT 0
58*11be35a1SLionel Sambuc #define K_HELPER_PROP_STR_OK 1
59*11be35a1SLionel Sambuc #define K_HELPER_PROP_STR_VAL 2
60*11be35a1SLionel Sambuc #define K_HELPER_PROP_INT_OK 3
61*11be35a1SLionel Sambuc #define K_HELPER_PROP_INT_VAL 4
62*11be35a1SLionel Sambuc #define K_HELPER_PROP_INT_LOAD 5
63*11be35a1SLionel Sambuc 
64*11be35a1SLionel Sambuc SYSCTL_SETUP(sysctl_k_helper_setup, "sysctl k_helper subtree setup")
65*11be35a1SLionel Sambuc {
66*11be35a1SLionel Sambuc 
67*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
68*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
69*11be35a1SLionel Sambuc 	               CTLTYPE_NODE, "k_helper", NULL,
70*11be35a1SLionel Sambuc 	               NULL, 0, NULL, 0,
71*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, CTL_EOL);
72*11be35a1SLionel Sambuc 
73*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
74*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
75*11be35a1SLionel Sambuc 	               CTLTYPE_INT, "present",
76*11be35a1SLionel Sambuc 		       SYSCTL_DESCR("Whether the module was loaded or not"),
77*11be35a1SLionel Sambuc 		       NULL, 0, &present, 0,
78*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, K_HELPER_PRESENT, CTL_EOL);
79*11be35a1SLionel Sambuc 
80*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
81*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
82*11be35a1SLionel Sambuc 	               CTLTYPE_INT, "prop_str_ok",
83*11be35a1SLionel Sambuc 		       SYSCTL_DESCR("String property's validity"),
84*11be35a1SLionel Sambuc 		       NULL, 0, &prop_str_ok, 0,
85*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, K_HELPER_PROP_STR_OK, CTL_EOL);
86*11be35a1SLionel Sambuc 
87*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
88*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
89*11be35a1SLionel Sambuc 	               CTLTYPE_STRING, "prop_str_val",
90*11be35a1SLionel Sambuc 		       SYSCTL_DESCR("String property's value"),
91*11be35a1SLionel Sambuc 		       NULL, 0, prop_str_val, 0,
92*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, K_HELPER_PROP_STR_VAL, CTL_EOL);
93*11be35a1SLionel Sambuc 
94*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
95*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
96*11be35a1SLionel Sambuc 	               CTLTYPE_INT, "prop_int_ok",
97*11be35a1SLionel Sambuc 		       SYSCTL_DESCR("String property's validity"),
98*11be35a1SLionel Sambuc 		       NULL, 0, &prop_int_ok, 0,
99*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, K_HELPER_PROP_INT_OK, CTL_EOL);
100*11be35a1SLionel Sambuc 
101*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
102*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
103*11be35a1SLionel Sambuc 	               CTLTYPE_QUAD, "prop_int_val",
104*11be35a1SLionel Sambuc 		       SYSCTL_DESCR("String property's value"),
105*11be35a1SLionel Sambuc 		       NULL, 0, &prop_int_val, 0,
106*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, K_HELPER_PROP_INT_VAL, CTL_EOL);
107*11be35a1SLionel Sambuc 
108*11be35a1SLionel Sambuc 	sysctl_createv(clog, 0, NULL, NULL,
109*11be35a1SLionel Sambuc 	               CTLFLAG_PERMANENT,
110*11be35a1SLionel Sambuc 	               CTLTYPE_INT, "prop_int_load",
111*11be35a1SLionel Sambuc 		       SYSCTL_DESCR("Status of recursive modload"),
112*11be35a1SLionel Sambuc 		       NULL, 0, &prop_int_load, 0,
113*11be35a1SLionel Sambuc 	               CTL_VENDOR, K_HELPER, K_HELPER_PROP_INT_LOAD, CTL_EOL);
114*11be35a1SLionel Sambuc }
115*11be35a1SLionel Sambuc 
116*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
117*11be35a1SLionel Sambuc /* Module management.                                                    */
118*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc static
121*11be35a1SLionel Sambuc int
k_helper_init(prop_dictionary_t props)122*11be35a1SLionel Sambuc k_helper_init(prop_dictionary_t props)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc 	prop_object_t p;
125*11be35a1SLionel Sambuc 
126*11be35a1SLionel Sambuc 	p = prop_dictionary_get(props, "prop_str");
127*11be35a1SLionel Sambuc 	if (p == NULL)
128*11be35a1SLionel Sambuc 		prop_str_ok = 0;
129*11be35a1SLionel Sambuc 	else if (prop_object_type(p) != PROP_TYPE_STRING)
130*11be35a1SLionel Sambuc 		prop_str_ok = 0;
131*11be35a1SLionel Sambuc 	else {
132*11be35a1SLionel Sambuc 		const char *msg = prop_string_cstring_nocopy(p);
133*11be35a1SLionel Sambuc 		if (msg == NULL)
134*11be35a1SLionel Sambuc 			prop_str_ok = 0;
135*11be35a1SLionel Sambuc 		else {
136*11be35a1SLionel Sambuc 			strlcpy(prop_str_val, msg, sizeof(prop_str_val));
137*11be35a1SLionel Sambuc 			prop_str_ok = 1;
138*11be35a1SLionel Sambuc 		}
139*11be35a1SLionel Sambuc 	}
140*11be35a1SLionel Sambuc 	if (!prop_str_ok)
141*11be35a1SLionel Sambuc 		strlcpy(prop_str_val, "", sizeof(prop_str_val));
142*11be35a1SLionel Sambuc 
143*11be35a1SLionel Sambuc 	p = prop_dictionary_get(props, "prop_int");
144*11be35a1SLionel Sambuc 	if (p == NULL)
145*11be35a1SLionel Sambuc 		prop_int_ok = 0;
146*11be35a1SLionel Sambuc 	else if (prop_object_type(p) != PROP_TYPE_NUMBER)
147*11be35a1SLionel Sambuc 		prop_int_ok = 0;
148*11be35a1SLionel Sambuc 	else {
149*11be35a1SLionel Sambuc 		prop_int_val = prop_number_integer_value(p);
150*11be35a1SLionel Sambuc 		prop_int_ok = 1;
151*11be35a1SLionel Sambuc 	}
152*11be35a1SLionel Sambuc 	if (!prop_int_ok)
153*11be35a1SLionel Sambuc 		prop_int_val = -1;
154*11be35a1SLionel Sambuc 
155*11be35a1SLionel Sambuc 	p = prop_dictionary_get(props, "prop_recurse");
156*11be35a1SLionel Sambuc 	if (p != NULL && prop_object_type(p) == PROP_TYPE_STRING) {
157*11be35a1SLionel Sambuc 		const char *recurse_name = prop_string_cstring_nocopy(p);
158*11be35a1SLionel Sambuc 		if (recurse_name != NULL)
159*11be35a1SLionel Sambuc 			prop_int_load = module_load(recurse_name,
160*11be35a1SLionel Sambuc 			    MODCTL_NO_PROP, NULL, MODULE_CLASS_ANY);
161*11be35a1SLionel Sambuc 		else
162*11be35a1SLionel Sambuc 			prop_int_load = -1;
163*11be35a1SLionel Sambuc 	} else
164*11be35a1SLionel Sambuc 		prop_int_load = -2;
165*11be35a1SLionel Sambuc 
166*11be35a1SLionel Sambuc 	sysctl_k_helper_setup(&clogp);
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc 	return 0;
169*11be35a1SLionel Sambuc }
170*11be35a1SLionel Sambuc 
171*11be35a1SLionel Sambuc static
172*11be35a1SLionel Sambuc int
k_helper_fini(void * arg)173*11be35a1SLionel Sambuc k_helper_fini(void *arg)
174*11be35a1SLionel Sambuc {
175*11be35a1SLionel Sambuc 
176*11be35a1SLionel Sambuc 	sysctl_teardown(&clogp);
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc 	return 0;
179*11be35a1SLionel Sambuc }
180*11be35a1SLionel Sambuc 
181*11be35a1SLionel Sambuc static
182*11be35a1SLionel Sambuc int
k_helper_modcmd(modcmd_t cmd,void * arg)183*11be35a1SLionel Sambuc k_helper_modcmd(modcmd_t cmd, void *arg)
184*11be35a1SLionel Sambuc {
185*11be35a1SLionel Sambuc 	int ret;
186*11be35a1SLionel Sambuc 
187*11be35a1SLionel Sambuc 	switch (cmd) {
188*11be35a1SLionel Sambuc 	case MODULE_CMD_INIT:
189*11be35a1SLionel Sambuc 		ret = k_helper_init(arg);
190*11be35a1SLionel Sambuc 		break;
191*11be35a1SLionel Sambuc 
192*11be35a1SLionel Sambuc 	case MODULE_CMD_FINI:
193*11be35a1SLionel Sambuc 		ret = k_helper_fini(arg);
194*11be35a1SLionel Sambuc 		break;
195*11be35a1SLionel Sambuc 
196*11be35a1SLionel Sambuc 	case MODULE_CMD_STAT:
197*11be35a1SLionel Sambuc 	default:
198*11be35a1SLionel Sambuc 		ret = ENOTTY;
199*11be35a1SLionel Sambuc 	}
200*11be35a1SLionel Sambuc 
201*11be35a1SLionel Sambuc 	return ret;
202*11be35a1SLionel Sambuc }
203