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