]>
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 *);
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
136 int lkopenspool(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
142 spoollocktype
= init_locktype(spoollocking
);
145 return lkopen(file
, access
, mode
, spoollocktype
, failed_to_lock
);
150 * Versions of lkopen that return a FILE *
154 lkfopendata(const char *file
, const char *mode
, int *failed_to_lock
)
157 int oflags
= str2accbits(mode
);
165 if ((fd
= lkopendata(file
, oflags
, 0666, failed_to_lock
)) == -1)
168 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
177 lkfopenspool(const char *file
, const char *mode
)
180 int oflags
= str2accbits(mode
);
181 int failed_to_lock
= 0;
189 if ((fd
= lkopenspool(file
, oflags
, 0666, &failed_to_lock
)) == -1)
192 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
202 * Corresponding close functions.
204 * A note here: All of the kernel locking functions terminate the lock
205 * when the descriptor is closed, so why write the code to explicitly
206 * unlock the file? We only need to do this in the dot-locking case.
210 lkclosedata(int fd
, const char *name
)
214 if (datalocktype
== DOT_LOCKING
)
215 lkclose_dot(fd
, name
);
221 lkfclosedata(FILE *f
, const char *name
)
231 if (datalocktype
== DOT_LOCKING
)
232 lkclose_dot(fd
, name
);
238 lkclosespool(int fd
, const char *name
)
242 if (spoollocktype
== DOT_LOCKING
)
243 lkclose_dot(fd
, name
);
249 lkfclosespool(FILE *f
, const char *name
)
259 if (spoollocktype
== DOT_LOCKING
)
260 lkclose_dot(fd
, name
);
267 * Convert fopen() mode argument to open() bits
271 str2accbits(const char *mode
)
273 if (strcmp (mode
, "r") == 0)
275 if (strcmp (mode
, "r+") == 0)
277 if (strcmp (mode
, "w") == 0)
278 return O_WRONLY
| O_CREAT
| O_TRUNC
;
279 if (strcmp (mode
, "w+") == 0)
280 return O_RDWR
| O_CREAT
| O_TRUNC
;
281 if (strcmp (mode
, "a") == 0)
282 return O_WRONLY
| O_CREAT
| O_APPEND
;
283 if (strcmp (mode
, "a+") == 0)
284 return O_RDWR
| O_CREAT
| O_APPEND
;
291 * Internal routine to switch between different locking types.
295 lkopen (const char *file
, int access
, mode_t mode
, enum locktype ltype
,
301 return lkopen_fcntl(file
, access
, mode
, failed_to_lock
);
304 return lkopen_dot(file
, access
, mode
, failed_to_lock
);
308 return lkopen_flock(file
, access
, mode
, failed_to_lock
);
309 #endif /* HAVE_FLOCK */
313 return lkopen_lockf(file
, access
, mode
, failed_to_lock
);
314 #endif /* HAVE_FLOCK */
317 adios(NULL
, "Internal locking error: unsupported lock type used!");
325 * Routine to clean up the dot locking file
329 lkclose_dot (int fd
, const char *file
)
331 struct lockinfo lkinfo
;
333 lockname (file
, &lkinfo
, 0); /* get name of lock file */
334 #if !defined(HAVE_LIBLOCKFILE)
335 (void) m_unlink (lkinfo
.curlock
); /* remove lock file */
337 lockfile_remove(lkinfo
.curlock
);
338 #endif /* HAVE_LIBLOCKFILE */
339 timerOFF (fd
); /* turn off lock timer */
344 * Open and lock a file, using fcntl locking
348 lkopen_fcntl(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
350 int fd
, i
, saved_errno
;
354 * The assumption here is that if you open the file for writing, you
355 * need an exclusive lock.
358 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
359 if ((fd
= open(file
, access
, mode
)) == -1)
364 flk
.l_type
= (access
& O_ACCMODE
) == O_RDONLY
? F_RDLCK
: F_WRLCK
;
365 flk
.l_whence
= SEEK_SET
;
367 if (fcntl(fd
, F_SETLK
, &flk
) != -1)
383 * Open and lock a file, using flock locking
387 lkopen_flock(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
389 int fd
, i
, saved_errno
, locktype
;
392 * The assumption here is that if you open the file for writing, you
393 * need an exclusive lock.
396 locktype
= (((access
& O_ACCMODE
) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
) |
399 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
400 if ((fd
= open(file
, access
, mode
)) == -1)
403 if (flock(fd
, locktype
) != -1)
415 #endif /* HAVE_FLOCK */
418 * Open and lock a file, using lockf locking
422 lkopen_lockf(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
424 int fd
, i
, saved_errno
, saved_access
;
429 * Because lockf locks start from the current offset, mask off O_APPEND
430 * and seek to the end of the file later if it was requested.
432 * lockf locks require write access to the file, so always add it
433 * even if it wasn't requested.
436 saved_access
= access
;
440 if ((access
& O_ACCMODE
) == O_RDONLY
) {
445 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
446 if ((fd
= open(file
, access
, mode
)) == -1)
449 if (lockf(fd
, F_TLOCK
, 0) != -1) {
451 * Seek to end if requested
453 if (saved_access
& O_APPEND
) {
454 lseek(fd
, 0, SEEK_END
);
471 * open and lock a file, using dot locking
475 lkopen_dot (const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
478 struct lockinfo lkinfo
;
481 if ((fd
= open (file
, access
, mode
)) == -1)
485 * Get the name of the eventual lock file, as well
486 * as a name for a temporary lock file.
488 lockname (file
, &lkinfo
, 1);
490 #if !defined(HAVE_LIBLOCKFILE)
493 for (i
= 0; i
< LOCK_RETRIES
; ++i
) {
496 /* attempt to create lock file */
497 if (lockit (&lkinfo
) == 0) {
498 /* if successful, turn on timer and return */
499 timerON (lkinfo
.curlock
, fd
);
504 * Abort locking, if we fail to lock after 5 attempts
505 * and are never able to stat the lock file. Or, if
506 * we can stat the lockfile but exceed LOCK_RETRIES
507 * seconds waiting for it (by falling out of the loop).
509 if (stat (lkinfo
.curlock
, &st
) == -1) {
516 /* check for stale lockfile, else sleep */
517 if (curtime
> st
.st_ctime
+ RSECS
)
518 (void) m_unlink (lkinfo
.curlock
);
522 lockname (file
, &lkinfo
, 1);
529 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
530 timerON(lkinfo
.curlock
, fd
);
537 #endif /* HAVE_LIBLOCKFILE */
540 #if !defined(HAVE_LIBLOCKFILE)
542 * Routine that actually tries to create
547 lockit (struct lockinfo
*li
)
550 char *curlock
, *tmpfile
;
556 curlock
= li
->curlock
;
558 if ((tmpfile
= m_mktemp(li
->tmplock
, &fd
, NULL
)) == NULL
) {
559 inform("unable to create temporary file in %s", li
->tmplock
);
564 /* write our process id into lock file */
565 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
566 write(fd
, buffer
, strlen(buffer
) + 1);
572 * Now try to create the real lock file
573 * by linking to the temporary file.
575 fd
= link(tmpfile
, curlock
);
576 (void) m_unlink(tmpfile
);
578 return (fd
== -1 ? -1 : 0);
580 #endif /* HAVE_LIBLOCKFILE */
583 * Get name of lock file, and temporary lock file
587 lockname (const char *file
, struct lockinfo
*li
, int isnewlock
)
597 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
603 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
604 tmplen
= strlen (bp
);
609 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
610 tmplen
= strlen (bp
);
618 * mmdf style dot locking. Currently not supported.
619 * If we start supporting mmdf style dot locking,
620 * we will need to change the return value of lockname
622 if (stat (file
, &st
) == -1)
625 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
626 st
.st_dev
, st
.st_ino
);
629 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
631 #if defined(HAVE_LIBLOCKFILE)
632 NMH_UNUSED(isnewlock
);
635 * If this is for a new lock, create a name for
636 * the temporary lock file for lockit()
639 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
640 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
642 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
643 (int)(cp
- li
->curlock
), li
->curlock
);
650 * Add new lockfile to the list of open lockfiles
651 * and start the lock file timer.
655 timerON (char *curlock
, int fd
)
660 lp
->l_lock
= mh_xstrdup(curlock
);
665 /* perhaps SIGT{STP,TIN,TOU} ? */
666 SIGNAL (SIGALRM
, alrmser
);
674 * Search through the list of lockfiles for the
675 * current lockfile, and remove it from the list.
681 struct lock
*pp
, *lp
;
686 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
694 pp
->l_next
= lp
->l_next
;
701 /* if there are locks left, restart timer */
708 * If timer goes off, we update the ctime of all open
709 * lockfiles, so another command doesn't remove them.
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)
725 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
729 lockfile_touch(lockfile
);
733 /* restart the alarm */
739 * Return a locking algorithm based on the string name
743 init_locktype(const char *lockname
)
745 if (strcasecmp(lockname
, "fcntl") == 0) {
746 return FCNTL_LOCKING
;
748 if (strcasecmp(lockname
, "lockf") == 0) {
750 return LOCKF_LOCKING
;
751 #else /* ! HAVE_LOCKF */
752 adios(NULL
, "lockf not supported on this system");
753 #endif /* HAVE_LOCKF */
755 if (strcasecmp(lockname
, "flock") == 0) {
757 return FLOCK_LOCKING
;
758 #else /* ! HAVE_FLOCK */
759 adios(NULL
, "flock not supported on this system");
760 #endif /* HAVE_FLOCK */
762 if (strcasecmp(lockname
, "dot") == 0) {
765 adios(NULL
, "Unknown lock type: \"%s\"", lockname
);