Skip to content

Commit

Permalink
mcount: Protect FD for message channel
Browse files Browse the repository at this point in the history
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 <rshen@shenrs.eu>
  • Loading branch information
Rongsong Shen authored and namhyung committed May 1, 2024
1 parent 68aed9b commit d79deba
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions libmcount/wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d79deba

Please sign in to comment.