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.11 by ayin, Sun Jan 22 10:19:44 2006 UTC vs.
Revision 1.19 by root, Mon Jan 23 11:36:01 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 * Copyright (c) 2006 Emanuele Giaquinta <e.giaquinta@glauco.it>
12 * 12 *
13 * 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
14 * 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
15 * the Free Software Foundation; either version 2 of the License, or 15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version. 16 * (at your option) any later version.
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>
371 } 372 }
372 373
373 return true; 374 return true;
374} 375}
375 376
377/////////////////////////////////////////////////////////////////////////////
378// helper/proxy support
379
376#if PTYTTY_HELPER 380#if PTYTTY_HELPER
377 381
378static int sock_fd = -1; 382static int sock_fd = -1, lock_fd = -1;
379static int helper_pid, owner_pid; 383static int helper_pid, owner_pid;
380 384
381struct command 385struct command
382{ 386{
383 enum { get, login, destroy } type; 387 enum { get, login, destroy } type;
482 } 486 }
483 } 487 }
484 else if (cmd.type == command::login) 488 else if (cmd.type == command::login)
485 { 489 {
486#if UTMP_SUPPORT 490#if UTMP_SUPPORT
487 if (find (ptys.begin (), ptys.end (), cmd.id)) 491 if (find (ptys.begin (), ptys.end (), cmd.id) != ptys.end ())
488 { 492 {
489 cmd.hostname[sizeof (cmd.hostname) - 1] = 0; 493 cmd.hostname[sizeof (cmd.hostname) - 1] = 0;
490 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname); 494 cmd.id->login (cmd.cmd_pid, cmd.login_shell, cmd.hostname);
491 } 495 }
492#endif 496#endif
523 int sv[2]; 527 int sv[2];
524 528
525 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv)) 529 if (socketpair (AF_UNIX, SOCK_STREAM, 0, sv))
526 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n"); 530 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
527 531
532#ifdef PTYTTY_REENTRANT
533 int lv[2];
534
535 if (socketpair (AF_UNIX, SOCK_STREAM, 0, lv))
536 ptytty_fatal ("could not create socket to communicate with pty/sessiondb helper, aborting.\n");
537#endif
538
528 helper_pid = fork (); 539 helper_pid = fork ();
529 540
530 if (helper_pid < 0) 541 if (helper_pid < 0)
531 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n"); 542 ptytty_fatal ("could not create pty/sessiondb helper process, aborting.\n");
532 543
534 { 545 {
535 // client, process 546 // client, process
536 sock_fd = sv[0]; 547 sock_fd = sv[0];
537 close (sv[1]); 548 close (sv[1]);
538 fcntl (sock_fd, F_SETFD, FD_CLOEXEC); 549 fcntl (sock_fd, F_SETFD, FD_CLOEXEC);
550#ifdef PTYTTY_REENTRANT
551 lock_fd = lv[0];
552 close (lv[1]);
553 fcntl (lock_fd, F_SETFD, FD_CLOEXEC);
554#endif
539 } 555 }
540 else 556 else
541 { 557 {
542 // server, pty-helper 558 // server, pty-helper
543 sock_fd = sv[1]; 559 sock_fd = sv[1];
560#ifdef PTYTTY_REENTRANT
561 lock_fd = lv[1];
562#endif
544 563
545 chdir ("/"); 564 chdir ("/");
546 565
566 signal (SIGHUP, SIG_IGN);
567 signal (SIGTERM, SIG_IGN);
568 signal (SIGINT, SIG_IGN);
569 signal (SIGPIPE, SIG_IGN);
570
547 for (int fd = 0; fd < 1023; fd++) 571 for (int fd = 0; fd < 1023; fd++)
548 if (fd != sock_fd) 572 if (fd != sock_fd && fd != lock_fd)
549 close (fd); 573 close (fd);
550 574
551 serve (); 575 serve ();
552 _exit (EXIT_SUCCESS); 576 _exit (EXIT_SUCCESS);
553 } 577 }
609 if (uid != geteuid () 633 if (uid != geteuid ()
610 || gid != getegid ()) 634 || gid != getegid ())
611 ptytty_fatal ("unable to drop privileges, aborting.\n"); 635 ptytty_fatal ("unable to drop privileges, aborting.\n");
612} 636}
613 637
638/////////////////////////////////////////////////////////////////////////////
639// C API
640
641#ifndef NO_C_API
642
643#define DEFINE_METHOD(retval, name, args1, args2) \
644extern "C" retval ptytty_ ## name args1 \
645{ return ((struct ptytty *)ptytty)->name args2; }
646
647DEFINE_METHOD(int,pty,(void *ptytty),)
648DEFINE_METHOD(int,tty,(void *ptytty),)
649DEFINE_METHOD(int,get,(void *ptytty),())
650DEFINE_METHOD(void,login,(void *ptytty, int cmd_pid, bool login_shell, const char *hostname),(cmd_pid,login_shell,hostname))
651
652DEFINE_METHOD(void,close_tty,(void *ptytty),())
653DEFINE_METHOD(int,make_controlling_tty,(void *ptytty),())
654DEFINE_METHOD(void,set_utf8_mode,(void *ptytty, int on),(on))
655
656#define DEFINE_STATIC(retval, name, args) \
657extern "C" retval ptytty_ ## name args \
658{ return ptytty::name args; }
659
660DEFINE_STATIC(void,drop_privileges,())
661DEFINE_STATIC(void,use_helper,())
662DEFINE_STATIC(void,init,())
663
664DEFINE_STATIC(void *,create,())
665
666void ptytty_delete (void *ptytty)
667{
668 delete (struct ptytty *)ptytty;
669}
670
671// send_fd, recv_fd not exposed
672
673#endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines