X-Git-Url: http://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftdb2%2Fopen.c;h=e88c9e694c531b67bf4a6ff33d113a3b4078ad59;hb=252bd427f813c2709c07e64378d5a6c67f51a922;hp=1292fcc1ee32ee59431284527706461696096211;hpb=8cca0397ef6f6017b13ce9ab4999bf3d92a2dee5;p=ccan
diff --git a/ccan/tdb2/open.c b/ccan/tdb2/open.c
index 1292fcc1..e88c9e69 100644
--- a/ccan/tdb2/open.c
+++ b/ccan/tdb2/open.c
@@ -1,3 +1,20 @@
+ /*
+ Trivial Database 2: opening and closing TDBs
+ Copyright (C) Rusty Russell 2010
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 3 of the License, or (at your option) any later version.
+
+ This library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with this library; if not, see .
+*/
#include "private.h"
#include
#include
@@ -333,6 +350,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
struct tdb_attribute_openhook *openhook = NULL;
tdb_bool_err berr;
enum TDB_ERROR ecode;
+ int openlock;
tdb = malloc(sizeof(*tdb) + (name ? strlen(name) + 1 : 0));
if (!tdb) {
@@ -383,7 +401,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
}
if (tdb_flags & ~(TDB_INTERNAL | TDB_NOLOCK | TDB_NOMMAP | TDB_CONVERT
- | TDB_NOSYNC | TDB_SEQNUM)) {
+ | TDB_NOSYNC | TDB_SEQNUM | TDB_ALLOW_NESTING)) {
ecode = tdb_logerr(tdb, TDB_ERR_EINVAL, TDB_LOG_USE_ERROR,
"tdb_open: unknown flags %u", tdb_flags);
goto fail;
@@ -399,9 +417,11 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
if ((open_flags & O_ACCMODE) == O_RDONLY) {
tdb->read_only = true;
tdb->mmap_flags = PROT_READ;
+ openlock = F_RDLCK;
} else {
tdb->read_only = false;
tdb->mmap_flags = PROT_READ | PROT_WRITE;
+ openlock = F_WRLCK;
}
/* internal databases don't need any of the rest. */
@@ -446,12 +466,15 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
tdb_logerr(tdb, TDB_ERR_IO, TDB_LOG_ERROR,
"tdb_open: could not stat open %s: %s",
name, strerror(errno));
+ close(fd);
goto fail_errno;
}
ecode = tdb_new_file(tdb);
- if (ecode != TDB_SUCCESS)
+ if (ecode != TDB_SUCCESS) {
+ close(fd);
goto fail;
+ }
tdb->file->next = files;
tdb->file->fd = fd;
@@ -462,7 +485,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
}
/* ensure there is only one process initialising at once */
- ecode = tdb_lock_open(tdb, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
+ ecode = tdb_lock_open(tdb, openlock, TDB_LOCK_WAIT|TDB_LOCK_NOCHECK);
if (ecode != TDB_SUCCESS) {
saved_errno = errno;
goto fail_errno;
@@ -534,7 +557,7 @@ struct tdb_context *tdb_open(const char *name, int tdb_flags,
goto fail;
}
- tdb_unlock_open(tdb);
+ tdb_unlock_open(tdb, openlock);
/* This make sure we have current map_size and mmap. */
tdb->methods->oob(tdb, tdb->file->map_size + 1, true);