… | |
… | |
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) |
|
|
91 | { |
89 | { /* slave now unlocked */ |
92 | /* slave now unlocked */ |
90 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
93 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
91 | return pfd; |
94 | return pfd; |
92 | } |
95 | } |
93 | |
96 | |
94 | close (pfd); |
97 | close (pfd); |
95 | } |
98 | } |
96 | |
99 | |
97 | return -1; |
100 | return -1; |
98 | } |
101 | } |
|
|
102 | |
99 | #elif defined(HAVE_OPENPTY) |
103 | #elif defined(HAVE_OPENPTY) |
|
|
104 | |
100 | static int |
105 | static int |
101 | get_pty (int *fd_tty, char **ttydev) |
106 | get_pty (int *fd_tty, char **ttydev) |
102 | { |
107 | { |
103 | int pfd; |
108 | int pfd; |
104 | int res; |
109 | int res; |
105 | char tty_name[32]; |
110 | |
106 | |
|
|
107 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
111 | res = openpty (&pfd, fd_tty, NULL, NULL, NULL); |
|
|
112 | |
108 | if (res != -1) |
113 | if (res != -1) |
109 | { |
114 | { |
|
|
115 | *ttydev = strdup (ttyname (*fd_tty)); |
|
|
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 | char *slave; |
|
|
129 | |
|
|
130 | slave = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
|
|
131 | |
|
|
132 | if (slave != NULL) { |
110 | *ttydev = strdup (tty_name); |
133 | *ttydev = strdup (slave); |
111 | return pfd; |
134 | return pfd; |
112 | } |
135 | } |
113 | |
136 | |
114 | return -1; |
137 | return -1; |
115 | } |
138 | } |
116 | #elif defined(HAVE__GETPTY) |
|
|
117 | static int |
|
|
118 | get_pty (int *fd_tty, char **ttydev) |
|
|
119 | { |
|
|
120 | int pfd; |
|
|
121 | |
139 | |
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) |
140 | #elif defined(HAVE_DEV_PTC) |
|
|
141 | |
129 | static int |
142 | static int |
130 | get_pty (int *fd_tty, char **ttydev) |
143 | get_pty (int *fd_tty, char **ttydev) |
131 | { |
144 | { |
132 | int pfd; |
145 | int pfd; |
133 | |
146 | |
134 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
147 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
135 | { |
148 | { |
136 | *ttydev = strdup (ttyname (pfd)); |
149 | *ttydev = strdup (ttyname (pfd)); |
137 | return pfd; |
150 | return pfd; |
138 | } |
151 | } |
139 | |
152 | |
140 | return -1; |
153 | return -1; |
141 | } |
154 | } |
|
|
155 | |
142 | #elif defined(HAVE_DEV_CLONE) |
156 | #elif defined(HAVE_DEV_CLONE) |
|
|
157 | |
143 | static int |
158 | static int |
144 | get_pty (int *fd_tty, char **ttydev) |
159 | get_pty (int *fd_tty, char **ttydev) |
145 | { |
160 | { |
146 | int pfd; |
161 | int pfd; |
147 | |
162 | |
148 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
163 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
149 | { |
164 | { |
150 | *ttydev = strdup (ptsname (pfd)); |
165 | *ttydev = strdup (ptsname (pfd)); |
151 | return pfd; |
166 | return pfd; |
152 | } |
167 | } |
153 | |
168 | |
154 | return -1; |
169 | return -1; |
155 | } |
170 | } |
|
|
171 | |
156 | #else |
172 | #else |
|
|
173 | |
157 | /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
174 | /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
158 | static int |
175 | static int |
159 | get_pty (int *fd_tty, char **ttydev) |
176 | get_pty (int *fd_tty, char **ttydev) |
160 | { |
177 | { |
161 | int pfd; |
178 | int pfd; |
162 | int i; |
179 | int i; |
163 | char pty_name[32]; |
180 | char pty_name[32]; |
164 | char tty_name[32]; |
181 | char tty_name[32]; |
165 | const char *majors = "pqrstuvwxyzabcde"; |
182 | const char *majors = "pqrstuvwxyzabcde"; |
166 | const char *minors = "0123456789abcdef"; |
183 | const char *minors = "0123456789abcdef"; |
|
|
184 | |
167 | for (i = 0; i < 256; i++) |
185 | for (i = 0; i < 256; i++) |
168 | { |
186 | { |
169 | snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
187 | 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]); |
188 | snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
|
|
189 | |
171 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
190 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
172 | { |
191 | { |
173 | snprintf(pty_name, 32, "/dev/ptyp%d", i); |
192 | snprintf(pty_name, 32, "/dev/ptyp%d", i); |
174 | snprintf(tty_name, 32, "/dev/ttyp%d", i); |
193 | snprintf(tty_name, 32, "/dev/ttyp%d", i); |
175 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
194 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
176 | continue; |
195 | continue; |
177 | } |
196 | } |
|
|
197 | |
178 | if (access (tty_name, R_OK | W_OK) == 0) |
198 | if (access (tty_name, R_OK | W_OK) == 0) |
179 | { |
199 | { |
180 | *ttydev = strdup (tty_name); |
200 | *ttydev = strdup (tty_name); |
181 | return pfd; |
201 | return pfd; |
182 | } |
202 | } |
183 | |
203 | |
184 | close (pfd); |
204 | close (pfd); |
185 | } |
205 | } |
186 | |
206 | |
187 | return -1; |
207 | return -1; |
188 | } |
208 | } |
|
|
209 | |
189 | #endif |
210 | #endif |
190 | |
211 | |
191 | /*----------------------------------------------------------------------*/ |
212 | /*----------------------------------------------------------------------*/ |
192 | /* |
213 | /* |
193 | * Returns tty file descriptor, or -1 on failure |
214 | * Returns tty file descriptor, or -1 on failure |
194 | */ |
215 | */ |
195 | static int |
216 | static int |
196 | get_tty (char *ttydev) |
217 | get_tty (char *ttydev) |
197 | { |
218 | { |
198 | return open (ttydev, O_RDWR | O_NOCTTY, 0); |
219 | return open (ttydev, O_RDWR | O_NOCTTY, 0); |
… | |
… | |
203 | * Make our tty a controlling tty so that /dev/tty points to us |
224 | * Make our tty a controlling tty so that /dev/tty points to us |
204 | */ |
225 | */ |
205 | static int |
226 | static int |
206 | control_tty (int fd_tty) |
227 | control_tty (int fd_tty) |
207 | { |
228 | { |
|
|
229 | int fd; |
|
|
230 | |
208 | setsid (); |
231 | setsid (); |
209 | |
232 | |
210 | #if defined(HAVE_DEV_PTMX) && defined(I_PUSH) |
233 | #if defined(HAVE_DEV_PTMX) && defined(I_PUSH) |
211 | /* |
234 | /* |
212 | * Push STREAMS modules: |
235 | * Push STREAMS modules: |
… | |
… | |
233 | ioctl (fd_tty, I_PUSH, "ldterm"); |
256 | ioctl (fd_tty, I_PUSH, "ldterm"); |
234 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
257 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
235 | } |
258 | } |
236 | #endif |
259 | #endif |
237 | |
260 | |
|
|
261 | #ifdef TIOCSCTTY |
238 | ioctl (fd_tty, TIOCSCTTY, NULL); |
262 | ioctl (fd_tty, TIOCSCTTY, NULL); |
|
|
263 | #else |
|
|
264 | fd = open (ttyname (fd_tty), O_RDWR); |
|
|
265 | if (fd >= 0) |
|
|
266 | close (fd); |
|
|
267 | #endif |
239 | |
268 | |
240 | int fd = open ("/dev/tty", O_WRONLY); |
269 | fd = open ("/dev/tty", O_WRONLY); |
241 | if (fd < 0) |
270 | if (fd < 0) |
242 | return -1; /* fatal */ |
271 | return -1; /* fatal */ |
243 | |
272 | |
244 | close (fd); |
273 | close (fd); |
245 | |
274 | |
… | |
… | |
297 | { |
326 | { |
298 | #ifdef TTY_GID_SUPPORT |
327 | #ifdef TTY_GID_SUPPORT |
299 | struct group *gr = getgrnam ("tty"); |
328 | struct group *gr = getgrnam ("tty"); |
300 | |
329 | |
301 | if (gr) |
330 | if (gr) |
|
|
331 | { |
302 | { /* change group ownership of tty to "tty" */ |
332 | /* change group ownership of tty to "tty" */ |
303 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
333 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
304 | gid = gr->gr_gid; |
334 | gid = gr->gr_gid; |
305 | } |
335 | } |
306 | else |
336 | else |
307 | #endif /* TTY_GID_SUPPORT */ |
337 | #endif /* TTY_GID_SUPPORT */ |
308 | { |
338 | { |
309 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
339 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
310 | gid = 0; |
340 | gid = 0; |
311 | } |
341 | } |
312 | } |
342 | } |
… | |
… | |
329 | } |
359 | } |
330 | |
360 | |
331 | void |
361 | void |
332 | ptytty_unix::put () |
362 | ptytty_unix::put () |
333 | { |
363 | { |
|
|
364 | if (name) |
|
|
365 | { |
334 | chmod (name, RESTORE_TTY_MODE); |
366 | chmod (name, RESTORE_TTY_MODE); |
335 | chown (name, 0, ttyconf.gid); |
367 | chown (name, 0, ttyconf.gid); |
|
|
368 | } |
336 | |
369 | |
337 | close_tty (); |
370 | close_tty (); |
338 | |
371 | |
339 | if (pty >= 0) |
372 | if (pty >= 0) |
340 | close (pty); |
373 | close (pty); |