]>
diplodocus.org Git - nmh/blob - sbr/lock_file.c
1 /* lock_file.c -- routines to lock/unlock files
3 * This code is Copyright (c) 2002, by the authors of nmh. See the
4 * COPYRIGHT file in the root directory of the nmh distribution for
5 * complete copyright information.
8 /* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
10 * Since liblockfile locking shares most of its code with dot locking, it
11 * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
13 * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
17 #include <h/signals.h>
20 #include "lock_file.h"
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
29 # include <sys/file.h>
32 #if defined(HAVE_LIBLOCKFILE)
33 # include <lockfile.h>
37 char *lockdir
= LOCKDIR
;
40 /* struct for getting name of lock file to create */
43 #if !defined(HAVE_LIBLOCKFILE)
49 * Number of tries to retry locking
51 #define LOCK_RETRIES 60
54 * Amount of time to wait before
55 * updating ctime of lock file.
59 #if !defined(HAVE_LIBLOCKFILE)
61 * How old does a lock file need to be
62 * before we remove it.
65 #endif /* HAVE_LIBLOCKFILE */
67 /* struct for recording and updating locks */
74 enum locktype
{ FCNTL_LOCKING
, FLOCK_LOCKING
, LOCKF_LOCKING
, DOT_LOCKING
};
76 /* Our saved lock types. */
77 static enum locktype datalocktype
, spoollocktype
;
79 /* top of list containing all open locks */
80 static struct lock
*l_top
= NULL
;
82 static int lkopen(const char *, int, mode_t
, enum locktype
, int *);
83 static int str2accbits(const char *);
85 static int lkopen_fcntl (const char *, int, mode_t
, int *);
87 static int lkopen_lockf (const char *, int, mode_t
, int *);
88 #endif /* HAVE_LOCKF */
90 static int lkopen_flock (const char *, int, mode_t
, int *);
91 #endif /* HAVE_FLOCK */
93 static enum locktype
init_locktype(const char *) PURE
;
95 static int lkopen_dot (const char *, int, mode_t
, int *);
96 static void lkclose_dot (int, const char *);
97 static void lockname (const char *, struct lockinfo
*, int);
98 static void timerON (char *, int);
99 static void timerOFF (int);
100 static void alrmser (int);
102 #if !defined(HAVE_LIBLOCKFILE)
103 static int lockit (struct lockinfo
*);
107 * Base functions: determine the data type used to lock files and
108 * call the underlying function.
112 lkopendata(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
120 if ((dl
= context_find("datalocking"))) {
121 datalocktype
= init_locktype(dl
);
123 /* We default to fcntl locking for data files */
124 datalocktype
= FCNTL_LOCKING
;
128 return lkopen(file
, access
, mode
, datalocktype
, failed_to_lock
);
133 * Locking using the spool locking algorithm
137 lkopenspool(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
143 spoollocktype
= init_locktype(spoollocking
);
146 return lkopen(file
, access
, mode
, spoollocktype
, failed_to_lock
);
151 * Versions of lkopen that return a FILE *
155 lkfopendata(const char *file
, const char *mode
, int *failed_to_lock
)
158 int oflags
= str2accbits(mode
);
166 if ((fd
= lkopendata(file
, oflags
, 0666, failed_to_lock
)) == -1)
169 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
178 lkfopenspool(const char *file
, const char *mode
)
181 int oflags
= str2accbits(mode
);
182 int failed_to_lock
= 0;
190 if ((fd
= lkopenspool(file
, oflags
, 0666, &failed_to_lock
)) == -1)
193 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
203 * Corresponding close functions.
205 * A note here: All of the kernel locking functions terminate the lock
206 * when the descriptor is closed, so why write the code to explicitly
207 * unlock the file? We only need to do this in the dot-locking case.
211 lkclosedata(int fd
, const char *name
)
215 if (datalocktype
== DOT_LOCKING
)
216 lkclose_dot(fd
, name
);
222 lkfclosedata(FILE *f
, const char *name
)
232 if (datalocktype
== DOT_LOCKING
)
233 lkclose_dot(fd
, name
);
239 lkclosespool(int fd
, const char *name
)
243 if (spoollocktype
== DOT_LOCKING
)
244 lkclose_dot(fd
, name
);
250 lkfclosespool(FILE *f
, const char *name
)
260 if (spoollocktype
== DOT_LOCKING
)
261 lkclose_dot(fd
, name
);
268 * Convert fopen() mode argument to open() bits
272 str2accbits(const char *mode
)
274 if (strcmp (mode
, "r") == 0)
276 if (strcmp (mode
, "r+") == 0)
278 if (strcmp (mode
, "w") == 0)
279 return O_WRONLY
| O_CREAT
| O_TRUNC
;
280 if (strcmp (mode
, "w+") == 0)
281 return O_RDWR
| O_CREAT
| O_TRUNC
;
282 if (strcmp (mode
, "a") == 0)
283 return O_WRONLY
| O_CREAT
| O_APPEND
;
284 if (strcmp (mode
, "a+") == 0)
285 return O_RDWR
| O_CREAT
| O_APPEND
;
292 * Internal routine to switch between different locking types.
296 lkopen (const char *file
, int access
, mode_t mode
, enum locktype ltype
,
302 return lkopen_fcntl(file
, access
, mode
, failed_to_lock
);
305 return lkopen_dot(file
, access
, mode
, failed_to_lock
);
309 return lkopen_flock(file
, access
, mode
, failed_to_lock
);
310 #endif /* HAVE_FLOCK */
314 return lkopen_lockf(file
, access
, mode
, failed_to_lock
);
315 #endif /* HAVE_FLOCK */
318 die("Internal locking error: unsupported lock type used!");
326 * Routine to clean up the dot locking file
330 lkclose_dot (int fd
, const char *file
)
332 struct lockinfo lkinfo
;
334 lockname (file
, &lkinfo
, 0); /* get name of lock file */
335 #if !defined(HAVE_LIBLOCKFILE)
336 (void) m_unlink (lkinfo
.curlock
); /* remove lock file */
338 lockfile_remove(lkinfo
.curlock
);
339 #endif /* HAVE_LIBLOCKFILE */
340 timerOFF (fd
); /* turn off lock timer */
345 * Open and lock a file, using fcntl locking
349 lkopen_fcntl(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
351 int fd
, i
, saved_errno
;
355 * The assumption here is that if you open the file for writing, you
356 * need an exclusive lock.
359 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
360 if ((fd
= open(file
, access
, mode
)) == -1)
365 flk
.l_type
= (access
& O_ACCMODE
) == O_RDONLY
? F_RDLCK
: F_WRLCK
;
366 flk
.l_whence
= SEEK_SET
;
368 if (fcntl(fd
, F_SETLK
, &flk
) != -1)
384 * Open and lock a file, using flock locking
388 lkopen_flock(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
390 int fd
, i
, saved_errno
, locktype
;
393 * The assumption here is that if you open the file for writing, you
394 * need an exclusive lock.
397 locktype
= (((access
& O_ACCMODE
) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
) |
400 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
401 if ((fd
= open(file
, access
, mode
)) == -1)
404 if (flock(fd
, locktype
) != -1)
416 #endif /* HAVE_FLOCK */
419 * Open and lock a file, using lockf locking
423 lkopen_lockf(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
425 int fd
, i
, saved_errno
, saved_access
;
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.
433 * lockf locks require write access to the file, so always add it
434 * even if it wasn't requested.
437 saved_access
= access
;
441 if ((access
& O_ACCMODE
) == O_RDONLY
) {
446 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
447 if ((fd
= open(file
, access
, mode
)) == -1)
450 if (lockf(fd
, F_TLOCK
, 0) != -1) {
452 * Seek to end if requested
454 if (saved_access
& O_APPEND
) {
455 lseek(fd
, 0, SEEK_END
);
472 * open and lock a file, using dot locking
476 lkopen_dot (const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
479 struct lockinfo lkinfo
;
482 if ((fd
= open (file
, access
, mode
)) == -1)
486 * Get the name of the eventual lock file, as well
487 * as a name for a temporary lock file.
489 lockname (file
, &lkinfo
, 1);
491 #if !defined(HAVE_LIBLOCKFILE)
494 for (i
= 0; i
< LOCK_RETRIES
; ++i
) {
497 /* attempt to create lock file */
498 if (lockit (&lkinfo
) == 0) {
499 /* if successful, turn on timer and return */
500 timerON (lkinfo
.curlock
, fd
);
505 * Abort locking, if we fail to lock after 5 attempts
506 * and are never able to stat the lock file. Or, if
507 * we can stat the lockfile but exceed LOCK_RETRIES
508 * seconds waiting for it (by falling out of the loop).
510 if (stat (lkinfo
.curlock
, &st
) == -1) {
517 /* check for stale lockfile, else sleep */
518 if (curtime
> st
.st_ctime
+ RSECS
)
519 (void) m_unlink (lkinfo
.curlock
);
523 lockname (file
, &lkinfo
, 1);
531 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
532 timerON(lkinfo
.curlock
, fd
);
539 #endif /* HAVE_LIBLOCKFILE */
542 #if !defined(HAVE_LIBLOCKFILE)
544 * Routine that actually tries to create
549 lockit (struct lockinfo
*li
)
552 char *curlock
, *tmpfile
;
558 curlock
= li
->curlock
;
560 if ((tmpfile
= m_mktemp(li
->tmplock
, &fd
, NULL
)) == NULL
) {
561 inform("unable to create temporary file in %s", li
->tmplock
);
566 /* write our process id into lock file */
567 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
568 write(fd
, buffer
, strlen(buffer
) + 1);
574 * Now try to create the real lock file
575 * by linking to the temporary file.
577 fd
= link(tmpfile
, curlock
);
578 (void) m_unlink(tmpfile
);
580 return fd
== -1 ? -1 : 0;
582 #endif /* HAVE_LIBLOCKFILE */
585 * Get name of lock file, and temporary lock file
589 lockname (const char *file
, struct lockinfo
*li
, int isnewlock
)
599 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
605 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
606 tmplen
= strlen (bp
);
611 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
612 tmplen
= strlen (bp
);
620 * mmdf style dot locking. Currently not supported.
621 * If we start supporting mmdf style dot locking,
622 * we will need to change the return value of lockname
624 if (stat (file
, &st
) == -1)
627 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
628 st
.st_dev
, st
.st_ino
);
631 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
633 #if defined(HAVE_LIBLOCKFILE)
634 NMH_UNUSED(isnewlock
);
637 * If this is for a new lock, create a name for
638 * the temporary lock file for lockit()
641 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
642 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
644 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
645 (int)(cp
- li
->curlock
), li
->curlock
);
652 * Add new lockfile to the list of open lockfiles
653 * and start the lock file timer.
657 timerON (char *curlock
, int fd
)
662 lp
->l_lock
= mh_xstrdup(curlock
);
667 /* perhaps SIGT{STP,TIN,TOU} ? */
668 SIGNAL (SIGALRM
, alrmser
);
676 * Search through the list of lockfiles for the
677 * current lockfile, and remove it from the list.
683 struct lock
*pp
, *lp
;
688 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
696 pp
->l_next
= lp
->l_next
;
703 /* if there are locks left, restart timer */
710 * If timer goes off, we update the ctime of all open
711 * lockfiles, so another command doesn't remove them.
721 /* update the ctime of all the lock files */
722 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
723 lockfile
= lp
->l_lock
;
724 #if !defined(HAVE_LIBLOCKFILE)
727 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
731 lockfile_touch(lockfile
);
735 /* restart the alarm */
741 * Return a locking algorithm based on the string name
745 init_locktype(const char *lockname
)
747 if (strcasecmp(lockname
, "fcntl") == 0) {
748 return FCNTL_LOCKING
;
750 if (strcasecmp(lockname
, "lockf") == 0) {
752 return LOCKF_LOCKING
;
753 #else /* ! HAVE_LOCKF */
754 die("lockf not supported on this system");
755 #endif /* HAVE_LOCKF */
757 if (strcasecmp(lockname
, "flock") == 0) {
759 return FLOCK_LOCKING
;
760 #else /* ! HAVE_FLOCK */
761 die("flock not supported on this system");
762 #endif /* HAVE_FLOCK */
764 if (strcasecmp(lockname
, "dot") == 0) {
767 die("Unknown lock type: \"%s\"", lockname
);