1314817Sngie/*	$NetBSD: t_modcmd.c,v 1.10 2017/01/13 21:30:43 christos Exp $	*/
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2009 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17272343Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19272343Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20272343Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21272343Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23272343Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25272343Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27272343Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28272343Sngie */
29272343Sngie
30272343Sngie#include <sys/types.h>
31272343Sngie#include <sys/mount.h>
32272343Sngie#include <sys/sysctl.h>
33272343Sngie
34272343Sngie#include <rump/rump.h>
35272343Sngie#include <rump/rump_syscalls.h>
36272343Sngie
37272343Sngie#include <fs/tmpfs/tmpfs_args.h>
38272343Sngie
39272343Sngie#include <atf-c.h>
40272343Sngie#include <dlfcn.h>
41272343Sngie#include <err.h>
42272343Sngie#include <errno.h>
43272343Sngie#include <stdio.h>
44272343Sngie#include <stdlib.h>
45272343Sngie#include <string.h>
46272343Sngie#include <unistd.h>
47272343Sngie#include <util.h>
48272343Sngie
49314817Sngie#include "h_macros.h"
50272343Sngie/*
51272343Sngie * We verify that modules can be loaded and unloaded.
52272343Sngie * tmpfs was chosen because it does not depend on an image.
53272343Sngie */
54272343Sngie
55272343SngieATF_TC(cmsg_modcmd);
56272343SngieATF_TC_HEAD(cmsg_modcmd, tc)
57272343Sngie{
58272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks that loading and unloading "
59272343Sngie	    "a module (vfs/tmpfs) is possible");
60272343Sngie}
61272343Sngie
62272343Sngiestatic int
63272343Sngiedisable_autoload(void)
64272343Sngie{
65272343Sngie	struct sysctlnode q, ans[256];
66272343Sngie	int mib[3];
67272343Sngie	size_t alen;
68272343Sngie	unsigned i;
69272343Sngie	bool no;
70272343Sngie
71272343Sngie	mib[0] = CTL_KERN;
72272343Sngie	mib[1] = CTL_QUERY;
73272343Sngie	alen = sizeof(ans);
74272343Sngie
75272343Sngie	memset(&q, 0, sizeof(q));
76272343Sngie	q.sysctl_flags = SYSCTL_VERSION;
77272343Sngie
78272343Sngie	if (rump_sys___sysctl(mib, 2, ans, &alen, &q, sizeof(q)) == -1)
79272343Sngie		return -1;
80272343Sngie
81272343Sngie	for (i = 0; i < __arraycount(ans); i++)
82272343Sngie		if (strcmp("module", ans[i].sysctl_name) == 0)
83272343Sngie			break;
84272343Sngie	if (i == __arraycount(ans)) {
85272343Sngie		errno = ENOENT;
86272343Sngie		return -1;
87272343Sngie	}
88272343Sngie
89272343Sngie	mib[1] = ans[i].sysctl_num;
90272343Sngie	mib[2] = CTL_QUERY;
91272343Sngie
92272343Sngie	if (rump_sys___sysctl(mib, 3, ans, &alen, &q, sizeof(q)) == -1)
93272343Sngie		return errno;
94272343Sngie
95272343Sngie	for (i = 0; i < __arraycount(ans); i++)
96272343Sngie		if (strcmp("autoload", ans[i].sysctl_name) == 0)
97272343Sngie			break;
98272343Sngie	if (i == __arraycount(ans)) {
99272343Sngie		errno = ENOENT;
100272343Sngie		return -1;
101272343Sngie	}
102272343Sngie
103272343Sngie	mib[2] = ans[i].sysctl_num;
104272343Sngie
105272343Sngie	no = false;
106272343Sngie	alen = 0;
107272343Sngie	if (rump_sys___sysctl(mib, 3, NULL, &alen, &no, sizeof(no)) == -1)
108272343Sngie		return errno;
109272343Sngie
110272343Sngie	return 0;
111272343Sngie
112272343Sngie}
113272343Sngie
114272343Sngie#define TMPFSMODULE "librumpfs_tmpfs.so"
115272343SngieATF_TC_BODY(cmsg_modcmd, tc)
116272343Sngie{
117272343Sngie	struct tmpfs_args args;
118272343Sngie	const struct modinfo *const *mi_start, *const *mi_end;
119272343Sngie	void *handle;
120272343Sngie	int i, rv, loop = 0;
121272343Sngie
122272343Sngie	rump_init();
123272343Sngie
124272343Sngie	if (disable_autoload() == -1)
125272343Sngie		atf_tc_fail_errno("count not disable module autoload");
126272343Sngie
127272343Sngie	memset(&args, 0, sizeof(args));
128272343Sngie	args.ta_version = TMPFS_ARGS_VERSION;
129272343Sngie	args.ta_root_mode = 0777;
130272343Sngie
131272343Sngie	if (rump_sys_mkdir("/mp", 0777) == -1)
132272343Sngie		atf_tc_fail_errno("mkdir mountpoint");
133272343Sngie	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
134272343Sngie		atf_tc_fail("mount unexpectedly succeeded");
135272343Sngie
136272343Sngie	handle = dlopen(TMPFSMODULE, RTLD_GLOBAL);
137272343Sngie	if (handle == NULL) {
138272343Sngie		const char *dlmsg = dlerror();
139272343Sngie		atf_tc_fail("cannot open %s: %s", TMPFSMODULE, dlmsg);
140272343Sngie	}
141272343Sngie
142272343Sngie again:
143272343Sngie	mi_start = dlsym(handle, "__start_link_set_modules");
144272343Sngie	mi_end = dlsym(handle, "__stop_link_set_modules");
145272343Sngie	if (mi_start == NULL || mi_end == NULL)
146272343Sngie		atf_tc_fail("cannot find module info");
147272343Sngie	if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))!=0)
148272343Sngie		atf_tc_fail("module init failed: %d (%s)", rv, strerror(rv));
149272343Sngie	if ((rv = rump_pub_module_init(mi_start, (size_t)(mi_end-mi_start)))==0)
150272343Sngie		atf_tc_fail("module double init succeeded");
151272343Sngie
152272343Sngie	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) == -1)
153272343Sngie		atf_tc_fail_errno("still cannot mount");
154272343Sngie	if (rump_sys_unmount("/mp", 0) == -1)
155272343Sngie		atf_tc_fail("cannot unmount");
156272343Sngie	for (i = 0; i < (int)(mi_end-mi_start); i++) {
157272343Sngie		if ((rv = rump_pub_module_fini(mi_start[i])) != 0)
158272343Sngie			atf_tc_fail("module fini failed: %d (%s)",
159272343Sngie			    rv, strerror(rv));
160272343Sngie	}
161272343Sngie	for (i = 0; i < (int)(mi_end-mi_start); i++) {
162272343Sngie		if ((rv = rump_pub_module_fini(mi_start[i])) == 0)
163272343Sngie			atf_tc_fail("module double fini succeeded");
164272343Sngie	}
165272343Sngie	if (loop++ == 0)
166272343Sngie		goto again;
167272343Sngie
168272343Sngie	if (dlclose(handle)) {
169272343Sngie		const char *dlmsg = dlerror();
170272343Sngie		atf_tc_fail("cannot close %s: %s", TMPFSMODULE, dlmsg);
171272343Sngie	}
172272343Sngie
173272343Sngie	if (rump_sys_mount(MOUNT_TMPFS, "/mp", 0, &args, sizeof(args)) != -1)
174272343Sngie		atf_tc_fail("mount unexpectedly succeeded");
175272343Sngie}
176272343Sngie
177272343SngieATF_TP_ADD_TCS(tp)
178272343Sngie{
179272343Sngie	ATF_TP_ADD_TC(tp, cmsg_modcmd);
180272343Sngie
181272343Sngie	return atf_no_error();
182272343Sngie}
183