ldb_mdb: Store pid to change destructor on fork
authorGarming Sam <garming@catalyst.net.nz>
Mon, 5 Mar 2018 03:04:03 +0000 (16:04 +1300)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 23 May 2018 00:27:11 +0000 (02:27 +0200)
Signed-off-by: Garming Sam <garming@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/ldb/ldb_mdb/ldb_mdb.c
lib/ldb/ldb_mdb/ldb_mdb.h

index d154ede8ede9aaa36a5dc256b9250c044ecde904..e073e17da3cf5a35d2709b7742c844accaa80579 100644 (file)
@@ -595,6 +595,25 @@ static int lmdb_pvt_destructor(struct lmdb_private *lmdb)
 {
        struct lmdb_trans *ltx = NULL;
 
+       /* Check if this is a forked child */
+       if (getpid() != lmdb->pid) {
+               int fd = 0;
+               /*
+                * We cannot call mdb_env_close or commit any transactions,
+                * otherwise they might appear finished in the parent.
+                *
+                */
+
+               if (mdb_env_get_fd(lmdb->env, &fd) == 0) {
+                       close(fd);
+               }
+
+               /* Remove the pointer, so that no access should occur */
+               lmdb->env = NULL;
+
+               return 0;
+       }
+
        /*
         * Close the read transaction if it's open
         */
@@ -685,6 +704,9 @@ static int lmdb_pvt_open(TALLOC_CTX *mem_ctx,
                return ldb_mdb_err_map(ret);
        }
 
+       /* Store the original pid during the LMDB open */
+       lmdb->pid = getpid();
+
        return LDB_SUCCESS;
 }
 
index d4a635ca6931226595782734048b939f5ef2acb9..8f21493927b535f380cbf9d9870fa0672492c480 100644 (file)
@@ -41,6 +41,8 @@ struct lmdb_private {
        int error;
        MDB_txn *read_txn;
 
+       pid_t pid;
+
 };
 
 struct lmdb_trans {