… | |
… | |
63 | |
63 | |
64 | /* ------------------------------------------------------------------------- * |
64 | /* ------------------------------------------------------------------------- * |
65 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
65 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
66 | * ------------------------------------------------------------------------- */ |
66 | * ------------------------------------------------------------------------- */ |
67 | /* |
67 | /* |
68 | * Returns pty file descriptor, or -1 on failure |
68 | * Returns pty file descriptor, or -1 on failure |
69 | * If successful, ttydev is set to the name of the slave device. |
69 | * If successful, ttydev is set to the name of the slave device. |
70 | * fd_tty _may_ also be set to an open fd to the slave device |
70 | * fd_tty _may_ also be set to an open fd to the slave device |
71 | */ |
71 | */ |
72 | #if defined(UNIX98_PTY) |
72 | #if defined(UNIX98_PTY) |
|
|
73 | |
73 | static int |
74 | static int |
74 | get_pty (int *fd_tty, char **ttydev) |
75 | get_pty (int *fd_tty, char **ttydev) |
75 | { |
76 | { |
76 | int pfd; |
77 | int pfd; |
77 | |
78 | |
78 | # if defined(HAVE_GETPT) |
79 | # if defined(HAVE_GETPT) |
79 | pfd = getpt(); |
80 | pfd = getpt(); |
80 | # elif defined(HAVE_POSIX_OPENPT) |
81 | # elif defined(HAVE_POSIX_OPENPT) |
81 | pfd = posix_openpt (O_RDWR); |
82 | pfd = posix_openpt (O_RDWR); |
82 | # else |
83 | # else |
83 | pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0); |
84 | pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0); |
84 | # endif |
85 | # endif |
|
|
86 | |
85 | if (pfd >= 0) |
87 | if (pfd >= 0) |
86 | { |
88 | { |
87 | if (grantpt (pfd) == 0 /* change slave permissions */ |
89 | if (grantpt (pfd) == 0 /* change slave permissions */ |
88 | && unlockpt (pfd) == 0) |
90 | && unlockpt (pfd) == 0) |
89 | { /* slave now unlocked */ |
91 | { /* slave now unlocked */ |
90 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
92 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
91 | return pfd; |
93 | return pfd; |
92 | } |
94 | } |
93 | |
95 | |
94 | close (pfd); |
96 | close (pfd); |
95 | } |
97 | } |
96 | |
98 | |
97 | return -1; |
99 | return -1; |
98 | } |
100 | } |
|
|
101 | |
99 | #elif defined(HAVE_OPENPTY) |
102 | #elif defined(HAVE_OPENPTY) |
|
|
103 | |
100 | static int |
104 | static int |
101 | get_pty (int *fd_tty, char **ttydev) |
105 | get_pty (int *fd_tty, char **ttydev) |
102 | { |
106 | { |
103 | int pfd; |
107 | int pfd; |
104 | int res; |
108 | int res; |
105 | char tty_name[32]; |
109 | char tty_name[32]; |
106 | |
110 | |
107 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
111 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
|
|
112 | |
108 | if (res != -1) |
113 | if (res != -1) |
109 | { |
114 | { |
110 | *ttydev = strdup (tty_name); |
115 | *ttydev = strdup (tty_name); |
|
|
116 | return pfd; |
|
|
117 | } |
|
|
118 | |
|
|
119 | return -1; |
|
|
120 | } |
|
|
121 | |
|
|
122 | #elif defined(HAVE__GETPTY) |
|
|
123 | |
|
|
124 | static int |
|
|
125 | get_pty (int *fd_tty, char **ttydev) |
|
|
126 | { |
|
|
127 | int pfd; |
|
|
128 | |
|
|
129 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
|
|
130 | |
|
|
131 | if (*ttydev != NULL) |
111 | return pfd; |
132 | return pfd; |
112 | } |
|
|
113 | |
133 | |
114 | return -1; |
134 | return -1; |
115 | } |
135 | } |
116 | #elif defined(HAVE__GETPTY) |
|
|
117 | static int |
|
|
118 | get_pty (int *fd_tty, char **ttydev) |
|
|
119 | { |
|
|
120 | int pfd; |
|
|
121 | |
136 | |
122 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
|
|
123 | if (*ttydev != NULL) |
|
|
124 | return pfd; |
|
|
125 | |
|
|
126 | return -1; |
|
|
127 | } |
|
|
128 | #elif defined(HAVE_DEV_PTC) |
137 | #elif defined(HAVE_DEV_PTC) |
|
|
138 | |
129 | static int |
139 | static int |
130 | get_pty (int *fd_tty, char **ttydev) |
140 | get_pty (int *fd_tty, char **ttydev) |
131 | { |
141 | { |
132 | int pfd; |
142 | int pfd; |
133 | |
143 | |
134 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
144 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
135 | { |
145 | { |
136 | *ttydev = strdup (ttyname (pfd)); |
146 | *ttydev = strdup (ttyname (pfd)); |
137 | return pfd; |
147 | return pfd; |
138 | } |
148 | } |
139 | |
149 | |
140 | return -1; |
150 | return -1; |
141 | } |
151 | } |
|
|
152 | |
142 | #elif defined(HAVE_DEV_CLONE) |
153 | #elif defined(HAVE_DEV_CLONE) |
|
|
154 | |
143 | static int |
155 | static int |
144 | get_pty (int *fd_tty, char **ttydev) |
156 | get_pty (int *fd_tty, char **ttydev) |
145 | { |
157 | { |
146 | int pfd; |
158 | int pfd; |
147 | |
159 | |
148 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
160 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
149 | { |
161 | { |
150 | *ttydev = strdup (ptsname (pfd)); |
162 | *ttydev = strdup (ptsname (pfd)); |
151 | return pfd; |
163 | return pfd; |
152 | } |
164 | } |
153 | |
165 | |
154 | return -1; |
166 | return -1; |
155 | } |
167 | } |
|
|
168 | |
156 | #else |
169 | #else |
|
|
170 | |
157 | /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
171 | /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
158 | static int |
172 | static int |
159 | get_pty (int *fd_tty, char **ttydev) |
173 | get_pty (int *fd_tty, char **ttydev) |
160 | { |
174 | { |
161 | int pfd; |
175 | int pfd; |
162 | int i; |
176 | int i; |
163 | char pty_name[32]; |
177 | char pty_name[32]; |
164 | char tty_name[32]; |
178 | char tty_name[32]; |
165 | const char *majors = "pqrstuvwxyzabcde"; |
179 | const char *majors = "pqrstuvwxyzabcde"; |
166 | const char *minors = "0123456789abcdef"; |
180 | const char *minors = "0123456789abcdef"; |
|
|
181 | |
167 | for (i = 0; i < 256; i++) |
182 | for (i = 0; i < 256; i++) |
168 | { |
183 | { |
169 | snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
184 | snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
170 | snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
185 | snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
|
|
186 | |
171 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
187 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
172 | { |
188 | { |
173 | snprintf(pty_name, 32, "/dev/ptyp%d", i); |
189 | snprintf(pty_name, 32, "/dev/ptyp%d", i); |
174 | snprintf(tty_name, 32, "/dev/ttyp%d", i); |
190 | snprintf(tty_name, 32, "/dev/ttyp%d", i); |
175 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
191 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
176 | continue; |
192 | continue; |
177 | } |
193 | } |
|
|
194 | |
178 | if (access (tty_name, R_OK | W_OK) == 0) |
195 | if (access (tty_name, R_OK | W_OK) == 0) |
179 | { |
196 | { |
180 | *ttydev = strdup (tty_name); |
197 | *ttydev = strdup (tty_name); |
181 | return pfd; |
198 | return pfd; |
182 | } |
199 | } |
183 | |
200 | |
184 | close (pfd); |
201 | close (pfd); |
185 | } |
202 | } |
186 | |
203 | |
187 | return -1; |
204 | return -1; |
188 | } |
205 | } |
|
|
206 | |
189 | #endif |
207 | #endif |
190 | |
208 | |
191 | /*----------------------------------------------------------------------*/ |
209 | /*----------------------------------------------------------------------*/ |
192 | /* |
210 | /* |
193 | * Returns tty file descriptor, or -1 on failure |
211 | * Returns tty file descriptor, or -1 on failure |
194 | */ |
212 | */ |
195 | static int |
213 | static int |
196 | get_tty (char *ttydev) |
214 | get_tty (char *ttydev) |
197 | { |
215 | { |
198 | return open (ttydev, O_RDWR | O_NOCTTY, 0); |
216 | return open (ttydev, O_RDWR | O_NOCTTY, 0); |
… | |
… | |
233 | ioctl (fd_tty, I_PUSH, "ldterm"); |
251 | ioctl (fd_tty, I_PUSH, "ldterm"); |
234 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
252 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
235 | } |
253 | } |
236 | #endif |
254 | #endif |
237 | |
255 | |
|
|
256 | #ifdef TIOCSCTTY |
238 | ioctl (fd_tty, TIOCSCTTY, NULL); |
257 | ioctl (fd_tty, TIOCSCTTY, NULL); |
|
|
258 | #else |
|
|
259 | fd = open (name, O_RDWR); |
|
|
260 | if (fd >= 0) |
|
|
261 | close (fd); |
|
|
262 | #endif |
239 | |
263 | |
240 | int fd = open ("/dev/tty", O_WRONLY); |
264 | int fd = open ("/dev/tty", O_WRONLY); |
241 | if (fd < 0) |
265 | if (fd < 0) |
242 | return -1; /* fatal */ |
266 | return -1; /* fatal */ |
243 | |
267 | |
… | |
… | |
329 | } |
353 | } |
330 | |
354 | |
331 | void |
355 | void |
332 | ptytty_unix::put () |
356 | ptytty_unix::put () |
333 | { |
357 | { |
|
|
358 | if (name) |
|
|
359 | { |
334 | chmod (name, RESTORE_TTY_MODE); |
360 | chmod (name, RESTORE_TTY_MODE); |
335 | chown (name, 0, ttyconf.gid); |
361 | chown (name, 0, ttyconf.gid); |
|
|
362 | } |
336 | |
363 | |
337 | close_tty (); |
364 | close_tty (); |
338 | |
365 | |
339 | if (pty >= 0) |
366 | if (pty >= 0) |
340 | close (pty); |
367 | close (pty); |