11 #include "jimautoconf.h"
12 #include <jim-subcmd.h>
13 #include <jim-signal.h>
15 #define MAX_SIGNALS (sizeof(jim_wide) * 8)
17 static jim_wide
*sigloc
;
18 static jim_wide sigsblocked
;
19 static struct sigaction
*sa_old
;
23 } siginfo
[MAX_SIGNALS
];
25 /* Make sure to do this as a wide, not int */
26 #define sig_to_bit(SIG) ((jim_wide)1 << (SIG))
28 static void signal_handler(int sig
)
30 /* We just remember which signals occurred. Jim_Eval() will
31 * notice this as soon as it can and throw an error
33 *sigloc
|= sig_to_bit(sig
);
36 static void signal_ignorer(int sig
)
38 /* We just remember which signals occurred */
39 sigsblocked
|= sig_to_bit(sig
);
42 static void signal_init_names(void)
44 #define SET_SIG_NAME(SIG) siginfo[SIG].name = #SIG
46 SET_SIG_NAME(SIGABRT
);
47 SET_SIG_NAME(SIGALRM
);
49 SET_SIG_NAME(SIGCHLD
);
50 SET_SIG_NAME(SIGCONT
);
58 SET_SIG_NAME(SIGKILL
);
59 SET_SIG_NAME(SIGPIPE
);
60 SET_SIG_NAME(SIGPROF
);
61 SET_SIG_NAME(SIGQUIT
);
62 SET_SIG_NAME(SIGSEGV
);
63 SET_SIG_NAME(SIGSTOP
);
65 SET_SIG_NAME(SIGTERM
);
66 SET_SIG_NAME(SIGTRAP
);
67 SET_SIG_NAME(SIGTSTP
);
68 SET_SIG_NAME(SIGTTIN
);
69 SET_SIG_NAME(SIGTTOU
);
71 SET_SIG_NAME(SIGUSR1
);
72 SET_SIG_NAME(SIGUSR2
);
73 SET_SIG_NAME(SIGVTALRM
);
74 SET_SIG_NAME(SIGWINCH
);
75 SET_SIG_NAME(SIGXCPU
);
76 SET_SIG_NAME(SIGXFSZ
);
87 SET_SIG_NAME(SIGLOST
);
90 SET_SIG_NAME(SIGPOLL
);
93 SET_SIG_NAME(SIGINFO
);
98 *----------------------------------------------------------------------
102 * Return a textual identifier for a signal number.
105 * This procedure returns a machine-readable textual identifier
106 * that corresponds to sig. The identifier is the same as the
107 * #define name in signal.h.
112 *----------------------------------------------------------------------
114 const char *Jim_SignalId(int sig
)
116 if (sig
>=0 && sig
< MAX_SIGNALS
) {
117 if (siginfo
[sig
].name
) {
118 return siginfo
[sig
].name
;
121 return "unknown signal";
124 const char *Jim_SignalName(int sig
)
126 #ifdef HAVE_SYS_SIGLIST
127 if (sig
>= 0 && sig
< NSIG
) {
128 return sys_siglist
[sig
];
131 return Jim_SignalId(sig
);
135 * Given the name of a signal, returns the signal value if found,
136 * or returns -1 (and sets an error) if not found.
137 * We accept -SIGINT, SIGINT, INT or any lowercase version or a number,
138 * either positive or negative.
140 static int find_signal_by_name(Jim_Interp
*interp
, const char *name
)
143 const char *pt
= name
;
145 /* Remove optional - and SIG from the front of the name */
149 if (strncasecmp(name
, "sig", 3) == 0) {
152 if (isdigit(UCHAR(pt
[0]))) {
154 if (i
> 0 && i
< MAX_SIGNALS
) {
159 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
160 /* Jim_SignalId() returns names such as SIGINT, and
161 * returns "unknown signal" if unknown, so this will work
163 if (strcasecmp(Jim_SignalId(i
) + 3, pt
) == 0) {
168 Jim_SetResultFormatted(interp
, "unknown signal %s", name
);
173 #define SIGNAL_ACTION_HANDLE 1
174 #define SIGNAL_ACTION_IGNORE -1
175 #define SIGNAL_ACTION_DEFAULT 0
177 static int do_signal_cmd(Jim_Interp
*interp
, int action
, int argc
, Jim_Obj
*const *argv
)
183 Jim_SetResult(interp
, Jim_NewListObj(interp
, NULL
, 0));
184 for (i
= 1; i
< MAX_SIGNALS
; i
++) {
185 if (siginfo
[i
].status
== action
) {
186 /* Add signal name to the list */
187 Jim_ListAppendElement(interp
, Jim_GetResult(interp
),
188 Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
194 /* Catch all the signals we care about */
195 if (action
!= SIGNAL_ACTION_DEFAULT
) {
196 memset(&sa
, 0, sizeof(sa
));
197 if (action
== SIGNAL_ACTION_HANDLE
) {
198 sa
.sa_handler
= signal_handler
;
201 sa
.sa_handler
= signal_ignorer
;
205 /* Iterate through the provided signals */
206 for (i
= 0; i
< argc
; i
++) {
207 int sig
= find_signal_by_name(interp
, Jim_String(argv
[i
]));
212 if (action
!= siginfo
[sig
].status
) {
213 /* Need to change the action for this signal */
215 case SIGNAL_ACTION_HANDLE
:
216 case SIGNAL_ACTION_IGNORE
:
217 if (siginfo
[sig
].status
== SIGNAL_ACTION_DEFAULT
) {
219 /* Allocate the structure the first time through */
220 sa_old
= Jim_Alloc(sizeof(*sa_old
) * MAX_SIGNALS
);
222 sigaction(sig
, &sa
, &sa_old
[sig
]);
225 sigaction(sig
, &sa
, 0);
229 case SIGNAL_ACTION_DEFAULT
:
230 /* Restore old handler */
232 sigaction(sig
, &sa_old
[sig
], 0);
235 siginfo
[sig
].status
= action
;
242 static int signal_cmd_handle(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
244 return do_signal_cmd(interp
, SIGNAL_ACTION_HANDLE
, argc
, argv
);
247 static int signal_cmd_ignore(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
249 return do_signal_cmd(interp
, SIGNAL_ACTION_IGNORE
, argc
, argv
);
252 static int signal_cmd_default(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
254 return do_signal_cmd(interp
, SIGNAL_ACTION_DEFAULT
, argc
, argv
);
257 static int signal_set_sigmask_result(Jim_Interp
*interp
, jim_wide sigmask
)
260 Jim_Obj
*listObj
= Jim_NewListObj(interp
, NULL
, 0);
262 for (i
= 0; i
< MAX_SIGNALS
; i
++) {
263 if (sigmask
& sig_to_bit(i
)) {
264 Jim_ListAppendElement(interp
, listObj
, Jim_NewStringObj(interp
, Jim_SignalId(i
), -1));
267 Jim_SetResult(interp
, listObj
);
271 static int signal_cmd_check(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
277 if (argc
> 0 && Jim_CompareStringImmediate(interp
, argv
[0], "-clear")) {
283 /* Signals specified */
284 for (i
= clear
; i
< argc
; i
++) {
285 int sig
= find_signal_by_name(interp
, Jim_String(argv
[i
]));
287 if (sig
< 0 || sig
>= MAX_SIGNALS
) {
290 mask
|= sig_to_bit(sig
);
294 /* No signals specified, so check/clear all */
298 if ((sigsblocked
& mask
) == 0) {
299 /* No matching signals, so empty result and nothing to do */
302 /* Be careful we don't have a race condition where signals are cleared but not returned */
303 blocked
= sigsblocked
& mask
;
305 sigsblocked
&= ~blocked
;
308 signal_set_sigmask_result(interp
, blocked
);
312 static int signal_cmd_throw(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
317 if ((sig
= find_signal_by_name(interp
, Jim_String(argv
[0]))) < 0) {
322 /* If the signal is ignored (blocked) ... */
323 if (siginfo
[sig
].status
== SIGNAL_ACTION_IGNORE
) {
324 sigsblocked
|= sig_to_bit(sig
);
328 /* Just set the signal */
329 interp
->sigmask
|= sig_to_bit(sig
);
331 /* Set the canonical name of the signal as the result */
332 Jim_SetResultString(interp
, Jim_SignalId(sig
), -1);
334 /* And simply say we caught the signal */
339 *-----------------------------------------------------------------------------
342 * Implements the TCL signal command:
343 * signal handle|ignore|default|throw ?signals ...?
344 * signal throw signal
346 * Specifies which signals are handled by Tcl code.
347 * If the one of the given signals is caught, it causes a JIM_SIGNAL
348 * exception to be thrown which can be caught by catch.
350 * Use 'signal ignore' to ignore the signal(s)
351 * Use 'signal default' to go back to the default behaviour
352 * Use 'signal throw signal' to raise the given signal
354 * If no arguments are given, returns the list of signals which are being handled
357 * Standard TCL results.
359 *-----------------------------------------------------------------------------
361 static const jim_subcmd_type signal_command_table
[] = {
367 /* Description: Lists handled signals, or adds to handled signals */
374 /* Description: Lists ignored signals, or adds to ignored signals */
381 /* Description: Lists defaulted signals, or adds to defaulted signals */
384 "?-clear? ?signals ...?",
388 /* Description: Returns ignored signals which have occurred, and optionally clearing them */
395 /* Description: Raises the given signal (default SIGINT) */
400 static int Jim_AlarmCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
405 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
412 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
424 ret
= Jim_GetLong(interp
, argv
[1], &t
);
434 static int Jim_SleepCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
439 Jim_WrongNumArgs(interp
, 1, argv
, "seconds");
445 ret
= Jim_GetDouble(interp
, argv
[1], &t
);
448 usleep((int)((t
- (int)t
) * 1e6
));
457 static int Jim_KillCmd(Jim_Interp
*interp
, int argc
, Jim_Obj
*const *argv
)
464 if (argc
!= 2 && argc
!= 3) {
465 Jim_WrongNumArgs(interp
, 1, argv
, "?SIG|-0? pid");
474 signame
= Jim_String(argv
[1]);
477 /* Special 'kill -0 pid' to determine if a pid exists */
478 if (strcmp(signame
, "-0") == 0 || strcmp(signame
, "0") == 0) {
482 sig
= find_signal_by_name(interp
, signame
);
489 if (Jim_GetLong(interp
, pidObj
, &pid
) != JIM_OK
) {
493 if (kill(pid
, sig
) == 0) {
497 Jim_SetResultString(interp
, "kill: Failed to deliver signal", -1);
501 int Jim_signalInit(Jim_Interp
*interp
)
503 if (Jim_PackageProvide(interp
, "signal", "1.0", JIM_ERRMSG
))
508 /* Teach the jim core how to set a result from a sigmask */
509 interp
->signal_set_result
= signal_set_sigmask_result
;
511 /* Make sure we know where to store the signals which occur */
512 sigloc
= &interp
->sigmask
;
514 Jim_CreateCommand(interp
, "signal", Jim_SubCmdProc
, (void *)signal_command_table
, NULL
);
515 Jim_CreateCommand(interp
, "alarm", Jim_AlarmCmd
, 0, 0);
516 Jim_CreateCommand(interp
, "kill", Jim_KillCmd
, 0, 0);
518 /* Sleep is slightly dubious here */
519 Jim_CreateCommand(interp
, "sleep", Jim_SleepCmd
, 0, 0);