From d79debabca9eaa1f6ad9e5bee93a1aa76256f22a Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Thu, 11 Apr 2024 20:15:55 -0700 Subject: [PATCH] mcount: Protect FD for message channel uftrace and libmcount communicate using a pipe in the data directory. We need to keep the file descriptor open to ensure that uftrace and libmcount can communicate correctly. Some daemon programs will close all file descriptors during they startup, we need to protect file descriptor which will be used to communicate uftrace and libmcount to avoid broken communication issue. Signed-off-by: Rongsong Shen --- libmcount/wrap.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libmcount/wrap.c b/libmcount/wrap.c index f258052b2..ba69e9c64 100644 --- a/libmcount/wrap.c +++ b/libmcount/wrap.c @@ -267,6 +267,7 @@ static int (*real_posix_spawnp)(pid_t *pid, const char *file, static int (*real_execve)(const char *path, char *const argv[], char *const envp[]); static int (*real_execvpe)(const char *file, char *const argv[], char *const envp[]); static int (*real_fexecve)(int fd, char *const argv[], char *const envp[]); +static int (*real_close)(int fd); void mcount_hook_functions(void) { @@ -284,6 +285,7 @@ void mcount_hook_functions(void) real_execve = dlsym(RTLD_NEXT, "execve"); real_execvpe = dlsym(RTLD_NEXT, "execvpe"); real_fexecve = dlsym(RTLD_NEXT, "fexecve"); + real_close = dlsym(RTLD_NEXT, "close"); } __visible_default int backtrace(void **buffer, int sz) @@ -608,6 +610,18 @@ __visible_default int fexecve(int fd, char *const argv[], char *const envp[]) return real_fexecve(fd, argv, new_envp); } +__visible_default int close(int fd) +{ + if (unlikely(real_close == NULL)) + mcount_hook_functions(); + + if (unlikely(fd == pfd)) { + return 0; + } + + return real_close(fd); +} + #ifdef UNIT_TEST TEST_CASE(mcount_wrap_dlopen)