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