From 8d5c67ee5ded0080b2780d8f958375d160f1a2f5 Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Fri, 20 Aug 2021 19:17:02 +0800 Subject: [PATCH] [TD-6164]: fix deadlock with fopen's malloc --- src/os/src/linux/linuxEnv.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/src/os/src/linux/linuxEnv.c b/src/os/src/linux/linuxEnv.c index e3eadbc94b..a08a5aeed6 100644 --- a/src/os/src/linux/linuxEnv.c +++ b/src/os/src/linux/linuxEnv.c @@ -44,14 +44,20 @@ char cmdline[1024]; char* taosGetCmdlineByPID(int pid) { sprintf(cmdline, "/proc/%d/cmdline", pid); - FILE* f = fopen(cmdline, "r"); - if (f) { - size_t size; - size = fread(cmdline, sizeof(char), 1024, f); - if (size > 0) { - if ('\n' == cmdline[size - 1]) cmdline[size - 1] = '\0'; - } - fclose(f); + + int fd = open(cmdline, O_RDONLY); + if (fd >= 0) { + int n = read(fd, cmdline, sizeof(cmdline) - 1); + if (n < 0) n = 0; + + if (n > 0 && cmdline[n - 1] == '\n') --n; + + cmdline[n] = 0; + + close(fd); + } else { + cmdline[0] = 0; } + return cmdline; } -- GitLab