Home | History | Annotate | Line # | Download | only in rumpkern
t_modcmd.c revision 1.7
      1  1.7  pooka /*	$NetBSD: t_modcmd.c,v 1.7 2010/05/01 12:11:53 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*
      4  1.1  pooka  * Copyright (c) 2009 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      8  1.1  pooka  * modification, are permitted provided that the following conditions
      9  1.1  pooka  * are met:
     10  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     15  1.1  pooka  *
     16  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1  pooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1  pooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1  pooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  pooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1  pooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1  pooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1  pooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  pooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1  pooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  pooka  */
     29  1.1  pooka 
     30  1.1  pooka #include <sys/types.h>
     31  1.1  pooka #include <sys/mount.h>
     32  1.6  pooka #include <sys/sysctl.h>
     33  1.1  pooka 
     34  1.1  pooka #include <rump/rump.h>
     35  1.1  pooka #include <rump/rump_syscalls.h>
     36  1.1  pooka 
     37  1.1  pooka #include <fs/tmpfs/tmpfs_args.h>
     38  1.1  pooka 
     39  1.1  pooka #include <atf-c.h>
     40  1.1  pooka #include <dlfcn.h>
     41  1.1  pooka #include <err.h>
     42  1.1  pooka #include <errno.h>
     43  1.1  pooka #include <stdio.h>
     44  1.1  pooka #include <stdlib.h>
     45  1.1  pooka #include <string.h>
     46  1.1  pooka #include <unistd.h>
     47  1.1  pooka #include <util.h>
     48  1.1  pooka 
     49  1.7  pooka #define USE_ATF
     50  1.7  pooka 
     51  1.7  pooka #ifdef USE_ATF
     52  1.7  pooka 
     53  1.1  pooka #include "../../h_macros.h"
     54  1.1  pooka /*
     55  1.1  pooka  * We verify that modules can be loaded and unloaded.
     56  1.1  pooka  * tmpfs was chosen because it does not depend on an image.
     57  1.1  pooka  */
     58  1.1  pooka 
     59  1.1  pooka ATF_TC(cmsg_modcmd);
     60  1.1  pooka ATF_TC_HEAD(cmsg_modcmd, tc)
     61  1.1  pooka {
     62  1.1  pooka 	atf_tc_set_md_var(tc, "descr", "Checks that loading and unloading "
     63  1.1  pooka 	    "a module (vfs/tmpfs) is possible");
     64  1.1  pooka }
     65  1.7  pooka #else
     66  1.7  pooka #define atf_tc_fail_errno(...) err(1, __VA_ARGS__)
     67  1.7  pooka #endif
     68  1.1  pooka 
     69  1.6  pooka static int
     70  1.6  pooka disable_autoload(void)
     71  1.6  pooka {
     72  1.6  pooka 	struct sysctlnode q, ans[256];
     73  1.6  pooka 	int mib[3];
     74  1.6  pooka 	size_t alen;
     75  1.6  pooka 	unsigned i;
     76  1.6  pooka 	bool no;
     77  1.6  pooka 
     78  1.6  pooka 	mib[0] = CTL_KERN;
     79  1.6  pooka 	mib[1] = CTL_QUERY;
     80  1.6  pooka 	alen = sizeof(ans);
     81  1.6  pooka 
     82  1.6  pooka 	memset(&q, 0, sizeof(q));
     83  1.6  pooka 	q.sysctl_flags = SYSCTL_VERSION;
     84  1.6  pooka 
     85  1.6  pooka 	if (rump_sys___sysctl(mib, 2, ans, &alen, &q, sizeof(q)) == -1)
     86  1.6  pooka 		return -1;
     87  1.6  pooka 
     88  1.6  pooka 	for (i = 0; i < __arraycount(ans); i++)
     89  1.6  pooka 		if (strcmp("module", ans[i].sysctl_name) == 0)
     90  1.6  pooka 			break;
     91  1.6  pooka 	if (i == __arraycount(ans)) {
     92  1.6  pooka 		errno = ENOENT;
     93  1.6  pooka 		return -1;
     94  1.6  pooka 	}
     95  1.6  pooka 
     96  1.6  pooka 	mib[1] = ans[i].sysctl_num;
     97  1.6  pooka 	mib[2] = CTL_QUERY;
     98  1.6  pooka 
     99  1.6  pooka 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1)
    100  1.6  pooka 		return errno;
    101  1.6  pooka 
    102  1.6  pooka 	for (i = 0; i < __arraycount(ans); i++)
    103  1.6  pooka 		if (strcmp("autoload", ans[i].sysctl_name) == 0)
    104  1.6  pooka 			break;
    105  1.6  pooka 	if (i == __arraycount(ans)) {
    106  1.6  pooka 		errno = ENOENT;
    107  1.6  pooka 		return -1;
    108  1.6  pooka 	}
    109  1.6  pooka 
    110  1.6  pooka 	mib[2] = ans[i].sysctl_num;
    111  1.6  pooka 
    112  1.6  pooka 	no = false;
    113  1.6  pooka 	alen = 0;
    114  1.6  pooka 	if (rump_sys___sysctl(mib, 3, NULL, &alen, &no, sizeof(no)) == -1)
    115  1.6  pooka 		return errno;
    116  1.6  pooka 
    117  1.6  pooka 	return 0;
    118  1.6  pooka 
    119  1.6  pooka }
    120  1.6  pooka 
    121  1.2  pooka #define TMPFSMODULE "librumpfs_tmpfs.so"
    122  1.7  pooka #ifdef USE_ATF
    123  1.1  pooka ATF_TC_BODY(cmsg_modcmd, tc)
    124  1.7  pooka #else
    125  1.7  pooka int main(int argc, char *argv[])
    126  1.7  pooka #endif
    127  1.1  pooka {
    128  1.1  pooka 	struct tmpfs_args args;
    129  1.5  pooka 	const struct modinfo *const *mi_start, *const *mi_end;
    130  1.1  pooka 	void *handle;
    131  1.5  pooka 	int i, rv, loop = 0;
    132  1.1  pooka 
    133  1.1  pooka 	rump_init();
    134  1.6  pooka 
    135  1.6  pooka 	if (disable_autoload() == -1)
    136  1.6  pooka 		atf_tc_fail_errno("count not disable module autoload");
    137  1.6  pooka 
    138  1.1  pooka 	memset(&args, 0, sizeof(args));
    139  1.1  pooka 	args.ta_version = TMPFS_ARGS_VERSION;
    140  1.1  pooka 	args.ta_root_mode = 0777;
    141  1.1  pooka 
    142  1.1  pooka 	if (rump_sys_mkdir("/mp", 0777) == -1)
    143  1.1  pooka 		atf_tc_fail_errno("mkdir mountpoint");
    144  1.1  pooka 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
    145  1.1  pooka 		atf_tc_fail("mount unexpectedly succeeded");
    146  1.1  pooka 
    147  1.1  pooka 	handle = dlopen(TMPFSMODULE, RTLD_GLOBAL);
    148  1.1  pooka 	if (handle == NULL) {
    149  1.1  pooka 		const char *dlmsg = dlerror();
    150  1.1  pooka 		atf_tc_fail("cannot open %s: %s", TMPFSMODULE, dlmsg);
    151  1.1  pooka 	}
    152  1.5  pooka 
    153  1.5  pooka  again:
    154  1.5  pooka 	mi_start = dlsym(handle, "__start_link_set_modules");
    155  1.5  pooka 	mi_end = dlsym(handle, "__stop_link_set_modules");
    156  1.5  pooka 	if (mi_start == NULL || mi_end == NULL)
    157  1.1  pooka 		atf_tc_fail("cannot find module info");
    158  1.5  pooka 	if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))!=0)
    159  1.1  pooka 		atf_tc_fail("module init failed: %d (%s)", rv, strerror(rv));
    160  1.5  pooka 	if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))==0)
    161  1.5  pooka 		atf_tc_fail("module double init succeeded");
    162  1.1  pooka 
    163  1.1  pooka 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) == -1)
    164  1.1  pooka 		atf_tc_fail_errno("still cannot mount");
    165  1.1  pooka 	if (rump_sys_unmount("/mp", 0) == -1)
    166  1.1  pooka 		atf_tc_fail("cannot unmount");
    167  1.5  pooka 	for (i = 0; i < (int)(mi_end-mi_start); i++) {
    168  1.5  pooka 		if ((rv = rump_pub_module_fini(mi_start[i])) != 0)
    169  1.5  pooka 			atf_tc_fail("module fini failed: %d (%s)",
    170  1.5  pooka 			    rv, strerror(rv));
    171  1.5  pooka 	}
    172  1.5  pooka 	for (i = 0; i < (int)(mi_end-mi_start); i++) {
    173  1.5  pooka 		if ((rv = rump_pub_module_fini(mi_start[i])) == 0)
    174  1.5  pooka 			atf_tc_fail("module double fini succeeded");
    175  1.5  pooka 	}
    176  1.5  pooka 	if (loop++ == 0)
    177  1.5  pooka 		goto again;
    178  1.5  pooka 
    179  1.1  pooka 	if (dlclose(handle)) {
    180  1.1  pooka 		const char *dlmsg = dlerror();
    181  1.1  pooka 		atf_tc_fail("cannot close %s: %s", TMPFSMODULE, dlmsg);
    182  1.1  pooka 	}
    183  1.1  pooka 
    184  1.1  pooka 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
    185  1.1  pooka 		atf_tc_fail("mount unexpectedly succeeded");
    186  1.1  pooka }
    187  1.1  pooka 
    188  1.7  pooka #ifdef USE_ATF
    189  1.1  pooka ATF_TP_ADD_TCS(tp)
    190  1.1  pooka {
    191  1.1  pooka 	ATF_TP_ADD_TC(tp, cmsg_modcmd);
    192  1.4  pooka 
    193  1.4  pooka 	return atf_no_error();
    194  1.1  pooka }
    195  1.7  pooka #endif
    196