hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Signals.cpp
Go to the documentation of this file.
1 #include <Signals.h>
2 func_list *Signal::functions = NULL;
3 sig_list *Signal::old_handlers = NULL;
4 int Signal::used_signals[255];
5 
7 {
8  memset(used_signals, 0, sizeof(used_signals));
9  functions = NULL;
10  old_handlers = NULL;
11 }
12 
14 {
15 }
16 
18 {
19 
20 }
21 void Signal::sig_func(int sig)
22 {
23  func_list *current;
24  for (current = functions; current; current = current->next)
25  {
26  if (current->sig_num == sig)
27  {
28  current->func(sig);
29  }
30  }
31 }
32 int Signal::Attach(int sig_num, sig_function func)
33 {
34  func_list *buf = NULL;
35  sig_list *sbuf = NULL;
36  buf = new(std::nothrow) func_list;
37  if(!buf)
38  {
39  return -1;
40  }
41  buf->sig_num = sig_num;
42  buf->func = func;
43  if (!used_signals[sig_num])
44  {
45  struct sigaction sa;
46  sbuf = new(std::nothrow) sig_list;
47  if(!sbuf)
48  {
49  return -1;
50  }
51  sigemptyset(&sa.sa_mask);
52  sa.sa_handler = this->sig_func;
53  sa.sa_flags = SA_RESTART;
54  if (sigaction(sig_num, &sa, &sbuf->sa) != -1)
55  {
56  buf->next = functions;
57  functions = buf;
58  sbuf->sig_num = sig_num;
59  sbuf->next = old_handlers;
60  old_handlers = sbuf;
61  used_signals[sig_num]++;
62  return 0;
63  }
64  delete buf;
65  delete sbuf;
66  return -1;
67  }
68  buf->next = functions;
69  functions = buf;
70  used_signals[sig_num]++;
71  return 0;
72 }
73 void Signal::Detach(int sig_num, sig_function func)
74 {
75  func_list *buf, *prev;
76  sig_list *sbuf, *sprev;
77  buf = functions;
78  prev = NULL;
79  while (buf)
80  {
81  if (buf->sig_num == sig_num && buf->func == func)
82  {
83  if (!(--used_signals[sig_num]))
84  {//set old function for signal
85  sbuf = old_handlers;
86  sprev = NULL;
87  while (sbuf)
88  {
89  if (sbuf->sig_num == sig_num)
90  {
91  sigaction(sig_num, &sbuf->sa, NULL);
92  if (sprev)
93  {
94  sprev->next = sbuf->next;
95  }
96  else
97  {
98  old_handlers = old_handlers->next;
99  }
100  delete sbuf;
101  break;
102  }
103  sprev = sbuf;
104  sbuf = sbuf->next;
105  }
106  }
107  if (prev)
108  {
109  prev->next = buf->next;
110  }
111  else
112  {
113  functions = functions->next;
114  }
115  delete buf;
116  break;
117  }
118  prev = buf;
119  buf = buf->next;
120  }
121 }