diff options
author | Michel Dänzer <mdaenzer@redhat.com> | 2020-01-31 18:22:48 +0100 |
---|---|---|
committer | Marge Bot <eric+marge@anholt.net> | 2020-02-04 19:53:09 +0000 |
commit | ed271a9c2f40f8ec881bf3e4568d35dbfcd9cf70 (patch) | |
tree | 40b401b62240a29ad540475b536875bb19571afa | |
parent | b8db24315a302118ee9da060b2ef360f6622bf0c (diff) |
util: Add os_same_file_description helper
Acked-by: Pierre-Eric Pelloux-Prayer <pierre-eric.pelloux-prayer@amd.com>
(Cherry picked from commit f76cbc7901f7d500f5a4f74aedfd29970d1efd00)
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/merge_requests/3693>
-rw-r--r-- | src/util/os_file.c | 23 | ||||
-rw-r--r-- | src/util/os_file.h | 8 |
2 files changed, 31 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 diff --git a/src/util/os_file.h b/src/util/os_file.h index d691302d12d..1972beba32b 100644 --- a/src/util/os_file.h +++ b/src/util/os_file.h @@ -8,6 +8,7 @@ #ifndef _OS_FILE_H_ #define _OS_FILE_H_ +#include <stdbool.h> #include <stdio.h> #ifdef __cplusplus @@ -30,6 +31,13 @@ os_file_create_unique(const char *filename, int filemode); char * os_read_file(const char *filename); +/* + * Returns true if the two file descriptors passed in can be determined to + * reference the same file description, false otherwise + */ +bool +os_same_file_description(int fd1, int fd2); + #ifdef __cplusplus } #endif |