File: signals_posix.cpp

package info (click to toggle)
falconpl 0.9.6.9-git20120606-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 46,176 kB
  • sloc: cpp: 181,389; ansic: 109,025; yacc: 2,310; xml: 1,218; sh: 403; objc: 245; makefile: 82; sql: 20
file content (178 lines) | stat: -rw-r--r-- 4,036 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
   FALCON - The Falcon Programming Language.
   FILE: signals_posix.cpp

   POSIX-specific signal handling
   -------------------------------------------------------------------
   Author: Jan Dvorak
   Begin: Fri, 12 Feb 2010 11:23:13 +0100

   -------------------------------------------------------------------
   (C) Copyright 2009: the FALCON developers (see list in AUTHORS file)

   See LICENSE file for licensing details.
*/

#include <falcon/signals_posix.h>
#include <falcon/lineardict.h>
#include <unistd.h>
#include <string.h>

#if !defined(__linux__) || !defined(__sun)
# define __BSD__
#endif	

namespace Falcon {

SignalReceiver *signalReceiver = 0;

void signal_handler(int signum, siginfo_t *siginfo, void *ctx)
{
   signalReceiver->deliver(signum, siginfo);
}

SignalReceiver::SignalReceiver(VMachine *targetVM):
   m_thread(0),
   m_shallRun(false)
{
   m_targetVM = targetVM;
}

SignalReceiver::~SignalReceiver()
{
   stop();
}

void SignalReceiver::start()
{
   if (0 != m_thread)
      return;

   m_thread = new SysThread(this);
   m_shallRun = true;
   m_thread->start();
}

void SignalReceiver::stop()
{
   if (0 == m_thread)
      return;

   void *dummy;
   m_shallRun = false;
   wakeup();
   m_thread->join(dummy);
   m_thread = 0;
}

void *SignalReceiver::run()
{
   sigset_t sigset;

   /*
    * Allow all signals in this thread.
    */
   sigemptyset(&sigset);
   sigaddset(&sigset, SIGCONT);
   pthread_sigmask(SIG_SETMASK, &sigset, NULL);

   /* Wait... and wait... and wait. */
   while (m_shallRun)
   {
   #ifndef __APPLE__
      sigwaitinfo(&sigset, NULL);
   #else
      sigwait(&sigset, NULL);   
   #endif
   }
   
   return 0;
}

bool SignalReceiver::trap(int signum)
{
   struct sigaction sigaction;

   sigaction.sa_sigaction = signal_handler;
   sigfillset(&sigaction.sa_mask);
   sigaction.sa_flags = SA_SIGINFO;

   return 0 == ::sigaction(signum, &sigaction, 0);
}

bool SignalReceiver::reset(int signum)
{
   return 0 == ::signal(signum, SIG_DFL);
}

void SignalReceiver::deliver(int signum, siginfo_t *siginfo)
{
   VMMessage *m = new VMMessage("os.signal");
   LinearDict *ld = new LinearDict();
   CoreDict *cd = new CoreDict(ld);

   ld->put(new CoreString("signo"), (int32)signum);
   ld->put(new CoreString("errno"), (int32)siginfo->si_errno);
   ld->put(new CoreString("code"), (int32)siginfo->si_code);

#ifndef __APPLE__
   if (SIGCHLD == signum || (signum >= SIGRTMIN && signum <= SIGRTMAX)) {
      ld->put(new CoreString("pid"), (int32)siginfo->si_pid);
      ld->put(new CoreString("uid"), (int32)siginfo->si_uid);
#ifndef __BSD__
      ld->put(new CoreString("utime"), (int64)siginfo->si_utime);
      ld->put(new CoreString("stime"), (int64)siginfo->si_stime);
#endif
   }
   if (signum >= SIGRTMIN && signum <= SIGRTMAX) {
#ifdef _POSIX_SOURCE
      ld->put(new CoreString("overrun"), (int32)siginfo->si_overrun);
      ld->put(new CoreString("timerid"), (int32)siginfo->si_timerid);
      ld->put(new CoreString("int"), (int32)siginfo->si_int);
#endif
   }
   if (SIGCHLD == signum) {
      ld->put(new CoreString("status"), (int32)siginfo->si_status);
   }
#ifndef __BSD__
   if (SIGPOLL == signum) {
      ld->put(new CoreString("band"), (int32)siginfo->si_band);
      ld->put(new CoreString("fd"), (int32)siginfo->si_fd);
   }
#endif
#else /* MACOSX */
      ld->put(new CoreString("pid"), (int32)siginfo->si_pid);
      ld->put(new CoreString("uid"), (int32)siginfo->si_uid);
      ld->put(new CoreString("status"), (int32)siginfo->si_status);
      ld->put(new CoreString("band"), (int32)siginfo->si_band);
#endif

   cd->bless(true);

   m->addParam(SafeItem(cd));
   m_targetVM->postMessage(m);
}

void SignalReceiver::wakeup(void)
{
   kill(getpid(), SIGCONT);
}

void BlockSignals()
{
   sigset_t sigset;
   sigfillset(&sigset);
   pthread_sigmask(SIG_SETMASK, &sigset, NULL);
}

void UnblockSignals()
{
   sigset_t sigset;
   sigemptyset(&sigset);
   pthread_sigmask(SIG_SETMASK, &sigset, NULL);
}

}

// vim: et ts=3 sw=3 :
/* end of signals_posix.cpp */