POSIX signal handling: inputs needed

M

monk

Hi all,

Is it safe/sane/common practice to have a program react to every
single POSIX signal there is out there available in the OS?

Some friends have suggested to just stick to the basic ones such as
TSTP and INT. Meaning, tell the program to do something (like an
email alert) if it receives a control-Z or control-C only. But what
about the others?

My fear is that my chronic paranoia might get in the way, thus making
my programs unstable.
At the same time, I don't want to miss any unseen events that may
affect my programs reliability.

Positively, _*any*_ thoughts are welcome.

for example, to see what I mean if confusing above. I don't know if it
matters but I'm using Linux 2.6.x and AIX 5.3 with perl 5.8.2. Thanks
in advance. :

#!/usr/bin/perl
use strict;
use warnings;

#all this does is print "hello world" every 1 second --given a true
condition.
#if *any* signal is received, the status changes to false exiting the
program gracefully.

#my vars
my $signal;
my $status = 1;
my $pid;
my @all_signals;
my $file_handler;


#sub routine handling the signal received
sub signal_handler {
$signal = shift;
$status = 0;
$SIG{$signal} = \&signal_handler;

}

#get all signals available on AIX
#add them up to an array

sub get_all_my_signals {
foreach my $list (sort keys %SIG){
push (@all_signals, $list);
}

}

sub go {

#get all signals available on this OS
get_all_my_signals();


#if any signal is received, change status to zero
#thus exiting the loop.
foreach (@all_signals){
$SIG{$_} = \&signal_handler;
}

#Current process' PID is
$pid = $$;

#writing pid to a file
open ($file_handler, ">", "test.pid");
print $file_handler $pid;
close $file_handler;

while ($status == 1){
print "hello world\n";
sleep 1;
}
print "out of loop. Got signal \$SIG{$signal}\n";

#house keeping
unlink "test.pid" if (-e "test.pid");
exit(0);
}

###done defniing now let's call it
#call it now..
go();
 
P

Peter Makholm

monk said:
Some friends have suggested to just stick to the basic ones such as
TSTP and INT. Meaning, tell the program to do something (like an
email alert) if it receives a control-Z or control-C only. But what
about the others?

I would suggest to stick with the signals intended meanings. Do not
redefine SIGINT to mean anything but 'user tried to interrupt the
process'. Only exception is that you're allowed to use SIGHUP for
restarting af server and rereading the configuration.

If you need to use signals for some user defined actions you should
use SIGUSR1 and SIGUSR2.
My fear is that my chronic paranoia might get in the way, thus making
my programs unstable.
At the same time, I don't want to miss any unseen events that may
affect my programs reliability.

Catching all signals will lead to instabilities. Taking you example,
do you really mean to stop just because you're resizin you window to
see more hellos (SIGWINCH)? And if you using tcp communication, do you
really want to take some action if the other end sends a specially
crafted package (SIGURG) - this can lead to a DoS-attack.

//Makholm
 
M

monk

Thanks for your comments mate.
I would suggest to stick with the signals intended meanings.
Ok. I'll use HUP, INT, TSTP, and TERM -after reading your
warnings.
As you pointed out, "Catching all signals will lead to instabilities"
which is quite right.

Of all the signal posix, which ones in your opinion are the most
common ones? (besides that ones above and the ones you already
mentioned.)
 
X

xhoster

monk said:
Hi all,

Is it safe/sane/common practice to have a program react to every
single POSIX signal there is out there available in the OS?


Sounds strange to me. If you don't know what the signal is, why you are
getting it, or what to do with it, then why not just let the default
behavior prevail? I'm only going to override the default behavior
(presumably crafted by people who know far more about such system things
than I do) if I have specific knowledge about how my program should
interact with that part of their system.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,044
Messages
2,570,388
Members
47,052
Latest member
ketan

Latest Threads

Top