]>
git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/grantpt.c
2 * Copyright (c) 2002 The FreeBSD Project, Inc.
5 * This software includes code contributed to the FreeBSD Project
6 * by Ryan Younce of North Carolina State University.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the FreeBSD Project nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT AND CONTRIBUTORS ``AS IS''
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR ITS CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: src/lib/libc/stdlib/grantpt.c,v 1.2 2003/01/04 08:10:55 tjr Exp $");
38 #include "namespace.h"
39 #include <sys/types.h>
43 #include <sys/resource.h>
55 #include "un-namespace.h"
57 #define PTM_MAJOR 6 /* pseudo tty master major */
58 #define PTS_MAJOR 5 /* pseudo tty slave major */
59 #define PTM_PREFIX "pty" /* pseudo tty master naming convention */
60 #define PTS_PREFIX "tty" /* pseudo tty slave naming convention */
63 * The following are range values for pseudo TTY devices. Pseudo TTYs have a
64 * name of /dev/[pt]ty[p-sP-S][0-9a-v], yielding 256 combinations per major.
67 #define PT_DEV1 "pqrsPQRS"
68 #define PT_DEV2 "0123456789abcdefghijklmnopqrstuv"
71 * grantpt(3) support utility.
73 #define _PATH_PTCHOWN "/usr/libexec/pt_chown"
76 * ISPTM(x) returns 0 for struct stat x if x is not a pty master.
77 * The bounds checking may be unnecessary but it does eliminate doubt.
79 #define ISPTM(x) (S_ISCHR((x).st_mode) && \
80 major((x).st_rdev) == PTM_MAJOR && \
81 minor((x).st_rdev) >= 0 && \
82 minor((x).st_rdev) < PT_MAX)
85 * grantpt(): grant ownership of a slave pseudo-terminal device to the
92 int retval
, serrno
, status
;
96 sigset_t oblock
, nblock
;
102 if ((slave
= ptsname(fildes
)) != NULL
) {
106 (void)sigemptyset(&nblock
);
107 (void)sigaddset(&nblock
, SIGCHLD
);
108 (void)_sigprocmask(SIG_BLOCK
, &nblock
, &oblock
);
110 switch (pid
= fork()) {
115 * pt_chown expects the master pseudo TTY to be its
118 (void)_dup2(fildes
, STDIN_FILENO
);
119 (void)_sigprocmask(SIG_SETMASK
, &oblock
, NULL
);
120 execl(_PATH_PTCHOWN
, _PATH_PTCHOWN
, (char *)NULL
);
121 _exit(EX_UNAVAILABLE
);
123 default: /* parent */
125 * Just wait for the process. Error checking is
128 while ((spid
= _waitpid(pid
, &status
, 0)) == -1 &&
131 if (spid
!= -1 && WIFEXITED(status
) &&
132 WEXITSTATUS(status
) == EX_OK
)
140 * Restore process's signal mask.
142 (void)_sigprocmask(SIG_SETMASK
, &oblock
, NULL
);
146 * pt_chown failed. Try to manually change the
147 * permissions for the slave.
149 gid
= (grp
= getgrnam("tty")) ? grp
->gr_gid
: -1;
150 if (chown(slave
, getuid(), gid
) == -1 ||
151 chmod(slave
, S_IRUSR
| S_IWUSR
| S_IWGRP
) == -1)
165 * posix_openpt(): open the first available master pseudo-terminal device
166 * and return descriptor.
169 posix_openpt(int oflag
)
171 char *mc1
, *mc2
, master
[] = _PATH_DEV PTM_PREFIX
"XY";
172 const char *pc1
, *pc2
;
173 int fildes
, bflag
, serrno
;
180 * Check flag validity. POSIX doesn't require it,
181 * but we still do so.
183 if (oflag
& ~(O_RDWR
| O_NOCTTY
))
186 mc1
= master
+ strlen(_PATH_DEV PTM_PREFIX
);
189 /* Cycle through all possible master PTY devices. */
190 for (pc1
= PT_DEV1
; !bflag
&& (*mc1
= *pc1
); ++pc1
)
191 for (pc2
= PT_DEV2
; (*mc2
= *pc2
) != '\0'; ++pc2
) {
193 * Break out if we successfully open a PTY,
194 * or if open() fails due to limits.
196 if ((fildes
= _open(master
, oflag
)) != -1 ||
197 (errno
== EMFILE
|| errno
== ENFILE
)) {
213 * ptsname(): return the pathname of the slave pseudo-terminal device
214 * associated with the specified master.
219 static char slave
[] = _PATH_DEV PTS_PREFIX
"XY";
225 if (_fstat(fildes
, &sbuf
) == 0) {
229 (void)sprintf(slave
, _PATH_DEV PTS_PREFIX
"%c%c",
230 PT_DEV1
[minor(sbuf
.st_rdev
) / 32],
231 PT_DEV2
[minor(sbuf
.st_rdev
) % 32]);
240 * unlockpt(): unlock a pseudo-terminal device pair.
249 * Unlocking a master/slave pseudo-terminal pair has no meaning in a
250 * non-streams PTY environment. However, we do ensure fildes is a
251 * valid master pseudo-terminal device.
253 if ((retval
= _fstat(fildes
, &sbuf
)) == 0 && !ISPTM(sbuf
)) {