Implementing read and write locks on files

From: Roger Leigh (${rleigh}_at_invalid.whinlatter.ukfsn.org.invalid)
Date: 09/25/05


Date: Sun, 25 Sep 2005 17:21:13 +0100


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm working on a program called schroot, which is used to enter chroot
environments. It also supports automatic creation and destruction of
chroot environments using LVM snapshots. A prerelease is here:
http://people.debian.org/~rleigh/schroot-0.1.6.tar.bz2

In order to support the snapshot system properly, I need to be able to
create and destroy the chroots independently of running
commands/shells inside the chroot, and I also need to keep a count of
current users so it's not possible to accidentally wipe a chroot which
is in use. This implies keeping session state somewhere.

When a chroot is created, I will write a session file to
/var/lib/schroot/sessions/$UUID.

When a chroot is entered, I will read the session specified by the
user (by UUID), increment the usage count and write it back.

When the chroot is destroyed, I will read the session specified by the
user (by UUID), and destroy it only if the usage count it 0.

Locking during each of these step is simple, if I create a separate
lockfile with O_CREAT|O_EXCL. However, there is also a command to
list the currently active sessions, which requires reading the session
files. I don't want to lock them for writing (I might block
indefinitely, since some chroot types require persistent locks), but I
do want to ensure that it's safe to read (i.e. it's not currently
being written out). How might I do this?

I think I could do this by having separate write and read locks, and
the read lock is taken by the writer to prevent reading during
modification, but not at other times. I'm not an expert by any means,
so was wondering if there were alternative approaches.

Many thanks,
Roger

- --
Roger Leigh
                Printing on GNU/Linux? http://gimp-print.sourceforge.net/
                Debian GNU/Linux http://www.debian.org/
                GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Processed by Mailcrypt 3.5.8+ <http://mailcrypt.sourceforge.net/>

iD8DBQFDNs53VcFcaSW/uEgRAhaYAKCCatsNiAdmpDA2zovA8PbgF5komACeLh45
lB4aBjqExVnucl9Ti/6bIRM=
=M6ar
-----END PGP SIGNATURE-----



Relevant Pages


Loading