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.56 by root, Sun Jan 22 04:01:52 2006 UTC vs.
Revision 1.59 by root, Mon Jan 23 12:05:12 2006 UTC

6 *----------------------------------------------------------------------* 6 *----------------------------------------------------------------------*
7 * 7 *
8 * All portions of code are copyright by their respective author/s. 8 * All portions of code are copyright by their respective author/s.
9 * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com> 9 * Copyright (c) 1999-2001 Geoff Wing <gcw@pobox.com>
10 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com> 10 * Copyright (c) 2004-2006 Marc Lehmann <pcg@goof.com>
11 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
11 * 12 *
12 * This program is free software; you can redistribute it and/or modify 13 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by 14 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or 15 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version. 16 * (at your option) any later version.
28 29
29#include "ptytty.h" 30#include "ptytty.h"
30 31
31#include <cstdlib> 32#include <cstdlib>
32#include <cstring> 33#include <cstring>
34#include <csignal>
33 35
34#include <sys/types.h> 36#include <sys/types.h>
35#include <sys/socket.h> 37#include <sys/socket.h>
36#include <unistd.h> 38#include <unistd.h>
37#include <fcntl.h> 39#include <fcntl.h>
370 } 372 }
371 373
372 return true; 374 return true;
373} 375}
374 376
377/////////////////////////////////////////////////////////////////////////////
378// helper/proxy support
379
375#if PTYTTY_HELPER 380#if PTYTTY_HELPER
376 381
377static int sock_fd = -1; 382static int sock_fd = -1, lock_fd = -1;
378static int helper_pid, owner_pid; 383static int helper_pid, owner_pid;
379 384
380struct command 385struct command
381{ 386{
382 enum { get, login, destroy } type; 387 enum { get, login, destroy } type;
401 406
402 bool get (); 407 bool get ();
403 void login (int cmd_pid, bool login_shell, const char *hostname); 408 void login (int cmd_pid, bool login_shell, const char *hostname);
404}; 409};
405 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
406bool 419bool
407ptytty_proxy::get () 420ptytty_proxy::get ()
408{ 421{
422 NEED_TOKEN;
423
409 command cmd; 424 command cmd;
410 425
411 cmd.type = command::get; 426 cmd.type = command::get;
412 427
413 write (sock_fd, &cmd, sizeof (cmd)); 428 write (sock_fd, &cmd, sizeof (cmd));
414 429
415 if (read (sock_fd, &id, sizeof (id)) != sizeof (id)) 430 if (read (sock_fd, &id, sizeof (id)) != sizeof (id))
416 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");
417 432
418 if (!id) 433 if (!id)
434 {
435 GIVE_TOKEN;
419 return false; 436 return false;
437 }
420 438
421 if ((pty = recv_fd (sock_fd)) < 0 439 if ((pty = recv_fd (sock_fd)) < 0
422 || (tty = recv_fd (sock_fd)) < 0) 440 || (tty = recv_fd (sock_fd)) < 0)
423 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");
424 442
443 GIVE_TOKEN;
425 return true; 444 return true;
426} 445}
427 446
428void 447void
429ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname) 448ptytty_proxy::login (int cmd_pid, bool login_shell, const char *hostname)
430{ 449{
450 NEED_TOKEN;
451
431 command cmd; 452 command cmd;
432 453
433 cmd.type = command::login; 454 cmd.type = command::login;
434 cmd.id = id; 455 cmd.id = id;
435 cmd.cmd_pid = cmd_pid; 456 cmd.cmd_pid = cmd_pid;
436 cmd.login_shell = login_shell; 457 cmd.login_shell = login_shell;
437 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname)); 458 strncpy (cmd.hostname, hostname, sizeof (cmd.hostname));
438 459
439 write (sock_fd, &cmd, sizeof (cmd)); 460 write (sock_fd, &cmd, sizeof (cmd));
461
462 GIVE_TOKEN;
440} 463}
441 464
442ptytty_proxy::~ptytty_proxy () 465ptytty_proxy::~ptytty_proxy ()
443{ 466{
444 if (id) 467 if (id)
445 { 468 {
469 NEED_TOKEN;
470
446 command cmd; 471 command cmd;
447 472
448 cmd.type = command::destroy; 473 cmd.type = command::destroy;
449 cmd.id = id; 474 cmd.id = id;
450 475
451 write (sock_fd, &cmd, sizeof (cmd)); 476 write (sock_fd, &cmd, sizeof (cmd));
477
478 GIVE_TOKEN;
452 } 479 }
453} 480}
454 481
455static 482static
456void serve () 483void serve ()
457{ 484{
458 command cmd; 485 command cmd;
459 vector<ptytty *> ptys; 486 vector<ptytty *> ptys;
460 487
488 for (;;)
489 {
490 GIVE_TOKEN;
491
461 while (read (sock_fd, &cmd, sizeof (command)) == sizeof (command)) 492 if (read (sock_fd, &cmd, sizeof (command)) != sizeof (command))
462 { 493 break;
494
463 if (cmd.type == command::get) 495 if (cmd.type == command::get)
464 { 496 {
465 // -> id ptyfd ttyfd 497 // -> id ptyfd ttyfd
466 cmd.id = new ptytty_unix; 498 cmd.id = new ptytty_unix;
467 499
481 } 513 }
482 } 514 }
483 else if (cmd.type == command::login) 515 else if (cmd.type == command::login)
484 { 516 {
485#if UTMP_SUPPORT 517#if UTMP_SUPPORT
486 if (find (ptys.begin (), ptys.end (), cmd.id)) 518 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
487 { 519 {
488 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 520 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
489 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 521 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
490 } 522 }
491#endif 523#endif
500 ptys.erase (pty); 532 ptys.erase (pty);
501 } 533 }
502 } 534 }
503 else 535 else
504 break; 536 break;
537
538 NEED_TOKEN;
505 } 539 }
506 540
507 // destroy all ptys 541 // destroy all ptys
508 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); ) 542 for (vector<ptytty *>::iterator i = ptys.end (); i-- > ptys.begin (); )
509 delete *i; 543 delete *i;
510} 544}
511 545
512void 546void
513ptytty::use_helper () 547ptytty::use_helper ()
514{ 548{
549#ifndef PTYTTY_NO_PID_CHECK
515 int pid = getpid (); 550 int pid = getpid ();
551#endif
516 552
517 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 )
518 return; 558 return;
519 559
560#ifndef PTYTTY_NO_PID_CHECK
520 owner_pid = pid; 561 owner_pid = pid;
562#endif
521 563
522 int sv[2]; 564 int sv[2];
523 565
524 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 566 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
525 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
526 575
527 helper_pid = fork (); 576 helper_pid = fork ();
528 577
529 if (helper_pid < 0) 578 if (helper_pid < 0)
530 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 579 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
533 { 582 {
534 // client, process 583 // client, process
535 sock_fd = sv[0]; 584 sock_fd = sv[0];
536 close (sv[1]); 585 close (sv[1]);
537 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
538 } 592 }
539 else 593 else
540 { 594 {
541 // server, pty-helper 595 // server, pty-helper
542 sock_fd = sv[1]; 596 sock_fd = sv[1];
597#ifdef PTYTTY_REENTRANT
598 lock_fd = lv[1];
599#endif
543 600
544 chdir ("/"); 601 chdir ("/");
545 602
603 signal (SIGHUP, SIG_IGN);
604 signal (SIGTERM, SIG_IGN);
605 signal (SIGINT, SIG_IGN);
606 signal (SIGPIPE, SIG_IGN);
607
546 for (int fd = 0; fd < 1023; fd++) 608 for (int fd = 0; fd < 1023; fd++)
547 if (fd != sock_fd) 609 if (fd != sock_fd && fd != lock_fd)
548 close (fd); 610 close (fd);
549 611
550 serve (); 612 serve ();
551 _exit (EXIT_SUCCESS); 613 _exit (EXIT_SUCCESS);
552 } 614 }
556 618
557ptytty * 619ptytty *
558ptytty::create () 620ptytty::create ()
559{ 621{
560#if PTYTTY_HELPER 622#if PTYTTY_HELPER
623 if (helper_pid
624# ifndef PTYTTY_NO_PID_CHECK
561 if (helper_pid && getpid () == owner_pid) 625 && getpid () == owner_pid
626# endif
627 )
562 // use helper process 628 // use helper process
563 return new ptytty_proxy; 629 return new ptytty_proxy;
564 else 630 else
565#endif 631#endif
566 return new ptytty_unix; 632 return new ptytty_unix;
608 if (uid != geteuid () 674 if (uid != geteuid ()
609 || gid != getegid ()) 675 || gid != getegid ())
610 ptytty_fatal ("unable to drop privileges, aborting.\n"); 676 ptytty_fatal ("unable to drop privileges, aborting.\n");
611} 677}
612 678
679/////////////////////////////////////////////////////////////////////////////
680// C API
681
682#ifndef PTYTTY_NO_C_API
683
684#define DEFINE_METHOD(retval, name, args1, args2) \
685extern "C" retval ptytty_ ## name args1 \
686{ return ((struct ptytty *)ptytty)->name args2; }
687
688DEFINE_METHOD(int,pty,(void *ptytty),)
689DEFINE_METHOD(int,tty,(void *ptytty),)
690DEFINE_METHOD(int,get,(void *ptytty),())
691DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
692
693DEFINE_METHOD(void,close_tty,(void *ptytty),())
694DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),())
695DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on))
696
697#define DEFINE_STATIC(retval, name, args) \
698extern "C" retval ptytty_ ## name args \
699{ return ptytty::name args; }
700
701DEFINE_STATIC(void,drop_privileges,())
702DEFINE_STATIC(void,use_helper,())
703DEFINE_STATIC(void,init,())
704
705DEFINE_STATIC(void *,create,())
706
707void ptytty_delete (void *ptytty)
708{
709 delete (struct ptytty *)ptytty;
710}
711
712// send_fd, recv_fd not exposed
713
714#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines