ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/ptytty.C
(Generate patch)

Comparing rxvt-unicode/src/ptytty.C (file contents):
Revision 1.41 by root, Tue Jan 17 15:41:33 2006 UTC vs.
Revision 1.49 by ayin, Thu Jan 19 16:40:42 2006 UTC

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 */
69static int 69static inline int
70get_pty (int *fd_tty, char **ttydev) 70get_pty_streams (int *fd_tty, char **ttydev)
71{ 71{
72#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT)
73# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX)
72 int pfd; 74 int pfd;
73 75
76# if defined(PTYS_ARE_GETPT)
77 pfd = getpt();
78# elif defined(PTYS_ARE_POSIX)
79 pfd = posix_openpt (O_RDWR);
80# else
81 pfd = open ("/dev/ptmx", O_RDWR | O_NOCTTY, 0);
82# endif
83 if (pfd >= 0)
84 {
85 if (grantpt (pfd) == 0 /* change slave permissions */
86 && unlockpt (pfd) == 0)
87 { /* slave now unlocked */
88 *ttydev = strdup (ptsname (pfd)); /* get slave's name */
89 return pfd;
90 }
91
92 close (pfd);
93 }
94# endif
95#endif
96
97 return -1;
98}
99
100static inline int
101get_pty_openpty (int *fd_tty, char **ttydev)
102{
74#ifdef PTYS_ARE_OPENPTY 103#ifdef PTYS_ARE_OPENPTY
104 int pfd;
105 int res;
75 char tty_name[sizeof "/dev/pts/????\0"]; 106 char tty_name[sizeof "/dev/pts/????\0"];
76 107
77 int res = openpty (&pfd, fd_tty, tty_name, NULL, NULL); 108 res = openpty (&pfd, fd_tty, tty_name, NULL, NULL);
78
79 if (res != -1) 109 if (res != -1)
80 { 110 {
81 *ttydev = strdup (tty_name); 111 *ttydev = strdup (tty_name);
82 return pfd; 112 return pfd;
83 } 113 }
84#endif 114#endif
85 115
116 return -1;
117}
118
119static inline int
120get_pty__getpty (int *fd_tty, char **ttydev)
121{
86#ifdef PTYS_ARE__GETPTY 122#ifdef PTYS_ARE__GETPTY
123 int pfd;
124
87 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0); 125 *ttydev = _getpty (&pfd, O_RDWR | O_NONBLOCK | O_NOCTTY, 0622, 0);
88 if (*ttydev != NULL) 126 if (*ttydev != NULL)
89 return pfd; 127 return pfd;
90#endif 128#endif
91 129
92#if defined(HAVE_GRANTPT) && defined(HAVE_UNLOCKPT) 130 return -1;
93# if defined(PTYS_ARE_GETPT) || defined(PTYS_ARE_POSIX) || defined(PTYS_ARE_PTMX) 131}
94 132
95 { 133static inline int
96# ifdef PTYS_ARE_GETPT 134get_pty_ptc (int *fd_tty, char **ttydev)
97 pfd = getpt(); 135{
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 136#ifdef PTYS_ARE_PTC
137 int pfd;
138
121 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0) 139 if ((pfd = open ("/dev/ptc", O_RDWR | O_NOCTTY, 0)) >= 0)
122 { 140 {
123 *ttydev = strdup (ttyname (pfd)); 141 *ttydev = strdup (ttyname (pfd));
124 return pfd; 142 return pfd;
125 } 143 }
126#endif 144#endif
127 145
146 return -1;
147}
148
149static inline int
150get_pty_clone (int *fd_tty, char **ttydev)
151{
128#ifdef PTYS_ARE_CLONE 152#ifdef PTYS_ARE_CLONE
153 int pfd;
154
129 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0) 155 if ((pfd = open ("/dev/ptym/clone", O_RDWR | O_NOCTTY, 0)) >= 0)
130 { 156 {
131 *ttydev = strdup (ptsname (pfd)); 157 *ttydev = strdup (ptsname (pfd));
132 return pfd; 158 return pfd;
133 } 159 }
134#endif 160#endif
135 161
162 return -1;
163}
164
165static inline int
166get_pty_numeric (int *fd_tty, char **ttydev)
167{
136#ifdef PTYS_ARE_NUMERIC 168#ifdef PTYS_ARE_NUMERIC
137 { 169 int pfd;
138 int idx; 170 int idx;
139 char *c1, *c2; 171 char *c1, *c2;
140 char pty_name[] = "/dev/ptyp???"; 172 char pty_name[] = "/dev/ptyp???";
141 char tty_name[] = "/dev/ttyp???"; 173 char tty_name[] = "/dev/ttyp???";
142 174
143 c1 = &(pty_name[sizeof (pty_name) - 4]); 175 c1 = &(pty_name[sizeof (pty_name) - 4]);
144 c2 = &(tty_name[sizeof (tty_name) - 4]); 176 c2 = &(tty_name[sizeof (tty_name) - 4]);
177
145 for (idx = 0; idx < 256; idx++) 178 for (idx = 0; idx < 256; idx++)
146 { 179 {
147 sprintf (c1, "%d", idx); 180 sprintf (c1, "%d", idx);
148 sprintf (c2, "%d", idx); 181 sprintf (c2, "%d", idx);
182
149 if (access (tty_name, F_OK) < 0) 183 if (access (tty_name, F_OK) < 0)
150 { 184 {
151 idx = 256; 185 idx = 256;
152 break; 186 break;
153 } 187 }
154 188
155 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 189 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
156 { 190 {
157 if (access (tty_name, R_OK | W_OK) == 0) 191 if (access (tty_name, R_OK | W_OK) == 0)
158 { 192 {
159 *ttydev = strdup (tty_name); 193 *ttydev = strdup (tty_name);
160 return pfd; 194 return pfd;
161 } 195 }
162 196
163 close (pfd); 197 close (pfd);
164 } 198 }
165 } 199 }
166 }
167#endif 200#endif
168 201
202 return -1;
203}
204
205static inline int
206get_pty_searched (int *fd_tty, char **ttydev)
207{
169#ifdef PTYS_ARE_SEARCHED 208#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 209# ifndef PTYCHAR1
176# define PTYCHAR1 "pqrstuvwxyz" 210# define PTYCHAR1 "pqrstuvwxyz"
177# endif 211# endif
178# ifndef PTYCHAR2 212# ifndef PTYCHAR2
179# define PTYCHAR2 "0123456789abcdef" 213# define PTYCHAR2 "0123456789abcdef"
180# endif 214# endif
215 int pfd;
216 const char *c1, *c2;
217 char pty_name[] = "/dev/pty??";
218 char tty_name[] = "/dev/tty??";
181 219
182 for (c1 = PTYCHAR1; *c1; c1++) 220 for (c1 = PTYCHAR1; *c1; c1++)
183 { 221 {
184 pty_name[ (sizeof (pty_name) - 3)] = 222 pty_name[ (sizeof (pty_name) - 3)] =
185 tty_name[ (sizeof (pty_name) - 3)] = *c1; 223 tty_name[ (sizeof (pty_name) - 3)] = *c1;
224
186 for (c2 = PTYCHAR2; *c2; c2++) 225 for (c2 = PTYCHAR2; *c2; c2++)
187 { 226 {
188 pty_name[ (sizeof (pty_name) - 2)] = 227 pty_name[ (sizeof (pty_name) - 2)] =
189 tty_name[ (sizeof (pty_name) - 2)] = *c2; 228 tty_name[ (sizeof (pty_name) - 2)] = *c2;
229
190 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0) 230 if ((pfd = open (pty_name, O_RDWR | O_NOCTTY, 0)) >= 0)
191 { 231 {
192 if (access (tty_name, R_OK | W_OK) == 0) 232 if (access (tty_name, R_OK | W_OK) == 0)
193 { 233 {
194 *ttydev = strdup (tty_name); 234 *ttydev = strdup (tty_name);
195 return pfd; 235 return pfd;
196 } 236 }
197 237
198 close (pfd); 238 close (pfd);
199 } 239 }
200 } 240 }
201 } 241 }
202 }
203#endif 242#endif
243
244 return -1;
245}
246
247static int
248get_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;
204 260
205 return -1; 261 return -1;
206} 262}
207 263
208/*----------------------------------------------------------------------*/ 264/*----------------------------------------------------------------------*/
220 * Make our tty a controlling tty so that /dev/tty points to us 276 * Make our tty a controlling tty so that /dev/tty points to us
221 */ 277 */
222static int 278static int
223control_tty (int fd_tty) 279control_tty (int fd_tty)
224{ 280{
225 int fd;
226
227 /* ---------------------------------------- */
228 setsid (); 281 setsid ();
229 282
230 /* ---------------------------------------- */
231# if defined(PTYS_ARE_PTMX) && defined(I_PUSH) 283#if defined(PTYS_ARE_PTMX) && defined(I_PUSH)
232 /* 284 /*
233 * Push STREAMS modules: 285 * Push STREAMS modules:
234 * ptem: pseudo-terminal hardware emulation module. 286 * ptem: pseudo-terminal hardware emulation module.
235 * ldterm: standard terminal line discipline. 287 * ldterm: standard terminal line discipline.
236 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module. 288 * ttcompat: V7, 4BSD and XENIX STREAMS compatibility module.
244 * documentation is really unclear about whether it is any close () on 296 * documentation is really unclear about whether it is any close () on
245 * the master side or the last close () - i.e. a proper STREAMS dismantling 297 * the master side or the last close () - i.e. a proper STREAMS dismantling
246 * close () - on the master side which causes a hang up to be sent 298 * close () - on the master side which causes a hang up to be sent
247 * through - Geoff Wing 299 * through - Geoff Wing
248 */ 300 */
249# ifdef HAVE_ISASTREAM 301# ifdef HAVE_ISASTREAM
250 if (isastream (fd_tty) == 1) 302 if (isastream (fd_tty) == 1)
251# endif 303# endif
252 { 304 {
253 ioctl (fd_tty, I_PUSH, "ptem"); 305 ioctl (fd_tty, I_PUSH, "ptem");
254 ioctl (fd_tty, I_PUSH, "ldterm"); 306 ioctl (fd_tty, I_PUSH, "ldterm");
255 ioctl (fd_tty, I_PUSH, "ttcompat"); 307 ioctl (fd_tty, I_PUSH, "ttcompat");
256 } 308 }
257# endif 309#endif
258 /* ---------------------------------------- */ 310
259 fd = ioctl (fd_tty, TIOCSCTTY, NULL); 311 ioctl (fd_tty, TIOCSCTTY, NULL);
260 /* ---------------------------------------- */ 312
261 fd = open ("/dev/tty", O_WRONLY); 313 int fd = open ("/dev/tty", O_WRONLY);
262 if (fd < 0) 314 if (fd < 0)
263 return -1; /* fatal */ 315 return -1; /* fatal */
316
264 close (fd); 317 close (fd);
265 /* ---------------------------------------- */
266 318
267 return 0; 319 return 0;
268} 320}
269 321
270void 322void
308 } 360 }
309 } 361 }
310#endif 362#endif
311} 363}
312 364
313/////////////////////////////////////////////////////////////////////////////
314
315#ifndef NO_SETOWNER_TTYDEV
316static struct ttyconf { 365static struct ttyconf {
317 gid_t gid; 366 gid_t gid;
318 mode_t mode; 367 mode_t mode;
319 368
320 ttyconf () 369 ttyconf ()
329 } 378 }
330 else 379 else
331#endif /* TTY_GID_SUPPORT */ 380#endif /* TTY_GID_SUPPORT */
332 { 381 {
333 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH; 382 mode = S_IRUSR | S_IWUSR | S_IWGRP | S_IWOTH;
334 gid = getgid (); 383 gid = 0;
335 } 384 }
336 } 385 }
337} ttyconf; 386} ttyconf;
338
339/////////////////////////////////////////////////////////////////////////////
340
341void
342rxvt_ptytty_unix::privileges (rxvt_privaction action)
343{
344 if (!name || !*name)
345 return;
346
347 if (action == SAVE)
348 {
349# ifndef RESET_TTY_TO_COMMON_DEFAULTS
350 /* store original tty status for restoration rxvt_clean_exit () -- rgg 04/12/95 */
351 if (lstat (name, &savestat) < 0) /* you lose out */
352 ;
353 else
354# endif
355 {
356 saved = true;
357 chown (name, getuid (), ttyconf.gid); /* fail silently */
358 chmod (name, ttyconf.mode);
359# ifdef HAVE_REVOKE
360 revoke (name);
361# endif
362 }
363 }
364 else
365 { /* action == RESTORE */
366# ifndef RESET_TTY_TO_COMMON_DEFAULTS
367 if (saved)
368 {
369 chmod (name, savestat.st_mode);
370 chown (name, savestat.st_uid, savestat.st_gid);
371 }
372# else
373 chmod (name, (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH));
374 chown (name, 0, 0);
375# endif
376
377 }
378}
379#endif
380 387
381rxvt_ptytty_unix::rxvt_ptytty_unix () 388rxvt_ptytty_unix::rxvt_ptytty_unix ()
382{ 389{
383 pty = tty = -1; 390 pty = tty = -1;
384 name = 0; 391 name = 0;
385#ifndef NO_SETOWNER_TTYDEV
386 saved = false;
387#endif
388#if UTMP_SUPPORT 392#if UTMP_SUPPORT
389 cmd_pid = 0; 393 cmd_pid = 0;
390#endif 394#endif
391} 395}
392 396
393rxvt_ptytty_unix::~rxvt_ptytty_unix () 397rxvt_ptytty_unix::~rxvt_ptytty_unix ()
394{ 398{
399#if UTMP_SUPPORT
395 logout (); 400 logout ();
401#endif
396 put (); 402 put ();
397} 403}
398 404
399void 405void
400rxvt_ptytty_unix::put () 406rxvt_ptytty_unix::put ()
401{ 407{
402#ifndef NO_SETOWNER_TTYDEV 408 chmod (name, RESTORE_TTY_MODE);
403 privileges (RESTORE); 409 chown (name, 0, ttyconf.gid);
404#endif
405 410
406 if (pty >= 0) close (pty); 411 if (pty >= 0) close (pty);
407 close_tty (); 412 close_tty ();
408 free (name); 413 free (name);
409 414
422 427
423 /* get slave (tty) */ 428 /* get slave (tty) */
424 if (tty < 0) 429 if (tty < 0)
425 { 430 {
426#ifndef NO_SETOWNER_TTYDEV 431#ifndef NO_SETOWNER_TTYDEV
427 privileges (SAVE); 432 chown (name, getuid (), ttyconf.gid); /* fail silently */
433 chmod (name, ttyconf.mode);
434# ifdef HAVE_REVOKE
435 revoke (name);
436# endif
428#endif 437#endif
429 438
430 if ((tty = get_tty (name)) < 0) 439 if ((tty = get_tty (name)) < 0)
431 { 440 {
432 put (); 441 put ();
537 write (sock_fd, &cmd.id, sizeof (cmd.id)); 546 write (sock_fd, &cmd.id, sizeof (cmd.id));
538 } 547 }
539 } 548 }
540 else if (cmd.type == command::login) 549 else if (cmd.type == command::login)
541 { 550 {
551#if UTMP_SUPPORT
542 if (find (ptys.begin (), ptys.end (), cmd.id)) 552 if (find (ptys.begin (), ptys.end (), cmd.id))
543 { 553 {
544 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 554 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
545 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 555 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
546 } 556 }
557#endif
547 } 558 }
548 else if (cmd.type == command::destroy) 559 else if (cmd.type == command::destroy)
549 { 560 {
550 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); 561 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id);
551 562
552 if (*pty) 563 if (pty)
553 { 564 {
565 delete *pty;
554 ptys.erase (pty); 566 ptys.erase (pty);
555 delete *pty;
556 } 567 }
557 } 568 }
558 else 569 else
559 break; 570 break;
560 } 571 }
583 close (sv[1]); 594 close (sv[1]);
584 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 595 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
585 } 596 }
586 else 597 else
587 { 598 {
588 setgid (getegid ());
589 setuid (geteuid ());
590
591 // server, pty-helper 599 // server, pty-helper
592 sock_fd = sv[1]; 600 sock_fd = sv[1];
593 601
594 for (int fd = 0; fd < 1023; fd++) 602 for (int fd = 0; fd < 1023; fd++)
595 if (fd != sock_fd) 603 if (fd != sock_fd)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines