script/rtail.pl untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 139 CC 4 SCAR 16.4
CC
4
Cov
0%
MakeSocket
12.0
cleanup
6.0
timer
2.0
CRAP 5.2
1 #!/usr/local/bin/perl
2  
3 eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
4 if 0; # not running under some shell
5 #
6 # Cover for remote use of Tail.pm
7 # Will be called by a local Tail process if a file of type UNIX-REMOTE
8 # is specified.
9 # Called e.g.: rtail.pl -file somefile -heartbeat send -type UNIX
10 # Args are passed directly to WatchFile.
11 # See Tail.pm.
12 #
13 # $Id: rtail.pl,v 2.21 2008/07/03 20:30:58 mprewitt Exp $
14 # $Source: /usr/local/src/perllib/Tail/0.1/RCS/rtail.pl,v $
15 # $Locker: $
16 #
17 # DMJA, Inc <mprewitt@dmja.com>
18 #
19 # Copyright (C) 2003-2008 DMJA, Inc, File::SmartTail comes with
20 # ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to
21 # redistribute it and/or modify it under the same terms as Perl itself.
22 # See the "The Artistic License" L<perlartistic> for more details.
23  
24 use strict;
25  
26 use IO::Socket;
27 use Getopt::Std;
28 use Sys::Hostname;
29 use Fcntl;
30 use File::SmartTail;
31  
32 my %args = @ARGV;
33 if (exists $args{-help}) {
34 die "Usage: $0 -file name -host host[-type UNIX -date parsed -heartbeat send -test yes -tietype [ DB_File | NDBM_File ] ]\n";
35 }
36  
37 my $test = 0;
38 if (exists $args{-test}) {
39 $test++;
40 delete $args{-test};
41 }
42  
43 my $tietype;
44 if (exists $args{-tietype}) {
45 $tietype = $args{-tietype};
46 } else {
47 $tietype = 'DB_File';
48 }
49  
50 my ($port, $sock);
51 if (!$test) {
52 if ( fork ) {
53 #
54 # Parent.
55 #
56 wait;
57 exit 0;
58 } else {
59 ($port, $sock) = MakeSocket();
60 die "failure creating socket [$!]\n"
61 unless $sock;
62
63 $|=1;
64 print "$port\n";
65 close(STDIN);
66 close(STDERR);
67 close(STDOUT);
68 if ( my $pid = fork ) {
69 #
70 # Parent again.
71 #
72 exit 0;
73 }
74 }
75 } else {
76 #
77 # We're testing, so make the socket outside the fork.
78 #
79 ($port, $sock) = MakeSocket();
80 die "failure creating socket [$!]\n"
81 unless $sock;
82  
83 }
84  
85 my $timeout = 45; # seconds
86  
87 $SIG{ALRM} = \&timer;
88 $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = \&cleanup;
89  
90 my @newargs = ( '-tietype' => $tietype );
91 $args{-statuskey} and push @newargs, '-statuskey' => $args{-statuskey};
92 my $tail = new File::SmartTail( @newargs );
93  
94 $tail->WatchFile(%args);
95  
96 open (STDOUT, ">> /tmp/rtail.out.$$"); # Diagnostics.
97 open (STDERR, ">> /tmp/rtail.out.$$"); # Diagnostics.
98 chmod( 0700, "/tmp/rtail.out.$$" );
99  
100 my $oldfh = select(STDOUT); $| = 1; select(STDERR); $| = 1; select($oldfh);
101  
102 alarm $timeout;
103 my $new_sock = $sock->accept();
104 undef $sock;
105  
106 $new_sock->autoflush(1);
107  
108 alarm 0;
109  
110 while (my $line = $tail->GetLine()) {
111 print $new_sock $line || do {
112 print STDERR "Print failed: $!\n";
113 last;
114 };
115 }
116  
117 cleanup();
118  
119 sub timer {
120 print "peer did not connect within $timeout secs.\n";
121 cleanup();
122 }
123  
124 sub cleanup {
125 print "SIGPIPE or timeout. cleaning up.\n";
126 close($sock);
127 close($new_sock);
128 $File::SmartTail::STATFILE->sync if $File::SmartTail::STATFILE;
129 close(STDOUT);
130 exit 0;
131 }
132  
133 sub MakeSocket {
134 #
135 # Seed random number with process id.
136 #
137 srand($$);
138 #
139 # Pick a port
140 #
141 my $port = int (rand(3073) + 1024);
142
143 my $sock;
144 for (my $i = 0; $i < 15; $i++) {
145 $sock = new IO::Socket::INET(LocalPort => $port,
146 Proto => 'tcp',
147 Listen => 5,
148 Reuse => 1);
149 if (defined $sock) {
150 last;
151 #
152 # EBADF here may mean port is in use. Try again.
153 #
154 } elsif ($! =~ /^Bad file/) {
155 $port = int (rand(3073) + 1024);
156 }
157 }
158 return ($port, $sock);
159 }
160  
161