lockfile patch
Heath Kehoe
heath.kehoe at intermec.com
Mon Aug 14 23:13:11 UTC 2000
This patch was made against CURRENT; but it also applies to 2.3.
* Rename LockFile to lock_file (per Russ' suggestion).
* Add type parameter to lock_file and change its return value to
be consistent with LockRange.
* Modify lockf() version of lock_file so it will lock
the entire file even if the current position of the file
is not at the beginning, because lockf() locks a range
starting at the current position.
-heath
Index: backends/filechan.c
===================================================================
RCS file: /home/heath/inn/repository/inn/backends/filechan.c,v
retrieving revision 1.5
diff -u -r1.5 filechan.c
--- backends/filechan.c 1998/05/21 03:00:10 1.5
+++ backends/filechan.c 2000/08/14 21:53:16
@@ -126,7 +126,7 @@
fd = open(p, O_CREAT | O_WRONLY | O_APPEND, BATCHFILE_MODE);
if (fd >= 0) {
/* Try to lock it and set the ownership right. */
- (void)LockFile(fd, TRUE);
+ (void)lock_file(fd, LOCK_WRITE, TRUE);
if (myuid == 0 && uid != 0)
(void)chown(p, uid, gid);
Index: backends/innxmit.c
===================================================================
RCS file: /home/heath/inn/repository/inn/backends/innxmit.c,v
retrieving revision 1.38
diff -u -r1.38 innxmit.c
--- backends/innxmit.c 2000/07/30 02:44:07 1.38
+++ backends/innxmit.c 2000/08/14 21:53:50
@@ -1008,7 +1008,7 @@
SMshutdown();
exit(1);
}
- if (LockFile(QIOfileno(BATCHqp), TRUE) < 0) {
+ if (!lock_file(QIOfileno(BATCHqp), LOCK_WRITE, TRUE)) {
#if defined(EWOULDBLOCK)
if (errno == EWOULDBLOCK) {
SMshutdown();
Index: doc/man/libinn.3
===================================================================
RCS file: /home/heath/inn/repository/inn/doc/man/libinn.3,v
retrieving revision 1.9
diff -u -r1.9 libinn.3
--- doc/man/libinn.3 2000/07/29 06:17:02 1.9
+++ doc/man/libinn.3 2000/08/14 22:47:07
@@ -66,10 +66,11 @@
.B " int fd;"
.B " int flag;"
-.B "int"
-.B "LockFile(fd, flag)"
+.B "BOOL"
+.B "lock_file(fd, type, flag)"
.B " int fd;"
-.B " int flag;"
+.B " LOCKTYPE type;"
+.B " BOOL block;"
.B "int"
.B "ReadInnConf()"
@@ -294,14 +295,16 @@
is zero) non-blocking I/O on the indicated descriptor.
It returns \-1 on failure or zero on success.
.PP
-.I LockFile
+.I lock_file
tries to lock the file descriptor
.IR fd .
If
-.I flag
-is non-zero it will block until the lock can be made, otherwise
-it will return \-1 if the file cannot be locked.
-It returns \-1 on failure or zero on success.
+.I block
+is TRUE it will block until the lock can be made, otherwise
+it will return FALSE if the file cannot be locked.
+.I type
+is one of: LOCK_READ, LOCK_WRITE, or LOCK_UNLOCK.
+It returns FALSE on failure or TRUE on success.
.PP
.I ReadInnConf
Reads the values of the
Index: include/libinn.h
===================================================================
RCS file: /home/heath/inn/repository/inn/include/libinn.h,v
retrieving revision 1.80
diff -u -r1.80 libinn.h
--- include/libinn.h 2000/08/11 06:58:51 1.80
+++ include/libinn.h 2000/08/14 22:18:33
@@ -140,11 +140,10 @@
extern void CAclose(void);
/* File locking. */
-extern int LockFile(int fd, BOOL block);
-
-#ifdef HAVE_FCNTL
typedef enum { LOCK_READ, LOCK_WRITE, LOCK_UNLOCK } LOCKTYPE;
+extern BOOL lock_file(int fd, LOCKTYPE type, BOOL block);
+#ifdef HAVE_FCNTL
extern BOOL LockRange(int fd, LOCKTYPE type, BOOL block,
OFFSET_T offset, OFFSET_T size);
#endif
Index: lib/lockfile.c
===================================================================
RCS file: /home/heath/inn/repository/inn/lib/lockfile.c,v
retrieving revision 1.6
diff -u -r1.6 lockfile.c
--- lib/lockfile.c 1999/10/23 21:45:27 1.6
+++ lib/lockfile.c 2000/08/14 22:12:17
@@ -2,7 +2,7 @@
**
** Lock a file or a range in a file, portably.
**
-** Provides a LockFile() function to lock a file, and if fcntl() is
+** Provides a lock_file() function to lock a file, and if fcntl() is
** available, a LockRange() function to lock a range of a file. Prefer
** fcntl() if available. If not, fall back on flock() and then to
** lockf() as a last resort. If all fails, don't lock at all, and return
@@ -23,26 +23,38 @@
# define SEEK_SET 0
# endif
-int
-LockFile(int fd, BOOL block)
+BOOL
+lock_file(int fd, LOCKTYPE type, BOOL block)
{
struct flock fl;
- fl.l_type = F_WRLCK;
+ switch(type) {
+ case LOCK_READ: fl.l_type = F_RDLCK; break;
+ case LOCK_WRITE: fl.l_type = F_WRLCK; break;
+ default:
+ case LOCK_UNLOCK: fl.l_type = F_UNLCK; break;
+ }
fl.l_whence = SEEK_SET;
fl.l_start = 0;
fl.l_len = 0;
- return fcntl(fd, block ? F_SETLKW : F_SETLK, &fl);
+ return (fcntl(fd, block ? F_SETLKW : F_SETLK, &fl) == 0);
}
#else /* !HAVE_FCNTL */
# ifdef HAVE_FLOCK
# include <sys/file.h>
-int
-LockFile(int fd, BOOL block)
+BOOL
+lock_file(int fd, LOCKTYPE type, BOOL block)
{
- return flock(fd, LOCK_EX | (block ? 0 : LOCK_NB));
+ int mode;
+ switch(type) {
+ case LOCK_READ: mode = LOCK_SH | (block ? 0 : LOCK_NB); break;
+ case LOCK_WRITE: mode = LOCK_EX | (block ? 0 : LOCK_NB); break;
+ default:
+ case LOCK_UNLOCK: mode = LOCK_UN; break;
+ }
+ return (flock(fd, mode) == 0);
}
# else /* !HAVE_FLOCK */
@@ -54,21 +66,36 @@
# include <fcntl.h>
# endif
-int
-LockFile(int fd, BOOL block)
+BOOL
+lock_file(int fd, LOCKTYPE type, BOOL block)
{
- return lockf(fd, block ? F_LOCK : F_TLOCK, 0);
+ BOOL ret;
+ int mode;
+ struct stat Sb;
+ off_t pos = lseek(fd, 0, SEEK_CUR);
+ switch(type) {
+ case LOCK_READ: return fstat(fd, &Sb);
+ case LOCK_WRITE: mode = block ? F_LOCK : F_TLOCK; break;
+ default:
+ case LOCK_UNLOCK: mode = F_ULOCK; break;
+ }
+ if(pos != -1)
+ lseek(fd, 0, SEEK_SET);
+ ret = (lockf(fd, mode, 0) == 0);
+ if(pos != -1)
+ lseek(fd, pos, SEEK_SET);
+ return ret;
}
# else /* !HAVE_LOCKF */
# include <sys/stat.h>
-int
-LockFile(int fd, BOOL block)
+BOOL
+lock_file(int fd, LOCKTYPE type, BOOL block)
{
struct stat Sb;
- return fstat(fd, &Sb);
+ return (fstat(fd, &Sb) == 0);
}
# endif /* !HAVE_LOCKF */
Index: storage/timecaf/caf.c
===================================================================
RCS file: /home/heath/inn/repository/inn/storage/timecaf/caf.c,v
retrieving revision 1.5
diff -u -r1.5 caf.c
--- storage/timecaf/caf.c 2000/07/30 02:51:23 1.5
+++ storage/timecaf/caf.c 2000/08/14 22:05:12
@@ -765,7 +765,7 @@
}
/* shouldn't be anyone else locking our file, since temp file has unique
PID-based name ... */
- if (LockFile(fd, FALSE) < 0) {
+ if (!lock_file(fd, LOCK_WRITE, FALSE)) {
CAFError(CAF_ERR_IO);
(void) close(fd);
return -1;
@@ -857,7 +857,7 @@
}
/* try a nonblocking lock attempt first. */
- if (LockFile(fd, FALSE) >= 0) break;
+ if (lock_file(fd, LOCK_WRITE, FALSE)) break;
if (!waitlock) {
CAFError(CAF_ERR_FILEBUSY);
@@ -865,7 +865,7 @@
return -1;
}
/* wait around to try and get a lock. */
- (void) LockFile(fd, TRUE);
+ (void) lock_file(fd, LOCK_WRITE, TRUE);
/*
** and then close and reopen the file, in case someone changed the
** file out from under us.
@@ -1238,10 +1238,10 @@
}
}
/* try a nonblocking lock attempt first. */
- if (LockFile(fd, FALSE) >= 0) break;
+ if (lock_file(fd, LOCK_WRITE, FALSE)) break;
/* wait around to try and get a lock. */
- (void) LockFile(fd, TRUE);
+ (void) lock_file(fd, LOCK_WRITE, TRUE);
/*
** and then close and reopen the file, in case someone changed the
** file out from under us.
@@ -1466,10 +1466,10 @@
}
/* try a nonblocking lock attempt first. */
- if (LockFile(fdin, FALSE) >= 0) break;
+ if (lock_file(fdin, LOCK_WRITE, FALSE)) break;
/* wait around to try and get a lock. */
- (void) LockFile(fdin, TRUE);
+ (void) lock_file(fdin, LOCK_WRITE, TRUE);
/*
** and then close and reopen the file, in case someone changed the
** file out from under us.
More information about the inn-patches
mailing list