summaryrefslogtreecommitdiffstats
path: root/lib/libc/db/hash
diff options
context:
space:
mode:
authormillert <millert@openbsd.org>2001-08-04 21:11:10 +0000
committermillert <millert@openbsd.org>2001-08-04 21:11:10 +0000
commit9a1282d03e81cc25a5ad8df9df7e44c6982f5c2f (patch)
tree3d55e8481fe5fa0c92ea51a6d722d9c197c8ed74 /lib/libc/db/hash
parentdon't return on void. (diff)
downloadwireguard-openbsd-9a1282d03e81cc25a5ad8df9df7e44c6982f5c2f.tar.xz
wireguard-openbsd-9a1282d03e81cc25a5ad8df9df7e44c6982f5c2f.zip
Move decision whether to make a new hash table or not to be after
the open(). That way if the user opened with O_EXLOCK and another process has the .db locked we don't decide to zero hash before the locking process has had a chance to write to the database. This also makes the logic simpler since we can just fstat the fd and check for size == 0 && !O_RDONLY. Closes PR 1934.
Diffstat (limited to 'lib/libc/db/hash')
-rw-r--r--lib/libc/db/hash/hash.c21
1 files changed, 8 insertions, 13 deletions
diff --git a/lib/libc/db/hash/hash.c b/lib/libc/db/hash/hash.c
index cd550fd06a3..f647dc81bed 100644
--- a/lib/libc/db/hash/hash.c
+++ b/lib/libc/db/hash/hash.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: hash.c,v 1.9 2001/01/03 15:24:09 millert Exp $ */
+/* $OpenBSD: hash.c,v 1.10 2001/08/04 21:11:10 millert Exp $ */
/*-
* Copyright (c) 1990, 1993, 1994
@@ -40,7 +40,7 @@
#if 0
static char sccsid[] = "@(#)hash.c 8.9 (Berkeley) 6/16/94";
#else
-static char rcsid[] = "$OpenBSD: hash.c,v 1.9 2001/01/03 15:24:09 millert Exp $";
+static char rcsid[] = "$OpenBSD: hash.c,v 1.10 2001/08/04 21:11:10 millert Exp $";
#endif
#endif /* LIBC_SCCS and not lint */
@@ -108,13 +108,12 @@ __hash_open(file, flags, mode, info, dflags)
HTAB *hashp;
struct stat statbuf;
DB *dbp;
- int bpages, hdrsize, new_table, nsegs, save_errno, rv;
+ int bpages, hdrsize, new_table, nsegs, save_errno;
if ((flags & O_ACCMODE) == O_WRONLY) {
errno = EINVAL;
return (NULL);
}
-
if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
return (NULL);
hashp->fp = -1;
@@ -127,19 +126,15 @@ __hash_open(file, flags, mode, info, dflags)
*/
hashp->flags = flags;
- new_table = 0;
- if (!file || (flags & O_TRUNC) ||
- ((rv = stat(file, &statbuf)) && errno == ENOENT) ||
- (rv == 0 && !statbuf.st_size && (flags & O_ACCMODE) != O_RDONLY)) {
- if (errno == ENOENT)
- errno = 0; /* Just in case someone looks at errno */
- new_table = 1;
- }
if (file) {
if ((hashp->fp = open(file, flags, mode)) == -1)
RETURN_ERROR(errno, error0);
(void)fcntl(hashp->fp, F_SETFD, 1);
- }
+ new_table = fstat(hashp->fp, &statbuf) == 0 &&
+ statbuf.st_size == 0 && (flags & O_ACCMODE) != O_RDONLY;
+ } else
+ new_table = 1;
+
if (new_table) {
if (!(hashp = init_hash(hashp, file, (HASHINFO *)info)))
RETURN_ERROR(errno, error1);