… | |
… | |
20 | * along with this program; if not, write to the Free Software |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
22 | *---------------------------------------------------------------------*/ |
22 | *---------------------------------------------------------------------*/ |
23 | |
23 | |
24 | #include "../config.h" /* NECESSARY */ |
24 | #include "../config.h" /* NECESSARY */ |
|
|
25 | |
|
|
26 | #include "fdpass.h" |
25 | #include "rxvt.h" |
27 | #include "ptytty.h" |
26 | |
28 | |
27 | #include <cstdlib> |
29 | #include <cstdlib> |
28 | #include <cstring> |
30 | #include <cstring> |
29 | |
31 | |
30 | #include <sys/types.h> |
32 | #include <sys/types.h> |
… | |
… | |
51 | #ifdef TTY_GID_SUPPORT |
53 | #ifdef TTY_GID_SUPPORT |
52 | #include <grp.h> |
54 | #include <grp.h> |
53 | #endif |
55 | #endif |
54 | |
56 | |
55 | #include <cstdio> |
57 | #include <cstdio> |
56 | |
|
|
57 | #include "rxvtutil.h" |
|
|
58 | #include "fdpass.h" |
|
|
59 | #include "ptytty.h" |
|
|
60 | |
58 | |
61 | ///////////////////////////////////////////////////////////////////////////// |
59 | ///////////////////////////////////////////////////////////////////////////// |
62 | |
60 | |
63 | /* ------------------------------------------------------------------------- * |
61 | /* ------------------------------------------------------------------------- * |
64 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
62 | * GET PSEUDO TELETYPE - MASTER AND SLAVE * |
… | |
… | |
242 | |
240 | |
243 | return 0; |
241 | return 0; |
244 | } |
242 | } |
245 | |
243 | |
246 | void |
244 | void |
247 | rxvt_ptytty::close_tty () |
245 | ptytty::close_tty () |
248 | { |
246 | { |
249 | if (tty < 0) |
247 | if (tty < 0) |
250 | return; |
248 | return; |
251 | |
249 | |
252 | close (tty); |
250 | close (tty); |
253 | tty = -1; |
251 | tty = -1; |
254 | } |
252 | } |
255 | |
253 | |
256 | bool |
254 | bool |
257 | rxvt_ptytty::make_controlling_tty () |
255 | ptytty::make_controlling_tty () |
258 | { |
256 | { |
259 | return control_tty (tty) >= 0; |
257 | return control_tty (tty) >= 0; |
260 | } |
258 | } |
261 | |
259 | |
262 | void |
260 | void |
263 | rxvt_ptytty::set_utf8_mode (bool on) |
261 | ptytty::set_utf8_mode (bool on) |
264 | { |
262 | { |
265 | #ifdef IUTF8 |
263 | #ifdef IUTF8 |
266 | if (pty < 0) |
264 | if (pty < 0) |
267 | return; |
265 | return; |
268 | |
266 | |
… | |
… | |
307 | gid = 0; |
305 | gid = 0; |
308 | } |
306 | } |
309 | } |
307 | } |
310 | } ttyconf; |
308 | } ttyconf; |
311 | |
309 | |
312 | rxvt_ptytty_unix::rxvt_ptytty_unix () |
310 | ptytty_unix::ptytty_unix () |
313 | { |
311 | { |
314 | name = 0; |
312 | name = 0; |
315 | #if UTMP_SUPPORT |
313 | #if UTMP_SUPPORT |
316 | cmd_pid = 0; |
314 | cmd_pid = 0; |
317 | #endif |
315 | #endif |
318 | } |
316 | } |
319 | |
317 | |
320 | rxvt_ptytty_unix::~rxvt_ptytty_unix () |
318 | ptytty_unix::~ptytty_unix () |
321 | { |
319 | { |
322 | #if UTMP_SUPPORT |
320 | #if UTMP_SUPPORT |
323 | logout (); |
321 | logout (); |
324 | #endif |
322 | #endif |
325 | put (); |
323 | put (); |
326 | } |
324 | } |
327 | |
325 | |
328 | void |
326 | void |
329 | rxvt_ptytty_unix::put () |
327 | ptytty_unix::put () |
330 | { |
328 | { |
331 | chmod (name, RESTORE_TTY_MODE); |
329 | chmod (name, RESTORE_TTY_MODE); |
332 | chown (name, 0, ttyconf.gid); |
330 | chown (name, 0, ttyconf.gid); |
333 | |
331 | |
334 | close_tty (); |
332 | close_tty (); |
… | |
… | |
341 | pty = tty = -1; |
339 | pty = tty = -1; |
342 | name = 0; |
340 | name = 0; |
343 | } |
341 | } |
344 | |
342 | |
345 | bool |
343 | bool |
346 | rxvt_ptytty_unix::get () |
344 | ptytty_unix::get () |
347 | { |
345 | { |
348 | /* get master (pty) */ |
346 | /* get master (pty) */ |
349 | if ((pty = get_pty (&tty, &name)) < 0) |
347 | if ((pty = get_pty (&tty, &name)) < 0) |
350 | return false; |
348 | return false; |
351 | |
349 | |
… | |
… | |
379 | |
377 | |
380 | struct command |
378 | struct command |
381 | { |
379 | { |
382 | enum { get, login, destroy } type; |
380 | enum { get, login, destroy } type; |
383 | |
381 | |
384 | rxvt_ptytty *id; |
382 | ptytty *id; |
385 | |
383 | |
386 | bool login_shell; |
384 | bool login_shell; |
387 | int cmd_pid; |
385 | int cmd_pid; |
388 | char hostname[512]; // arbitrary, but should be plenty |
386 | char hostname[512]; // arbitrary, but should be plenty |
389 | }; |
387 | }; |
390 | |
388 | |
391 | struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty |
389 | struct ptytty_proxy : ptytty |
392 | { |
390 | { |
393 | rxvt_ptytty *id; |
391 | ptytty *id; |
394 | |
392 | |
|
|
393 | ptytty_proxy () |
|
|
394 | : id(0) |
|
|
395 | { |
|
|
396 | } |
|
|
397 | |
395 | ~rxvt_ptytty_proxy (); |
398 | ~ptytty_proxy (); |
396 | |
399 | |
397 | bool get (); |
400 | bool get (); |
398 | void login (int cmd_pid, bool login_shell, const char *hostname); |
401 | void login (int cmd_pid, bool login_shell, const char *hostname); |
399 | }; |
402 | }; |
400 | |
403 | |
401 | bool |
404 | bool |
402 | rxvt_ptytty_proxy::get () |
405 | ptytty_proxy::get () |
403 | { |
406 | { |
404 | command cmd; |
407 | command cmd; |
405 | |
408 | |
406 | cmd.type = command::get; |
409 | cmd.type = command::get; |
407 | |
410 | |
408 | write (sock_fd, &cmd, sizeof (cmd)); |
411 | write (sock_fd, &cmd, sizeof (cmd)); |
409 | |
412 | |
410 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
413 | if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) |
411 | rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n"); |
414 | fatal ("protocol error while creating pty using helper process, aborting.\n"); |
412 | |
415 | |
413 | if (!id) |
416 | if (!id) |
414 | return false; |
417 | return false; |
415 | |
418 | |
416 | if ((pty = rxvt_recv_fd (sock_fd)) < 0 |
419 | if ((pty = ptytty_recv_fd (sock_fd)) < 0 |
417 | || (tty = rxvt_recv_fd (sock_fd)) < 0) |
420 | || (tty = ptytty_recv_fd (sock_fd)) < 0) |
418 | rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
421 | fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); |
419 | |
422 | |
420 | return true; |
423 | return true; |
421 | } |
424 | } |
422 | |
425 | |
423 | void |
426 | void |
424 | rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
427 | ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) |
425 | { |
428 | { |
426 | command cmd; |
429 | command cmd; |
427 | |
430 | |
428 | cmd.type = command::login; |
431 | cmd.type = command::login; |
429 | cmd.id = id; |
432 | cmd.id = id; |
… | |
… | |
432 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
435 | strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); |
433 | |
436 | |
434 | write (sock_fd, &cmd, sizeof (cmd)); |
437 | write (sock_fd, &cmd, sizeof (cmd)); |
435 | } |
438 | } |
436 | |
439 | |
437 | rxvt_ptytty_proxy::~rxvt_ptytty_proxy () |
440 | ptytty_proxy::~ptytty_proxy () |
438 | { |
441 | { |
|
|
442 | if (id) |
|
|
443 | { |
439 | command cmd; |
444 | command cmd; |
440 | |
445 | |
441 | cmd.type = command::destroy; |
446 | cmd.type = command::destroy; |
442 | cmd.id = id; |
447 | cmd.id = id; |
443 | |
448 | |
444 | write (sock_fd, &cmd, sizeof (cmd)); |
449 | write (sock_fd, &cmd, sizeof (cmd)); |
|
|
450 | } |
445 | } |
451 | } |
446 | |
452 | |
447 | static |
453 | static |
448 | void serve () |
454 | void serve () |
449 | { |
455 | { |
450 | command cmd; |
456 | command cmd; |
451 | vector<rxvt_ptytty *> ptys; |
457 | vector<ptytty *> ptys; |
452 | |
458 | |
453 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
459 | while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) |
454 | { |
460 | { |
455 | if (cmd.type == command::get) |
461 | if (cmd.type == command::get) |
456 | { |
462 | { |
457 | // -> id ptyfd ttyfd |
463 | // -> id ptyfd ttyfd |
458 | cmd.id = new rxvt_ptytty_unix; |
464 | cmd.id = new ptytty_unix; |
459 | |
465 | |
460 | if (cmd.id->get ()) |
466 | if (cmd.id->get ()) |
461 | { |
467 | { |
462 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
468 | write (sock_fd, &cmd.id, sizeof (cmd.id)); |
463 | ptys.push_back (cmd.id); |
469 | ptys.push_back (cmd.id); |
464 | |
470 | |
465 | rxvt_send_fd (sock_fd, cmd.id->pty); |
471 | ptytty_send_fd (sock_fd, cmd.id->pty); |
466 | rxvt_send_fd (sock_fd, cmd.id->tty); |
472 | ptytty_send_fd (sock_fd, cmd.id->tty); |
467 | } |
473 | } |
468 | else |
474 | else |
469 | { |
475 | { |
470 | delete cmd.id; |
476 | delete cmd.id; |
471 | cmd.id = 0; |
477 | cmd.id = 0; |
… | |
… | |
482 | } |
488 | } |
483 | #endif |
489 | #endif |
484 | } |
490 | } |
485 | else if (cmd.type == command::destroy) |
491 | else if (cmd.type == command::destroy) |
486 | { |
492 | { |
487 | rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
493 | ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id); |
488 | |
494 | |
489 | if (pty) |
495 | if (pty) |
490 | { |
496 | { |
491 | delete *pty; |
497 | delete *pty; |
492 | ptys.erase (pty); |
498 | ptys.erase (pty); |
… | |
… | |
495 | else |
501 | else |
496 | break; |
502 | break; |
497 | } |
503 | } |
498 | |
504 | |
499 | // destroy all ptys |
505 | // destroy all ptys |
500 | for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
506 | for (ptytty **i = ptys.end (); i-- > ptys.begin (); ) |
501 | delete *i; |
507 | delete *i; |
502 | } |
508 | } |
503 | |
509 | |
504 | void rxvt_ptytty_server () |
510 | void ptytty_server () |
505 | { |
511 | { |
506 | int sv[2]; |
512 | int sv[2]; |
507 | |
513 | |
508 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
514 | if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) |
509 | rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
515 | fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); |
510 | |
516 | |
511 | pid = fork (); |
517 | pid = fork (); |
512 | |
518 | |
513 | if (pid < 0) |
519 | if (pid < 0) |
514 | rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
520 | fatal ("could not create pty/sessiondb helper process, aborting.\n"); |
515 | |
521 | |
516 | if (pid) |
522 | if (pid) |
517 | { |
523 | { |
518 | // client, urxvt |
524 | // client, process |
519 | sock_fd = sv[0]; |
525 | sock_fd = sv[0]; |
520 | close (sv[1]); |
526 | close (sv[1]); |
521 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
527 | fcntl (sock_fd, F_SETFD, FD_CLOEXEC); |
522 | } |
528 | } |
523 | else |
529 | else |
… | |
… | |
535 | } |
541 | } |
536 | |
542 | |
537 | #endif |
543 | #endif |
538 | |
544 | |
539 | // a "factory" *g* |
545 | // a "factory" *g* |
540 | rxvt_ptytty * |
546 | ptytty * |
541 | rxvt_new_ptytty () |
547 | new_ptytty () |
542 | { |
548 | { |
543 | #if PTYTTY_HELPER |
549 | #if PTYTTY_HELPER |
544 | if (pid > 0) |
550 | if (pid > 0) |
545 | // use helper process |
551 | // use helper process |
546 | return new rxvt_ptytty_proxy; |
552 | return new ptytty_proxy; |
547 | else |
553 | else |
548 | #endif |
554 | #endif |
549 | return new rxvt_ptytty_unix; |
555 | return new ptytty_unix; |
550 | } |
556 | } |
551 | |
557 | |
552 | /*----------------------- end-of-file (C source) -----------------------*/ |
558 | /*----------------------- end-of-file (C source) -----------------------*/ |
553 | |
559 | |