2004-01-21 07:07:16 +01:00
|
|
|
/*
|
|
|
|
* Please note: this implementation of openpty() is far from complete.
|
|
|
|
* it is just enough for portable OpenSSH's needs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2004-02-17 06:49:41 +01:00
|
|
|
* Copyright (c) 2004 Damien Miller <djm@mindrot.org>
|
2004-01-21 07:07:16 +01:00
|
|
|
*
|
2004-02-17 06:49:41 +01:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2004-01-21 07:07:16 +01:00
|
|
|
*
|
2004-02-17 06:49:41 +01:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
2004-01-21 07:07:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
|
|
|
* Allocating a pseudo-terminal, and making it the controlling tty.
|
|
|
|
*
|
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
#if !defined(HAVE_OPENPTY)
|
|
|
|
|
2006-07-11 10:00:06 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
2006-08-06 13:25:24 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2006-07-11 10:00:06 +02:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
# include <sys/stat.h>
|
|
|
|
#endif
|
2006-08-24 11:52:30 +02:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
|
|
|
# include <sys/ioctl.h>
|
|
|
|
#endif
|
2006-07-11 10:00:06 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
# include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
2004-01-21 07:07:16 +01:00
|
|
|
#ifdef HAVE_UTIL_H
|
|
|
|
# include <util.h>
|
|
|
|
#endif /* HAVE_UTIL_H */
|
|
|
|
|
|
|
|
#ifdef HAVE_PTY_H
|
|
|
|
# include <pty.h>
|
|
|
|
#endif
|
|
|
|
#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
|
|
|
|
# include <sys/stropts.h>
|
|
|
|
#endif
|
|
|
|
|
2006-03-15 04:42:54 +01:00
|
|
|
#include <signal.h>
|
2006-07-24 07:08:35 +02:00
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2006-03-15 04:42:54 +01:00
|
|
|
|
2020-01-23 11:06:45 +01:00
|
|
|
#include "misc.h"
|
2022-02-14 04:19:40 +01:00
|
|
|
#include "log.h"
|
2020-01-23 11:06:45 +01:00
|
|
|
|
2004-01-21 07:07:16 +01:00
|
|
|
#ifndef O_NOCTTY
|
|
|
|
#define O_NOCTTY 0
|
|
|
|
#endif
|
|
|
|
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
#if defined(HAVE_DEV_PTMX) && !defined(HAVE__GETPTY)
|
|
|
|
static int
|
2022-02-14 04:19:40 +01:00
|
|
|
openpty_streams(int *amaster, int *aslave)
|
2004-01-21 07:07:16 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
|
|
|
|
* also has bsd-style ptys, but they simply do not work.)
|
|
|
|
*/
|
|
|
|
int ptm;
|
|
|
|
char *pts;
|
2020-01-23 11:06:45 +01:00
|
|
|
sshsig_t old_signal;
|
2004-01-21 07:07:16 +01:00
|
|
|
|
|
|
|
if ((ptm = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* XXX: need to close ptm on error? */
|
2020-01-23 08:10:22 +01:00
|
|
|
old_signal = ssh_signal(SIGCHLD, SIG_DFL);
|
2004-01-21 07:07:16 +01:00
|
|
|
if (grantpt(ptm) < 0)
|
|
|
|
return (-1);
|
2020-01-23 08:10:22 +01:00
|
|
|
ssh_signal(SIGCHLD, old_signal);
|
2004-01-21 07:07:16 +01:00
|
|
|
|
|
|
|
if (unlockpt(ptm) < 0)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if ((pts = ptsname(ptm)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
*amaster = ptm;
|
|
|
|
|
|
|
|
/* Open the slave side. */
|
|
|
|
if ((*aslave = open(pts, O_RDWR | O_NOCTTY)) == -1) {
|
|
|
|
close(*amaster);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
2019-04-26 10:06:34 +02:00
|
|
|
# if defined(I_FIND) && defined(__SVR4)
|
|
|
|
/*
|
|
|
|
* If the streams modules have already been pushed then there
|
|
|
|
* is no more work to do here.
|
|
|
|
*/
|
|
|
|
if (ioctl(*aslave, I_FIND, "ptem") != 0)
|
|
|
|
return 0;
|
|
|
|
# endif
|
|
|
|
|
2004-01-21 07:07:16 +01:00
|
|
|
/*
|
2016-08-02 01:44:25 +02:00
|
|
|
* Try to push the appropriate streams modules, as described
|
2004-01-21 07:07:16 +01:00
|
|
|
* in Solaris pts(7).
|
|
|
|
*/
|
|
|
|
ioctl(*aslave, I_PUSH, "ptem");
|
|
|
|
ioctl(*aslave, I_PUSH, "ldterm");
|
|
|
|
# ifndef __hpux
|
|
|
|
ioctl(*aslave, I_PUSH, "ttcompat");
|
|
|
|
# endif /* __hpux */
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
#endif
|
2004-01-21 07:07:16 +01:00
|
|
|
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
int
|
|
|
|
openpty(int *amaster, int *aslave, char *name, struct termios *termp,
|
|
|
|
struct winsize *winp)
|
|
|
|
{
|
|
|
|
#if defined(HAVE__GETPTY)
|
|
|
|
/*
|
|
|
|
* _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
|
|
|
|
* pty's automagically when needed
|
|
|
|
*/
|
|
|
|
char *slave;
|
|
|
|
|
|
|
|
if ((slave = _getpty(amaster, O_RDWR, 0622, 0)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* Open the slave side. */
|
|
|
|
if ((*aslave = open(slave, O_RDWR | O_NOCTTY)) == -1) {
|
|
|
|
close(*amaster);
|
|
|
|
return (-1);
|
|
|
|
}
|
2004-01-21 07:07:16 +01:00
|
|
|
return (0);
|
|
|
|
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
#elif defined(HAVE_DEV_PTMX)
|
|
|
|
|
|
|
|
#ifdef SSHD_ACQUIRES_CTTY
|
|
|
|
/*
|
|
|
|
* On some (most? all?) SysV based systems with STREAMS based terminals,
|
|
|
|
* sshd will acquire a controlling terminal when it pushes the "ptem"
|
|
|
|
* module. On such platforms, first allocate a sacrificial pty so
|
|
|
|
* that sshd already has a controlling terminal before allocating the
|
|
|
|
* one that will be passed back to the user process. This ensures
|
|
|
|
* the second pty is not already the controlling terminal for a
|
|
|
|
* different session and is available to become controlling terminal
|
|
|
|
* for the client's subprocess. See bugzilla #245 for details.
|
|
|
|
*/
|
2022-02-14 04:19:40 +01:00
|
|
|
int r, fd;
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
static int junk_ptyfd = -1, junk_ttyfd;
|
|
|
|
|
2022-02-14 04:19:40 +01:00
|
|
|
r = openpty_streams(amaster, aslave);
|
|
|
|
if (junk_ptyfd == -1 && (fd = open(_PATH_TTY, O_RDWR|O_NOCTTY)) >= 0) {
|
|
|
|
close(fd);
|
|
|
|
junk_ptyfd = *amaster;
|
|
|
|
junk_ttyfd = *aslave;
|
|
|
|
debug("STREAMS bug workaround pty %d tty %d name %s",
|
|
|
|
junk_ptyfd, junk_ttyfd, ttyname(junk_ttyfd));
|
|
|
|
} else
|
|
|
|
return r;
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
#endif
|
|
|
|
|
2022-02-14 04:19:40 +01:00
|
|
|
return openpty_streams(amaster, aslave);
|
Move SSHD_ACQUIRES_CTTY workaround into compat.
On some (most? all?) SysV based systems with STREAMS based ptys,
sshd could acquire a controlling terminal during pty setup when
it pushed the "ptem" module, due to what is probably a bug in
the STREAMS driver that's old enough to vote. Because it was the
privileged sshd's controlling terminal, it was not available for
the user's session, which ended up without one. This is known to
affect at least Solaris <=10, derivatives such as OpenIndiana and
several other SysV systems. See bz#245 for the backstory.
In the we past worked around that by not calling setsid in the
privileged sshd child, which meant it was not a session or process
group leader. This solved controlling terminal problem because sshd
was not eligble to acquire one, but had other side effects such as
not cleaning up helper subprocesses in the SIGALRM handler since it
was not PG leader. Recent cleanups in the signal handler uncovered
this, resulting in the LoginGraceTime timer not cleaning up privsep
unprivileged processes.
This change moves the workaround into the STREAMS pty allocation code,
by allocating a sacrificial pty to act as sshd's controlling terminal
before allocating user ptys, so those are still available for users'
sessions.
On the down side:
- this will waste a pty per ssh connection on affected platforms.
On the up side:
- it makes the process group behaviour consistent between platforms.
- it puts the workaround nearest the code that actually causes the
problem and competely out of the mainline code.
- the workaround is only activated if you use the STREAMS code. If,
say, Solaris 11 has the bug but also a working openpty() it doesn't
matter that we defined SSHD_ACQUIRES_CTTY.
- the workaround is only activated when the fist pty is allocated,
ie in the post-auth privsep monitor. This means there's no risk
of fd leaks to the unprivileged processes, and there's no effect on
sessions that do not allocate a pty.
Based on analysis and work by djm@, ok djm@
2022-02-11 11:00:35 +01:00
|
|
|
|
2004-01-21 07:07:16 +01:00
|
|
|
#elif defined(HAVE_DEV_PTS_AND_PTC)
|
|
|
|
/* AIX-style pty code. */
|
|
|
|
const char *ttname;
|
|
|
|
|
|
|
|
if ((*amaster = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1)
|
|
|
|
return (-1);
|
|
|
|
if ((ttname = ttyname(*amaster)) == NULL)
|
|
|
|
return (-1);
|
|
|
|
if ((*aslave = open(ttname, O_RDWR | O_NOCTTY)) == -1) {
|
|
|
|
close(*amaster);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* BSD-style pty code. */
|
|
|
|
char ptbuf[64], ttbuf[64];
|
|
|
|
int i;
|
|
|
|
const char *ptymajors = "pqrstuvwxyzabcdefghijklmno"
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
const char *ptyminors = "0123456789abcdef";
|
|
|
|
int num_minors = strlen(ptyminors);
|
|
|
|
int num_ptys = strlen(ptymajors) * num_minors;
|
|
|
|
struct termios tio;
|
|
|
|
|
|
|
|
for (i = 0; i < num_ptys; i++) {
|
2016-08-02 01:44:25 +02:00
|
|
|
snprintf(ptbuf, sizeof(ptbuf), "/dev/pty%c%c",
|
2004-01-21 07:07:16 +01:00
|
|
|
ptymajors[i / num_minors], ptyminors[i % num_minors]);
|
|
|
|
snprintf(ttbuf, sizeof(ttbuf), "/dev/tty%c%c",
|
|
|
|
ptymajors[i / num_minors], ptyminors[i % num_minors]);
|
|
|
|
|
|
|
|
if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1) {
|
|
|
|
/* Try SCO style naming */
|
|
|
|
snprintf(ptbuf, sizeof(ptbuf), "/dev/ptyp%d", i);
|
|
|
|
snprintf(ttbuf, sizeof(ttbuf), "/dev/ttyp%d", i);
|
|
|
|
if ((*amaster = open(ptbuf, O_RDWR | O_NOCTTY)) == -1)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the slave side. */
|
|
|
|
if ((*aslave = open(ttbuf, O_RDWR | O_NOCTTY)) == -1) {
|
|
|
|
close(*amaster);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* set tty modes to a sane state for broken clients */
|
|
|
|
if (tcgetattr(*amaster, &tio) != -1) {
|
|
|
|
tio.c_lflag |= (ECHO | ISIG | ICANON);
|
|
|
|
tio.c_oflag |= (OPOST | ONLCR);
|
|
|
|
tio.c_iflag |= ICRNL;
|
|
|
|
tcsetattr(*amaster, TCSANOW, &tio);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !defined(HAVE_OPENPTY) */
|
|
|
|
|