ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/main.C
(Generate patch)

Comparing rxvt-unicode/src/main.C (file contents):
Revision 1.182 by root, Tue Jan 17 10:14:26 2006 UTC vs.
Revision 1.192 by root, Fri Jan 20 12:16:28 2006 UTC

46 46
47#ifdef HAVE_TERMIOS_H 47#ifdef HAVE_TERMIOS_H
48# include <termios.h> 48# include <termios.h>
49#endif 49#endif
50 50
51#if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__) 51vector<rxvt_term *> rxvt_term::termlist;
52static uid_t saved_euid; 52
53static gid_t saved_egid; 53// used to tell global functions which terminal instance is "active"
54#endif 54rxvt_t rxvt_current_term;
55
56static char curlocale[128], savelocale[128];
55 57
56bool 58bool
57rxvt_tainted ()
58{
59#if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__)
60 return getuid () != saved_euid || getgid () != saved_egid;
61#else
62 return false;
63#endif
64}
65
66vector<rxvt_term *> rxvt_term::termlist;
67
68static char curlocale[128], savelocale[128];
69
70bool
71rxvt_set_locale (const char *locale) 59rxvt_set_locale (const char *locale) NOTHROW
72{ 60{
73 if (!locale || !strncmp (locale, curlocale, 128)) 61 if (!locale || !strncmp (locale, curlocale, 128))
74 return false; 62 return false;
75 63
76 strncpy (curlocale, locale, 128); 64 strncpy (curlocale, locale, 128);
77 setlocale (LC_CTYPE, curlocale); 65 setlocale (LC_CTYPE, curlocale);
78 return true; 66 return true;
79} 67}
80 68
81void 69void
82rxvt_push_locale (const char *locale) 70rxvt_push_locale (const char *locale) NOTHROW
83{ 71{
84 strcpy (savelocale, curlocale); 72 strcpy (savelocale, curlocale);
85 rxvt_set_locale (locale); 73 rxvt_set_locale (locale);
86} 74}
87 75
88void 76void
89rxvt_pop_locale () 77rxvt_pop_locale () NOTHROW
90{ 78{
91 rxvt_set_locale (savelocale); 79 rxvt_set_locale (savelocale);
92} 80}
93 81
94#if ENABLE_COMBINING 82#if ENABLE_COMBINING
184#ifdef USE_XIM 172#ifdef USE_XIM
185 im_ev (this, &rxvt_term::im_cb), 173 im_ev (this, &rxvt_term::im_cb),
186#endif 174#endif
187 termwin_ev (this, &rxvt_term::x_cb), 175 termwin_ev (this, &rxvt_term::x_cb),
188 vt_ev (this, &rxvt_term::x_cb), 176 vt_ev (this, &rxvt_term::x_cb),
177 child_ev (this, &rxvt_term::child_cb),
189 check_ev (this, &rxvt_term::check_cb), 178 check_ev (this, &rxvt_term::check_cb),
190 flush_ev (this, &rxvt_term::flush_cb), 179 flush_ev (this, &rxvt_term::flush_cb),
191 destroy_ev (this, &rxvt_term::destroy_cb), 180 destroy_ev (this, &rxvt_term::destroy_cb),
192 pty_ev (this, &rxvt_term::pty_cb), 181 pty_ev (this, &rxvt_term::pty_cb),
193 incr_ev (this, &rxvt_term::incr_cb) 182 incr_ev (this, &rxvt_term::incr_cb)
196 185
197 termlist.push_back (this); 186 termlist.push_back (this);
198 187
199#ifdef KEYSYM_RESOURCE 188#ifdef KEYSYM_RESOURCE
200 keyboard = new keyboard_manager; 189 keyboard = new keyboard_manager;
201
202 if (!keyboard)
203 rxvt_fatal ("out of memory, aborting.\n");
204#endif 190#endif
205} 191}
206 192
207// clean up the most important stuff, do *not* call x or free mem etc. 193// clean up the most important stuff, do *not* call x or free mem etc.
208// for use before an emergency exit 194// for use before an emergency exit
209void rxvt_term::emergency_cleanup () 195void rxvt_term::emergency_cleanup ()
210{ 196{
211 if (cmd_pid) 197 if (cmd_pid)
212 kill (-cmd_pid, SIGHUP); 198 kill (-cmd_pid, SIGHUP);
213 199
214#ifdef UTMP_SUPPORT 200 delete pty; pty = 0;
215 privileged_utmp (RESTORE);
216#endif
217
218 pty.put ();
219} 201}
220 202
221rxvt_term::~rxvt_term () 203rxvt_term::~rxvt_term ()
222{ 204{
223 HOOK_INVOKE ((this, HOOK_DESTROY, DT_END));
224
225 termlist.erase (find (termlist.begin (), termlist.end(), this)); 205 termlist.erase (find (termlist.begin (), termlist.end(), this));
226 206
227 emergency_cleanup (); 207 emergency_cleanup ();
228 208
229#if ENABLE_STYLES 209#if ENABLE_STYLES
304#ifdef KEYSYM_RESOURCE 284#ifdef KEYSYM_RESOURCE
305 delete keyboard; 285 delete keyboard;
306#endif 286#endif
307} 287}
308 288
289// child has exited, usually destroys
309void 290void
310rxvt_term::child_exit () 291rxvt_term::child_cb (child_watcher &w, int status)
311{ 292{
293 HOOK_INVOKE ((this, HOOK_CHILD_EXIT, DT_INT, status, DT_END));
294
312 cmd_pid = 0; 295 cmd_pid = 0;
313 296
314 if (!OPTION (Opt_hold)) 297 if (!OPTION (Opt_hold))
315 destroy (); 298 destroy ();
316} 299}
317 300
318void 301void
319rxvt_term::destroy () 302rxvt_term::destroy ()
320{ 303{
321 if (destroy_ev.active) 304 if (destroy_ev.active)
305 return;
306
307 if (HOOK_INVOKE ((this, HOOK_DESTROY, DT_END)))
322 return; 308 return;
323 309
324#if ENABLE_OVERLAY 310#if ENABLE_OVERLAY
325 scr_overlay_off (); 311 scr_overlay_off ();
326#endif 312#endif
483#ifdef HAVE_SCROLLBARS 469#ifdef HAVE_SCROLLBARS
484 if (OPTION (Opt_scrollBar)) 470 if (OPTION (Opt_scrollBar))
485 scrollBar.setIdle (); /* set existence for size calculations */ 471 scrollBar.setIdle (); /* set existence for size calculations */
486#endif 472#endif
487 473
488#if ENABLE_PERL 474 pty = rxvt_new_ptytty ();
489 if (!rs[Rs_perl_ext_1])
490 rs[Rs_perl_ext_1] = "default";
491
492 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1])
493 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2])
494 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
495 {
496#if (defined(HAVE_SETEUID) || defined(HAVE_SETREUID)) && !defined(__CYGWIN32__)
497 // ignore some perl-related arguments if some bozo installed us set[ug]id
498 if (rxvt_tainted ())
499 {
500 if ((rs[Rs_perl_lib] && *rs[Rs_perl_lib])
501 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
502 {
503 rxvt_warn ("running with elevated privileges: ignoring perl-lib and perl-eval.\n");
504 rs[Rs_perl_lib] = 0;
505 rs[Rs_perl_eval] = 0;
506 }
507 }
508#endif
509 rxvt_perl.init (this);
510 HOOK_INVOKE ((this, HOOK_INIT, DT_END));
511 }
512#endif
513 475
514 create_windows (argc, argv); 476 create_windows (argc, argv);
515 477
516 dDisp; 478 dDisp;
517 479
534 check_our_parents (); 496 check_our_parents ();
535 rootwin_ev.start (display, display->root); 497 rootwin_ev.start (display, display->root);
536 } 498 }
537#endif 499#endif
538 500
501 set_colorfgbg ();
502
503 init_command (cmd_argv);
504
505 free (cmd_argv);
506
507 if (pty->pty >= 0)
508 pty_ev.start (pty->pty, EVENT_READ);
509
510 check_ev.start ();
511
512 HOOK_INVOKE ((this, HOOK_START, DT_END));
513
539 XMapWindow (disp, vt); 514 XMapWindow (disp, vt);
540 XMapWindow (disp, parent[0]); 515 XMapWindow (disp, parent[0]);
541 516
542 set_colorfgbg ();
543
544 init_command (cmd_argv);
545
546 free (cmd_argv);
547
548 if (pty.pty >= 0)
549 pty_ev.start (pty.pty, EVENT_READ);
550
551 check_ev.start ();
552
553 HOOK_INVOKE ((this, HOOK_START, DT_END));
554
555 return true; 517 return true;
556} 518}
557 519
558static struct sig_handlers 520static struct sig_handlers
559{ 521{
560 sig_watcher sw_chld, sw_term, sw_int; 522 sig_watcher sw_term, sw_int;
561 523
562 void sig_chld (sig_watcher &w)
563 {
564 // we are being called for every SIGCHLD, find the corresponding term
565 int pid;
566
567 while ((pid = waitpid (-1, NULL, WNOHANG)) > 0)
568 for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
569 if (pid == (*t)->cmd_pid)
570 {
571 (*t)->child_exit ();
572 break;
573 }
574 }
575
576 /* 524 /*
577 * Catch a fatal signal and tidy up before quitting 525 * Catch a fatal signal and tidy up before quitting
578 */ 526 */
579 void 527 void
580 sig_term (sig_watcher &w) 528 sig_term (sig_watcher &w)
586 signal (w.signum, SIG_DFL); 534 signal (w.signum, SIG_DFL);
587 kill (getpid (), w.signum); 535 kill (getpid (), w.signum);
588 } 536 }
589 537
590 sig_handlers () 538 sig_handlers ()
591 : sw_chld (this, &sig_handlers::sig_chld),
592 sw_term (this, &sig_handlers::sig_term), 539 : sw_term (this, &sig_handlers::sig_term),
593 sw_int (this, &sig_handlers::sig_term) 540 sw_int (this, &sig_handlers::sig_term)
594 { 541 {
595 } 542 }
596} sig_handlers; 543} sig_handlers;
597 544
598char **rxvt_environ; // startup environment 545char **rxvt_environ; // startup environment
599 546
600void 547void
601rxvt_init () 548rxvt_init ()
602{ 549{
550 uid_t uid = getuid ();
551 gid_t gid = getgid ();
552
553 // before doing anything else, check for setuid/setgid operation,
554 // start the helper process and drop privileges
555 if (uid != geteuid ()
556 || gid != getegid ())
557 {
558#if PTYTTY_HELPER
559 rxvt_ptytty_server ();
560#else
561 rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
562#endif
563
564 // drop privileges
565#if HAVE_SETRESUID
566 setresgid (gid, gid, gid);
567 setresuid (uid, uid, uid);
568#elif HAVE_SETREUID
569 setregid (gid, gid);
570 setreuid (uid, uid);
571#elif HAVE_SETUID
572 setgid (gid);
573 setuid (uid);
574#endif
575
576 if (uid != geteuid ()
577 || gid != getegid ())
578 rxvt_fatal ("unable to drop privileges, aborting.\n");
579 }
580
603 rxvt_environ = environ; 581 rxvt_environ = environ;
604
605 /*
606 * Save and then give up any super-user privileges
607 * If we need privileges in any area then we must specifically request it.
608 * We should only need to be root in these cases:
609 * 1. write utmp entries on some systems
610 * 2. chown tty on some systems
611 */
612 rxvt_privileges (SAVE);
613 rxvt_privileges (IGNORE);
614 582
615 signal (SIGHUP, SIG_IGN); 583 signal (SIGHUP, SIG_IGN);
616 signal (SIGPIPE, SIG_IGN); 584 signal (SIGPIPE, SIG_IGN);
617 585
618 sig_handlers.sw_chld.start (SIGCHLD);
619 sig_handlers.sw_term.start (SIGTERM); 586 sig_handlers.sw_term.start (SIGTERM);
620 sig_handlers.sw_int.start (SIGINT); 587 sig_handlers.sw_int.start (SIGINT);
621 588
622 /* need to trap SIGURG for SVR4 (Unixware) rlogin */ 589 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
623 /* signal (SIGURG, SIG_DFL); */ 590 /* signal (SIGURG, SIG_DFL); */
652 rxvt_fatal ("memory allocation failure. aborting.\n"); 619 rxvt_fatal ("memory allocation failure. aborting.\n");
653 620
654 return p; 621 return p;
655} 622}
656 623
657void * 624void *
658rxvt_realloc (void *ptr, size_t size) 625rxvt_realloc (void *ptr, size_t size)
659{ 626{
660 void *p = realloc (ptr, size); 627 void *p = realloc (ptr, size);
661 628
662 if (!p) 629 if (!p)
663 rxvt_fatal ("memory allocation failure. aborting.\n"); 630 rxvt_fatal ("memory allocation failure. aborting.\n");
664 631
665 return p; 632 return p;
666} 633}
667
668/* ------------------------------------------------------------------------- *
669 * PRIVILEGED OPERATIONS *
670 * ------------------------------------------------------------------------- */
671/* take care of suid/sgid super-user (root) privileges */
672void
673rxvt_privileges (rxvt_privaction action)
674{
675#if ! defined(__CYGWIN32__)
676# if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
677 /* setreuid () is the poor man's setuid (), seteuid () */
678# define seteuid(a) setreuid(-1, (a))
679# define setegid(a) setregid(-1, (a))
680# define HAVE_SETEUID
681# endif
682# ifdef HAVE_SETEUID
683 switch (action)
684 {
685 case IGNORE:
686 /*
687 * change effective uid/gid - not real uid/gid - so we can switch
688 * back to root later, as required
689 */
690 setegid (getgid ());
691 seteuid (getuid ());
692 break;
693 case SAVE:
694 saved_egid = getegid ();
695 saved_euid = geteuid ();
696 break;
697 case RESTORE:
698 setegid (saved_egid);
699 seteuid (saved_euid);
700 break;
701 }
702# else
703 switch (action)
704 {
705 case IGNORE:
706 setgid (getgid ());
707 setuid (getuid ());
708 /* FALLTHROUGH */
709 case SAVE:
710 /* FALLTHROUGH */
711 case RESTORE:
712 break;
713 }
714# endif
715#endif
716}
717
718#ifdef UTMP_SUPPORT
719void
720rxvt_term::privileged_utmp (rxvt_privaction action)
721{
722 if (OPTION (Opt_utmpInhibit)
723 || !pty.name || !*pty.name)
724 return;
725
726 rxvt_privileges (RESTORE);
727
728 if (action == SAVE)
729 pty.login (cmd_pid, OPTION (Opt_loginShell), rs[Rs_display_name]);
730 else
731 pty.logout ();
732
733 rxvt_privileges (IGNORE);
734}
735#endif
736 634
737/*----------------------------------------------------------------------*/ 635/*----------------------------------------------------------------------*/
738/* 636/*
739 * window size/position calculcations for XSizeHint and other storage. 637 * window size/position calculcations for XSizeHint and other storage.
740 * if width/height are non-zero then override calculated width/height 638 * if width/height are non-zero then override calculated width/height
874 * Called after a window size change. 772 * Called after a window size change.
875 */ 773 */
876void 774void
877rxvt_term::tt_winch () 775rxvt_term::tt_winch ()
878{ 776{
879 if (pty.pty < 0) 777 if (pty->pty < 0)
880 return; 778 return;
881 779
882 struct winsize ws; 780 struct winsize ws;
883 781
884 ws.ws_col = ncol; 782 ws.ws_col = ncol;
885 ws.ws_row = nrow; 783 ws.ws_row = nrow;
886 ws.ws_xpixel = width; 784 ws.ws_xpixel = width;
887 ws.ws_ypixel = height; 785 ws.ws_ypixel = height;
888 (void)ioctl (pty.pty, TIOCSWINSZ, &ws); 786 (void)ioctl (pty->pty, TIOCSWINSZ, &ws);
889 787
890#if 0 788#if 0
891 // TIOCSWINSZ⎈ is supposed to do this automatically and correctly 789 // TIOCSWINSZ⎈ is supposed to do this automatically and correctly
892 if (cmd_pid) /* force through to the command */ 790 if (cmd_pid) /* force through to the command */
893 kill (-cmd_pid, SIGWINCH); 791 kill (-cmd_pid, SIGWINCH);
1679 XFree (preedit_attr); 1577 XFree (preedit_attr);
1680 XFree (status_attr); 1578 XFree (status_attr);
1681} 1579}
1682#endif /* USE_XIM */ 1580#endif /* USE_XIM */
1683 1581
1684/*----------------------------------------------------------------------*/
1685rxvt_t rxvt_current_term;
1686
1687/*----------------------- end-of-file (C source) -----------------------*/ 1582/*----------------------- end-of-file (C source) -----------------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines