1 /* $NetBSD: t_modcmd.c,v 1.10 2017/01/13 21:30:43 christos Exp $ */
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/types.h>
31 #include <sys/mount.h>
32 #include <sys/sysctl.h>
34 #include <rump/rump.h>
35 #include <rump/rump_syscalls.h>
37 #include <fs/tmpfs/tmpfs_args.h>
51 * We verify that modules can be loaded and unloaded.
52 * tmpfs was chosen because it does not depend on an image.
56 ATF_TC_HEAD(cmsg_modcmd, tc)
58 atf_tc_set_md_var(tc, "descr", "Checks that loading and unloading "
59 "a module (vfs/tmpfs) is possible");
63 disable_autoload(void)
65 struct sysctlnode q, ans[256];
75 memset(&q, 0, sizeof(q));
76 q.sysctl_flags = SYSCTL_VERSION;
78 if (rump_sys___sysctl(mib, 2, ans, &alen, &q, sizeof(q)) == -1)
81 for (i = 0; i < __arraycount(ans); i++)
82 if (strcmp("module", ans[i].sysctl_name) == 0)
84 if (i == __arraycount(ans)) {
89 mib[1] = ans[i].sysctl_num;
92 if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1)
95 for (i = 0; i < __arraycount(ans); i++)
96 if (strcmp("autoload", ans[i].sysctl_name) == 0)
98 if (i == __arraycount(ans)) {
103 mib[2] = ans[i].sysctl_num;
107 if (rump_sys___sysctl(mib, 3, NULL, &alen, &no, sizeof(no)) == -1)
114 #define TMPFSMODULE "librumpfs_tmpfs.so"
115 ATF_TC_BODY(cmsg_modcmd, tc)
117 struct tmpfs_args args;
118 const struct modinfo *const *mi_start, *const *mi_end;
124 if (disable_autoload() == -1)
125 atf_tc_fail_errno("count not disable module autoload");
127 memset(&args, 0, sizeof(args));
128 args.ta_version = TMPFS_ARGS_VERSION;
129 args.ta_root_mode = 0777;
131 if (rump_sys_mkdir("/mp", 0777) == -1)
132 atf_tc_fail_errno("mkdir mountpoint");
133 if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
134 atf_tc_fail("mount unexpectedly succeeded");
136 handle = dlopen(TMPFSMODULE, RTLD_GLOBAL);
137 if (handle == NULL) {
138 const char *dlmsg = dlerror();
139 atf_tc_fail("cannot open %s: %s", TMPFSMODULE, dlmsg);
143 mi_start = dlsym(handle, "__start_link_set_modules");
144 mi_end = dlsym(handle, "__stop_link_set_modules");
145 if (mi_start == NULL || mi_end == NULL)
146 atf_tc_fail("cannot find module info");
147 if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))!=0)
148 atf_tc_fail("module init failed: %d (%s)", rv, strerror(rv));
149 if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))==0)
150 atf_tc_fail("module double init succeeded");
152 if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) == -1)
153 atf_tc_fail_errno("still cannot mount");
154 if (rump_sys_unmount("/mp", 0) == -1)
155 atf_tc_fail("cannot unmount");
156 for (i = 0; i < (int)(mi_end-mi_start); i++) {
157 if ((rv = rump_pub_module_fini(mi_start[i])) != 0)
158 atf_tc_fail("module fini failed: %d (%s)",
161 for (i = 0; i < (int)(mi_end-mi_start); i++) {
162 if ((rv = rump_pub_module_fini(mi_start[i])) == 0)
163 atf_tc_fail("module double fini succeeded");
168 if (dlclose(handle)) {
169 const char *dlmsg = dlerror();
170 atf_tc_fail("cannot close %s: %s", TMPFSMODULE, dlmsg);
173 if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
174 atf_tc_fail("mount unexpectedly succeeded");
179 ATF_TP_ADD_TC(tp, cmsg_modcmd);
181 return atf_no_error();