ns-3 Direct Code Execution
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
dce-signal.cc
Go to the documentation of this file.
1 #include "dce-signal.h"
2 #include "utils.h"
3 #include "process.h"
4 #include "dce-manager.h"
5 #include "ns3/log.h"
6 #include "ns3/assert.h"
7 #include <vector>
8 #include <errno.h>
9 
10 NS_LOG_COMPONENT_DEFINE ("SimuSignal");
11 
12 using namespace ns3;
13 
14 sighandler_t dce_signal (int signum, sighandler_t handler)
15 {
16  NS_LOG_FUNCTION (Current () << UtilsGetNodeId () << signum << handler);
17  NS_ASSERT (Current () != 0);
18  struct sigaction action, old_action;
19  action.sa_handler = handler;
20  sigemptyset (&action.sa_mask);
21  action.sa_flags = 0;
22  int status = dce_sigaction (signum, &action, &old_action);
23  if (status != 0)
24  {
25  return SIG_ERR;
26  }
27  if (old_action.sa_flags & SA_SIGINFO)
28  {
29  return (sighandler_t)old_action.sa_sigaction;
30  }
31  else
32  {
33  return old_action.sa_handler;
34  }
35 }
36 int dce_sigaction (int signum, const struct sigaction *act,
37  struct sigaction *oldact)
38 {
39  Thread *current = Current ();
40  NS_LOG_FUNCTION (current << UtilsGetNodeId () << signum << act << oldact);
41  NS_ASSERT (current != 0);
42 
43  for (std::vector<SignalHandler>::iterator i = current->process->signalHandlers.begin ();
44  i != current->process->signalHandlers.end (); ++i)
45  {
46  if (i->signal == signum)
47  {
48  if (oldact != 0)
49  {
50  oldact->sa_flags = i->flags;
51  oldact->sa_mask = i->mask;
52  if (oldact->sa_flags & SA_SIGINFO)
53  {
54  oldact->sa_sigaction = i->sigaction;
55  }
56  else
57  {
58  oldact->sa_handler = i->handler;
59  }
60  }
61  if (act != 0)
62  {
63  i->flags = act->sa_flags;
64  i->mask = act->sa_mask;
65  if (act->sa_flags & SA_SIGINFO)
66  {
67  i->sigaction = act->sa_sigaction;
68  }
69  else
70  {
71  i->handler = act->sa_handler;
72  }
73  }
74  return 0;
75  }
76  }
77  if (act != 0)
78  {
79  struct SignalHandler handler;
80  handler.signal = signum;
81  handler.flags = act->sa_flags;
82  handler.mask = act->sa_mask;
83  if (act->sa_flags & SA_SIGINFO)
84  {
85  handler.sigaction = act->sa_sigaction;
86  }
87  else
88  {
89  handler.handler = act->sa_handler;
90  }
91  current->process->signalHandlers.push_back (handler);
92  }
93  if (oldact != 0)
94  {
95  oldact->sa_handler = SIG_IGN;
96  oldact->sa_flags = 0;
97  sigemptyset (&oldact->sa_mask);
98  }
99 
100  return 0;
101 }
102 
103 int dce_sigwait (const sigset_t *set, int *sig)
104 {
105  Thread *current = Current ();
106  NS_LOG_FUNCTION (current << UtilsGetNodeId () << set << sig);
107  NS_ASSERT (current != 0);
108 
109  int ret = 0;
110  // NEED TO WORK!!
111  // XXX: we need to add signal num notiifcation
112  current->process->manager->Wait ();
113  // sigdelset (&current->pendingSignals, numnum);
114 
115  return ret;
116 }
117 int dce_sigprocmask (int how, const sigset_t *set, sigset_t *oldset)
118 {
119  Thread *current = Current ();
120  NS_LOG_FUNCTION (current << UtilsGetNodeId () << how);
121  NS_ASSERT (current != 0);
122 
123  if (0 != oldset)
124  {
125  *oldset = current->signalMask;
126  }
127 
128  switch (how)
129  {
130  case SIG_BLOCK:
131  {
132  if (set)
133  {
134  sigorset (&current->signalMask, &current->signalMask, set);
135  }
136  } break;
137 
138  case SIG_UNBLOCK:
139  {
140  if (set)
141  {
142  for (int s = SIGINT; s <= SIGRTMAX ; s++)
143  {
144  if (sigismember (set, s))
145  {
146  sigdelset (&current->signalMask, s);
147  }
148  }
149  }
150  } break;
151 
152  case SIG_SETMASK:
153  {
154  if (set)
155  {
156  current->signalMask = *set;
157  }
158  } break;
159 
160  default:
161  {
162  current->err = EINVAL;
163  return -1;
164  }
165  }
166  return 0;
167 }