summaryrefslogtreecommitdiffstats
path: root/tclsignal/signal_ext.c
blob: e478d02030467fb4489782d593709d3a9d59ffda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/*
 * Smithsonian Astrophysical Observatory, Cambridge, MA, USA
 * This code has been modified under the terms listed below and is made
 * available under the same terms.
 */

/*****************************************************************
** Signal Extension for Tcl/Tk
** $Header$
** Author: Michael I. Schwartz, SCCS, mschwart@nyx.net, http://www.nyx.net/~mschwart
** This extension adds signal handling to Tcl/Tk scripts.
** It has only been tested under Unix.
**
** To ensure that signal handling does _not_ happen asynchronously,
** and only occurs in the X loop, the signal handler writes the
** signal number on a pipe, which is handled through the Tcl
** file handling mechanism (like XtAppAddInput in X).
**
**  Copyright (C) 1996 Schwartz Computer Consulting Services
** 
**  Permission to use, copy, modify, distribute, and sell this software and its
**  documentation for any purpose is hereby granted without fee, provided that
**  the above copyright notice appear in all copies and that both that
**  copyright notice and this permission notice appear in supporting
**  documentation, and that the name of SCCS not be used in advertising or
**  publicity pertaining to distribution of the software without specific,
**  written prior permission.  SCCS makes no representations about the
**  suitability of this software for any purpose.  It is provided "as is"
**  without express or implied warranty.
** 
*****************************************************************/
#include <tcl.h>

#include <signal.h> /* for sigaction() and struct sigaction */
#include <string.h> /* For strcmp(), strerror() */
#include <errno.h>  /* for errno */
#include <ctype.h>  /* for isdigit() */
#include <stdlib.h> /* for allocation and freeing */
#include <unistd.h> /* for pipe(), read(), write() */

/* Structure for remembering signal procs */
/* Changed to add 3 members for asynchronous signal handling */
static struct
{
  int  is_handled;
  char *signal_proc;
  int  is_async;
  Tcl_AsyncHandler async;
  Tcl_Interp* save_interp; /* Used as a fallback interpreter for async */
} signal_handlers[NSIG];

/* Now for the complicated part: Getting a lookup structure
** to manage a few of the most commonly used signals:
** Don't bother with KILL and STOP--they aren't allowed anyway
*/
static const struct
{
  int signum;
  char *signame;
} signal_name_mapping[] =
{
#ifdef SIGHUP
  { SIGHUP, "SIGHUP" },
#endif
#ifdef SIGINT
  { SIGINT, "SIGINT" },
#endif
#ifdef SIGABRT
  { SIGABRT, "SIGABRT" },
#endif
#ifdef SIGFPE
  { SIGFPE, "SIGFPE" },
#endif
#ifdef SIGILL
  { SIGILL, "SIGILL" },
#endif
#ifdef SIGSEGV
  { SIGSEGV, "SIGSEGV" },
#endif
#ifdef SIGTERM
  { SIGTERM, "SIGTERM" },
#endif
#ifdef SIGUSR1
  { SIGUSR1, "SIGUSR1" },
#endif
#ifdef SIGUSR2
  { SIGUSR2, "SIGUSR2" },
#endif
#ifdef SIGUSR3
  { SIGUSR3, "SIGUSR3" },
#endif
#ifdef SIGBREAK
  { SIGBREAK, "SIGBREAK" },
#endif
#ifdef SIGCHLD
  { SIGCHLD, "SIGCHLD" },
#endif
#ifdef SIGTERM
  { SIGTERM, "SIGTERM"},
#endif
#ifdef SIGURG
  { SIGURG, "SIGURG"},
#endif
#ifdef SIGIO
  { SIGIO, "SIGIO" },
#endif
#ifdef SIGWAITING
  { SIGWAITING, "SIGWAITING" },
#endif
#ifdef SIGPOLL
  { SIGPOLL, "SIGPOLL" },
#endif
#ifdef SIGQUIT
  { SIGQUIT, "SIGQUIT" },
#endif
#ifdef SIGBUS
  { SIGBUS, "SIGBUS" },
#endif
#ifdef SIGSYS
  { SIGSYS, "SIGSYS" },
#endif
#ifdef SIGPIPE
  { SIGPIPE, "SIGPIPE" },
#endif
#ifdef SIGALRM
  { SIGALRM,  "SIGALRM" },
#endif
#ifdef SIGTSTP
  { SIGTSTP, "SIGTSTP" },
#endif
#ifdef SIGCONT
  { SIGCONT, "SIGCONT" },
#endif
#ifdef SIGTTIN
  { SIGTTIN, "SIGTTIN" },
#endif
#ifdef SIGTTOU
  { SIGTTOU, "SIGTTOU" },
#endif
#ifdef SIGWINCH
  { SIGWINCH, "SIGWINCH" },
#endif
};

/* A lookup function for the signal names to numbers and vice versa */
static const char *signo_to_signame(int signo);
static int signame_to_signo(const char *signame);
static int signal_spec(const char *arg);
static const char * signal_name(int i);

/*
** A pair of ints for a pipe between the signal handler and the 
** X main loop
** To support multiple interpreters, these should probably be associated
** with the interpreter, and not with the program as a whole.
*/
static int fds[2];

/* The actual signal handler declarations */
static void handle_sig(int signo);
static void handle_async_signal (int sig);

/* A Tcl_AsyncHandler */
static int handle_async (ClientData clientData, Tcl_Interp *interp, int code);

/* A Tcl_FileProc */
static void HandleSignalPipe (ClientData d, int mask);

/* Tcl_CmdProc declaration(s) */
static int DoSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, const char *argv[]);
static int AddSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, char *argv[]);
static int DeleteSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, char *argv[]);
static int PrintSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, char *argv[]);

/**
** Goal: add signal handling to Tcl/Tk scripts.
** Do this by using a channel, writing the signal to the channel,
** and having the channel reader interpret the signal number
** and call the appropriate handler.
** Use POSIX signal semantics by using the sigaction structure.
**/
int Signal_ext_Init ( Tcl_Interp *interp )
{
  static int initialized = 0;

  if (Tcl_InitStubs(interp, TCL_PATCH_LEVEL, 0) == NULL)
    return TCL_ERROR;

  if (Tcl_PkgProvide(interp, PACKAGE_NAME, PACKAGE_VERSION) != TCL_OK)
    return TCL_ERROR;

  if (initialized == 0)
  {
    initialized = 1;

    /* Open the pipe */
    pipe(fds);

    /* Create a Tcl-compliant handler for the pipe */
    Tcl_CreateFileHandler(fds[0], TCL_READABLE, HandleSignalPipe, 
                         (ClientData)interp);

    /* Add the signal command */
    Tcl_CreateCommand(interp, "signal", DoSignalHandler,
		      (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
  }

  return 0;
}

int Signal_Init ( Tcl_Interp *interp )
{
  return Signal_ext_Init(interp);
}

int Signal_ext_SafeInit ( Tcl_Interp *interp )
{
  return Signal_ext_Init(interp);
}

static void HandleSignalPipe (ClientData d, int mask)
{
  int sig;
  int result;
  char *command;
  Tcl_Interp *i = (Tcl_Interp *)d;

  /* Get the signal that caused the handler to fire */
  if ( (result = read(fds[0], &sig, sizeof(sig))) <= 0 )
  {
    fprintf(stderr, "False alarm in Signal package!\n");
    return;
  }

  if ( sig <= 0 || sig > NSIG )
  {
    fprintf(stderr, "Bad signal %d received by Signal package!\n", sig);
    return;
  }

  /* Look up the name in the process structure for signal sig */
  command = signal_handlers[sig].signal_proc;

  /* If it is valid, Eval it */
  if (command)
  {
    Tcl_Eval(i, command);
  }
}

static char Usage[] = "Usage: signal add signo proc [-async]| "
                      "signal delete signo | "
                      "signal print [signo] | "
		      "signal version";

static int DoSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, const char *argvv[])
{
  char** argv = (char**)argvv;

  /* argv[0] is "signal" */
  if (argc < 2)
  {
    Tcl_SetResult (i, Usage, TCL_STATIC);
    return TCL_ERROR;
  }
  if ( strcmp(argv[1], "add") == 0 )
    return AddSignalHandler(d,i,argc-1,&argv[1]);
  else if ( strcmp(argv[1], "delete") == 0 )
    return DeleteSignalHandler(d,i,argc-1,&argv[1]);
  else if ( strcmp(argv[1], "print" ) == 0 )
    return PrintSignalHandler(d, i, argc-1, &argv[1]);
  else if ( strcmp(argv[1], "version" ) == 0 )
  {
     Tcl_SetResult (i, PACKAGE_VERSION, TCL_STATIC);
     return TCL_OK;
  }
  else
  {
    Tcl_SetResult (i, Usage, TCL_STATIC);
    return TCL_ERROR;
  }
}

static int AddSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, char *argv[])
{
  int sig;
  char *procname;
  struct sigaction sa;
  int async = 0;

  if (argc == 4 && strcmp(argv[3], "-async") == 0 )
    async = 1;
  else if (argc != 3 )
  {
    Tcl_SetResult (i, "Usage: signal add signo proc [-async]", TCL_STATIC);
    return TCL_ERROR;
  }
  
  sig = signal_spec(argv[1]);
  procname = argv[2];
  if ( sig <= 0 || sig > NSIG )
  {
    Tcl_AppendResult (i, "Signal ", argv[1], " not understood or out of range\n"
			 "Usage: signal add signo proc", 0);
    return TCL_ERROR;
  }

  /* Free any old process name in the structure */
  if (signal_handlers[sig].signal_proc)
  {
    ckfree(signal_handlers[sig].signal_proc);
    if (signal_handlers[sig].is_async)
    {
      Tcl_AsyncDelete(signal_handlers[sig].async);
      signal_handlers[sig].is_async = 0;
      signal_handlers[sig].save_interp = 0;
    }
  }

  /* Dup the process name into the structure */
  if ((signal_handlers[sig].signal_proc = (char *)ckalloc(strlen(procname)+1)))
    strcpy(signal_handlers[sig].signal_proc, procname);
  else
    signal_handlers[sig].signal_proc = 0;

  /* Set up the signal handler: ignore sa_sigaction since flags will be 0 */
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);

  if (async)
  {
    long bar = sig;
    signal_handlers[sig].async = Tcl_AsyncCreate(handle_async,
						 (ClientData)bar);
    sa.sa_handler = handle_async_signal;
    signal_handlers[sig].save_interp = i;
  }
  else /* Normal handler */
  {
    sa.sa_handler = handle_sig;
  }
  
  if ( sigaction (sig, &sa, 0) == -1 )
  {
    Tcl_AppendResult(i, "Error in sigaction: ", strerror(errno), 0);
    return TCL_ERROR;
  }

  /* Remember we're handling it */
  signal_handlers[sig].is_handled = 1;
  signal_handlers[sig].is_async   = async;
  
  return TCL_OK;
}

static int DeleteSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, char *argv[])
{
  int sig;
  struct sigaction sa;

  if (argc != 2)
  {
    Tcl_SetResult (i, "Usage: signal delete signo", TCL_STATIC);
    return TCL_ERROR;
  }
  sig = signal_spec(argv[1]);

  if (sig <= 0 || sig > NSIG )
  {
    Tcl_AppendResult (i, "Signal ", argv[1], " not understood or out of range\n"
			 "Usage: signal delete signo", 0);
    return TCL_ERROR;
  }

  /* Deinstall the signal handler */
  sa.sa_handler = SIG_DFL;
  sa.sa_flags = 0;
  sigemptyset(&sa.sa_mask);
  sigaction (sig, &sa, 0);

  /* Free any old process name in the structure */
  if (signal_handlers[sig].signal_proc)
  {
    ckfree(signal_handlers[sig].signal_proc);
    if (signal_handlers[sig].is_async)
    {
      Tcl_AsyncDelete(signal_handlers[sig].async);
      signal_handlers[sig].is_async = 0;
      signal_handlers[sig].save_interp = 0;
    }
  }

  /* Zero out the process name in the structure */
  signal_handlers[sig].signal_proc = 0;
  
  /* Remember we're not handling it */
  signal_handlers[sig].is_handled = 0;
  return TCL_OK;
}

static int PrintSignalHandler (ClientData d, Tcl_Interp *i, 
				int argc, char *argv[])
{
  int sig;
  char *command;
  if ( argc < 1 )
  {
    Tcl_SetResult (i, "Usage: signal print [signo]", TCL_STATIC);
    return TCL_ERROR;
  }

  if ( argc == 1 ) /* Just print-- print all of them */
  {
    int j;
    int reported = 0; /* To avoid extra newlines */

    for (j=0; j < NSIG; j++)
    {
      const char *cp = signal_handlers[j].signal_proc;

      if (signal_handlers[j].is_handled)
      {
	if (reported == 0)
	  reported = 1;
	else
	  Tcl_AppendResult(i, "\n", 0);
	if (signal_handlers[j].is_async)
          Tcl_AppendResult(i, signal_name(j), " !---> ", cp?cp:" ", 0);
	else
          Tcl_AppendResult(i, signal_name(j), " ----> ", cp?cp:" ", 0);
      }
    }
    return TCL_OK;
  }

  /* Just 1 signal */
  sig = signal_spec(argv[1]);
  
  if ( sig > 0 && sig < NSIG )
  {
    if ( signal_handlers[sig].is_handled == 0 )
      command = "UNHANDLED";
    else if ( (command = signal_handlers[sig].signal_proc ) == 0 || 
	       command[0] == '\0')
      command = " ";
    Tcl_SetResult (i, command, 0);
    return TCL_OK;
  }
  else
  {
    Tcl_AppendResult (i, "Signal ", argv[1], " not understood or out of range\n"
			 "Usage: signal print [signo]", 0);
    return TCL_ERROR;
  }
}

/* The actual signal handler */
static void handle_sig(int signo)
{
  write(fds[1], &signo, sizeof(signo));
}

/* An async proc handler */
static int handle_async (ClientData clientData, Tcl_Interp *i, int code)
{
  /* There is a fundamental difficulty in that usually there seems to 
     be no interpreter available when this function is called.
     This means that scripts will generally not be invoked if they depend
     on the interp variable passed in.
     The choices are 
     (a) to allow only a few things to happen in an async handler,
         e.g., exit or raise an error
     (b) use the interpreter that was active when the handler was created
         to invoke the scripts
     Neither seems particularly appealing.
     I chose (b).
     I'm sure I will receive appropriate comments indicating why a different
     choice would be better. Perhaps the next release will have it righter...
  */
  int sig = (int)clientData;
  int tcode;
  Tcl_DString result;
  char * errorcode = 0;
  char * errorinfo = 0;
  Tcl_Interp *interp = i;

  if ( sig <= 0 || sig > NSIG ) /* Oh, oh! Don't even try! */
  {
    fprintf(stderr, "Bad async signal %d received by Signal package!\n", sig);
    return code;
  }

  if (interp == 0 )
    interp = signal_handlers[sig].save_interp;

  Tcl_DStringInit(&result);
  
  if (interp)
  {
    /* Save result, errorInfo and errorCode for the given interpreter */
    Tcl_DStringGetResult(interp,&result);
    errorcode = strdup(Tcl_GetVar(interp, "errorCode", TCL_GLOBAL_ONLY));
    errorinfo = strdup(Tcl_GetVar(interp, "errorInfo", TCL_GLOBAL_ONLY));
  }

  if (interp && signal_handlers[sig].signal_proc)
  {
    if ((tcode = Tcl_Eval(interp, signal_handlers[sig].signal_proc)) 
        != TCL_OK )
    {
      /* A means is provided to allow the called routine to raise
      ** an error or otherwise interrupt the interpretation of commands.
      ** If you use the -async this way, BE AWARE of the warnings in the
      ** Tcl_AsyncCreate man page; you may have unexpected results.
      */
      code = tcode; /* Reset the code and don't reset the results! */
      free(errorcode);
      free(errorinfo);
    }
    else if (interp)
    {
      Tcl_DStringResult(interp,&result);
      Tcl_SetVar(interp, "errorCode", errorcode, TCL_GLOBAL_ONLY);
      Tcl_SetVar(interp, "errorInfo", errorinfo, TCL_GLOBAL_ONLY);
      free(errorcode);
      free(errorinfo);
    }
  }
  else if (interp)
  {
    Tcl_DStringResult(interp,&result);
    Tcl_SetVar(interp, "errorCode", errorcode, TCL_GLOBAL_ONLY);
    Tcl_SetVar(interp, "errorInfo", errorinfo, TCL_GLOBAL_ONLY);
    free(errorcode);
    free(errorinfo);
  }
    
  return code;
}

static void handle_async_signal (int sig)
{
  if (signal_handlers[sig].is_handled &&
      signal_handlers[sig].signal_proc &&
      signal_handlers[sig].is_async ) 
    Tcl_AsyncMark(signal_handlers[sig].async);
}

/* Some utility functions */
static const char *signo_to_signame(int signo)
{
  int i;
  if (signo <= 0 || signo > NSIG )
    return 0;
    
  for (i=0; i < sizeof(signal_name_mapping) / sizeof(signal_name_mapping[0]); 
	i++)
  {
    if ( signal_name_mapping[i].signum == signo )
      return signal_name_mapping[i].signame;
  }
  return 0;
}

static int signame_to_signo(const char *signame)
{
  int i;
  if (signame == 0)
    return -1;
  for (i=0; i < sizeof(signal_name_mapping) / sizeof(signal_name_mapping[0]); 
	i++)
  {
    /* Note: strcasecmp is a commonly implemented non-standard function */
    if ( strcasecmp(signal_name_mapping[i].signame, signame) == 0 )
      return signal_name_mapping[i].signum;
  }
  return 0;
}

static int signal_spec(const char *arg)
{
  if ( arg == 0)
    return -1;
  if (isdigit(arg[0]) )
    return atoi(arg);
  else
    return signame_to_signo(arg);
}

static const char *signal_name(int i)
{
  static char tmp_sig_name[10]; /* 7 + 2 + 1 */
  const char *cp = signo_to_signame(i);
  if (cp)
    return cp;
  if ( i < 0 || i > NSIG )
    return "Illegal";
  sprintf(tmp_sig_name, "Signal %d", i);
  return tmp_sig_name;
}