xref: /netbsd-src/tests/modules/t_builtin.c (revision f3cfa6f6ce31685c6c4a758bc430e69eb99f50a4)
1 /*	$NetBSD: t_builtin.c,v 1.4 2019/01/27 02:08:50 pgoyette Exp $	*/
2 
3 /*-
4  * Copyright (c) 2010 The NetBSD Foundation, Inc.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/types.h>
30 #include <sys/module.h>
31 #include <sys/mount.h>
32 
33 #include <atf-c.h>
34 #include <fcntl.h>
35 #include <stdbool.h>
36 
37 #include <miscfs/kernfs/kernfs.h>
38 
39 #include <rump/rump.h>
40 #include <rump/rump_syscalls.h>
41 
42 #include "h_macros.h"
43 
44 #define MYMP "/mnt"
45 #define HZFILE MYMP "/hz"
46 
47 static char kernfs[] = "kernfs";
48 
49 static bool
50 check_kernfs(void)
51 {
52 	char buf[16];
53 	bool rv = true;
54 	int fd;
55 
56 	fd = rump_sys_open(HZFILE, O_RDONLY);
57 	if (fd == -1)
58 		return false;
59 	if (rump_sys_read(fd, buf, sizeof(buf)) < 1)
60 		rv = false;
61 	RL(rump_sys_close(fd));
62 
63 	return rv;
64 }
65 
66 ATF_TC(disable);
67 ATF_TC_HEAD(disable, tc)
68 {
69 
70 	atf_tc_set_md_var(tc, "descr", "Tests that builtin modules can "
71 	    "be disabled");
72 }
73 
74 ATF_TC_BODY(disable, tc)
75 {
76 
77 	rump_init();
78 	RL(rump_sys_mkdir(MYMP, 0777));
79 	RL(rump_sys_mount(MOUNT_KERNFS, MYMP, 0, NULL, 0));
80 	ATF_REQUIRE(check_kernfs());
81 	RL(rump_sys_unmount(MYMP, 0));
82 	RL(rump_sys_modctl(MODCTL_UNLOAD, kernfs));
83 }
84 
85 ATF_TC(noauto);
86 ATF_TC_HEAD(noauto, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr", "Tests that disabled builtin modules "
89 	    "will not autoload");
90 }
91 
92 ATF_TC_BODY(noauto, tc)
93 {
94 
95 	rump_init();
96 	RL(rump_sys_mkdir(MYMP, 0777));
97 
98 	RL(rump_sys_modctl(MODCTL_UNLOAD, kernfs));
99 
100 	ATF_REQUIRE_ERRNO(ENODEV,
101 	    rump_sys_mount(MOUNT_KERNFS, MYMP, 0, NULL, 0) == -1);
102 }
103 
104 ATF_TC(forcereload);
105 ATF_TC_HEAD(forcereload, tc)
106 {
107 	atf_tc_set_md_var(tc, "descr", "Tests that disabled builtin modules "
108 	    "can be force-reloaded");
109 }
110 
111 ATF_TC_BODY(forcereload, tc)
112 {
113 	struct modctl_load mod;
114 
115 	rump_init();
116 	RL(rump_sys_mkdir(MYMP, 0777));
117 
118 	RL(rump_sys_modctl(MODCTL_UNLOAD, kernfs));
119 	ATF_REQUIRE_ERRNO(ENODEV,
120 	    rump_sys_mount(MOUNT_KERNFS, MYMP, 0, NULL, 0) == -1);
121 
122 	memset(&mod, 0, sizeof(mod));
123 	mod.ml_filename = kernfs;
124 	mod.ml_flags = MODCTL_LOAD_FORCE;
125 
126 	RL(rump_sys_modctl(MODCTL_LOAD, &mod));
127 
128 	RL(rump_sys_mount(MOUNT_KERNFS, MYMP, 0, NULL, 0));
129 	ATF_REQUIRE(check_kernfs());
130 	RL(rump_sys_unmount(MYMP, 0));
131 }
132 
133 ATF_TC(disabledstat);
134 ATF_TC_HEAD(disabledstat, tc)
135 {
136 	atf_tc_set_md_var(tc, "descr", "Tests that disabled builtin modules "
137 	    "show up in modstat with refcount -1");
138 }
139 
140 ATF_TC_BODY(disabledstat, tc)
141 {
142 	modstat_t *ms;
143 	struct iovec iov;
144 	size_t len;
145 	int count;
146 	bool found = false;
147 
148 	rump_init();
149 	RL(rump_sys_mkdir(MYMP, 0777));
150 
151 	RL(rump_sys_modctl(MODCTL_UNLOAD, kernfs));
152 
153 	for (len = 8192; ;) {
154 		iov.iov_base = malloc(len);
155 		iov.iov_len = len;
156 
157 		errno = 0;
158 
159 		if (rump_sys_modctl(MODCTL_STAT, &iov) != 0) {
160 			int err = errno;
161 			fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n",
162 			    strerror(err));
163 			atf_tc_fail("Failed to query module status");
164 		}
165 		if (len >= iov.iov_len)
166 			break;
167 		free(iov.iov_base);
168 		len = iov.iov_len;
169 	}
170 
171 	count = *(int *)iov.iov_base;
172 	ms = (modstat_t *)((char *)iov.iov_base + sizeof(int));
173 	while ( count ) {
174 		if (strcmp(ms->ms_name, kernfs) == 0) {
175 			ATF_REQUIRE_EQ(ms->ms_refcnt, (u_int)-1);
176 			found = true;
177 			break;
178 		}
179 		ms++;
180 		count--;
181 
182 	}
183 	ATF_REQUIRE(found);
184 }
185 
186 ATF_TC(busydisable);
187 ATF_TC_HEAD(busydisable, tc)
188 {
189 	atf_tc_set_md_var(tc, "descr", "Tests that busy builtin modules "
190 	    "cannot be disabled");
191 }
192 
193 ATF_TC_BODY(busydisable, tc)
194 {
195 
196 	rump_init();
197 	RL(rump_sys_mkdir(MYMP, 0777));
198 	RL(rump_sys_mount(MOUNT_KERNFS, MYMP, 0, NULL, 0));
199 	ATF_REQUIRE(check_kernfs());
200 	ATF_REQUIRE_ERRNO(EBUSY,
201 	    rump_sys_modctl(MODCTL_UNLOAD, kernfs) == -1);
202 }
203 
204 ATF_TP_ADD_TCS(tp)
205 {
206 
207 	ATF_TP_ADD_TC(tp, disable);
208 	ATF_TP_ADD_TC(tp, noauto);
209 	ATF_TP_ADD_TC(tp, forcereload);
210 	ATF_TP_ADD_TC(tp, disabledstat);
211 	ATF_TP_ADD_TC(tp, busydisable);
212 
213 	return atf_no_error();
214 }
215