|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 (permalink) |
|
Messages: n/a
Hébergeur: |
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/ |
|
|
|
#2 (permalink) |
|
Messages: n/a
Hébergeur: |
Grant wrote:
> [...] > > 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? Grant, it should be enough to set up a lockfile each time the client is about to send a query and remove it just after the reply is read. File existence check and its respective creation should be done atomically. There may be some better way to do it in gawk, but this should generally work well (with bash set as the default shell): system("(set -o noclobber; echo > /var/run/ip2c_lock) 2>/dev/null") As long as the above statement's result is 1, the FIFO is locked by another client. Once the result is 0, we have just locked the FIFO for ourselves. Don't forget to remove it after the reply is read. The simple script for manual query utilizing the locking mechanism would look something like this: #!/bin/sh # Usage: ip2c IPADDR # Atomic file check and creation mechanism in bash set -o noclobber while ! echo > /var/run/ip2c_lock; do usleep; done 2>/dev/null # Remove the lock file no matter how we die trap "rm -f /var/run/ip2c_lock" EXIT SIGINT SIGTERM echo "$1" > /var/run/ip2c_query cat /var/run/ip2c_reply Hope this s, Jan |
|
![]() |
| Outils de la discussion | |
|
|