xref: /freebsd/contrib/netbsd-tests/lib/libc/ttyio/t_ptm.c (revision 9268022b74279434ed6300244e3f977e56a8ceb5)
1*57718be8SEnji Cooper /* $NetBSD: t_ptm.c,v 1.1 2011/01/13 03:19:57 pgoyette Exp $ */
2*57718be8SEnji Cooper 
3*57718be8SEnji Cooper /*
4*57718be8SEnji Cooper  * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
5*57718be8SEnji Cooper  * All rights reserved.
6*57718be8SEnji Cooper  *
7*57718be8SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8*57718be8SEnji Cooper  * by Christos Zoulas.
9*57718be8SEnji Cooper  *
10*57718be8SEnji Cooper  * Redistribution and use in source and binary forms, with or without
11*57718be8SEnji Cooper  * modification, are permitted provided that the following conditions
12*57718be8SEnji Cooper  * are met:
13*57718be8SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15*57718be8SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16*57718be8SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
17*57718be8SEnji Cooper  *    documentation and/or other materials provided with the distribution.
18*57718be8SEnji Cooper  *
19*57718be8SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*57718be8SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*57718be8SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*57718be8SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*57718be8SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*57718be8SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*57718be8SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*57718be8SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*57718be8SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*57718be8SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*57718be8SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
30*57718be8SEnji Cooper  */
31*57718be8SEnji Cooper 
32*57718be8SEnji Cooper #include <sys/cdefs.h>
33*57718be8SEnji Cooper __COPYRIGHT("@(#) Copyright (c) 2008\
34*57718be8SEnji Cooper  The NetBSD Foundation, inc. All rights reserved.");
35*57718be8SEnji Cooper __RCSID("$NetBSD: t_ptm.c,v 1.1 2011/01/13 03:19:57 pgoyette Exp $");
36*57718be8SEnji Cooper 
37*57718be8SEnji Cooper #include <sys/ioctl.h>
38*57718be8SEnji Cooper #include <sys/stat.h>
39*57718be8SEnji Cooper 
40*57718be8SEnji Cooper #include <errno.h>
41*57718be8SEnji Cooper #include <fcntl.h>
42*57718be8SEnji Cooper #include <grp.h>
43*57718be8SEnji Cooper #include <stdlib.h>
44*57718be8SEnji Cooper #include <string.h>
45*57718be8SEnji Cooper #include <unistd.h>
46*57718be8SEnji Cooper 
47*57718be8SEnji Cooper #include <atf-c.h>
48*57718be8SEnji Cooper 
49*57718be8SEnji Cooper #define REQUIRE_ERRNO(x, v) \
50*57718be8SEnji Cooper 		ATF_REQUIRE_MSG(x != v, "%s: %s", #x, strerror(errno))
51*57718be8SEnji Cooper 
52*57718be8SEnji Cooper ATF_TC(ptm);
53*57718be8SEnji Cooper 
ATF_TC_HEAD(ptm,tc)54*57718be8SEnji Cooper ATF_TC_HEAD(ptm, tc)
55*57718be8SEnji Cooper {
56*57718be8SEnji Cooper 
57*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks /dev/ptm device");
58*57718be8SEnji Cooper }
59*57718be8SEnji Cooper 
ATF_TC_BODY(ptm,tc)60*57718be8SEnji Cooper ATF_TC_BODY(ptm, tc)
61*57718be8SEnji Cooper {
62*57718be8SEnji Cooper 	struct stat stm, sts;
63*57718be8SEnji Cooper 	struct ptmget ptm;
64*57718be8SEnji Cooper 	int fdm;
65*57718be8SEnji Cooper 	struct group *gp;
66*57718be8SEnji Cooper 
67*57718be8SEnji Cooper 	if ((fdm = open("/dev/ptm", O_RDWR)) == -1) {
68*57718be8SEnji Cooper 		if (errno == ENOENT || errno == ENODEV)
69*57718be8SEnji Cooper 			atf_tc_skip("/dev/ptm: %s", strerror(errno));
70*57718be8SEnji Cooper 		atf_tc_fail("/dev/ptm: %s", strerror(errno));
71*57718be8SEnji Cooper 	}
72*57718be8SEnji Cooper 
73*57718be8SEnji Cooper 	REQUIRE_ERRNO(fstat(fdm, &stm), -1);
74*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(major(stm.st_rdev), 165);
75*57718be8SEnji Cooper 	REQUIRE_ERRNO(ioctl(fdm, TIOCPTMGET, &ptm), -1);
76*57718be8SEnji Cooper 
77*57718be8SEnji Cooper 	ATF_REQUIRE_MSG(strncmp(ptm.cn, "/dev/pty", 8) == 0
78*57718be8SEnji Cooper 		|| strncmp(ptm.cn, "/dev/null", 9) == 0,
79*57718be8SEnji Cooper 		"bad master name: %s", ptm.cn);
80*57718be8SEnji Cooper 
81*57718be8SEnji Cooper 	ATF_REQUIRE_MSG(strncmp(ptm.sn, "/dev/tty", 8) == 0
82*57718be8SEnji Cooper 		|| strncmp(ptm.sn, "/dev/pts/", 9) == 0,
83*57718be8SEnji Cooper 		"bad slave name: %s", ptm.sn);
84*57718be8SEnji Cooper 
85*57718be8SEnji Cooper 	if (strncmp(ptm.cn, "/dev/null", 9) != 0) {
86*57718be8SEnji Cooper 		REQUIRE_ERRNO(fstat(ptm.cfd, &stm), -1);
87*57718be8SEnji Cooper 		REQUIRE_ERRNO(stat(ptm.cn, &sts), -1);
88*57718be8SEnji Cooper 		ATF_REQUIRE_EQ(stm.st_rdev, sts.st_rdev);
89*57718be8SEnji Cooper 	}
90*57718be8SEnji Cooper 
91*57718be8SEnji Cooper 	REQUIRE_ERRNO(fstat(ptm.sfd, &stm), -1);
92*57718be8SEnji Cooper 	REQUIRE_ERRNO(stat(ptm.sn, &sts), -1);
93*57718be8SEnji Cooper 	ATF_REQUIRE_EQ(stm.st_rdev, sts.st_rdev);
94*57718be8SEnji Cooper 
95*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(sts.st_uid, getuid(), "bad slave uid");
96*57718be8SEnji Cooper 
97*57718be8SEnji Cooper 	ATF_REQUIRE_MSG((gp = getgrnam("tty")) != NULL,
98*57718be8SEnji Cooper 	    "cannot find `tty' group");
99*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(sts.st_gid, gp->gr_gid, "bad slave grid");
100*57718be8SEnji Cooper 
101*57718be8SEnji Cooper 	(void)close(ptm.sfd);
102*57718be8SEnji Cooper 	(void)close(ptm.cfd);
103*57718be8SEnji Cooper 	(void)close(fdm);
104*57718be8SEnji Cooper }
105*57718be8SEnji Cooper 
106*57718be8SEnji Cooper /*
107*57718be8SEnji Cooper  * On NetBSD /dev/ptyp0 == /dev/pts/0 so we can check for major
108*57718be8SEnji Cooper  * and minor device numbers. This check is non-portable. This
109*57718be8SEnji Cooper  * check is now disabled because we might not have /dev/ptyp0
110*57718be8SEnji Cooper  * at all.
111*57718be8SEnji Cooper  */
112*57718be8SEnji Cooper 
113*57718be8SEnji Cooper /*
114*57718be8SEnji Cooper  * #define PTY_DEVNO_CHECK
115*57718be8SEnji Cooper  */
116*57718be8SEnji Cooper 
117*57718be8SEnji Cooper ATF_TC(ptmx);
118*57718be8SEnji Cooper 
ATF_TC_HEAD(ptmx,tc)119*57718be8SEnji Cooper ATF_TC_HEAD(ptmx, tc)
120*57718be8SEnji Cooper {
121*57718be8SEnji Cooper 
122*57718be8SEnji Cooper 	atf_tc_set_md_var(tc, "descr", "Checks /dev/ptmx device");
123*57718be8SEnji Cooper }
124*57718be8SEnji Cooper 
ATF_TC_BODY(ptmx,tc)125*57718be8SEnji Cooper ATF_TC_BODY(ptmx, tc)
126*57718be8SEnji Cooper {
127*57718be8SEnji Cooper 	struct stat stm, sts;
128*57718be8SEnji Cooper 	char *pty;
129*57718be8SEnji Cooper 	int fdm, fds;
130*57718be8SEnji Cooper 	struct group *gp;
131*57718be8SEnji Cooper 
132*57718be8SEnji Cooper 	if ((fdm = posix_openpt(O_RDWR|O_NOCTTY)) == -1) {
133*57718be8SEnji Cooper 		if (errno == ENOENT || errno == ENODEV)
134*57718be8SEnji Cooper 			atf_tc_skip("/dev/ptmx: %s", strerror(errno));
135*57718be8SEnji Cooper 
136*57718be8SEnji Cooper 		atf_tc_fail("/dev/ptmx: %s", strerror(errno));
137*57718be8SEnji Cooper 	}
138*57718be8SEnji Cooper 
139*57718be8SEnji Cooper 	REQUIRE_ERRNO(fstat(fdm, &stm), -1);
140*57718be8SEnji Cooper 
141*57718be8SEnji Cooper #ifdef PTY_DEVNO_CHECK
142*57718be8SEnji Cooper 	REQUIRE_ERRNO(stat("/dev/ptyp0", &sts), -1);
143*57718be8SEnji Cooper 
144*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(major(stm.st_rdev), major(sts.st_rdev),
145*57718be8SEnji Cooper 		"bad master major number");
146*57718be8SEnji Cooper #endif
147*57718be8SEnji Cooper 
148*57718be8SEnji Cooper 	REQUIRE_ERRNO(grantpt(fdm), -1);
149*57718be8SEnji Cooper 	REQUIRE_ERRNO(unlockpt(fdm), -1);
150*57718be8SEnji Cooper 	REQUIRE_ERRNO((pty = ptsname(fdm)), NULL);
151*57718be8SEnji Cooper 
152*57718be8SEnji Cooper 	REQUIRE_ERRNO((fds = open(pty, O_RDWR|O_NOCTTY)), -1);
153*57718be8SEnji Cooper 	REQUIRE_ERRNO(fstat(fds, &sts), -1);
154*57718be8SEnji Cooper 
155*57718be8SEnji Cooper #ifdef PTY_DEVNO_CHECK
156*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(minor(stm.st_rdev), minor(sts.st_rdev),
157*57718be8SEnji Cooper 		"bad slave minor number");
158*57718be8SEnji Cooper #endif
159*57718be8SEnji Cooper 
160*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(sts.st_uid, getuid(), "bad slave uid");
161*57718be8SEnji Cooper 	ATF_REQUIRE_MSG((gp = getgrnam("tty")) != NULL,
162*57718be8SEnji Cooper 	    "cannot find `tty' group");
163*57718be8SEnji Cooper 
164*57718be8SEnji Cooper 	ATF_REQUIRE_EQ_MSG(sts.st_gid, gp->gr_gid, "bad slave gid");
165*57718be8SEnji Cooper }
166*57718be8SEnji Cooper 
ATF_TP_ADD_TCS(tp)167*57718be8SEnji Cooper ATF_TP_ADD_TCS(tp)
168*57718be8SEnji Cooper {
169*57718be8SEnji Cooper 
170*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, ptm);
171*57718be8SEnji Cooper 	ATF_TP_ADD_TC(tp, ptmx);
172*57718be8SEnji Cooper 
173*57718be8SEnji Cooper 	return atf_no_error();
174*57718be8SEnji Cooper }
175