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