xref: /freebsd/contrib/netbsd-tests/fs/ptyfs/t_nullpts.c (revision 1a36faad54665288ed4eb839d2a4699ae2ead45e)
1*63d1fd59SEnji Cooper /*	$NetBSD: t_nullpts.c,v 1.6 2017/01/13 21:30:40 christos Exp $	*/
257718be8SEnji Cooper 
357718be8SEnji Cooper #include <sys/types.h>
457718be8SEnji Cooper #include <sys/mount.h>
557718be8SEnji Cooper #include <sys/ioctl.h>
657718be8SEnji Cooper 
757718be8SEnji Cooper #include <atf-c.h>
857718be8SEnji Cooper #include <err.h>
957718be8SEnji Cooper #include <errno.h>
1057718be8SEnji Cooper #include <fcntl.h>
1157718be8SEnji Cooper #include <stdio.h>
1257718be8SEnji Cooper #include <unistd.h>
1357718be8SEnji Cooper #include <string.h>
1457718be8SEnji Cooper #include <stdlib.h>
1557718be8SEnji Cooper 
1657718be8SEnji Cooper #include <rump/rump.h>
1757718be8SEnji Cooper #include <rump/rump_syscalls.h>
1857718be8SEnji Cooper 
1957718be8SEnji Cooper #include <fs/ptyfs/ptyfs.h>
2057718be8SEnji Cooper #include <miscfs/nullfs/null.h>
2157718be8SEnji Cooper 
22*63d1fd59SEnji Cooper #include "h_macros.h"
2357718be8SEnji Cooper 
2457718be8SEnji Cooper static void
mountptyfs(const char * mp,int flags)2557718be8SEnji Cooper mountptyfs(const char *mp, int flags)
2657718be8SEnji Cooper {
2757718be8SEnji Cooper 	struct ptyfs_args args;
2857718be8SEnji Cooper 
2957718be8SEnji Cooper 	if (rump_sys_mkdir(mp, 0777) == -1) {
3057718be8SEnji Cooper 		if (errno != EEXIST)
3157718be8SEnji Cooper 			atf_tc_fail_errno("null create %s", mp);
3257718be8SEnji Cooper 	}
3357718be8SEnji Cooper 	memset(&args, 0, sizeof(args));
3457718be8SEnji Cooper 	args.version = PTYFS_ARGSVERSION;
3557718be8SEnji Cooper 	args.mode = 0777;
3657718be8SEnji Cooper 	if (rump_sys_mount(MOUNT_PTYFS, mp, flags, &args, sizeof(args)) == -1)
3757718be8SEnji Cooper 		atf_tc_fail_errno("could not mount ptyfs");
3857718be8SEnji Cooper }
3957718be8SEnji Cooper 
4057718be8SEnji Cooper static void
mountnull(const char * what,const char * mp,int flags)4157718be8SEnji Cooper mountnull(const char *what, const char *mp, int flags)
4257718be8SEnji Cooper {
4357718be8SEnji Cooper 	struct null_args nargs;
4457718be8SEnji Cooper 
4557718be8SEnji Cooper 	if (rump_sys_mkdir(what, 0777) == -1) {
4657718be8SEnji Cooper 		if (errno != EEXIST)
4757718be8SEnji Cooper 			atf_tc_fail_errno("null create %s", what);
4857718be8SEnji Cooper 	}
4957718be8SEnji Cooper 	if (rump_sys_mkdir(mp, 0777) == -1) {
5057718be8SEnji Cooper 		if (errno != EEXIST)
5157718be8SEnji Cooper 			atf_tc_fail_errno("null create %s", mp);
5257718be8SEnji Cooper 	}
5357718be8SEnji Cooper 	memset(&nargs, 0, sizeof(nargs));
5457718be8SEnji Cooper 	nargs.nulla_target = __UNCONST(what);
5557718be8SEnji Cooper 	if (rump_sys_mount(MOUNT_NULL, mp, flags, &nargs, sizeof(nargs)) == -1)
5657718be8SEnji Cooper 		atf_tc_fail_errno("could not mount nullfs");
5757718be8SEnji Cooper }
5857718be8SEnji Cooper 
5957718be8SEnji Cooper ATF_TC(nullrevoke);
ATF_TC_HEAD(nullrevoke,tc)6057718be8SEnji Cooper ATF_TC_HEAD(nullrevoke, tc)
6157718be8SEnji Cooper {
6257718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "null mount ptyfs and revoke");
6357718be8SEnji Cooper }
6457718be8SEnji Cooper 
ATF_TC_BODY(nullrevoke,tc)6557718be8SEnji Cooper ATF_TC_BODY(nullrevoke, tc)
6657718be8SEnji Cooper {
6757718be8SEnji Cooper 	char path[MAXPATHLEN];
6857718be8SEnji Cooper 	struct ptmget ptg;
6957718be8SEnji Cooper 	int ptm;
7057718be8SEnji Cooper 
7157718be8SEnji Cooper 	rump_init();
7257718be8SEnji Cooper 
7357718be8SEnji Cooper 	/*
7457718be8SEnji Cooper 	 * mount /dev/pts
7557718be8SEnji Cooper 	 */
7657718be8SEnji Cooper 	mountptyfs("/dev/pts", 0);
7757718be8SEnji Cooper 
7857718be8SEnji Cooper 	/*
7957718be8SEnji Cooper 	 * null mount /dev/pts to /null/dev/pts
8057718be8SEnji Cooper 	 */
8157718be8SEnji Cooper 	if (rump_sys_mkdir("/null", 0777) == -1) {
8257718be8SEnji Cooper 		if (errno != EEXIST)
8357718be8SEnji Cooper 			atf_tc_fail_errno("null create /null");
8457718be8SEnji Cooper 	}
8557718be8SEnji Cooper 	if (rump_sys_mkdir("/null/dev", 0777) == -1) {
8657718be8SEnji Cooper 		if (errno != EEXIST)
8757718be8SEnji Cooper 			atf_tc_fail_errno("null create /null/dev");
8857718be8SEnji Cooper 	}
8957718be8SEnji Cooper 
9057718be8SEnji Cooper 	mountnull("/dev/pts", "/null/dev/pts", 0);
9157718be8SEnji Cooper 
9257718be8SEnji Cooper 	/*
9357718be8SEnji Cooper 	 * get slave/master pair.
9457718be8SEnji Cooper 	 */
9557718be8SEnji Cooper 	ptm = rump_sys_open("/dev/ptm", O_RDWR);
9657718be8SEnji Cooper 	if (rump_sys_ioctl(ptm, TIOCPTMGET, &ptg) == -1)
9757718be8SEnji Cooper 		atf_tc_fail_errno("get pty");
9857718be8SEnji Cooper 
9957718be8SEnji Cooper 	/*
10057718be8SEnji Cooper 	 * Build nullfs path to slave.
10157718be8SEnji Cooper 	 */
10257718be8SEnji Cooper 	strcpy(path, "/null");
10357718be8SEnji Cooper 	strcat(path, ptg.sn);
10457718be8SEnji Cooper 
10557718be8SEnji Cooper 	/*
10657718be8SEnji Cooper 	 * Open slave tty via nullfs.
10757718be8SEnji Cooper 	 */
10857718be8SEnji Cooper 	if (rump_sys_open(path, O_RDWR) == -1)
10957718be8SEnji Cooper 		atf_tc_fail_errno("slave null open");
11057718be8SEnji Cooper 
11157718be8SEnji Cooper 	/*
11257718be8SEnji Cooper 	 * Close slave opened with /dev/ptm.  Need purely non-null refs to it.
11357718be8SEnji Cooper 	 */
11457718be8SEnji Cooper 	rump_sys_close(ptg.sfd);
11557718be8SEnji Cooper 
11657718be8SEnji Cooper 	/* revoke slave tty. */
11757718be8SEnji Cooper 	rump_sys_revoke(path);
11857718be8SEnji Cooper 
11957718be8SEnji Cooper 	/* done */
12057718be8SEnji Cooper }
12157718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)12257718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
12357718be8SEnji Cooper {
12457718be8SEnji Cooper 
12557718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, nullrevoke);
12657718be8SEnji Cooper 
12757718be8SEnji Cooper 	return atf_no_error();
12857718be8SEnji Cooper }
129