1Net::Server::SIG(3)   User Contributed Perl Documentation  Net::Server::SIG(3)
2
3
4

NAME

6       Net::Server::SIG - adpf - Safer signal handling
7

SYNOPSIS

9         use Net::Server::SIG qw(register_sig check_sigs);
10         use IO::Select ();
11         use POSIX qw(WNOHANG);
12
13         my $select = IO::Select->new();
14
15         register_sig(PIPE => 'IGNORE',
16                      HUP  => 'DEFAULT',
17                      USR1 => sub { print "I got a SIG $_[0]\n"; },
18                      USR2 => sub { print "I got a SIG $_[0]\n"; },
19                      CHLD => sub { 1 while (waitpid(-1, WNOHANG) > 0); },
20                      );
21
22         ### add some handles to the select
23         $select->add(\*STDIN);
24
25         ### loop forever trying to stay alive
26         while ( 1 ){
27
28           ### do a timeout to see if any signals got passed us
29           ### while we were processing another signal
30           my @fh = $select->can_read(10);
31
32           my $key;
33           my $val;
34
35           ### this is the handler for safe (fine under unsafe also)
36           if( &check_sigs() ){
37             # or my @sigs = &check_sigs();
38             next unless @fh;
39           }
40
41           my $handle = $fh[@fh];
42
43           ### do something with the handle
44
45         }
46

DESCRIPTION

48       Signals in Perl 5 are unsafe.  Some future releases may be able to fix
49       some of this (ie Perl 5.8 or 6.0), but it would be nice to have some
50       safe, portable signal handling now.  Clarification - much of the time,
51       signals are safe enough.  However, if the program employs forking or
52       becomes a daemon which can receive many simultaneous signals, then the
53       signal handling of Perl is normally not sufficient for the task.
54
55       Using a property of the select() function, Net::Server::SIG attempts to
56       fix the unsafe problem.  If a process is blocking on select() any
57       signal will short circuit the select.  Using this concept,
58       Net::Server::SIG does the least work possible (changing one bit from 0
59       to 1).  And depends upon the actual processing of the signals to take
60       place immediately after the the select call via the "check_sigs"
61       function.  See the example shown above and also see the sigtest.pl
62       script located in the examples directory of this distribution.
63

FUNCTIONS

65       "register_sig($SIG => \&code_ref)"
66           Takes key/value pairs where the key is the signal name, and the
67           argument is either a code ref, or the words 'DEFAULT' or 'IGNORE'.
68           The function register_sig must be used in conjuction with
69           check_sigs, and with a blocking select() function call --
70           otherwise, you will observe the registered signal mysteriously
71           vanish.
72
73       "unregister_sig($SIG)"
74           Takes the name of a signal as an argument.  Calls register_sig with
75           a this signal name and 'DEFAULT' as arguments (same as
76           register_sig(SIG,'DEFAULT')
77
78       "check_sigs()"
79           Checks to see if any registered signals have occured.  If so, it
80           will play the registered code ref for that signal.  Return value is
81           array containing any SIGNAL names that had occured.
82

AUTHORS

84       Paul Seamons (paul@seamons.com)
85
86       Rob B Brown (rob@roobik.com) - Provided a sounding board and feedback
87       in creating Net::Server::SIG and sigtest.pl.
88

LICENSE

90         This package may be distributed under the terms of either the
91         GNU General Public License
92           or the
93         Perl Artistic License
94
95         All rights reserved.
96
97
98
99perl v5.12.0                      2007-02-03               Net::Server::SIG(3)
Impressum