13 #include "jim-subcmd.h"
14 #include "jim-signal.h"
16 #define MAX_SIGNALS (sizeof(jim_wide) * 8)
18 static jim_wide
*sigloc
;
19 static jim_wide sigsblocked
;
20 static struct sigaction
*sa_old
;
21 static int signal_handling
[MAX_SIGNALS
];
23 /* Make sure to do this as a wide, not int */
24 #define sig_to_bit(SIG) ((jim_wide)1 << (SIG))
26 static void signal_handler(int sig
)
28 /* We just remember which signals occurred. Jim_Eval() will
29 * notice this as soon as it can and throw an error
31 *sigloc
|= sig_to_bit(sig
);
34 static void signal_ignorer(int sig
)
36 /* We just remember which signals occurred */
37 sigsblocked
|= sig_to_bit(sig
);
41 *----------------------------------------------------------------------
45 * Return a textual identifier for a signal number.
48 * This procedure returns a machine-readable textual identifier
49 * that corresponds to sig. The identifier is the same as the
50 * #define name in signal.h.
55 *----------------------------------------------------------------------
57 #define CHECK_SIG(NAME) if (sig == NAME) return #NAME
59 const char *Jim_SignalId(int sig
)
108 return "unknown signal";
112 * Given the name of a signal, returns the signal value if found,
113 * or returns -1 (and sets an error) if not found.
114 * We accept -SIGINT, SIGINT, INT or any lowercase version or a number,
115 * either positive or negative.
117 static int find_signal_by_name(Jim_Interp
*interp
, const char *name
)
120 const char *pt
= name
;
122 /* Remove optional - and SIG from the front of the name */
126 if (strncasecmp(name
, "sig", 3) == 0) {
129 if (isdigit(pt
[0])) {
131 if (i
> 0 && i
< MAX_SIGNALS
) {
136 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
137 /* Jim_SignalId() returns names such as SIGINT, and
138 * returns "unknown signal id" if unknown, so this will work
140 if (strcasecmp(Jim_SignalId(i
) + 3, pt
) == 0) {
145 Jim_SetResultString(interp
, "unknown signal ", -1);
146 Jim_AppendString(interp
, Jim_GetResult(interp
), name
, -1);
151 #define SIGNAL_ACTION_HANDLE 1
152 #define SIGNAL_ACTION_IGNORE -1
153 #define SIGNAL_ACTION_DEFAULT 0
155 static int do_signal_cmd(Jim_Interp
*interp
, int action
, int argc
, Jim_Obj
*const *argv
)
161 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
162 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
163 if (signal_handling
[i
] == action
) {
164 /* Add signal name to the list */
165 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
166 Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
172 /* Catch all the signals we care about */
173 if (action
!= SIGNAL_ACTION_DEFAULT
) {
175 sigemptyset(&sa
.sa_mask
);
176 if (action
== SIGNAL_ACTION_HANDLE
) {
177 sa
.sa_handler
= signal_handler
;
180 sa
.sa_handler
= signal_ignorer
;
184 /* Iterate through the provided signals */
185 for (i
= 0; i
< argc
; i
++) {
186 int sig
= find_signal_by_name(interp
, Jim_GetString(argv
[i
], NULL
));
191 if (action
!= signal_handling
[sig
]) {
192 /* Need to change the action for this signal */
194 case SIGNAL_ACTION_HANDLE
:
195 case SIGNAL_ACTION_IGNORE
:
196 if (signal_handling
[sig
] == SIGNAL_ACTION_DEFAULT
) {
198 /* Allocate the structure the first time through */
199 sa_old
= Jim_Alloc(sizeof(*sa_old
) * MAX_SIGNALS
);
201 sigaction(sig
, &sa
, &sa_old
[sig
]);
204 sigaction(sig
, &sa
, 0);
208 case SIGNAL_ACTION_DEFAULT
:
209 /* Restore old handler */
211 sigaction(sig
, &sa_old
[sig
], 0);
214 signal_handling
[sig
] = action
;
221 static int signal_cmd_handle(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
223 return do_signal_cmd(interp
, SIGNAL_ACTION_HANDLE
, argc
, argv
);
226 static int signal_cmd_ignore(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
228 return do_signal_cmd(interp
, SIGNAL_ACTION_IGNORE
, argc
, argv
);
231 static int signal_cmd_default(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
233 return do_signal_cmd(interp
, SIGNAL_ACTION_DEFAULT
, argc
, argv
);
236 static int signal_set_sigmask_result(Jim_Interp
*interp
, jim_wide sigmask
)
239 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
241 for (i
= 0; i
< MAX_SIGNALS
; i
++) {
242 if (sigmask
& sig_to_bit(i
)) {
243 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
246 Jim_SetResult(interp
, listObj
);
250 static int signal_cmd_check(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
256 if (argc
> 0 && Jim_CompareStringImmediate(interp
, argv
[0], "-clear")) {
262 /* Signals specified */
263 for (i
= clear
; i
< argc
; i
++) {
264 int sig
= find_signal_by_name(interp
, Jim_GetString(argv
[i
], NULL
));
266 if (sig
< 0 || sig
>= MAX_SIGNALS
) {
269 mask
|= sig_to_bit(sig
);
273 /* No signals specified, so check/clear all */
277 if ((sigsblocked
& mask
) == 0) {
278 /* No matching signals, so empty result and nothing to do */
281 /* Be careful we don't have a race condition where signals are cleared but not returned */
282 blocked
= sigsblocked
& mask
;
284 sigsblocked
&= ~blocked
;
287 signal_set_sigmask_result(interp
, blocked
);
291 static int signal_cmd_throw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
296 if ((sig
= find_signal_by_name(interp
, Jim_GetString(argv
[0], NULL
))) < 0) {
301 /* If the signal is ignored (blocked) ... */
302 if (signal_handling
[sig
] == SIGNAL_ACTION_IGNORE
) {
303 sigsblocked
|= sig_to_bit(sig
);
307 /* Just set the signal */
308 interp
->sigmask
|= sig_to_bit(sig
);
310 /* Set the canonical name of the signal as the result */
311 Jim_SetResultString(interp
, Jim_SignalId(sig
), -1);
313 /* And simply say we caught the signal */
318 *-----------------------------------------------------------------------------
321 * Implements the TCL signal command:
322 * signal handle|ignore|default|throw ?signals ...?
323 * signal throw signal
325 * Specifies which signals are handled by Tcl code.
326 * If the one of the given signals is caught, it causes a JIM_SIGNAL
327 * exception to be thrown which can be caught by catch.
329 * Use 'signal ignore' to ignore the signal(s)
330 * Use 'signal default' to go back to the default behaviour
331 * Use 'signal throw signal' to raise the given signal
333 * If no arguments are given, returns the list of signals which are being handled
336 * Standard TCL results.
338 *-----------------------------------------------------------------------------
340 static const jim_subcmd_type signal_command_table
[] = {
342 .args
= "?signals ...?",
343 .function
= signal_cmd_handle
,
346 .description
= "Lists handled signals, or adds to handled signals"
349 .args
= "?signals ...?",
350 .function
= signal_cmd_ignore
,
353 .description
= "Lists ignored signals, or adds to ignored signals"
356 .args
= "?signals ...?",
357 .function
= signal_cmd_default
,
360 .description
= "Lists defaulted signals, or adds to defaulted signals"
363 .args
= "?-clear? ?signals ...?",
364 .function
= signal_cmd_check
,
367 .description
= "Returns ignored signals which have occurred, and optionally clearing them"
371 .function
= signal_cmd_throw
,
374 .description
= "Raises the given signal (default SIGINT)"
379 static int Jim_AlarmCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
384 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
391 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
403 ret
= Jim_GetLong(interp
, argv
[1], &t
);
413 static int Jim_SleepCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
418 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
424 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
438 static int Jim_KillCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
445 if (argc
!= 2 && argc
!= 3) {
446 Jim_WrongNumArgs(interp
, 1, argv
, "?SIG|-0? pid");
455 signame
= Jim_GetString(argv
[1], NULL
);
459 /* Special 'kill -0 pid' to determine if a pid exists */
460 if (strcmp(signame
, "-0") == 0 || strcmp(signame
, "0") == 0) {
464 sig
= find_signal_by_name(interp
, signame
);
470 if (Jim_GetLong(interp
, pidObj
, &pid
) != JIM_OK
) {
474 if (kill(pid
, sig
) == 0) {
478 Jim_SetResultString(interp
, "kill: Failed to deliver signal", -1);
482 int Jim_signalInit(Jim_Interp
*interp
)
484 /* Teach the jim core how to set a result from a sigmask */
485 interp
->signal_set_result
= signal_set_sigmask_result
;
487 /* Make sure we know where to store the signals which occur */
488 sigloc
= &interp
->sigmask
;
490 Jim_CreateCommand(interp
, "signal", Jim_SubCmdProc
, (void *)signal_command_table
, NULL
);
491 Jim_CreateCommand(interp
, "alarm", Jim_AlarmCmd
, 0, 0);
492 Jim_CreateCommand(interp
, "kill", Jim_KillCmd
, 0, 0);
494 /* Sleep is slightly dubious here */
495 Jim_CreateCommand(interp
, "sleep", Jim_SleepCmd
, 0, 0);