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