/*
- * lock.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
- * complete copyright information.
- */
-
-/* Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
- *
- * Since liblockfile locking shares most of its code with dot locking, it
- * is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
- *
- * Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
- */
+** lock.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
+** complete copyright information.
+*/
+
+/*
+** Modified by Ruud de Rooij to support Miquel van Smoorenburg's liblockfile
+**
+** Since liblockfile locking shares most of its code with dot locking, it
+** is enabled by defining both DOT_LOCKING and HAVE_LIBLOCKFILE.
+**
+** Ruud de Rooij <ruud@debian.org> Sun, 28 Mar 1999 15:34:03 +0200
+*/
#include <h/mh.h>
#include <h/signals.h>
};
/*
- * Amount of time to wait before
- * updating ctime of lock file.
- */
+** Amount of time to wait before
+** updating ctime of lock file.
+*/
#define NSECS 20
#if !defined(HAVE_LIBLOCKFILE)
/*
- * How old does a lock file need to be
- * before we remove it.
- */
+** How old does a lock file need to be
+** before we remove it.
+*/
#define RSECS 180
#endif /* HAVE_LIBLOCKFILE */
#endif /* DOT_LOCKING */
/*
- * static prototypes
- */
+** static prototypes
+*/
#ifdef KERNEL_LOCKING
static int lkopen_kernel (char *, int, mode_t);
#endif
#endif
/*
- * Base routine to open and lock a file,
- * and return a file descriptor.
- */
+** Base routine to open and lock a file,
+** and return a file descriptor.
+*/
int
lkopen (char *file, int access, mode_t mode)
/*
- * Base routine to close and unlock a file,
- * given a file descriptor.
- */
+** Base routine to close and unlock a file,
+** given a file descriptor.
+*/
int
lkclose (int fd, char *file)
/*
- * Base routine to open and lock a file,
- * and return a FILE pointer
- */
+** Base routine to open and lock a file,
+** and return a FILE pointer
+*/
FILE *
lkfopen (char *file, char *mode)
/*
- * Base routine to close and unlock a file,
- * given a FILE pointer
- */
+** Base routine to close and unlock a file,
+** given a FILE pointer
+*/
int
lkfclose (FILE *fp, char *file)
#ifdef KERNEL_LOCKING
/*
- * open and lock a file, using kernel locking
- */
+** open and lock a file, using kernel locking
+*/
static int
lkopen_kernel (char *file, int access, mode_t mode)
access &= ~O_APPEND;
/*
- * We MUST have write permission or
- * lockf/fcntl() won't work
- */
+ ** We MUST have write permission or
+ ** lockf/fcntl() won't work
+ */
if ((access & 03) == O_RDONLY) {
access &= ~O_RDONLY;
access |= O_RDWR;
#ifdef DOT_LOCKING
/*
- * open and lock a file, using dot locking
- */
+** open and lock a file, using dot locking
+*/
static int
lkopen_dot (char *file, int access, mode_t mode)
return -1;
/*
- * Get the name of the eventual lock file, as well
- * as a name for a temporary lock file.
- */
+ ** Get the name of the eventual lock file, as well
+ ** as a name for a temporary lock file.
+ */
lockname (file, &lkinfo, 1);
#if !defined(HAVE_LIBLOCKFILE)
return fd;
} else {
/*
- * Abort locking, if we fail to lock after 5 attempts
- * and are never able to stat the lock file.
- */
+ ** Abort locking, if we fail to lock after 5
+ ** attempts and are never able to stat the
+ ** lock file.
+ */
struct stat st;
if (stat (lkinfo.curlock, &st) == -1) {
if (i++ > 5)
i = 0;
time (&curtime);
- /* check for stale lockfile, else sleep */
+ /*
+ ** check for stale lockfile,
+ ** else sleep
+ */
if (curtime > st.st_ctime + RSECS)
unlink (lkinfo.curlock);
else
#if !defined(HAVE_LIBLOCKFILE)
/*
- * Routine that actually tries to create
- * the lock file.
- */
+** Routine that actually tries to create
+** the lock file.
+*/
static int
lockit (struct lockinfo *li)
close (fd);
/*
- * Now try to create the real lock file
- * by linking to the temporary file.
- */
+ ** Now try to create the real lock file
+ ** by linking to the temporary file.
+ */
fd = link(tmplock, curlock);
unlink(tmplock);
#endif /* HAVE_LIBLOCKFILE */
/*
- * Get name of lock file, and temporary lock file
- */
+** Get name of lock file, and temporary lock file
+*/
static void
lockname (char *file, struct lockinfo *li, int isnewlock)
#if 0
/*
- * mmdf style dot locking. Currently not supported.
- * If we start supporting mmdf style dot locking,
- * we will need to change the return value of lockname
- */
+ ** mmdf style dot locking. Currently not supported.
+ ** If we start supporting mmdf style dot locking,
+ ** we will need to change the return value of lockname
+ */
if (stat (file, &st) == -1)
return -1;
#if !defined(HAVE_LIBLOCKFILE)
/*
- * If this is for a new lock, create a name for
- * the temporary lock file for lockit()
- */
+ ** If this is for a new lock, create a name for
+ ** the temporary lock file for lockit()
+ */
if (isnewlock) {
if ((cp = strrchr (li->curlock, '/')) == NULL || *++cp == 0)
strncpy (li->tmplock, ",LCK.XXXXXX", sizeof(li->tmplock));
/*
- * Add new lockfile to the list of open lockfiles
- * and start the lock file timer.
- */
+** Add new lockfile to the list of open lockfiles
+** and start the lock file timer.
+*/
static void
timerON (char *curlock, int fd)
/*
- * Search through the list of lockfiles for the
- * current lockfile, and remove it from the list.
- */
+** Search through the list of lockfiles for the
+** current lockfile, and remove it from the list.
+*/
static void
timerOFF (int fd)
/*
- * If timer goes off, we update the ctime of all open
- * lockfiles, so another command doesn't remove them.
- */
+** If timer goes off, we update the ctime of all open
+** lockfiles, so another command doesn't remove them.
+*/
static RETSIGTYPE
alrmser (int sig)