| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
|
| 4 |
use File::Basename qw(basename); |
| 5 |
use Getopt::Long; |
| 6 |
use IO::Handle; |
| 7 |
|
| 8 |
my $default_delay = 0.5; |
| 9 |
my $program = basename($0); |
| 10 |
my %opts = (); |
| 11 |
|
| 12 |
GetOptions(\%opts, 'delay=f', 'fifo=s', 'pidfile=s', 'help', 'verbose'); |
| 13 |
|
| 14 |
usage() if $opts{'help'}; |
| 15 |
|
| 16 |
my $delay = $opts{'delay'} ? $opts{'delay'} : $default_delay; |
| 17 |
|
| 18 |
die "$program: missing required --fifo parameter\n" unless $opts{'fifo'}; |
| 19 |
my $fifo = $opts{'fifo'}; |
| 20 |
|
| 21 |
my $verbose = $opts{'verbose'} ? 1 : 0; |
| 22 |
|
| 23 |
open(my $fifo_fh, "+> $fifo") or die "$program: unable to open $fifo: $!\n"; |
| 24 |
|
| 25 |
if ($opts{'pidfile'}) { |
| 26 |
my $path = $opts{'pidfile'}; |
| 27 |
|
| 28 |
if (open(my $fh, ">> $path")) { |
| 29 |
print $fh "$$\n"; |
| 30 |
unless (close($fh)) { |
| 31 |
die("$program: can't write pidfile '$path': $!\n"); |
| 32 |
} |
| 33 |
|
| 34 |
} else { |
| 35 |
die("$program: can't open pidfile '$path': $!\n"); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
while (1) { |
| 40 |
my $key = <$fifo_fh>; |
| 41 |
chomp($key); |
| 42 |
print STDERR "$program: read '$key'\n" if $verbose; |
| 43 |
|
| 44 |
# Lookup a value for the given key. |
| 45 |
my $value = lookup_value($key); |
| 46 |
|
| 47 |
print $fifo_fh "$value\n" if $verbose; |
| 48 |
$fifo_fh->flush(); |
| 49 |
|
| 50 |
print STDERR "$program: wrote '$value'\n" if $verbose; |
| 51 |
|
| 52 |
# Wait for the buffer's byte to be cleared before reading again. |
| 53 |
# wait_fifo($fifo_fh); |
| 54 |
} |
| 55 |
|
| 56 |
close($fifo_fh); |
| 57 |
print STDOUT "$program: done\n" if $verbose; |
| 58 |
|
| 59 |
exit 0; |
| 60 |
|
| 61 |
# -------------------------------------------------------------------------- |
| 62 |
sub lookup_value { |
| 63 |
my ($key) = @_; |
| 64 |
|
| 65 |
# NOTE: do something to obtain a value for the given key here. |
| 66 |
chomp(my $value = $key); |
| 67 |
|
| 68 |
$value = join('', reverse(split(//, $value))); |
| 69 |
return $value; |
| 70 |
} |
| 71 |
|
| 72 |
# -------------------------------------------------------------------------- |
| 73 |
sub usage { |
| 74 |
print STDOUT <<END_OF_USAGE; |
| 75 |
|
| 76 |
usage: $program [options] |
| 77 |
|
| 78 |
--delay Configure the buffer check delay. |
| 79 |
The default is $default_delay seconds. |
| 80 |
|
| 81 |
--fifo Configure the path to the FIFO. Required. |
| 82 |
|
| 83 |
--help Displays this message. |
| 84 |
|
| 85 |
--verbose Enables verbose output while $program runs. |
| 86 |
|
| 87 |
END_OF_USAGE |
| 88 |
|
| 89 |
exit 0; |
| 90 |
} |
| 91 |
|
| 92 |
# -------------------------------------------------------------------------- |
| 93 |
sub wait_fifo { |
| 94 |
my ($fh) = @_; |
| 95 |
|
| 96 |
# Now we get tricky. Use ioctl(2) to poll the number of bytes to |
| 97 |
# be read from the FIFO filehandle. When the number drops to zero, |
| 98 |
# it means that the data we just wrote has been read from the buffer |
| 99 |
# by some other process, so we can go back to the top of this loop. |
| 100 |
# Otherwise, if this program loops faster than the reader/writer on |
| 101 |
# the other end of the FIFO, we'd end up reading the data we just |
| 102 |
# wrote. Quite annoying, actually. |
| 103 |
# |
| 104 |
# Note: this value must be manually extracted from the system header files |
| 105 |
# using the following program: |
| 106 |
# |
| 107 |
# -------- fionread.c ------------------- |
| 108 |
# #include <stdio.h> |
| 109 |
# #include <sys/ioctl.h> |
| 110 |
# |
| 111 |
# int main(int argc, char *argv[]) { |
| 112 |
# printf("%#08x\n", FIONREAD); |
| 113 |
# return 0; |
| 114 |
# } |
| 115 |
# --------------------------------------- |
| 116 |
# |
| 117 |
# > cc -o fionread fionread.c |
| 118 |
# > ./fionread |
| 119 |
|
| 120 |
my $FIONREAD = 0x00541b; |
| 121 |
|
| 122 |
# Hack for my Mac laptop |
| 123 |
if ($^O eq 'darwin') { |
| 124 |
$FIONREAD = 0x4004667f; |
| 125 |
} |
| 126 |
|
| 127 |
my $size = pack('L', 0); |
| 128 |
ioctl($fh, $FIONREAD, $size) or die "$program: unable to use ioctl: $!\n"; |
| 129 |
$size = unpack('L', $size); |
| 130 |
|
| 131 |
while ($size != 0) { |
| 132 |
print STDERR "$program: waiting for buffer ($size bytes) to be read\n" if $verbose; |
| 133 |
select(undef, undef, undef, $delay); |
| 134 |
|
| 135 |
$size = pack('L', 0); |
| 136 |
ioctl($fh, $FIONREAD, $size) or die "$program: unable to use ioctl: $!\n"; |
| 137 |
$size = unpack('L', $size); |
| 138 |
} |
| 139 |
} |