usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / xl2tpd / pty.c
blob3e7a3928864cf6bdb500b72a9fe3a4ca2aceec2a
1 /*
2 * Layer Two Tunnelling Protocol Daemon
3 * Copyright (C) 1998 Adtran, Inc.
4 * Copyright (C) 2002 Jeff McAdams
6 * Mark Spencer
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
18 #define _XOPEN_SOURCE
19 #define _BSD_SOURCE
20 #define _XOPEN_SOURCE_EXTENDED
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include "l2tp.h"
32 #ifdef SOLARIS
33 #define PTY00 "/dev/ptyXX"
34 #define PTY10 "pqrstuvwxyz"
35 #define PTY01 "0123456789abcdef"
36 #endif
38 #ifdef LINUX
39 #define PTY00 "/dev/ptyXX"
40 #define PTY10 "pqrstuvwxyzabcde"
41 #define PTY01 "0123456789abcdef"
42 #endif
44 #ifdef FREEBSD
45 #define PTY00 "/dev/ptyXX"
46 #define PTY10 "p"
47 #define PTY01 "0123456789abcdefghijklmnopqrstuv"
48 #endif
50 #ifndef OPENBSD
51 int getPtyMaster_pty (char *tty10, char *tty01)
53 char *p10;
54 char *p01;
55 static char dev[] = PTY00;
56 int fd;
58 for (p10 = PTY10; *p10; p10++)
60 dev[8] = *p10;
61 for (p01 = PTY01; *p01; p01++)
63 dev[9] = *p01;
64 fd = open (dev, O_RDWR | O_NONBLOCK);
65 if (fd >= 0)
67 *tty10 = *p10;
68 *tty01 = *p01;
69 return fd;
73 l2tp_log (LOG_CRIT, "%s: No more free pseudo-tty's\n", __FUNCTION__);
74 return -1;
77 int getPtyMaster_ptmx(char *ttybuf, int ttybuflen)
79 int fd;
80 char *tty;
82 fd = open("/dev/ptmx", O_RDWR);
83 if (fd == -1)
85 l2tp_log (LOG_WARNING, "%s: unable to open /dev/ptmx to allocate pty\n",
86 __FUNCTION__);
87 return -EINVAL;
90 /* change the onwership */
91 if (grantpt(fd))
93 l2tp_log (LOG_WARNING, "%s: unable to grantpt() on pty\n",
94 __FUNCTION__);
95 close(fd);
96 return -EINVAL;
99 if (unlockpt(fd))
101 l2tp_log (LOG_WARNING, "%s: unable to unlockpt() on pty\n",
102 __FUNCTION__);
103 close(fd);
104 return -EINVAL;
107 tty = ptsname(fd);
108 if (tty == NULL)
110 l2tp_log (LOG_WARNING, "%s: unable to obtain name of slave tty\n",
111 __FUNCTION__);
112 close(fd);
113 return -EINVAL;
115 ttybuf[0]='\0';
116 strncat(ttybuf, tty, ttybuflen);
118 return fd;
120 #endif
121 #ifdef OPENBSD
122 int getPtyMaster_ptm(char *ttybuf, int ttybuflen)
124 int amaster, aslave;
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));
131 free(tty);
132 return -EINVAL;
135 ttybuf[0] = '\0';
136 strncat(ttybuf, tty, ttybuflen);
138 free(tty);
140 return amaster;
142 #endif /* OPENBSD */
144 int getPtyMaster(char *ttybuf, int ttybuflen)
146 int fd;
147 #ifndef OPENBSD
148 fd = getPtyMaster_ptmx(ttybuf, ttybuflen);
149 char a, b;
151 if(fd >= 0) {
152 return fd;
155 l2tp_log (LOG_WARNING, "%s: failed to use pts -- using legacy ptys\n", __FUNCTION__);
156 fd = getPtyMaster_pty(&a,&b);
158 if(fd >= 0) {
159 snprintf(ttybuf, ttybuflen, "/dev/tty%c%c", a, b);
160 return fd;
162 #endif
163 #ifdef OPENBSD
165 fd = getPtyMaster_ptm(ttybuf, ttybuflen);
166 if(fd >= 0) {
167 return fd;
169 #endif /* OPENBSD */
171 return -EINVAL;