]> diplodocus.org Git - nmh/blob - sbr/lock_file.c
mh-sequence.man: document new '=+' and '=-' for selecting relative msgs
[nmh] / 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 #include <h/mts.h>
22
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
25 #endif
26 #include <time.h>
27 #include <fcntl.h>
28 #ifdef HAVE_FLOCK
29 # include <sys/file.h>
30 #endif
31 #ifdef HAVE_LOCKF
32 # include <unistd.h>
33 #endif
34
35 #if defined(HAVE_LIBLOCKFILE)
36 #include <lockfile.h>
37 #endif
38
39 #ifdef LOCKDIR
40 char *lockdir = LOCKDIR;
41 #endif
42
43 /* struct for getting name of lock file to create */
44 struct lockinfo {
45 char curlock[BUFSIZ];
46 #if !defined(HAVE_LIBLOCKFILE)
47 char tmplock[BUFSIZ];
48 #endif
49 };
50
51 /*
52 * Number of tries to retry locking
53 */
54 #define LOCK_RETRIES 5
55
56 /*
57 * Amount of time to wait before
58 * updating ctime of lock file.
59 */
60 #define NSECS 20
61
62 #if !defined(HAVE_LIBLOCKFILE)
63 /*
64 * How old does a lock file need to be
65 * before we remove it.
66 */
67 #define RSECS 180
68 #endif /* HAVE_LIBLOCKFILE */
69
70 /* struct for recording and updating locks */
71 struct lock {
72 int l_fd;
73 char *l_lock;
74 struct lock *l_next;
75 };
76
77 enum locktype { FCNTL_LOCKING, FLOCK_LOCKING, LOCKF_LOCKING, DOT_LOCKING };
78
79 /*
80 * Flags to indicate whether we've initialized the lock types, and
81 * our saved lock types
82 */
83 static int datalockinit = 0;
84 static int spoollockinit = 0;
85 static enum locktype datalocktype, spoollocktype;
86
87
88 /* top of list containing all open locks */
89 static struct lock *l_top = NULL;
90
91 static int lkopen(const char *, int, mode_t, enum locktype);
92 static int str2accbits(const char *);
93
94 static int lkopen_fcntl (const char *, int, mode_t);
95 #ifdef HAVE_LOCKF
96 static int lkopen_lockf (const char *, int, mode_t);
97 #endif /* HAVE_LOCKF */
98 #ifdef HAVE_FLOCK
99 static int lkopen_flock (const char *, int, mode_t);
100 #endif /* HAVE_FLOCK */
101
102 static enum locktype init_locktype(const char *);
103
104 static int lkopen_dot (const char *, int, mode_t);
105 static void lkclose_dot (int, const char *);
106 static void lockname (const char *, struct lockinfo *, int);
107 static void timerON (char *, int);
108 static void timerOFF (int);
109 static void alrmser (int);
110
111 #if !defined(HAVE_LIBLOCKFILE)
112 static int lockit (struct lockinfo *);
113 #endif
114
115 /*
116 * Base functions: determine the data type used to lock files and
117 * call the underlying function.
118 */
119
120 int
121 lkopendata(const char *file, int access, mode_t mode)
122 {
123 if (! datalockinit) {
124 char *cp = context_find("datalocking");
125
126 if (cp) {
127 datalocktype = init_locktype(cp);
128 } else {
129 /* We default to fcntl locking for data files */
130 datalocktype = FCNTL_LOCKING;
131 }
132
133 datalockinit = 1;
134 }
135
136 return lkopen(file, access, mode, datalocktype);
137 }
138
139
140 /*
141 * Locking using the spool locking algorithm
142 */
143
144 int lkopenspool(const char *file, int access, mode_t mode)
145 {
146 if (! spoollockinit) {
147 spoollocktype = init_locktype(spoollocking);
148
149 spoollockinit = 1;
150 }
151
152 return lkopen(file, access, mode, spoollocktype);
153 }
154
155
156 /*
157 * Versions of lkopen that return a FILE *
158 */
159
160 FILE *
161 lkfopendata(const char *file, const char *mode)
162 {
163 FILE *fp;
164 int oflags = str2accbits(mode);
165 int fd;
166
167 if (oflags == -1) {
168 errno = EINVAL;
169 return NULL;
170 }
171
172 if ((fd = lkopendata(file, oflags, 0666)) == -1)
173 return NULL;
174
175 if ((fp = fdopen (fd, mode)) == NULL) {
176 close (fd);
177 return NULL;
178 }
179
180 return fp;
181 }
182
183 FILE *
184 lkfopenspool(const char *file, const char *mode)
185 {
186 FILE *fp;
187 int oflags = str2accbits(mode);
188 int fd;
189
190 if (oflags == -1) {
191 errno = EINVAL;
192 return NULL;
193 }
194
195 if ((fd = lkopenspool(file, oflags, 0666)) == -1)
196 return NULL;
197
198 if ((fp = fdopen (fd, mode)) == NULL) {
199 close (fd);
200 return NULL;
201 }
202
203 return fp;
204 }
205
206
207 /*
208 * Corresponding close functions.
209 *
210 * A note here: All of the kernel locking functions terminate the lock
211 * when the descriptor is closed, so why write the code to explicitly
212 * unlock the file? We only need to do this in the dot-locking case.
213 */
214
215 int
216 lkclosedata(int fd, const char *name)
217 {
218 int rc = close(fd);
219
220 if (datalocktype == DOT_LOCKING)
221 lkclose_dot(fd, name);
222
223 return rc;
224 }
225
226 int
227 lkfclosedata(FILE *f, const char *name)
228 {
229 int fd, rc;
230
231 if (f == NULL)
232 return 0;
233
234 fd = fileno(f);
235 rc = fclose(f);
236
237 if (datalocktype == DOT_LOCKING)
238 lkclose_dot(fd, name);
239
240 return rc;
241 }
242
243 int
244 lkclosespool(int fd, const char *name)
245 {
246 int rc = close(fd);
247
248 if (spoollocktype == DOT_LOCKING)
249 lkclose_dot(fd, name);
250
251 return rc;
252 }
253
254 int
255 lkfclosespool(FILE *f, const char *name)
256 {
257 int fd, rc;
258
259 if (f == NULL)
260 return 0;
261
262 fd = fileno(f);
263 rc = fclose(f);
264
265 if (spoollocktype == DOT_LOCKING)
266 lkclose_dot(fd, name);
267
268 return rc;
269 }
270
271
272 /*
273 * Convert fopen() mode argument to open() bits
274 */
275
276 static int
277 str2accbits(const char *mode)
278 {
279 if (strcmp (mode, "r") == 0)
280 return O_RDONLY;
281 else if (strcmp (mode, "r+") == 0)
282 return O_RDWR;
283 else if (strcmp (mode, "w") == 0)
284 return O_WRONLY | O_CREAT | O_TRUNC;
285 else if (strcmp (mode, "w+") == 0)
286 return O_RDWR | O_CREAT | O_TRUNC;
287 else if (strcmp (mode, "a") == 0)
288 return O_WRONLY | O_CREAT | O_APPEND;
289 else if (strcmp (mode, "a+") == 0)
290 return O_RDWR | O_CREAT | O_APPEND;
291 else {
292 errno = EINVAL;
293 return -1;
294 }
295 }
296
297 /*
298 * Internal routine to switch between different locking types.
299 */
300
301 static int
302 lkopen (const char *file, int access, mode_t mode, enum locktype ltype)
303 {
304 switch (ltype) {
305
306 case FCNTL_LOCKING:
307 return lkopen_fcntl(file, access, mode);
308
309 case DOT_LOCKING:
310 return lkopen_dot(file, access, mode);
311
312 #ifdef HAVE_FLOCK
313 case FLOCK_LOCKING:
314 return lkopen_flock(file, access, mode);
315 #endif /* HAVE_FLOCK */
316
317 #ifdef HAVE_LOCKF
318 case LOCKF_LOCKING:
319 return lkopen_lockf(file, access, mode);
320 #endif /* HAVE_FLOCK */
321
322 default:
323 adios(NULL, "Internal locking error: unsupported lock type used!");
324 }
325
326 return -1;
327 }
328
329
330 /*
331 * Routine to clean up the dot locking file
332 */
333
334 static void
335 lkclose_dot (int fd, const char *file)
336 {
337 struct lockinfo lkinfo;
338
339 lockname (file, &lkinfo, 0); /* get name of lock file */
340 #if !defined(HAVE_LIBLOCKFILE)
341 unlink (lkinfo.curlock); /* remove lock file */
342 #else
343 lockfile_remove(lkinfo.curlock);
344 #endif /* HAVE_LIBLOCKFILE */
345 timerOFF (fd); /* turn off lock timer */
346 }
347
348
349 /*
350 * Open and lock a file, using fcntl locking
351 */
352
353 static int
354 lkopen_fcntl(const char *file, int access, mode_t mode)
355 {
356 int fd, i, saved_errno;
357 struct flock flk;
358
359 /*
360 * The assumption here is that if you open the file for writing, you
361 * need an exclusive lock.
362 */
363
364 for (i = 0; i < LOCK_RETRIES; i++) {
365 if ((fd = open(file, access, mode)) == -1)
366 return -1;
367
368 flk.l_start = 0;
369 flk.l_len = 0;
370 flk.l_type = (access & O_ACCMODE) == O_RDONLY ? F_RDLCK : F_WRLCK;
371 flk.l_whence = SEEK_SET;
372
373 if (fcntl(fd, F_SETLK, &flk) != -1)
374 return fd;
375
376 saved_errno = errno;
377 close(fd);
378 sleep(1);
379 }
380
381 errno = saved_errno;
382 return -1;
383 }
384
385
386 /*
387 * Open and lock a file, using flock locking
388 */
389
390 static int
391 lkopen_flock(const char *file, int access, mode_t mode)
392 {
393 int fd, i, saved_errno, locktype;
394
395 /*
396 * The assumption here is that if you open the file for writing, you
397 * need an exclusive lock.
398 */
399
400 locktype = (((access & O_ACCMODE) == O_RDONLY) ? LOCK_SH : LOCK_EX) |
401 LOCK_NB;
402
403 for (i = 0; i < LOCK_RETRIES; i++) {
404 if ((fd = open(file, access, mode)) == -1)
405 return -1;
406
407 if (flock(fd, locktype) != -1)
408 return fd;
409
410 saved_errno = errno;
411 close(fd);
412 sleep(1);
413 }
414
415 errno = saved_errno;
416 return -1;
417 }
418
419
420 /*
421 * Open and lock a file, using lockf locking
422 */
423
424 static int
425 lkopen_lockf(const char *file, int access, mode_t mode)
426 {
427 int fd, i, saved_errno, saved_access;
428
429 /*
430 * Two notes:
431 *
432 * Because lockf locks start from the current offset, mask off O_APPEND
433 * and seek to the end of the file later if it was requested.
434 *
435 * lockf locks require write access to the file, so always add it
436 * even if it wasn't requested.
437 */
438
439 saved_access = access;
440
441 access &= ~O_APPEND;
442
443 if ((access & O_ACCMODE) == O_RDONLY) {
444 access &= ~O_RDONLY;
445 access |= O_RDWR;
446 }
447
448 for (i = 0; i < LOCK_RETRIES; i++) {
449 if ((fd = open(file, access, mode)) == -1)
450 return -1;
451
452 if (lockf(fd, F_TLOCK, 0) != -1) {
453 /*
454 * Seek to end if requested
455 */
456 if (saved_access & O_APPEND) {
457 lseek(fd, 0, SEEK_END);
458 }
459 return fd;
460 }
461
462 saved_errno = errno;
463 close(fd);
464 sleep(1);
465 }
466
467 errno = saved_errno;
468 return -1;
469 }
470
471
472 /*
473 * open and lock a file, using dot locking
474 */
475
476 static int
477 lkopen_dot (const char *file, int access, mode_t mode)
478 {
479 int fd;
480 struct lockinfo lkinfo;
481
482 /* open the file */
483 if ((fd = open (file, access, mode)) == -1)
484 return -1;
485
486 /*
487 * Get the name of the eventual lock file, as well
488 * as a name for a temporary lock file.
489 */
490 lockname (file, &lkinfo, 1);
491
492 #if !defined(HAVE_LIBLOCKFILE)
493 {
494 int i;
495 for (i = 0;;) {
496 /* attempt to create lock file */
497 if (lockit (&lkinfo) == 0) {
498 /* if successful, turn on timer and return */
499 timerON (lkinfo.curlock, fd);
500 return fd;
501 } else {
502 /*
503 * Abort locking, if we fail to lock after 5 attempts
504 * and are never able to stat the lock file.
505 */
506 struct stat st;
507 if (stat (lkinfo.curlock, &st) == -1) {
508 if (i++ > 5)
509 return -1;
510 sleep (5);
511 } else {
512 time_t curtime;
513 i = 0;
514 time (&curtime);
515
516 /* check for stale lockfile, else sleep */
517 if (curtime > st.st_ctime + RSECS)
518 unlink (lkinfo.curlock);
519 else
520 sleep (5);
521 }
522 lockname (file, &lkinfo, 1);
523 }
524 }
525 }
526 #else
527 if (lockfile_create(lkinfo.curlock, 5, 0) == L_SUCCESS) {
528 timerON(lkinfo.curlock, fd);
529 return fd;
530 }
531 else {
532 close(fd);
533 return -1;
534 }
535 #endif /* HAVE_LIBLOCKFILE */
536 }
537
538 #if !defined(HAVE_LIBLOCKFILE)
539 /*
540 * Routine that actually tries to create
541 * the lock file.
542 */
543
544 static int
545 lockit (struct lockinfo *li)
546 {
547 int fd;
548 char *curlock, *tmplock;
549
550 #if 0
551 char buffer[128];
552 #endif
553
554 curlock = li->curlock;
555 tmplock = li->tmplock;
556
557 if ((fd = mkstemp(tmplock)) == -1)
558 return -1;
559
560 #if 0
561 /* write our process id into lock file */
562 snprintf (buffer, sizeof(buffer), "nmh lock: pid %d\n", (int) getpid());
563 write(fd, buffer, strlen(buffer) + 1);
564 #endif
565
566 close (fd);
567
568 /*
569 * Now try to create the real lock file
570 * by linking to the temporary file.
571 */
572 fd = link(tmplock, curlock);
573 unlink(tmplock);
574
575 return (fd == -1 ? -1 : 0);
576 }
577 #endif /* HAVE_LIBLOCKFILE */
578
579 /*
580 * Get name of lock file, and temporary lock file
581 */
582
583 static void
584 lockname (const char *file, struct lockinfo *li, int isnewlock)
585 {
586 int bplen, tmplen;
587 char *bp;
588 const char *cp;
589
590 #if 0
591 struct stat st;
592 #endif
593
594 if ((cp = strrchr (file, '/')) == NULL || *++cp == 0)
595 cp = file;
596
597 bp = li->curlock;
598 bplen = 0;
599 #ifdef LOCKDIR
600 snprintf (bp, sizeof(li->curlock), "%s/", lockdir);
601 tmplen = strlen (bp);
602 bp += tmplen;
603 bplen += tmplen;
604 #else
605 if (cp != file) {
606 snprintf (bp, sizeof(li->curlock), "%.*s", (int)(cp - file), file);
607 tmplen = strlen (bp);
608 bp += tmplen;
609 bplen += tmplen;
610 }
611 #endif
612
613 #if 0
614 /*
615 * mmdf style dot locking. Currently not supported.
616 * If we start supporting mmdf style dot locking,
617 * we will need to change the return value of lockname
618 */
619 if (stat (file, &st) == -1)
620 return -1;
621
622 snprintf (bp, sizeof(li->curlock) - bplen, "LCK%05d.%05d",
623 st.st_dev, st.st_ino);
624 #endif
625
626 snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp);
627
628 #if !defined(HAVE_LIBLOCKFILE)
629 /*
630 * If this is for a new lock, create a name for
631 * the temporary lock file for lockit()
632 */
633 if (isnewlock) {
634 if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
635 strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
636 else
637 snprintf (li->tmplock, sizeof(li->tmplock), "%.*s,LCK.XXXXXX",
638 (int)(cp - li->curlock), li->curlock);
639 }
640 #endif
641 }
642
643
644 /*
645 * Add new lockfile to the list of open lockfiles
646 * and start the lock file timer.
647 */
648
649 static void
650 timerON (char *curlock, int fd)
651 {
652 struct lock *lp;
653 size_t len;
654
655 lp = (struct lock *) mh_xmalloc (sizeof(*lp));
656
657 len = strlen(curlock) + 1;
658 lp->l_fd = fd;
659 lp->l_lock = mh_xmalloc (len);
660 memcpy (lp->l_lock, curlock, len);
661 lp->l_next = l_top;
662
663 if (!l_top) {
664 /* perhaps SIGT{STP,TIN,TOU} ? */
665 SIGNAL (SIGALRM, alrmser);
666 alarm (NSECS);
667 }
668
669 l_top = lp;
670 }
671
672
673 /*
674 * Search through the list of lockfiles for the
675 * current lockfile, and remove it from the list.
676 */
677
678 static void
679 timerOFF (int fd)
680 {
681 struct lock *pp, *lp;
682
683 alarm(0);
684
685 if (l_top) {
686 for (pp = lp = l_top; lp; pp = lp, lp = lp->l_next) {
687 if (lp->l_fd == fd)
688 break;
689 }
690 if (lp) {
691 if (lp == l_top)
692 l_top = lp->l_next;
693 else
694 pp->l_next = lp->l_next;
695
696 free (lp->l_lock);
697 free (lp);
698 }
699 }
700
701 /* if there are locks left, restart timer */
702 if (l_top)
703 alarm (NSECS);
704 }
705
706
707 /*
708 * If timer goes off, we update the ctime of all open
709 * lockfiles, so another command doesn't remove them.
710 */
711
712 static void
713 alrmser (int sig)
714 {
715 char *lockfile;
716 struct lock *lp;
717 NMH_UNUSED (sig);
718
719 /* update the ctime of all the lock files */
720 for (lp = l_top; lp; lp = lp->l_next) {
721 lockfile = lp->l_lock;
722 #if !defined(HAVE_LIBLOCKFILE)
723 {
724 int j;
725 if (*lockfile && (j = creat (lockfile, 0600)) != -1)
726 close (j);
727 }
728 #else
729 lockfile_touch(lockfile);
730 #endif
731 }
732
733 /* restart the alarm */
734 alarm (NSECS);
735 }
736
737
738 /*
739 * Return a locking algorithm based on the string name
740 */
741
742 static enum locktype
743 init_locktype(const char *lockname)
744 {
745 if (strcasecmp(lockname, "fcntl") == 0) {
746 return FCNTL_LOCKING;
747 } else if (strcasecmp(lockname, "lockf") == 0) {
748 #ifdef HAVE_LOCKF
749 return LOCKF_LOCKING;
750 #else /* ! HAVE_LOCKF */
751 adios(NULL, "lockf not supported on this system");
752 #endif /* HAVE_LOCKF */
753 } else if (strcasecmp(lockname, "flock") == 0) {
754 #ifdef HAVE_FLOCK
755 return FLOCK_LOCKING;
756 #else /* ! HAVE_FLOCK */
757 adios(NULL, "flock not supported on this system");
758 #endif /* HAVE_FLOCK */
759 } else if (strcasecmp(lockname, "dot") == 0) {
760 return DOT_LOCKING;
761 } else {
762 adios(NULL, "Unknown lock type: \"%s\"", lockname);
763 /* NOTREACHED */
764 return 0;
765 }
766 }