-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjsiSignal.c
408 lines (370 loc) · 12 KB
/
jsiSignal.c
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#ifndef JSI_LITE_ONLY
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#ifndef JSI_AMALGAMATION
/* Adapted to update so "signal.handle(func) gets called back like setInterval/setTimeout */
#include "jsiInt.h"
#endif
#ifndef JSI_OMIT_SIGNAL
enum { SIGNAL_ACTION_IGNORE=-1, SIGNAL_ACTION_DEFAULT=0, SIGNAL_ACTION_HANDLE=1, MAX_SIGNALS = (sizeof(Jsi_Wide) * 8)};
static Jsi_Wide *sigloc;
static Jsi_Wide sigsblocked;
static struct sigaction *sa_old;
static int signal_handling[MAX_SIGNALS] = {0};
/* Make sure to do this as a wide, not int */
#define sig_to_bit(SIG) ((Jsi_Wide)1 << (SIG))
static void signal_handler(int sig)
{
/* We just remember which signals occurred. Jsi_Eval() will
* notice this as soon as it can and throw an error
*/
*sigloc |= sig_to_bit(sig);
}
static void signal_ignorer(int sig)
{
/* We just remember which signals occurred */
sigsblocked |= sig_to_bit(sig);
}
/*
*----------------------------------------------------------------------
*
* Tcl_SignalId --
*
* Return a textual identifier for a signal number.
*
* Results:
* This procedure returns a machine-readable textual identifier
* that corresponds to sig. The identifier is the same as the
* #define name in signal.h.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
#define CASESIGNAL(name) case SIG##name: return "SIG" #name;
const char *Jsi_SignalId(int sig)
{
switch (sig) {
CASESIGNAL(ABRT); CASESIGNAL(ALRM); CASESIGNAL(BUS); CASESIGNAL(CONT); CASESIGNAL(FPE);
CASESIGNAL(HUP); CASESIGNAL(ILL); CASESIGNAL(INT); CASESIGNAL(KILL); CASESIGNAL(PIPE);
CASESIGNAL(PROF); CASESIGNAL(QUIT); CASESIGNAL(SEGV); CASESIGNAL(STOP); CASESIGNAL(SYS);
CASESIGNAL(TERM); CASESIGNAL(TRAP); CASESIGNAL(TSTP); CASESIGNAL(TTIN); CASESIGNAL(TTOU);
CASESIGNAL(URG); CASESIGNAL(USR1); CASESIGNAL(USR2); CASESIGNAL(VTALRM); CASESIGNAL(WINCH);
CASESIGNAL(XCPU);
CASESIGNAL(XFSZ);
#ifdef SIGPWR
CASESIGNAL(PWR);
#endif
#ifdef SIGCLD
CASESIGNAL(CLD);
#endif
#ifdef SIGEMT
CASESIGNAL(EMT);
#endif
#ifdef SIGLOST
CASESIGNAL(LOST);
#endif
#ifdef SIGIO
CASESIGNAL(IO);
#endif
#if defined(SIGPOLL) && (SIGPOLL != SIGIO)
CASESIGNAL(POLL);
#endif
#ifdef SIGINFO
CASESIGNAL(INFO);
#endif
}
return NULL;
}
const char *Jsi_SignalName(int sig)
{
const char *cp;
#ifdef HAVE_SYS_SIGLIST
if (sig >= 0 && sig < NSIG) {
return sys_siglist[sig];
}
#endif
cp = Jsi_SignalId(sig);
if (cp == NULL)
cp = "unknown signal";
return cp;
}
/**
* Given the name of a signal, returns the signal value if found,
* or returns -1 (and sets an error) if not found.
* We accept -SIGINT, SIGINT, INT or any lowercase version or a number,
* either positive or negative.
*/
static int find_signal_by_name(Jsi_Interp *interp, const char *name)
{
int i;
const char *pt = name;
/* Remove optional - and SIG from the front of the name */
if (*pt == '-') {
pt++;
}
if (strncasecmp(name, "sig", 3) == 0) {
pt += 3;
}
if (isdigit(UCHAR(pt[0]))) {
i = atoi(pt);
if (i > 0 && i < MAX_SIGNALS) {
return i;
}
}
else {
for (i = 1; i < MAX_SIGNALS; i++) {
/* Jsi_SignalId() returns names such as SIGINT, and
* returns "unknown signal id" if unknown, so this will work
*/
if (Jsi_Strncasecmp(Jsi_SignalName(i) + 3, pt, -1) == 0) {
return i;
}
}
}
return -1;
}
void jsi_SignalClear(Jsi_Interp *interp, int sigNum) {
Jsi_Wide nsig = sig_to_bit(sigNum);
(*sigloc) &= ~nsig;
}
int jsi_SignalIsSet(Jsi_Interp *interp, int sigNum) {
return (((*sigloc) & sig_to_bit(sigNum)) != 0);
}
static void jsi_SignalSet(Jsi_Interp *interp, int sig) {
struct sigaction sa;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sa.sa_handler = signal_handler;
if (SIGNAL_ACTION_HANDLE != signal_handling[sig]) {
if (signal_handling[sig] == SIGNAL_ACTION_DEFAULT) {
if (!sa_old) {
/* Allocate the structure the first time through */
sa_old = Jsi_Calloc(MAX_SIGNALS, sizeof(*sa_old));
}
sigaction(sig, &sa, &sa_old[sig]);
}
else {
sigaction(sig, &sa, 0);
}
signal_handling[sig] = SIGNAL_ACTION_HANDLE;
}
}
static int SignalSub(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr, int action)
{
struct sigaction sa;
int i, argc = Jsi_ValueGetLength(interp, args);
if (&interp->sigmask != sigloc) {
Jsi_LogWarn("not primary interp");
return JSI_OK;
}
if (argc == 0) {
Jsi_Obj *nobj = Jsi_ObjNew(interp);
int m;
Jsi_ValueMakeArrayObject(interp,*ret, nobj);
for (i = 0, m = 0; i < MAX_SIGNALS; i++) {
if (signal_handling[i] == action) {
/* Add signal name to the list */
Jsi_ObjArraySet(interp, nobj, Jsi_ValueNewStringKey(interp, Jsi_SignalName(i)), m++);
}
}
return JSI_OK;
}
/* Catch all the signals we care about */
if (action != SIGNAL_ACTION_DEFAULT) {
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
if (action == SIGNAL_ACTION_HANDLE) {
sa.sa_handler = signal_handler;
}
else {
sa.sa_handler = signal_ignorer;
}
}
/* Iterate through the provided signals */
for (i = 0; i < argc; i++) {
char *sstr;
int sig = find_signal_by_name(interp, sstr=Jsi_ValueArrayIndexToStr(interp,args,i, NULL));
if (sig < 0) {
Jsi_LogError("unknown signal: %s", sstr);
return JSI_ERROR;
}
if (action != signal_handling[sig]) {
/* Need to change the action for this signal */
switch (action) {
case SIGNAL_ACTION_HANDLE:
case SIGNAL_ACTION_IGNORE:
if (signal_handling[sig] == SIGNAL_ACTION_DEFAULT) {
if (!sa_old) {
/* Allocate the structure the first time through */
sa_old = Jsi_Calloc(MAX_SIGNALS, sizeof(*sa_old));
}
sigaction(sig, &sa, &sa_old[sig]);
}
else {
sigaction(sig, &sa, 0);
}
break;
case SIGNAL_ACTION_DEFAULT:
/* Restore old handler */
if (sa_old) {
sigaction(sig, &sa_old[sig], 0);
}
}
signal_handling[sig] = action;
}
}
return JSI_OK;
}
static int SignalHandleCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
return SignalSub(interp, args, _this, ret, funcPtr, SIGNAL_ACTION_HANDLE);
}
static int SignalIgnoreCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
return SignalSub(interp, args, _this, ret, funcPtr, SIGNAL_ACTION_IGNORE);
}
static int SignalDefaultCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
return SignalSub(interp, args, _this, ret, funcPtr, SIGNAL_ACTION_DEFAULT);
}
static int SignalNamesCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
Jsi_Obj *nobj = Jsi_ObjNew(interp);
int i, m;
Jsi_ValueMakeArrayObject(interp, *ret, nobj);
for (i=0, m=0; i<MAX_SIGNALS; i++) {
const char *nam;
nam = Jsi_SignalId(i);
if (nam)
Jsi_ObjArraySet(interp, nobj, Jsi_ValueNewStringKey(interp, nam), m++);
}
return JSI_OK;
}
static int SignalAlarmCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
Jsi_Value *sv = Jsi_ValueArrayIndex(interp, args, 0);
uint rc;
Jsi_Number dtim;
if (Jsi_GetNumberFromValue(interp, sv, &dtim) != JSI_OK) {
Jsi_LogError("bad time");
return JSI_ERROR;
}
if (dtim<1)
rc = ualarm(dtim * 1e6, 0);
else
rc = alarm((uint)dtim);
Jsi_ValueMakeNumber(interp, *ret, (Jsi_Number)rc);
return JSI_OK;
}
static int SignalKillCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
Jsi_Value *vpid = Jsi_ValueArrayIndex(interp, args, 0);
Jsi_Value *sv = Jsi_ValueArrayIndex(interp, args, 1);
Jsi_Number npid;
int rc, sigNum = SIGTERM;
if (Jsi_GetNumberFromValue(interp, vpid, &npid) != JSI_OK) {
Jsi_LogError("bad pid");
return JSI_ERROR;
}
if (sv) {
if (sv->vt == JSI_VT_NUMBER)
sigNum = (int)sv->d.num;
else {
char *ts = Jsi_ValueArrayIndexToStr(interp, args, 1, NULL);
if ((sigNum = find_signal_by_name(interp, ts)) < 0) {
Jsi_LogError("bad signal: %s", ts);
return JSI_ERROR;
}
}
}
if (sigNum < 0 || sigNum >= MAX_SIGNALS) {
Jsi_LogError("bad signal: %d", sigNum);
return JSI_ERROR;
}
rc = kill((int)npid, sigNum);
if (rc != 0) {
Jsi_LogError("kill failure");
return JSI_ERROR;
}
return JSI_OK;
}
static int SignalCallbackCmd(Jsi_Interp *interp, Jsi_Value *args, Jsi_Value *_this,
Jsi_Value **ret, Jsi_Func *funcPtr)
{
int isNew;
Jsi_Event *ev;
uint id;
int sigNum;
Jsi_Value *fv = Jsi_ValueArrayIndex(interp, args, 0);
Jsi_Value *sv = Jsi_ValueArrayIndex(interp, args, 1);
if (!Jsi_ValueIsFunction(interp, fv)) {
Jsi_LogError("expected function");
return JSI_ERROR;
}
if (sv && sv->vt == JSI_VT_NUMBER)
sigNum = (int)sv->d.num;
else {
char *ts = Jsi_ValueArrayIndexToStr(interp, args, 1, NULL);
if ((sigNum = find_signal_by_name(interp, ts)) < 0) {
Jsi_LogError("expected signal");
return JSI_ERROR;
}
}
if (sigNum < 0 || sigNum >= MAX_SIGNALS) {
Jsi_LogError("unknown signal: %d", sigNum);
return JSI_ERROR;
}
while (1) {
id = interp->eventIdx++;
Jsi_HashEntry *hPtr = Jsi_HashEntryCreate(interp->eventTbl, (void*)id, &isNew);
if (!isNew)
continue;
ev = Jsi_Calloc(1, sizeof(*ev));
SIGINIT(ev,EVENT);
ev->id = id;
ev->funcVal = fv;
Jsi_IncrRefCount(interp, fv);
ev->hPtr = hPtr;
ev->sigNum = sigNum;
ev->evType = JSI_EVENT_SIGNAL;
Jsi_HashValueSet(hPtr, ev);
break;
}
jsi_SignalSet(interp, sigNum);
Jsi_ValueMakeNumber(interp, *ret, (Jsi_Number)id);
return JSI_OK;
}
static Jsi_CmdSpec signalCmds[] = {
{ "alarm", SignalAlarmCmd, 1, 1, "secs", .help="Setup alarm in seconds" },
{ "callback", SignalCallbackCmd, 2, 2, "func,sig", .help="Setup callback handler for signal"},
{ "default", SignalDefaultCmd, 0,-1, "?sig,sig,...?", .help="Set named signals to default action" },
{ "handle", SignalHandleCmd, 0,-1, "?sig,sig,...?", .help="Set named signals to handle action" },
{ "ignore", SignalIgnoreCmd, 0,-1, "?sig,sig,...?", .help="Set named signals to ignore action" },
{ "kill", SignalKillCmd, 1, 2, "pid?,sig?", .help="Send signal to process id (default SIGTERM)" },
{ "names", SignalNamesCmd, 0, 0, "", .help="Return names of all signals" },
{ NULL, .help="Commands for handling unix signals" }
};
int jsi_Initsignal(Jsi_Interp *interp)
{
static int isinit = 0;
//interp->signal_set_result = signal_set_sigmask_result;
if (!isinit) {
sigloc = &interp->sigmask;
isinit = 1;
}
Jsi_CommandCreateSpecs(interp, "signal", signalCmds, NULL, JSI_CMDSPEC_NOTOBJ);
return JSI_OK;
}
#endif
#endif