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.184 by root, Tue Jan 17 12:22:59 2006 UTC vs.
Revision 1.191 by root, Fri Jan 20 10:27:08 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
215 privileged_utmp (RESTORE);
216#endif
217
218 delete pty; pty = 0; 200 delete pty; pty = 0;
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
478 464
479#ifdef KEYSYM_RESOURCE 465#ifdef KEYSYM_RESOURCE
480 keyboard->register_done (); 466 keyboard->register_done ();
481#endif 467#endif
482 468
469#if ENABLE_PERL
470 if (!rs[Rs_perl_ext_1])
471 rs[Rs_perl_ext_1] = "default";
472
473 if ((rs[Rs_perl_ext_1] && *rs[Rs_perl_ext_1])
474 || (rs[Rs_perl_ext_2] && *rs[Rs_perl_ext_2])
475 || (rs[Rs_perl_eval] && *rs[Rs_perl_eval]))
476 {
477 rxvt_perl.init (this);
478 HOOK_INVOKE ((this, HOOK_INIT, DT_END));
479 }
480#endif
481
483#ifdef HAVE_SCROLLBARS 482#ifdef HAVE_SCROLLBARS
484 if (OPTION (Opt_scrollBar)) 483 if (OPTION (Opt_scrollBar))
485 scrollBar.setIdle (); /* set existence for size calculations */ 484 scrollBar.setIdle (); /* set existence for size calculations */
486#endif 485#endif
487 486
488#if ENABLE_PERL
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
514 pty = rxvt_new_ptytty (); 487 pty = rxvt_new_ptytty ();
515 488
516 create_windows (argc, argv); 489 create_windows (argc, argv);
517 490
518 dDisp; 491 dDisp;
536 check_our_parents (); 509 check_our_parents ();
537 rootwin_ev.start (display, display->root); 510 rootwin_ev.start (display, display->root);
538 } 511 }
539#endif 512#endif
540 513
514 set_colorfgbg ();
515
516 init_command (cmd_argv);
517
518 free (cmd_argv);
519
520 if (pty->pty >= 0)
521 pty_ev.start (pty->pty, EVENT_READ);
522
523 check_ev.start ();
524
525 HOOK_INVOKE ((this, HOOK_START, DT_END));
526
541 XMapWindow (disp, vt); 527 XMapWindow (disp, vt);
542 XMapWindow (disp, parent[0]); 528 XMapWindow (disp, parent[0]);
543 529
544 set_colorfgbg ();
545
546 init_command (cmd_argv);
547
548 free (cmd_argv);
549
550 if (pty->pty >= 0)
551 pty_ev.start (pty->pty, EVENT_READ);
552
553 check_ev.start ();
554
555 HOOK_INVOKE ((this, HOOK_START, DT_END));
556
557 return true; 530 return true;
558} 531}
559 532
560static struct sig_handlers 533static struct sig_handlers
561{ 534{
562 sig_watcher sw_chld, sw_term, sw_int; 535 sig_watcher sw_term, sw_int;
563 536
564 void sig_chld (sig_watcher &w)
565 {
566 // we are being called for every SIGCHLD, find the corresponding term
567 int pid;
568
569 while ((pid = waitpid (-1, NULL, WNOHANG)) > 0)
570 for (rxvt_term **t = rxvt_term::termlist.begin (); t < rxvt_term::termlist.end (); t++)
571 if (pid == (*t)->cmd_pid)
572 {
573 (*t)->child_exit ();
574 break;
575 }
576 }
577
578 /* 537 /*
579 * Catch a fatal signal and tidy up before quitting 538 * Catch a fatal signal and tidy up before quitting
580 */ 539 */
581 void 540 void
582 sig_term (sig_watcher &w) 541 sig_term (sig_watcher &w)
588 signal (w.signum, SIG_DFL); 547 signal (w.signum, SIG_DFL);
589 kill (getpid (), w.signum); 548 kill (getpid (), w.signum);
590 } 549 }
591 550
592 sig_handlers () 551 sig_handlers ()
593 : sw_chld (this, &sig_handlers::sig_chld),
594 sw_term (this, &sig_handlers::sig_term), 552 : sw_term (this, &sig_handlers::sig_term),
595 sw_int (this, &sig_handlers::sig_term) 553 sw_int (this, &sig_handlers::sig_term)
596 { 554 {
597 } 555 }
598} sig_handlers; 556} sig_handlers;
599 557
600char **rxvt_environ; // startup environment 558char **rxvt_environ; // startup environment
601 559
602void 560void
603rxvt_init () 561rxvt_init ()
604{ 562{
563 uid_t uid = getuid ();
564 gid_t gid = getgid ();
565
566 // before doing anything else, check for setuid/setgid operation,
567 // start the helper process and drop privileges
568 if (uid != geteuid ()
569 || gid != getegid ())
570 {
571#if PTYTTY_HELPER
572 rxvt_ptytty_server ();
573#else
574 rxvt_warn ("running setuid/setgid without pty helper compiled in, continuing unprivileged.\n");
575#endif
576
577 // drop privileges
578#if HAVE_SETRESUID
579 setresgid (gid, gid, gid);
580 setresuid (uid, uid, uid);
581#elif HAVE_SETREUID
582 setregid (gid, gid);
583 setreuid (uid, uid);
584#elif HAVE_SETUID
585 setgid (gid);
586 setuid (uid);
587#endif
588
589 if (uid != geteuid ()
590 || gid != getegid ())
591 rxvt_fatal ("unable to drop privileges, aborting.\n");
592 }
593
605 rxvt_environ = environ; 594 rxvt_environ = environ;
606
607 /*
608 * Save and then give up any super-user privileges
609 * If we need privileges in any area then we must specifically request it.
610 * We should only need to be root in these cases:
611 * 1. write utmp entries on some systems
612 * 2. chown tty on some systems
613 */
614 rxvt_privileges (SAVE);
615 rxvt_privileges (IGNORE);
616 595
617 signal (SIGHUP, SIG_IGN); 596 signal (SIGHUP, SIG_IGN);
618 signal (SIGPIPE, SIG_IGN); 597 signal (SIGPIPE, SIG_IGN);
619 598
620 sig_handlers.sw_chld.start (SIGCHLD);
621 sig_handlers.sw_term.start (SIGTERM); 599 sig_handlers.sw_term.start (SIGTERM);
622 sig_handlers.sw_int.start (SIGINT); 600 sig_handlers.sw_int.start (SIGINT);
623 601
624 /* need to trap SIGURG for SVR4 (Unixware) rlogin */ 602 /* need to trap SIGURG for SVR4 (Unixware) rlogin */
625 /* signal (SIGURG, SIG_DFL); */ 603 /* signal (SIGURG, SIG_DFL); */
654 rxvt_fatal ("memory allocation failure. aborting.\n"); 632 rxvt_fatal ("memory allocation failure. aborting.\n");
655 633
656 return p; 634 return p;
657} 635}
658 636
659void * 637void *
660rxvt_realloc (void *ptr, size_t size) 638rxvt_realloc (void *ptr, size_t size)
661{ 639{
662 void *p = realloc (ptr, size); 640 void *p = realloc (ptr, size);
663 641
664 if (!p) 642 if (!p)
665 rxvt_fatal ("memory allocation failure. aborting.\n"); 643 rxvt_fatal ("memory allocation failure. aborting.\n");
666 644
667 return p; 645 return p;
668} 646}
669
670/* ------------------------------------------------------------------------- *
671 * PRIVILEGED OPERATIONS *
672 * ------------------------------------------------------------------------- */
673/* take care of suid/sgid super-user (root) privileges */
674void
675rxvt_privileges (rxvt_privaction action)
676{
677#if ! defined(__CYGWIN32__)
678# if !defined(HAVE_SETEUID) && defined(HAVE_SETREUID)
679 /* setreuid () is the poor man's setuid (), seteuid () */
680# define seteuid(a) setreuid(-1, (a))
681# define setegid(a) setregid(-1, (a))
682# define HAVE_SETEUID
683# endif
684# ifdef HAVE_SETEUID
685 switch (action)
686 {
687 case IGNORE:
688 /*
689 * change effective uid/gid - not real uid/gid - so we can switch
690 * back to root later, as required
691 */
692 setegid (getgid ());
693 seteuid (getuid ());
694 break;
695 case SAVE:
696 saved_egid = getegid ();
697 saved_euid = geteuid ();
698 break;
699 case RESTORE:
700 setegid (saved_egid);
701 seteuid (saved_euid);
702 break;
703 }
704# else
705 switch (action)
706 {
707 case IGNORE:
708 setgid (getgid ());
709 setuid (getuid ());
710 /* FALLTHROUGH */
711 case SAVE:
712 /* FALLTHROUGH */
713 case RESTORE:
714 break;
715 }
716# endif
717#endif
718}
719
720#ifdef UTMP_SUPPORT
721void
722rxvt_term::privileged_utmp (rxvt_privaction action)
723{
724 if (OPTION (Opt_utmpInhibit))
725 return;
726
727 rxvt_privileges (RESTORE);
728
729 if (action == SAVE)
730 pty->login (cmd_pid, OPTION (Opt_loginShell), rs[Rs_display_name]);
731 else
732 pty->logout ();
733
734 rxvt_privileges (IGNORE);
735}
736#endif
737 647
738/*----------------------------------------------------------------------*/ 648/*----------------------------------------------------------------------*/
739/* 649/*
740 * window size/position calculcations for XSizeHint and other storage. 650 * window size/position calculcations for XSizeHint and other storage.
741 * if width/height are non-zero then override calculated width/height 651 * if width/height are non-zero then override calculated width/height
1680 XFree (preedit_attr); 1590 XFree (preedit_attr);
1681 XFree (status_attr); 1591 XFree (status_attr);
1682} 1592}
1683#endif /* USE_XIM */ 1593#endif /* USE_XIM */
1684 1594
1685/*----------------------------------------------------------------------*/
1686rxvt_t rxvt_current_term;
1687
1688/*----------------------- end-of-file (C source) -----------------------*/ 1595/*----------------------- end-of-file (C source) -----------------------*/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines