… | |
… | |
33 | #include <fcntl.h> |
33 | #include <fcntl.h> |
34 | |
34 | |
35 | #ifdef HAVE_SYS_IOCTL_H |
35 | #ifdef HAVE_SYS_IOCTL_H |
36 | # include <sys/ioctl.h> |
36 | # include <sys/ioctl.h> |
37 | #endif |
37 | #endif |
38 | #if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) |
38 | #if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H) |
39 | # include <sys/stropts.h> /* for I_PUSH */ |
39 | # include <sys/stropts.h> /* for I_PUSH */ |
40 | #endif |
40 | #endif |
41 | #ifdef HAVE_ISASTREAM |
41 | #ifdef HAVE_ISASTREAM |
42 | # include <stropts.h> |
42 | # include <stropts.h> |
43 | #endif |
43 | #endif |
… | |
… | |
46 | #elif defined(HAVE_LIBUTIL_H) |
46 | #elif defined(HAVE_LIBUTIL_H) |
47 | # include <libutil.h> |
47 | # include <libutil.h> |
48 | #elif defined(HAVE_UTIL_H) |
48 | #elif defined(HAVE_UTIL_H) |
49 | # include <util.h> |
49 | # include <util.h> |
50 | #endif |
50 | #endif |
|
|
51 | #ifdef TTY_GID_SUPPORT |
|
|
52 | #include <grp.h> |
|
|
53 | #endif |
51 | |
54 | |
52 | #include <cstdio> |
55 | #include <cstdio> |
53 | #include <grp.h> |
|
|
54 | |
56 | |
55 | #include "rxvtutil.h" |
57 | #include "rxvtutil.h" |
56 | #include "fdpass.h" |
58 | #include "fdpass.h" |
57 | #include "ptytty.h" |
59 | #include "ptytty.h" |
58 | |
60 | |
… | |
… | |
64 | /* |
66 | /* |
65 | * Returns pty file descriptor, or -1 on failure |
67 | * Returns pty file descriptor, or -1 on failure |
66 | * If successful, ttydev is set to the name of the slave device. |
68 | * If successful, ttydev is set to the name of the slave device. |
67 | * fd_tty _may_ also be set to an open fd to the slave device |
69 | * fd_tty _may_ also be set to an open fd to the slave device |
68 | */ |
70 | */ |
69 | static inline int |
71 | #if defined(UNIX98_PTY) |
|
|
72 | static int |
70 | get_pty_streams (int *fd_tty, char **ttydev) |
73 | get_pty (int *fd_tty, char **ttydev) |
71 | { |
74 | { |
72 | #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) |
|
|
73 | # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) |
|
|
74 | int pfd; |
75 | int pfd; |
75 | |
76 | |
76 | # if defined(PTYS_ARE_GETPT) |
77 | # if defined(HAVE_GETPT) |
77 | pfd = getpt(); |
78 | pfd = getpt(); |
78 | # elif defined(PTYS_ARE_POSIX) |
79 | # elif defined(HAVE_POSIX_OPENPT) |
79 | pfd = posix_openpt (O_RDWR); |
80 | pfd = posix_openpt (O_RDWR); |
80 | # else |
81 | # else |
81 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
82 | pfd = open (CLONE_DEVICE, O_RDWR | O_NOCTTY, 0); |
82 | # endif |
83 | # endif |
83 | if (pfd >= 0) |
84 | if (pfd >= 0) |
84 | { |
85 | { |
85 | if (grantpt (pfd) == 0 /* change slave permissions */ |
86 | if (grantpt (pfd) == 0 /* change slave permissions */ |
86 | && unlockpt (pfd) == 0) |
87 | && unlockpt (pfd) == 0) |
… | |
… | |
89 | return pfd; |
90 | return pfd; |
90 | } |
91 | } |
91 | |
92 | |
92 | close (pfd); |
93 | close (pfd); |
93 | } |
94 | } |
94 | # endif |
|
|
95 | #endif |
|
|
96 | |
95 | |
97 | return -1; |
96 | return -1; |
98 | } |
97 | } |
99 | |
98 | #elif defined(HAVE_OPENPTY) |
100 | static inline int |
99 | static int |
101 | get_pty_openpty (int *fd_tty, char **ttydev) |
100 | get_pty (int *fd_tty, char **ttydev) |
102 | { |
101 | { |
103 | #ifdef PTYS_ARE_OPENPTY |
|
|
104 | int pfd; |
102 | int pfd; |
105 | int res; |
103 | int res; |
106 | char tty_name[sizeof "/dev/pts/????\0"]; |
104 | char tty_name[32]; |
107 | |
105 | |
108 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
106 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
109 | if (res != -1) |
107 | if (res != -1) |
110 | { |
108 | { |
111 | *ttydev = strdup (tty_name); |
109 | *ttydev = strdup (tty_name); |
112 | return pfd; |
110 | return pfd; |
113 | } |
111 | } |
114 | #endif |
|
|
115 | |
112 | |
116 | return -1; |
113 | return -1; |
117 | } |
114 | } |
118 | |
115 | #elif defined(HAVE__GETPTY) |
119 | static inline int |
116 | static int |
120 | get_pty__getpty (int *fd_tty, char **ttydev) |
117 | get_pty (int *fd_tty, char **ttydev) |
121 | { |
118 | { |
122 | #ifdef PTYS_ARE__GETPTY |
|
|
123 | int pfd; |
119 | int pfd; |
124 | |
120 | |
125 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
121 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
126 | if (*ttydev != NULL) |
122 | if (*ttydev != NULL) |
127 | return pfd; |
123 | return pfd; |
128 | #endif |
|
|
129 | |
124 | |
130 | return -1; |
125 | return -1; |
131 | } |
126 | } |
132 | |
127 | #elif defined(HAVE_DEV_PTC) |
133 | static inline int |
128 | static int |
134 | get_pty_ptc (int *fd_tty, char **ttydev) |
129 | get_pty (int *fd_tty, char **ttydev) |
135 | { |
130 | { |
136 | #ifdef PTYS_ARE_PTC |
|
|
137 | int pfd; |
131 | int pfd; |
138 | |
132 | |
139 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
133 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
140 | { |
134 | { |
141 | *ttydev = strdup (ttyname (pfd)); |
135 | *ttydev = strdup (ttyname (pfd)); |
142 | return pfd; |
136 | return pfd; |
143 | } |
137 | } |
144 | #endif |
|
|
145 | |
138 | |
146 | return -1; |
139 | return -1; |
147 | } |
140 | } |
148 | |
141 | #elif defined(HAVE_DEV_CLONE) |
149 | static inline int |
142 | static int |
150 | get_pty_clone (int *fd_tty, char **ttydev) |
143 | get_pty (int *fd_tty, char **ttydev) |
151 | { |
144 | { |
152 | #ifdef PTYS_ARE_CLONE |
|
|
153 | int pfd; |
145 | int pfd; |
154 | |
146 | |
155 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
147 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
156 | { |
148 | { |
157 | *ttydev = strdup (ptsname (pfd)); |
149 | *ttydev = strdup (ptsname (pfd)); |
158 | return pfd; |
150 | return pfd; |
159 | } |
151 | } |
160 | #endif |
|
|
161 | |
152 | |
162 | return -1; |
153 | return -1; |
163 | } |
154 | } |
164 | |
155 | #else |
165 | static inline int |
156 | /* Based on the code in openssh/openbsd-compat/bsd-openpty.c */ |
|
|
157 | static int |
166 | get_pty_numeric (int *fd_tty, char **ttydev) |
158 | get_pty (int *fd_tty, char **ttydev) |
167 | { |
159 | { |
168 | #ifdef PTYS_ARE_NUMERIC |
|
|
169 | int pfd; |
160 | int pfd; |
170 | int idx; |
161 | int i; |
171 | char *c1, *c2; |
162 | char pty_name[32]; |
172 | char pty_name[] = "/dev/ptyp???"; |
163 | char tty_name[32]; |
173 | char tty_name[] = "/dev/ttyp???"; |
164 | const char *majors = "pqrstuvwxyzabcde"; |
174 | |
165 | const char *minors = "0123456789abcdef"; |
175 | c1 = &(pty_name[sizeof (pty_name) - 4]); |
|
|
176 | c2 = &(tty_name[sizeof (tty_name) - 4]); |
|
|
177 | |
|
|
178 | for (idx = 0; idx < 256; idx++) |
166 | for (i = 0; i < 256; i++) |
179 | { |
167 | { |
180 | sprintf (c1, "%d", idx); |
168 | snprintf(pty_name, 32, "/dev/pty%c%c", majors[i / 16], minors[i % 16]); |
181 | sprintf (c2, "%d", idx); |
169 | snprintf(tty_name, 32, "/dev/tty%c%c", majors[i / 16], minors[i % 16]); |
182 | |
|
|
183 | if (access (tty_name, F_OK) < 0) |
|
|
184 | { |
|
|
185 | idx = 256; |
|
|
186 | break; |
|
|
187 | } |
|
|
188 | |
|
|
189 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
170 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
|
|
171 | { |
|
|
172 | snprintf(pty_name, 32, "/dev/ptyp%d", i); |
|
|
173 | snprintf(tty_name, 32, "/dev/ttyp%d", i); |
|
|
174 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) == -1) |
|
|
175 | continue; |
190 | { |
176 | } |
191 | if (access (tty_name, R_OK | W_OK) == 0) |
177 | if (access (tty_name, R_OK | W_OK) == 0) |
192 | { |
178 | { |
193 | *ttydev = strdup (tty_name); |
179 | *ttydev = strdup (tty_name); |
194 | return pfd; |
180 | return pfd; |
195 | } |
181 | } |
196 | |
182 | |
197 | close (pfd); |
183 | close (pfd); |
198 | } |
|
|
199 | } |
184 | } |
200 | #endif |
|
|
201 | |
|
|
202 | return -1; |
|
|
203 | } |
185 | } |
204 | |
|
|
205 | static inline int |
|
|
206 | get_pty_searched (int *fd_tty, char **ttydev) |
|
|
207 | { |
|
|
208 | #ifdef PTYS_ARE_SEARCHED |
|
|
209 | # ifndef PTYCHAR1 |
|
|
210 | # define PTYCHAR1 "pqrstuvwxyz" |
|
|
211 | # endif |
186 | #endif |
212 | # ifndef PTYCHAR2 |
|
|
213 | # define PTYCHAR2 "0123456789abcdef" |
|
|
214 | # endif |
|
|
215 | int pfd; |
|
|
216 | const char *c1, *c2; |
|
|
217 | char pty_name[] = "/dev/pty??"; |
|
|
218 | char tty_name[] = "/dev/tty??"; |
|
|
219 | |
|
|
220 | for (c1 = PTYCHAR1; *c1; c1++) |
|
|
221 | { |
|
|
222 | pty_name[ (sizeof (pty_name) - 3)] = |
|
|
223 | tty_name[ (sizeof (pty_name) - 3)] = *c1; |
|
|
224 | |
|
|
225 | for (c2 = PTYCHAR2; *c2; c2++) |
|
|
226 | { |
|
|
227 | pty_name[ (sizeof (pty_name) - 2)] = |
|
|
228 | tty_name[ (sizeof (pty_name) - 2)] = *c2; |
|
|
229 | |
|
|
230 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
|
|
231 | { |
|
|
232 | if (access (tty_name, R_OK | W_OK) == 0) |
|
|
233 | { |
|
|
234 | *ttydev = strdup (tty_name); |
|
|
235 | return pfd; |
|
|
236 | } |
|
|
237 | |
|
|
238 | close (pfd); |
|
|
239 | } |
|
|
240 | } |
|
|
241 | } |
|
|
242 | #endif |
|
|
243 | |
|
|
244 | return -1; |
|
|
245 | } |
|
|
246 | |
|
|
247 | static int |
|
|
248 | get_pty (int *fd_tty, char **ttydev) |
|
|
249 | { |
|
|
250 | int pfd; |
|
|
251 | |
|
|
252 | if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1 |
|
|
253 | || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1 |
|
|
254 | || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1 |
|
|
255 | || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1 |
|
|
256 | || (pfd = get_pty_clone (fd_tty, ttydev)) != -1 |
|
|
257 | || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 |
|
|
258 | || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) |
|
|
259 | return pfd; |
|
|
260 | |
|
|
261 | return -1; |
|
|
262 | } |
|
|
263 | |
187 | |
264 | /*----------------------------------------------------------------------*/ |
188 | /*----------------------------------------------------------------------*/ |
265 | /* |
189 | /* |
266 | * Returns tty file descriptor, or -1 on failure |
190 | * Returns tty file descriptor, or -1 on failure |
267 | */ |
191 | */ |
… | |
… | |
278 | static int |
202 | static int |
279 | control_tty (int fd_tty) |
203 | control_tty (int fd_tty) |
280 | { |
204 | { |
281 | setsid (); |
205 | setsid (); |
282 | |
206 | |
283 | #if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
207 | #if defined(HAVE_DEV_PTMX) && defined(I_PUSH) |
284 | /* |
208 | /* |
285 | * Push STREAMS modules: |
209 | * Push STREAMS modules: |
286 | * ptem: pseudo-terminal hardware emulation module. |
210 | * ptem: pseudo-terminal hardware emulation module. |
287 | * ldterm: standard terminal line discipline. |
211 | * ldterm: standard terminal line discipline. |
288 | * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
212 | * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. |
… | |
… | |
385 | } |
309 | } |
386 | } ttyconf; |
310 | } ttyconf; |
387 | |
311 | |
388 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
312 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
389 | { |
313 | { |
390 | pty = tty = -1; |
|
|
391 | name = 0; |
314 | name = 0; |
392 | #if UTMP_SUPPORT |
315 | #if UTMP_SUPPORT |
393 | cmd_pid = 0; |
316 | cmd_pid = 0; |
394 | #endif |
317 | #endif |
395 | } |
318 | } |