]>
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>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
27 # include <sys/file.h>
30 #if defined(HAVE_LIBLOCKFILE)
31 # include <lockfile.h>
35 char *lockdir
= LOCKDIR
;
38 /* struct for getting name of lock file to create */
41 #if !defined(HAVE_LIBLOCKFILE)
47 * Number of tries to retry locking
49 #define LOCK_RETRIES 60
52 * Amount of time to wait before
53 * updating ctime of lock file.
57 #if !defined(HAVE_LIBLOCKFILE)
59 * How old does a lock file need to be
60 * before we remove it.
63 #endif /* HAVE_LIBLOCKFILE */
65 /* struct for recording and updating locks */
72 enum locktype
{ FCNTL_LOCKING
, FLOCK_LOCKING
, LOCKF_LOCKING
, DOT_LOCKING
};
74 /* Our saved lock types. */
75 static enum locktype datalocktype
, spoollocktype
;
77 /* top of list containing all open locks */
78 static struct lock
*l_top
= NULL
;
80 static int lkopen(const char *, int, mode_t
, enum locktype
, int *);
81 static int str2accbits(const char *);
83 static int lkopen_fcntl (const char *, int, mode_t
, int *);
85 static int lkopen_lockf (const char *, int, mode_t
, int *);
86 #endif /* HAVE_LOCKF */
88 static int lkopen_flock (const char *, int, mode_t
, int *);
89 #endif /* HAVE_FLOCK */
91 static enum locktype
init_locktype(const char *);
93 static int lkopen_dot (const char *, int, mode_t
, int *);
94 static void lkclose_dot (int, const char *);
95 static void lockname (const char *, struct lockinfo
*, int);
96 static void timerON (char *, int);
97 static void timerOFF (int);
98 static void alrmser (int);
100 #if !defined(HAVE_LIBLOCKFILE)
101 static int lockit (struct lockinfo
*);
105 * Base functions: determine the data type used to lock files and
106 * call the underlying function.
110 lkopendata(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
118 if ((dl
= context_find("datalocking"))) {
119 datalocktype
= init_locktype(dl
);
121 /* We default to fcntl locking for data files */
122 datalocktype
= FCNTL_LOCKING
;
126 return lkopen(file
, access
, mode
, datalocktype
, failed_to_lock
);
131 * Locking using the spool locking algorithm
134 int lkopenspool(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
140 spoollocktype
= init_locktype(spoollocking
);
143 return lkopen(file
, access
, mode
, spoollocktype
, failed_to_lock
);
148 * Versions of lkopen that return a FILE *
152 lkfopendata(const char *file
, const char *mode
, int *failed_to_lock
)
155 int oflags
= str2accbits(mode
);
163 if ((fd
= lkopendata(file
, oflags
, 0666, failed_to_lock
)) == -1)
166 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
175 lkfopenspool(const char *file
, const char *mode
)
178 int oflags
= str2accbits(mode
);
179 int failed_to_lock
= 0;
187 if ((fd
= lkopenspool(file
, oflags
, 0666, &failed_to_lock
)) == -1)
190 if ((fp
= fdopen (fd
, mode
)) == NULL
) {
200 * Corresponding close functions.
202 * A note here: All of the kernel locking functions terminate the lock
203 * when the descriptor is closed, so why write the code to explicitly
204 * unlock the file? We only need to do this in the dot-locking case.
208 lkclosedata(int fd
, const char *name
)
212 if (datalocktype
== DOT_LOCKING
)
213 lkclose_dot(fd
, name
);
219 lkfclosedata(FILE *f
, const char *name
)
229 if (datalocktype
== DOT_LOCKING
)
230 lkclose_dot(fd
, name
);
236 lkclosespool(int fd
, const char *name
)
240 if (spoollocktype
== DOT_LOCKING
)
241 lkclose_dot(fd
, name
);
247 lkfclosespool(FILE *f
, const char *name
)
257 if (spoollocktype
== DOT_LOCKING
)
258 lkclose_dot(fd
, name
);
265 * Convert fopen() mode argument to open() bits
269 str2accbits(const char *mode
)
271 if (strcmp (mode
, "r") == 0)
273 if (strcmp (mode
, "r+") == 0)
275 if (strcmp (mode
, "w") == 0)
276 return O_WRONLY
| O_CREAT
| O_TRUNC
;
277 if (strcmp (mode
, "w+") == 0)
278 return O_RDWR
| O_CREAT
| O_TRUNC
;
279 if (strcmp (mode
, "a") == 0)
280 return O_WRONLY
| O_CREAT
| O_APPEND
;
281 if (strcmp (mode
, "a+") == 0)
282 return O_RDWR
| O_CREAT
| O_APPEND
;
289 * Internal routine to switch between different locking types.
293 lkopen (const char *file
, int access
, mode_t mode
, enum locktype ltype
,
299 return lkopen_fcntl(file
, access
, mode
, failed_to_lock
);
302 return lkopen_dot(file
, access
, mode
, failed_to_lock
);
306 return lkopen_flock(file
, access
, mode
, failed_to_lock
);
307 #endif /* HAVE_FLOCK */
311 return lkopen_lockf(file
, access
, mode
, failed_to_lock
);
312 #endif /* HAVE_FLOCK */
315 adios(NULL
, "Internal locking error: unsupported lock type used!");
323 * Routine to clean up the dot locking file
327 lkclose_dot (int fd
, const char *file
)
329 struct lockinfo lkinfo
;
331 lockname (file
, &lkinfo
, 0); /* get name of lock file */
332 #if !defined(HAVE_LIBLOCKFILE)
333 (void) m_unlink (lkinfo
.curlock
); /* remove lock file */
335 lockfile_remove(lkinfo
.curlock
);
336 #endif /* HAVE_LIBLOCKFILE */
337 timerOFF (fd
); /* turn off lock timer */
342 * Open and lock a file, using fcntl locking
346 lkopen_fcntl(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
348 int fd
, i
, saved_errno
;
352 * The assumption here is that if you open the file for writing, you
353 * need an exclusive lock.
356 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
357 if ((fd
= open(file
, access
, mode
)) == -1)
362 flk
.l_type
= (access
& O_ACCMODE
) == O_RDONLY
? F_RDLCK
: F_WRLCK
;
363 flk
.l_whence
= SEEK_SET
;
365 if (fcntl(fd
, F_SETLK
, &flk
) != -1)
381 * Open and lock a file, using flock locking
385 lkopen_flock(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
387 int fd
, i
, saved_errno
, locktype
;
390 * The assumption here is that if you open the file for writing, you
391 * need an exclusive lock.
394 locktype
= (((access
& O_ACCMODE
) == O_RDONLY
) ? LOCK_SH
: LOCK_EX
) |
397 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
398 if ((fd
= open(file
, access
, mode
)) == -1)
401 if (flock(fd
, locktype
) != -1)
413 #endif /* HAVE_FLOCK */
416 * Open and lock a file, using lockf locking
420 lkopen_lockf(const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
422 int fd
, i
, saved_errno
, saved_access
;
427 * Because lockf locks start from the current offset, mask off O_APPEND
428 * and seek to the end of the file later if it was requested.
430 * lockf locks require write access to the file, so always add it
431 * even if it wasn't requested.
434 saved_access
= access
;
438 if ((access
& O_ACCMODE
) == O_RDONLY
) {
443 for (i
= 0; i
< LOCK_RETRIES
; i
++) {
444 if ((fd
= open(file
, access
, mode
)) == -1)
447 if (lockf(fd
, F_TLOCK
, 0) != -1) {
449 * Seek to end if requested
451 if (saved_access
& O_APPEND
) {
452 lseek(fd
, 0, SEEK_END
);
469 * open and lock a file, using dot locking
473 lkopen_dot (const char *file
, int access
, mode_t mode
, int *failed_to_lock
)
476 struct lockinfo lkinfo
;
479 if ((fd
= open (file
, access
, mode
)) == -1)
483 * Get the name of the eventual lock file, as well
484 * as a name for a temporary lock file.
486 lockname (file
, &lkinfo
, 1);
488 #if !defined(HAVE_LIBLOCKFILE)
491 for (i
= 0; i
< LOCK_RETRIES
; ++i
) {
494 /* attempt to create lock file */
495 if (lockit (&lkinfo
) == 0) {
496 /* if successful, turn on timer and return */
497 timerON (lkinfo
.curlock
, fd
);
502 * Abort locking, if we fail to lock after 5 attempts
503 * and are never able to stat the lock file. Or, if
504 * we can stat the lockfile but exceed LOCK_RETRIES
505 * seconds waiting for it (by falling out of the loop).
507 if (stat (lkinfo
.curlock
, &st
) == -1) {
514 /* check for stale lockfile, else sleep */
515 if (curtime
> st
.st_ctime
+ RSECS
)
516 (void) m_unlink (lkinfo
.curlock
);
520 lockname (file
, &lkinfo
, 1);
527 if (lockfile_create(lkinfo
.curlock
, 5, 0) == L_SUCCESS
) {
528 timerON(lkinfo
.curlock
, fd
);
535 #endif /* HAVE_LIBLOCKFILE */
538 #if !defined(HAVE_LIBLOCKFILE)
540 * Routine that actually tries to create
545 lockit (struct lockinfo
*li
)
548 char *curlock
, *tmpfile
;
554 curlock
= li
->curlock
;
556 if ((tmpfile
= m_mktemp(li
->tmplock
, &fd
, NULL
)) == NULL
) {
557 inform("unable to create temporary file in %s", li
->tmplock
);
562 /* write our process id into lock file */
563 snprintf (buffer
, sizeof(buffer
), "nmh lock: pid %d\n", (int) getpid());
564 write(fd
, buffer
, strlen(buffer
) + 1);
570 * Now try to create the real lock file
571 * by linking to the temporary file.
573 fd
= link(tmpfile
, curlock
);
574 (void) m_unlink(tmpfile
);
576 return (fd
== -1 ? -1 : 0);
578 #endif /* HAVE_LIBLOCKFILE */
581 * Get name of lock file, and temporary lock file
585 lockname (const char *file
, struct lockinfo
*li
, int isnewlock
)
595 if ((cp
= strrchr (file
, '/')) == NULL
|| *++cp
== 0)
601 snprintf (bp
, sizeof(li
->curlock
), "%s/", lockdir
);
602 tmplen
= strlen (bp
);
607 snprintf (bp
, sizeof(li
->curlock
), "%.*s", (int)(cp
- file
), file
);
608 tmplen
= strlen (bp
);
616 * mmdf style dot locking. Currently not supported.
617 * If we start supporting mmdf style dot locking,
618 * we will need to change the return value of lockname
620 if (stat (file
, &st
) == -1)
623 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "LCK%05d.%05d",
624 st
.st_dev
, st
.st_ino
);
627 snprintf (bp
, sizeof(li
->curlock
) - bplen
, "%s.lock", cp
);
629 #if !defined(HAVE_LIBLOCKFILE)
631 * If this is for a new lock, create a name for
632 * the temporary lock file for lockit()
635 if ((cp
= strrchr (li
->curlock
, '/')) == NULL
|| *++cp
== 0)
636 strncpy (li
->tmplock
, ",LCK.XXXXXX", sizeof(li
->tmplock
));
638 snprintf (li
->tmplock
, sizeof(li
->tmplock
), "%.*s,LCK.XXXXXX",
639 (int)(cp
- li
->curlock
), li
->curlock
);
646 * Add new lockfile to the list of open lockfiles
647 * and start the lock file timer.
651 timerON (char *curlock
, int fd
)
656 lp
->l_lock
= mh_xstrdup(curlock
);
661 /* perhaps SIGT{STP,TIN,TOU} ? */
662 SIGNAL (SIGALRM
, alrmser
);
670 * Search through the list of lockfiles for the
671 * current lockfile, and remove it from the list.
677 struct lock
*pp
, *lp
;
682 for (pp
= lp
= l_top
; lp
; pp
= lp
, lp
= lp
->l_next
) {
690 pp
->l_next
= lp
->l_next
;
697 /* if there are locks left, restart timer */
704 * If timer goes off, we update the ctime of all open
705 * lockfiles, so another command doesn't remove them.
715 /* update the ctime of all the lock files */
716 for (lp
= l_top
; lp
; lp
= lp
->l_next
) {
717 lockfile
= lp
->l_lock
;
718 #if !defined(HAVE_LIBLOCKFILE)
721 if (*lockfile
&& (j
= creat (lockfile
, 0600)) != -1)
725 lockfile_touch(lockfile
);
729 /* restart the alarm */
735 * Return a locking algorithm based on the string name
739 init_locktype(const char *lockname
)
741 if (strcasecmp(lockname
, "fcntl") == 0) {
742 return FCNTL_LOCKING
;
744 if (strcasecmp(lockname
, "lockf") == 0) {
746 return LOCKF_LOCKING
;
747 #else /* ! HAVE_LOCKF */
748 adios(NULL
, "lockf not supported on this system");
749 #endif /* HAVE_LOCKF */
751 if (strcasecmp(lockname
, "flock") == 0) {
753 return FLOCK_LOCKING
;
754 #else /* ! HAVE_FLOCK */
755 adios(NULL
, "flock not supported on this system");
756 #endif /* HAVE_FLOCK */
758 if (strcasecmp(lockname
, "dot") == 0) {
761 adios(NULL
, "Unknown lock type: \"%s\"", lockname
);