]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ldb_mdb: Use mdb_env_get_fd() to get the FD for fstat() and FD_CLOEXEC
authorAndrew Bartlett <abartlet@samba.org>
Thu, 17 May 2018 01:10:25 +0000 (13:10 +1200)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 23 May 2018 00:27:11 +0000 (02:27 +0200)
This ensures we leave the FD behind if we exec() in a child process.

This deliberatly the same as TDB, as we want the same behaviour as
we have come to expect with that backend.

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
Reviewed-by: Garming Sam <garming@catalyst.net.nz>
lib/ldb/ldb_mdb/ldb_mdb.c

index 9364970f3f3387c5daf56df5959a83af0b675159..4eae7f4520221db9146984f5bad0d7496574235d 100644 (file)
@@ -715,6 +715,8 @@ static int lmdb_open_env(TALLOC_CTX *mem_ctx,
        struct mdb_env_wrap *w;
        struct stat st;
        pid_t pid = getpid();
+       int fd = 0;
+       unsigned v;
 
        if (stat(path, &st) == 0) {
                for (w=mdb_list;w;w=w->next) {
@@ -782,7 +784,20 @@ static int lmdb_open_env(TALLOC_CTX *mem_ctx,
                return ldb_mdb_err_map(ret);
        }
 
-       if (stat(path, &st) != 0) {
+       ret = mdb_env_get_fd(*env, &fd);
+       if (ret != 0) {
+               ldb_asprintf_errstring(ldb,
+                                      "Could not obtain DB FD %s: %s\n",
+                                      path, mdb_strerror(ret));
+               TALLOC_FREE(w);
+               return ldb_mdb_err_map(ret);
+       }
+
+       /* Just as for TDB: on exec, don't inherit the fd */
+       v = fcntl(fd, F_GETFD, 0);
+       fcntl(fd, F_SETFD, v | FD_CLOEXEC);
+
+       if (fstat(fd, &st) != 0) {
                ldb_asprintf_errstring(
                        ldb,
                        "Could not stat %s:\n",