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