From fe342309661e3fe8b9e192e6df6ef84267207dac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Benn=C3=A9e?= Date: Mon, 24 May 2021 12:19:18 +0100 Subject: [PATCH 2/2] linux-user: glib-ify is_proc_myself MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For the cost of a couple of heap allocations we can reduce the code complexity to a couple of string compares. While we are at it make the function a bool return and fixup the fake_open function prototypes. Signed-off-by: Alex Bennée --- v2 - use danpb's suggestion --- linux-user/syscall.c | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) diff --git a/linux-user/syscall.c b/linux-user/syscall.c index e739921e86..8af48b5f1f 100644 --- a/linux-user/syscall.c +++ b/linux-user/syscall.c @@ -7987,33 +7987,16 @@ static int open_self_auxv(void *cpu_env, int fd) return 0; } -static int is_proc_myself(const char *filename, const char *entry) -{ - if (!strncmp(filename, "/proc/", strlen("/proc/"))) { - filename += strlen("/proc/"); - if (!strncmp(filename, "self/", strlen("self/"))) { - filename += strlen("self/"); - } else if (*filename >= '1' && *filename <= '9') { - char myself[80]; - snprintf(myself, sizeof(myself), "%d/", getpid()); - if (!strncmp(filename, myself, strlen(myself))) { - filename += strlen(myself); - } else { - return 0; - } - } else { - return 0; - } - if (!strcmp(filename, entry)) { - return 1; - } - } - return 0; +static bool is_proc_myself(const char *filename, const char *entry) +{ + g_autofree char *procself = g_strdup_printf("/proc/self/%s", entry); + g_autofree char *procpid = g_strdup_printf("/proc/%d/%s", getpid(), entry); + return g_str_equal(filename, procself) || g_str_equal(filename, procpid); } #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN) || \ defined(TARGET_SPARC) || defined(TARGET_M68K) || defined(TARGET_HPPA) -static int is_proc(const char *filename, const char *entry) +static bool is_proc(const char *filename, const char *entry) { return strcmp(filename, entry) == 0; } @@ -8097,7 +8080,7 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags, struct fake_open { const char *filename; int (*fill)(void *cpu_env, int fd); - int (*cmp)(const char *s1, const char *s2); + bool (*cmp)(const char *s1, const char *s2); }; const struct fake_open *fake_open; static const struct fake_open fakes[] = { -- 2.20.1