13 #include "jimautoconf.h"
14 #include "jim-subcmd.h"
15 #include "jim-signal.h"
17 #define MAX_SIGNALS (sizeof(jim_wide) * 8)
19 static jim_wide
*sigloc
;
20 static jim_wide sigsblocked
;
21 static struct sigaction
*sa_old
;
22 static int signal_handling
[MAX_SIGNALS
];
24 /* Make sure to do this as a wide, not int */
25 #define sig_to_bit(SIG) ((jim_wide)1 << (SIG))
27 static void signal_handler(int sig
)
29 /* We just remember which signals occurred. Jim_Eval() will
30 * notice this as soon as it can and throw an error
32 *sigloc
|= sig_to_bit(sig
);
35 static void signal_ignorer(int sig
)
37 /* We just remember which signals occurred */
38 sigsblocked
|= sig_to_bit(sig
);
42 *----------------------------------------------------------------------
46 * Return a textual identifier for a signal number.
49 * This procedure returns a machine-readable textual identifier
50 * that corresponds to sig. The identifier is the same as the
51 * #define name in signal.h.
56 *----------------------------------------------------------------------
58 #define CHECK_SIG(NAME) if (sig == NAME) return #NAME
60 const char *Jim_SignalId(int sig
)
111 return "unknown signal";
114 const char *Jim_SignalName(int sig
)
116 #ifdef HAVE_SYS_SIGLIST
117 if (sig
>= 0 && sig
< NSIG
) {
118 return sys_siglist
[sig
];
121 return Jim_SignalId(sig
);
125 * Given the name of a signal, returns the signal value if found,
126 * or returns -1 (and sets an error) if not found.
127 * We accept -SIGINT, SIGINT, INT or any lowercase version or a number,
128 * either positive or negative.
130 static int find_signal_by_name(Jim_Interp
*interp
, const char *name
)
133 const char *pt
= name
;
135 /* Remove optional - and SIG from the front of the name */
139 if (strncasecmp(name
, "sig", 3) == 0) {
142 if (isdigit(UCHAR(pt
[0]))) {
144 if (i
> 0 && i
< MAX_SIGNALS
) {
149 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
150 /* Jim_SignalId() returns names such as SIGINT, and
151 * returns "unknown signal id" if unknown, so this will work
153 if (strcasecmp(Jim_SignalId(i
) + 3, pt
) == 0) {
158 Jim_SetResultString(interp
, "unknown signal ", -1);
159 Jim_AppendString(interp
, Jim_GetResult(interp
), name
, -1);
164 #define SIGNAL_ACTION_HANDLE 1
165 #define SIGNAL_ACTION_IGNORE -1
166 #define SIGNAL_ACTION_DEFAULT 0
168 static int do_signal_cmd(Jim_Interp
*interp
, int action
, int argc
, Jim_Obj
*const *argv
)
174 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
175 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
176 if (signal_handling
[i
] == action
) {
177 /* Add signal name to the list */
178 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
179 Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
185 /* Catch all the signals we care about */
186 if (action
!= SIGNAL_ACTION_DEFAULT
) {
188 sigemptyset(&sa
.sa_mask
);
189 if (action
== SIGNAL_ACTION_HANDLE
) {
190 sa
.sa_handler
= signal_handler
;
193 sa
.sa_handler
= signal_ignorer
;
197 /* Iterate through the provided signals */
198 for (i
= 0; i
< argc
; i
++) {
199 int sig
= find_signal_by_name(interp
, Jim_String(argv
[i
]));
204 if (action
!= signal_handling
[sig
]) {
205 /* Need to change the action for this signal */
207 case SIGNAL_ACTION_HANDLE
:
208 case SIGNAL_ACTION_IGNORE
:
209 if (signal_handling
[sig
] == SIGNAL_ACTION_DEFAULT
) {
211 /* Allocate the structure the first time through */
212 sa_old
= Jim_Alloc(sizeof(*sa_old
) * MAX_SIGNALS
);
214 sigaction(sig
, &sa
, &sa_old
[sig
]);
217 sigaction(sig
, &sa
, 0);
221 case SIGNAL_ACTION_DEFAULT
:
222 /* Restore old handler */
224 sigaction(sig
, &sa_old
[sig
], 0);
227 signal_handling
[sig
] = action
;
234 static int signal_cmd_handle(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
236 return do_signal_cmd(interp
, SIGNAL_ACTION_HANDLE
, argc
, argv
);
239 static int signal_cmd_ignore(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
241 return do_signal_cmd(interp
, SIGNAL_ACTION_IGNORE
, argc
, argv
);
244 static int signal_cmd_default(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
246 return do_signal_cmd(interp
, SIGNAL_ACTION_DEFAULT
, argc
, argv
);
249 static int signal_set_sigmask_result(Jim_Interp
*interp
, jim_wide sigmask
)
252 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
254 for (i
= 0; i
< MAX_SIGNALS
; i
++) {
255 if (sigmask
& sig_to_bit(i
)) {
256 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
259 Jim_SetResult(interp
, listObj
);
263 static int signal_cmd_check(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
269 if (argc
> 0 && Jim_CompareStringImmediate(interp
, argv
[0], "-clear")) {
275 /* Signals specified */
276 for (i
= clear
; i
< argc
; i
++) {
277 int sig
= find_signal_by_name(interp
, Jim_String(argv
[i
]));
279 if (sig
< 0 || sig
>= MAX_SIGNALS
) {
282 mask
|= sig_to_bit(sig
);
286 /* No signals specified, so check/clear all */
290 if ((sigsblocked
& mask
) == 0) {
291 /* No matching signals, so empty result and nothing to do */
294 /* Be careful we don't have a race condition where signals are cleared but not returned */
295 blocked
= sigsblocked
& mask
;
297 sigsblocked
&= ~blocked
;
300 signal_set_sigmask_result(interp
, blocked
);
304 static int signal_cmd_throw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
309 if ((sig
= find_signal_by_name(interp
, Jim_String(argv
[0]))) < 0) {
314 /* If the signal is ignored (blocked) ... */
315 if (signal_handling
[sig
] == SIGNAL_ACTION_IGNORE
) {
316 sigsblocked
|= sig_to_bit(sig
);
320 /* Just set the signal */
321 interp
->sigmask
|= sig_to_bit(sig
);
323 /* Set the canonical name of the signal as the result */
324 Jim_SetResultString(interp
, Jim_SignalId(sig
), -1);
326 /* And simply say we caught the signal */
331 *-----------------------------------------------------------------------------
334 * Implements the TCL signal command:
335 * signal handle|ignore|default|throw ?signals ...?
336 * signal throw signal
338 * Specifies which signals are handled by Tcl code.
339 * If the one of the given signals is caught, it causes a JIM_SIGNAL
340 * exception to be thrown which can be caught by catch.
342 * Use 'signal ignore' to ignore the signal(s)
343 * Use 'signal default' to go back to the default behaviour
344 * Use 'signal throw signal' to raise the given signal
346 * If no arguments are given, returns the list of signals which are being handled
349 * Standard TCL results.
351 *-----------------------------------------------------------------------------
353 static const jim_subcmd_type signal_command_table
[] = {
355 .args
= "?signals ...?",
356 .function
= signal_cmd_handle
,
359 .description
= "Lists handled signals, or adds to handled signals"
362 .args
= "?signals ...?",
363 .function
= signal_cmd_ignore
,
366 .description
= "Lists ignored signals, or adds to ignored signals"
369 .args
= "?signals ...?",
370 .function
= signal_cmd_default
,
373 .description
= "Lists defaulted signals, or adds to defaulted signals"
376 .args
= "?-clear? ?signals ...?",
377 .function
= signal_cmd_check
,
380 .description
= "Returns ignored signals which have occurred, and optionally clearing them"
384 .function
= signal_cmd_throw
,
387 .description
= "Raises the given signal (default SIGINT)"
392 static int Jim_AlarmCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
397 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
404 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
416 ret
= Jim_GetLong(interp
, argv
[1], &t
);
426 static int Jim_SleepCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
431 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
437 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
452 static int Jim_KillCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
459 if (argc
!= 2 && argc
!= 3) {
460 Jim_WrongNumArgs(interp
, 1, argv
, "?SIG|-0? pid");
469 signame
= Jim_String(argv
[1]);
473 /* Special 'kill -0 pid' to determine if a pid exists */
474 if (strcmp(signame
, "-0") == 0 || strcmp(signame
, "0") == 0) {
478 sig
= find_signal_by_name(interp
, signame
);
484 if (Jim_GetLong(interp
, pidObj
, &pid
) != JIM_OK
) {
488 if (kill(pid
, sig
) == 0) {
492 Jim_SetResultString(interp
, "kill: Failed to deliver signal", -1);
496 int Jim_signalInit(Jim_Interp
*interp
)
498 if (Jim_PackageProvide(interp
, "signal", "1.0", JIM_ERRMSG
))
501 /* Teach the jim core how to set a result from a sigmask */
502 interp
->signal_set_result
= signal_set_sigmask_result
;
504 /* Make sure we know where to store the signals which occur */
505 sigloc
= &interp
->sigmask
;
507 Jim_CreateCommand(interp
, "signal", Jim_SubCmdProc
, (void *)signal_command_table
, NULL
);
508 Jim_CreateCommand(interp
, "alarm", Jim_AlarmCmd
, 0, 0);
509 Jim_CreateCommand(interp
, "kill", Jim_KillCmd
, 0, 0);
511 /* Sleep is slightly dubious here */
512 Jim_CreateCommand(interp
, "sleep", Jim_SleepCmd
, 0, 0);