Edward Roper wrote:

> Perhaps someone has or can point me towards a link that would contain C code
> to accomplish the following:
> - Check a users mailbox for an existing lock. If so, keep trying.
>    (sendmail lock style)
> - If no lock exists, lock the file
> - Unlock the file when done.
> Obviously I can go dig through sendmail, pine, mutt, elm code but there is a
> lot of extra code in there to complicate the procedure. I'm looking for minimal
> code.

You might want to look at the source for the `lockfile' program, which
is part of the procmail distribution. This does exactly that.

Basically, you need to create a temporary version of the lockfile
(e.g. /var/spool/mail/joeuser.xxxx), and then use link() to create a
link to it under the name /var/spool/mail/joeuser.lock.

You need to use link() as it's guaranteed to be atomic, even on
NFS-mounted filesystems, whereas open(..., O_CREAT | O_EXCL) isn't. If
link() returns the error EEXIST, then the mailbox is already locked. 
If link() succeeds, then you have successfully locked the mailbox. In
either case, you should then unlink() the temporary file.

You can simply remove the lockfile with unlink() when you've finished
with it.

-- 
Glynn Clements <[EMAIL PROTECTED]>

Reply via email to