… | |
… | |
32 | #include <cstdlib> |
32 | #include <cstdlib> |
33 | #include <cstring> |
33 | #include <cstring> |
34 | #include <csignal> |
34 | #include <csignal> |
35 | |
35 | |
36 | #include <sys/types.h> |
36 | #include <sys/types.h> |
|
|
37 | #include <sys/stat.h> |
37 | #include <unistd.h> |
38 | #include <unistd.h> |
38 | #include <fcntl.h> |
39 | #include <fcntl.h> |
39 | |
40 | |
40 | #ifdef HAVE_SYS_IOCTL_H |
41 | #ifdef HAVE_SYS_IOCTL_H |
41 | # include <sys/ioctl.h> |
42 | # include <sys/ioctl.h> |
… | |
… | |
86 | |
87 | |
87 | if (pfd >= 0) |
88 | if (pfd >= 0) |
88 | { |
89 | { |
89 | if (grantpt (pfd) == 0 /* change slave permissions */ |
90 | if (grantpt (pfd) == 0 /* change slave permissions */ |
90 | && unlockpt (pfd) == 0) |
91 | && unlockpt (pfd) == 0) |
|
|
92 | { |
91 | { /* slave now unlocked */ |
93 | /* slave now unlocked */ |
92 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
94 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
93 | return pfd; |
95 | return pfd; |
94 | } |
96 | } |
95 | |
97 | |
96 | close (pfd); |
98 | close (pfd); |
… | |
… | |
104 | static int |
106 | static int |
105 | get_pty (int *fd_tty, char **ttydev) |
107 | get_pty (int *fd_tty, char **ttydev) |
106 | { |
108 | { |
107 | int pfd; |
109 | int pfd; |
108 | int res; |
110 | int res; |
109 | char tty_name[32]; |
|
|
110 | |
111 | |
111 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
112 | res = openpty (&pfd, fd_tty, NULL, NULL, NULL); |
112 | |
113 | |
113 | if (res != -1) |
114 | if (res != -1) |
114 | { |
115 | { |
115 | *ttydev = strdup (tty_name); |
116 | *ttydev = strdup (ttyname (*fd_tty)); |
116 | return pfd; |
117 | return pfd; |
117 | } |
118 | } |
118 | |
119 | |
119 | return -1; |
120 | return -1; |
120 | } |
121 | } |
… | |
… | |
123 | |
124 | |
124 | static int |
125 | static int |
125 | get_pty (int *fd_tty, char **ttydev) |
126 | get_pty (int *fd_tty, char **ttydev) |
126 | { |
127 | { |
127 | int pfd; |
128 | int pfd; |
|
|
129 | char *slave; |
128 | |
130 | |
129 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
131 | slave = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
130 | |
132 | |
131 | if (*ttydev != NULL) |
133 | if (slave != NULL) |
|
|
134 | { |
|
|
135 | *ttydev = strdup (slave); |
132 | return pfd; |
136 | return pfd; |
|
|
137 | } |
133 | |
138 | |
134 | return -1; |
139 | return -1; |
135 | } |
140 | } |
136 | |
141 | |
137 | #elif defined(HAVE_DEV_PTC) |
142 | #elif defined(HAVE_DEV_PTC) |
… | |
… | |
221 | * Make our tty a controlling tty so that /dev/tty points to us |
226 | * Make our tty a controlling tty so that /dev/tty points to us |
222 | */ |
227 | */ |
223 | static int |
228 | static int |
224 | control_tty (int fd_tty) |
229 | control_tty (int fd_tty) |
225 | { |
230 | { |
|
|
231 | int fd; |
|
|
232 | |
226 | setsid (); |
233 | setsid (); |
227 | |
234 | |
228 | #if defined(HAVE_DEV_PTMX) && defined(I_PUSH) |
235 | #if defined(HAVE_DEV_PTMX) && defined(I_PUSH) |
229 | /* |
236 | /* |
230 | * Push STREAMS modules: |
237 | * Push STREAMS modules: |
… | |
… | |
254 | #endif |
261 | #endif |
255 | |
262 | |
256 | #ifdef TIOCSCTTY |
263 | #ifdef TIOCSCTTY |
257 | ioctl (fd_tty, TIOCSCTTY, NULL); |
264 | ioctl (fd_tty, TIOCSCTTY, NULL); |
258 | #else |
265 | #else |
259 | fd = open (name, O_RDWR); |
266 | fd = open (ttyname (fd_tty), O_RDWR); |
260 | if (fd >= 0) |
267 | if (fd >= 0) |
261 | close (fd); |
268 | close (fd); |
262 | #endif |
269 | #endif |
263 | |
270 | |
264 | int fd = open ("/dev/tty", O_WRONLY); |
271 | fd = open ("/dev/tty", O_WRONLY); |
265 | if (fd < 0) |
272 | if (fd < 0) |
266 | return -1; /* fatal */ |
273 | return -1; /* fatal */ |
267 | |
274 | |
268 | close (fd); |
275 | close (fd); |
269 | |
276 | |
… | |
… | |
321 | { |
328 | { |
322 | #ifdef TTY_GID_SUPPORT |
329 | #ifdef TTY_GID_SUPPORT |
323 | struct group *gr = getgrnam ("tty"); |
330 | struct group *gr = getgrnam ("tty"); |
324 | |
331 | |
325 | if (gr) |
332 | if (gr) |
|
|
333 | { |
326 | { /* change group ownership of tty to "tty" */ |
334 | /* change group ownership of tty to "tty" */ |
327 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
335 | mode = S_IRUSR | S_IWUSR | S_IWGRP; |
328 | gid = gr->gr_gid; |
336 | gid = gr->gr_gid; |
329 | } |
337 | } |
330 | else |
338 | else |
331 | #endif /* TTY_GID_SUPPORT */ |
339 | #endif /* TTY_GID_SUPPORT */ |
332 | { |
340 | { |
333 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
341 | mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; |
334 | gid = 0; |
342 | gid = 0; |
335 | } |
343 | } |
336 | } |
344 | } |