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