xref: /minix3/tests/modules/t_modctl.c (revision 11be35a165022172ed3cea20f2b5df0307540b0e)
1*11be35a1SLionel Sambuc /*	$NetBSD: t_modctl.c,v 1.12 2012/08/20 08:07:52 martin Exp $	*/
2*11be35a1SLionel Sambuc /*
3*11be35a1SLionel Sambuc  * Copyright (c) 2008 The NetBSD Foundation, Inc.
4*11be35a1SLionel Sambuc  * 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/cdefs.h>
30*11be35a1SLionel Sambuc __KERNEL_RCSID(0, "$NetBSD: t_modctl.c,v 1.12 2012/08/20 08:07:52 martin Exp $");
31*11be35a1SLionel Sambuc 
32*11be35a1SLionel Sambuc #include <sys/module.h>
33*11be35a1SLionel Sambuc #include <sys/sysctl.h>
34*11be35a1SLionel Sambuc 
35*11be35a1SLionel Sambuc #include <assert.h>
36*11be35a1SLionel Sambuc #include <errno.h>
37*11be35a1SLionel Sambuc #include <stdarg.h>
38*11be35a1SLionel Sambuc #include <stdbool.h>
39*11be35a1SLionel Sambuc #include <stdio.h>
40*11be35a1SLionel Sambuc #include <stdlib.h>
41*11be35a1SLionel Sambuc #include <string.h>
42*11be35a1SLionel Sambuc 
43*11be35a1SLionel Sambuc #include <prop/proplib.h>
44*11be35a1SLionel Sambuc 
45*11be35a1SLionel Sambuc #include <atf-c.h>
46*11be35a1SLionel Sambuc 
47*11be35a1SLionel Sambuc enum presence_check { both_checks, stat_check, sysctl_check };
48*11be35a1SLionel Sambuc 
49*11be35a1SLionel Sambuc static void	check_permission(void);
50*11be35a1SLionel Sambuc static bool	get_modstat_info(const char *, modstat_t *);
51*11be35a1SLionel Sambuc static bool	get_sysctl(const char *, void *buf, const size_t);
52*11be35a1SLionel Sambuc static bool	k_helper_is_present_stat(void);
53*11be35a1SLionel Sambuc static bool	k_helper_is_present_sysctl(void);
54*11be35a1SLionel Sambuc static bool	k_helper_is_present(enum presence_check);
55*11be35a1SLionel Sambuc static int	load(prop_dictionary_t, bool, const char *, ...);
56*11be35a1SLionel Sambuc static int	unload(const char *, bool);
57*11be35a1SLionel Sambuc static void	unload_cleanup(const char *);
58*11be35a1SLionel Sambuc 
59*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
60*11be35a1SLionel Sambuc /* Auxiliary functions                                                   */
61*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
62*11be35a1SLionel Sambuc 
63*11be35a1SLionel Sambuc /*
64*11be35a1SLionel Sambuc  * A function checking wether we are allowed to load modules currently
65*11be35a1SLionel Sambuc  * (either the kernel is not modular, or securelevel may prevent it)
66*11be35a1SLionel Sambuc  */
67*11be35a1SLionel Sambuc static void
check_permission(void)68*11be35a1SLionel Sambuc check_permission(void)
69*11be35a1SLionel Sambuc {
70*11be35a1SLionel Sambuc 	int err;
71*11be35a1SLionel Sambuc 
72*11be35a1SLionel Sambuc 	err = modctl(MODCTL_EXISTS, 0);
73*11be35a1SLionel Sambuc 	if (err == 0) return;
74*11be35a1SLionel Sambuc 	if (errno == ENOSYS)
75*11be35a1SLionel Sambuc 		atf_tc_skip("Kernel does not have 'options MODULAR'.");
76*11be35a1SLionel Sambuc 	else if (errno == EPERM)
77*11be35a1SLionel Sambuc 		atf_tc_skip("Module loading administratively forbidden");
78*11be35a1SLionel Sambuc 	ATF_REQUIRE_EQ_MSG(errno, 0, "unexpected error %d from "
79*11be35a1SLionel Sambuc 	    "modctl(MODCTL_EXISTS, 0)", errno);
80*11be35a1SLionel Sambuc }
81*11be35a1SLionel Sambuc 
82*11be35a1SLionel Sambuc static bool
get_modstat_info(const char * name,modstat_t * msdest)83*11be35a1SLionel Sambuc get_modstat_info(const char *name, modstat_t *msdest)
84*11be35a1SLionel Sambuc {
85*11be35a1SLionel Sambuc 	bool found;
86*11be35a1SLionel Sambuc 	size_t len;
87*11be35a1SLionel Sambuc 	struct iovec iov;
88*11be35a1SLionel Sambuc 	modstat_t *ms;
89*11be35a1SLionel Sambuc 
90*11be35a1SLionel Sambuc 	check_permission();
91*11be35a1SLionel Sambuc 	for (len = 4096; ;) {
92*11be35a1SLionel Sambuc 		iov.iov_base = malloc(len);
93*11be35a1SLionel Sambuc 		iov.iov_len = len;
94*11be35a1SLionel Sambuc 
95*11be35a1SLionel Sambuc 		errno = 0;
96*11be35a1SLionel Sambuc 
97*11be35a1SLionel Sambuc 		if (modctl(MODCTL_STAT, &iov) != 0) {
98*11be35a1SLionel Sambuc 			int err = errno;
99*11be35a1SLionel Sambuc 			fprintf(stderr, "modctl(MODCTL_STAT) failed: %s\n",
100*11be35a1SLionel Sambuc 			    strerror(err));
101*11be35a1SLionel Sambuc 			atf_tc_fail("Failed to query module status");
102*11be35a1SLionel Sambuc 		}
103*11be35a1SLionel Sambuc 		if (len >= iov.iov_len)
104*11be35a1SLionel Sambuc 			break;
105*11be35a1SLionel Sambuc 		free(iov.iov_base);
106*11be35a1SLionel Sambuc 		len = iov.iov_len;
107*11be35a1SLionel Sambuc 	}
108*11be35a1SLionel Sambuc 
109*11be35a1SLionel Sambuc 	found = false;
110*11be35a1SLionel Sambuc 	len = iov.iov_len / sizeof(modstat_t);
111*11be35a1SLionel Sambuc 	for (ms = (modstat_t *)iov.iov_base; len != 0 && !found;
112*11be35a1SLionel Sambuc 	    ms++, len--) {
113*11be35a1SLionel Sambuc 		if (strcmp(ms->ms_name, name) == 0) {
114*11be35a1SLionel Sambuc 			if (msdest != NULL)
115*11be35a1SLionel Sambuc 				*msdest = *ms;
116*11be35a1SLionel Sambuc 			found = true;
117*11be35a1SLionel Sambuc 		}
118*11be35a1SLionel Sambuc 	}
119*11be35a1SLionel Sambuc 
120*11be35a1SLionel Sambuc 	free(iov.iov_base);
121*11be35a1SLionel Sambuc 
122*11be35a1SLionel Sambuc 	return found;
123*11be35a1SLionel Sambuc }
124*11be35a1SLionel Sambuc 
125*11be35a1SLionel Sambuc /*
126*11be35a1SLionel Sambuc  * Queries a sysctl property.
127*11be35a1SLionel Sambuc  */
128*11be35a1SLionel Sambuc static bool
get_sysctl(const char * name,void * buf,const size_t len)129*11be35a1SLionel Sambuc get_sysctl(const char *name, void *buf, const size_t len)
130*11be35a1SLionel Sambuc {
131*11be35a1SLionel Sambuc 	size_t len2 = len;
132*11be35a1SLionel Sambuc 	printf("Querying sysctl variable: %s\n", name);
133*11be35a1SLionel Sambuc 	int ret = sysctlbyname(name, buf, &len2, NULL, 0);
134*11be35a1SLionel Sambuc 	if (ret == -1 && errno != ENOENT) {
135*11be35a1SLionel Sambuc 		fprintf(stderr, "sysctlbyname(2) failed: %s\n",
136*11be35a1SLionel Sambuc 		    strerror(errno));
137*11be35a1SLionel Sambuc 		atf_tc_fail("Failed to query %s", name);
138*11be35a1SLionel Sambuc 	}
139*11be35a1SLionel Sambuc 	return ret != -1;
140*11be35a1SLionel Sambuc }
141*11be35a1SLionel Sambuc 
142*11be35a1SLionel Sambuc /*
143*11be35a1SLionel Sambuc  * Returns a boolean indicating if the k_helper module was loaded
144*11be35a1SLionel Sambuc  * successfully.  This implementation uses modctl(2)'s MODCTL_STAT
145*11be35a1SLionel Sambuc  * subcommand to do the check.
146*11be35a1SLionel Sambuc  */
147*11be35a1SLionel Sambuc static bool
k_helper_is_present_stat(void)148*11be35a1SLionel Sambuc k_helper_is_present_stat(void)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc 
151*11be35a1SLionel Sambuc 	return get_modstat_info("k_helper", NULL);
152*11be35a1SLionel Sambuc }
153*11be35a1SLionel Sambuc 
154*11be35a1SLionel Sambuc /*
155*11be35a1SLionel Sambuc  * Returns a boolean indicating if the k_helper module was loaded
156*11be35a1SLionel Sambuc  * successfully.  This implementation uses the module's sysctl
157*11be35a1SLionel Sambuc  * installed node to do the check.
158*11be35a1SLionel Sambuc  */
159*11be35a1SLionel Sambuc static bool
k_helper_is_present_sysctl(void)160*11be35a1SLionel Sambuc k_helper_is_present_sysctl(void)
161*11be35a1SLionel Sambuc {
162*11be35a1SLionel Sambuc 	size_t present;
163*11be35a1SLionel Sambuc 
164*11be35a1SLionel Sambuc 	return get_sysctl("vendor.k_helper.present", &present,
165*11be35a1SLionel Sambuc 	    sizeof(present));
166*11be35a1SLionel Sambuc }
167*11be35a1SLionel Sambuc 
168*11be35a1SLionel Sambuc /*
169*11be35a1SLionel Sambuc  * Returns a boolean indicating if the k_helper module was loaded
170*11be35a1SLionel Sambuc  * successfully.  The 'how' parameter specifies the implementation to
171*11be35a1SLionel Sambuc  * use to do the check.
172*11be35a1SLionel Sambuc  */
173*11be35a1SLionel Sambuc static bool
k_helper_is_present(enum presence_check how)174*11be35a1SLionel Sambuc k_helper_is_present(enum presence_check how)
175*11be35a1SLionel Sambuc {
176*11be35a1SLionel Sambuc 	bool found;
177*11be35a1SLionel Sambuc 
178*11be35a1SLionel Sambuc 	switch (how) {
179*11be35a1SLionel Sambuc 	case both_checks:
180*11be35a1SLionel Sambuc 		found = k_helper_is_present_stat();
181*11be35a1SLionel Sambuc 		ATF_CHECK(k_helper_is_present_sysctl() == found);
182*11be35a1SLionel Sambuc 		break;
183*11be35a1SLionel Sambuc 
184*11be35a1SLionel Sambuc 	case stat_check:
185*11be35a1SLionel Sambuc 		found = k_helper_is_present_stat();
186*11be35a1SLionel Sambuc 		break;
187*11be35a1SLionel Sambuc 
188*11be35a1SLionel Sambuc 	case sysctl_check:
189*11be35a1SLionel Sambuc 		found = k_helper_is_present_sysctl();
190*11be35a1SLionel Sambuc 		break;
191*11be35a1SLionel Sambuc 
192*11be35a1SLionel Sambuc 	default:
193*11be35a1SLionel Sambuc 		found = false;
194*11be35a1SLionel Sambuc 		assert(found);
195*11be35a1SLionel Sambuc 	}
196*11be35a1SLionel Sambuc 
197*11be35a1SLionel Sambuc 	return found;
198*11be35a1SLionel Sambuc }
199*11be35a1SLionel Sambuc 
200*11be35a1SLionel Sambuc /*
201*11be35a1SLionel Sambuc  * Loads the specified module from a file.  If fatal is set and an error
202*11be35a1SLionel Sambuc  * occurs when loading the module, an error message is printed and the
203*11be35a1SLionel Sambuc  * test case is aborted.
204*11be35a1SLionel Sambuc  */
205*11be35a1SLionel Sambuc static __printflike(3, 4) int
load(prop_dictionary_t props,bool fatal,const char * fmt,...)206*11be35a1SLionel Sambuc load(prop_dictionary_t props, bool fatal, const char *fmt, ...)
207*11be35a1SLionel Sambuc {
208*11be35a1SLionel Sambuc 	int err;
209*11be35a1SLionel Sambuc 	va_list ap;
210*11be35a1SLionel Sambuc 	char filename[MAXPATHLEN], *propsstr;
211*11be35a1SLionel Sambuc 	modctl_load_t ml;
212*11be35a1SLionel Sambuc 
213*11be35a1SLionel Sambuc 	check_permission();
214*11be35a1SLionel Sambuc 	if (props == NULL) {
215*11be35a1SLionel Sambuc 		props = prop_dictionary_create();
216*11be35a1SLionel Sambuc 		propsstr = prop_dictionary_externalize(props);
217*11be35a1SLionel Sambuc 		ATF_CHECK(propsstr != NULL);
218*11be35a1SLionel Sambuc 		prop_object_release(props);
219*11be35a1SLionel Sambuc 	} else {
220*11be35a1SLionel Sambuc 		propsstr = prop_dictionary_externalize(props);
221*11be35a1SLionel Sambuc 		ATF_CHECK(propsstr != NULL);
222*11be35a1SLionel Sambuc 	}
223*11be35a1SLionel Sambuc 
224*11be35a1SLionel Sambuc 	va_start(ap, fmt);
225*11be35a1SLionel Sambuc 	vsnprintf(filename, sizeof(filename), fmt, ap);
226*11be35a1SLionel Sambuc 	va_end(ap);
227*11be35a1SLionel Sambuc 
228*11be35a1SLionel Sambuc 	ml.ml_filename = filename;
229*11be35a1SLionel Sambuc 	ml.ml_flags = 0;
230*11be35a1SLionel Sambuc 	ml.ml_props = propsstr;
231*11be35a1SLionel Sambuc 	ml.ml_propslen = strlen(propsstr);
232*11be35a1SLionel Sambuc 
233*11be35a1SLionel Sambuc 	printf("Loading module %s\n", filename);
234*11be35a1SLionel Sambuc 	errno = err = 0;
235*11be35a1SLionel Sambuc 
236*11be35a1SLionel Sambuc 	if (modctl(MODCTL_LOAD, &ml) == -1) {
237*11be35a1SLionel Sambuc 		err = errno;
238*11be35a1SLionel Sambuc 		fprintf(stderr, "modctl(MODCTL_LOAD, %s), failed: %s\n",
239*11be35a1SLionel Sambuc 		    filename, strerror(err));
240*11be35a1SLionel Sambuc 		if (fatal)
241*11be35a1SLionel Sambuc 			atf_tc_fail("Module load failed");
242*11be35a1SLionel Sambuc 	}
243*11be35a1SLionel Sambuc 
244*11be35a1SLionel Sambuc 	free(propsstr);
245*11be35a1SLionel Sambuc 
246*11be35a1SLionel Sambuc 	return err;
247*11be35a1SLionel Sambuc }
248*11be35a1SLionel Sambuc 
249*11be35a1SLionel Sambuc /*
250*11be35a1SLionel Sambuc  * Unloads the specified module.  If silent is true, nothing will be
251*11be35a1SLionel Sambuc  * printed and no errors will be raised if the unload was unsuccessful.
252*11be35a1SLionel Sambuc  */
253*11be35a1SLionel Sambuc static int
unload(const char * name,bool fatal)254*11be35a1SLionel Sambuc unload(const char *name, bool fatal)
255*11be35a1SLionel Sambuc {
256*11be35a1SLionel Sambuc 	int err;
257*11be35a1SLionel Sambuc 
258*11be35a1SLionel Sambuc 	check_permission();
259*11be35a1SLionel Sambuc 	printf("Unloading module %s\n", name);
260*11be35a1SLionel Sambuc 	errno = err = 0;
261*11be35a1SLionel Sambuc 
262*11be35a1SLionel Sambuc 	if (modctl(MODCTL_UNLOAD, __UNCONST(name)) == -1) {
263*11be35a1SLionel Sambuc 		err = errno;
264*11be35a1SLionel Sambuc 		fprintf(stderr, "modctl(MODCTL_UNLOAD, %s) failed: %s\n",
265*11be35a1SLionel Sambuc 		    name, strerror(err));
266*11be35a1SLionel Sambuc 		if (fatal)
267*11be35a1SLionel Sambuc 			atf_tc_fail("Module unload failed");
268*11be35a1SLionel Sambuc 	}
269*11be35a1SLionel Sambuc 	return err;
270*11be35a1SLionel Sambuc }
271*11be35a1SLionel Sambuc 
272*11be35a1SLionel Sambuc /*
273*11be35a1SLionel Sambuc  * A silent version of unload, to be called as part of the cleanup
274*11be35a1SLionel Sambuc  * process only.
275*11be35a1SLionel Sambuc  */
276*11be35a1SLionel Sambuc static void
unload_cleanup(const char * name)277*11be35a1SLionel Sambuc unload_cleanup(const char *name)
278*11be35a1SLionel Sambuc {
279*11be35a1SLionel Sambuc 
280*11be35a1SLionel Sambuc 	(void)modctl(MODCTL_UNLOAD, __UNCONST(name));
281*11be35a1SLionel Sambuc }
282*11be35a1SLionel Sambuc 
283*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
284*11be35a1SLionel Sambuc /* Test cases                                                            */
285*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
286*11be35a1SLionel Sambuc 
287*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cmd_load);
ATF_TC_HEAD(cmd_load,tc)288*11be35a1SLionel Sambuc ATF_TC_HEAD(cmd_load, tc)
289*11be35a1SLionel Sambuc {
290*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command");
291*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
292*11be35a1SLionel Sambuc }
ATF_TC_BODY(cmd_load,tc)293*11be35a1SLionel Sambuc ATF_TC_BODY(cmd_load, tc)
294*11be35a1SLionel Sambuc {
295*11be35a1SLionel Sambuc 	char longname[MAXPATHLEN];
296*11be35a1SLionel Sambuc 	size_t i;
297*11be35a1SLionel Sambuc 
298*11be35a1SLionel Sambuc 	ATF_CHECK(load(NULL, false, " ") == ENOENT);
299*11be35a1SLionel Sambuc 	ATF_CHECK(load(NULL, false, "non-existent.o") == ENOENT);
300*11be35a1SLionel Sambuc 
301*11be35a1SLionel Sambuc 	for (i = 0; i < MAXPATHLEN - 1; i++)
302*11be35a1SLionel Sambuc 		longname[i] = 'a';
303*11be35a1SLionel Sambuc 	longname[MAXPATHLEN - 1] = '\0';
304*11be35a1SLionel Sambuc 	ATF_CHECK(load(NULL, false, "%s", longname) == ENAMETOOLONG);
305*11be35a1SLionel Sambuc 
306*11be35a1SLionel Sambuc 	ATF_CHECK(!k_helper_is_present(stat_check));
307*11be35a1SLionel Sambuc 	load(NULL, true, "%s/k_helper/k_helper.kmod",
308*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
309*11be35a1SLionel Sambuc 	printf("Checking if load was successful\n");
310*11be35a1SLionel Sambuc 	ATF_CHECK(k_helper_is_present(stat_check));
311*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cmd_load,tc)312*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cmd_load, tc)
313*11be35a1SLionel Sambuc {
314*11be35a1SLionel Sambuc 	unload_cleanup("k_helper");
315*11be35a1SLionel Sambuc }
316*11be35a1SLionel Sambuc 
317*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cmd_load_props);
ATF_TC_HEAD(cmd_load_props,tc)318*11be35a1SLionel Sambuc ATF_TC_HEAD(cmd_load_props, tc)
319*11be35a1SLionel Sambuc {
320*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command, "
321*11be35a1SLionel Sambuc 	    "providing extra load-time properties");
322*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
323*11be35a1SLionel Sambuc }
ATF_TC_BODY(cmd_load_props,tc)324*11be35a1SLionel Sambuc ATF_TC_BODY(cmd_load_props, tc)
325*11be35a1SLionel Sambuc {
326*11be35a1SLionel Sambuc 	prop_dictionary_t props;
327*11be35a1SLionel Sambuc 
328*11be35a1SLionel Sambuc 	printf("Loading module without properties\n");
329*11be35a1SLionel Sambuc 	props = prop_dictionary_create();
330*11be35a1SLionel Sambuc 	load(props, true, "%s/k_helper/k_helper.kmod",
331*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
332*11be35a1SLionel Sambuc 	prop_object_release(props);
333*11be35a1SLionel Sambuc 	{
334*11be35a1SLionel Sambuc 		int ok;
335*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
336*11be35a1SLionel Sambuc 		    &ok, sizeof(ok)));
337*11be35a1SLionel Sambuc 		ATF_CHECK(!ok);
338*11be35a1SLionel Sambuc 	}
339*11be35a1SLionel Sambuc 	unload("k_helper", true);
340*11be35a1SLionel Sambuc 
341*11be35a1SLionel Sambuc 	printf("Loading module with a string property\n");
342*11be35a1SLionel Sambuc 	props = prop_dictionary_create();
343*11be35a1SLionel Sambuc 	prop_dictionary_set(props, "prop_str",
344*11be35a1SLionel Sambuc 	    prop_string_create_cstring("1st string"));
345*11be35a1SLionel Sambuc 	load(props, true, "%s/k_helper/k_helper.kmod",
346*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
347*11be35a1SLionel Sambuc 	prop_object_release(props);
348*11be35a1SLionel Sambuc 	{
349*11be35a1SLionel Sambuc 		int ok;
350*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
351*11be35a1SLionel Sambuc 		    &ok, sizeof(ok)));
352*11be35a1SLionel Sambuc 		ATF_CHECK(ok);
353*11be35a1SLionel Sambuc 
354*11be35a1SLionel Sambuc 		char val[128];
355*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
356*11be35a1SLionel Sambuc 		    &val, sizeof(val)));
357*11be35a1SLionel Sambuc 		ATF_CHECK(strcmp(val, "1st string") == 0);
358*11be35a1SLionel Sambuc 	}
359*11be35a1SLionel Sambuc 	unload("k_helper", true);
360*11be35a1SLionel Sambuc 
361*11be35a1SLionel Sambuc 	printf("Loading module with a different string property\n");
362*11be35a1SLionel Sambuc 	props = prop_dictionary_create();
363*11be35a1SLionel Sambuc 	prop_dictionary_set(props, "prop_str",
364*11be35a1SLionel Sambuc 	    prop_string_create_cstring("2nd string"));
365*11be35a1SLionel Sambuc 	load(props, true, "%s/k_helper/k_helper.kmod",
366*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
367*11be35a1SLionel Sambuc 	prop_object_release(props);
368*11be35a1SLionel Sambuc 	{
369*11be35a1SLionel Sambuc 		int ok;
370*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_ok",
371*11be35a1SLionel Sambuc 		    &ok, sizeof(ok)));
372*11be35a1SLionel Sambuc 		ATF_CHECK(ok);
373*11be35a1SLionel Sambuc 
374*11be35a1SLionel Sambuc 		char val[128];
375*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_str_val",
376*11be35a1SLionel Sambuc 		    &val, sizeof(val)));
377*11be35a1SLionel Sambuc 		ATF_CHECK(strcmp(val, "2nd string") == 0);
378*11be35a1SLionel Sambuc 	}
379*11be35a1SLionel Sambuc 	unload("k_helper", true);
380*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cmd_load_props,tc)381*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cmd_load_props, tc)
382*11be35a1SLionel Sambuc {
383*11be35a1SLionel Sambuc 	unload_cleanup("k_helper");
384*11be35a1SLionel Sambuc }
385*11be35a1SLionel Sambuc 
386*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cmd_load_recurse);
ATF_TC_HEAD(cmd_load_recurse,tc)387*11be35a1SLionel Sambuc ATF_TC_HEAD(cmd_load_recurse, tc)
388*11be35a1SLionel Sambuc {
389*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_LOAD command, "
390*11be35a1SLionel Sambuc 	    "with recursive module_load()");
391*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
392*11be35a1SLionel Sambuc }
ATF_TC_BODY(cmd_load_recurse,tc)393*11be35a1SLionel Sambuc ATF_TC_BODY(cmd_load_recurse, tc)
394*11be35a1SLionel Sambuc {
395*11be35a1SLionel Sambuc 	prop_dictionary_t props;
396*11be35a1SLionel Sambuc 	char filename[MAXPATHLEN];
397*11be35a1SLionel Sambuc 
398*11be35a1SLionel Sambuc 	printf("Loading module with request to load another module\n");
399*11be35a1SLionel Sambuc 	props = prop_dictionary_create();
400*11be35a1SLionel Sambuc 	snprintf(filename, sizeof(filename), "%s/k_helper2/k_helper2.kmod",
401*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
402*11be35a1SLionel Sambuc 	prop_dictionary_set(props, "prop_recurse",
403*11be35a1SLionel Sambuc 	    prop_string_create_cstring(filename));
404*11be35a1SLionel Sambuc 	load(props, true, "%s/k_helper/k_helper.kmod",
405*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
406*11be35a1SLionel Sambuc 	{
407*11be35a1SLionel Sambuc 		int ok;
408*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper.prop_int_load",
409*11be35a1SLionel Sambuc 		    &ok, sizeof(ok)));
410*11be35a1SLionel Sambuc 		ATF_CHECK(ok == 0);
411*11be35a1SLionel Sambuc 		ATF_CHECK(get_sysctl("vendor.k_helper2.present",
412*11be35a1SLionel Sambuc 		    &ok, sizeof(ok)));
413*11be35a1SLionel Sambuc 		ATF_CHECK(ok);
414*11be35a1SLionel Sambuc 	}
415*11be35a1SLionel Sambuc 	unload("k_helper", true);
416*11be35a1SLionel Sambuc 	unload("k_helper2", true);
417*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cmd_load_recurse,tc)418*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cmd_load_recurse, tc)
419*11be35a1SLionel Sambuc {
420*11be35a1SLionel Sambuc 	unload_cleanup("k_helper");
421*11be35a1SLionel Sambuc 	unload_cleanup("k_helper2");
422*11be35a1SLionel Sambuc }
423*11be35a1SLionel Sambuc 
424*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cmd_stat);
ATF_TC_HEAD(cmd_stat,tc)425*11be35a1SLionel Sambuc ATF_TC_HEAD(cmd_stat, tc)
426*11be35a1SLionel Sambuc {
427*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_STAT command");
428*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
429*11be35a1SLionel Sambuc }
ATF_TC_BODY(cmd_stat,tc)430*11be35a1SLionel Sambuc ATF_TC_BODY(cmd_stat, tc)
431*11be35a1SLionel Sambuc {
432*11be35a1SLionel Sambuc 	ATF_CHECK(!k_helper_is_present(both_checks));
433*11be35a1SLionel Sambuc 
434*11be35a1SLionel Sambuc 	load(NULL, true, "%s/k_helper/k_helper.kmod",
435*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
436*11be35a1SLionel Sambuc 	ATF_CHECK(k_helper_is_present(both_checks));
437*11be35a1SLionel Sambuc 	{
438*11be35a1SLionel Sambuc 		modstat_t ms;
439*11be35a1SLionel Sambuc 		ATF_CHECK(get_modstat_info("k_helper", &ms));
440*11be35a1SLionel Sambuc 
441*11be35a1SLionel Sambuc 		ATF_CHECK(ms.ms_class == MODULE_CLASS_MISC);
442*11be35a1SLionel Sambuc 		ATF_CHECK(ms.ms_source == MODULE_SOURCE_FILESYS);
443*11be35a1SLionel Sambuc 		ATF_CHECK(ms.ms_refcnt == 0);
444*11be35a1SLionel Sambuc 	}
445*11be35a1SLionel Sambuc 	unload("k_helper", true);
446*11be35a1SLionel Sambuc 
447*11be35a1SLionel Sambuc 	ATF_CHECK(!k_helper_is_present(both_checks));
448*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cmd_stat,tc)449*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cmd_stat, tc)
450*11be35a1SLionel Sambuc {
451*11be35a1SLionel Sambuc 	unload_cleanup("k_helper");
452*11be35a1SLionel Sambuc }
453*11be35a1SLionel Sambuc 
454*11be35a1SLionel Sambuc ATF_TC_WITH_CLEANUP(cmd_unload);
ATF_TC_HEAD(cmd_unload,tc)455*11be35a1SLionel Sambuc ATF_TC_HEAD(cmd_unload, tc)
456*11be35a1SLionel Sambuc {
457*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Tests for the MODCTL_UNLOAD command");
458*11be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "require.user", "root");
459*11be35a1SLionel Sambuc }
ATF_TC_BODY(cmd_unload,tc)460*11be35a1SLionel Sambuc ATF_TC_BODY(cmd_unload, tc)
461*11be35a1SLionel Sambuc {
462*11be35a1SLionel Sambuc 	load(NULL, true, "%s/k_helper/k_helper.kmod",
463*11be35a1SLionel Sambuc 	    atf_tc_get_config_var(tc, "srcdir"));
464*11be35a1SLionel Sambuc 
465*11be35a1SLionel Sambuc 	ATF_CHECK(unload("", false) == ENOENT);
466*11be35a1SLionel Sambuc 	ATF_CHECK(unload("non-existent.kmod", false) == ENOENT);
467*11be35a1SLionel Sambuc 	ATF_CHECK(unload("k_helper.kmod", false) == ENOENT);
468*11be35a1SLionel Sambuc 
469*11be35a1SLionel Sambuc 	ATF_CHECK(k_helper_is_present(stat_check));
470*11be35a1SLionel Sambuc 	unload("k_helper", true);
471*11be35a1SLionel Sambuc 	printf("Checking if unload was successful\n");
472*11be35a1SLionel Sambuc 	ATF_CHECK(!k_helper_is_present(stat_check));
473*11be35a1SLionel Sambuc }
ATF_TC_CLEANUP(cmd_unload,tc)474*11be35a1SLionel Sambuc ATF_TC_CLEANUP(cmd_unload, tc)
475*11be35a1SLionel Sambuc {
476*11be35a1SLionel Sambuc 	unload_cleanup("k_helper");
477*11be35a1SLionel Sambuc }
478*11be35a1SLionel Sambuc 
479*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
480*11be35a1SLionel Sambuc /* Main                                                                  */
481*11be35a1SLionel Sambuc /* --------------------------------------------------------------------- */
482*11be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)483*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
484*11be35a1SLionel Sambuc {
485*11be35a1SLionel Sambuc 
486*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cmd_load);
487*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cmd_load_props);
488*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cmd_stat);
489*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cmd_load_recurse);
490*11be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, cmd_unload);
491*11be35a1SLionel Sambuc 
492*11be35a1SLionel Sambuc 	return atf_no_error();
493*11be35a1SLionel Sambuc }
494