2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
8 * This software is distributed under the terms
9 * of the GPL, which you should have received
10 * along with this source.
12 * Pseudo-pty allocation routines... Concepts and code borrowed
13 * from pty-redir by Magosanyi Arpad.
17 #define _ISOC99_SOURCE
20 #define _XOPEN_SOURCE_EXTENDED
33 #define PTY00 "/dev/ptyXX"
34 #define PTY10 "pqrstuvwxyz"
35 #define PTY01 "0123456789abcdef"
39 #define PTY00 "/dev/ptyXX"
40 #define PTY10 "pqrstuvwxyzabcde"
41 #define PTY01 "0123456789abcdef"
45 #define PTY00 "/dev/ptyXX"
47 #define PTY01 "0123456789abcdefghijklmnopqrstuv"
51 int getPtyMaster_pty (char *tty10
, char *tty01
)
55 static char dev
[] = PTY00
;
58 for (p10
= PTY10
; *p10
; p10
++)
61 for (p01
= PTY01
; *p01
; p01
++)
64 fd
= open (dev
, O_RDWR
| O_NONBLOCK
);
73 l2tp_log (LOG_CRIT
, "%s: No more free pseudo-tty's\n", __FUNCTION__
);
77 int getPtyMaster_ptmx(char *ttybuf
, int ttybuflen
)
82 fd
= open("/dev/ptmx", O_RDWR
);
85 l2tp_log (LOG_WARNING
, "%s: unable to open /dev/ptmx to allocate pty\n",
90 /* change the onwership */
93 l2tp_log (LOG_WARNING
, "%s: unable to grantpt() on pty\n",
101 l2tp_log (LOG_WARNING
, "%s: unable to unlockpt() on pty\n",
110 l2tp_log (LOG_WARNING
, "%s: unable to obtain name of slave tty\n",
116 strncat(ttybuf
, tty
, ttybuflen
);
122 int getPtyMaster_ptm(char *ttybuf
, int ttybuflen
)
125 char *tty
= (char*) malloc(64);
127 if((openpty(&amaster
, &aslave
, tty
, NULL
, NULL
)) == -1)
129 l2tp_log (LOG_WARNING
, "%s: openpty() returned %s\n",
130 __FUNCTION__
, strerror(errno
));
136 strncat(ttybuf
, tty
, ttybuflen
);
144 int getPtyMaster(char *ttybuf
, int ttybuflen
)
148 fd
= getPtyMaster_ptmx(ttybuf
, ttybuflen
);
155 l2tp_log (LOG_WARNING
, "%s: failed to use pts -- using legacy ptys\n", __FUNCTION__
);
156 fd
= getPtyMaster_pty(&a
,&b
);
159 snprintf(ttybuf
, ttybuflen
, "/dev/tty%c%c", a
, b
);
165 fd
= getPtyMaster_ptm(ttybuf
, ttybuflen
);