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