moonbridge

changeset 281:6bb191b6ead5

Make :kill(signal) accept also strings and default to signal 9 (KILL); Improved error handling of :kill(signal)
author jbe
date Thu Jun 08 01:52:18 2017 +0200 (2017-06-08)
parents 1a4f89f4c712
children 850f5c8fec37
files moonbridge_io.c reference.txt
line diff
     1.1 --- a/moonbridge_io.c	Tue Jun 06 21:58:26 2017 +0200
     1.2 +++ b/moonbridge_io.c	Thu Jun 08 01:52:18 2017 +0200
     1.3 @@ -1518,11 +1518,13 @@
     1.4    child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
     1.5    if (child->pid) {
     1.6      int status;
     1.7 -    if (kill(child->pid, SIGKILL)) {
     1.8 +    int pid = child->pid;
     1.9 +    child->pid = 0;
    1.10 +    if (kill(pid, SIGKILL)) {
    1.11        moonbr_io_prepare_errmsg();
    1.12        luaL_error(L, "Error in kill call during garbage collection: %s", errmsg);
    1.13      }
    1.14 -    while (waitpid(child->pid, &status, 0) == -1) {
    1.15 +    while (waitpid(pid, &status, 0) == -1) {
    1.16        if (errno != EINTR) {
    1.17          moonbr_io_prepare_errmsg();
    1.18          luaL_error(L, "Error in waitpid call during garbage collection: %s", errmsg);
    1.19 @@ -1536,7 +1538,18 @@
    1.20    moonbr_io_child_t *child;
    1.21    int sig;
    1.22    child = luaL_checkudata(L, 1, MOONBR_IO_CHILD_MT_REGKEY);
    1.23 -  sig = luaL_optinteger(L, 2, SIGTERM);
    1.24 +  if (lua_type(L, 2) == LUA_TSTRING) {
    1.25 +    lua_getfield(L, LUA_REGISTRYINDEX, MOONBR_IO_SIGNALS_REGKEY);
    1.26 +    lua_pushvalue(L, 2);
    1.27 +    lua_gettable(L, -2);
    1.28 +    sig = lua_tointeger(L, -1);
    1.29 +    if (!sig) {
    1.30 +      lua_pushvalue(L, 2);
    1.31 +      luaL_error(L, "Unknown signal \"%s\"", lua_tostring(L, 2));
    1.32 +    }
    1.33 +  } else {
    1.34 +    sig = luaL_optinteger(L, 2, SIGKILL);
    1.35 +  }
    1.36    if (!child->pid) luaL_error(L, "Attempt to kill an already collected child process");
    1.37    if (kill(child->pid, sig)) {
    1.38      moonbr_io_prepare_errmsg();
     2.1 --- a/reference.txt	Tue Jun 06 21:58:26 2017 +0200
     2.2 +++ b/reference.txt	Thu Jun 08 01:52:18 2017 +0200
     2.3 @@ -310,8 +310,8 @@
     2.4  - :wait_call(waitfunc) 
     2.5  - :wait_yield()
     2.6  
     2.7 -Use :kill(signal) to terminate the process with the given signal (defaults to
     2.8 -15 for SIGTERM).
     2.9 +Use :kill(signal) to terminate the process with the given signal (defaults to 9
    2.10 +for SIGKILL).
    2.11  
    2.12  The :wait() method will wait for the process to terminate and return its exit
    2.13  code. If the process was terminated by a signal, a negative integer is returned

Impressum / About Us