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