]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/lock.c
tdb2: check lock owner in tdb1 backend.
[ccan] / ccan / tdb2 / lock.c
index 7d4311c8e568b8125d72d2e91ec3dd0c8b08d4b9..bf62d9719e90c60f795ecd8acbce34c99df212b4 100644 (file)
@@ -30,7 +30,7 @@
 #include <ccan/build_assert/build_assert.h>
 
 /* If we were threaded, we could wait for unlock, but we're not, so fail. */
-static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call)
+enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call)
 {
        return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
                          "%s: lock owned by another tdb in this process.",
@@ -38,8 +38,7 @@ static enum TDB_ERROR owner_conflict(struct tdb_context *tdb, const char *call)
 }
 
 /* If we fork, we no longer really own locks. */
-static bool check_lock_pid(struct tdb_context *tdb,
-                          const char *call, bool log)
+bool check_lock_pid(struct tdb_context *tdb, const char *call, bool log)
 {
        /* No locks?  No problem! */
        if (tdb->file->allrecord_lock.count == 0
@@ -787,6 +786,11 @@ enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb,
                        return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,
                                          "tdb_unlock_hashes RO allrecord!");
                }
+               if (tdb->file->allrecord_lock.owner != tdb) {
+                       return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_USE_ERROR,
+                                         "tdb_unlock_hashes:"
+                                         " not locked by us!");
+               }
                return TDB_SUCCESS;
        }
 
@@ -817,6 +821,10 @@ enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off,
                if (!check_lock_pid(tdb, "tdb_lock_free_bucket", true))
                        return TDB_ERR_LOCK;
 
+               if (tdb->file->allrecord_lock.owner != tdb) {
+                       return owner_conflict(tdb, "tdb_lock_free_bucket");
+               }
+
                if (tdb->file->allrecord_lock.ltype == F_WRLCK)
                        return 0;
                return tdb_logerr(tdb, TDB_ERR_LOCK, TDB_LOG_ERROR,