12 #include "jimautoconf.h"
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
)
110 return "unknown signal";
113 const char *Jim_SignalName(int sig
)
115 #ifdef HAVE_SYS_SIGLIST
116 if (sig
>= 0 && sig
< NSIG
) {
117 return sys_siglist
[sig
];
120 return Jim_SignalId(sig
);
124 * Given the name of a signal, returns the signal value if found,
125 * or returns -1 (and sets an error) if not found.
126 * We accept -SIGINT, SIGINT, INT or any lowercase version or a number,
127 * either positive or negative.
129 static int find_signal_by_name(Jim_Interp
*interp
, const char *name
)
132 const char *pt
= name
;
134 /* Remove optional - and SIG from the front of the name */
138 if (strncasecmp(name
, "sig", 3) == 0) {
141 if (isdigit(UCHAR(pt
[0]))) {
143 if (i
> 0 && i
< MAX_SIGNALS
) {
148 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
149 /* Jim_SignalId() returns names such as SIGINT, and
150 * returns "unknown signal id" if unknown, so this will work
152 if (strcasecmp(Jim_SignalId(i
) + 3, pt
) == 0) {
157 Jim_SetResultString(interp
, "unknown signal ", -1);
158 Jim_AppendString(interp
, Jim_GetResult(interp
), name
, -1);
163 #define SIGNAL_ACTION_HANDLE 1
164 #define SIGNAL_ACTION_IGNORE -1
165 #define SIGNAL_ACTION_DEFAULT 0
167 static int do_signal_cmd(Jim_Interp
*interp
, int action
, int argc
, Jim_Obj
*const *argv
)
173 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
174 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
175 if (signal_handling
[i
] == action
) {
176 /* Add signal name to the list */
177 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
178 Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
184 /* Catch all the signals we care about */
185 if (action
!= SIGNAL_ACTION_DEFAULT
) {
187 sigemptyset(&sa
.sa_mask
);
188 if (action
== SIGNAL_ACTION_HANDLE
) {
189 sa
.sa_handler
= signal_handler
;
192 sa
.sa_handler
= signal_ignorer
;
196 /* Iterate through the provided signals */
197 for (i
= 0; i
< argc
; i
++) {
198 int sig
= find_signal_by_name(interp
, Jim_String(argv
[i
]));
203 if (action
!= signal_handling
[sig
]) {
204 /* Need to change the action for this signal */
206 case SIGNAL_ACTION_HANDLE
:
207 case SIGNAL_ACTION_IGNORE
:
208 if (signal_handling
[sig
] == SIGNAL_ACTION_DEFAULT
) {
210 /* Allocate the structure the first time through */
211 sa_old
= Jim_Alloc(sizeof(*sa_old
) * MAX_SIGNALS
);
213 sigaction(sig
, &sa
, &sa_old
[sig
]);
216 sigaction(sig
, &sa
, 0);
220 case SIGNAL_ACTION_DEFAULT
:
221 /* Restore old handler */
223 sigaction(sig
, &sa_old
[sig
], 0);
226 signal_handling
[sig
] = action
;
233 static int signal_cmd_handle(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
235 return do_signal_cmd(interp
, SIGNAL_ACTION_HANDLE
, argc
, argv
);
238 static int signal_cmd_ignore(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
240 return do_signal_cmd(interp
, SIGNAL_ACTION_IGNORE
, argc
, argv
);
243 static int signal_cmd_default(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
245 return do_signal_cmd(interp
, SIGNAL_ACTION_DEFAULT
, argc
, argv
);
248 static int signal_set_sigmask_result(Jim_Interp
*interp
, jim_wide sigmask
)
251 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
253 for (i
= 0; i
< MAX_SIGNALS
; i
++) {
254 if (sigmask
& sig_to_bit(i
)) {
255 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
258 Jim_SetResult(interp
, listObj
);
262 static int signal_cmd_check(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
268 if (argc
> 0 && Jim_CompareStringImmediate(interp
, argv
[0], "-clear")) {
274 /* Signals specified */
275 for (i
= clear
; i
< argc
; i
++) {
276 int sig
= find_signal_by_name(interp
, Jim_String(argv
[i
]));
278 if (sig
< 0 || sig
>= MAX_SIGNALS
) {
281 mask
|= sig_to_bit(sig
);
285 /* No signals specified, so check/clear all */
289 if ((sigsblocked
& mask
) == 0) {
290 /* No matching signals, so empty result and nothing to do */
293 /* Be careful we don't have a race condition where signals are cleared but not returned */
294 blocked
= sigsblocked
& mask
;
296 sigsblocked
&= ~blocked
;
299 signal_set_sigmask_result(interp
, blocked
);
303 static int signal_cmd_throw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
308 if ((sig
= find_signal_by_name(interp
, Jim_String(argv
[0]))) < 0) {
313 /* If the signal is ignored (blocked) ... */
314 if (signal_handling
[sig
] == SIGNAL_ACTION_IGNORE
) {
315 sigsblocked
|= sig_to_bit(sig
);
319 /* Just set the signal */
320 interp
->sigmask
|= sig_to_bit(sig
);
322 /* Set the canonical name of the signal as the result */
323 Jim_SetResultString(interp
, Jim_SignalId(sig
), -1);
325 /* And simply say we caught the signal */
330 *-----------------------------------------------------------------------------
333 * Implements the TCL signal command:
334 * signal handle|ignore|default|throw ?signals ...?
335 * signal throw signal
337 * Specifies which signals are handled by Tcl code.
338 * If the one of the given signals is caught, it causes a JIM_SIGNAL
339 * exception to be thrown which can be caught by catch.
341 * Use 'signal ignore' to ignore the signal(s)
342 * Use 'signal default' to go back to the default behaviour
343 * Use 'signal throw signal' to raise the given signal
345 * If no arguments are given, returns the list of signals which are being handled
348 * Standard TCL results.
350 *-----------------------------------------------------------------------------
352 static const jim_subcmd_type signal_command_table
[] = {
358 /* Description: Lists handled signals, or adds to handled signals */
365 /* Description: Lists ignored signals, or adds to ignored signals */
372 /* Description: Lists defaulted signals, or adds to defaulted signals */
375 "?-clear? ?signals ...?",
379 /* Description: Returns ignored signals which have occurred, and optionally clearing them */
386 /* Description: Raises the given signal (default SIGINT) */
391 static int Jim_AlarmCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
396 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
403 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
415 ret
= Jim_GetLong(interp
, argv
[1], &t
);
425 static int Jim_SleepCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
430 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
436 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
451 static int Jim_KillCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
458 if (argc
!= 2 && argc
!= 3) {
459 Jim_WrongNumArgs(interp
, 1, argv
, "?SIG|-0? pid");
468 signame
= Jim_String(argv
[1]);
472 /* Special 'kill -0 pid' to determine if a pid exists */
473 if (strcmp(signame
, "-0") == 0 || strcmp(signame
, "0") == 0) {
477 sig
= find_signal_by_name(interp
, signame
);
483 if (Jim_GetLong(interp
, pidObj
, &pid
) != JIM_OK
) {
487 if (kill(pid
, sig
) == 0) {
491 Jim_SetResultString(interp
, "kill: Failed to deliver signal", -1);
495 int Jim_signalInit(Jim_Interp
*interp
)
497 if (Jim_PackageProvide(interp
, "signal", "1.0", JIM_ERRMSG
))
500 /* Teach the jim core how to set a result from a sigmask */
501 interp
->signal_set_result
= signal_set_sigmask_result
;
503 /* Make sure we know where to store the signals which occur */
504 sigloc
= &interp
->sigmask
;
506 Jim_CreateCommand(interp
, "signal", Jim_SubCmdProc
, (void *)signal_command_table
, NULL
);
507 Jim_CreateCommand(interp
, "alarm", Jim_AlarmCmd
, 0, 0);
508 Jim_CreateCommand(interp
, "kill", Jim_KillCmd
, 0, 0);
510 /* Sleep is slightly dubious here */
511 Jim_CreateCommand(interp
, "sleep", Jim_SleepCmd
, 0, 0);