aboutsummaryrefslogtreecommitdiffstats
path: root/src/util/os_file.c
diff options
context:
space:
mode:
authorMichel Dänzer <mdaenzer@redhat.com>2020-01-06 18:24:52 +0100
committerMichel Dänzer <michel@daenzer.net>2020-01-23 17:39:34 +0100
commitf76cbc7901f7d500f5a4f74aedfd29970d1efd00 (patch)
tree0ee857c4cb81541c004ed1e762607f88449b8f04 /src/util/os_file.c
parentc6468f66c7a47f0e16df2f1200db33eef6d2d1f4 (diff)
util: Add os_same_file_description helper
Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3202>
Diffstat (limited to 'src/util/os_file.c')
-rw-r--r--src/util/os_file.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/util/os_file.c b/src/util/os_file.c
index c670e127c6b..b502ff4b0ef 100644
--- a/src/util/os_file.c
+++ b/src/util/os_file.c
@@ -34,7 +34,9 @@ os_file_create_unique(const char *filename, int filemode)
#if defined(__linux__)
#include <fcntl.h>
+#include <linux/kcmp.h>
#include <sys/stat.h>
+#include <sys/syscall.h>
#include <unistd.h>
@@ -130,8 +132,18 @@ os_read_file(const char *filename)
return buf;
}
+bool
+os_same_file_description(int fd1, int fd2)
+{
+ pid_t pid = getpid();
+
+ return syscall(SYS_kcmp, pid, pid, KCMP_FILE, fd1, fd2) == 0;
+}
+
#else
+#include "u_debug.h"
+
char *
os_read_file(const char *filename)
{
@@ -139,4 +151,15 @@ os_read_file(const char *filename)
return NULL;
}
+bool
+os_same_file_description(int fd1, int fd2)
+{
+ if (fd1 == fd2)
+ return true;
+
+ debug_warn_once("Can't tell if different file descriptors reference the same"
+ " file description, false negatives might cause trouble!\n");
+ return false;
+}
+
#endif