xref: /freebsd/contrib/netbsd-tests/fs/ptyfs/t_nullpts.c (revision 57718be8fa0bd5edc11ab9a72e68cc71982939a6)
1*57718be8SEnji Cooper /*	$NetBSD: t_nullpts.c,v 1.5 2011/01/10 11:11:04 hannken Exp $	*/
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper #include <sys/types.h>
4*57718be8SEnji Cooper #include <sys/mount.h>
5*57718be8SEnji Cooper #include <sys/ioctl.h>
6*57718be8SEnji Cooper 
7*57718be8SEnji Cooper #include <atf-c.h>
8*57718be8SEnji Cooper #include <err.h>
9*57718be8SEnji Cooper #include <errno.h>
10*57718be8SEnji Cooper #include <fcntl.h>
11*57718be8SEnji Cooper #include <stdio.h>
12*57718be8SEnji Cooper #include <unistd.h>
13*57718be8SEnji Cooper #include <string.h>
14*57718be8SEnji Cooper #include <stdlib.h>
15*57718be8SEnji Cooper 
16*57718be8SEnji Cooper #include <rump/rump.h>
17*57718be8SEnji Cooper #include <rump/rump_syscalls.h>
18*57718be8SEnji Cooper 
19*57718be8SEnji Cooper #include <fs/ptyfs/ptyfs.h>
20*57718be8SEnji Cooper #include <miscfs/nullfs/null.h>
21*57718be8SEnji Cooper 
22*57718be8SEnji Cooper #include "../../h_macros.h"
23*57718be8SEnji Cooper 
24*57718be8SEnji Cooper static void
25*57718be8SEnji Cooper mountptyfs(const char *mp, int flags)
26*57718be8SEnji Cooper {
27*57718be8SEnji Cooper 	struct ptyfs_args args;
28*57718be8SEnji Cooper 
29*57718be8SEnji Cooper 	if (rump_sys_mkdir(mp, 0777) == -1) {
30*57718be8SEnji Cooper 		if (errno != EEXIST)
31*57718be8SEnji Cooper 			atf_tc_fail_errno("null create %s", mp);
32*57718be8SEnji Cooper 	}
33*57718be8SEnji Cooper 	memset(&args, 0, sizeof(args));
34*57718be8SEnji Cooper 	args.version = PTYFS_ARGSVERSION;
35*57718be8SEnji Cooper 	args.mode = 0777;
36*57718be8SEnji Cooper 	if (rump_sys_mount(MOUNT_PTYFS, mp, flags, &args, sizeof(args)) == -1)
37*57718be8SEnji Cooper 		atf_tc_fail_errno("could not mount ptyfs");
38*57718be8SEnji Cooper }
39*57718be8SEnji Cooper 
40*57718be8SEnji Cooper static void
41*57718be8SEnji Cooper mountnull(const char *what, const char *mp, int flags)
42*57718be8SEnji Cooper {
43*57718be8SEnji Cooper 	struct null_args nargs;
44*57718be8SEnji Cooper 
45*57718be8SEnji Cooper 	if (rump_sys_mkdir(what, 0777) == -1) {
46*57718be8SEnji Cooper 		if (errno != EEXIST)
47*57718be8SEnji Cooper 			atf_tc_fail_errno("null create %s", what);
48*57718be8SEnji Cooper 	}
49*57718be8SEnji Cooper 	if (rump_sys_mkdir(mp, 0777) == -1) {
50*57718be8SEnji Cooper 		if (errno != EEXIST)
51*57718be8SEnji Cooper 			atf_tc_fail_errno("null create %s", mp);
52*57718be8SEnji Cooper 	}
53*57718be8SEnji Cooper 	memset(&nargs, 0, sizeof(nargs));
54*57718be8SEnji Cooper 	nargs.nulla_target = __UNCONST(what);
55*57718be8SEnji Cooper 	if (rump_sys_mount(MOUNT_NULL, mp, flags, &nargs, sizeof(nargs)) == -1)
56*57718be8SEnji Cooper 		atf_tc_fail_errno("could not mount nullfs");
57*57718be8SEnji Cooper }
58*57718be8SEnji Cooper 
59*57718be8SEnji Cooper ATF_TC(nullrevoke);
60*57718be8SEnji Cooper ATF_TC_HEAD(nullrevoke, tc)
61*57718be8SEnji Cooper {
62*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "null mount ptyfs and revoke");
63*57718be8SEnji Cooper }
64*57718be8SEnji Cooper 
65*57718be8SEnji Cooper ATF_TC_BODY(nullrevoke, tc)
66*57718be8SEnji Cooper {
67*57718be8SEnji Cooper 	char path[MAXPATHLEN];
68*57718be8SEnji Cooper 	struct ptmget ptg;
69*57718be8SEnji Cooper 	int ptm;
70*57718be8SEnji Cooper 
71*57718be8SEnji Cooper 	rump_init();
72*57718be8SEnji Cooper 
73*57718be8SEnji Cooper 	/*
74*57718be8SEnji Cooper 	 * mount /dev/pts
75*57718be8SEnji Cooper 	 */
76*57718be8SEnji Cooper 	mountptyfs("/dev/pts", 0);
77*57718be8SEnji Cooper 
78*57718be8SEnji Cooper 	/*
79*57718be8SEnji Cooper 	 * null mount /dev/pts to /null/dev/pts
80*57718be8SEnji Cooper 	 */
81*57718be8SEnji Cooper 	if (rump_sys_mkdir("/null", 0777) == -1) {
82*57718be8SEnji Cooper 		if (errno != EEXIST)
83*57718be8SEnji Cooper 			atf_tc_fail_errno("null create /null");
84*57718be8SEnji Cooper 	}
85*57718be8SEnji Cooper 	if (rump_sys_mkdir("/null/dev", 0777) == -1) {
86*57718be8SEnji Cooper 		if (errno != EEXIST)
87*57718be8SEnji Cooper 			atf_tc_fail_errno("null create /null/dev");
88*57718be8SEnji Cooper 	}
89*57718be8SEnji Cooper 
90*57718be8SEnji Cooper 	mountnull("/dev/pts", "/null/dev/pts", 0);
91*57718be8SEnji Cooper 
92*57718be8SEnji Cooper 	/*
93*57718be8SEnji Cooper 	 * get slave/master pair.
94*57718be8SEnji Cooper 	 */
95*57718be8SEnji Cooper 	ptm = rump_sys_open("/dev/ptm", O_RDWR);
96*57718be8SEnji Cooper 	if (rump_sys_ioctl(ptm, TIOCPTMGET, &ptg) == -1)
97*57718be8SEnji Cooper 		atf_tc_fail_errno("get pty");
98*57718be8SEnji Cooper 
99*57718be8SEnji Cooper 	/*
100*57718be8SEnji Cooper 	 * Build nullfs path to slave.
101*57718be8SEnji Cooper 	 */
102*57718be8SEnji Cooper 	strcpy(path, "/null");
103*57718be8SEnji Cooper 	strcat(path, ptg.sn);
104*57718be8SEnji Cooper 
105*57718be8SEnji Cooper 	/*
106*57718be8SEnji Cooper 	 * Open slave tty via nullfs.
107*57718be8SEnji Cooper 	 */
108*57718be8SEnji Cooper 	if (rump_sys_open(path, O_RDWR) == -1)
109*57718be8SEnji Cooper 		atf_tc_fail_errno("slave null open");
110*57718be8SEnji Cooper 
111*57718be8SEnji Cooper 	/*
112*57718be8SEnji Cooper 	 * Close slave opened with /dev/ptm.  Need purely non-null refs to it.
113*57718be8SEnji Cooper 	 */
114*57718be8SEnji Cooper 	rump_sys_close(ptg.sfd);
115*57718be8SEnji Cooper 
116*57718be8SEnji Cooper 	/* revoke slave tty. */
117*57718be8SEnji Cooper 	rump_sys_revoke(path);
118*57718be8SEnji Cooper 
119*57718be8SEnji Cooper 	/* done */
120*57718be8SEnji Cooper }
121*57718be8SEnji Cooper 
122*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
123*57718be8SEnji Cooper {
124*57718be8SEnji Cooper 
125*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, nullrevoke);
126*57718be8SEnji Cooper 
127*57718be8SEnji Cooper 	return atf_no_error();
128*57718be8SEnji Cooper }
129