Use headers defined in the C[++] standards:
[fw/sdcc] / sim / ucsim / gui.src / serio.src / posix_signal.cc
1 /******************************************************************************
2  * posix_signal.cc - A signal handleing class for linux + solaris             *
3  * to convert posix into somthing easier to use                               *
4  * Tim Hurman - t.hurman@virgin.net                                           *
5  * Last edited on 01th Oct 19999                                              *
6  ******************************************************************************/
7 /*
8  * A quick note, fscking linux, none of this would be neccessary if
9  * linux contained support for sighold, sigrelse, sigignore and sigpause.
10  *
11  */
12
13 #include <sys/types.h>
14 #include <iostream>
15 #include <sys/wait.h>   /* header for waitpid() and various macros */
16 #include <signal.h>     /* header for signal functions */
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <strings.h>
21 #include <errno.h>
22
23 #include "posix_signal.hh"
24
25 // constructor
26 SigHandler::SigHandler()
27 {
28 }
29
30 // destructor
31 SigHandler::~SigHandler()
32 {
33 }
34
35 /* set a signal */
36 int SigHandler::SetSignal(int SIGNAL, SIG_PF ACTION)
37 {
38         struct sigaction act;
39
40         /* declare what is going to be called when */
41         act.sa_handler = ACTION;
42
43         /* clear the structure's mask */
44         sigemptyset(&act.sa_mask);
45
46         /* set up some flags */
47         if(SIGNAL == SIGCHLD) {
48                 act.sa_flags = SA_NOCLDSTOP;
49         }
50
51         /* set the signal handler */
52         if(sigaction(SIGNAL, &act, NULL) < 0)
53         {
54                 std::cerr << "sigaction(): " << strerror(errno) << "\n";
55                 exit(-1);
56         }
57
58         /* all ok */
59         return(0);
60 }
61
62
63 /* block a signal */
64 int SigHandler::BlockSignal(int SIGNAL)
65 {
66         sigset_t set;
67
68         /* initalise */
69         sigemptyset(&set);
70
71         /* add the SIGNAL to the set */
72         sigaddset(&set, SIGNAL);
73
74         /* block it */
75         if(sigprocmask(SIG_BLOCK, &set, NULL) < 0)
76         {
77                 std::cerr << "sigprocmask(): " << strerror(errno) << "\n";
78                 exit(-1);
79         }
80
81         /* done */
82         return(0);
83 }
84
85
86 /* unblock a signal */
87 int SigHandler::UnBlockSignal(int SIGNAL)
88 {
89         sigset_t set;
90
91         /* initalise */
92         sigemptyset(&set);
93
94         /* add the SIGNAL to the set */
95         sigaddset(&set, SIGNAL);
96
97         /* block it */
98         if(sigprocmask(SIG_UNBLOCK, &set, NULL) < 0)
99         {
100                 std::cerr << "sigprocmask(): " << strerror(errno) << "\n";
101                 exit(-1);
102         }
103
104         /* done */
105         return(0);
106 }