util-src/signal.c
author Matthew Wild <mwild1@gmail.com>
Sun, 10 Jan 2010 03:54:29 +0000
changeset 2432 1e045b3e9dce
parent 2431 c4d32b35fe60
child 2459 66f60e69567b
permissions -rw-r--r--
util.signal: Restore the old debug hook earlier, just in case we receive another signal between clearing the signal queue and restoring it
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
/*
2405
88f73ee1c46f util.signal: Update header comment for new filename
Matthew Wild <mwild1@gmail.com>
parents: 2343
diff changeset
     2
 * signal.c -- Signal Handler Library for Lua
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
 *
2431
c4d32b35fe60 util.signal: Update version in header, and author's email
Matthew Wild <mwild1@gmail.com>
parents: 2430
diff changeset
     4
 * Version: 1.000+changes
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
 *
2431
c4d32b35fe60 util.signal: Update version in header, and author's email
Matthew Wild <mwild1@gmail.com>
parents: 2430
diff changeset
     6
 * Copyright (C) 2007  Patrick J. Donnelly (batrick@batbytes.com)
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
 * This software is distributed under the same license as Lua 5.0:
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
 * Permission is hereby granted, free of charge, to any person obtaining a
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
 * copy of this software and associated documentation files (the "Software"),
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
 * to deal in the Software without restriction, including without limitation
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
 * and/or sell copies of the Software, and to permit persons to whom the
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
 * Software is furnished to do so, subject to the following conditions:
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
 * The above copyright notice and this permission notice shall be included
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
 * in all copies or substantial portions of the Software.
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
 * OTHER DEALINGS IN THE SOFTWARE. 
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
*/
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
#include <signal.h>
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
    30
#include <malloc.h>
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
#include "lua.h"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
#include "lauxlib.h"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
#ifndef lsig
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
#define lsig
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
struct lua_signal
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
  char *name; /* name of the signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
  int sig; /* the signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
};
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
#define LUA_SIGNAL "lua_signal"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
static const struct lua_signal lua_signals[] = {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
  /* ANSI C signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
#ifdef SIGABRT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
  {"SIGABRT", SIGABRT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
#ifdef SIGFPE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
  {"SIGFPE", SIGFPE},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
#ifdef SIGILL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
  {"SIGILL", SIGILL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
#ifdef SIGINT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
  {"SIGINT", SIGINT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
#ifdef SIGSEGV
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
  {"SIGSEGV", SIGSEGV},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
#ifdef SIGTERM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
  {"SIGTERM", SIGTERM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
  /* posix signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
#ifdef SIGHUP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
  {"SIGHUP", SIGHUP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
#ifdef SIGQUIT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
  {"SIGQUIT", SIGQUIT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
#ifdef SIGTRAP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
  {"SIGTRAP", SIGTRAP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
#ifdef SIGKILL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
  {"SIGKILL", SIGKILL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
#ifdef SIGUSR1
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
  {"SIGUSR1", SIGUSR1},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
#ifdef SIGUSR2
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
  {"SIGUSR2", SIGUSR2},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
#ifdef SIGPIPE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
  {"SIGPIPE", SIGPIPE},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
#ifdef SIGALRM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
  {"SIGALRM", SIGALRM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
#ifdef SIGCHLD
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
  {"SIGCHLD", SIGCHLD},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
#ifdef SIGCONT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
  {"SIGCONT", SIGCONT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
#ifdef SIGSTOP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
  {"SIGSTOP", SIGSTOP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
#ifdef SIGTTIN
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
  {"SIGTTIN", SIGTTIN},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
#ifdef SIGTTOU
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
  {"SIGTTOU", SIGTTOU},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
  /* some BSD signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
#ifdef SIGIOT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
  {"SIGIOT", SIGIOT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
#ifdef SIGBUS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
  {"SIGBUS", SIGBUS},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
#ifdef SIGCLD
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
  {"SIGCLD", SIGCLD},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
#ifdef SIGURG
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
  {"SIGURG", SIGURG},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
#ifdef SIGXCPU
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
  {"SIGXCPU", SIGXCPU},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
#ifdef SIGXFSZ
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
  {"SIGXFSZ", SIGXFSZ},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
#ifdef SIGVTALRM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
  {"SIGVTALRM", SIGVTALRM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
#ifdef SIGPROF
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
  {"SIGPROF", SIGPROF},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
#ifdef SIGWINCH
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
  {"SIGWINCH", SIGWINCH},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
#ifdef SIGPOLL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
  {"SIGPOLL", SIGPOLL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
#ifdef SIGIO
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
  {"SIGIO", SIGIO},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
  /* add odd signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
#ifdef SIGSTKFLT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
  {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   146
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   147
#ifdef SIGSYS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
  {"SIGSYS", SIGSYS},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
  {NULL, 0}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
};
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
static lua_State *Lsig = NULL;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
static lua_Hook Hsig = NULL;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
static int Hmask = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
static int Hcount = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   158
static struct signal_event
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   159
{
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   160
	int Nsig;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   161
	struct signal_event *next_event;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   162
} *signal_queue = NULL;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   163
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   164
static struct signal_event *last_event = NULL;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   165
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   166
static void sighook(lua_State *L, lua_Debug *ar)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   167
{
2432
1e045b3e9dce util.signal: Restore the old debug hook earlier, just in case we receive another signal between clearing the signal queue and restoring it
Matthew Wild <mwild1@gmail.com>
parents: 2431
diff changeset
   168
  /* restore the old hook */
1e045b3e9dce util.signal: Restore the old debug hook earlier, just in case we receive another signal between clearing the signal queue and restoring it
Matthew Wild <mwild1@gmail.com>
parents: 2431
diff changeset
   169
  lua_sethook(L, Hsig, Hmask, Hcount);
1e045b3e9dce util.signal: Restore the old debug hook earlier, just in case we receive another signal between clearing the signal queue and restoring it
Matthew Wild <mwild1@gmail.com>
parents: 2431
diff changeset
   170
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   171
  lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   172
  lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   173
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   174
  struct signal_event *event;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   175
  while((event = signal_queue))
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   176
  {
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   177
    lua_pushnumber(L, event->Nsig);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   178
    lua_gettable(L, -2);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   179
    lua_call(L, 0, 0);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   180
    signal_queue = event->next_event;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   181
    free(event);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   182
  };
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   183
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   184
  lua_pop(L, 1); /* pop lua_signal table */
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   185
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   186
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   188
static void handle(int sig)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   189
{
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   190
  if(!signal_queue)
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   191
  {
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   192
    /* Store the existing debug hook (if any) and its parameters */
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   193
    Hsig = lua_gethook(Lsig);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   194
    Hmask = lua_gethookmask(Lsig);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   195
    Hcount = lua_gethookcount(Lsig);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   196
    
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   197
    signal_queue = malloc(sizeof(struct signal_event));
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   198
    signal_queue->Nsig = sig;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   199
    signal_queue->next_event = NULL;
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   200
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   201
    last_event = signal_queue;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   202
    
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   203
    /* Set our new debug hook */
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   204
    lua_sethook(Lsig, sighook, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   205
  }
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   206
  else
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   207
  {
2430
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   208
    last_event->next_event = malloc(sizeof(struct signal_event));
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   209
    last_event->next_event->Nsig = sig;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   210
    last_event->next_event->next_event = NULL;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   211
    
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   212
    last_event = last_event->next_event;
69d377c84fe3 util.signal: Queue up multiple signals, instead of trampling on the previous debug hook (and never clearing our own)
Matthew Wild <mwild1@gmail.com>
parents: 2405
diff changeset
   213
  }
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   214
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   215
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   216
/*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   217
 * l_signal == signal(signal [, func [, chook]])
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   218
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   219
 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   220
 * func = Lua function to call
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
 * chook = catch within C functions
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
 *         if caught, Lua function _must_
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
 *         exit, as the stack is most likely
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
 *         in an unstable state.
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
*/  
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
static int l_signal(lua_State *L)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   228
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
  int args = lua_gettop(L);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
  int t, sig; /* type, signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   232
  /* get type of signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
  luaL_checkany(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
  t = lua_type(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
  if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
    sig = (int) lua_tonumber(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
  else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
    lua_pushvalue(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
    lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
    if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
      luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
    sig = (int) lua_tonumber(L, -1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
    lua_pop(L, 1); /* get rid of number we pushed */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
    luaL_checknumber(L, 1); /* will always error, with good error msg */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
  /* set handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   251
  if (args == 1 || lua_isnil(L, 2)) /* clear handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   253
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
    lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   256
    lua_gettable(L, -2); /* return old handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
    lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
    lua_pushnil(L);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
    lua_settable(L, -4);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
    lua_remove(L, -2); /* remove LUA_SIGNAL table */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   261
    signal(sig, SIG_DFL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   262
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   263
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
    luaL_checktype(L, 2, LUA_TFUNCTION);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   265
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
    lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
    lua_pushvalue(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
    lua_settable(L, -3);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   272
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   273
    /* Set the state for the handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
    Lsig = L;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
    if (lua_toboolean(L, 3)) /* c hook? */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
    {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
      if (signal(sig, handle) == SIG_ERR)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
        lua_pushboolean(L, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   280
      else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   281
        lua_pushboolean(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   282
    } else /* lua_hook */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   283
    {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   284
      if (signal(sig, handle) == SIG_ERR)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   285
        lua_pushboolean(L, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
      else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   287
        lua_pushboolean(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
    }
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
  }
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   290
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   291
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   292
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   293
/*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   294
 * l_raise == raise(signal)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   295
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   296
 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   297
*/  
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   298
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   299
static int l_raise(lua_State *L)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   300
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   301
  /* int args = lua_gettop(L); */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   302
  int t = 0; /* type */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   303
  lua_Number ret;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   304
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   305
  luaL_checkany(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   306
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   307
  t = lua_type(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   308
  if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   309
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   310
    ret = (lua_Number) raise((int) lua_tonumber(L, 1));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   311
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
  } else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   313
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   314
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   315
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
    lua_pushvalue(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
    lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   318
    if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
      luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   320
    ret = (lua_Number) raise((int) lua_tonumber(L, -1));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   321
    lua_pop(L, 1); /* get rid of number we pushed */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   323
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
    luaL_checknumber(L, 1); /* will always error, with good error msg */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   325
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   326
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   327
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   328
1841
0dc1daa94e7f util.signal: Change ifdef to allow signal.kill() on Solaris (thanks Filip)
Matthew Wild <mwild1@gmail.com>
parents: 988
diff changeset
   329
#if defined _POSIX_SOURCE || (defined(sun) || defined(__sun))
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   330
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   331
/* define some posix only functions */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   332
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   333
/*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   334
 * l_kill == kill(pid, signal)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   335
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   336
 * pid = process id
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   338
*/  
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   339
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   340
static int l_kill(lua_State *L)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   341
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   342
  int t; /* type */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   343
  lua_Number ret; /* return value */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   345
  luaL_checknumber(L, 1); /* must be int for pid */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
  luaL_checkany(L, 2); /* check for a second arg */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
  t = lua_type(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
  if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   350
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   351
    ret = (lua_Number) kill((int) lua_tonumber(L, 1),
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   352
        (int) lua_tonumber(L, 2));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   353
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
  } else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   356
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
    lua_pushvalue(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   359
    lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   360
    if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   361
      luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   362
    ret = (lua_Number) kill((int) lua_tonumber(L, 1),
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   363
        (int) lua_tonumber(L, -1));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   364
    lua_pop(L, 1); /* get rid of number we pushed */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   367
    luaL_checknumber(L, 2); /* will always error, with good error msg */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   369
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   370
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   371
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   372
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   373
static const struct luaL_Reg lsignal_lib[] = {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
  {"signal", l_signal},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
  {"raise", l_raise},
2343
4b05fd561cbd util.signal: Make kill() available on Solaris; really fixes #136.
Waqas Hussain <waqas20@gmail.com>
parents: 2317
diff changeset
   376
#if defined _POSIX_SOURCE || (defined(sun) || defined(__sun))
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   377
  {"kill", l_kill},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   378
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
  {NULL, NULL}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   380
};
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   382
int luaopen_util_signal(lua_State *L)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   383
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   384
  int i = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   385
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   386
  /* add the library */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   387
  luaL_register(L, "signal", lsignal_lib);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   388
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   389
  /* push lua_signals table into the registry */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   390
  /* put the signals inside the library table too,
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   391
   * they are only a reference */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
  lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   393
  lua_createtable(L, 0, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   394
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   395
  while (lua_signals[i].name != NULL)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   397
    /* registry table */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   398
    lua_pushstring(L, lua_signals[i].name);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   399
    lua_pushnumber(L, lua_signals[i].sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   400
    lua_settable(L, -3);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   401
    /* signal table */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   402
    lua_pushstring(L, lua_signals[i].name);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   403
    lua_pushnumber(L, lua_signals[i].sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   404
    lua_settable(L, -5);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   405
    i++;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   406
  }
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   407
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   408
  /* add newtable to the registry */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   409
  lua_settable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   410
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   411
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   412
}