source: Sophya/trunk/SophyaLib/SysTools/psighand.cc@ 2598

Last change on this file since 2598 was 2598, checked in by ansari, 21 years ago

Documentation (ajoutee ou completee) pour les classes du module SysTools - Reza 11 Aout 2004

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