docs: update documentation for recent changes
[jimtcl.git] / jim-signal.c
bloba819a9208c007dc79796f2a8ca3b23124d59defb
1 /*
2 * jim-signal.c
4 */
6 #include <signal.h>
7 #include <string.h>
8 #include <ctype.h>
9 #include <unistd.h>
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;
20 static struct {
21 int status;
22 const char *name;
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);
48 SET_SIG_NAME(SIGBUS);
49 SET_SIG_NAME(SIGCHLD);
50 SET_SIG_NAME(SIGCONT);
51 SET_SIG_NAME(SIGFPE);
52 SET_SIG_NAME(SIGHUP);
53 SET_SIG_NAME(SIGILL);
54 SET_SIG_NAME(SIGINT);
55 #ifdef SIGIO
56 SET_SIG_NAME(SIGIO);
57 #endif
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);
64 SET_SIG_NAME(SIGSYS);
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);
70 SET_SIG_NAME(SIGURG);
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);
77 #ifdef SIGPWR
78 SET_SIG_NAME(SIGPWR);
79 #endif
80 #ifdef SIGCLD
81 SET_SIG_NAME(SIGCLD);
82 #endif
83 #ifdef SIGEMT
84 SET_SIG_NAME(SIGEMT);
85 #endif
86 #ifdef SIGLOST
87 SET_SIG_NAME(SIGLOST);
88 #endif
89 #ifdef SIGPOLL
90 SET_SIG_NAME(SIGPOLL);
91 #endif
92 #ifdef SIGINFO
93 SET_SIG_NAME(SIGINFO);
94 #endif
98 *----------------------------------------------------------------------
100 * Tcl_SignalId --
102 * Return a textual identifier for a signal number.
104 * Results:
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.
109 * Side effects:
110 * None.
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];
130 #endif
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)
142 int i;
143 const char *pt = name;
145 /* Remove optional - and SIG from the front of the name */
146 if (*pt == '-') {
147 pt++;
149 if (strncasecmp(name, "sig", 3) == 0) {
150 pt += 3;
152 if (isdigit(UCHAR(pt[0]))) {
153 i = atoi(pt);
154 if (i > 0 && i < MAX_SIGNALS) {
155 return i;
158 else {
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) {
164 return i;
168 Jim_SetResultFormatted(interp, "unknown signal %s", name);
170 return -1;
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)
179 struct sigaction sa;
180 int i;
182 if (argc == 0) {
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));
191 return JIM_OK;
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;
200 else {
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]));
209 if (sig < 0) {
210 return JIM_ERR;
212 if (action != siginfo[sig].status) {
213 /* Need to change the action for this signal */
214 switch (action) {
215 case SIGNAL_ACTION_HANDLE:
216 case SIGNAL_ACTION_IGNORE:
217 if (siginfo[sig].status == SIGNAL_ACTION_DEFAULT) {
218 if (!sa_old) {
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]);
224 else {
225 sigaction(sig, &sa, 0);
227 break;
229 case SIGNAL_ACTION_DEFAULT:
230 /* Restore old handler */
231 if (sa_old) {
232 sigaction(sig, &sa_old[sig], 0);
235 siginfo[sig].status = action;
239 return JIM_OK;
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)
259 int i;
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);
268 return JIM_OK;
271 static int signal_cmd_check(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
273 int clear = 0;
274 jim_wide mask = 0;
275 jim_wide blocked;
277 if (argc > 0 && Jim_CompareStringImmediate(interp, argv[0], "-clear")) {
278 clear++;
280 if (argc > clear) {
281 int i;
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) {
288 return -1;
290 mask |= sig_to_bit(sig);
293 else {
294 /* No signals specified, so check/clear all */
295 mask = ~mask;
298 if ((sigsblocked & mask) == 0) {
299 /* No matching signals, so empty result and nothing to do */
300 return JIM_OK;
302 /* Be careful we don't have a race condition where signals are cleared but not returned */
303 blocked = sigsblocked & mask;
304 if (clear) {
305 sigsblocked &= ~blocked;
307 /* Set the result */
308 signal_set_sigmask_result(interp, blocked);
309 return JIM_OK;
312 static int signal_cmd_throw(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
314 int sig = SIGINT;
316 if (argc == 1) {
317 if ((sig = find_signal_by_name(interp, Jim_String(argv[0]))) < 0) {
318 return JIM_ERR;
322 /* If the signal is ignored (blocked) ... */
323 if (siginfo[sig].status == SIGNAL_ACTION_IGNORE) {
324 sigsblocked |= sig_to_bit(sig);
325 return JIM_OK;
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 */
335 return JIM_SIGNAL;
339 *-----------------------------------------------------------------------------
341 * Jim_SignalCmd --
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
356 * Results:
357 * Standard TCL results.
359 *-----------------------------------------------------------------------------
361 static const jim_subcmd_type signal_command_table[] = {
362 { "handle",
363 "?signals ...?",
364 signal_cmd_handle,
367 /* Description: Lists handled signals, or adds to handled signals */
369 { "ignore",
370 "?signals ...?",
371 signal_cmd_ignore,
374 /* Description: Lists ignored signals, or adds to ignored signals */
376 { "default",
377 "?signals ...?",
378 signal_cmd_default,
381 /* Description: Lists defaulted signals, or adds to defaulted signals */
383 { "check",
384 "?-clear? ?signals ...?",
385 signal_cmd_check,
388 /* Description: Returns ignored signals which have occurred, and optionally clearing them */
390 { "throw",
391 "?signal?",
392 signal_cmd_throw,
395 /* Description: Raises the given signal (default SIGINT) */
397 { NULL }
400 static int Jim_AlarmCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
402 int ret;
404 if (argc != 2) {
405 Jim_WrongNumArgs(interp, 1, argv, "seconds");
406 return JIM_ERR;
408 else {
409 #ifdef HAVE_UALARM
410 double t;
412 ret = Jim_GetDouble(interp, argv[1], &t);
413 if (ret == JIM_OK) {
414 if (t < 1) {
415 ualarm(t * 1e6, 0);
417 else {
418 alarm(t);
421 #else
422 long t;
424 ret = Jim_GetLong(interp, argv[1], &t);
425 if (ret == JIM_OK) {
426 alarm(t);
428 #endif
431 return ret;
434 static int Jim_SleepCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
436 int ret;
438 if (argc != 2) {
439 Jim_WrongNumArgs(interp, 1, argv, "seconds");
440 return JIM_ERR;
442 else {
443 double t;
445 ret = Jim_GetDouble(interp, argv[1], &t);
446 if (ret == JIM_OK) {
447 #ifdef HAVE_USLEEP
448 usleep((int)((t - (int)t) * 1e6));
449 #endif
450 sleep(t);
454 return ret;
457 static int Jim_KillCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
459 int sig;
460 long pid;
461 Jim_Obj *pidObj;
462 const char *signame;
464 if (argc != 2 && argc != 3) {
465 Jim_WrongNumArgs(interp, 1, argv, "?SIG|-0? pid");
466 return JIM_ERR;
469 if (argc == 2) {
470 sig = SIGTERM;
471 pidObj = argv[1];
473 else {
474 signame = Jim_String(argv[1]);
475 pidObj = argv[2];
477 /* Special 'kill -0 pid' to determine if a pid exists */
478 if (strcmp(signame, "-0") == 0 || strcmp(signame, "0") == 0) {
479 sig = 0;
481 else {
482 sig = find_signal_by_name(interp, signame);
483 if (sig < 0) {
484 return JIM_ERR;
489 if (Jim_GetLong(interp, pidObj, &pid) != JIM_OK) {
490 return JIM_ERR;
493 if (kill(pid, sig) == 0) {
494 return JIM_OK;
497 Jim_SetResultString(interp, "kill: Failed to deliver signal", -1);
498 return JIM_ERR;
501 int Jim_signalInit(Jim_Interp *interp)
503 if (Jim_PackageProvide(interp, "signal", "1.0", JIM_ERRMSG))
504 return JIM_ERR;
506 signal_init_names();
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);
520 return JIM_OK;