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