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