#!/usr/bin/perl use strict; use warnings; use IO::Poll qw(POLLIN POLLHUP POLLERR); use IO::Socket; ### Behavior of SIGHUP handler. # # 0 = print string and nothing else. Errno = "Interrupted system call". Expected behaviour. # 1 = same as 0 plus open/close of filehandle. Errno = "Inappropriate ioctl for device" regardless of contents in file. # 2 = same as 1 plus reading a line from file into scalar before closing. If file is empty, then errno is empty. Otherwise, Errno = "Bad file descriptor" # 3 = same as 2 but setting $/ to undef, reading until end of file into scalar. Errno is emtpy regardless of contents in file. # 4 = Similar to 2/3 but reading file into an array. Errno is empty regardless of contents in file. # my $behaviour = 0; ### Variables you might want to change. my $file = "file.txt"; my $poll_timeout = 5; my $handle; my $pret; my $readbuffer; my $rret; my $string; my @ready; my @strings; unless (-r $file) { print "File \"$file\" is not readable...\n"; exit 1; } sub SIGHUP_handler { print "Got SIGHUP...\n"; if ($behaviour == 1) { open(FILE, $file); close FILE; } elsif ($behaviour == 2) { open(FILE, $file); $string = ; close FILE; } elsif ($behaviour == 3) { open(FILE, $file); undef $/; $string = ; close FILE; } elsif ($behaviour == 4) { open(FILE, $file); @strings = ; close FILE; } } $SIG{HUP} = 'SIGHUP_handler'; my $poll = IO::Poll->new; my $sock = new IO::Socket::INET( PeerAddr => "127.0.0.1", PeerPort => "12000", Proto => 'tcp') or die "Error creating socket: $!"; $poll->mask($sock, POLLIN); while(1) { $pret = $poll->poll($poll_timeout); if ($pret == 0) { syswrite(STDOUT, "Timeout occured...\n"); } elsif ($pret == -1) { syswrite(STDOUT, "Error occured: $!\n"); } else { @ready = $poll->handles(); foreach $handle (@ready) { $rret = sysread($handle, $readbuffer, 1024); if ($rret == 0) { syswrite(STDOUT, "No data to read on socket, exiting...\n"); exit 1; } syswrite(STDOUT, "msg: $readbuffer"); } } }