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)
committerStefan Metzmacher <metze@samba.org>
Thu, 12 Apr 2018 14:27:16 +0000 (16: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 8505b29d0f465e7f58937f125df63b3fbe07dcfa..8c2fa66087deaf5da7fbadc82fefe0cd294190e0 100644 (file)
@@ -592,6 +592,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
         */
@@ -678,6 +697,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 {