moonbridge
changeset 297:dd0ec48592e7
Bugfix regarding waiting for child processes in moonbridge_io.poll(...)
author | jbe |
---|---|
date | Sat Jun 17 22:43:44 2017 +0200 (2017-06-17) |
parents | ce109800ae2d |
children | 09d6428fdf6c |
files | moonbridge_io.c |
line diff
1.1 --- a/moonbridge_io.c Sat Jun 17 22:35:44 2017 +0200 1.2 +++ b/moonbridge_io.c Sat Jun 17 22:43:44 2017 +0200 1.3 @@ -117,6 +117,7 @@ 1.4 } moonbr_io_child_t; 1.5 1.6 volatile sig_atomic_t moonbr_io_sigterm_flag = 0; 1.7 +volatile sig_atomic_t moonbr_io_sigchld_flag = 0; 1.8 1.9 static int moonbr_io_yield(lua_State *L) { 1.10 return lua_yield(L, lua_gettop(L)); 1.11 @@ -1615,6 +1616,10 @@ 1.12 moonbr_io_sigterm_flag = 1; 1.13 } 1.14 1.15 +static void moonbr_io_sigchld_handler(int sig) { 1.16 + moonbr_io_sigchld_flag = 1; 1.17 +} 1.18 + 1.19 int moonbr_io_catch_sigterm(lua_State *L) { 1.20 signal(SIGTERM, moonbr_io_sigterm_handler); 1.21 return 0; 1.22 @@ -1665,7 +1670,7 @@ 1.23 int force_wakeup = 0; 1.24 int use_timeout = 0; // negative for negative timeout 1.25 int check_sigterm = 0; 1.26 - int check_child = 0; 1.27 + int check_sigchld = 0; 1.28 int childstatus; 1.29 sigset_t mask, orig_mask; 1.30 int status; 1.31 @@ -1698,7 +1703,11 @@ 1.32 } else { 1.33 child = luaL_testudata(L, -2, MOONBR_IO_CHILD_MT_REGKEY); 1.34 if (child) { 1.35 - check_child = 1; 1.36 + if (!check_sigchld) { 1.37 + check_sigchld = 1; 1.38 + moonbr_io_sigchld_flag = 0; 1.39 + signal(SIGCHLD, moonbr_io_sigchld_handler); 1.40 + } 1.41 if (waitpid(child->pid, &childstatus, WNOHANG|WNOWAIT) != -1) { 1.42 force_wakeup = 1; 1.43 } else if (errno != ECHILD && errno != EINTR) { 1.44 @@ -1758,10 +1767,10 @@ 1.45 if (use_timeout < 0) force_wakeup = 1; 1.46 if (!lua_isnoneornil(L, 4)) luaL_checktype(L, 4, LUA_TBOOLEAN); 1.47 check_sigterm = lua_toboolean(L, 4); 1.48 - if ((check_sigterm || check_child) && !force_wakeup) { 1.49 + if ((check_sigterm || check_sigchld) && !force_wakeup) { 1.50 sigemptyset(&mask); 1.51 if (check_sigterm) sigaddset(&mask, SIGTERM); 1.52 - if (check_child) sigaddset(&mask, SIGCHLD); 1.53 + if (check_sigchld) sigaddset(&mask, SIGCHLD); 1.54 if (sigprocmask(SIG_BLOCK, &mask, &orig_mask)) abort(); 1.55 } 1.56 if (check_sigterm && moonbr_io_sigterm_flag) { 1.57 @@ -1773,15 +1782,9 @@ 1.58 lua_pushboolean(L, 1); 1.59 return 3; 1.60 } 1.61 - if (check_child && !force_wakeup) { 1.62 - if (wait3(&childstatus, WNOHANG|WNOWAIT, NULL) != -1) { 1.63 - if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.64 - force_wakeup = 1; 1.65 - } else if (errno != ECHILD && errno != EINTR) { 1.66 - moonbr_io_prepare_errmsg(); 1.67 - if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.68 - luaL_error(L, "Error in waitpid call: %s", errmsg); 1.69 - } 1.70 + if (check_sigchld && !force_wakeup && moonbr_io_sigchld_flag) { 1.71 + if (sigprocmask(SIG_SETMASK, &orig_mask, NULL)) abort(); 1.72 + force_wakeup = 1; 1.73 } 1.74 if (use_timeout < 0) { 1.75 lua_pushboolean(L, 0);