0e14e8de665ba05ce5ec41d87a4153a835f9b1cf
[mmh] / sbr / lock_file.c
1
2 /*
3  * lock.c -- routines to lock/unlock files
4  *
5  * This code is Copyright (c) 2002, by the authors of nmh.  See the
6  * COPYRIGHT file in the root directory of the nmh distribution for
7  * complete copyright information.
8  */
9
10 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
11  *
12  * Since liblockfile locking shares most of its code with dot locking, it
13  * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
14  *
15  * Ruud de Rooij <ruud@debian.org>  Sun, 28 Mar 1999 15:34:03 +0200
16  */
17  
18 #include <h/mh.h>
19 #include <h/signals.h>
20 #include <h/utils.h>
21
22 #ifdef TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # ifdef TM_IN_SYS_TIME
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32 #include <errno.h>
33
34 #ifdef HAVE_FCNTL_H
35 # include <fcntl.h>
36 #else
37 # include <sys/file.h>
38 #endif
39
40 #if defined(LOCKF_LOCKING) || defined(FLOCK_LOCKING)
41 # include <sys/file.h>
42 #endif
43
44 #include <signal.h>
45
46 #if defined(HAVE_LIBLOCKFILE)
47 #include <lockfile.h>
48 #endif
49
50 #ifdef LOCKDIR
51 char *lockdir = LOCKDIR;
52 #endif
53
54 /* Are we using any kernel locking? */
55 #if defined (FLOCK_LOCKING) || defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
56 # define KERNEL_LOCKING
57 #endif
58
59 #ifdef DOT_LOCKING
60
61 /* struct for getting name of lock file to create */
62 struct lockinfo {
63     char curlock[BUFSIZ];
64 #if !defined(HAVE_LIBLOCKFILE)
65     char tmplock[BUFSIZ];
66 #endif
67 };
68
69 /*
70  * Amount of time to wait before
71  * updating ctime of lock file.
72  */
73 #define NSECS 20
74
75 #if !defined(HAVE_LIBLOCKFILE)
76 /*
77  * How old does a lock file need to be
78  * before we remove it.
79  */
80 #define RSECS 180
81 #endif /* HAVE_LIBLOCKFILE */
82
83 /* struct for recording and updating locks */
84 struct lock {
85     int l_fd;
86     char *l_lock;
87     struct lock *l_next;
88 };
89
90 /* top of list containing all open locks */
91 static struct lock *l_top = NULL;
92 #endif /* DOT_LOCKING */
93
94 /*
95  * static prototypes
96  */
97 #ifdef KERNEL_LOCKING
98 static int lkopen_kernel (char *, int, mode_t);
99 #endif
100
101 #ifdef DOT_LOCKING
102 static int lkopen_dot (char *, int, mode_t);
103 static void lockname (char *, struct lockinfo *, int);
104 static void timerON (char *, int);
105 static void timerOFF (int);
106 static RETSIGTYPE alrmser (int);
107
108 #if !defined(HAVE_LIBLOCKFILE)
109 static int lockit (struct lockinfo *);
110 #endif
111 #endif
112
113 /*
114  * Base routine to open and lock a file,
115  * and return a file descriptor.
116  */
117
118 int
119 lkopen (char *file, int access, mode_t mode)
120 {
121 #ifdef KERNEL_LOCKING
122     return lkopen_kernel(file, access, mode);
123 #endif
124
125 #ifdef DOT_LOCKING
126     return lkopen_dot(file, access, mode);
127 #endif
128 }
129
130
131 /*
132  * Base routine to close and unlock a file,
133  * given a file descriptor.
134  */
135
136 int
137 lkclose (int fd, char *file)
138 {
139 #ifdef FCNTL_LOCKING
140     struct flock buf;
141 #endif
142
143 #ifdef DOT_LOCKING
144     struct lockinfo lkinfo;
145 #endif
146
147     if (fd == -1)
148         return 0;
149
150 #ifdef FCNTL_LOCKING
151     buf.l_type   = F_UNLCK;
152     buf.l_whence = SEEK_SET;
153     buf.l_start  = 0;
154     buf.l_len    = 0;
155     fcntl(fd, F_SETLK, &buf);
156 #endif
157
158 #ifdef FLOCK_LOCKING
159     flock (fd, LOCK_UN);
160 #endif
161
162 #ifdef LOCKF_LOCKING
163     /* make sure we unlock the whole thing */
164     lseek (fd, (off_t) 0, SEEK_SET);
165     lockf (fd, F_ULOCK, 0L);
166 #endif  
167
168 #ifdef DOT_LOCKING
169     lockname (file, &lkinfo, 0);        /* get name of lock file */
170 #if !defined(HAVE_LIBLOCKFILE)
171     unlink (lkinfo.curlock);            /* remove lock file      */
172 #else
173     lockfile_remove(lkinfo.curlock);
174 #endif /* HAVE_LIBLOCKFILE */
175     timerOFF (fd);                      /* turn off lock timer   */
176 #endif /* DOT_LOCKING */
177
178     return (close (fd));
179 }
180
181
182 /*
183  * Base routine to open and lock a file,
184  * and return a FILE pointer
185  */
186
187 FILE *
188 lkfopen (char *file, char *mode)
189 {
190     int fd, access;
191     FILE *fp;
192
193     if (strcmp (mode, "r") == 0)
194         access = O_RDONLY;
195     else if (strcmp (mode, "r+") == 0)
196         access = O_RDWR;
197     else if (strcmp (mode, "w") == 0)
198         access = O_WRONLY | O_CREAT | O_TRUNC;
199     else if (strcmp (mode, "w+") == 0)
200         access = O_RDWR | O_CREAT | O_TRUNC;
201     else if (strcmp (mode, "a") == 0)
202         access = O_WRONLY | O_CREAT | O_APPEND;
203     else if (strcmp (mode, "a+") == 0)
204         access = O_RDWR | O_CREAT | O_APPEND;
205     else {
206         errno = EINVAL;
207         return NULL;
208     }
209
210     if ((fd = lkopen (file, access, 0666)) == -1)
211         return NULL;
212
213     if ((fp = fdopen (fd, mode)) == NULL) {
214         close (fd);
215         return NULL;
216     }
217
218     return fp;
219 }
220
221
222 /*
223  * Base routine to close and unlock a file,
224  * given a FILE pointer
225  */
226
227 int
228 lkfclose (FILE *fp, char *file)
229 {
230 #ifdef FCNTL_LOCKING
231     struct flock buf;
232 #endif
233
234 #ifdef DOT_LOCKING
235     struct lockinfo lkinfo;
236 #endif
237
238     if (fp == NULL)
239         return 0;
240
241 #ifdef FCNTL_LOCKING
242     buf.l_type   = F_UNLCK;
243     buf.l_whence = SEEK_SET;
244     buf.l_start  = 0;
245     buf.l_len    = 0;
246     fcntl(fileno(fp), F_SETLK, &buf);
247 #endif
248
249 #ifdef FLOCK_LOCKING
250     flock (fileno(fp), LOCK_UN);
251 #endif
252
253 #ifdef LOCKF_LOCKING
254     /* make sure we unlock the whole thing */
255     fseek (fp, 0L, SEEK_SET);
256     lockf (fileno(fp), F_ULOCK, 0L);
257 #endif
258
259 #ifdef DOT_LOCKING
260     lockname (file, &lkinfo, 0);        /* get name of lock file */
261 #if !defined(HAVE_LIBLOCKFILE)
262     unlink (lkinfo.curlock);            /* remove lock file      */
263 #else
264     lockfile_remove(lkinfo.curlock);
265 #endif /* HAVE_LIBLOCKFILE */
266     timerOFF (fileno(fp));              /* turn off lock timer   */
267 #endif /* DOT_LOCKING */
268
269     return (fclose (fp));
270 }
271
272
273 #ifdef KERNEL_LOCKING
274
275 /*
276  * open and lock a file, using kernel locking
277  */
278
279 static int
280 lkopen_kernel (char *file, int access, mode_t mode)
281 {
282     int fd, i, j;
283
284 # ifdef FCNTL_LOCKING
285     struct flock buf;
286 # endif /* FCNTL_LOCKING */
287
288     for (i = 0; i < 5; i++) {
289
290 # if defined(LOCKF_LOCKING) || defined(FCNTL_LOCKING)
291         /* remember the original mode */
292         j = access;
293
294         /* make sure we open at the beginning */
295         access &= ~O_APPEND;
296
297         /*
298          * We MUST have write permission or
299          * lockf/fcntl() won't work
300          */
301         if ((access & 03) == O_RDONLY) {
302             access &= ~O_RDONLY;
303             access |= O_RDWR;
304         }
305 # endif /* LOCKF_LOCKING || FCNTL_LOCKING */
306
307         if ((fd = open (file, access | O_NDELAY, mode)) == -1)
308             return -1;
309
310 # ifdef FCNTL_LOCKING
311         buf.l_type   = F_WRLCK;
312         buf.l_whence = SEEK_SET;
313         buf.l_start  = 0;
314         buf.l_len    = 0;
315         if (fcntl (fd, F_SETLK, &buf) != -1)
316             return fd;
317 # endif
318
319 # ifdef FLOCK_LOCKING
320         if (flock (fd, (((access & 03) == O_RDONLY) ? LOCK_SH : LOCK_EX)
321                    | LOCK_NB) != -1)
322             return fd;
323 # endif
324
325 # ifdef LOCKF_LOCKING
326         if (lockf (fd, F_TLOCK, 0L) != -1) {
327             /* see if we should be at the end */
328             if (j & O_APPEND)
329                 lseek (fd, (off_t) 0, SEEK_END);
330             return fd;
331         }
332 # endif
333
334         j = errno;
335         close (fd);
336         sleep (5);
337     }
338
339     close (fd);
340     errno = j;
341     return -1;
342 }
343
344 #endif /* KERNEL_LOCKING */
345
346
347 #ifdef DOT_LOCKING
348
349 /*
350  * open and lock a file, using dot locking
351  */
352
353 static int
354 lkopen_dot (char *file, int access, mode_t mode)
355 {
356     int fd;
357     struct lockinfo lkinfo;
358
359     /* open the file */
360     if ((fd = open (file, access, mode)) == -1)
361         return -1;
362
363     /*
364      * Get the name of the eventual lock file, as well
365      * as a name for a temporary lock file.
366      */
367     lockname (file, &lkinfo, 1);
368
369 #if !defined(HAVE_LIBLOCKFILE)
370     {
371         int i;
372         for (i = 0;;) {
373             /* attempt to create lock file */
374             if (lockit (&lkinfo) == 0) {
375                 /* if successful, turn on timer and return */
376                 timerON (lkinfo.curlock, fd);
377                 return fd;
378             } else {
379                 /*
380                  * Abort locking, if we fail to lock after 5 attempts
381                  * and are never able to stat the lock file.
382                  */
383                 struct stat st;
384                 if (stat (lkinfo.curlock, &st) == -1) {
385                     if (i++ > 5)
386                         return -1;
387                     sleep (5);
388                 } else {
389                     time_t curtime;
390                     i = 0;
391                     time (&curtime);
392                     
393                     /* check for stale lockfile, else sleep */
394                     if (curtime > st.st_ctime + RSECS)
395                         unlink (lkinfo.curlock);
396                     else
397                         sleep (5);
398                 }
399                 lockname (file, &lkinfo, 1);
400             }
401         }
402     }
403 #else
404     if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
405         timerON(lkinfo.curlock, fd);
406         return fd;
407     }
408     else {
409         close(fd);
410         return -1;
411     }
412 #endif /* HAVE_LIBLOCKFILE */
413 }
414
415 #if !defined(HAVE_LIBLOCKFILE)
416 /*
417  * Routine that actually tries to create
418  * the lock file.
419  */
420
421 static int
422 lockit (struct lockinfo *li)
423 {
424     int fd;
425     char *curlock, *tmplock;
426
427 #if 0
428     char buffer[128];
429 #endif
430
431     curlock = li->curlock;
432     tmplock = li->tmplock;
433
434 #ifdef HAVE_MKSTEMP
435     if ((fd = mkstemp(tmplock)) == -1)
436         return -1;
437 #else
438     if (mktemp(tmplock) == NULL)
439         return -1;
440     if (unlink(tmplock) == -1 && errno != ENOENT)
441         return -1;
442     /* create the temporary lock file */
443     if ((fd = creat(tmplock, 0600)) == -1)
444         return -1;
445 #endif
446
447 #if 0
448     /* write our process id into lock file */
449     snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
450     write(fd, buffer, strlen(buffer) + 1);
451 #endif
452
453     close (fd);
454
455     /*
456      * Now try to create the real lock file
457      * by linking to the temporary file.
458      */
459     fd = link(tmplock, curlock);
460     unlink(tmplock);
461
462     return (fd == -1 ? -1 : 0);
463 }
464 #endif /* HAVE_LIBLOCKFILE */
465
466 /*
467  * Get name of lock file, and temporary lock file
468  */
469
470 static void
471 lockname (char *file, struct lockinfo *li, int isnewlock)
472 {
473     int bplen, tmplen;
474     char *bp, *cp;
475
476 #if 0
477     struct stat st;
478 #endif
479
480     if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
481         cp = file;
482
483     bp = li->curlock;
484     bplen = 0;
485 #ifdef LOCKDIR
486     snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
487     tmplen = strlen (bp);
488     bp    += tmplen;
489     bplen += tmplen;
490 #else
491     if (cp != file) {
492         snprintf (bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
493         tmplen = strlen (bp);
494         bp    += tmplen;
495         bplen += tmplen;
496     }
497 #endif
498
499 #if 0
500     /*
501      * mmdf style dot locking.  Currently not supported.
502      * If we start supporting mmdf style dot locking,
503      * we will need to change the return value of lockname
504      */
505     if (stat (file, &st) == -1)
506         return -1;
507
508     snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
509         st.st_dev, st.st_ino);
510 #endif
511
512     snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
513
514 #if !defined(HAVE_LIBLOCKFILE)
515     /*
516      * If this is for a new lock, create a name for
517      * the temporary lock file for lockit()
518      */
519     if (isnewlock) {
520         if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
521             strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
522         else
523             snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
524                      (int)(cp - li->curlock), li->curlock);
525     }
526 #endif
527 }
528
529
530 /*
531  * Add new lockfile to the list of open lockfiles
532  * and start the lock file timer.
533  */
534
535 static void
536 timerON (char *curlock, int fd)
537 {
538     struct lock *lp;
539     size_t len;
540
541     lp = (struct lock *) mh_xmalloc (sizeof(*lp));
542
543     len = strlen(curlock) + 1;
544     lp->l_fd = fd;
545     lp->l_lock = mh_xmalloc (len);
546     memcpy (lp->l_lock, curlock, len);
547     lp->l_next = l_top;
548
549     if (!l_top) {
550         /* perhaps SIGT{STP,TIN,TOU} ? */
551         SIGNAL (SIGALRM, alrmser);
552         alarm (NSECS);
553     }
554
555     l_top = lp;
556 }
557
558
559 /*
560  * Search through the list of lockfiles for the
561  * current lockfile, and remove it from the list.
562  */
563
564 static void
565 timerOFF (int fd)
566 {
567     struct lock *pp, *lp;
568
569     alarm(0);
570
571     if (l_top) {
572         for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
573             if (lp->l_fd == fd)
574                 break;
575         }
576         if (lp) {
577             if (lp == l_top)
578                 l_top = lp->l_next;
579             else
580                 pp->l_next = lp->l_next;
581
582             free (lp->l_lock);
583             free (lp);
584         }
585     }
586
587     /* if there are locks left, restart timer */
588     if (l_top)
589         alarm (NSECS);
590 }
591
592
593 /*
594  * If timer goes off, we update the ctime of all open
595  * lockfiles, so another command doesn't remove them.
596  */
597
598 static RETSIGTYPE
599 alrmser (int sig)
600 {
601     char *lockfile;
602     struct lock *lp;
603
604 #ifndef RELIABLE_SIGNALS
605     SIGNAL (SIGALRM, alrmser);
606 #endif
607
608     /* update the ctime of all the lock files */
609     for (lp = l_top; lp; lp = lp->l_next) {
610         lockfile = lp->l_lock;
611 #if !defined(HAVE_LIBLOCKFILE)
612         {
613             int j;
614             if (*lockfile && (j = creat (lockfile, 0600)) != -1)
615                 close (j);
616         }
617 #else
618     lockfile_touch(lockfile);
619 #endif
620     }
621
622     /* restart the alarm */
623     alarm (NSECS);
624 }
625
626 #endif /* DOT_LOCKING */