From 7b94ee79e3896251f74f77da7a5a516fd536d53d Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Thu, 8 Aug 2019 10:49:27 +0200 Subject: [PATCH] shared: Simplify get_exe_path Change-Id: I26839430a4836454f96c1f273a1500899dc6b607 --- src/crash-manager/crash-manager.c | 8 +++++--- src/crash-stack/crash-stack.c | 11 +++-------- src/shared/util.c | 31 ++++++++++++------------------- src/shared/util.h | 2 +- 4 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c index fedd35f..b01253c 100644 --- a/src/crash-manager/crash-manager.c +++ b/src/crash-manager/crash-manager.c @@ -265,11 +265,13 @@ static bool get_cmd_info(struct crash_info *cinfo) if (!read_proc_file(cinfo->pid_info, "cmdline", cinfo->cmd_line, PATH_MAX, NULL)) goto err; - cinfo->cmd_path = get_exe_path(cinfo->pid_info); - if (!cinfo->cmd_path) + char buf[PATH_MAX]; + if (!get_exe_path(cinfo->pid_info, buf, sizeof(buf))) goto err; - return true; + cinfo->cmd_path = strdup(buf); + + return !!cinfo->cmd_path; err: free(cinfo->cmd_line); diff --git a/src/crash-stack/crash-stack.c b/src/crash-stack/crash-stack.c index c0f9604..0445d9c 100644 --- a/src/crash-stack/crash-stack.c +++ b/src/crash-stack/crash-stack.c @@ -262,15 +262,10 @@ void callstack_destructor(Callstack *callstack) */ static void __crash_stack_print_exe(FILE* outputfile, pid_t pid) { - char *file_path; + char path[PATH_MAX]; + bool has_path = get_exe_path(pid, path, sizeof(path)); - file_path = get_exe_path(pid); - if (file_path == NULL) - file_path = strdup(""); - - fprintf(outputfile, "Executable File Path: %s\n", file_path); - - free(file_path); + fprintf(outputfile, "Executable File Path: %s\n", has_path ? path : ""); } /** diff --git a/src/shared/util.c b/src/shared/util.c index 84fa25e..1860bab 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -519,34 +519,27 @@ err_close: return false; } -char* get_exe_path(pid_t pid) +bool get_exe_path(pid_t pid, char *outbuf, size_t outbuf_len) { - char exe_link[PATH_MAX]; - char buffer[PATH_MAX]; + assert(outbuf); + assert(outbuf_len > 0); - snprintf(exe_link, sizeof(exe_link), - "/proc/%d/exe", pid); + char exe_link[PATH_MAX]; - ssize_t ret = readlink(exe_link, buffer, sizeof(buffer) - 1); + snprintf(exe_link, sizeof(exe_link), "/proc/%d/exe", pid); + ssize_t ret = readlink(exe_link, outbuf, outbuf_len - 1); if (ret <= 0) { _E("Failed to read link %s: %m", exe_link); - return NULL; - } - buffer[ret] = '\0'; - - if (access(buffer, F_OK) == -1) { - _E("Invalid path %s", buffer); - return NULL; + return false; } + outbuf[ret] = '\0'; - char *result; - if (asprintf(&result, "%s", buffer) == -1) { - _E("asprintf() error: %m\n"); - return NULL; + if (access(outbuf, F_OK) == -1) { + _E("Unable to access %s: %m", outbuf); + return false; } - - return result; + return true; } /* This function is supposed to accept same data as passed to execve diff --git a/src/shared/util.h b/src/shared/util.h index 0f81ca7..f3177e8 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -58,7 +58,7 @@ int log_kmsg(char *fmt, ...); int find_crash_tid(int pid); -char* get_exe_path(pid_t pid); +bool get_exe_path(pid_t pid, char *outbuf, size_t outbuf_len); typedef bool (*charp0filter)(char *buf, int size, int datalen); bool filter_drop_trailing_whitespace(char *buf, int size, int datalen); -- 2.7.4