1 /* $NetBSD: t_modctl.c,v 1.2 2008/05/02 14:20:50 ad 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.2 2008/05/02 14:20:50 ad Exp $");
32 #include <sys/module.h>
33 #include <sys/sysctl.h>
43 #include <prop/proplib.h>
47 static bool have_modular
= false;
49 enum presence_check
{ both_checks
, stat_check
, sysctl_check
};
51 /* --------------------------------------------------------------------- */
52 /* Auxiliary functions */
53 /* --------------------------------------------------------------------- */
56 * Checks if the kernel has 'options MODULAR' built into it and returns
57 * a boolean indicating this condition. This function must be called
58 * during the test program's initialization and the result be stored
59 * globally for further (efficient) usage of require_modular().
71 if (modctl(MODCTL_STAT
, &iov
) == 0)
74 res
= (errno
!= ENOSYS
);
80 * Makes sure that the kernel has 'options MODULAR' built into it and
81 * skips the test otherwise. Cannot be called unless check_modular()
82 * has been executed before.
90 atf_tc_skip("Kernel does not have 'options MODULAR'.");
95 get_modstat_info(const char *name
, modstat_t
*msdest
)
102 for (len
= 4096; ;) {
103 iov
.iov_base
= malloc(len
);
105 if (modctl(MODCTL_STAT
, &iov
) != 0) {
107 fprintf(stderr
, "modctl(MODCTL_STAT) failed: %s\n",
109 atf_tc_fail("Failed to query module status");
111 if (len
>= iov
.iov_len
)
118 len
= iov
.iov_len
/ sizeof(modstat_t
);
119 for (ms
= (modstat_t
*)iov
.iov_base
; len
!= 0 && !found
;
121 if (strcmp(ms
->ms_name
, name
) == 0) {
134 * Queries a sysctl property.
138 get_sysctl(const char *name
, void *buf
, const size_t len
)
141 printf("Querying sysctl variable: %s\n", name
);
142 int ret
= sysctlbyname(name
, buf
, &len2
, NULL
, 0);
143 if (ret
== -1 && errno
!= ENOENT
) {
144 fprintf(stderr
, "sysctlbyname(2) failed: %s\n",
146 atf_tc_fail("Failed to query %s", name
);
152 * Returns a boolean indicating if the k_helper module was loaded
153 * successfully. This implementation uses modctl(2)'s MODCTL_STAT
154 * subcommand to do the check.
158 k_helper_is_present_stat(void)
161 return get_modstat_info("k_helper", NULL
);
165 * Returns a boolean indicating if the k_helper module was loaded
166 * successfully. This implementation uses the module's sysctl
167 * installed node to do the check.
171 k_helper_is_present_sysctl(void)
175 return get_sysctl("vendor.k_helper.present", &present
,
180 * Returns a boolean indicating if the k_helper module was loaded
181 * successfully. The 'how' parameter specifies the implementation to
182 * use to do the check.
186 k_helper_is_present(enum presence_check how
)
192 found
= k_helper_is_present_stat();
193 ATF_CHECK(k_helper_is_present_sysctl() == found
);
197 found
= k_helper_is_present_stat();
201 found
= k_helper_is_present_sysctl();
212 * Loads the specified module from a file. If fatal is set and an error
213 * occurs when loading the module, an error message is printed and the
214 * test case is aborted.
218 load(prop_dictionary_t props
, bool fatal
, const char *fmt
, ...)
222 char filename
[MAXPATHLEN
], *propsstr
;
226 props
= prop_dictionary_create();
227 propsstr
= prop_dictionary_externalize(props
);
228 ATF_CHECK(propsstr
!= NULL
);
229 prop_object_release(props
);
231 propsstr
= prop_dictionary_externalize(props
);
232 ATF_CHECK(propsstr
!= NULL
);
236 vsnprintf(filename
, sizeof(filename
), fmt
, ap
);
239 ml
.ml_filename
= filename
;
241 ml
.ml_props
= propsstr
;
242 ml
.ml_propslen
= strlen(propsstr
);
244 printf("Loading module %s\n", filename
);
246 if (modctl(MODCTL_LOAD
, &ml
) == -1) {
248 fprintf(stderr
, "modctl(MODCTL_LOAD, %s), failed: %s\n",
249 filename
, strerror(err
));
251 atf_tc_fail("Module load failed");
260 * Unloads the specified module. If silent is true, nothing will be
261 * printed and no errors will be raised if the unload was unsuccessful.
265 unload(const char *name
, bool fatal
)
269 printf("Unloading module %s\n", name
);
271 if (modctl(MODCTL_UNLOAD
, __UNCONST(name
)) == -1) {
273 fprintf(stderr
, "modctl(MODCTL_UNLOAD, %s) failed: %s\n",
274 name
, strerror(err
));
276 atf_tc_fail("Module unload failed");
282 * A silent version of unload, to be called as part of the cleanup
287 unload_cleanup(const char *name
)
290 (void)modctl(MODCTL_UNLOAD
, __UNCONST(name
));
293 /* --------------------------------------------------------------------- */
295 /* --------------------------------------------------------------------- */
297 ATF_TC_WITH_CLEANUP(cmd_load
);
298 ATF_TC_HEAD(cmd_load
, tc
)
300 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_LOAD command");
301 atf_tc_set_md_var(tc
, "require.user", "root");
303 ATF_TC_BODY(cmd_load
, tc
)
305 char longname
[MAXPATHLEN
];
310 ATF_CHECK(load(NULL
, false, "") == ENOENT
);
311 ATF_CHECK(load(NULL
, false, "non-existent.o") == ENOENT
);
313 for (i
= 0; i
< MAXPATHLEN
- 1; i
++)
315 longname
[MAXPATHLEN
- 1] = '\0';
316 ATF_CHECK(load(NULL
, false, longname
) == ENAMETOOLONG
);
318 ATF_CHECK(!k_helper_is_present(stat_check
));
319 load(NULL
, true, "%s/k_helper/k_helper.kmod",
320 atf_tc_get_config_var(tc
, "srcdir"));
321 printf("Checking if load was successful\n");
322 ATF_CHECK(k_helper_is_present(stat_check
));
324 ATF_TC_CLEANUP(cmd_load
, tc
)
326 unload_cleanup("k_helper");
329 ATF_TC_WITH_CLEANUP(cmd_load_props
);
330 ATF_TC_HEAD(cmd_load_props
, tc
)
332 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_LOAD command, "
333 "providing extra load-time properties");
334 atf_tc_set_md_var(tc
, "require.user", "root");
336 ATF_TC_BODY(cmd_load_props
, tc
)
338 prop_dictionary_t props
;
342 printf("Loading module without properties\n");
343 props
= prop_dictionary_create();
344 load(props
, true, "%s/k_helper/k_helper.kmod",
345 atf_tc_get_config_var(tc
, "srcdir"));
346 prop_object_release(props
);
349 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
353 unload("k_helper", true);
355 printf("Loading module with a string property\n");
356 props
= prop_dictionary_create();
357 prop_dictionary_set(props
, "prop_str",
358 prop_string_create_cstring("1st string"));
359 load(props
, true, "%s/k_helper/k_helper.kmod",
360 atf_tc_get_config_var(tc
, "srcdir"));
361 prop_object_release(props
);
364 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
369 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
371 ATF_CHECK(strcmp(val
, "1st string") == 0);
373 unload("k_helper", true);
375 printf("Loading module with a different string property\n");
376 props
= prop_dictionary_create();
377 prop_dictionary_set(props
, "prop_str",
378 prop_string_create_cstring("2nd string"));
379 load(props
, true, "%s/k_helper/k_helper.kmod",
380 atf_tc_get_config_var(tc
, "srcdir"));
381 prop_object_release(props
);
384 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
389 ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
391 ATF_CHECK(strcmp(val
, "2nd string") == 0);
393 unload("k_helper", true);
395 ATF_TC_CLEANUP(cmd_load_props
, tc
)
397 unload_cleanup("k_helper");
400 ATF_TC_WITH_CLEANUP(cmd_stat
);
401 ATF_TC_HEAD(cmd_stat
, tc
)
403 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_STAT command");
404 atf_tc_set_md_var(tc
, "require.user", "root");
406 ATF_TC_BODY(cmd_stat
, tc
)
410 ATF_CHECK(!k_helper_is_present(both_checks
));
412 load(NULL
, true, "%s/k_helper/k_helper.kmod",
413 atf_tc_get_config_var(tc
, "srcdir"));
414 ATF_CHECK(k_helper_is_present(both_checks
));
417 ATF_CHECK(get_modstat_info("k_helper", &ms
));
419 ATF_CHECK(ms
.ms_class
== MODULE_CLASS_MISC
);
420 ATF_CHECK(ms
.ms_source
== MODULE_SOURCE_FILESYS
);
421 ATF_CHECK(ms
.ms_refcnt
== 0);
423 unload("k_helper", true);
425 ATF_CHECK(!k_helper_is_present(both_checks
));
427 ATF_TC_CLEANUP(cmd_stat
, tc
)
429 unload_cleanup("k_helper");
432 ATF_TC_WITH_CLEANUP(cmd_unload
);
433 ATF_TC_HEAD(cmd_unload
, tc
)
435 atf_tc_set_md_var(tc
, "descr", "Tests for the MODCTL_UNLOAD command");
436 atf_tc_set_md_var(tc
, "require.user", "root");
438 ATF_TC_BODY(cmd_unload
, tc
)
442 load(NULL
, true, "%s/k_helper/k_helper.kmod",
443 atf_tc_get_config_var(tc
, "srcdir"));
445 ATF_CHECK(unload("", false) == ENOENT
);
446 ATF_CHECK(unload("non-existent.kmod", false) == ENOENT
);
447 ATF_CHECK(unload("k_helper.kmod", false) == ENOENT
);
449 ATF_CHECK(k_helper_is_present(stat_check
));
450 unload("k_helper", true);
451 printf("Checking if unload was successful\n");
452 ATF_CHECK(!k_helper_is_present(stat_check
));
454 ATF_TC_CLEANUP(cmd_unload
, tc
)
456 unload_cleanup("k_helper");
459 /* --------------------------------------------------------------------- */
461 /* --------------------------------------------------------------------- */
465 have_modular
= check_modular();
467 ATF_TP_ADD_TC(tp
, cmd_load
);
468 ATF_TP_ADD_TC(tp
, cmd_load_props
);
469 ATF_TP_ADD_TC(tp
, cmd_stat
);
470 ATF_TP_ADD_TC(tp
, cmd_unload
);
472 return atf_no_error();