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 PTYM_PREFIX "pty" /* pty(4) master naming convention */
60 #define PTYS_PREFIX "tty" /* pty(4) slave naming convention */
61 #define PTMXS_PREFIX "pts/" /* pts(4) slave naming convention */
65 * The following are range values for pseudo TTY devices. Pseudo TTYs have a
66 * name of /dev/[pt]ty[l-sL-S][0-9a-v].
68 #define PTY_DEV1 "pqrsPQRSlmnoLMNO"
69 #define PTY_DEV2 "0123456789abcdefghijklmnopqrstuv"
72 * grantpt(3) support utility.
74 #define _PATH_PTCHOWN "/usr/libexec/pt_chown"
84 len = sizeof(use_pts);
85 error = sysctlbyname("kern.pts.enable", &use_pts, &len, NULL, 0);
89 if (stat(_PATH_DEV PTMX, &sb) != 0)
98 * grantpt(): grant ownership of a slave pseudo-terminal device to the
105 int retval, serrno, status;
109 sigset_t oblock, nblock;
115 if ((slave = ptsname(fildes)) != NULL) {
119 (void)sigemptyset(&nblock);
120 (void)sigaddset(&nblock, SIGCHLD);
121 (void)_sigprocmask(SIG_BLOCK, &nblock, &oblock);
123 switch (pid = fork()) {
128 * pt_chown expects the master pseudo TTY to be its
131 (void)_dup2(fildes, STDIN_FILENO);
132 (void)_sigprocmask(SIG_SETMASK, &oblock, NULL);
133 execl(_PATH_PTCHOWN, _PATH_PTCHOWN, (char *)NULL);
134 _exit(EX_UNAVAILABLE);
136 default: /* parent */
138 * Just wait for the process. Error checking is
141 while ((spid = _waitpid(pid, &status, 0)) == -1 &&
144 if (spid != -1 && WIFEXITED(status) &&
145 WEXITSTATUS(status) == EX_OK)
153 * Restore process's signal mask.
155 (void)_sigprocmask(SIG_SETMASK, &oblock, NULL);
159 * pt_chown failed. Try to manually change the
160 * permissions for the slave.
162 gid = (grp = getgrnam("tty")) ? grp->gr_gid : -1;
163 if (chown(slave, getuid(), gid) == -1 ||
164 chmod(slave, S_IRUSR | S_IWUSR | S_IWGRP) == -1)
178 * posix_openpt(): open the first available master pseudo-terminal device
179 * and return descriptor.
182 posix_openpt(int oflag)
184 char *mc1, *mc2, master[] = _PATH_DEV PTYM_PREFIX "XY";
185 const char *pc1, *pc2;
186 int fildes, bflag, serrno;
193 * Check flag validity. POSIX doesn't require it,
194 * but we still do so.
196 if (oflag & ~(O_RDWR | O_NOCTTY))
201 fildes = _open(_PATH_DEV PTMX, oflag);
205 mc1 = master + strlen(_PATH_DEV PTYM_PREFIX);
208 /* Cycle through all possible master PTY devices. */
209 for (pc1 = PTY_DEV1; !bflag && (*mc1 = *pc1); ++pc1)
210 for (pc2 = PTY_DEV2; (*mc2 = *pc2) != '\0'; ++pc2) {
212 * Break out if we successfully open a PTY,
213 * or if open() fails due to limits.
215 if ((fildes = _open(master, oflag)) != -1 ||
216 (errno == EMFILE || errno == ENFILE)) {
232 * ptsname(): return the pathname of the slave pseudo-terminal device
233 * associated with the specified master.
238 static char pty_slave[] = _PATH_DEV PTYS_PREFIX "XY";
240 static char ptmx_slave[] = _PATH_DEV PTMXS_PREFIX "4294967295";
247 /* Handle pts(4) masters first. */
248 if (_ioctl(fildes, TIOCGPTN, &ptn) == 0) {
249 (void)snprintf(ptmx_slave, sizeof(ptmx_slave),
250 _PATH_DEV PTMXS_PREFIX "%d", ptn);
255 /* All master pty's must be char devices. */
256 if (_fstat(fildes, &sbuf) == -1)
258 if (!S_ISCHR(sbuf.st_mode))
261 /* Check to see if this device is a pty(4) master. */
262 master = devname(sbuf.st_rdev, S_IFCHR);
263 if (strlen(master) != strlen(PTYM_PREFIX "XY"))
265 if (strncmp(master, PTYM_PREFIX, strlen(PTYM_PREFIX)) != 0)
268 /* It is, so generate the corresponding pty(4) slave name. */
269 (void)snprintf(pty_slave, sizeof(pty_slave), _PATH_DEV PTYS_PREFIX "%s",
270 master + strlen(PTYM_PREFIX));
279 * unlockpt(): unlock a pseudo-terminal device pair.
287 * Even though unlocking a PTY has no meaning in a non-streams
288 * PTY environment, make this function call revoke() to ensure
289 * the PTY slave device is not being evesdropped.
291 if ((slave = ptsname(fildes)) == NULL)
294 if (revoke(slave) == -1) {