From: Andrew Bartlett Date: Thu, 17 May 2018 01:10:25 +0000 (+1200) Subject: ldb_mdb: Use mdb_env_get_fd() to get the FD for fstat() and FD_CLOEXEC X-Git-Tag: ldb-1.4.0~100 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4dc44659dd290035a5500a13197e9fcee7d9a21b;p=thirdparty%2Fsamba.git ldb_mdb: Use mdb_env_get_fd() to get the FD for fstat() and FD_CLOEXEC 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 Reviewed-by: Garming Sam --- diff --git a/lib/ldb/ldb_mdb/ldb_mdb.c b/lib/ldb/ldb_mdb/ldb_mdb.c index 9364970f3f3..4eae7f45202 100644 --- a/lib/ldb/ldb_mdb/ldb_mdb.c +++ b/lib/ldb/ldb_mdb/ldb_mdb.c @@ -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",