/* $NetBSD: t_modctl.c,v 1.16 2020/02/22 19:54:34 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.
*/
/*
* A function checking whether 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);
}
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 ) {
memcpy(&m, ms, sizeof(m));
if (strcmp(m.ms_name, name) == 0) {
if (msdest != NULL)
memcpy(msdest, &m, sizeof(*msdest));
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);
}
/*
* 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;
/*
* 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 all_checks:
found = k_helper_is_present_stat();
ATF_CHECK(k_helper_is_present_sysctl() == found);
ATF_CHECK(k_helper_is_present_evcnt() == found);
break;
case stat_check:
found = k_helper_is_present_stat();
break;
case sysctl_check:
found = k_helper_is_present_sysctl();
break;
case evcnt_check:
found = k_helper_is_present_evcnt();
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;
/*
* 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;