| [219] | 1 | //  Pour la gestion des signaux a travers des exceptions
 | 
|---|
 | 2 | #include <stdlib.h>
 | 
|---|
 | 3 | #include <stdio.h>
 | 
|---|
 | 4 | #include <string.h>
 | 
|---|
 | 5 | #include <signal.h>
 | 
|---|
 | 6 | #include "psighand.h"
 | 
|---|
| [480] | 7 | #include "pexceptions.h"
 | 
|---|
| [219] | 8 | 
 | 
|---|
| [480] | 9 | static bool sigprstate[6] = {false, false, false, false, false, false};
 | 
|---|
| [241] | 10 | 
 | 
|---|
| [913] | 11 | void SophyaProcessSignal(int s);
 | 
|---|
| [219] | 12 | 
 | 
|---|
 | 13 | /* Nouvelle-Fonction */
 | 
|---|
| [913] | 14 | /*!\ingroup  SysTools 
 | 
|---|
 | 15 |    Configures signal handling. When the flag is true, an exception 
 | 
|---|
 | 16 |    \b CaughtSignalExc is thrown with the excpetion id set to the 
 | 
|---|
 | 17 |    signal id. With the flag is false, the default signal handling
 | 
|---|
 | 18 |    is restored.
 | 
|---|
 | 19 |    \param cfpe : Floating point exception \c SIGFPE
 | 
|---|
 | 20 |    \param csegv : Segmentation fault \c SIGSEGV
 | 
|---|
 | 21 |    \param cint : Interrupt signal \c SIGINT
 | 
|---|
 | 22 |    \param cquit : Quit signal \c SIGINT
 | 
|---|
 | 23 |    \param cusr1 : User signal 1 \c SIGUSR1
 | 
|---|
 | 24 |    \param cusr2 : User signal 1 \c SIGUSR2
 | 
|---|
 | 25 | */
 | 
|---|
 | 26 | void SOPHYA::SophyaConfigureSignalhandling(bool cfpe, bool csegv, bool cint, bool cquit, bool cusr1, bool cusr2) 
 | 
|---|
| [219] | 27 | {
 | 
|---|
 | 28 |   struct sigaction ae, ad;
 | 
|---|
 | 29 | 
 | 
|---|
| [913] | 30 |   ae.sa_handler = SophyaProcessSignal;
 | 
|---|
| [219] | 31 |   ad.sa_handler = SIG_DFL;
 | 
|---|
 | 32 |   memset( &(ae.sa_mask), 0, sizeof(sigset_t) ); 
 | 
|---|
 | 33 |   ae.sa_flags = 0;
 | 
|---|
 | 34 |   memset( &(ad.sa_mask), 0, sizeof(sigset_t) ); 
 | 
|---|
 | 35 |   ad.sa_flags = 0;
 | 
|---|
 | 36 | #ifdef  OSF1
 | 
|---|
 | 37 |   ae.sa_flags = SA_RESTART;
 | 
|---|
 | 38 |   ad.sa_mask = 0;
 | 
|---|
 | 39 | #endif
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | // SIGFPE 
 | 
|---|
 | 42 |   if (sigprstate[0] != cfpe) {
 | 
|---|
 | 43 |     if (cfpe)   { 
 | 
|---|
 | 44 |       sigaction(SIGFPE, &ae, NULL);
 | 
|---|
| [913] | 45 |       puts("SophyaConfigureSignalhandling(): Activating SIGFPE handling (-> throw) ...");
 | 
|---|
| [219] | 46 |       }
 | 
|---|
 | 47 |     else  { 
 | 
|---|
 | 48 |       sigaction(SIGFPE, &ad, NULL);
 | 
|---|
| [913] | 49 |       puts("SophyaConfigureSignalhandling(): Back to default SIGFPE handling ...");
 | 
|---|
| [219] | 50 |       }
 | 
|---|
 | 51 |     sigprstate[0] = cfpe;
 | 
|---|
 | 52 |     }
 | 
|---|
 | 53 | // SIGSEGV 
 | 
|---|
 | 54 |   if (sigprstate[1] != csegv) {
 | 
|---|
 | 55 |     if (csegv)   { 
 | 
|---|
 | 56 |       sigaction(SIGSEGV, &ae, NULL);
 | 
|---|
| [913] | 57 |       puts("SophyaConfigureSignalhandling(): Activating SIGSEGV handling (-> throw) ...");
 | 
|---|
| [219] | 58 |       }
 | 
|---|
 | 59 |     else  { 
 | 
|---|
 | 60 |       sigaction(SIGSEGV, &ad, NULL);
 | 
|---|
| [913] | 61 |       puts("SophyaConfigureSignalhandling(): Back to default SIGSEGV handling ...");
 | 
|---|
| [219] | 62 |       }
 | 
|---|
 | 63 |     sigprstate[1] = csegv;
 | 
|---|
 | 64 |     }
 | 
|---|
| [480] | 65 | 
 | 
|---|
 | 66 | // SIGINT
 | 
|---|
 | 67 |   if (sigprstate[2] != cint) {
 | 
|---|
| [706] | 68 |     if (cint)   { 
 | 
|---|
| [480] | 69 |       sigaction(SIGINT, &ae, NULL);
 | 
|---|
| [913] | 70 |       puts("SophyaConfigureSignalhandling(): Activating SIGINT handling (-> throw) ...");
 | 
|---|
| [480] | 71 |       }
 | 
|---|
 | 72 |     else  { 
 | 
|---|
 | 73 |       sigaction(SIGINT, &ad, NULL);
 | 
|---|
| [913] | 74 |       puts("SophyaConfigureSignalhandling(): Back to default SIGINT handling ...");
 | 
|---|
| [480] | 75 |       }
 | 
|---|
 | 76 |     sigprstate[2] = cint; 
 | 
|---|
 | 77 |     }
 | 
|---|
 | 78 |   
 | 
|---|
| [219] | 79 | // SIGQUIT
 | 
|---|
| [480] | 80 |   if (sigprstate[3] != cquit) {
 | 
|---|
| [219] | 81 |     if (cquit)   { 
 | 
|---|
 | 82 |       sigaction(SIGQUIT, &ae, NULL);
 | 
|---|
| [913] | 83 |       puts("SophyaConfigureSignalhandling(): Activating SIGQUIT handling (-> throw) ...");
 | 
|---|
| [219] | 84 |       }
 | 
|---|
 | 85 |     else  { 
 | 
|---|
 | 86 |       sigaction(SIGQUIT, &ad, NULL);
 | 
|---|
| [913] | 87 |       puts("SophyaConfigureSignalhandling(): Back to default SIGQUIT handling ...");
 | 
|---|
| [219] | 88 |       }
 | 
|---|
| [480] | 89 |     sigprstate[3] = cquit; 
 | 
|---|
| [219] | 90 |     }
 | 
|---|
 | 91 |   
 | 
|---|
 | 92 | // SIGUSR1
 | 
|---|
| [480] | 93 |   if (sigprstate[4] != cusr1) {
 | 
|---|
| [219] | 94 |     if (cusr1)   { 
 | 
|---|
 | 95 |       sigaction(SIGUSR1, &ae, NULL);
 | 
|---|
| [913] | 96 |       puts("SophyaConfigureSignalhandling(): Activating SIGUSR1 handling (-> throw) ...");
 | 
|---|
| [219] | 97 |       }
 | 
|---|
 | 98 |     else  { 
 | 
|---|
 | 99 |       sigaction(SIGUSR1, &ad, NULL);
 | 
|---|
| [913] | 100 |       puts("SophyaConfigureSignalhandling(): Back to default SIGUSR1 handling ...");
 | 
|---|
| [219] | 101 |       }
 | 
|---|
| [480] | 102 |     sigprstate[4] = cusr1;
 | 
|---|
| [219] | 103 |     }
 | 
|---|
| [480] | 104 | // SIGUSR2
 | 
|---|
 | 105 |   if (sigprstate[5] != cusr2) {
 | 
|---|
| [219] | 106 |     if (cusr2)   { 
 | 
|---|
 | 107 |       sigaction(SIGUSR2, &ae, NULL);
 | 
|---|
| [913] | 108 |       puts("SophyaConfigureSignalhandling(): Activating SIGUSR2 handling (-> throw) ...");
 | 
|---|
| [219] | 109 |       }
 | 
|---|
 | 110 |     else  { 
 | 
|---|
 | 111 |       sigaction(SIGUSR2, &ad, NULL);
 | 
|---|
| [913] | 112 |       puts("SophyaConfigureSignalhandling(): Back to default SIGUSR2 handling ...");
 | 
|---|
| [219] | 113 |       }
 | 
|---|
| [480] | 114 |     sigprstate[5] = cusr2;
 | 
|---|
| [219] | 115 |     }    
 | 
|---|
 | 116 | }
 | 
|---|
 | 117 | /* Nouvelle-Fonction */
 | 
|---|
| [913] | 118 | void SophyaProcessSignal(int s)
 | 
|---|
| [219] | 119 | {
 | 
|---|
| [480] | 120 | string msg;
 | 
|---|
| [219] | 121 | switch(s)
 | 
|---|
 | 122 |   {
 | 
|---|
 | 123 |   case SIGFPE :
 | 
|---|
| [913] | 124 |     puts("SophyaProcessSignal: ###Signal SIGFPE catched, throw catchedSIGFPE ###");
 | 
|---|
| [480] | 125 |     msg = "Caught SIGFPE";
 | 
|---|
 | 126 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 127 |   case SIGSEGV :
 | 
|---|
| [913] | 128 |     puts("SophyaProcessSignal: ###Signal SIGSEGV catched, throw catchedSIGSEGV ###");
 | 
|---|
| [480] | 129 |     msg = "Caught SIGSEGV";
 | 
|---|
 | 130 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 131 |   case SIGINT :
 | 
|---|
| [913] | 132 |     puts("SophyaProcessSignal: ###Signal SIGINT catched, throw catchedSIGINT ###");
 | 
|---|
| [480] | 133 |     msg = "Caught SIGINT";
 | 
|---|
 | 134 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 135 |   case SIGQUIT :
 | 
|---|
| [913] | 136 |     puts("SophyaProcessSignal: ###Signal SIGQUIT catched, throw catchedSIGQUIT ###");
 | 
|---|
| [480] | 137 |     msg = "Caught SIGQUIT";
 | 
|---|
 | 138 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 139 |   case SIGUSR1 :
 | 
|---|
| [913] | 140 |     puts("SophyaProcessSignal: ###Signal SIGUSR1 catched, throw catchedSIGUSR1 ###");
 | 
|---|
| [480] | 141 |     msg = "Caught SIGUSR1";
 | 
|---|
 | 142 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 143 |   case SIGUSR2 :
 | 
|---|
| [913] | 144 |     puts("SophyaProcessSignal: ###Signal SIGUSR2 catched, throw catchedSIGUSR2 ###");
 | 
|---|
| [480] | 145 |     msg = "Caught SIGUSR2";
 | 
|---|
 | 146 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 147 |   default :
 | 
|---|
| [913] | 148 |     printf("SophyaProcessSignal: ###Signal %d catched, throw  inconsistentErr ### \n", s);
 | 
|---|
| [480] | 149 |     msg = "Caught SIG???";
 | 
|---|
 | 150 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| [219] | 151 |   }
 | 
|---|
| [480] | 152 | 
 | 
|---|
 | 153 | // return;
 | 
|---|
| [219] | 154 | }
 | 
|---|