Hi there,
For some months I've been running a gawk program via this bash fragment
to put it into the background:
FIFO_PERMS="660"
FIFO_OWNER="root:wheel"
IP2C_PATH="/usr/local/sbin"
IP2C_PID="/var/run/ip2c.pid"
....
rm -f /var/run/ip2c_{query,reply}
mkfifo -m $FIFO_PERMS /var/run/ip2c_query || exit 2
mkfifo -m $FIFO_PERMS /var/run/ip2c_reply || exit 2
chown $FIFO_OWNER /var/run/ip2c_{query,reply} || exit 2
setsid $IP2C_PATH/ip2c-server 0<> /dev/null >&0 2>&0 &
echo "$!" > $IP2C_PID
The gawk server loop:
for (;

{
getline query < ip2c_query
close(ip2c_query, "from")
print search_ip2country(query) > ip2c_reply
close(ip2c_reply, "to")
}
The gawk client code:
function search_ip2country(a, o)
{
# entry: dotquad IP, returns cidr/nn:ccx or -:-- if not found
if (no_ip2c) return "-:-- " # no database loaded
if (client) { # perhaps use optional server
print a > ip2c_query
close(ip2c_query, "to")
getline o < ip2c_reply
close(ip2c_reply, "from")
if (!o) {
client = 0 # no server? go standalone mode
o = "No ip2c-server, load database files"
read_database_files(o)
search_ip2country(a)
}
return o
}
# standalone operation
if (!ip_size) { # shutdown ip2c if no database loaded
++no_ip2c; return "-:-- "
}
....
An ad hoc CLI query looks like this:
grant@deltree:~$ echo "220.240.123.123" > /var/run/ip2c_query
grant@deltree:~$ cat /var/run/ip2c_reply
220.240.0.0/16:AU :Australia:3706716160:3706781695
My problem is that there's no file locking, a cron job runs each hour
throwing several hundred queries at the server via the FIFOs, if I
happen to run a manual query at that time the thing may lockup.
I'm looking for ideas on how to add exclusive access locking?
Am running bash 3.1.17, gawk 3.1.5 on slackware-10.2.
Thanks,
Grant.
--
http://bugsplatter.mine.nu/