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.39 by root, Tue Jan 17 12:22:59 2006 UTC vs.
Revision 1.40 by root, Tue Jan 17 15:17:39 2006 UTC

22 *---------------------------------------------------------------------*/ 22 *---------------------------------------------------------------------*/
23 23
24#include "../config.h" /* NECESSARY */ 24#include "../config.h" /* NECESSARY */
25#include "rxvt.h" 25#include "rxvt.h"
26 26
27# include <cstdlib> 27#include <cstdlib>
28# include <cstring> 28#include <cstring>
29 29
30#ifdef HAVE_SYS_TYPES_H
31# include <sys/types.h> 30#include <sys/types.h>
32#endif 31#include <sys/socket.h>
33#ifdef HAVE_UNISTD_H
34# include <unistd.h> 32#include <unistd.h>
35#endif
36#ifdef HAVE_FCNTL_H
37# include <fcntl.h> 33#include <fcntl.h>
38#endif 34
39#ifdef HAVE_SYS_IOCTL_H 35#ifdef HAVE_SYS_IOCTL_H
40# include <sys/ioctl.h> 36# include <sys/ioctl.h>
41#endif 37#endif
42#if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H) 38#if defined(PTYS_ARE_PTMX) && defined(HAVE_SYS_STROPTS_H)
43# include <sys/stropts.h> /* for I_PUSH */ 39# include <sys/stropts.h> /* for I_PUSH */
54#endif 50#endif
55 51
56#include <cstdio> 52#include <cstdio>
57#include <grp.h> 53#include <grp.h>
58 54
55#include "rxvtutil.h"
56#include "fdpass.h"
59#include "ptytty.h" 57#include "ptytty.h"
60 58
61///////////////////////////////////////////////////////////////////////////// 59/////////////////////////////////////////////////////////////////////////////
62 60
63/* ------------------------------------------------------------------------- * 61/* ------------------------------------------------------------------------- *
443 } 441 }
444 442
445 return true; 443 return true;
446} 444}
447 445
446#if PTYTTY_HELPER
447
448static int sock_fd;
449static int pid;
450
451struct command
452{
453 enum { get, login, destroy } type;
454
455 rxvt_ptytty *id;
456
457 bool login_shell;
458 int cmd_pid;
459 char hostname[512]; // arbitrary, but should be plenty
460};
461
462struct rxvt_ptytty_proxy : zero_initialized, rxvt_ptytty
463{
464 rxvt_ptytty *id;
465
466 rxvt_ptytty_proxy ();
467 ~rxvt_ptytty_proxy ();
468
469 bool get ();
470 void login (int cmd_pid, bool login_shell, const char *hostname);
471};
472
473bool
474rxvt_ptytty_proxy::get ()
475{
476 command cmd;
477
478 cmd.type = command::get;
479
480 write (sock_fd, &cmd, sizeof (cmd));
481
482 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
483 rxvt_fatal ("protocol error while creating pty using helper process, aborting.\n");
484
485 if (!id)
486 return false;
487
488 if ((pty = rxvt_recv_fd (sock_fd)) < 0
489 || (tty = rxvt_recv_fd (sock_fd)) < 0)
490 rxvt_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
491
492 return true;
493}
494
495void
496rxvt_ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
497{
498 command cmd;
499
500 cmd.type = command::login;
501 cmd.id = id;
502 cmd.cmd_pid = cmd_pid;
503 cmd.login_shell = login_shell;
504 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
505
506 write (sock_fd, &cmd, sizeof (cmd));
507}
508
509rxvt_ptytty_proxy::~rxvt_ptytty_proxy ()
510{
511 command cmd;
512
513 cmd.type = command::destroy;
514 cmd.id = id;
515
516 write (sock_fd, &cmd, sizeof (cmd));
517}
518
519static
520void serve ()
521{
522 command cmd;
523 vector<rxvt_ptytty *> ptys;
524
525 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command))
526 {
527 if (cmd.type == command::get)
528 {
529 // -> id ptyfd ttyfd
530 cmd.id = new rxvt_ptytty_unix;
531
532 if (cmd.id->get ())
533 {
534 write (sock_fd, &cmd.id, sizeof (cmd.id));
535 ptys.push_back (cmd.id);
536
537 rxvt_send_fd (sock_fd, cmd.id->pty);
538 rxvt_send_fd (sock_fd, cmd.id->tty);
539 }
540 else
541 {
542 delete cmd.id;
543 cmd.id = 0;
544 write (sock_fd, &cmd.id, sizeof (cmd.id));
545 }
546 }
547 else if (cmd.type == command::login)
548 {
549 if (find (ptys.begin (), ptys.end (), cmd.id))
550 {
551 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
552 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
553 }
554 else printf ("xxx hiya login no match %p\n", cmd.id);
555 }
556 else if (cmd.type == command::destroy)
557 {
558 rxvt_ptytty **pty = find (ptys.begin (), ptys.end (), cmd.id);
559
560 if (*pty)
561 {
562 ptys.erase (pty);
563 delete *pty;
564 }
565 else printf ("xxx hiya destroy no match %p\n", cmd.id);
566 }
567 else
568 break;
569 }
570
571 // destroy all ptys
572 for (rxvt_ptytty **i = ptys.end (); i-- > ptys.begin (); )
573 delete *i;
574}
575
576void rxvt_ptytty_server ()
577{
578 int sv[2];
579
580 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
581 rxvt_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
582
583 pid = fork ();
584
585 if (pid < 0)
586 rxvt_fatal ("could not create pty/sessiondb helper process, aborting.\n");
587
588 if (pid)
589 {
590 // client, urxvt
591 sock_fd = sv[0];
592 close (sv[1]);
593 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
594 }
595 else
596 {
597 // server, pty-helper
598 sock_fd = sv[1];
599
600 close (sv[0]);//D
601// for (int fd = 0; fd < 1023; fd++)
602// if (fd != sock_fd)
603// close (fd);
604
605 serve ();
606 _exit (EXIT_SUCCESS);
607 }
608}
609#endif
610
448// a "factory" *g* 611// a "factory" *g*
449rxvt_ptytty *rxvt_new_ptytty () 612rxvt_ptytty *
613rxvt_new_ptytty ()
450{ 614{
615#if PTYTTY_HELPER
616 if (pid > 0)
617 {
618 // use helper process
619 return new rxvt_ptytty_proxy;
620 }
621 else
622#endif
451 return new rxvt_ptytty_unix; 623 return new rxvt_ptytty_unix;
452} 624}
453 625
454/*----------------------- end-of-file (C source) -----------------------*/ 626/*----------------------- end-of-file (C source) -----------------------*/
455 627

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines