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