[1202] in Kerberos-V5-bugs
krb5b4pl3: slave/kprop.c: use krb5_lock_file instead of doing locking directly
daemon@ATHENA.MIT.EDU (Jonathan I. Kamens)
Sun Mar 19 20:48:04 1995
From: "Jonathan I. Kamens" <jik@cam.ov.com>
Date: Sun, 19 Mar 1995 20:51:15 -0500
To: krb5-bugs@MIT.EDU
kprop should use the existing file-locking code in krb5_lock_file,
rather than duplicating its efforts.
Here's a patch.
--- slave/kprop.c 1995/03/13 20:41:24 1.1
+++ slave/kprop.c 1995/03/13 20:48:57 1.2
@@ -25,9 +25,6 @@
#include <errno.h>
-#ifdef POSIX_FILE_LOCKS
-#include <fcntl.h>
-#endif
#include <stdio.h>
#include <ctype.h>
@@ -49,6 +46,7 @@
#include <krb5/kdb_dbm.h>
#include <krb5/ext-proto.h>
#include <krb5/los-proto.h>
+#include <krb5/libos.h> /* used for krb5_lock_file */
#include <com_err.h>
#ifdef NEED_SYS_FCNTL_H
@@ -409,12 +407,11 @@
int *size;
{
int fd;
+ FILE *fp;
struct stat stbuf, stbuf_ok;
char *data_ok_fn;
static char ok[] = ".dump_ok";
-#ifdef POSIX_FILE_LOCKS
- struct flock lock_arg;
-#endif
+ krb5_error_code retval;
if ((fd = open(data_fn, O_RDONLY)) < 0) {
com_err(progname, errno, "while trying to open %s",
@@ -422,28 +419,25 @@
exit(1);
}
-#ifdef POSIX_FILE_LOCKS
- lock_arg.l_whence = 0;
- lock_arg.l_start = 0;
- lock_arg.l_len = 0;
- if (fcntl(fd, F_SETLK, &lock_arg) == -1) {
- if (errno == EACCES || errno == EAGAIN)
- com_err(progname, 0, "database locked");
- else
- com_err(progname, errno, "while trying to flock %s",
- data_fn);
- exit(1);
- }
-#else
- if (flock(fd, LOCK_SH | LOCK_NB) < 0) {
- if (errno == EWOULDBLOCK || errno == EAGAIN)
- com_err(progname, 0, "database locked");
- else
- com_err(progname, errno, "while trying to flock %s",
- data_fn);
- exit(1);
+ if (! (fp = fopen(data_fn, "r")))
+ fd = -1;
+ else
+ fd = fileno(fp);
+ if (fd < 0) {
+ com_err(progname, errno, "while trying to open %s",
+ data_fn);
+ exit(1);
+ }
+ if (retval = krb5_lock_file(fp, data_fn, KRB5_LOCKMODE_SHARED |
+ KRB5_LOCKMODE_DONTBLOCK)) {
+ if (retval == EWOULDBLOCK || retval == EAGAIN)
+ com_err(progname, 0, "database locked");
+ else
+ com_err(progname, retval, "while trying to lock %s",
+ data_fn);
+ exit(1);
}
-#endif
+
if (fstat(fd, &stbuf)) {
com_err(progname, errno, "while trying to stat %s",
data_fn);
@@ -468,6 +462,13 @@
exit(1);
}
*size = stbuf.st_size;
+ /* XXX FILE * leak -- it would be nice if we closed fp before
+ kprop exits, but it doesn't really matter because the lock
+ will be released automatically when the program exits. On
+ the other hand, if this code is ever incorporated into a
+ long-running program, this could be a problem. I guess
+ we'll cross that bridge when we come to it.
+ */
return(fd);
}