X-Git-Url: http://git.marmaro.de/?p=mmh;a=blobdiff_plain;f=sbr%2Flock_file.c;h=d3d447ee3c0ffcc9f17138d0465065de2c89c27a;hp=5bc83cc8d8869ae0f47513a21cc864ca1ed51a9d;hb=ced6090a330d3d83d0bce709f756aa3d7d65fea4;hpb=337338b404931f06f0db2119c9e145e8ca5a9860 diff --git a/sbr/lock_file.c b/sbr/lock_file.c index 5bc83cc..d3d447e 100644 --- a/sbr/lock_file.c +++ b/sbr/lock_file.c @@ -1,18 +1,19 @@ /* - * 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 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 Sun, 28 Mar 1999 15:34:03 +0200 +*/ #include #include @@ -69,16 +70,16 @@ struct lockinfo { }; /* - * 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 */ @@ -94,8 +95,8 @@ static struct lock *l_top = NULL; #endif /* DOT_LOCKING */ /* - * static prototypes - */ +** static prototypes +*/ #ifdef KERNEL_LOCKING static int lkopen_kernel (char *, int, mode_t); #endif @@ -113,9 +114,9 @@ static int lockit (struct lockinfo *); #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) @@ -131,9 +132,9 @@ 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) @@ -182,9 +183,9 @@ 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) @@ -222,9 +223,9 @@ 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) @@ -275,8 +276,8 @@ 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) @@ -297,9 +298,9 @@ 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; @@ -349,8 +350,8 @@ lkopen_kernel (char *file, int access, mode_t mode) #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) @@ -363,9 +364,9 @@ 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) @@ -379,9 +380,10 @@ lkopen_dot (char *file, int access, mode_t mode) 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) @@ -392,7 +394,10 @@ lkopen_dot (char *file, int access, mode_t mode) 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 @@ -415,9 +420,9 @@ lkopen_dot (char *file, int access, mode_t mode) #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) @@ -454,9 +459,9 @@ 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); @@ -465,8 +470,8 @@ lockit (struct lockinfo *li) #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) @@ -499,10 +504,10 @@ 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; @@ -514,9 +519,9 @@ lockname (char *file, struct lockinfo *li, int isnewlock) #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)); @@ -529,9 +534,9 @@ lockname (char *file, struct lockinfo *li, int isnewlock) /* - * 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) @@ -558,9 +563,9 @@ 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) @@ -592,9 +597,9 @@ 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)