Subject: [HELP] Folder locking mechanism

From: [email-masked] (Thang M Nguyen)
Date: Fri, 15 Jul 1994 04:39:00 GMT

Hello,

I am needing help on how the folder locking mechanism works within PINE.  
I would like to know what does pine has to go through in order to do the 
folder locking.  In other words, what is the algorithm/procedures of the 
locking mechanism.  If any of you out there know about this problem, can 
you email to me or post on this newsgroup.  I am very appreciate it.  
Thank you.

--thang

---

From: [email-masked] (Jim Davis)
Date: 14 Jul 1994 23:08:34 -0700

In article <[email-masked]>,
Thang M Nguyen <[email-masked]> wrote:
:Hello,
:
:I am needing help on how the folder locking mechanism works within PINE.  
:I would like to know what does pine has to go through in order to do the 
:folder locking.  In other words, what is the algorithm/procedures of the 
:locking mechanism.

You have the source (or can fetch it from ftp.cac.washington.edu); if
you're in a hurry then a tool like trace or truss can give some hints.
For instance, I ran 'truss -topen pine' and here's (part of) the
output.  In that session, I saved a message from my inbox to the
saved-messages folder.  It looks like on my system, pine uses a lock
file.  (That might well be system dependent.)

open("/var/mail/jdavis.lock", O_WRONLY|O_CREAT|O_EXCL, 0666) = 6
open("/var/mail/jdavis", O_RDWR, 0)		= 6
open("/home/jdavis/mail/saved-messages", O_RDONLY, 0) = 6
open("/home/jdavis/mail/saved-messages", O_RDONLY, 0) = 6
open("/home/jdavis/mail/saved-messages.lock", O_WRONLY|O_CREAT|O_EXCL, 0666) = 6
open("/home/jdavis/mail/saved-messages", O_WRONLY|O_APPEND|O_CREAT, 0600) = 6
open("/var/mail/jdavis.lock", O_WRONLY|O_CREAT|O_EXCL, 0666) = 6

---

From: David L Miller <[email-masked]>
Date: Fri, 15 Jul 1994 14:23:59 -0700 (PDT)

>From the upcoming Pine FAQ:

What is folder locking and how does it work? 

Locks are used by Pine and other mail programs to prevent damage from
occurring to the mail file when multiple programs try to write to the file at
the same time. 

Because there are many different schemes of mail file locking used on UNIX,
Pine implements all of them. The result is a lot of complexity. 

There are several reasons why locking needs to be done: 

 1. If you want to read the mail file, you want to make sure that no other
process will modify the mail file while you are reading it. 

 2. If you want to write to the mail file, you want to make sure that no
other process is accessing the mail file while you are writing it. 

 3. If you have the mail file open, you want to make sure that no other
process can alter any of the internal contents of the mail file that you have
read, but it is OK if another process appends new data to the mail file. 

 4. If you want to alter any of the internal contents of the mail file, you
want to make sure that no other process has the mail file open. 

There are several mechanisms of locking: 

The creation of a file which has the same name as the mail file, but with a
suffix of ".lock" (for example, this lock for /usr/spool/mail/isma is named
/usr/spool/mail/isma.lock). This file accomplishes locks (1) and (2) above. 
This is an exclusive lock. 
   The use of an flock() with LOCK_SH on the mail file. This accomplishes
lock (1), and prevents lock (2). Multiple processes can do this. 
   The use of an flock() with LOCK_EX on the mail file. This accomplishes
lock (2), and prevents lock (1). This is an exclusive lock. 
   The use of an flock() with LOCK_EX on a file on /tmp. The file name used
depends upon the version of Pine. This accomplishes locks (3) and (4). This
is an exclusive lock. 

On SVR4-based systems, the lockf() subroutine or fcntl() system call it used
instead of flock(). It is rumored that this creates a kind of lock file as
well, but this has not been directly verified. 

[MRC]

--DLM

|\ |  |\/|  David L. Miller    [email-masked]  (206) 685-6240
|/ |_ |  |  Software Engineer, Pine Development Team   (206) 685-4045 (FAX)
University of Washington, Networks & Distributed Computing, JE-20
4545 15th Ave NE, Seattle WA 98105, USA

---

From: Shahjehan Khatri <[email-masked]>
Date: Fri, 15 Jul 1994 18:52:45 -0700

> Pine implements all of them.

_All_?  How did you find out the scheme _I_ use? = )

I am locked out of a Friday-night date, so decided to make up by reading
"c-client" code having to do with locks....

>  * Program:	Berkeley mail routines
>  *
>  * Author:	Mark Crispin

Mark, see signature. = )

> int bezerk_lock (file,flags,mode,lock,op)
> 	char *file;
> 	int flags;
> 	int mode;
> 	char *lock;
> 	int op;
> {
>   int fd,ld,j;
>   int i = LOCKTIMEOUT * 60 - 1;
>   char tmp[MAILTMPLEN];
>   struct timeval tp;
>   struct stat sb;
> 				/* build lock filename */
>   strcat (bezerk_file (lock,file),".lock");
>   do {				/* until OK or out of tries */
>     gettimeofday (&tp,NIL);	/* get the time now */

[...]

> 				/* try to get the lock */
>     if ((ld = open (lock,O_WRONLY|O_CREAT|O_EXCL,0666)) < 0) switch (errno) {
>     case EEXIST:		/* if extant and old, grab it for ourselves */
>       if ((!stat (lock,&sb)) && tp.tv_sec > sb.st_ctime + LOCKTIMEOUT * 60)
> 	ld = open (lock,O_WRONLY|O_CREAT,0666);
>       break;

Pardon the unaffected simplicy, but what happens if multiple processes
decide to "grab it for ourselves." 

[...]

>     if ((ld < 0) && *lock) {	/* if failed to make lock file and retry OK */
>       if (!(i%15)) {
> 	sprintf (tmp,"Mailbox %s is locked, will override in %d seconds...",
> 		 file,i);
> 	mm_log (tmp,WARN);
>       }
>       sleep (1);		/* wait 1 second before next try */
>     }
>   } while (i-- && ld < 0 && *lock);
> 				/* open file */
>   if ((fd = open (file,flags,mode)) >= 0) flock (fd,op);
>   else {			/* open failed */
>     j = errno;			/* preserve error code */
>     if (*lock) unlink (lock);	/* flush the lock file if any */
>     errno = j;			/* restore error code */
>   }
>   return fd;
> }

Please explain why you don't check whether "flock (fd,op)" above succeeds.

Thanks!

--

"The sin of my ingratitude even now
Was heavy on me.  Thou art so far before,
That swiftest wing of recompense is slow
To overtake thee.  Would thou hadst less deserv'd,
That the proportion both of thanks and payment
Might have been mine!  Only I have left to say,
More is thy due than more than all can pay."

                              -- Shakespeare