util-src/signal.c
author Waqas Hussain <waqas20@gmail.com>
Tue, 15 Jun 2010 16:57:15 +0500
changeset 3274 366ac5e47096
parent 2459 66f60e69567b
child 3282 8ef3beed0779
permissions -rw-r--r--
util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
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>
2459
66f60e69567b util.signal: Using stdlib.h instead of malloc.h.
Tobias Markmann <tm@ayena.de>
parents: 2432
diff changeset
    30
#include <stdlib.h>
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
3274
366ac5e47096 util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
Waqas Hussain <waqas20@gmail.com>
parents: 2459
diff changeset
    32
#ifdef __unix__
366ac5e47096 util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
Waqas Hussain <waqas20@gmail.com>
parents: 2459
diff changeset
    33
#include <sys/param.h>
366ac5e47096 util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
Waqas Hussain <waqas20@gmail.com>
parents: 2459
diff changeset
    34
#endif
366ac5e47096 util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
Waqas Hussain <waqas20@gmail.com>
parents: 2459
diff changeset
    35
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
#include "lua.h"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
#include "lauxlib.h"
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
#ifndef lsig
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
#define lsig
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
struct lua_signal
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
  char *name; /* name of the signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
  int sig; /* the signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
};
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
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
#define LUA_SIGNAL "lua_signal"
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
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
    54
  /* ANSI C signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
#ifdef SIGABRT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
  {"SIGABRT", SIGABRT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
#ifdef SIGFPE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
  {"SIGFPE", SIGFPE},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
#ifdef SIGILL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
  {"SIGILL", SIGILL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
#ifdef SIGINT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
  {"SIGINT", SIGINT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
#ifdef SIGSEGV
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
  {"SIGSEGV", SIGSEGV},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
#ifdef SIGTERM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
  {"SIGTERM", SIGTERM},
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
  /* posix signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
#ifdef SIGHUP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
  {"SIGHUP", SIGHUP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
#ifdef SIGQUIT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
  {"SIGQUIT", SIGQUIT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
#ifdef SIGTRAP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
  {"SIGTRAP", SIGTRAP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
#ifdef SIGKILL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
  {"SIGKILL", SIGKILL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
#ifdef SIGUSR1
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
  {"SIGUSR1", SIGUSR1},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
#ifdef SIGUSR2
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
  {"SIGUSR2", SIGUSR2},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
#ifdef SIGPIPE
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
  {"SIGPIPE", SIGPIPE},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
#ifdef SIGALRM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
  {"SIGALRM", SIGALRM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
#ifdef SIGCHLD
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
  {"SIGCHLD", SIGCHLD},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
#ifdef SIGCONT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
  {"SIGCONT", SIGCONT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
#ifdef SIGSTOP
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
  {"SIGSTOP", SIGSTOP},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
#ifdef SIGTTIN
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
  {"SIGTTIN", SIGTTIN},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
#ifdef SIGTTOU
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
  {"SIGTTOU", SIGTTOU},
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
  /* some BSD signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
#ifdef SIGIOT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
  {"SIGIOT", SIGIOT},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
#ifdef SIGBUS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
  {"SIGBUS", SIGBUS},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
#ifdef SIGCLD
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
  {"SIGCLD", SIGCLD},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
#ifdef SIGURG
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
  {"SIGURG", SIGURG},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
#ifdef SIGXCPU
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
  {"SIGXCPU", SIGXCPU},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
#ifdef SIGXFSZ
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
  {"SIGXFSZ", SIGXFSZ},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
#ifdef SIGVTALRM
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
  {"SIGVTALRM", SIGVTALRM},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   135
#ifdef SIGPROF
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   136
  {"SIGPROF", SIGPROF},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   137
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   138
#ifdef SIGWINCH
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   139
  {"SIGWINCH", SIGWINCH},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   140
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   141
#ifdef SIGPOLL
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   142
  {"SIGPOLL", SIGPOLL},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   143
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   144
#ifdef SIGIO
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   145
  {"SIGIO", SIGIO},
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
  /* add odd signals */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   148
#ifdef SIGSTKFLT
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   149
  {"SIGSTKFLT", SIGSTKFLT}, /* stack fault */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   150
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   151
#ifdef SIGSYS
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   152
  {"SIGSYS", SIGSYS},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   153
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   154
  {NULL, 0}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   155
};
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   156
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   157
static lua_State *Lsig = NULL;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   158
static lua_Hook Hsig = NULL;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   159
static int Hmask = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   160
static int Hcount = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   161
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
   162
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
   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
	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
   165
	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
   166
} *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
   167
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
   168
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
   169
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   170
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
   171
{
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
   172
  /* 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
   173
  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
   174
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   175
  lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   176
  lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   177
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
   178
  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
   179
  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
   180
  {
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
    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
   182
    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
   183
    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
   184
    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
   185
    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
   186
  };
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   187
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
   188
  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
   189
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   190
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   191
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   192
static void handle(int sig)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   193
{
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
   194
  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
   195
  {
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
    /* 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
   197
    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
   198
    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
   199
    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
   200
    
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
    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
   202
    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
   203
    signal_queue->next_event = NULL;
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   204
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
   205
    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
   206
    
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
   207
    /* 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
   208
    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
   209
  }
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
  else
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   211
  {
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
   212
    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
   213
    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
   214
    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
   215
    
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
   216
    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
   217
  }
988
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
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   220
/*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   221
 * l_signal == signal(signal [, func [, chook]])
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   222
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   223
 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   224
 * func = Lua function to call
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   225
 * chook = catch within C functions
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   226
 *         if caught, Lua function _must_
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   227
 *         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
   228
 *         in an unstable state.
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   229
*/  
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   230
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   231
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
   232
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   233
  int args = lua_gettop(L);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   234
  int t, sig; /* type, signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   235
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   236
  /* get type of signal */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   237
  luaL_checkany(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   238
  t = lua_type(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   239
  if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   240
    sig = (int) lua_tonumber(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   241
  else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   242
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   243
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   244
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   245
    lua_pushvalue(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   246
    lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   247
    if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   248
      luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   249
    sig = (int) lua_tonumber(L, -1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   250
    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
   251
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   252
    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
   253
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   254
  /* set handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   255
  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
   256
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   257
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   258
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   259
    lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   260
    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
   261
    lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   262
    lua_pushnil(L);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   263
    lua_settable(L, -4);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   264
    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
   265
    signal(sig, SIG_DFL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   266
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   267
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   268
    luaL_checktype(L, 2, LUA_TFUNCTION);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   269
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   270
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   271
    lua_gettable(L, LUA_REGISTRYINDEX);
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
    lua_pushnumber(L, sig);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   274
    lua_pushvalue(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   275
    lua_settable(L, -3);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   276
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   277
    /* Set the state for the handler */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   278
    Lsig = L;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   279
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   280
    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
   281
    {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   282
      if (signal(sig, handle) == SIG_ERR)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   283
        lua_pushboolean(L, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   284
      else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   285
        lua_pushboolean(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   286
    } else /* lua_hook */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   287
    {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   288
      if (signal(sig, handle) == SIG_ERR)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   289
        lua_pushboolean(L, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   290
      else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   291
        lua_pushboolean(L, 1);
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
  return 1;
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
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
 * l_raise == raise(signal)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   299
 *
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   300
 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   301
*/  
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   302
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   303
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
   304
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   305
  /* int args = lua_gettop(L); */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   306
  int t = 0; /* type */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   307
  lua_Number ret;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   308
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   309
  luaL_checkany(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   310
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   311
  t = lua_type(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   312
  if (t == LUA_TNUMBER)
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
    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
   315
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   316
  } else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   317
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   318
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   319
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   320
    lua_pushvalue(L, 1);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   321
    lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   322
    if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   323
      luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   324
    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
   325
    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
   326
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   327
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   328
    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
   329
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   330
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   331
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   332
3274
366ac5e47096 util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
Waqas Hussain <waqas20@gmail.com>
parents: 2459
diff changeset
   333
#ifdef __unix__
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   334
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   335
/* define some posix only functions */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   336
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   337
/*
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   338
 * l_kill == kill(pid, signal)
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
 * pid = process id
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   341
 * signal = signal number or string
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   342
*/  
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   343
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   344
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
   345
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   346
  int t; /* type */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   347
  lua_Number ret; /* return value */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   348
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   349
  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
   350
  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
   351
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   352
  t = lua_type(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   353
  if (t == LUA_TNUMBER)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   354
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   355
    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
   356
        (int) lua_tonumber(L, 2));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   357
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   358
  } else if (t == LUA_TSTRING)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   359
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   360
    lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   361
    lua_gettable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   362
    lua_pushvalue(L, 2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   363
    lua_gettable(L, -2);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   364
    if (!lua_isnumber(L, -1))
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   365
      luaL_error(L, "invalid signal string");
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   366
    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
   367
        (int) lua_tonumber(L, -1));
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   368
    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
   369
    lua_pushnumber(L, ret);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   370
  } else
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   371
    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
   372
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   373
}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   374
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   375
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   376
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   377
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
   378
  {"signal", l_signal},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   379
  {"raise", l_raise},
3274
366ac5e47096 util.signal: Make kill() available on OpenBSD, NetBSD (thanks Florob, filip).
Waqas Hussain <waqas20@gmail.com>
parents: 2459
diff changeset
   380
#ifdef __unix__
988
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   381
  {"kill", l_kill},
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   382
#endif
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   383
  {NULL, NULL}
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   384
};
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
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
   387
{
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   388
  int i = 0;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   389
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   390
  /* add the library */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   391
  luaL_register(L, "signal", lsignal_lib);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   392
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   393
  /* 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
   394
  /* 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
   395
   * they are only a reference */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   396
  lua_pushstring(L, LUA_SIGNAL);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   397
  lua_createtable(L, 0, 0);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   398
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   399
  while (lua_signals[i].name != NULL)
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   400
  {
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   401
    /* registry 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, -3);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   405
    /* signal table */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   406
    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
   407
    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
   408
    lua_settable(L, -5);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   409
    i++;
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
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   412
  /* add newtable to the registry */
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   413
  lua_settable(L, LUA_REGISTRYINDEX);
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   414
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   415
  return 1;
8cb9a0d59e78 util.lsignal: Add lsignal library for catching POSIX signals
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   416
}