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