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