PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > Add locking to FIFO (named pipe) client/server?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

Add locking to FIFO (named pipe) client/server?

Réponse
 
LinkBack Outils de la discussion
Vieux 26/08/2006, 00h24   #1 (permalink)
Grant
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Add locking to FIFO (named pipe) client/server?

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/
  Réponse avec citation
Vieux 30/08/2006, 12h55   #2 (permalink)
Jan Tomka
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Add locking to FIFO (named pipe) client/server?

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

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 09h29.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09164 seconds with 10 queries