1 /* $NetBSD: t_modctl.c,v 1.12 2012/08/20 08:07:52 martin Exp $ */
3 * Copyright (c) 2008 The NetBSD Foundation, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: t_modctl.c,v 1.12 2012/08/20 08:07:52 martin Exp $");
32 #include <sys/module.h>
33 #include <sys/sysctl.h>
43 #include <prop/proplib.h>
47 enum presence_check
{ both_checks
, stat_check
, sysctl_check
};
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 *);
59 /* --------------------------------------------------------------------- */
60 /* Auxiliary functions */
61 /* --------------------------------------------------------------------- */
64 * A function checking wether we are allowed to load modules currently
65 * (either the kernel is not modular, or securelevel may prevent it)
68 check_permission(void)
72 err
= modctl(MODCTL_EXISTS
, 0);
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
);
83 get_modstat_info(const char *name
, modstat_t
*msdest
)
92 iov
.iov_base
= malloc(len
);
97 if (modctl(MODCTL_STAT
, &iov
) != 0) {
99 fprintf(stderr
, "modctl(MODCTL_STAT) failed: %s\n",
101 atf_tc_fail("Failed to query module status");
103 if (len
>= iov
.iov_len
)
110 len
= iov
.iov_len
/ sizeof(modstat_t
);
111 for (ms
= (modstat_t
*)iov
.iov_base
; len
!= 0 && !found
;
113 if (strcmp(ms
->ms_name
, name
) == 0) {
126 * Queries a sysctl property.
129 get_sysctl(const char *name
, void *buf
, const size_t 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",
137 atf_tc_fail("Failed to query %s", name
);
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.
148 k_helper_is_present_stat(void)
151 return get_modstat_info("k_helper", NULL
);
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.
160 k_helper_is_present_sysctl(void)
164 return get_sysctl("vendor.k_helper.present", &present
,
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.
174 k_helper_is_present(enum presence_check how
)
180 found
= k_helper_is_present_stat();
181 ATF_CHECK(k_helper_is_present_sysctl() == found
);
185 found
= k_helper_is_present_stat();
189 found
= k_helper_is_present_sysctl();
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.
205 static __printflike(3, 4) int
206 load(prop_dictionary_t props
, bool fatal
, const char *fmt
, ...)
210 char filename
[MAXPATHLEN
], *propsstr
;
215 props
= prop_dictionary_create();
216 propsstr
= prop_dictionary_externalize(props
);
217 ATF_CHECK(propsstr
!= NULL
);
218 prop_object_release(props
);
220 propsstr
= prop_dictionary_externalize(props
);
221 ATF_CHECK(propsstr
!= NULL
);
225 vsnprintf(filename
, sizeof(filename
), fmt
, ap
);
228 ml
.ml_filename
= filename
;
230 ml
.ml_props
= propsstr
;
231 ml
.ml_propslen
= strlen(propsstr
);
233 printf("Loading module %s\n", filename
);
236 if (modctl(MODCTL_LOAD
, &ml
) == -1) {
238 fprintf(stderr
, "modctl(MODCTL_LOAD, %s), failed: %s\n",
239 filename
, strerror(err
));
241 atf_tc_fail("Module load failed");
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.
254 unload(const char *name
, bool fatal
)
259 printf("Unloading module %s\n", name
);
262 if (modctl(MODCTL_UNLOAD
, __UNCONST(name
)) == -1) {
264 fprintf(stderr
, "modctl(MODCTL_UNLOAD, %s) failed: %s\n",
265 name
, strerror(err
));
267 atf_tc_fail("Module unload failed");
273 * A silent version of unload, to be called as part of the cleanup
277 unload_cleanup(const char *name
)
280 (void)modctl(MODCTL_UNLOAD
, __UNCONST(name
));
283 /* --------------------------------------------------------------------- */
285 /* --------------------------------------------------------------------- */
287 ATF_TC_WITH_CLEANUP(cmd_load
);
288 ATF_TC_HEAD(cmd_load
, tc
)
290 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_LOAD command");
291 atf_tc_set_md_var(tc
, "require.user", "root");
293 ATF_TC_BODY(cmd_load
, tc
)
295 char longname
[MAXPATHLEN
];
298 ATF_CHECK(load(NULL
, false, " ") == ENOENT
);
299 ATF_CHECK(load(NULL
, false, "non-existent.o") == ENOENT
);
301 for (i
= 0; i
< MAXPATHLEN
- 1; i
++)
303 longname
[MAXPATHLEN
- 1] = '\0';
304 ATF_CHECK(load(NULL
, false, "%s", longname
) == ENAMETOOLONG
);
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
));
312 ATF_TC_CLEANUP(cmd_load
, tc
)
314 unload_cleanup("k_helper");
317 ATF_TC_WITH_CLEANUP(cmd_load_props
);
318 ATF_TC_HEAD(cmd_load_props
, tc
)
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");
324 ATF_TC_BODY(cmd_load_props
, tc
)
326 prop_dictionary_t props
;
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
);
335 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
339 unload("k_helper", true);
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
);
350 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
355 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
357 ATF_CHECK(strcmp(val
, "1st string") == 0);
359 unload("k_helper", true);
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
);
370 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
375 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
377 ATF_CHECK(strcmp(val
, "2nd string") == 0);
379 unload("k_helper", true);
381 ATF_TC_CLEANUP(cmd_load_props
, tc
)
383 unload_cleanup("k_helper");
386 ATF_TC_WITH_CLEANUP(cmd_load_recurse
);
387 ATF_TC_HEAD(cmd_load_recurse
, tc
)
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");
393 ATF_TC_BODY(cmd_load_recurse
, tc
)
395 prop_dictionary_t props
;
396 char filename
[MAXPATHLEN
];
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"));
408 ATF_CHECK(get_sysctl("vendor.k_helper.prop_int_load",
411 ATF_CHECK(get_sysctl("vendor.k_helper2.present",
415 unload("k_helper", true);
416 unload("k_helper2", true);
418 ATF_TC_CLEANUP(cmd_load_recurse
, tc
)
420 unload_cleanup("k_helper");
421 unload_cleanup("k_helper2");
424 ATF_TC_WITH_CLEANUP(cmd_stat
);
425 ATF_TC_HEAD(cmd_stat
, tc
)
427 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_STAT command");
428 atf_tc_set_md_var(tc
, "require.user", "root");
430 ATF_TC_BODY(cmd_stat
, tc
)
432 ATF_CHECK(!k_helper_is_present(both_checks
));
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
));
439 ATF_CHECK(get_modstat_info("k_helper", &ms
));
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);
445 unload("k_helper", true);
447 ATF_CHECK(!k_helper_is_present(both_checks
));
449 ATF_TC_CLEANUP(cmd_stat
, tc
)
451 unload_cleanup("k_helper");
454 ATF_TC_WITH_CLEANUP(cmd_unload
);
455 ATF_TC_HEAD(cmd_unload
, tc
)
457 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_UNLOAD command");
458 atf_tc_set_md_var(tc
, "require.user", "root");
460 ATF_TC_BODY(cmd_unload
, tc
)
462 load(NULL
, true, "%s/k_helper/k_helper.kmod",
463 atf_tc_get_config_var(tc
, "srcdir"));
465 ATF_CHECK(unload("", false) == ENOENT
);
466 ATF_CHECK(unload("non-existent.kmod", false) == ENOENT
);
467 ATF_CHECK(unload("k_helper.kmod", false) == ENOENT
);
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
));
474 ATF_TC_CLEANUP(cmd_unload
, tc
)
476 unload_cleanup("k_helper");
479 /* --------------------------------------------------------------------- */
481 /* --------------------------------------------------------------------- */
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
);
492 return atf_no_error();