1 /* $NetBSD: t_ptm.c,v 1.1 2011/01/13 03:19:57 pgoyette Exp $ */
4 * Copyright (c) 2004, 2008 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __COPYRIGHT("@(#) Copyright (c) 2008\
34 The NetBSD Foundation, inc. All rights reserved.");
35 __RCSID("$NetBSD: t_ptm.c,v 1.1 2011/01/13 03:19:57 pgoyette Exp $");
37 #include <sys/ioctl.h>
49 #define REQUIRE_ERRNO(x, v) \
50 ATF_REQUIRE_MSG(x != v, "%s: %s", #x, strerror(errno))
57 atf_tc_set_md_var(tc
, "descr", "Checks /dev/ptm device");
67 if ((fdm
= open("/dev/ptm", O_RDWR
)) == -1) {
68 if (errno
== ENOENT
|| errno
== ENODEV
)
69 atf_tc_skip("/dev/ptm: %s", strerror(errno
));
70 atf_tc_fail("/dev/ptm: %s", strerror(errno
));
73 REQUIRE_ERRNO(fstat(fdm
, &stm
), -1);
74 ATF_REQUIRE_EQ(major(stm
.st_rdev
), 165);
75 REQUIRE_ERRNO(ioctl(fdm
, TIOCPTMGET
, &ptm
), -1);
77 ATF_REQUIRE_MSG(strncmp(ptm
.cn
, "/dev/pty", 8) == 0
78 || strncmp(ptm
.cn
, "/dev/null", 9) == 0,
79 "bad master name: %s", ptm
.cn
);
81 ATF_REQUIRE_MSG(strncmp(ptm
.sn
, "/dev/tty", 8) == 0
82 || strncmp(ptm
.sn
, "/dev/pts/", 9) == 0,
83 "bad slave name: %s", ptm
.sn
);
85 if (strncmp(ptm
.cn
, "/dev/null", 9) != 0) {
86 REQUIRE_ERRNO(fstat(ptm
.cfd
, &stm
), -1);
87 REQUIRE_ERRNO(stat(ptm
.cn
, &sts
), -1);
88 ATF_REQUIRE_EQ(stm
.st_rdev
, sts
.st_rdev
);
91 REQUIRE_ERRNO(fstat(ptm
.sfd
, &stm
), -1);
92 REQUIRE_ERRNO(stat(ptm
.sn
, &sts
), -1);
93 ATF_REQUIRE_EQ(stm
.st_rdev
, sts
.st_rdev
);
95 ATF_REQUIRE_EQ_MSG(sts
.st_uid
, getuid(), "bad slave uid");
97 ATF_REQUIRE_MSG((gp
= getgrnam("tty")) != NULL
,
98 "cannot find `tty' group");
99 ATF_REQUIRE_EQ_MSG(sts
.st_gid
, gp
->gr_gid
, "bad slave grid");
101 (void)close(ptm
.sfd
);
102 (void)close(ptm
.cfd
);
107 * On NetBSD /dev/ptyp0 == /dev/pts/0 so we can check for major
108 * and minor device numbers. This check is non-portable. This
109 * check is now disabled because we might not have /dev/ptyp0
114 * #define PTY_DEVNO_CHECK
119 ATF_TC_HEAD(ptmx
, tc
)
122 atf_tc_set_md_var(tc
, "descr", "Checks /dev/ptmx device");
125 ATF_TC_BODY(ptmx
, tc
)
127 struct stat stm
, sts
;
132 if ((fdm
= posix_openpt(O_RDWR
|O_NOCTTY
)) == -1) {
133 if (errno
== ENOENT
|| errno
== ENODEV
)
134 atf_tc_skip("/dev/ptmx: %s", strerror(errno
));
136 atf_tc_fail("/dev/ptmx: %s", strerror(errno
));
139 REQUIRE_ERRNO(fstat(fdm
, &stm
), -1);
141 #ifdef PTY_DEVNO_CHECK
142 REQUIRE_ERRNO(stat("/dev/ptyp0", &sts
), -1);
144 ATF_REQUIRE_EQ_MSG(major(stm
.st_rdev
), major(sts
.st_rdev
),
145 "bad master major number");
148 REQUIRE_ERRNO(grantpt(fdm
), -1);
149 REQUIRE_ERRNO(unlockpt(fdm
), -1);
150 REQUIRE_ERRNO((pty
= ptsname(fdm
)), NULL
);
152 REQUIRE_ERRNO((fds
= open(pty
, O_RDWR
|O_NOCTTY
)), -1);
153 REQUIRE_ERRNO(fstat(fds
, &sts
), -1);
155 #ifdef PTY_DEVNO_CHECK
156 ATF_REQUIRE_EQ_MSG(minor(stm
.st_rdev
), minor(sts
.st_rdev
),
157 "bad slave minor number");
160 ATF_REQUIRE_EQ_MSG(sts
.st_uid
, getuid(), "bad slave uid");
161 ATF_REQUIRE_MSG((gp
= getgrnam("tty")) != NULL
,
162 "cannot find `tty' group");
164 ATF_REQUIRE_EQ_MSG(sts
.st_gid
, gp
->gr_gid
, "bad slave gid");
170 ATF_TP_ADD_TC(tp
, ptm
);
171 ATF_TP_ADD_TC(tp
, ptmx
);
173 return atf_no_error();