xref: /freebsd/contrib/netbsd-tests/modules/t_modctl.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1 /*	$NetBSD: t_modctl.c,v 1.12 2012/08/20 08:07:52 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: t_modctl.c,v 1.12 2012/08/20 08:07:52 martin Exp $");
31 
32 #include <sys/module.h>
33 #include <sys/sysctl.h>
34 
35 #include <assert.h>
36 #include <errno.h>
37 #include <stdarg.h>
38 #include <stdbool.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 
43 #include <prop/proplib.h>
44 
45 #include <atf-c.h>
46 
47 enum presence_check { both_checks, stat_check, sysctl_check };
48 
49 static void	check_permission(void);
50 static bool	get_modstat_info(const char *, modstat_t *);
51 static bool	get_sysctl(const char *, void *buf, const size_t);
52 static bool	k_helper_is_present_stat(void);
53 static bool	k_helper_is_present_sysctl(void);
54 static bool	k_helper_is_present(enum presence_check);
55 static int	load(prop_dictionary_t, bool, const char *, ...);
56 static int	unload(const char *, bool);
57 static void	unload_cleanup(const char *);
58 
59 /* --------------------------------------------------------------------- */
60 /* Auxiliary functions                                                   */
61 /* --------------------------------------------------------------------- */
62 
63 /*
64  * A function checking wether we are allowed to load modules currently
65  * (either the kernel is not modular, or securelevel may prevent it)
66  */
67 static void
check_permission(void)68 check_permission(void)
69 {
70 	int err;
71 
72 	err = modctl(MODCTL_EXISTS, 0);
73 	if (err == 0) return;
74 	if (errno == ENOSYS)
75 		atf_tc_skip("Kernel does not have 'options MODULAR'.");
76 	else if (errno == EPERM)
77 		atf_tc_skip("Module loading administratively forbidden");
78 	ATF_REQUIRE_EQ_MSG(errno, 0, "unexpected error %d from "
79 	    "modctl(MODCTL_EXISTS, 0)", errno);
80 }
81 
82 static bool
get_modstat_info(const char * name,modstat_t * msdest)83 get_modstat_info(const char *name, modstat_t *msdest)
84 {
85 	bool found;
86 	size_t len;
87 	struct iovec iov;
88 	modstat_t *ms;
89 
90 	check_permission();
91 	for (len = 4096; ;) {
92 		iov.iov_base = malloc(len);
93 		iov.iov_len = len;
94 
95 		errno = 0;
96 
97 		if (modctl(MODCTL_STAT, &iov) != 0) {
98 			int err = errno;
99 			fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n",
100 			    strerror(err));
101 			atf_tc_fail("Failed to query module status");
102 		}
103 		if (len >= iov.iov_len)
104 			break;
105 		free(iov.iov_base);
106 		len = iov.iov_len;
107 	}
108 
109 	found = false;
110 	len = iov.iov_len / sizeof(modstat_t);
111 	for (ms = (modstat_t *)iov.iov_base; len != 0 && !found;
112 	    ms++, len--) {
113 		if (strcmp(ms->ms_name, name) == 0) {
114 			if (msdest != NULL)
115 				*msdest = *ms;
116 			found = true;
117 		}
118 	}
119 
120 	free(iov.iov_base);
121 
122 	return found;
123 }
124 
125 /*
126  * Queries a sysctl property.
127  */
128 static bool
get_sysctl(const char * name,void * buf,const size_t len)129 get_sysctl(const char *name, void *buf, const size_t len)
130 {
131 	size_t len2 = len;
132 	printf("Querying sysctl variable: %s\n", name);
133 	int ret = sysctlbyname(name, buf, &len2, NULL, 0);
134 	if (ret == -1 && errno != ENOENT) {
135 		fprintf(stderr, "sysctlbyname(2) failed: %s\n",
136 		    strerror(errno));
137 		atf_tc_fail("Failed to query %s", name);
138 	}
139 	return ret != -1;
140 }
141 
142 /*
143  * Returns a boolean indicating if the k_helper module was loaded
144  * successfully.  This implementation uses modctl(2)'s MODCTL_STAT
145  * subcommand to do the check.
146  */
147 static bool
k_helper_is_present_stat(void)148 k_helper_is_present_stat(void)
149 {
150 
151 	return get_modstat_info("k_helper", NULL);
152 }
153 
154 /*
155  * Returns a boolean indicating if the k_helper module was loaded
156  * successfully.  This implementation uses the module's sysctl
157  * installed node to do the check.
158  */
159 static bool
k_helper_is_present_sysctl(void)160 k_helper_is_present_sysctl(void)
161 {
162 	size_t present;
163 
164 	return get_sysctl("vendor.k_helper.present", &present,
165 	    sizeof(present));
166 }
167 
168 /*
169  * Returns a boolean indicating if the k_helper module was loaded
170  * successfully.  The 'how' parameter specifies the implementation to
171  * use to do the check.
172  */
173 static bool
k_helper_is_present(enum presence_check how)174 k_helper_is_present(enum presence_check how)
175 {
176 	bool found;
177 
178 	switch (how) {
179 	case both_checks:
180 		found = k_helper_is_present_stat();
181 		ATF_CHECK(k_helper_is_present_sysctl() == found);
182 		break;
183 
184 	case stat_check:
185 		found = k_helper_is_present_stat();
186 		break;
187 
188 	case sysctl_check:
189 		found = k_helper_is_present_sysctl();
190 		break;
191 
192 	default:
193 		found = false;
194 		assert(found);
195 	}
196 
197 	return found;
198 }
199 
200 /*
201  * Loads the specified module from a file.  If fatal is set and an error
202  * occurs when loading the module, an error message is printed and the
203  * test case is aborted.
204  */
205 static __printflike(3, 4) int
load(prop_dictionary_t props,bool fatal,const char * fmt,...)206 load(prop_dictionary_t props, bool fatal, const char *fmt, ...)
207 {
208 	int err;
209 	va_list ap;
210 	char filename[MAXPATHLEN], *propsstr;
211 	modctl_load_t ml;
212 
213 	check_permission();
214 	if (props == NULL) {
215 		props = prop_dictionary_create();
216 		propsstr = prop_dictionary_externalize(props);
217 		ATF_CHECK(propsstr != NULL);
218 		prop_object_release(props);
219 	} else {
220 		propsstr = prop_dictionary_externalize(props);
221 		ATF_CHECK(propsstr != NULL);
222 	}
223 
224 	va_start(ap, fmt);
225 	vsnprintf(filename, sizeof(filename), fmt, ap);
226 	va_end(ap);
227 
228 	ml.ml_filename = filename;
229 	ml.ml_flags = 0;
230 	ml.ml_props = propsstr;
231 	ml.ml_propslen = strlen(propsstr);
232 
233 	printf("Loading module %s\n", filename);
234 	errno = err = 0;
235 
236 	if (modctl(MODCTL_LOAD, &ml) == -1) {
237 		err = errno;
238 		fprintf(stderr, "modctl(MODCTL_LOAD, %s), failed: %s\n",
239 		    filename, strerror(err));
240 		if (fatal)
241 			atf_tc_fail("Module load failed");
242 	}
243 
244 	free(propsstr);
245 
246 	return err;
247 }
248 
249 /*
250  * Unloads the specified module.  If silent is true, nothing will be
251  * printed and no errors will be raised if the unload was unsuccessful.
252  */
253 static int
unload(const char * name,bool fatal)254 unload(const char *name, bool fatal)
255 {
256 	int err;
257 
258 	check_permission();
259 	printf("Unloading module %s\n", name);
260 	errno = err = 0;
261 
262 	if (modctl(MODCTL_UNLOAD, __UNCONST(name)) == -1) {
263 		err = errno;
264 		fprintf(stderr, "modctl(MODCTL_UNLOAD, %s) failed: %s\n",
265 		    name, strerror(err));
266 		if (fatal)
267 			atf_tc_fail("Module unload failed");
268 	}
269 	return err;
270 }
271 
272 /*
273  * A silent version of unload, to be called as part of the cleanup
274  * process only.
275  */
276 static void
unload_cleanup(const char * name)277 unload_cleanup(const char *name)
278 {
279 
280 	(void)modctl(MODCTL_UNLOAD, __UNCONST(name));
281 }
282 
283 /* --------------------------------------------------------------------- */
284 /* Test cases                                                            */
285 /* --------------------------------------------------------------------- */
286 
287 ATF_TC_WITH_CLEANUP(cmd_load);
ATF_TC_HEAD(cmd_load,tc)288 ATF_TC_HEAD(cmd_load, tc)
289 {
290 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command");
291 	atf_tc_set_md_var(tc, "require.user", "root");
292 }
ATF_TC_BODY(cmd_load,tc)293 ATF_TC_BODY(cmd_load, tc)
294 {
295 	char longname[MAXPATHLEN];
296 	size_t i;
297 
298 	ATF_CHECK(load(NULL, false, " ") == ENOENT);
299 	ATF_CHECK(load(NULL, false, "non-existent.o") == ENOENT);
300 
301 	for (i = 0; i < MAXPATHLEN - 1; i++)
302 		longname[i] = 'a';
303 	longname[MAXPATHLEN - 1] = '\0';
304 	ATF_CHECK(load(NULL, false, "%s", longname) == ENAMETOOLONG);
305 
306 	ATF_CHECK(!k_helper_is_present(stat_check));
307 	load(NULL, true, "%s/k_helper/k_helper.kmod",
308 	    atf_tc_get_config_var(tc, "srcdir"));
309 	printf("Checking if load was successful\n");
310 	ATF_CHECK(k_helper_is_present(stat_check));
311 }
ATF_TC_CLEANUP(cmd_load,tc)312 ATF_TC_CLEANUP(cmd_load, tc)
313 {
314 	unload_cleanup("k_helper");
315 }
316 
317 ATF_TC_WITH_CLEANUP(cmd_load_props);
ATF_TC_HEAD(cmd_load_props,tc)318 ATF_TC_HEAD(cmd_load_props, tc)
319 {
320 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command, "
321 	    "providing extra load-time properties");
322 	atf_tc_set_md_var(tc, "require.user", "root");
323 }
ATF_TC_BODY(cmd_load_props,tc)324 ATF_TC_BODY(cmd_load_props, tc)
325 {
326 	prop_dictionary_t props;
327 
328 	printf("Loading module without properties\n");
329 	props = prop_dictionary_create();
330 	load(props, true, "%s/k_helper/k_helper.kmod",
331 	    atf_tc_get_config_var(tc, "srcdir"));
332 	prop_object_release(props);
333 	{
334 		int ok;
335 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
336 		    &ok, sizeof(ok)));
337 		ATF_CHECK(!ok);
338 	}
339 	unload("k_helper", true);
340 
341 	printf("Loading module with a string property\n");
342 	props = prop_dictionary_create();
343 	prop_dictionary_set(props, "prop_str",
344 	    prop_string_create_cstring("1st string"));
345 	load(props, true, "%s/k_helper/k_helper.kmod",
346 	    atf_tc_get_config_var(tc, "srcdir"));
347 	prop_object_release(props);
348 	{
349 		int ok;
350 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
351 		    &ok, sizeof(ok)));
352 		ATF_CHECK(ok);
353 
354 		char val[128];
355 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
356 		    &val, sizeof(val)));
357 		ATF_CHECK(strcmp(val, "1st string") == 0);
358 	}
359 	unload("k_helper", true);
360 
361 	printf("Loading module with a different string property\n");
362 	props = prop_dictionary_create();
363 	prop_dictionary_set(props, "prop_str",
364 	    prop_string_create_cstring("2nd string"));
365 	load(props, true, "%s/k_helper/k_helper.kmod",
366 	    atf_tc_get_config_var(tc, "srcdir"));
367 	prop_object_release(props);
368 	{
369 		int ok;
370 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
371 		    &ok, sizeof(ok)));
372 		ATF_CHECK(ok);
373 
374 		char val[128];
375 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
376 		    &val, sizeof(val)));
377 		ATF_CHECK(strcmp(val, "2nd string") == 0);
378 	}
379 	unload("k_helper", true);
380 }
ATF_TC_CLEANUP(cmd_load_props,tc)381 ATF_TC_CLEANUP(cmd_load_props, tc)
382 {
383 	unload_cleanup("k_helper");
384 }
385 
386 ATF_TC_WITH_CLEANUP(cmd_load_recurse);
ATF_TC_HEAD(cmd_load_recurse,tc)387 ATF_TC_HEAD(cmd_load_recurse, tc)
388 {
389 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command, "
390 	    "with recursive module_load()");
391 	atf_tc_set_md_var(tc, "require.user", "root");
392 }
ATF_TC_BODY(cmd_load_recurse,tc)393 ATF_TC_BODY(cmd_load_recurse, tc)
394 {
395 	prop_dictionary_t props;
396 	char filename[MAXPATHLEN];
397 
398 	printf("Loading module with request to load another module\n");
399 	props = prop_dictionary_create();
400 	snprintf(filename, sizeof(filename), "%s/k_helper2/k_helper2.kmod",
401 	    atf_tc_get_config_var(tc, "srcdir"));
402 	prop_dictionary_set(props, "prop_recurse",
403 	    prop_string_create_cstring(filename));
404 	load(props, true, "%s/k_helper/k_helper.kmod",
405 	    atf_tc_get_config_var(tc, "srcdir"));
406 	{
407 		int ok;
408 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_int_load",
409 		    &ok, sizeof(ok)));
410 		ATF_CHECK(ok == 0);
411 		ATF_CHECK(get_sysctl("vendor.k_helper2.present",
412 		    &ok, sizeof(ok)));
413 		ATF_CHECK(ok);
414 	}
415 	unload("k_helper", true);
416 	unload("k_helper2", true);
417 }
ATF_TC_CLEANUP(cmd_load_recurse,tc)418 ATF_TC_CLEANUP(cmd_load_recurse, tc)
419 {
420 	unload_cleanup("k_helper");
421 	unload_cleanup("k_helper2");
422 }
423 
424 ATF_TC_WITH_CLEANUP(cmd_stat);
ATF_TC_HEAD(cmd_stat,tc)425 ATF_TC_HEAD(cmd_stat, tc)
426 {
427 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_STAT command");
428 	atf_tc_set_md_var(tc, "require.user", "root");
429 }
ATF_TC_BODY(cmd_stat,tc)430 ATF_TC_BODY(cmd_stat, tc)
431 {
432 	ATF_CHECK(!k_helper_is_present(both_checks));
433 
434 	load(NULL, true, "%s/k_helper/k_helper.kmod",
435 	    atf_tc_get_config_var(tc, "srcdir"));
436 	ATF_CHECK(k_helper_is_present(both_checks));
437 	{
438 		modstat_t ms;
439 		ATF_CHECK(get_modstat_info("k_helper", &ms));
440 
441 		ATF_CHECK(ms.ms_class == MODULE_CLASS_MISC);
442 		ATF_CHECK(ms.ms_source == MODULE_SOURCE_FILESYS);
443 		ATF_CHECK(ms.ms_refcnt == 0);
444 	}
445 	unload("k_helper", true);
446 
447 	ATF_CHECK(!k_helper_is_present(both_checks));
448 }
ATF_TC_CLEANUP(cmd_stat,tc)449 ATF_TC_CLEANUP(cmd_stat, tc)
450 {
451 	unload_cleanup("k_helper");
452 }
453 
454 ATF_TC_WITH_CLEANUP(cmd_unload);
ATF_TC_HEAD(cmd_unload,tc)455 ATF_TC_HEAD(cmd_unload, tc)
456 {
457 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_UNLOAD command");
458 	atf_tc_set_md_var(tc, "require.user", "root");
459 }
ATF_TC_BODY(cmd_unload,tc)460 ATF_TC_BODY(cmd_unload, tc)
461 {
462 	load(NULL, true, "%s/k_helper/k_helper.kmod",
463 	    atf_tc_get_config_var(tc, "srcdir"));
464 
465 	ATF_CHECK(unload("", false) == ENOENT);
466 	ATF_CHECK(unload("non-existent.kmod", false) == ENOENT);
467 	ATF_CHECK(unload("k_helper.kmod", false) == ENOENT);
468 
469 	ATF_CHECK(k_helper_is_present(stat_check));
470 	unload("k_helper", true);
471 	printf("Checking if unload was successful\n");
472 	ATF_CHECK(!k_helper_is_present(stat_check));
473 }
ATF_TC_CLEANUP(cmd_unload,tc)474 ATF_TC_CLEANUP(cmd_unload, tc)
475 {
476 	unload_cleanup("k_helper");
477 }
478 
479 /* --------------------------------------------------------------------- */
480 /* Main                                                                  */
481 /* --------------------------------------------------------------------- */
482 
ATF_TP_ADD_TCS(tp)483 ATF_TP_ADD_TCS(tp)
484 {
485 
486 	ATF_TP_ADD_TC(tp, cmd_load);
487 	ATF_TP_ADD_TC(tp, cmd_load_props);
488 	ATF_TP_ADD_TC(tp, cmd_stat);
489 	ATF_TP_ADD_TC(tp, cmd_load_recurse);
490 	ATF_TP_ADD_TC(tp, cmd_unload);
491 
492 	return atf_no_error();
493 }
494