lua-users home
lua-l archive

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


On Thu, Jan 29, 2004 at 09:09:21PM -0200, Luiz Henrique de Figueiredo wrote:
> But perhaps I'm missing something simpler.

i dug around some more and here's a brain dump.  this is on linux
with glibc, though i think it would apply to other unixen too.
perhaps someone else can verify this.

- with the default compile options on linux, signal() has bsd
  semantics which means that interrupted system calls are restarted.
  this is why the first SIGINT does not interrupt the in-progress
  pread().  since laction() in lua.c resets the handler to SIG_DFL,
  a second SIGINT ends up killing the interpreter.

- with -ansi compile option, signal() has sysv semantics which means
  that the first SIGINT interrupts the pread().  if necessary, this
  can be caught with pcall().  unfortunately, a subsequent SIGINT
  still ends up killing the interpreter since laction() resets the
  handler to SIG_DFL (also, a sysv signal() is oneshot only).

- in order to handle this "properly", i think lua.c would need to
  bump up its dependency from ansi signal() to posix sigaction().

  here's what i did:  removed the call to signal(i, SIG_DFL) in
  laction() and replaced the 2 calls to signal(SIGINT, ...) in lcall()
  with sig_catch(SIGINT, ...) where sig_catch() is

      static void
      sig_catch(int sig, void (*handler)(int))
      {
        struct sigaction sa;
        sa.sa_handler = handler;
        sa.sa_flags = 0;
        sigemptyset(&sa.sa_mask);
        sigaction(sig, &sa, 0);		/* XXX ignores errors */
      }

  i don't know if these changes create new problems though
  (besides being non-ansi)

to test this hodge podge of signal semantics, i used the following
script:

----------------------------
function foobar()
	local a
	if pcall(function() a=io.stdin:read('*a') end) then
		if a=="" then os.exit(0) end	-- eof
		io.write(string.gsub(a, "%c", "."), "\n")
	else
		print'boo-boo'
	end
end

while 1 do foobar() end
----------------------------

-taj