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

Comparing libptytty/src/ptytty.C (file contents):
Revision 1.15 by root, Mon Jan 23 11:22:08 2006 UTC vs.
Revision 1.22 by root, Mon Jan 23 12:27:13 2006 UTC

29 29
30#include "ptytty.h" 30#include "ptytty.h"
31 31
32#include <cstdlib> 32#include <cstdlib>
33#include <cstring> 33#include <cstring>
34#include <csignal>
34 35
35#include <sys/types.h> 36#include <sys/types.h>
36#include <sys/socket.h> 37#include <sys/socket.h>
37#include <unistd.h> 38#include <unistd.h>
38#include <fcntl.h> 39#include <fcntl.h>
376///////////////////////////////////////////////////////////////////////////// 377/////////////////////////////////////////////////////////////////////////////
377// helper/proxy support 378// helper/proxy support
378 379
379#if PTYTTY_HELPER 380#if PTYTTY_HELPER
380 381
381static int sock_fd = -1; 382static int sock_fd = -1, lock_fd = -1;
382static int helper_pid, owner_pid; 383static int helper_pid, owner_pid;
383 384
384struct command 385struct command
385{ 386{
386 enum { get, login, destroy } type; 387 enum { get, login, destroy } type;
405 406
406 bool get (); 407 bool get ();
407 void login (int cmd_pid, bool login_shell, const char *hostname); 408 void login (int cmd_pid, bool login_shell, const char *hostname);
408}; 409};
409 410
411#if PTYTTY_REENTRANT
412# define NEED_TOKEN do { char ch; read (lock_fd, &ch, 1); } while (0)
413# define GIVE_TOKEN do { char ch; write (lock_fd, &ch, 1); } while (0)
414#else
415# define NEED_TOKEN (void)0
416# define GIVE_TOKEN (void)0
417#endif
418
410bool 419bool
411ptytty_proxy::get () 420ptytty_proxy::get ()
412{ 421{
422 NEED_TOKEN;
423
413 command cmd; 424 command cmd;
414 425
415 cmd.type = command::get; 426 cmd.type = command::get;
416 427
417 write (sock_fd, &cmd, sizeof (cmd)); 428 write (sock_fd, &cmd, sizeof (cmd));
418 429
419 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 430 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
420 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n"); 431 ptytty_fatal ("protocol error while creating pty using helper process, aborting.\n");
421 432
422 if (!id) 433 if (!id)
434 {
435 GIVE_TOKEN;
423 return false; 436 return false;
437 }
424 438
425 if ((pty = recv_fd (sock_fd)) < 0 439 if ((pty = recv_fd (sock_fd)) < 0
426 || (tty = recv_fd (sock_fd)) < 0) 440 || (tty = recv_fd (sock_fd)) < 0)
427 ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n"); 441 ptytty_fatal ("protocol error while reading pty/tty fds from helper process, aborting.\n");
428 442
443 GIVE_TOKEN;
429 return true; 444 return true;
430} 445}
431 446
432void 447void
433ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 448ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
434{ 449{
450 NEED_TOKEN;
451
435 command cmd; 452 command cmd;
436 453
437 cmd.type = command::login; 454 cmd.type = command::login;
438 cmd.id = id; 455 cmd.id = id;
439 cmd.cmd_pid = cmd_pid; 456 cmd.cmd_pid = cmd_pid;
440 cmd.login_shell = login_shell; 457 cmd.login_shell = login_shell;
441 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 458 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
442 459
443 write (sock_fd, &cmd, sizeof (cmd)); 460 write (sock_fd, &cmd, sizeof (cmd));
461
462 GIVE_TOKEN;
444} 463}
445 464
446ptytty_proxy::~ptytty_proxy () 465ptytty_proxy::~ptytty_proxy ()
447{ 466{
448 if (id) 467 if (id)
449 { 468 {
469 NEED_TOKEN;
470
450 command cmd; 471 command cmd;
451 472
452 cmd.type = command::destroy; 473 cmd.type = command::destroy;
453 cmd.id = id; 474 cmd.id = id;
454 475
455 write (sock_fd, &cmd, sizeof (cmd)); 476 write (sock_fd, &cmd, sizeof (cmd));
477
478 GIVE_TOKEN;
456 } 479 }
457} 480}
458 481
459static 482static
460void serve () 483void serve ()
461{ 484{
462 command cmd; 485 command cmd;
463 vector<ptytty *> ptys; 486 vector<ptytty *> ptys;
464 487
488 for (;;)
489 {
490 GIVE_TOKEN;
491
465 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 492 if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command))
466 { 493 break;
494
467 if (cmd.type == command::get) 495 if (cmd.type == command::get)
468 { 496 {
469 // -> id ptyfd ttyfd 497 // -> id ptyfd ttyfd
470 cmd.id = new ptytty_unix; 498 cmd.id = new ptytty_unix;
471 499
504 ptys.erase (pty); 532 ptys.erase (pty);
505 } 533 }
506 } 534 }
507 else 535 else
508 break; 536 break;
537
538 NEED_TOKEN;
509 } 539 }
510 540
511 // destroy all ptys 541 // destroy all ptys
512 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); ) 542 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
513 delete *i; 543 delete *i;
514} 544}
515 545
516void 546void
517ptytty::use_helper () 547ptytty::use_helper ()
518{ 548{
549#ifndef PTYTTY_NO_PID_CHECK
519 int pid = getpid (); 550 int pid = getpid ();
551#endif
520 552
521 if (sock_fd >= 0 && pid == owner_pid) 553 if (sock_fd >= 0
554#ifndef PTYTTY_NO_PID_CHECK
555 && pid == owner_pid
556#endif
557 )
522 return; 558 return;
523 559
560#ifndef PTYTTY_NO_PID_CHECK
524 owner_pid = pid; 561 owner_pid = pid;
562#endif
525 563
526 int sv[2]; 564 int sv[2];
527 565
528 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 566 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
529 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); 567 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
568
569#ifdef PTYTTY_REENTRANT
570 int lv[2];
571
572 if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv))
573 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
574#endif
530 575
531 helper_pid = fork (); 576 helper_pid = fork ();
532 577
533 if (helper_pid < 0) 578 if (helper_pid < 0)
534 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 579 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
537 { 582 {
538 // client, process 583 // client, process
539 sock_fd = sv[0]; 584 sock_fd = sv[0];
540 close (sv[1]); 585 close (sv[1]);
541 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 586 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
587#ifdef PTYTTY_REENTRANT
588 lock_fd = lv[0];
589 close (lv[1]);
590 fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
591#endif
542 } 592 }
543 else 593 else
544 { 594 {
545 // server, pty-helper 595 // server, pty-helper
546 sock_fd = sv[1]; 596 sock_fd = sv[1];
597#ifdef PTYTTY_REENTRANT
598 lock_fd = lv[1];
599#endif
547 600
548 chdir ("/"); 601 chdir ("/");
549 602
603 signal (SIGHUP, SIG_IGN);
604 signal (SIGTERM, SIG_IGN);
605 signal (SIGINT, SIG_IGN);
606 signal (SIGPIPE, SIG_IGN);
607
550 for (int fd = 0; fd < 1023; fd++) 608 for (int fd = 0; fd < 1023; fd++)
551 if (fd != sock_fd) 609 if (fd != sock_fd && fd != lock_fd)
552 close (fd); 610 close (fd);
553 611
554 serve (); 612 serve ();
555 _exit (EXIT_SUCCESS); 613 _exit (EXIT_SUCCESS);
556 } 614 }
560 618
561ptytty * 619ptytty *
562ptytty::create () 620ptytty::create ()
563{ 621{
564#if PTYTTY_HELPER 622#if PTYTTY_HELPER
623 if (helper_pid
624# ifndef PTYTTY_NO_PID_CHECK
565 if (helper_pid && getpid () == owner_pid) 625 && getpid () == owner_pid
626# endif
627 )
566 // use helper process 628 // use helper process
567 return new ptytty_proxy; 629 return new ptytty_proxy;
568 else 630 else
569#endif 631#endif
570 return new ptytty_unix; 632 return new ptytty_unix;
615} 677}
616 678
617///////////////////////////////////////////////////////////////////////////// 679/////////////////////////////////////////////////////////////////////////////
618// C API 680// C API
619 681
620#ifndef NO_C_API 682#ifndef PTYTTY_NO_C_API
683
684typedef void *PTYTTY;
621 685
622#define DEFINE_METHOD(retval, name, args1, args2) \ 686#define DEFINE_METHOD(retval, name, args1, args2) \
623extern "C" retval ptytty_ ## name args1 \ 687extern "C" retval ptytty_ ## name args1 \
624{ return ((struct ptytty *)ptytty)->name args2; } 688{ return ((struct ptytty *)ptytty)->name args2; }
625 689
626DEFINE_METHOD(int,pty,(void *ptytty),) 690DEFINE_METHOD(int,pty,(PTYTTY ptytty),)
627DEFINE_METHOD(int,tty,(void *ptytty),) 691DEFINE_METHOD(int,tty,(PTYTTY ptytty),)
628DEFINE_METHOD(int,get,(void *ptytty),()) 692DEFINE_METHOD(int,get,(PTYTTY ptytty),())
629DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname)) 693DEFINE_METHOD(void,login,(PTYTTY ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
630 694
631DEFINE_METHOD(void,close_tty,(void *ptytty),()) 695DEFINE_METHOD(void,close_tty,(PTYTTY ptytty),())
632DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),()) 696DEFINE_METHOD(int,make_controlling_tty,(PTYTTY ptytty),())
633DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on)) 697DEFINE_METHOD(void,set_utf8_mode,(PTYTTY ptytty, int on),(on))
634 698
635#define DEFINE_STATIC(retval, name, args) \ 699#define DEFINE_STATIC(retval, name, args) \
636extern "C" retval ptytty_ ## name args \ 700extern "C" retval ptytty_ ## name args \
637{ return ptytty::name args; } 701{ return ptytty::name args; }
638 702
639DEFINE_STATIC(void,init,())
640DEFINE_STATIC(void *,create,())
641
642void ptytty_delete (void *ptytty)
643{
644 delete (struct ptytty *)ptytty;
645}
646
647DEFINE_STATIC(void,drop_privileges,()) 703DEFINE_STATIC(void,drop_privileges,())
648DEFINE_STATIC(void,use_helper,()) 704DEFINE_STATIC(void,use_helper,())
705DEFINE_STATIC(void,init,())
649 706
707DEFINE_STATIC(PTYTTY ,create,())
708
709void ptytty_delete (PTYTTY ptytty)
710{
711 delete (struct ptytty *)ptytty;
712}
713
714// send_fd, recv_fd not exposed
715
650#endif 716#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines