1 /* $NetBSD: ptm.c,v 1.4 2004/11/11 15:58:59 christos Exp $ */
4 * Copyright (c) 2004 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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: ptm.c,v 1.4 2004/11/11 15:58:59 christos Exp $");
40 #include <sys/ioctl.h>
43 main(int argc
, char *argv
[])
51 if ((fdm
= open("/dev/ptm", O_RDWR
)) == -1) {
52 if (errno
== ENOENT
|| errno
== ENODEV
)
54 err(1, "open multiplexor");
57 if (fstat(fdm
, &stm
) == -1)
58 err(1, "fstat multiplexor");
60 if (major(stm
.st_rdev
) != 165)
61 errx(1, "bad multiplexor major number %d", major(stm
.st_rdev
));
63 if (ioctl(fdm
, TIOCPTMGET
, &ptm
) == -1)
64 err(1, "ioctl(TIOCPTMGET)");
66 if (strncmp(ptm
.cn
, "/dev/pty", 8) != 0)
67 if (strncmp(ptm
.cn
, "/dev/null", 9) != 0)
68 errx(1, "bad master name %s", ptm
.cn
);
70 if (strncmp(ptm
.sn
, "/dev/tty", 8) != 0)
71 if (strncmp(ptm
.sn
, "/dev/pts/", 9) != 0)
72 errx(1, "bad slave name %s", ptm
.sn
);
74 if (strncmp(ptm
.cn
, "/dev/null", 9) != 0) {
75 if (fstat(ptm
.cfd
, &stm
) == -1)
76 err(1, "fstat master");
77 if (stat(ptm
.cn
, &sts
) == -1)
78 err(1, "stat master");
79 if (stm
.st_rdev
!= sts
.st_rdev
)
80 errx(1, "master device mismatch %lu != %lu",
81 (unsigned long)stm
.st_rdev
,
82 (unsigned long)sts
.st_rdev
);
85 if (fstat(ptm
.sfd
, &stm
) == -1)
86 err(1, "fstat slave");
87 if (stat(ptm
.sn
, &sts
) == -1)
89 if (stm
.st_rdev
!= sts
.st_rdev
)
90 errx(1, "slave device mismatch %lu != %lu",
91 (unsigned long)stm
.st_rdev
, (unsigned long)sts
.st_rdev
);
93 if (sts
.st_uid
!= getuid())
94 errx(1, "bad slave uid %lu != %lu", (unsigned long)stm
.st_uid
,
96 if ((gp
= getgrnam("tty")) == NULL
)
97 errx(1, "cannot find `tty' group");
98 if (sts
.st_gid
!= gp
->gr_gid
)
99 errx(1, "bad slave gid %lu != %lu", (unsigned long)stm
.st_gid
,
101 (void)close(ptm
.sfd
);
102 (void)close(ptm
.cfd
);