diff --git a/tests/lib/libc/stdio/t_fopen.c b/tests/lib/libc/stdio/t_fopen.c index 9fff6ef315bd..b0cf7ca970ee 100644 --- a/tests/lib/libc/stdio/t_fopen.c +++ b/tests/lib/libc/stdio/t_fopen.c @@ -1,606 +1,566 @@ /* $NetBSD: t_fopen.c,v 1.6 2019/02/05 17:30:19 kamil Exp $ */ /*- * Copyright (c) 2011 The NetBSD Foundation, Inc. * All rights reserved. * * This code is derived from software contributed to The NetBSD Foundation * by Jukka Ruohonen. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include __RCSID("$NetBSD: t_fopen.c,v 1.6 2019/02/05 17:30:19 kamil Exp $"); #include #include #include #include #include #include #include #include #include #include #include #include +#include "../../../modules/h_get_modstat_info.h" + static const char *path = "fopen"; ATF_TC_WITH_CLEANUP(fdopen_close); ATF_TC_HEAD(fdopen_close, tc) { atf_tc_set_md_var(tc, "descr", "See that descriptors are closed"); } ATF_TC_BODY(fdopen_close, tc) { FILE *f; int fd; /* * Check that the file descriptor * used to fdopen(3) a stream is * closed once the stream is closed. */ fd = open(path, O_RDWR | O_CREAT); ATF_REQUIRE(fd >= 0); f = fdopen(fd, "w+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fclose(f) == 0); ATF_REQUIRE(close(fd) == -1); ATF_REQUIRE(unlink(path) == 0); } ATF_TC_CLEANUP(fdopen_close, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(fdopen_err); ATF_TC_HEAD(fdopen_err, tc) { atf_tc_set_md_var(tc, "descr", "Test errors from fdopen(3)"); } ATF_TC_BODY(fdopen_err, tc) { int fd; fd = open(path, O_RDONLY | O_CREAT); ATF_REQUIRE(fd >= 0); errno = 0; ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "w") == NULL); errno = 0; ATF_REQUIRE_ERRNO(EINVAL, fdopen(fd, "a") == NULL); ATF_REQUIRE(close(fd) == 0); errno = 0; ATF_REQUIRE_ERRNO(EBADF, fdopen(fd, "r") == NULL); errno = 0; ATF_REQUIRE_ERRNO(EBADF, fdopen(-1, "w+") == NULL); (void)unlink(path); } ATF_TC_CLEANUP(fdopen_err, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(fdopen_seek); ATF_TC_HEAD(fdopen_seek, tc) { atf_tc_set_md_var(tc, "descr", "Test stream position with fdopen(3)"); } ATF_TC_BODY(fdopen_seek, tc) { FILE *f; int fd; /* * Verify that the file position associated * with the stream corresponds with the offset * set earlier for the file descriptor. */ fd = open(path, O_RDWR | O_CREAT); ATF_REQUIRE(fd >= 0); ATF_REQUIRE(write(fd, "garbage", 7) == 7); ATF_REQUIRE(lseek(fd, 3, SEEK_SET) == 3); f = fdopen(fd, "r+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(ftell(f) == 3); ATF_REQUIRE(fclose(f) == 0); ATF_REQUIRE(unlink(path) == 0); } ATF_TC_CLEANUP(fdopen_seek, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(fopen_err); ATF_TC_HEAD(fopen_err, tc) { atf_tc_set_md_var(tc, "descr", "Test errors from fopen(3)"); } ATF_TC_BODY(fopen_err, tc) { static const char *mode[] = { "x", "xr", "xr", "+r+", "R", "W+", " aXX", "Xr", " r+", "" }; char buf[PATH_MAX + 1]; size_t i; FILE *f; f = fopen(path, "w+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fclose(f) == 0); /* * Note that also "invalid" characters * may follow the mode-string whenever * the first character is valid. */ for (i = 0; i < __arraycount(mode); i++) { errno = 0; f = fopen(path, mode[i]); if (f == NULL && errno == EINVAL) continue; if (f != NULL) (void)fclose(f); atf_tc_fail_nonfatal("opened file as '%s'", mode[i]); } (void)unlink(path); (void)memset(buf, 'x', sizeof(buf)); errno = 0; ATF_REQUIRE_ERRNO(EISDIR, fopen("/usr/bin", "w") == NULL); errno = 0; ATF_REQUIRE_ERRNO(ENOENT, fopen("/a/b/c/d/e/f", "r") == NULL); errno = 0; ATF_REQUIRE_ERRNO(ENAMETOOLONG, fopen(buf, "r+") == NULL); } ATF_TC_CLEANUP(fopen_err, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(fopen_append); ATF_TC_HEAD(fopen_append, tc) { atf_tc_set_md_var(tc, "descr", "Test that append-mode works"); } ATF_TC_BODY(fopen_append, tc) { char buf[15]; FILE *f; (void)memset(buf, 'x', sizeof(buf)); f = fopen(path, "w+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7); ATF_REQUIRE(fclose(f) == 0); f = fopen(path, "a"); ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7); ATF_REQUIRE(fclose(f) == 0); f = fopen(path, "r"); ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14); ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0); ATF_REQUIRE(fclose(f) == 0); ATF_REQUIRE(unlink(path) == 0); } ATF_TC_CLEANUP(fopen_append, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(fopen_mode); ATF_TC_HEAD(fopen_mode, tc) { atf_tc_set_md_var(tc, "descr", "Test fopen(3) modes"); } ATF_TC_BODY(fopen_mode, tc) { size_t i; FILE *f; static const char *mode[] = { "r", "r+", "w", "w+", "a", "a+", "rb", "r+b", "wb", "w+b", "ab", "a+b", "re", "r+e", "we", "w+e", "ae", "a+e", "rf", "r+f", "wf", "w+f", "af", "a+f", "rl", "r+l", "wl", "w+l", "al", "a+l" }; f = fopen(path, "w+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fclose(f) == 0); /* * Verify that various modes work. */ for (i = 0; i < __arraycount(mode); i++) { f = fopen(path, mode[i]); if (f != NULL) { ATF_REQUIRE(fclose(f) == 0); continue; } atf_tc_fail_nonfatal("failed to open file as %s", mode[i]); } (void)unlink(path); } ATF_TC_CLEANUP(fopen_mode, tc) { (void)unlink(path); } -static void -check_kernel_modular(void) -{ - int err; - - err = modctl(MODCTL_EXISTS, 0); - if (err == 0) return; - if (errno == ENOSYS) - atf_tc_skip("Kernel does not have 'options MODULAR'."); - if (errno == EPERM) - return; /* Module loading can be administratively forbidden */ - ATF_REQUIRE_EQ_MSG(errno, 0, "unexpected error %d from " - "modctl(MODCTL_EXISTS, 0)", errno); -} - -static bool -is_module_present(const char *name) -{ - bool found; - size_t len; - int count; - struct iovec iov; - modstat_t *ms; - - for (len = 8192; ;) { - iov.iov_base = malloc(len); - iov.iov_len = len; - - errno = 0; - - if (modctl(MODCTL_STAT, &iov) != 0) { - fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n", - strerror(errno)); - atf_tc_fail("Failed to query module status"); - } - if (len >= iov.iov_len) - break; - free(iov.iov_base); - len = iov.iov_len; - } - - found = false; - count = *(int *)iov.iov_base; - ms = (modstat_t *)((char *)iov.iov_base + sizeof(int)); - while (count > 0) { - if (strcmp(ms->ms_name, name) == 0) { - found = true; - break; - } - ms++; - count--; - } - - free(iov.iov_base); - - return found; -} - #define COMPAT10_MODNAME "compat_10" ATF_TC(fopen_nullptr); ATF_TC_HEAD(fopen_nullptr, tc) { atf_tc_set_md_var(tc, "descr", "Test fopen(3) with NULL path (without " COMPAT10_MODNAME ")"); } ATF_TC_BODY(fopen_nullptr, tc) { - bool compat10; - - check_kernel_modular(); - compat10 = is_module_present(COMPAT10_MODNAME); + int compat10; - if (compat10) + compat10 = get_modstat_info(COMPAT10_MODNAME, NULL); + switch (compat10) { + case 0: atf_tc_skip("Kernel does have the " COMPAT10_MODNAME " module loaded into the kernel"); + case ENOSYS: + atf_tc_skip("Kernel does not have 'options MODULAR'."); + case ENOENT: + /* Expected module absent */ + break; + default: + atf_tc_fail("Failed to query module " COMPAT10_MODNAME " '%s'", + strerror(compat10)); + } /* NULL shall trigger error */ ATF_REQUIRE_ERRNO(EFAULT, fopen(NULL, "r") == NULL); } ATF_TC(fopen_nullptr_compat10); ATF_TC_HEAD(fopen_nullptr_compat10, tc) { atf_tc_set_md_var(tc, "descr", "Test fopen(3) with NULL path (with " COMPAT10_MODNAME ")"); } ATF_TC_BODY(fopen_nullptr_compat10, tc) { FILE *fp; - bool compat10; - - check_kernel_modular(); - compat10 = is_module_present(COMPAT10_MODNAME); - - if (!compat10) + int compat10; + + compat10 = get_modstat_info(COMPAT10_MODNAME, NULL); + switch (compat10) { + case 0: + /* Expected module present */ + break; + case ENOSYS: + atf_tc_skip("Kernel does not have 'options MODULAR'."); + case ENOENT: atf_tc_skip("Kernel does not have the " COMPAT10_MODNAME " module loaded into the kernel"); + default: + atf_tc_fail("Failed to query module " COMPAT10_MODNAME " '%s'", + strerror(compat10)); + } /* NULL is translated to "." and shall success */ fp = fopen(NULL, "r"); ATF_REQUIRE(fp != NULL); ATF_REQUIRE(fclose(fp) == 0); } ATF_TC(fopen_perm); ATF_TC_HEAD(fopen_perm, tc) { atf_tc_set_md_var(tc, "descr", "Test permissions with fopen(3)"); atf_tc_set_md_var(tc, "require.user", "unprivileged"); } ATF_TC_BODY(fopen_perm, tc) { errno = 0; ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "a+") == NULL); errno = 0; ATF_REQUIRE_ERRNO(EACCES, fopen("/bin/ls", "w+") == NULL); } ATF_TC(fopen_regular); ATF_TC_HEAD(fopen_regular, tc) { atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'f' mode"); } ATF_TC_BODY(fopen_regular, tc) { static const char *mode[] = { "rf", "r+f", "wf", "w+f", "af", "a+f" }; static const char *devs[] = { _PATH_DEVNULL }; size_t i, j; FILE *f; for (i = 0; i < __arraycount(devs); i++) { for (j = 0; j < __arraycount(mode); j++) { errno = 0; f = fopen(devs[i], mode[j]); if (f == NULL && errno == EFTYPE) continue; if (f != NULL) { (void)fclose(f); atf_tc_fail_nonfatal("opened %s as %s", devs[i], mode[j]); } else { atf_tc_fail_nonfatal( "err %d (%s) from open of %s as %s", errno, strerror(errno), devs[i], mode[j]); } } } } static char linkpath[] = "symlink"; ATF_TC_WITH_CLEANUP(fopen_symlink); ATF_TC_HEAD(fopen_symlink, tc) { atf_tc_set_md_var(tc, "descr", "Test fopen(3) with 'l' mode"); } ATF_TC_BODY(fopen_symlink, tc) { static const char *mode[] = { "rl", "r+l", "wl", "w+l", "al", "a+l" }; size_t j; FILE *f; ATF_CHECK(symlink("/dev/null", linkpath) != -1); for (j = 0; j < __arraycount(mode); j++) { errno = 0; f = fopen(linkpath, mode[j]); if (f == NULL && errno == EFTYPE) continue; if (f != NULL) { (void)fclose(f); atf_tc_fail_nonfatal("opened %s as %s", linkpath, mode[j]); } else { atf_tc_fail_nonfatal( "err %d (%s) from open of %s as %s", errno, strerror(errno), linkpath, mode[j]); } } ATF_REQUIRE(unlink(linkpath) == 0); } ATF_TC_CLEANUP(fopen_symlink, tc) { (void)unlink(linkpath); } ATF_TC_WITH_CLEANUP(fopen_seek); ATF_TC_HEAD(fopen_seek, tc) { atf_tc_set_md_var(tc, "descr", "Test initial stream position"); } ATF_TC_BODY(fopen_seek, tc) { FILE *f; f = fopen(path, "w+"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7); ATF_REQUIRE(fclose(f) == 0); /* * The position of the stream should be * at the start, except for append-mode. */ f = fopen(path, "r"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(ftello(f) == 0); ATF_REQUIRE(fclose(f) == 0); f = fopen(path, "a"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(ftello(f) == 7); ATF_REQUIRE(fclose(f) == 0); ATF_REQUIRE(unlink(path) == 0); } ATF_TC_CLEANUP(fopen_seek, tc) { (void)unlink(path); } ATF_TC_WITH_CLEANUP(freopen_std); ATF_TC_HEAD(freopen_std, tc) { atf_tc_set_md_var(tc, "descr", "A basic test of freopen(3)"); } ATF_TC_BODY(freopen_std, tc) { FILE *std[2] = { stdin, stdout }; char buf[15]; size_t i; FILE *f; /* * Associate a standard stream with a custom stream. * Then write to the standard stream and verify that * the result now appears in the custom stream. */ for (i = 0; i < __arraycount(std); i++) { (void)memset(buf, 'x', sizeof(buf)); f = fopen(path, "w+"); ATF_REQUIRE(f != NULL); f = freopen(path, "w+", std[i]); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fwrite("garbage", 1, 7, f) == 7); ATF_REQUIRE(fprintf(std[i], "garbage") == 7); ATF_REQUIRE(fclose(f) == 0); f = fopen(path, "r"); ATF_REQUIRE(f != NULL); ATF_REQUIRE(fread(buf, 1, sizeof(buf), f) == 14); ATF_REQUIRE(strncmp(buf, "garbagegarbage", 14) == 0); ATF_REQUIRE(fclose(f) == 0); } ATF_REQUIRE(unlink(path) == 0); } ATF_TC_CLEANUP(freopen_std, tc) { (void)unlink(path); } ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, fdopen_close); ATF_TP_ADD_TC(tp, fdopen_err); ATF_TP_ADD_TC(tp, fdopen_seek); ATF_TP_ADD_TC(tp, fopen_append); ATF_TP_ADD_TC(tp, fopen_err); ATF_TP_ADD_TC(tp, fopen_mode); ATF_TP_ADD_TC(tp, fopen_nullptr); ATF_TP_ADD_TC(tp, fopen_nullptr_compat10); ATF_TP_ADD_TC(tp, fopen_perm); ATF_TP_ADD_TC(tp, fopen_regular); ATF_TP_ADD_TC(tp, fopen_symlink); ATF_TP_ADD_TC(tp, fopen_seek); ATF_TP_ADD_TC(tp, freopen_std); return atf_no_error(); } diff --git a/tests/modules/h_get_modstat_info.h b/tests/modules/h_get_modstat_info.h new file mode 100644 index 000000000000..70531a37faf6 --- /dev/null +++ b/tests/modules/h_get_modstat_info.h @@ -0,0 +1,105 @@ +/* $NetBSD$ */ +/* + * Copyright (c) 2008 The NetBSD Foundation, Inc. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND + * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE + * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _GET_MODSTAT_INFO_H_ +#define _GET_MODSTAT_INFO_H_ + +#include +#include + +#include +#include +#include +#include +#include + +static int +get_modstat_info(const char *name, modstat_t *msdest) +{ + bool found; + size_t len; + int count; + struct iovec iov; + modstat_t *ms; + int saved_errno; + int rv; + + saved_errno = errno; + + if (modctl(MODCTL_EXISTS, 0) != 0 && errno != EPERM) { + rv = errno; + goto fini; + } + + for (len = 8192; ;) { + iov.iov_base = malloc(len); + if (iov.iov_base == NULL) { + rv = errno; + goto fini; + } + iov.iov_len = len; + + if (modctl(MODCTL_STAT, &iov) != 0) { + rv = errno; + free(iov.iov_base); + goto fini; + } + if (len >= iov.iov_len) + break; + free(iov.iov_base); + len = iov.iov_len; + } + + found = false; + count = *(int *)iov.iov_base; + ms = (modstat_t *)((char *)iov.iov_base + sizeof(int)); + while (count > 0) { + if (strncmp(ms->ms_name, name, MAXMODNAME) == 0) { + if (msdest != NULL) + *msdest = *ms; + found = true; + break; + } + ms++; + count--; + } + + free(iov.iov_base); + + if (found) + rv = 0; + else + rv = ENOENT; + +fini: + errno = saved_errno; + + return rv; +} + +#endif /* _GET_MODSTAT_INFO_H_ */ diff --git a/tests/modules/t_modctl.c b/tests/modules/t_modctl.c index 43ac6d4aea1a..f9a52661e14b 100644 --- a/tests/modules/t_modctl.c +++ b/tests/modules/t_modctl.c @@ -1,497 +1,451 @@ /* $NetBSD: t_modctl.c,v 1.13 2019/01/27 02:08:50 pgoyette Exp $ */ /* * Copyright (c) 2008 The NetBSD Foundation, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include __KERNEL_RCSID(0, "$NetBSD: t_modctl.c,v 1.13 2019/01/27 02:08:50 pgoyette Exp $"); #include #include #include #include #include #include #include #include #include #include #include +#include "h_get_modstat_info.h" + enum presence_check { both_checks, stat_check, sysctl_check }; static void check_permission(void); -static bool get_modstat_info(const char *, modstat_t *); static bool get_sysctl(const char *, void *buf, const size_t); static bool k_helper_is_present_stat(void); static bool k_helper_is_present_sysctl(void); static bool k_helper_is_present(enum presence_check); static int load(prop_dictionary_t, bool, const char *, ...); static int unload(const char *, bool); static void unload_cleanup(const char *); /* --------------------------------------------------------------------- */ /* Auxiliary functions */ /* --------------------------------------------------------------------- */ /* * A function checking wether we are allowed to load modules currently * (either the kernel is not modular, or securelevel may prevent it) */ static void check_permission(void) { int err; err = modctl(MODCTL_EXISTS, 0); if (err == 0) return; if (errno == ENOSYS) atf_tc_skip("Kernel does not have 'options MODULAR'."); else if (errno == EPERM) atf_tc_skip("Module loading administratively forbidden"); ATF_REQUIRE_EQ_MSG(errno, 0, "unexpected error %d from " "modctl(MODCTL_EXISTS, 0)", errno); } -static bool -get_modstat_info(const char *name, modstat_t *msdest) -{ - bool found; - size_t len; - int count; - struct iovec iov; - modstat_t *ms; - - check_permission(); - for (len = 8192; ;) { - iov.iov_base = malloc(len); - iov.iov_len = len; - - errno = 0; - - if (modctl(MODCTL_STAT, &iov) != 0) { - int err = errno; - fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n", - strerror(err)); - atf_tc_fail("Failed to query module status"); - } - if (len >= iov.iov_len) - break; - free(iov.iov_base); - len = iov.iov_len; - } - - found = false; - count = *(int *)iov.iov_base; - ms = (modstat_t *)((char *)iov.iov_base + sizeof(int)); - while ( count ) { - if (strcmp(ms->ms_name, name) == 0) { - if (msdest != NULL) - *msdest = *ms; - found = true; - break; - } - ms++; - count--; - } - - free(iov.iov_base); - - return found; -} - /* * Queries a sysctl property. */ static bool get_sysctl(const char *name, void *buf, const size_t len) { size_t len2 = len; printf("Querying sysctl variable: %s\n", name); int ret = sysctlbyname(name, buf, &len2, NULL, 0); if (ret == -1 && errno != ENOENT) { fprintf(stderr, "sysctlbyname(2) failed: %s\n", strerror(errno)); atf_tc_fail("Failed to query %s", name); } return ret != -1; } /* * Returns a boolean indicating if the k_helper module was loaded * successfully. This implementation uses modctl(2)'s MODCTL_STAT * subcommand to do the check. */ static bool k_helper_is_present_stat(void) { - return get_modstat_info("k_helper", NULL); + return get_modstat_info("k_helper", NULL) == 0; } /* * Returns a boolean indicating if the k_helper module was loaded * successfully. This implementation uses the module's sysctl * installed node to do the check. */ static bool k_helper_is_present_sysctl(void) { size_t present; return get_sysctl("vendor.k_helper.present", &present, sizeof(present)); } /* * Returns a boolean indicating if the k_helper module was loaded * successfully. The 'how' parameter specifies the implementation to * use to do the check. */ static bool k_helper_is_present(enum presence_check how) { bool found; switch (how) { case both_checks: found = k_helper_is_present_stat(); ATF_CHECK(k_helper_is_present_sysctl() == found); break; case stat_check: found = k_helper_is_present_stat(); break; case sysctl_check: found = k_helper_is_present_sysctl(); break; default: found = false; assert(found); } return found; } /* * Loads the specified module from a file. If fatal is set and an error * occurs when loading the module, an error message is printed and the * test case is aborted. */ static __printflike(3, 4) int load(prop_dictionary_t props, bool fatal, const char *fmt, ...) { int err; va_list ap; char filename[MAXPATHLEN], *propsstr; modctl_load_t ml; check_permission(); if (props == NULL) { props = prop_dictionary_create(); propsstr = prop_dictionary_externalize(props); ATF_CHECK(propsstr != NULL); prop_object_release(props); } else { propsstr = prop_dictionary_externalize(props); ATF_CHECK(propsstr != NULL); } va_start(ap, fmt); vsnprintf(filename, sizeof(filename), fmt, ap); va_end(ap); ml.ml_filename = filename; ml.ml_flags = 0; ml.ml_props = propsstr; ml.ml_propslen = strlen(propsstr); printf("Loading module %s\n", filename); errno = err = 0; if (modctl(MODCTL_LOAD, &ml) == -1) { err = errno; fprintf(stderr, "modctl(MODCTL_LOAD, %s), failed: %s\n", filename, strerror(err)); if (fatal) atf_tc_fail("Module load failed"); } free(propsstr); return err; } /* * Unloads the specified module. If silent is true, nothing will be * printed and no errors will be raised if the unload was unsuccessful. */ static int unload(const char *name, bool fatal) { int err; check_permission(); printf("Unloading module %s\n", name); errno = err = 0; if (modctl(MODCTL_UNLOAD, __UNCONST(name)) == -1) { err = errno; fprintf(stderr, "modctl(MODCTL_UNLOAD, %s) failed: %s\n", name, strerror(err)); if (fatal) atf_tc_fail("Module unload failed"); } return err; } /* * A silent version of unload, to be called as part of the cleanup * process only. */ static void unload_cleanup(const char *name) { (void)modctl(MODCTL_UNLOAD, __UNCONST(name)); } /* --------------------------------------------------------------------- */ /* Test cases */ /* --------------------------------------------------------------------- */ ATF_TC_WITH_CLEANUP(cmd_load); ATF_TC_HEAD(cmd_load, tc) { atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cmd_load, tc) { char longname[MAXPATHLEN]; size_t i; ATF_CHECK(load(NULL, false, " ") == ENOENT); ATF_CHECK(load(NULL, false, "non-existent.o") == ENOENT); for (i = 0; i < MAXPATHLEN - 1; i++) longname[i] = 'a'; longname[MAXPATHLEN - 1] = '\0'; ATF_CHECK(load(NULL, false, "%s", longname) == ENAMETOOLONG); ATF_CHECK(!k_helper_is_present(stat_check)); load(NULL, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); printf("Checking if load was successful\n"); ATF_CHECK(k_helper_is_present(stat_check)); } ATF_TC_CLEANUP(cmd_load, tc) { unload_cleanup("k_helper"); } ATF_TC_WITH_CLEANUP(cmd_load_props); ATF_TC_HEAD(cmd_load_props, tc) { atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command, " "providing extra load-time properties"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cmd_load_props, tc) { prop_dictionary_t props; printf("Loading module without properties\n"); props = prop_dictionary_create(); load(props, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); prop_object_release(props); { int ok; ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok", &ok, sizeof(ok))); ATF_CHECK(!ok); } unload("k_helper", true); printf("Loading module with a string property\n"); props = prop_dictionary_create(); prop_dictionary_set(props, "prop_str", prop_string_create_cstring("1st string")); load(props, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); prop_object_release(props); { int ok; ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok", &ok, sizeof(ok))); ATF_CHECK(ok); char val[128]; ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val", &val, sizeof(val))); ATF_CHECK(strcmp(val, "1st string") == 0); } unload("k_helper", true); printf("Loading module with a different string property\n"); props = prop_dictionary_create(); prop_dictionary_set(props, "prop_str", prop_string_create_cstring("2nd string")); load(props, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); prop_object_release(props); { int ok; ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok", &ok, sizeof(ok))); ATF_CHECK(ok); char val[128]; ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val", &val, sizeof(val))); ATF_CHECK(strcmp(val, "2nd string") == 0); } unload("k_helper", true); } ATF_TC_CLEANUP(cmd_load_props, tc) { unload_cleanup("k_helper"); } ATF_TC_WITH_CLEANUP(cmd_load_recurse); ATF_TC_HEAD(cmd_load_recurse, tc) { atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command, " "with recursive module_load()"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cmd_load_recurse, tc) { prop_dictionary_t props; char filename[MAXPATHLEN]; printf("Loading module with request to load another module\n"); props = prop_dictionary_create(); snprintf(filename, sizeof(filename), "%s/k_helper2/k_helper2.kmod", atf_tc_get_config_var(tc, "srcdir")); prop_dictionary_set(props, "prop_recurse", prop_string_create_cstring(filename)); load(props, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); { int ok; ATF_CHECK(get_sysctl("vendor.k_helper.prop_int_load", &ok, sizeof(ok))); ATF_CHECK(ok == 0); ATF_CHECK(get_sysctl("vendor.k_helper2.present", &ok, sizeof(ok))); ATF_CHECK(ok); } unload("k_helper", true); unload("k_helper2", true); } ATF_TC_CLEANUP(cmd_load_recurse, tc) { unload_cleanup("k_helper"); unload_cleanup("k_helper2"); } ATF_TC_WITH_CLEANUP(cmd_stat); ATF_TC_HEAD(cmd_stat, tc) { atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_STAT command"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cmd_stat, tc) { ATF_CHECK(!k_helper_is_present(both_checks)); load(NULL, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); ATF_CHECK(k_helper_is_present(both_checks)); { modstat_t ms; - ATF_CHECK(get_modstat_info("k_helper", &ms)); + ATF_CHECK(get_modstat_info("k_helper", &ms) == 0); ATF_CHECK(ms.ms_class == MODULE_CLASS_MISC); ATF_CHECK(ms.ms_source == MODULE_SOURCE_FILESYS); ATF_CHECK(ms.ms_refcnt == 0); } unload("k_helper", true); ATF_CHECK(!k_helper_is_present(both_checks)); } ATF_TC_CLEANUP(cmd_stat, tc) { unload_cleanup("k_helper"); } ATF_TC_WITH_CLEANUP(cmd_unload); ATF_TC_HEAD(cmd_unload, tc) { atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_UNLOAD command"); atf_tc_set_md_var(tc, "require.user", "root"); } ATF_TC_BODY(cmd_unload, tc) { load(NULL, true, "%s/k_helper/k_helper.kmod", atf_tc_get_config_var(tc, "srcdir")); ATF_CHECK(unload("", false) == ENOENT); ATF_CHECK(unload("non-existent.kmod", false) == ENOENT); ATF_CHECK(unload("k_helper.kmod", false) == ENOENT); ATF_CHECK(k_helper_is_present(stat_check)); unload("k_helper", true); printf("Checking if unload was successful\n"); ATF_CHECK(!k_helper_is_present(stat_check)); } ATF_TC_CLEANUP(cmd_unload, tc) { unload_cleanup("k_helper"); } /* --------------------------------------------------------------------- */ /* Main */ /* --------------------------------------------------------------------- */ ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, cmd_load); ATF_TP_ADD_TC(tp, cmd_load_props); ATF_TP_ADD_TC(tp, cmd_stat); ATF_TP_ADD_TC(tp, cmd_load_recurse); ATF_TP_ADD_TC(tp, cmd_unload); return atf_no_error(); }