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$");
38 #include "namespace.h"
39 #include <sys/types.h>
43 #include <sys/resource.h>
44 #include <sys/sysctl.h>
45 #include <sys/ioctl.h>
57 #include "un-namespace.h"
59 #define PTM_PREFIX "pty" /* pseudo tty master naming convention */
60 #define PTS_PREFIX "tty" /* pseudo tty slave naming convention */
61 #define NEWPTS_PREFIX "pts/"
65 * The following are range values for pseudo TTY devices. Pseudo TTYs have a
66 * name of /dev/[pt]ty[p-sP-S][0-9a-v], yielding 256 combinations per major.
69 #define PT_DEV1 "pqrsPQRS"
70 #define PT_DEV2 "0123456789abcdefghijklmnopqrstuv"
73 * grantpt(3) support utility.
75 #define _PATH_PTCHOWN "/usr/libexec/pt_chown"
78 * ISPTM(x) returns 0 for struct stat x if x is not a pty master.
79 * The bounds checking may be unnecessary but it does eliminate doubt.
81 #define ISPTM(x) (S_ISCHR((x).st_mode) && \
82 minor((x).st_rdev) >= 0 && \
83 minor((x).st_rdev) < PT_MAX)
91 return (_ioctl(fd, TIOCGPTN, &nb) == 0);
101 len = sizeof(use_pts);
102 error = sysctlbyname("kern.pts.enable", &use_pts, &len, NULL, 0);
106 if (stat("/dev/ptmx", &sb) != 0)
114 * grantpt(): grant ownership of a slave pseudo-terminal device to the
121 int retval, serrno, status;
125 sigset_t oblock, nblock;
131 if ((slave = ptsname(fildes)) != NULL) {
135 (void)sigemptyset(&nblock);
136 (void)sigaddset(&nblock, SIGCHLD);
137 (void)_sigprocmask(SIG_BLOCK, &nblock, &oblock);
139 switch (pid = fork()) {
144 * pt_chown expects the master pseudo TTY to be its
147 (void)_dup2(fildes, STDIN_FILENO);
148 (void)_sigprocmask(SIG_SETMASK, &oblock, NULL);
149 execl(_PATH_PTCHOWN, _PATH_PTCHOWN, (char *)NULL);
150 _exit(EX_UNAVAILABLE);
152 default: /* parent */
154 * Just wait for the process. Error checking is
157 while ((spid = _waitpid(pid, &status, 0)) == -1 &&
160 if (spid != -1 && WIFEXITED(status) &&
161 WEXITSTATUS(status) == EX_OK)
169 * Restore process's signal mask.
171 (void)_sigprocmask(SIG_SETMASK, &oblock, NULL);
175 * pt_chown failed. Try to manually change the
176 * permissions for the slave.
178 gid = (grp = getgrnam("tty")) ? grp->gr_gid : -1;
179 if (chown(slave, getuid(), gid) == -1 ||
180 chmod(slave, S_IRUSR | S_IWUSR | S_IWGRP) == -1)
194 * posix_openpt(): open the first available master pseudo-terminal device
195 * and return descriptor.
198 posix_openpt(int oflag)
200 char *mc1, *mc2, master[] = _PATH_DEV PTM_PREFIX "XY";
201 const char *pc1, *pc2;
202 int fildes, bflag, serrno;
209 * Check flag validity. POSIX doesn't require it,
210 * but we still do so.
212 if (oflag & ~(O_RDWR | O_NOCTTY))
216 fildes = _open(_PATH_DEV PTMX, oflag);
219 mc1 = master + strlen(_PATH_DEV PTM_PREFIX);
222 /* Cycle through all possible master PTY devices. */
223 for (pc1 = PT_DEV1; !bflag && (*mc1 = *pc1); ++pc1)
224 for (pc2 = PT_DEV2; (*mc2 = *pc2) != '\0'; ++pc2) {
226 * Break out if we successfully open a PTY,
227 * or if open() fails due to limits.
229 if ((fildes = _open(master, oflag)) != -1 ||
230 (errno == EMFILE || errno == ENFILE)) {
246 * ptsname(): return the pathname of the slave pseudo-terminal device
247 * associated with the specified master.
252 static char slave[] = _PATH_DEV PTS_PREFIX "XY";
253 static char new_slave[] = _PATH_DEV NEWPTS_PREFIX "4294967295";
259 if (_fstat(fildes, &sbuf) == 0) {
263 if (!is_pts(fildes)) {
264 (void)snprintf(slave, sizeof(slave),
265 _PATH_DEV PTS_PREFIX "%s",
266 devname(sbuf.st_rdev, S_IFCHR) +
270 (void)snprintf(new_slave, sizeof(new_slave),
271 _PATH_DEV NEWPTS_PREFIX "%s",
272 devname(sbuf.st_rdev, S_IFCHR) +
283 * unlockpt(): unlock a pseudo-terminal device pair.
292 * Unlocking a master/slave pseudo-terminal pair has no meaning in a
293 * non-streams PTY environment. However, we do ensure fildes is a
294 * valid master pseudo-terminal device.
296 if ((retval = _fstat(fildes, &sbuf)) == 0 && !ISPTM(sbuf)) {