2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
38 # include <sys/ioctl.h>
41 /* for solaris 2.1, Unixware (SVR4.2) and possibly others */
43 # include <sys/stropts.h>
46 #if defined(sun) && defined(LOCKPTY) && !defined(TIOCEXCL)
47 # include <sys/ttold.h>
52 # include <sys/sioctl.h>
57 # include <sys/sysmacros.h>
63 * if no PTYRANGE[01] is in the config file, we pick a default
66 # define PTYRANGE0 "qpr"
69 # define PTYRANGE1 "0123456789abcdef"
72 /* SVR4 pseudo ttys don't seem to work with SCO-5 */
74 # undef HAVE_SVR4_PTYS
79 /* used for opening a new pty-pair: */
80 static char PtyName
[32], TtyName
[32];
82 #if !(defined(sequent) || defined(_SEQUENT_) || defined(HAVE_SVR4_PTYS))
84 static char PtyProto
[] = "/dev/ptym/ptyXY";
85 static char TtyProto
[] = "/dev/pty/ttyXY";
88 static char PtyProto
[] = "/dev/ptypXY";
89 static char TtyProto
[] = "/dev/ttypXY";
91 static char PtyProto
[] = "/dev/ptyXY";
92 static char TtyProto
[] = "/dev/ttyXY";
97 static void initmaster
__P((int));
100 /* sun's utmp_update program opens the salve side, thus corrupting
108 * Open all ptys with O_NOCTTY, just to be on the safe side
109 * (RISCos mips breaks otherwise)
115 /***************************************************************/
122 tcflush(f
, TCIOFLUSH
);
125 (void) ioctl(f
, TIOCFLUSH
, (char *) 0);
129 (void) ioctl(f
, TIOCEXCL
, (char *) 0);
139 #if defined(I_PUSH) && defined(HAVE_SVR4_PTYS) && !defined(sgi) && !defined(linux) && !defined(__osf__) && !defined(M_UNIX)
140 if (ioctl(f
, I_PUSH
, "ptem"))
141 Panic(errno
, "InitPTY: cannot I_PUSH ptem");
142 if (ioctl(f
, I_PUSH
, "ldterm"))
143 Panic(errno
, "InitPTY: cannot I_PUSH ldterm");
145 if (ioctl(f
, I_PUSH
, "ttcompat"))
146 Panic(errno
, "InitPTY: cannot I_PUSH ttcompat");
151 /***************************************************************/
153 #if defined(OSX) && !defined(PTY_DONE)
160 if ((f
= open_controlling_pty(TtyName
)) < 0)
168 /***************************************************************/
170 #if (defined(sequent) || defined(_SEQUENT_)) && !defined(PTY_DONE)
179 if ((f
= getpseudotty(&s
, &m
)) < 0)
184 strncpy(PtyName
, m
, sizeof(PtyName
));
185 strncpy(TtyName
, s
, sizeof(TtyName
));
192 /***************************************************************/
194 #if defined(__sgi) && !defined(PTY_DONE)
201 char *name
, *_getpty();
202 sigret_t (*sigcld
)__P(SIGPROTOARG
);
205 * SIGCHLD set to SIG_DFL for _getpty() because it may fork() and
206 * exec() /usr/adm/mkpts
208 sigcld
= signal(SIGCHLD
, SIG_DFL
);
209 name
= _getpty(&f
, O_RDWR
| O_NONBLOCK
, 0600, 0);
210 signal(SIGCHLD
, sigcld
);
220 /***************************************************************/
222 #if defined(MIPS) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
231 strcpy(PtyName
, "/dev/ptc");
232 if ((f
= open(PtyName
, O_RDWR
| O_NOCTTY
| O_NONBLOCK
)) < 0)
234 if (fstat(f
, &buf
) < 0)
239 sprintf(TtyName
, "/dev/ttyq%d", minor(buf
.st_rdev
));
246 /***************************************************************/
248 #if defined(HAVE_SVR4_PTYS) && !defined(PTY_DONE)
256 int unlockpt
__P((int)), grantpt
__P((int));
257 #if defined(HAVE_GETPT) && defined(linux)
258 int getpt
__P((void));
260 sigret_t (*sigcld
)__P(SIGPROTOARG
);
262 strcpy(PtyName
, "/dev/ptmx");
263 #if defined(HAVE_GETPT) && defined(linux)
264 if ((f
= getpt()) == -1)
266 if ((f
= open(PtyName
, O_RDWR
| O_NOCTTY
)) == -1)
271 * SIGCHLD set to SIG_DFL for grantpt() because it fork()s and
274 sigcld
= signal(SIGCHLD
, SIG_DFL
);
275 if ((m
= ptsname(f
)) == NULL
|| grantpt(f
) || unlockpt(f
))
277 signal(SIGCHLD
, sigcld
);
281 signal(SIGCHLD
, sigcld
);
282 strncpy(TtyName
, m
, sizeof(TtyName
));
289 /***************************************************************/
291 #if defined(_AIX) && defined(HAVE_DEV_PTC) && !defined(PTY_DONE)
300 /* a dumb looking loop replaced by mycrofts code: */
301 strcpy (PtyName
, "/dev/ptc");
302 if ((f
= open (PtyName
, O_RDWR
| O_NOCTTY
)) < 0)
304 strncpy(TtyName
, ttyname(f
), sizeof(TtyName
));
305 if (eff_uid
&& access(TtyName
, R_OK
| W_OK
))
319 /***************************************************************/
321 #if defined(HAVE_OPENPTY) && !defined(PTY_DONE)
328 if (openpty(&f
, &s
, TtyName
, NULL
, NULL
) != 0)
338 /***************************************************************/
345 register char *p
, *q
, *l
, *d
;
348 debug("OpenPTY: Using BSD style ptys.\n");
349 strcpy(PtyName
, PtyProto
);
350 strcpy(TtyName
, TtyProto
);
351 for (p
= PtyName
; *p
!= 'X'; p
++)
353 for (q
= TtyName
; *q
!= 'X'; q
++)
355 for (l
= PTYRANGE0
; (*p
= *l
) != '\0'; l
++)
357 for (d
= PTYRANGE1
; (p
[1] = *d
) != '\0'; d
++)
359 debug1("OpenPTY tries '%s'\n", PtyName
);
360 if ((f
= open(PtyName
, O_RDWR
| O_NOCTTY
)) == -1)
364 if (eff_uid
&& access(TtyName
, R_OK
| W_OK
))
369 #if defined(sun) && defined(TIOCGPGRP) && !defined(SUNOS3)
370 /* Hack to ensure that the slave side of the pty is
371 * unused. May not work in anything other than SunOS4.1
376 /* tcgetpgrp does not work (uses TIOCGETPGRP)! */
377 if (ioctl(f
, TIOCGPGRP
, (char *)&pgrp
) != -1 || errno
!= EIO
)