lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On Thu, Feb 10, 2011 at 10:24 AM, Roberto Ierusalimschy
<> wrote:
> I am talking about signals, as reported by the macro WIFSIGNALED. Even
> when the process ran by the shell is interrupted by a signal, the shell
> itself is not. For instance, if I run the following:
>  f = assert(io.popen("lua -i"))
>  print(f:read"*all")
>  print(f:close())
> and then kill the process "lua -i", f:close() (as corrected by the
> proposed patch) still reports a normal termination through its
> second return.

I don't have a build with this patch, or a linux box right now, and
its late at night, but on OS X it appears the shell is terminating
with the full exit status of its last child, including signal number.

% sh -c cat ; echo $?
zsh: killed     sh -c cat

137 is 0x89, so my shell thought the sh command was killed with signal
9, even though I killed cat with signal 9 from another console.

  503  4463  4462   0  31  0  2437224   2252 -      S    s001    0:00.08 -zsh
  503  4492  4463   0  31  0  2426780    336 -      S+   s001    0:00.00 cat
block:~ % kill -9 4492

I suspect pclose() would have got the same status, 137. Pulling that
137 apart without access to the macros in <sys/wait.h> is pretty
painful, I can see why having lua do it in C is nice. Note that on OS
X, the top bits are an exit status if the bottom bits are zero... or
something like that.

Me, I wouldn't do it by putting exit or signal number in the same
return position, that seems ripe for confusion. I'd have pclose()
return exit status if WIFEXITED, or "nil,signo" if WIFSIGNALLED.

exitno, signo = f:close()

if not exitno then
  error("killed by signal "..signo)

status, exitno, signo = f:close() -- another option, backwards compatible

Returning the signal name as a string instead of a number would be
more lua-ish, but converting signal numbers to strings is perhaps not
so portable. Maybe the POSIX signals can have strings and any other
signal number would just be a string of the number.