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.17 by root, Mon Jan 23 11:22:52 2006 UTC vs.
Revision 1.20 by root, Mon Jan 23 11:45:44 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;
525 555
526 int sv[2]; 556 int sv[2];
527 557
528 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 558 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
529 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); 559 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
560
561#ifdef PTYTTY_REENTRANT
562 int lv[2];
563
564 if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv))
565 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
566#endif
530 567
531 helper_pid = fork (); 568 helper_pid = fork ();
532 569
533 if (helper_pid < 0) 570 if (helper_pid < 0)
534 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 571 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
537 { 574 {
538 // client, process 575 // client, process
539 sock_fd = sv[0]; 576 sock_fd = sv[0];
540 close (sv[1]); 577 close (sv[1]);
541 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 578 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
579#ifdef PTYTTY_REENTRANT
580 lock_fd = lv[0];
581 close (lv[1]);
582 fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
583#endif
542 } 584 }
543 else 585 else
544 { 586 {
545 // server, pty-helper 587 // server, pty-helper
546 sock_fd = sv[1]; 588 sock_fd = sv[1];
589#ifdef PTYTTY_REENTRANT
590 lock_fd = lv[1];
591#endif
547 592
548 chdir ("/"); 593 chdir ("/");
549 594
595 signal (SIGHUP, SIG_IGN);
596 signal (SIGTERM, SIG_IGN);
597 signal (SIGINT, SIG_IGN);
598 signal (SIGPIPE, SIG_IGN);
599
550 for (int fd = 0; fd < 1023; fd++) 600 for (int fd = 0; fd < 1023; fd++)
551 if (fd != sock_fd) 601 if (fd != sock_fd && fd != lock_fd)
552 close (fd); 602 close (fd);
553 603
554 serve (); 604 serve ();
555 _exit (EXIT_SUCCESS); 605 _exit (EXIT_SUCCESS);
556 } 606 }
631DEFINE_METHOD(void,close_tty,(void *ptytty),()) 681DEFINE_METHOD(void,close_tty,(void *ptytty),())
632DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),()) 682DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),())
633DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on)) 683DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on))
634 684
635#define DEFINE_STATIC(retval, name, args) \ 685#define DEFINE_STATIC(retval, name, args) \
636 retval ptytty_ ## name args \ 686extern "C" retval ptytty_ ## name args \
637{ return ptytty::name args; } 687{ return ptytty::name args; }
638 688
639DEFINE_STATIC(void,drop_privileges,()) 689DEFINE_STATIC(void,drop_privileges,())
640DEFINE_STATIC(void,use_helper,()) 690DEFINE_STATIC(void,use_helper,())
641DEFINE_STATIC(void,init,()) 691DEFINE_STATIC(void,init,())

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines