1Net::Server::SIG(3) User Contributed Perl Documentation Net::Server::SIG(3)
2
3
4
6 Net::Server::SIG - adpf - Safer signal handling
7
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
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 sig‐
57 nal 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" func‐
61 tion. See the example shown above and also see the sigtest.pl script
62 located in the examples directory of this distribution.
63
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 -- other‐
70 wise, you will observe the registered signal mysteriously vanish.
71
72 "unregister_sig($SIG)"
73 Takes the name of a signal as an argument. Calls register_sig with
74 a this signal name and 'DEFAULT' as arguments (same as regis‐
75 ter_sig(SIG,'DEFAULT')
76
77 "check_sigs()"
78 Checks to see if any registered signals have occured. If so, it
79 will play the registered code ref for that signal. Return value is
80 array containing any SIGNAL names that had occured.
81
83 Paul Seamons (paul@seamons.com)
84
85 Rob B Brown (rob@roobik.com) - Provided a sounding board and feedback
86 in creating Net::Server::SIG and sigtest.pl.
87
89 This package may be distributed under the terms of either the
90 GNU General Public License
91 or the
92 Perl Artistic License
93
94 All rights reserved.
95
96
97
98perl v5.8.8 2007-02-03 Net::Server::SIG(3)