About Social Code
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Osipenko <dmitry.osipenko@collabora.com>2024-09-03 12:00:31 +0300
committerMarge Bot <emma+marge@anholt.net>2024-10-25 18:06:14 +0000
commit6a2f5cb5568131b3d7aa41dce76c1c95b9403d4c (patch)
tree7a5ad8e8bdba77383b9561d4b454059e77772def
parent92893309bcc0c1a9ab9eab844a896d99cbc4b4e2 (diff)
util/mesa-db: Fix missing O_CLOEXEC
Use O_CLOEXEC flag for opened cache DB files to not leak cache FDs when process forks. Fixes: 32211788d053 ("util/disk_cache: Add new mesa-db cache type") Suggested-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Reviewed-by: Lionel Landwerlin <lionel.g.landwerlin@intel.com> Closes: https://gitlab.freedesktop.org/mesa/mesa/-/issues/11810 Signed-off-by: Dmitry Osipenko <dmitry.osipenko@collabora.com> Acked-by: Timothy Arceri <tarceri@itsqueeze.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/30988>
-rw-r--r--src/util/mesa_cache_db.c28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/util/mesa_cache_db.c b/src/util/mesa_cache_db.c
index a53c297f43b..77a89a20fa4 100644
--- a/src/util/mesa_cache_db.c
+++ b/src/util/mesa_cache_db.c
@@ -461,10 +461,21 @@ mesa_db_reload(struct mesa_cache_db *db)
return mesa_db_load(db, true);
}
-static void
-touch_file(const char* path)
+static FILE *
+mesa_db_fopen(const char *path)
{
- close(open(path, O_CREAT | O_CLOEXEC, 0644));
+ /* The fopen("r+b") mode doesn't auto-create new file, hence we need to
+ * explicitly create the file first.
+ */
+ int fd = open(path, O_CREAT | O_CLOEXEC | O_RDWR, 0644);
+ if (fd < 0)
+ return NULL;
+
+ FILE *f = fdopen(fd, "r+b");
+ if (!f)
+ close(fd);
+
+ return f;
}
static bool
@@ -475,12 +486,7 @@ mesa_db_open_file(struct mesa_cache_db_file *db_file,
if (asprintf(&db_file->path, "%s/%s", cache_path, filename) == -1)
return false;
- /* The fopen("r+b") mode doesn't auto-create new file, hence we need to
- * explicitly create the file first.
- */
- touch_file(db_file->path);
-
- db_file->file = fopen(db_file->path, "r+b");
+ db_file->file = mesa_db_fopen(db_file->path);
if (!db_file->file) {
free(db_file->path);
return false;
@@ -571,8 +577,8 @@ mesa_db_compact(struct mesa_cache_db *db, int64_t blob_size,
if (!entries)
return false;
- compacted_cache = fopen(db->cache.path, "r+b");
- compacted_index = fopen(db->index.path, "r+b");
+ compacted_cache = mesa_db_fopen(db->cache.path);
+ compacted_index = mesa_db_fopen(db->index.path);
if (!compacted_cache || !compacted_index)
goto cleanup;