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