1272343Sngie/* $NetBSD: t_ptm.c,v 1.1 2011/01/13 03:19:57 pgoyette Exp $ */
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * This code is derived from software contributed to The NetBSD Foundation
8272343Sngie * by Christos Zoulas.
9272343Sngie *
10272343Sngie * Redistribution and use in source and binary forms, with or without
11272343Sngie * modification, are permitted provided that the following conditions
12272343Sngie * are met:
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in the
17272343Sngie *    documentation and/or other materials provided with the distribution.
18272343Sngie *
19272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29272343Sngie * POSSIBILITY OF SUCH DAMAGE.
30272343Sngie */
31272343Sngie
32272343Sngie#include <sys/cdefs.h>
33272343Sngie__COPYRIGHT("@(#) Copyright (c) 2008\
34272343Sngie The NetBSD Foundation, inc. All rights reserved.");
35272343Sngie__RCSID("$NetBSD: t_ptm.c,v 1.1 2011/01/13 03:19:57 pgoyette Exp $");
36272343Sngie
37272343Sngie#include <sys/ioctl.h>
38272343Sngie#include <sys/stat.h>
39272343Sngie
40272343Sngie#include <errno.h>
41272343Sngie#include <fcntl.h>
42272343Sngie#include <grp.h>
43272343Sngie#include <stdlib.h>
44272343Sngie#include <string.h>
45272343Sngie#include <unistd.h>
46272343Sngie
47272343Sngie#include <atf-c.h>
48272343Sngie
49272343Sngie#define REQUIRE_ERRNO(x, v) \
50272343Sngie		ATF_REQUIRE_MSG(x != v, "%s: %s", #x, strerror(errno))
51272343Sngie
52272343SngieATF_TC(ptm);
53272343Sngie
54272343SngieATF_TC_HEAD(ptm, tc)
55272343Sngie{
56272343Sngie
57272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks /dev/ptm device");
58272343Sngie}
59272343Sngie
60272343SngieATF_TC_BODY(ptm, tc)
61272343Sngie{
62272343Sngie	struct stat stm, sts;
63272343Sngie	struct ptmget ptm;
64272343Sngie	int fdm;
65272343Sngie	struct group *gp;
66272343Sngie
67272343Sngie	if ((fdm = open("/dev/ptm", O_RDWR)) == -1) {
68272343Sngie		if (errno == ENOENT || errno == ENODEV)
69272343Sngie			atf_tc_skip("/dev/ptm: %s", strerror(errno));
70272343Sngie		atf_tc_fail("/dev/ptm: %s", strerror(errno));
71272343Sngie	}
72272343Sngie
73272343Sngie	REQUIRE_ERRNO(fstat(fdm, &stm), -1);
74272343Sngie	ATF_REQUIRE_EQ(major(stm.st_rdev), 165);
75272343Sngie	REQUIRE_ERRNO(ioctl(fdm, TIOCPTMGET, &ptm), -1);
76272343Sngie
77272343Sngie	ATF_REQUIRE_MSG(strncmp(ptm.cn, "/dev/pty", 8) == 0
78272343Sngie		|| strncmp(ptm.cn, "/dev/null", 9) == 0,
79272343Sngie		"bad master name: %s", ptm.cn);
80272343Sngie
81272343Sngie	ATF_REQUIRE_MSG(strncmp(ptm.sn, "/dev/tty", 8) == 0
82272343Sngie		|| strncmp(ptm.sn, "/dev/pts/", 9) == 0,
83272343Sngie		"bad slave name: %s", ptm.sn);
84272343Sngie
85272343Sngie	if (strncmp(ptm.cn, "/dev/null", 9) != 0) {
86272343Sngie		REQUIRE_ERRNO(fstat(ptm.cfd, &stm), -1);
87272343Sngie		REQUIRE_ERRNO(stat(ptm.cn, &sts), -1);
88272343Sngie		ATF_REQUIRE_EQ(stm.st_rdev, sts.st_rdev);
89272343Sngie	}
90272343Sngie
91272343Sngie	REQUIRE_ERRNO(fstat(ptm.sfd, &stm), -1);
92272343Sngie	REQUIRE_ERRNO(stat(ptm.sn, &sts), -1);
93272343Sngie	ATF_REQUIRE_EQ(stm.st_rdev, sts.st_rdev);
94272343Sngie
95272343Sngie	ATF_REQUIRE_EQ_MSG(sts.st_uid, getuid(), "bad slave uid");
96272343Sngie
97272343Sngie	ATF_REQUIRE_MSG((gp = getgrnam("tty")) != NULL,
98272343Sngie	    "cannot find `tty' group");
99272343Sngie	ATF_REQUIRE_EQ_MSG(sts.st_gid, gp->gr_gid, "bad slave grid");
100272343Sngie
101272343Sngie	(void)close(ptm.sfd);
102272343Sngie	(void)close(ptm.cfd);
103272343Sngie	(void)close(fdm);
104272343Sngie}
105272343Sngie
106272343Sngie/*
107272343Sngie * On NetBSD /dev/ptyp0 == /dev/pts/0 so we can check for major
108272343Sngie * and minor device numbers. This check is non-portable. This
109272343Sngie * check is now disabled because we might not have /dev/ptyp0
110272343Sngie * at all.
111272343Sngie */
112272343Sngie
113272343Sngie/*
114272343Sngie * #define PTY_DEVNO_CHECK
115272343Sngie */
116272343Sngie
117272343SngieATF_TC(ptmx);
118272343Sngie
119272343SngieATF_TC_HEAD(ptmx, tc)
120272343Sngie{
121272343Sngie
122272343Sngie	atf_tc_set_md_var(tc, "descr", "Checks /dev/ptmx device");
123272343Sngie}
124272343Sngie
125272343SngieATF_TC_BODY(ptmx, tc)
126272343Sngie{
127272343Sngie	struct stat stm, sts;
128272343Sngie	char *pty;
129272343Sngie	int fdm, fds;
130272343Sngie	struct group *gp;
131272343Sngie
132272343Sngie	if ((fdm = posix_openpt(O_RDWR|O_NOCTTY)) == -1) {
133272343Sngie		if (errno == ENOENT || errno == ENODEV)
134272343Sngie			atf_tc_skip("/dev/ptmx: %s", strerror(errno));
135272343Sngie
136272343Sngie		atf_tc_fail("/dev/ptmx: %s", strerror(errno));
137272343Sngie	}
138272343Sngie
139272343Sngie	REQUIRE_ERRNO(fstat(fdm, &stm), -1);
140272343Sngie
141272343Sngie#ifdef PTY_DEVNO_CHECK
142272343Sngie	REQUIRE_ERRNO(stat("/dev/ptyp0", &sts), -1);
143272343Sngie
144272343Sngie	ATF_REQUIRE_EQ_MSG(major(stm.st_rdev), major(sts.st_rdev),
145272343Sngie		"bad master major number");
146272343Sngie#endif
147272343Sngie
148272343Sngie	REQUIRE_ERRNO(grantpt(fdm), -1);
149272343Sngie	REQUIRE_ERRNO(unlockpt(fdm), -1);
150272343Sngie	REQUIRE_ERRNO((pty = ptsname(fdm)), NULL);
151272343Sngie
152272343Sngie	REQUIRE_ERRNO((fds = open(pty, O_RDWR|O_NOCTTY)), -1);
153272343Sngie	REQUIRE_ERRNO(fstat(fds, &sts), -1);
154272343Sngie
155272343Sngie#ifdef PTY_DEVNO_CHECK
156272343Sngie	ATF_REQUIRE_EQ_MSG(minor(stm.st_rdev), minor(sts.st_rdev),
157272343Sngie		"bad slave minor number");
158272343Sngie#endif
159272343Sngie
160272343Sngie	ATF_REQUIRE_EQ_MSG(sts.st_uid, getuid(), "bad slave uid");
161272343Sngie	ATF_REQUIRE_MSG((gp = getgrnam("tty")) != NULL,
162272343Sngie	    "cannot find `tty' group");
163272343Sngie
164272343Sngie	ATF_REQUIRE_EQ_MSG(sts.st_gid, gp->gr_gid, "bad slave gid");
165272343Sngie}
166272343Sngie
167272343SngieATF_TP_ADD_TCS(tp)
168272343Sngie{
169272343Sngie
170272343Sngie	ATF_TP_ADD_TC(tp, ptm);
171272343Sngie	ATF_TP_ADD_TC(tp, ptmx);
172272343Sngie
173272343Sngie	return atf_no_error();
174272343Sngie}
175