POSIX signal handling: inputs needed

Discussion in 'Perl Misc' started by monk, Nov 22, 2007.

  1. monk

    monk Guest

    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();
    monk, Nov 22, 2007
    #1
    1. Advertising

  2. monk <> writes:

    > 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
    Peter Makholm, Nov 22, 2007
    #2
    1. Advertising

  3. monk

    monk Guest

    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.)
    monk, Nov 22, 2007
    #3
  4. monk

    Guest

    monk <> wrote:
    > 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.
    , Nov 23, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. steffen staehle
    Replies:
    0
    Views:
    490
    steffen staehle
    Sep 7, 2004
  2. steffen staehle
    Replies:
    0
    Views:
    546
    steffen staehle
    Sep 7, 2004
  3. Michael Pronath
    Replies:
    1
    Views:
    1,165
    Diez B. Roggisch
    Jan 3, 2005
  4. Peter
    Replies:
    34
    Views:
    1,934
    James Kanze
    Oct 17, 2009
  5. msoulier
    Replies:
    1
    Views:
    136
    Ilmari Karonen
    Jul 16, 2005
Loading...

Share This Page