xref: /freebsd/contrib/netbsd-tests/rump/rumpkern/t_modcmd.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1 /*	$NetBSD: t_modcmd.c,v 1.9 2010/05/31 23:51:28 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 
62 static int
63 disable_autoload(void)
64 {
65 	struct sysctlnode q, ans[256];
66 	int mib[3];
67 	size_t alen;
68 	unsigned i;
69 	bool no;
70 
71 	mib[0] = CTL_KERN;
72 	mib[1] = CTL_QUERY;
73 	alen = sizeof(ans);
74 
75 	memset(&q, 0, sizeof(q));
76 	q.sysctl_flags = SYSCTL_VERSION;
77 
78 	if (rump_sys___sysctl(mib, 2, ans, &alen, &q, sizeof(q)) == -1)
79 		return -1;
80 
81 	for (i = 0; i < __arraycount(ans); i++)
82 		if (strcmp("module", ans[i].sysctl_name) == 0)
83 			break;
84 	if (i == __arraycount(ans)) {
85 		errno = ENOENT;
86 		return -1;
87 	}
88 
89 	mib[1] = ans[i].sysctl_num;
90 	mib[2] = CTL_QUERY;
91 
92 	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1)
93 		return errno;
94 
95 	for (i = 0; i < __arraycount(ans); i++)
96 		if (strcmp("autoload", ans[i].sysctl_name) == 0)
97 			break;
98 	if (i == __arraycount(ans)) {
99 		errno = ENOENT;
100 		return -1;
101 	}
102 
103 	mib[2] = ans[i].sysctl_num;
104 
105 	no = false;
106 	alen = 0;
107 	if (rump_sys___sysctl(mib, 3, NULL, &alen, &no, sizeof(no)) == -1)
108 		return errno;
109 
110 	return 0;
111 
112 }
113 
114 #define TMPFSMODULE "librumpfs_tmpfs.so"
115 ATF_TC_BODY(cmsg_modcmd, tc)
116 {
117 	struct tmpfs_args args;
118 	const struct modinfo *const *mi_start, *const *mi_end;
119 	void *handle;
120 	int i, rv, loop = 0;
121 
122 	rump_init();
123 
124 	if (disable_autoload() == -1)
125 		atf_tc_fail_errno("count not disable module autoload");
126 
127 	memset(&args, 0, sizeof(args));
128 	args.ta_version = TMPFS_ARGS_VERSION;
129 	args.ta_root_mode = 0777;
130 
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");
135 
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);
140 	}
141 
142  again:
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");
151 
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)",
159 			    rv, strerror(rv));
160 	}
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");
164 	}
165 	if (loop++ == 0)
166 		goto again;
167 
168 	if (dlclose(handle)) {
169 		const char *dlmsg = dlerror();
170 		atf_tc_fail("cannot close %s: %s", TMPFSMODULE, dlmsg);
171 	}
172 
173 	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
174 		atf_tc_fail("mount unexpectedly succeeded");
175 }
176 
177 ATF_TP_ADD_TCS(tp)
178 {
179 	ATF_TP_ADD_TC(tp, cmsg_modcmd);
180 
181 	return atf_no_error();
182 }
183