xref: /freebsd/contrib/netbsd-tests/rump/modautoload/t_modautoload.c (revision 935b2ac1f11515ec247712824a1f7911011763d5)
1  /*	$NetBSD: t_modautoload.c,v 1.6 2017/01/13 21:30:42 christos Exp $	*/
2  
3  #include <sys/types.h>
4  #include <sys/mount.h>
5  #include <sys/module.h>
6  #include <sys/dirent.h>
7  #include <sys/sysctl.h>
8  
9  #include <atf-c.h>
10  #include <err.h>
11  #include <errno.h>
12  #include <fcntl.h>
13  #include <stdio.h>
14  #include <unistd.h>
15  #include <string.h>
16  #include <stdlib.h>
17  
18  #include <rump/rump.h>
19  #include <rump/rump_syscalls.h>
20  
21  #include <miscfs/kernfs/kernfs.h>
22  
23  #include "h_macros.h"
24  
25  ATF_TC(modautoload);
26  ATF_TC_HEAD(modautoload, tc)
27  {
28  
29  	atf_tc_set_md_var(tc, "descr", "tests that kernel module "
30  	    "autoload works in rump");
31  }
32  
33  static void
34  mountkernfs(void)
35  {
36  	bool old_autoload, new_autoload;
37  	size_t old_len, new_len;
38  	int error;
39  
40  	if (!rump_nativeabi_p())
41  		atf_tc_skip("host kernel modules not supported");
42  
43  	rump_init();
44  
45  	if (rump_sys_mkdir("/kern", 0777) == -1)
46  		atf_tc_fail_errno("mkdir /kern");
47  
48  	new_autoload = true;
49  	old_len = sizeof(old_autoload);
50  	new_len = sizeof(new_autoload);
51  	error = sysctlbyname("kern.module.autoload",
52  				  &old_autoload, &old_len,
53  				  &new_autoload, new_len);
54  	if (error != 0)
55  		atf_tc_fail_errno("could not enable module autoload");
56  
57  	if (rump_sys_mount(MOUNT_KERNFS, "/kern", 0, NULL, 0) == -1)
58  		atf_tc_fail_errno("could not mount kernfs");
59  }
60  
61  /*
62   * Why use kernfs here?  It talks to plenty of other parts with the
63   * kernel (e.g. vfs_attach() in modcmd), but is still easy to verify
64   * it's working correctly.
65   */
66  
67  #define MAGICNUM 1323
68  ATF_TC_BODY(modautoload, tc)
69  {
70  	extern int rumpns_hz;
71  	char buf[64];
72  	int fd;
73  
74  	mountkernfs();
75  	rumpns_hz = MAGICNUM;
76  	if ((fd = rump_sys_open("/kern/hz", O_RDONLY)) == -1)
77  		atf_tc_fail_errno("open /kern/hz");
78  	if (rump_sys_read(fd, buf, sizeof(buf)) <= 0)
79  		atf_tc_fail_errno("read");
80  	ATF_REQUIRE(atoi(buf) == MAGICNUM);
81  }
82  
83  ATF_TP_ADD_TCS(tp)
84  {
85  	ATF_TP_ADD_TC(tp, modautoload);
86  
87  	return atf_no_error();
88  }
89