… | |
… | |
64 | /* |
64 | /* |
65 | * Returns pty file descriptor, or -1 on failure |
65 | * Returns pty file descriptor, or -1 on failure |
66 | * If successful, ttydev is set to the name of the slave device. |
66 | * 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 |
67 | * fd_tty _may_ also be set to an open fd to the slave device |
68 | */ |
68 | */ |
69 | static int |
69 | static inline int |
70 | get_pty (int *fd_tty, char **ttydev) |
70 | get_pty_streams (int *fd_tty, char **ttydev) |
71 | { |
71 | { |
|
|
72 | #ifdef NO_SETOWNER_TTYDEV |
72 | int pfd; |
73 | int pfd; |
73 | |
74 | |
|
|
75 | # if defined(PTYS_ARE_GETPT) |
|
|
76 | pfd = getpt(); |
|
|
77 | # elif defined(PTYS_ARE_POSIX) |
|
|
78 | pfd = posix_openpt (O_RDWR); |
|
|
79 | # else |
|
|
80 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
|
|
81 | # endif |
|
|
82 | if (pfd >= 0) |
|
|
83 | { |
|
|
84 | if (grantpt (pfd) == 0 /* change slave permissions */ |
|
|
85 | && unlockpt (pfd) == 0) |
|
|
86 | { /* slave now unlocked */ |
|
|
87 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
|
|
88 | return pfd; |
|
|
89 | } |
|
|
90 | |
|
|
91 | close (pfd); |
|
|
92 | } |
|
|
93 | #endif |
|
|
94 | |
|
|
95 | return -1; |
|
|
96 | } |
|
|
97 | |
|
|
98 | static inline int |
|
|
99 | get_pty_openpty (int *fd_tty, char **ttydev) |
|
|
100 | { |
74 | #ifdef PTYS_ARE_OPENPTY |
101 | #ifdef PTYS_ARE_OPENPTY |
|
|
102 | int pfd; |
|
|
103 | int res; |
75 | char tty_name[sizeof "/dev/pts/????\0"]; |
104 | char tty_name[sizeof "/dev/pts/????\0"]; |
76 | |
105 | |
77 | int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
106 | res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); |
78 | |
|
|
79 | if (res != -1) |
107 | if (res != -1) |
80 | { |
108 | { |
81 | *ttydev = strdup (tty_name); |
109 | *ttydev = strdup (tty_name); |
82 | return pfd; |
110 | return pfd; |
83 | } |
111 | } |
84 | #endif |
112 | #endif |
85 | |
113 | |
|
|
114 | return -1; |
|
|
115 | } |
|
|
116 | |
|
|
117 | static inline int |
|
|
118 | get_pty__getpty (int *fd_tty, char **ttydev) |
|
|
119 | { |
86 | #ifdef PTYS_ARE__GETPTY |
120 | #ifdef PTYS_ARE__GETPTY |
|
|
121 | int pfd; |
|
|
122 | |
87 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
123 | *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); |
88 | if (*ttydev != NULL) |
124 | if (*ttydev != NULL) |
89 | return pfd; |
125 | return pfd; |
90 | #endif |
126 | #endif |
91 | |
127 | |
92 | #if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) |
128 | return -1; |
93 | # if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) |
129 | } |
94 | |
130 | |
95 | { |
131 | static inline int |
96 | # ifdef PTYS_ARE_GETPT |
132 | get_pty_ptc (int *fd_tty, char **ttydev) |
97 | pfd = getpt(); |
133 | { |
98 | # else |
|
|
99 | # ifdef PTYS_ARE_POSIX |
|
|
100 | pfd = posix_openpt (O_RDWR); |
|
|
101 | # else |
|
|
102 | pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0); |
|
|
103 | # endif |
|
|
104 | # endif |
|
|
105 | |
|
|
106 | if (pfd >= 0) |
|
|
107 | { |
|
|
108 | if (grantpt (pfd) == 0 /* change slave permissions */ |
|
|
109 | && unlockpt (pfd) == 0) |
|
|
110 | { /* slave now unlocked */ |
|
|
111 | *ttydev = strdup (ptsname (pfd)); /* get slave's name */ |
|
|
112 | return pfd; |
|
|
113 | } |
|
|
114 | close (pfd); |
|
|
115 | } |
|
|
116 | } |
|
|
117 | # endif |
|
|
118 | #endif |
|
|
119 | |
|
|
120 | #ifdef PTYS_ARE_PTC |
134 | #ifdef PTYS_ARE_PTC |
|
|
135 | int pfd; |
|
|
136 | |
121 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
137 | if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) |
122 | { |
138 | { |
123 | *ttydev = strdup (ttyname (pfd)); |
139 | *ttydev = strdup (ttyname (pfd)); |
124 | return pfd; |
140 | return pfd; |
125 | } |
141 | } |
126 | #endif |
142 | #endif |
127 | |
143 | |
|
|
144 | return -1; |
|
|
145 | } |
|
|
146 | |
|
|
147 | static inline int |
|
|
148 | get_pty_clone (int *fd_tty, char **ttydev) |
|
|
149 | { |
128 | #ifdef PTYS_ARE_CLONE |
150 | #ifdef PTYS_ARE_CLONE |
|
|
151 | int pfd; |
|
|
152 | |
129 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
153 | if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) |
130 | { |
154 | { |
131 | *ttydev = strdup (ptsname (pfd)); |
155 | *ttydev = strdup (ptsname (pfd)); |
132 | return pfd; |
156 | return pfd; |
133 | } |
157 | } |
134 | #endif |
158 | #endif |
135 | |
159 | |
|
|
160 | return -1; |
|
|
161 | } |
|
|
162 | |
|
|
163 | static inline int |
|
|
164 | get_pty_numeric (int *fd_tty, char **ttydev) |
|
|
165 | { |
136 | #ifdef PTYS_ARE_NUMERIC |
166 | #ifdef PTYS_ARE_NUMERIC |
137 | { |
167 | int pfd; |
138 | int idx; |
168 | int idx; |
139 | char *c1, *c2; |
169 | char *c1, *c2; |
140 | char pty_name[] = "/dev/ptyp???"; |
170 | char pty_name[] = "/dev/ptyp???"; |
141 | char tty_name[] = "/dev/ttyp???"; |
171 | char tty_name[] = "/dev/ttyp???"; |
142 | |
172 | |
143 | c1 = &(pty_name[sizeof (pty_name) - 4]); |
173 | c1 = &(pty_name[sizeof (pty_name) - 4]); |
144 | c2 = &(tty_name[sizeof (tty_name) - 4]); |
174 | c2 = &(tty_name[sizeof (tty_name) - 4]); |
|
|
175 | |
145 | for (idx = 0; idx < 256; idx++) |
176 | for (idx = 0; idx < 256; idx++) |
146 | { |
177 | { |
147 | sprintf (c1, "%d", idx); |
178 | sprintf (c1, "%d", idx); |
148 | sprintf (c2, "%d", idx); |
179 | sprintf (c2, "%d", idx); |
|
|
180 | |
149 | if (access (tty_name, F_OK) < 0) |
181 | if (access (tty_name, F_OK) < 0) |
150 | { |
182 | { |
151 | idx = 256; |
183 | idx = 256; |
152 | break; |
184 | break; |
153 | } |
185 | } |
154 | |
186 | |
155 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
187 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
156 | { |
188 | { |
157 | if (access (tty_name, R_OK | W_OK) == 0) |
189 | if (access (tty_name, R_OK | W_OK) == 0) |
158 | { |
190 | { |
159 | *ttydev = strdup (tty_name); |
191 | *ttydev = strdup (tty_name); |
160 | return pfd; |
192 | return pfd; |
161 | } |
193 | } |
162 | |
194 | |
163 | close (pfd); |
195 | close (pfd); |
164 | } |
196 | } |
165 | } |
197 | } |
166 | } |
|
|
167 | #endif |
198 | #endif |
168 | |
199 | |
|
|
200 | return -1; |
|
|
201 | } |
|
|
202 | |
|
|
203 | static inline int |
|
|
204 | get_pty_searched (int *fd_tty, char **ttydev) |
|
|
205 | { |
169 | #ifdef PTYS_ARE_SEARCHED |
206 | #ifdef PTYS_ARE_SEARCHED |
170 | { |
|
|
171 | const char *c1, *c2; |
|
|
172 | char pty_name[] = "/dev/pty??"; |
|
|
173 | char tty_name[] = "/dev/tty??"; |
|
|
174 | |
|
|
175 | # ifndef PTYCHAR1 |
207 | # ifndef PTYCHAR1 |
176 | # define PTYCHAR1 "pqrstuvwxyz" |
208 | # define PTYCHAR1 "pqrstuvwxyz" |
177 | # endif |
209 | # endif |
178 | # ifndef PTYCHAR2 |
210 | # ifndef PTYCHAR2 |
179 | # define PTYCHAR2 "0123456789abcdef" |
211 | # define PTYCHAR2 "0123456789abcdef" |
180 | # endif |
212 | # endif |
|
|
213 | int pfd; |
|
|
214 | const char *c1, *c2; |
|
|
215 | char pty_name[] = "/dev/pty??"; |
|
|
216 | char tty_name[] = "/dev/tty??"; |
181 | |
217 | |
182 | for (c1 = PTYCHAR1; *c1; c1++) |
218 | for (c1 = PTYCHAR1; *c1; c1++) |
183 | { |
219 | { |
184 | pty_name[ (sizeof (pty_name) - 3)] = |
220 | pty_name[ (sizeof (pty_name) - 3)] = |
185 | tty_name[ (sizeof (pty_name) - 3)] = *c1; |
221 | tty_name[ (sizeof (pty_name) - 3)] = *c1; |
|
|
222 | |
186 | for (c2 = PTYCHAR2; *c2; c2++) |
223 | for (c2 = PTYCHAR2; *c2; c2++) |
187 | { |
224 | { |
188 | pty_name[ (sizeof (pty_name) - 2)] = |
225 | pty_name[ (sizeof (pty_name) - 2)] = |
189 | tty_name[ (sizeof (pty_name) - 2)] = *c2; |
226 | tty_name[ (sizeof (pty_name) - 2)] = *c2; |
|
|
227 | |
190 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
228 | if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) |
191 | { |
229 | { |
192 | if (access (tty_name, R_OK | W_OK) == 0) |
230 | if (access (tty_name, R_OK | W_OK) == 0) |
193 | { |
231 | { |
194 | *ttydev = strdup (tty_name); |
232 | *ttydev = strdup (tty_name); |
195 | return pfd; |
233 | return pfd; |
196 | } |
234 | } |
197 | |
235 | |
198 | close (pfd); |
236 | close (pfd); |
199 | } |
237 | } |
200 | } |
238 | } |
201 | } |
239 | } |
202 | } |
|
|
203 | #endif |
240 | #endif |
|
|
241 | |
|
|
242 | return -1; |
|
|
243 | } |
|
|
244 | |
|
|
245 | static int |
|
|
246 | get_pty (int *fd_tty, char **ttydev) |
|
|
247 | { |
|
|
248 | int pfd; |
|
|
249 | |
|
|
250 | if ((pfd = get_pty_streams (fd_tty, ttydev)) != -1 |
|
|
251 | || (pfd = get_pty_openpty (fd_tty, ttydev)) != -1 |
|
|
252 | || (pfd = get_pty__getpty (fd_tty, ttydev)) != -1 |
|
|
253 | || (pfd = get_pty_ptc (fd_tty, ttydev)) != -1 |
|
|
254 | || (pfd = get_pty_clone (fd_tty, ttydev)) != -1 |
|
|
255 | || (pfd = get_pty_numeric (fd_tty, ttydev)) != -1 |
|
|
256 | || (pfd = get_pty_searched (fd_tty, ttydev)) != -1) |
|
|
257 | return pfd; |
204 | |
258 | |
205 | return -1; |
259 | return -1; |
206 | } |
260 | } |
207 | |
261 | |
208 | /*----------------------------------------------------------------------*/ |
262 | /*----------------------------------------------------------------------*/ |
… | |
… | |
220 | * Make our tty a controlling tty so that /dev/tty points to us |
274 | * Make our tty a controlling tty so that /dev/tty points to us |
221 | */ |
275 | */ |
222 | static int |
276 | static int |
223 | control_tty (int fd_tty) |
277 | control_tty (int fd_tty) |
224 | { |
278 | { |
225 | int fd; |
|
|
226 | |
|
|
227 | /* ---------------------------------------- */ |
|
|
228 | setsid (); |
279 | setsid (); |
229 | |
280 | |
230 | /* ---------------------------------------- */ |
|
|
231 | # if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
281 | # if defined(PTYS_ARE_PTMX) && defined(I_PUSH) |
232 | /* |
282 | /* |
233 | * Push STREAMS modules: |
283 | * Push STREAMS modules: |
234 | * ptem: pseudo-terminal hardware emulation module. |
284 | * ptem: pseudo-terminal hardware emulation module. |
235 | * ldterm: standard terminal line discipline. |
285 | * ldterm: standard terminal line discipline. |
… | |
… | |
253 | ioctl (fd_tty, I_PUSH, "ptem"); |
303 | ioctl (fd_tty, I_PUSH, "ptem"); |
254 | ioctl (fd_tty, I_PUSH, "ldterm"); |
304 | ioctl (fd_tty, I_PUSH, "ldterm"); |
255 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
305 | ioctl (fd_tty, I_PUSH, "ttcompat"); |
256 | } |
306 | } |
257 | # endif |
307 | # endif |
258 | /* ---------------------------------------- */ |
308 | |
259 | fd = ioctl (fd_tty, TIOCSCTTY, NULL); |
309 | ioctl (fd_tty, TIOCSCTTY, NULL); |
260 | /* ---------------------------------------- */ |
310 | |
261 | fd = open ("/dev/tty", O_WRONLY); |
311 | int fd = open ("/dev/tty", O_WRONLY); |
262 | if (fd < 0) |
312 | if (fd < 0) |
263 | return -1; /* fatal */ |
313 | return -1; /* fatal */ |
|
|
314 | |
264 | close (fd); |
315 | close (fd); |
265 | /* ---------------------------------------- */ |
|
|
266 | |
316 | |
267 | return 0; |
317 | return 0; |
268 | } |
318 | } |
269 | |
319 | |
270 | void |
320 | void |
… | |
… | |
371 | } |
421 | } |
372 | # else |
422 | # else |
373 | chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); |
423 | chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)); |
374 | chown (name, 0, 0); |
424 | chown (name, 0, 0); |
375 | # endif |
425 | # endif |
376 | |
|
|
377 | } |
426 | } |
378 | } |
427 | } |
379 | #endif |
428 | #endif |
380 | |
429 | |
381 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
430 | rxvt_ptytty_unix::rxvt_ptytty_unix () |