Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 5 Sep 2003 14:14:36 -0400
From:      The Anarcat <anarcat@anarcat.ath.cx>
To:        FreeBSD Questions <freebsd-questions@FreeBSD.ORG>
Cc:        anarcat@anarcat.ath.cx
Subject:   bug in sh or misinterpretation?
Message-ID:  <20030905181436.GF514@inso.ath.cx>

index | next in thread | raw e-mail

[-- Attachment #1 --]
[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@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@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@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@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

[-- Attachment #2 --]
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/WNKMttcWHAnWiGcRAm7ZAJ4nBVa6JI5twAD52HX4DcdrynPumQCgk07W
3wkdSpZ3iVYnAsjH6aROp04=
=1yzv
-----END PGP SIGNATURE-----
help

Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20030905181436.GF514>