X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/d20cac6971c9e649cc88e6533e7fa1367e488e14..b698d5cd157d035658009158aa44a05bc4a01124:/sbr/lock_file.c diff --git a/sbr/lock_file.c b/sbr/lock_file.c index 9fbcbd78..81ad0453 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -1,6 +1,4 @@ - -/* - * lock.c -- routines to lock/unlock files +/* lock_file.c -- routines to lock/unlock files * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for @@ -15,10 +13,14 @@ * Ruud de Rooij Sun, 28 Mar 1999 15:34:03 +0200 */ -#include -#include -#include -#include +#include "h/mh.h" +#include "context_find.h" +#include "error.h" +#include "h/signals.h" +#include "h/utils.h" +#include "h/mts.h" +#include "lock_file.h" +#include "m_mktemp.h" #ifdef HAVE_SYS_TIME_H # include @@ -73,15 +75,9 @@ struct lock { enum locktype { FCNTL_LOCKING, FLOCK_LOCKING, LOCKF_LOCKING, DOT_LOCKING }; -/* - * Flags to indicate whether we've initialized the lock types, and - * our saved lock types - */ -static int datalockinit = 0; -static int spoollockinit = 0; +/* Our saved lock types. */ static enum locktype datalocktype, spoollocktype; - /* top of list containing all open locks */ static struct lock *l_top = NULL; @@ -96,7 +92,7 @@ static int lkopen_lockf (const char *, int, mode_t, int *); static int lkopen_flock (const char *, int, mode_t, int *); #endif /* HAVE_FLOCK */ -static enum locktype init_locktype(const char *); +static enum locktype init_locktype(const char *) PURE; static int lkopen_dot (const char *, int, mode_t, int *); static void lkclose_dot (int, const char *); @@ -117,17 +113,18 @@ static int lockit (struct lockinfo *); int lkopendata(const char *file, int access, mode_t mode, int *failed_to_lock) { - if (! datalockinit) { - char *cp = context_find("datalocking"); + static bool deja_vu; + + if (!deja_vu) { + char *dl; - if (cp) { - datalocktype = init_locktype(cp); + deja_vu = true; + if ((dl = context_find("datalocking"))) { + datalocktype = init_locktype(dl); } else { /* We default to fcntl locking for data files */ datalocktype = FCNTL_LOCKING; } - - datalockinit = 1; } return lkopen(file, access, mode, datalocktype, failed_to_lock); @@ -138,12 +135,14 @@ lkopendata(const char *file, int access, mode_t mode, int *failed_to_lock) * Locking using the spool locking algorithm */ -int lkopenspool(const char *file, int access, mode_t mode, int *failed_to_lock) +int +lkopenspool(const char *file, int access, mode_t mode, int *failed_to_lock) { - if (! spoollockinit) { - spoollocktype = init_locktype(spoollocking); + static bool deja_vu; - spoollockinit = 1; + if (!deja_vu) { + deja_vu = true; + spoollocktype = init_locktype(spoollocking); } return lkopen(file, access, mode, spoollocktype, failed_to_lock); @@ -162,7 +161,7 @@ lkfopendata(const char *file, const char *mode, int *failed_to_lock) int fd; if (oflags == -1) { - errno = EINVAL; + errno = EINVAL; return NULL; } @@ -186,7 +185,7 @@ lkfopenspool(const char *file, const char *mode) int fd; if (oflags == -1) { - errno = EINVAL; + errno = EINVAL; return NULL; } @@ -216,7 +215,7 @@ lkclosedata(int fd, const char *name) int rc = close(fd); if (datalocktype == DOT_LOCKING) - lkclose_dot(fd, name); + lkclose_dot(fd, name); return rc; } @@ -227,13 +226,13 @@ lkfclosedata(FILE *f, const char *name) int fd, rc; if (f == NULL) - return 0; + return 0; fd = fileno(f); rc = fclose(f); if (datalocktype == DOT_LOCKING) - lkclose_dot(fd, name); + lkclose_dot(fd, name); return rc; } @@ -244,7 +243,7 @@ lkclosespool(int fd, const char *name) int rc = close(fd); if (spoollocktype == DOT_LOCKING) - lkclose_dot(fd, name); + lkclose_dot(fd, name); return rc; } @@ -255,13 +254,13 @@ lkfclosespool(FILE *f, const char *name) int fd, rc; if (f == NULL) - return 0; + return 0; fd = fileno(f); rc = fclose(f); if (spoollocktype == DOT_LOCKING) - lkclose_dot(fd, name); + lkclose_dot(fd, name); return rc; } @@ -302,23 +301,23 @@ lkopen (const char *file, int access, mode_t mode, enum locktype ltype, switch (ltype) { case FCNTL_LOCKING: - return lkopen_fcntl(file, access, mode, failed_to_lock); + return lkopen_fcntl(file, access, mode, failed_to_lock); case DOT_LOCKING: - return lkopen_dot(file, access, mode, failed_to_lock); + return lkopen_dot(file, access, mode, failed_to_lock); #ifdef HAVE_FLOCK case FLOCK_LOCKING: - return lkopen_flock(file, access, mode, failed_to_lock); + return lkopen_flock(file, access, mode, failed_to_lock); #endif /* HAVE_FLOCK */ #ifdef HAVE_LOCKF case LOCKF_LOCKING: - return lkopen_lockf(file, access, mode, failed_to_lock); + return lkopen_lockf(file, access, mode, failed_to_lock); #endif /* HAVE_FLOCK */ default: - adios(NULL, "Internal locking error: unsupported lock type used!"); + die("Internal locking error: unsupported lock type used!"); } return -1; @@ -360,7 +359,7 @@ lkopen_fcntl(const char *file, int access, mode_t mode, int *failed_to_lock) */ for (i = 0; i < LOCK_RETRIES; i++) { - if ((fd = open(file, access, mode)) == -1) + if ((fd = open(file, access, mode)) == -1) return -1; flk.l_start = 0; @@ -398,10 +397,10 @@ lkopen_flock(const char *file, int access, mode_t mode, int *failed_to_lock) */ locktype = (((access & O_ACCMODE) == O_RDONLY) ? LOCK_SH : LOCK_EX) | - LOCK_NB; + LOCK_NB; for (i = 0; i < LOCK_RETRIES; i++) { - if ((fd = open(file, access, mode)) == -1) + if ((fd = open(file, access, mode)) == -1) return -1; if (flock(fd, locktype) != -1) @@ -442,12 +441,12 @@ lkopen_lockf(const char *file, int access, mode_t mode, int *failed_to_lock) access &= ~O_APPEND; if ((access & O_ACCMODE) == O_RDONLY) { - access &= ~O_RDONLY; + access &= ~O_RDONLY; access |= O_RDWR; } for (i = 0; i < LOCK_RETRIES; i++) { - if ((fd = open(file, access, mode)) == -1) + if ((fd = open(file, access, mode)) == -1) return -1; if (lockf(fd, F_TLOCK, 0) != -1) { @@ -455,7 +454,7 @@ lkopen_lockf(const char *file, int access, mode_t mode, int *failed_to_lock) * Seek to end if requested */ if (saved_access & O_APPEND) { - lseek(fd, 0, SEEK_END); + lseek(fd, 0, SEEK_END); } return fd; } @@ -526,6 +525,7 @@ lkopen_dot (const char *file, int access, mode_t mode, int *failed_to_lock) lockname (file, &lkinfo, 1); } + close(fd); *failed_to_lock = 1; return -1; } @@ -560,7 +560,7 @@ lockit (struct lockinfo *li) curlock = li->curlock; if ((tmpfile = m_mktemp(li->tmplock, &fd, NULL)) == NULL) { - advise(NULL, "unable to create temporary file in %s", li->tmplock); + inform("unable to create temporary file in %s", li->tmplock); return -1; } @@ -579,7 +579,7 @@ lockit (struct lockinfo *li) fd = link(tmpfile, curlock); (void) m_unlink(tmpfile); - return (fd == -1 ? -1 : 0); + return fd == -1 ? -1 : 0; } #endif /* HAVE_LIBLOCKFILE */ @@ -632,7 +632,9 @@ lockname (const char *file, struct lockinfo *li, int isnewlock) snprintf (bp, sizeof(li->curlock) - bplen, "%s.lock", cp); -#if !defined(HAVE_LIBLOCKFILE) +#if defined(HAVE_LIBLOCKFILE) + NMH_UNUSED(isnewlock); +#else /* * If this is for a new lock, create a name for * the temporary lock file for lockit() @@ -745,26 +747,26 @@ static enum locktype init_locktype(const char *lockname) { if (strcasecmp(lockname, "fcntl") == 0) { - return FCNTL_LOCKING; + return FCNTL_LOCKING; } if (strcasecmp(lockname, "lockf") == 0) { #ifdef HAVE_LOCKF return LOCKF_LOCKING; #else /* ! HAVE_LOCKF */ - adios(NULL, "lockf not supported on this system"); + die("lockf not supported on this system"); #endif /* HAVE_LOCKF */ } if (strcasecmp(lockname, "flock") == 0) { #ifdef HAVE_FLOCK return FLOCK_LOCKING; #else /* ! HAVE_FLOCK */ - adios(NULL, "flock not supported on this system"); + die("flock not supported on this system"); #endif /* HAVE_FLOCK */ } if (strcasecmp(lockname, "dot") == 0) { - return DOT_LOCKING; + return DOT_LOCKING; } - adios(NULL, "Unknown lock type: \"%s\"", lockname); + die("Unknown lock type: \"%s\"", lockname); /* NOTREACHED */ return 0; }