Signal Handler for sigpipe
Correct multithread with detach Reuse for the socket
This commit is contained in:
parent
be857877d3
commit
315ddffeb2
1 changed files with 16 additions and 4 deletions
20
gopher.pl
20
gopher.pl
|
@ -6,7 +6,6 @@
|
|||
#
|
||||
# TODO:
|
||||
# - Support for a list of tags
|
||||
# - Cleanup on ctrl-c
|
||||
# - Default port if failure
|
||||
# - ...
|
||||
|
||||
|
@ -27,16 +26,20 @@ print "Port is " . $port . "\n";
|
|||
print "Base dir is " . $basedir . "\n";
|
||||
|
||||
$SIG{INT} = \&signal_handler_interrupt;
|
||||
$SIG{PIPE} = \&signal_handler_pipe;
|
||||
|
||||
my $socket = new IO::Socket::INET (LocalPort => $port, Proto => 'tcp', Listen => '1');
|
||||
die "Unable to open port\n" unless $socket;
|
||||
my $socket = new IO::Socket::INET(LocalPort => $port,
|
||||
Proto => 'tcp',
|
||||
Listen => '1',
|
||||
Reuse => 1);
|
||||
die "Unable to open port: $! \n" unless $socket;
|
||||
|
||||
drop_privileges('nobody');
|
||||
|
||||
while (1) {
|
||||
my $listen_socket = $socket->accept();
|
||||
my $thr = threads->create(\&connection_thread, $listen_socket);
|
||||
$thr->join();
|
||||
$thr->detach();
|
||||
}
|
||||
|
||||
sub connection_thread {
|
||||
|
@ -63,6 +66,8 @@ sub connection_thread {
|
|||
}
|
||||
|
||||
$listen_socket->close();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub generate_header {
|
||||
|
@ -164,5 +169,12 @@ sub send_blog_entry {
|
|||
|
||||
sub signal_handler_interrupt {
|
||||
print "ctrl-c, exiting...\n";
|
||||
$socket->shutdown(2);
|
||||
$socket->close();
|
||||
exit 0;
|
||||
}
|
||||
|
||||
sub signal_handler_pipe {
|
||||
print "sigpipe, don't care\n";
|
||||
return 0;
|
||||
}
|
||||
|
|
Reference in a new issue