1 // This file is part of libptytty. Do not make local modifications.
2 // http://software.schmorp.de/pkg/libptytty
4 /*----------------------------------------------------------------------*
6 *----------------------------------------------------------------------*
8 * All portions of code are copyright by their respective author/s.
9 * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com>
10 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
11 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 *---------------------------------------------------------------------*/
28 #include "../config.h"
36 #include <sys/types.h>
41 #ifdef HAVE_SYS_IOCTL_H
42 # include <sys/ioctl.h>
44 #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
45 # include <sys/stropts.h> /* for I_PUSH */
47 #if defined(HAVE_ISASTREAM) && defined(HAVE_STROPTS_H)
50 #if defined(HAVE_PTY_H)
52 #elif defined(HAVE_LIBUTIL_H)
54 #elif defined(HAVE_UTIL_H)
57 #ifdef TTY_GID_SUPPORT
63 /////////////////////////////////////////////////////////////////////////////
65 /* ------------------------------------------------------------------------- *
66 * GET PSEUDO TELETYPE - MASTER AND SLAVE *
67 * ------------------------------------------------------------------------- */
69 * Returns pty file descriptor, or -1 on failure
70 * If successful, ttydev is set to the name of the slave device.
71 * fd_tty _may_ also be set to an open fd to the slave device
73 #if defined(UNIX98_PTY)
76 get_pty (int *fd_tty, char **ttydev)
80 # if defined(HAVE_GETPT)
82 # elif defined(HAVE_POSIX_OPENPT)
83 pfd = posix_openpt (O_RDWR);
85 pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0);
90 if (grantpt (pfd) == 0 /* change slave permissions */
91 && unlockpt (pfd) == 0)
93 /* slave now unlocked */
94 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
104 #elif defined(HAVE_OPENPTY)
107 get_pty (int *fd_tty, char **ttydev)
112 res = openpty (&pfd, fd_tty, NULL, NULL, NULL);
116 *ttydev = strdup (ttyname (*fd_tty));
123 #elif defined(HAVE__GETPTY)
126 get_pty (int *fd_tty, char **ttydev)
131 slave = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
135 *ttydev = strdup (slave);
142 #elif defined(HAVE_DEV_PTC)
145 get_pty (int *fd_tty, char **ttydev)
149 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
151 *ttydev = strdup (ttyname (pfd));
158 #elif defined(HAVE_DEV_CLONE)
161 get_pty (int *fd_tty, char **ttydev)
165 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
167 *ttydev = strdup (ptsname (pfd));
176 /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */
178 get_pty (int *fd_tty, char **ttydev)
184 const char *majors = "pqrstuvwxyzabcde";
185 const char *minors = "0123456789abcdef";
187 for (i = 0; i < 256; i++)
189 snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]);
190 snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]);
192 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
194 snprintf(pty_name, 32, "/dev/ptyp%d", i);
195 snprintf(tty_name, 32, "/dev/ttyp%d", i);
196 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1)
200 if (access (tty_name, R_OK | W_OK) == 0)
202 *ttydev = strdup (tty_name);
214 /*----------------------------------------------------------------------*/
216 * Returns tty file descriptor, or -1 on failure
219 get_tty (char *ttydev)
221 return open (ttydev, O_RDWR | O_NOCTTY, 0);
224 /*----------------------------------------------------------------------*/
226 * Make our tty a controlling tty so that /dev/tty points to us
229 control_tty (int fd_tty)
235 #if defined(HAVE_DEV_PTMX) && defined(I_PUSH)
237 * Push STREAMS modules:
238 * ptem: pseudo-terminal hardware emulation module.
239 * ldterm: standard terminal line discipline.
240 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
242 * After we push the STREAMS modules, the first open () on the slave side
243 * (i.e. the next section between the dashes giving us "tty opened OK")
244 * should make the "ptem" (or "ldterm" depending upon either which OS
245 * version or which set of manual pages you have) module give us a
246 * controlling terminal. We must already have close ()d the master side
247 * fd in this child process before we push STREAMS modules on because the
248 * documentation is really unclear about whether it is any close () on
249 * the master side or the last close () - i.e. a proper STREAMS dismantling
250 * close () - on the master side which causes a hang up to be sent
251 * through - Geoff Wing
253 #if defined(HAVE_ISASTREAM) && defined(HAVE_STROPTS_H)
254 if (isastream (fd_tty) == 1)
257 ioctl (fd_tty, I_PUSH, "ptem");
258 ioctl (fd_tty, I_PUSH, "ldterm");
259 ioctl (fd_tty, I_PUSH, "ttcompat");
264 ioctl (fd_tty, TIOCSCTTY, NULL);
266 fd = open (ttyname (fd_tty), O_RDWR);
271 fd = open ("/dev/tty", O_WRONLY);
273 return -1; /* fatal */
291 ptytty::make_controlling_tty ()
293 return control_tty (tty) >= 0;
297 ptytty::set_utf8_mode (bool on)
305 if (tcgetattr (pty, &tio) != -1)
307 tcflag_t new_cflag = tio.c_iflag;
314 if (new_cflag != tio.c_iflag)
316 tio.c_iflag = new_cflag;
317 tcsetattr (pty, TCSANOW, &tio);
323 static struct ttyconf {
329 #ifdef TTY_GID_SUPPORT
330 struct group *gr = getgrnam ("tty");
334 /* change group ownership of tty to "tty" */
335 mode = S_IRUSR | S_IWUSR | S_IWGRP;
339 #endif /* TTY_GID_SUPPORT */
341 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
347 ptytty_unix::ptytty_unix ()
355 ptytty_unix::~ptytty_unix ()
368 chmod (name, RESTORE_TTY_MODE);
369 chown (name, 0, ttyconf.gid);
386 /* get master (pty) */
387 if ((pty = get_pty (&tty, &name)) < 0)
390 fcntl (pty, F_SETFL, O_NONBLOCK);
392 /* get slave (tty) */
395 #ifndef NO_SETOWNER_TTYDEV
396 chown (name, getuid (), ttyconf.gid); /* fail silently */
397 chmod (name, ttyconf.mode);
403 if ((tty = get_tty (name)) < 0)