| 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"
 | 
|---|
| 7 | #include "pexceptions.h"
 | 
|---|
| 8 | 
 | 
|---|
| 9 | static bool sigprstate[6] = {false, false, false, false, false, false};
 | 
|---|
| 10 | 
 | 
|---|
| 11 | void SophyaProcessSignal(int s);
 | 
|---|
| 12 | 
 | 
|---|
| 13 | /* Nouvelle-Fonction */
 | 
|---|
| 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) 
 | 
|---|
| 27 | {
 | 
|---|
| 28 |   struct sigaction ae, ad;
 | 
|---|
| 29 | 
 | 
|---|
| 30 |   ae.sa_handler = SophyaProcessSignal;
 | 
|---|
| 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);
 | 
|---|
| 45 |       puts("SophyaConfigureSignalhandling(): Activating SIGFPE handling (-> throw) ...");
 | 
|---|
| 46 |       }
 | 
|---|
| 47 |     else  { 
 | 
|---|
| 48 |       sigaction(SIGFPE, &ad, NULL);
 | 
|---|
| 49 |       puts("SophyaConfigureSignalhandling(): Back to default SIGFPE handling ...");
 | 
|---|
| 50 |       }
 | 
|---|
| 51 |     sigprstate[0] = cfpe;
 | 
|---|
| 52 |     }
 | 
|---|
| 53 | // SIGSEGV 
 | 
|---|
| 54 |   if (sigprstate[1] != csegv) {
 | 
|---|
| 55 |     if (csegv)   { 
 | 
|---|
| 56 |       sigaction(SIGSEGV, &ae, NULL);
 | 
|---|
| 57 |       puts("SophyaConfigureSignalhandling(): Activating SIGSEGV handling (-> throw) ...");
 | 
|---|
| 58 |       }
 | 
|---|
| 59 |     else  { 
 | 
|---|
| 60 |       sigaction(SIGSEGV, &ad, NULL);
 | 
|---|
| 61 |       puts("SophyaConfigureSignalhandling(): Back to default SIGSEGV handling ...");
 | 
|---|
| 62 |       }
 | 
|---|
| 63 |     sigprstate[1] = csegv;
 | 
|---|
| 64 |     }
 | 
|---|
| 65 | 
 | 
|---|
| 66 | // SIGINT
 | 
|---|
| 67 |   if (sigprstate[2] != cint) {
 | 
|---|
| 68 |     if (cint)   { 
 | 
|---|
| 69 |       sigaction(SIGINT, &ae, NULL);
 | 
|---|
| 70 |       puts("SophyaConfigureSignalhandling(): Activating SIGINT handling (-> throw) ...");
 | 
|---|
| 71 |       }
 | 
|---|
| 72 |     else  { 
 | 
|---|
| 73 |       sigaction(SIGINT, &ad, NULL);
 | 
|---|
| 74 |       puts("SophyaConfigureSignalhandling(): Back to default SIGINT handling ...");
 | 
|---|
| 75 |       }
 | 
|---|
| 76 |     sigprstate[2] = cint; 
 | 
|---|
| 77 |     }
 | 
|---|
| 78 |   
 | 
|---|
| 79 | // SIGQUIT
 | 
|---|
| 80 |   if (sigprstate[3] != cquit) {
 | 
|---|
| 81 |     if (cquit)   { 
 | 
|---|
| 82 |       sigaction(SIGQUIT, &ae, NULL);
 | 
|---|
| 83 |       puts("SophyaConfigureSignalhandling(): Activating SIGQUIT handling (-> throw) ...");
 | 
|---|
| 84 |       }
 | 
|---|
| 85 |     else  { 
 | 
|---|
| 86 |       sigaction(SIGQUIT, &ad, NULL);
 | 
|---|
| 87 |       puts("SophyaConfigureSignalhandling(): Back to default SIGQUIT handling ...");
 | 
|---|
| 88 |       }
 | 
|---|
| 89 |     sigprstate[3] = cquit; 
 | 
|---|
| 90 |     }
 | 
|---|
| 91 |   
 | 
|---|
| 92 | // SIGUSR1
 | 
|---|
| 93 |   if (sigprstate[4] != cusr1) {
 | 
|---|
| 94 |     if (cusr1)   { 
 | 
|---|
| 95 |       sigaction(SIGUSR1, &ae, NULL);
 | 
|---|
| 96 |       puts("SophyaConfigureSignalhandling(): Activating SIGUSR1 handling (-> throw) ...");
 | 
|---|
| 97 |       }
 | 
|---|
| 98 |     else  { 
 | 
|---|
| 99 |       sigaction(SIGUSR1, &ad, NULL);
 | 
|---|
| 100 |       puts("SophyaConfigureSignalhandling(): Back to default SIGUSR1 handling ...");
 | 
|---|
| 101 |       }
 | 
|---|
| 102 |     sigprstate[4] = cusr1;
 | 
|---|
| 103 |     }
 | 
|---|
| 104 | // SIGUSR2
 | 
|---|
| 105 |   if (sigprstate[5] != cusr2) {
 | 
|---|
| 106 |     if (cusr2)   { 
 | 
|---|
| 107 |       sigaction(SIGUSR2, &ae, NULL);
 | 
|---|
| 108 |       puts("SophyaConfigureSignalhandling(): Activating SIGUSR2 handling (-> throw) ...");
 | 
|---|
| 109 |       }
 | 
|---|
| 110 |     else  { 
 | 
|---|
| 111 |       sigaction(SIGUSR2, &ad, NULL);
 | 
|---|
| 112 |       puts("SophyaConfigureSignalhandling(): Back to default SIGUSR2 handling ...");
 | 
|---|
| 113 |       }
 | 
|---|
| 114 |     sigprstate[5] = cusr2;
 | 
|---|
| 115 |     }    
 | 
|---|
| 116 | }
 | 
|---|
| 117 | /* Nouvelle-Fonction */
 | 
|---|
| 118 | void SophyaProcessSignal(int s)
 | 
|---|
| 119 | {
 | 
|---|
| 120 | string msg;
 | 
|---|
| 121 | switch(s)
 | 
|---|
| 122 |   {
 | 
|---|
| 123 |   case SIGFPE :
 | 
|---|
| 124 |     puts("SophyaProcessSignal: ###Signal SIGFPE catched, throw catchedSIGFPE ###");
 | 
|---|
| 125 |     msg = "Caught SIGFPE";
 | 
|---|
| 126 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 127 |   case SIGSEGV :
 | 
|---|
| 128 |     puts("SophyaProcessSignal: ###Signal SIGSEGV catched, throw catchedSIGSEGV ###");
 | 
|---|
| 129 |     msg = "Caught SIGSEGV";
 | 
|---|
| 130 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 131 |   case SIGINT :
 | 
|---|
| 132 |     puts("SophyaProcessSignal: ###Signal SIGINT catched, throw catchedSIGINT ###");
 | 
|---|
| 133 |     msg = "Caught SIGINT";
 | 
|---|
| 134 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 135 |   case SIGQUIT :
 | 
|---|
| 136 |     puts("SophyaProcessSignal: ###Signal SIGQUIT catched, throw catchedSIGQUIT ###");
 | 
|---|
| 137 |     msg = "Caught SIGQUIT";
 | 
|---|
| 138 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 139 |   case SIGUSR1 :
 | 
|---|
| 140 |     puts("SophyaProcessSignal: ###Signal SIGUSR1 catched, throw catchedSIGUSR1 ###");
 | 
|---|
| 141 |     msg = "Caught SIGUSR1";
 | 
|---|
| 142 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 143 |   case SIGUSR2 :
 | 
|---|
| 144 |     puts("SophyaProcessSignal: ###Signal SIGUSR2 catched, throw catchedSIGUSR2 ###");
 | 
|---|
| 145 |     msg = "Caught SIGUSR2";
 | 
|---|
| 146 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 147 |   default :
 | 
|---|
| 148 |     printf("SophyaProcessSignal: ###Signal %d catched, throw  inconsistentErr ### \n", s);
 | 
|---|
| 149 |     msg = "Caught SIG???";
 | 
|---|
| 150 |     throw ( CaughtSignalExc(msg, s) );
 | 
|---|
| 151 |   }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | // return;
 | 
|---|
| 154 | }
 | 
|---|