~ubuntu-branches/ubuntu/intrepid/ruby1.9/intrepid-updates

« back to all changes in this revision

Viewing changes to signal.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-09-04 16:01:17 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070904160117-i15zckg2nhxe9fyw
Tags: 1.9.0+20070830-2ubuntu1
* Sync from Debian; remaining changes:
  - Add -g to CFLAGS.
* Fixes build failure on ia64.
* Fixes build failure with gcc-4.2 on lpia.
* Robustify check for target_os, fixing build failure on lpia.
* Set Ubuntu maintainer address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
  signal.c -
4
4
 
5
 
  $Author: nobu $
6
 
  $Date: 2007-05-30 02:13:05 +0900 (水, 30  5月 2007) $
 
5
  $Author: matz $
 
6
  $Date: 2007-08-25 12:29:39 +0900 (土, 25  8月 2007) $
7
7
  created at: Tue Dec 20 10:13:44 JST 1994
8
8
 
9
 
  Copyright (C) 1993-2003 Yukihiro Matsumoto
 
9
  Copyright (C) 1993-2007 Yukihiro Matsumoto
10
10
  Copyright (C) 2000  Network Applied Communication Laboratory, Inc.
11
11
  Copyright (C) 2000  Information-technology Promotion Agency, Japan
12
12
 
13
13
**********************************************************************/
14
14
 
15
 
#include "ruby.h"
16
 
#include "rubysig.h"
17
 
#include "node.h"
18
 
#include "yarvcore.h"
 
15
#include "ruby/ruby.h"
 
16
#include "ruby/signal.h"
 
17
#include "ruby/node.h"
 
18
#include "vm_core.h"
19
19
#include <signal.h>
20
20
#include <stdio.h>
21
21
 
529
529
        exit(1);
530
530
    }
531
531
    else {
 
532
        extern int ruby_gc_stress;
532
533
        segv_received = 1;
 
534
        ruby_gc_stress = 0;
533
535
        rb_bug("Segmentation fault");
534
536
    }
535
537
}
549
551
    rb_proc_t *proc;
550
552
    VALUE signum = INT2FIX(sig);
551
553
    GetProcPtr(cmd, proc);
552
 
    th_invoke_proc(GET_THREAD(), proc, proc->block.self, 1, &signum);
 
554
    vm_invoke_proc(GET_THREAD(), proc, proc->block.self, 1, &signum);
553
555
}
554
556
 
555
557
void
635
637
};
636
638
 
637
639
static sighandler_t
638
 
trap_handler(VALUE *cmd)
639
 
{
640
 
    sighandler_t func = 0;
 
640
default_handler(int sig)
 
641
{
 
642
    sighandler_t func;
 
643
    switch (sig) {
 
644
      case SIGINT:
 
645
#ifdef SIGHUP
 
646
      case SIGHUP:
 
647
#endif
 
648
#ifdef SIGQUIT
 
649
      case SIGQUIT:
 
650
#endif
 
651
#ifdef SIGTERM
 
652
      case SIGTERM:
 
653
#endif
 
654
#ifdef SIGALRM
 
655
      case SIGALRM:
 
656
#endif
 
657
#ifdef SIGUSR1
 
658
      case SIGUSR1:
 
659
#endif
 
660
#ifdef SIGUSR2
 
661
      case SIGUSR2:
 
662
#endif
 
663
        func = sighandler;
 
664
        break;
 
665
#ifdef SIGBUS
 
666
      case SIGBUS:
 
667
        func = sigbus;
 
668
        break;
 
669
#endif
 
670
#ifdef SIGSEGV
 
671
      case SIGSEGV:
 
672
        func = sigsegv;
 
673
        break;
 
674
#endif
 
675
#ifdef SIGPIPE
 
676
      case SIGPIPE:
 
677
        func = sigpipe;
 
678
        break;
 
679
#endif
 
680
      default:
 
681
        func = SIG_DFL;
 
682
        break;
 
683
    }
 
684
 
 
685
    return func;
 
686
}
 
687
 
 
688
static RETSIGTYPE
 
689
wrong_trap(int sig)
 
690
{
 
691
}
 
692
 
 
693
static sighandler_t
 
694
trap_handler(VALUE *cmd, int sig)
 
695
{
 
696
    sighandler_t func = wrong_trap;
641
697
    VALUE command;
642
698
 
643
699
    if (NIL_P(*cmd)) {
649
705
            SafeStringValue(command);   /* taint check */
650
706
            switch (RSTRING_LEN(command)) {
651
707
              case 0:
652
 
                func = SIG_IGN;
 
708
                goto sig_ign;
653
709
                break;
 
710
              case 14:
 
711
                if (strncmp(RSTRING_PTR(command), "SYSTEM_DEFAULT", 14) == 0) {
 
712
                    func = SIG_DFL;
 
713
                    *cmd = 0;
 
714
                }
 
715
                break;
654
716
              case 7:
655
717
                if (strncmp(RSTRING_PTR(command), "SIG_IGN", 7) == 0) {
656
 
                    func = SIG_IGN;
 
718
sig_ign:
 
719
                    func = SIG_IGN;
 
720
                    *cmd = 0;
657
721
                }
658
722
                else if (strncmp(RSTRING_PTR(command), "SIG_DFL", 7) == 0) {
659
 
                    func = SIG_DFL;
 
723
sig_dfl:
 
724
                    func = default_handler(sig);
 
725
                    *cmd = 0;
660
726
                }
661
727
                else if (strncmp(RSTRING_PTR(command), "DEFAULT", 7) == 0) {
662
 
                    func = SIG_DFL;
 
728
                    goto sig_dfl;
663
729
                }
664
730
                break;
665
731
              case 6:
666
732
                if (strncmp(RSTRING_PTR(command), "IGNORE", 6) == 0) {
667
 
                    func = SIG_IGN;
 
733
                    goto sig_ign;
668
734
                }
669
735
                break;
670
736
              case 4:
674
740
                }
675
741
                break;
676
742
            }
677
 
            if (!func) {
 
743
            if (func == wrong_trap) {
678
744
                rb_raise(rb_eArgError, "wrong trap - %s", RSTRING_PTR(command));
679
745
            }
680
746
        }
684
750
            func = sighandler;
685
751
        }
686
752
    }
687
 
    if (func == SIG_IGN || func == SIG_DFL) {
688
 
        *cmd = 0;
689
 
    }
690
753
 
691
754
    return func;
692
755
}
729
792
    return sig;
730
793
}
731
794
 
732
 
static sighandler_t
733
 
default_handler(sighandler_t func, int sig)
734
 
{
735
 
    if (func == SIG_DFL) {
736
 
        switch (sig) {
737
 
          case SIGINT:
738
 
#ifdef SIGHUP
739
 
          case SIGHUP:
740
 
#endif
741
 
#ifdef SIGQUIT
742
 
          case SIGQUIT:
743
 
#endif
744
 
#ifdef SIGTERM
745
 
          case SIGTERM:
746
 
#endif
747
 
#ifdef SIGALRM
748
 
          case SIGALRM:
749
 
#endif
750
 
#ifdef SIGUSR1
751
 
          case SIGUSR1:
752
 
#endif
753
 
#ifdef SIGUSR2
754
 
          case SIGUSR2:
755
 
#endif
756
 
            func = sighandler;
757
 
            break;
758
 
#ifdef SIGBUS
759
 
          case SIGBUS:
760
 
            func = sigbus;
761
 
            break;
762
 
#endif
763
 
#ifdef SIGSEGV
764
 
          case SIGSEGV:
765
 
            func = sigsegv;
766
 
            break;
767
 
#endif
768
 
#ifdef SIGPIPE
769
 
          case SIGPIPE:
770
 
            func = sigpipe;
771
 
            break;
772
 
#endif
773
 
        }
774
 
    }
775
 
 
776
 
    return func;
777
 
}
778
 
 
779
795
static VALUE
780
796
trap(struct trap_arg *arg)
781
797
{
837
853
 
838
854
/*
839
855
 * call-seq:
840
 
 *   Signal.trap( signal, proc ) => obj
 
856
 *   Signal.trap( signal, command ) => obj
841
857
 *   Signal.trap( signal ) {| | block } => obj
842
858
 *
843
859
 * Specifies the handling of signals. The first parameter is a signal
844
860
 * name (a string such as ``SIGALRM'', ``SIGUSR1'', and so on) or a
845
861
 * signal number. The characters ``SIG'' may be omitted from the
846
862
 * signal name. The command or block specifies code to be run when the
847
 
 * signal is raised. If the command is the string ``IGNORE'' or
848
 
 * ``SIG_IGN'', the signal will be ignored. If the command is
849
 
 * ``DEFAULT'' or ``SIG_DFL'', the operating system's default handler
850
 
 * will be invoked. If the command is ``EXIT'', the script will be
851
 
 * terminated by the signal. Otherwise, the given command or block
852
 
 * will be run.
 
863
 * signal is raised.
 
864
 * If the command is the string ``IGNORE'' or ``SIG_IGN'', the signal
 
865
 * will be ignored.
 
866
 * If the command is ``DEFAULT'' or ``SIG_DFL'', the Ruby's default handler
 
867
 * will be invoked.
 
868
 * If the command is ``EXIT'', the script will be terminated by the signal.
 
869
 * If the command is ``SYSTEM_DEFAULT'', the operating system's default
 
870
 * handler will be invoked.
 
871
 * Otherwise, the given command or block will be run.
853
872
 * The special signal name ``EXIT'' or signal number zero will be
854
873
 * invoked just prior to program termination.
855
874
 * trap returns the previous handler for the given signal.
880
899
    }
881
900
    else if (argc == 2) {
882
901
        arg.cmd = argv[1];
883
 
        arg.func = default_handler(trap_handler(&arg.cmd), arg.sig);
 
902
        arg.func = trap_handler(&arg.cmd, arg.sig);
884
903
    }
885
904
 
886
905
    if (OBJ_TAINTED(arg.cmd)) {
974
993
#endif
975
994
}
976
995
 
 
996
#ifdef RUBY_DEBUG_ENV
 
997
int ruby_enable_coredump = 0;
 
998
#endif
 
999
 
977
1000
/*
978
1001
 * Many operating systems allow signals to be sent to running
979
1002
 * processes. Some signals have a defined effect on the process, while
1046
1069
    install_sighandler(SIGUSR2, sighandler);
1047
1070
#endif
1048
1071
 
 
1072
#ifdef RUBY_DEBUG_ENV
 
1073
    if (!ruby_enable_coredump)
 
1074
#endif
 
1075
    {
1049
1076
#ifdef SIGBUS
1050
1077
    install_sighandler(SIGBUS, sigbus);
1051
1078
#endif
1052
1079
#ifdef SIGSEGV
1053
1080
    install_sighandler(SIGSEGV, sigsegv);
1054
1081
#endif
 
1082
    }
1055
1083
#ifdef SIGPIPE
1056
1084
    install_sighandler(SIGPIPE, sigpipe);
1057
1085
#endif