bug-gawk
[Top][All Lists]
Advanced

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

Re: [bug-gawk] system() should return != 0 when the process is killed


From: Stephane Chazelas
Subject: Re: [bug-gawk] system() should return != 0 when the process is killed
Date: Tue, 8 Mar 2016 13:10:29 +0000
User-agent: Mutt/1.5.21 (2010-09-15)

2016-03-07 23:38:54 +0200, Aharon Robbins:
[...]
> Not true.  BWK awk returns some bizarre fraction

That's the value returned by system() divided by 256. It doesn't
look nice but has the benefit of being non-zero when the process
is killed and allows the user to know what signal number that
was and if a core was dumped.

> mawk exits

mawk exits if it receives the SIGINT itself in the parent (it
doesn't use system(3)), but otherwise, as already mentioned, it
returns the exit code (modulo 256) if the process called exit or
the signal name + 128 if killed (and another 128 if a core was
dumped). Similar to some shell's $? and also means that if you
call exit() with that number, the shell's $? will be consistent
with a process killed with that same number.

The good thing with mawk is that it does that for system() and
also for close() while other implementations have inconsistent
behaviours between system() and close().

> MKS awk
> and Busybox awk return zero.

so does /usr/xpg4/bin/awk on Solaris.

Having the equivalent of "kill -l" builtin, that is a way to
retrieve the signal name from the exit status could be useful,
even though one can do:

mawk '
  function signame(sig, cmd) {
    cmd = "kill -l " sig
    if ((cmd | getline sig) > 0 && close(cmd) == 0)
    # the check on close() being pedantic and for illustration
    # above
      return sig
    return "UNKNOWN"
  }

  BEGIN {
    n = system("exec kill -s KILL \"$$\"")
    if (n > 128) {
      print "killed by", signame(n), "signal"
    }
  }'
    

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]