bug in sh or misinterpretation?

The Anarcat anarcat at anarcat.ath.cx
Fri Sep 5 11:14:40 PDT 2003


[please CC, I don't follow -questions]

Hello!

I've been struggling with an odd quirk in sh(1). Here is my sample
code:

--- 8< cut here 8< ---
cleanup () {
    echo "cleaning up $pid"
    kill $pid
}

(
    echo "in subshell"
    tail -f /etc/motd &
    pid=$!
    trap cleanup 1 2 15
    wait $pid || echo "command failed"
) &

wait
--- 8< cut here 8< ---

The idea is to be able *in the subshell* to cleanup a backgrounded
command. The result is unexpected:

anarcat at shall[~/src/local/bin/worldtools]% sh test_trap.sh 
in subshell
FreeBSD 4.8-STABLE (SHALL) #0: Wed Aug 13 16:46:04 EDT 2003

Welcome to Shall, multi-purpose file/mail/web/ftp server.
^C
anarcat at shall[~/src/local/bin/worldtools]% ps
  PID  TT  STAT      TIME COMMAND
14162  p1  Ss     0:03,95 -zsh (zsh)
16918  p1  S      0:00,00 sh test_trap.sh
16919  p1  S      0:00,00 sh test_trap.sh
16920  p1  S      0:00,01 tail -f /etc/motd
16922  p1  R+     0:00,00 ps

i.e. the subshell doesn't call the trap!! I've searched long and large
manuals, I even tried debugging (i'm about to build sh with debugging
symbols now): the subshell *really* receives the signal, it just
ignores it.

The sh manpage says that:

             When the shell forks off a subshell, it resets trapped (but not
             ignored) signals to the default action.  The trap command has no
             effect on signals that were ignored on entry to the shell.

Well, it looks like calling trap in the subshell doesn't work.

For reference, in bash and zsh behave correctly:

anarcat at shall[~/src/local/bin/worldtools]% zsh test_trap.sh 
in subshell
FreeBSD 4.8-STABLE (SHALL) #0: Wed Aug 13 16:46:04 EDT 2003

Welcome to Shall, multi-purpose file/mail/web/ftp server.
^Ccleaning up 16947

anarcat at shall[~/src/local/bin/worldtools]% command failed

Is it me? Or do I head for GNATS? :)

A.



-- 
There has been only one Christian.
They caught him and crucified him -- early.
                        - Mark Twain
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20030905/0b561a2d/attachment.bin


More information about the freebsd-questions mailing list