Home | History | Annotate | Line # | Download | only in rumpkern
t_modcmd.c revision 1.8
      1 /*	$NetBSD: t_modcmd.c,v 1.8 2010/05/31 23:44:54 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  * We verify that modules can be loaded and unloaded.
     52  * tmpfs was chosen because it does not depend on an image.
     53  */
     54 
     55 ATF_TC(cmsg_modcmd);
     56 ATF_TC_HEAD(cmsg_modcmd, tc)
     57 {
     58 	atf_tc_set_md_var(tc, "descr", "Checks that loading and unloading "
     59 	    "a module (vfs/tmpfs) is possible");
     60 }
     61 #else
     62 #define atf_tc_fail_errno(...) err(1, __VA_ARGS__)
     63 #endif
     64 
     65 static int
     66 disable_autoload(void)
     67 {
     68 	struct sysctlnode q, ans[256];
     69 	int mib[3];
     70 	size_t alen;
     71 	unsigned i;
     72 	bool no;
     73 
     74 	mib[0] = CTL_KERN;
     75 	mib[1] = CTL_QUERY;
     76 	alen = sizeof(ans);
     77 
     78 	memset(&q, 0, sizeof(q));
     79 	q.sysctl_flags = SYSCTL_VERSION;
     80 
     81 	if (rump_sys___sysctl(mib, 2, ans, &alen, &q, sizeof(q)) == -1)
     82 		return -1;
     83 
     84 	for (i = 0; i < __arraycount(ans); i++)
     85 		if (strcmp("module", ans[i].sysctl_name) == 0)
     86 			break;
     87 	if (i == __arraycount(ans)) {
     88 		errno = ENOENT;
     89 		return -1;
     90 	}
     91 
     92 	mib[1] = ans[i].sysctl_num;
     93 	mib[2] = CTL_QUERY;
     94 
     95 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1)
     96 		return errno;
     97 
     98 	for (i = 0; i < __arraycount(ans); i++)
     99 		if (strcmp("autoload", ans[i].sysctl_name) == 0)
    100 			break;
    101 	if (i == __arraycount(ans)) {
    102 		errno = ENOENT;
    103 		return -1;
    104 	}
    105 
    106 	mib[2] = ans[i].sysctl_num;
    107 
    108 	no = false;
    109 	alen = 0;
    110 	if (rump_sys___sysctl(mib, 3, NULL, &alen, &no, sizeof(no)) == -1)
    111 		return errno;
    112 
    113 	return 0;
    114 
    115 }
    116 
    117 #define TMPFSMODULE "librumpfs_tmpfs.so"
    118 ATF_TC_BODY(cmsg_modcmd, tc)
    119 {
    120 	struct tmpfs_args args;
    121 	const struct modinfo *const *mi_start, *const *mi_end;
    122 	void *handle;
    123 	int i, rv, loop = 0;
    124 
    125 	rump_init();
    126 
    127 	if (disable_autoload() == -1)
    128 		atf_tc_fail_errno("count not disable module autoload");
    129 
    130 	memset(&args, 0, sizeof(args));
    131 	args.ta_version = TMPFS_ARGS_VERSION;
    132 	args.ta_root_mode = 0777;
    133 
    134 	if (rump_sys_mkdir("/mp", 0777) == -1)
    135 		atf_tc_fail_errno("mkdir mountpoint");
    136 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
    137 		atf_tc_fail("mount unexpectedly succeeded");
    138 
    139 	handle = dlopen(TMPFSMODULE, RTLD_GLOBAL);
    140 	if (handle == NULL) {
    141 		const char *dlmsg = dlerror();
    142 		atf_tc_fail("cannot open %s: %s", TMPFSMODULE, dlmsg);
    143 	}
    144 
    145  again:
    146 	mi_start = dlsym(handle, "__start_link_set_modules");
    147 	mi_end = dlsym(handle, "__stop_link_set_modules");
    148 	if (mi_start == NULL || mi_end == NULL)
    149 		atf_tc_fail("cannot find module info");
    150 	if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))!=0)
    151 		atf_tc_fail("module init failed: %d (%s)", rv, strerror(rv));
    152 	if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))==0)
    153 		atf_tc_fail("module double init succeeded");
    154 
    155 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) == -1)
    156 		atf_tc_fail_errno("still cannot mount");
    157 	if (rump_sys_unmount("/mp", 0) == -1)
    158 		atf_tc_fail("cannot unmount");
    159 	for (i = 0; i < (int)(mi_end-mi_start); i++) {
    160 		if ((rv = rump_pub_module_fini(mi_start[i])) != 0)
    161 			atf_tc_fail("module fini failed: %d (%s)",
    162 			    rv, strerror(rv));
    163 	}
    164 	for (i = 0; i < (int)(mi_end-mi_start); i++) {
    165 		if ((rv = rump_pub_module_fini(mi_start[i])) == 0)
    166 			atf_tc_fail("module double fini succeeded");
    167 	}
    168 	if (loop++ == 0)
    169 		goto again;
    170 
    171 	if (dlclose(handle)) {
    172 		const char *dlmsg = dlerror();
    173 		atf_tc_fail("cannot close %s: %s", TMPFSMODULE, dlmsg);
    174 	}
    175 
    176 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
    177 		atf_tc_fail("mount unexpectedly succeeded");
    178 }
    179 
    180 ATF_TP_ADD_TCS(tp)
    181 {
    182 	ATF_TP_ADD_TC(tp, cmsg_modcmd);
    183 
    184 	return atf_no_error();
    185 }
    186