Locking

Advisory locking

Description
Process have to agree on a semaphore.
When the semaphore is set, a process should not attempt to access the shared resource.

Implementation

Mandatory locking

Description
Processes request locks from an arbitrator
When locks are set, they are enforced by the arbitrator
Locks may be shared/exclusive (put simply: read/write)

Implementation

Locking in Empath

As the reader can see from the above list, there is no locking mechanism that can be considered reliable. If you're screaming 'but flock() is reliable' you're just plain wrong. Reliable on the local host but not over NFS is not reliable. Sometimes NFS is your only option, or removing NFS from a system would be extreme overkill to give reliable mail.

I'm not going to introduce ``it'll probably work'' into Empath. If Empath loses / corrupts mail, I'll be held responsible. People do read their mail over NFS. I've worked at places where that's how the system was set up when I arrived. The only way to fix it was to move everyone to local mailboxes, run all the mail clients on one system, or switch everyone to a Maildir-compliant MUA.

Unfortunately, due to certain management difficulties, changing the mail clients was impossible, so mailboxes had to be moved onto the host that the users' MUA was running on (changing the delivery routines and managing the new storage needed on each host). This was obviously impractical, as anyone who's managed a huge homogenous system will know.

The answer was to POP mail. Not ideal, I know, but at that point there were no graphical mailers (no decent ones) that supported Maildir. POP is easy - the user's mailbox is written wherever they want. I would have put everyone on mutt, but corporate policy dictated that everyone would have a graphical desktop. Pig ignorance and indifference to the technical difficulties involved is difficult to argue with.

Ok, so POP wasn't the best idea. It was easy, and worked, and wasn't going to be going for long (hahaha where did I hear that before ?) IMAP would have been a better solution, but again there weren't many IMAP-aware mailers about.

So, you say, why don't you use flock() for local mailboxes ?
Firstly, how do I know it's a local mailbox and not on NFS ?
Whatever, it's not worth it if I can't be sure.
Secondly, flock() might not exist on the target system, or be implemented via emulation (fcntl()).

There is an answer.

Don't use locks.

Maildir doesn't require locks, and therefore works over NFS too. The implementation must check writes for success - that's all. If you can't write to a mail file, it's not a drastic problem. The message is just considered to be undelivered, rather than lost.

So how do I handle people's mailboxes (mbox, MMDF et al) ?
Just treat them as read-only. I can't damage a mailbox if I don't write to it.

Sorted.