Bug 27163

Summary: sh trap TSTP ( ) deadly hangs
Product: Base System Reporter: Jin Guojun <jin>
Component: binAssignee: Martin Cracauer <cracauer>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 4.3-RELEASE   
Hardware: Any   
OS: Any   

Description Jin Guojun 2001-05-06 22:40:01 UTC
	According to the signal manpage:  

     Except for the SIGKILL and SIGSTOP signals, the
     signal() function allows for a signal to be caught, to be ignored, or to
     generate an interrupt.  These signals are defined in the file <signal.h>:

     Name            Default Action          Description
	...

    Would this imply that /bin/sh can trap (catch) the SIGTSTP ?
    However, it is not. Try the following script, it deadly hangs 
    if "" pressed.

How-To-Repeat: 
	---------- sh script -----
	trap    "echo do not try this; exit"    TSTP
	while [ 1 ] ;   do
        	date
        	sleep 2
	done
	---------- end of sh script -----

	sh script
	Sun May  6 13:26:05 PDT 2001
	Sun May  6 13:26:07 PDT 2001
	
Comment 1 Kris Kennaway freebsd_committer freebsd_triage 2001-07-13 00:44:34 UTC
Responsible Changed
From-To: freebsd-bugs->cracauer

Martin maintains /bin/sh
Comment 2 Martin Cracauer freebsd_committer freebsd_triage 2003-12-28 03:03:51 UTC
State Changed
From-To: open->closed

Closing, correct behaviour. 

In this script... 

trap    "echo do not try this; exit"    TSTP 
while [ 1 ] ;   do 
date 
sleep 2 
done 

... the trap handler for the SIGTSTP is correctly never invoked.  The 
reason is that the sleep(1) or date(1) are stopped and hence never 
return.  Trap handlers are excuted after the child exits - which never 
happens here. 

If the program being called is catching SIGTSTP, and is hence not 
stopped and then exits later, the handler is correctly excuted: 

Compile this to ./obscure 

#include <stdio.h> 
#include <signal.h> 

void onsig(int signo) 
{ 
fprintf(stderr, "foobarn"); /* yes I know, not allowed */ 
exit(1); 
} 

int main(void) 
{ 
signal(SIGTSTP, onsig); 
for (;;) 
; 
return 0; 
} 

And run this: 

trap    "echo do not try this; exit"    TSTP 
while [ 1 ] ;   do 
./obscure 
done 

The above trap will be executed at the correct time. 


I love this stuff, how could I live without it for no many moons :-)))