1 /* $NetBSD: ptmx.c,v 1.4 2004/11/11 15:57:47 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: ptmx.c,v 1.4 2004/11/11 15:57:47 christos Exp $");
42 * On NetBSD /dev/ptyp0 == /dev/pts/0 so we can check for major
43 * and minor device numbers. This check is non-portable. This
44 * check is now disabled because we might not have /dev/ptyp0
47 #undef PTY_DEVNO_CHECK
50 main(int argc
, char *argv
[])
57 if ((fdm
= posix_openpt(O_RDWR
|O_NOCTTY
)) == -1) {
58 if (errno
== ENOENT
|| errno
== ENODEV
)
60 err(1, "open master");
63 if (fstat(fdm
, &stm
) == -1)
64 err(1, "fstat master");
66 #ifdef PTY_DEVNO_CHECK
67 if (stat("/dev/ptyp0", &sts
) == -1)
68 err(1, "stat `%s'", /dev
/ptyp0
);
70 if (major(stm
.st_rdev
) != major(sts
.st_rdev
))
71 errx(1, "bad master major number %d", major(stm
.st_rdev
));
74 if (grantpt(fdm
) == -1)
77 if (unlockpt(fdm
) == -1)
80 if ((pty
= ptsname(fdm
)) == NULL
)
83 if ((fds
= open(pty
, O_RDWR
|O_NOCTTY
)) == -1)
86 if (fstat(fds
, &sts
) == -1)
87 err(1, "fstat slave");
89 #ifdef PTY_DEVNO_CHECK
90 if (minor(stm
.st_rdev
) != minor(sts
.st_rdev
))
91 errx(1, "bad slave minor number %d", major(stm
.st_rdev
));
94 if (sts
.st_uid
!= getuid())
95 errx(1, "bad slave uid %lu != %lu", (unsigned long)stm
.st_uid
,
98 if ((gp
= getgrnam("tty")) == NULL
)
99 errx(1, "cannot find `tty' group");
101 if (sts
.st_gid
!= gp
->gr_gid
)
102 errx(1, "bad slave gid %lu != %lu", (unsigned long)stm
.st_gid
,