From 51aa986303a4f8a8209d7bfb3a5f4583aa8030a1 Mon Sep 17 00:00:00 2001 From: Mateusz Moscicki Date: Tue, 28 Aug 2018 15:27:30 +0200 Subject: [PATCH] Move the function to read /proc//exe symlink to util.c Change-Id: Ieb533fb66dd3d06eb5bbc827aa016a308cf892a3 --- src/crash-manager/crash-manager.c | 47 ++-------------------------------- src/crash-stack/CMakeLists.txt | 2 +- src/crash-stack/crash-stack.c | 16 ++++-------- src/shared/util.c | 53 ++++++++++++++++++++++++++++++++++++++- src/shared/util.h | 4 +++ 5 files changed, 64 insertions(+), 58 deletions(-) diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c index 3f3e089..53b0175 100644 --- a/src/crash-manager/crash-manager.c +++ b/src/crash-manager/crash-manager.c @@ -428,51 +428,8 @@ static int make_dump_dir(void) static int get_cmd_info(struct crash_info *cinfo) { - int fd; - int ret; - char cmdline[PATH_MAX]; - char cmdline_path[PATH_MAX]; - char exe_link[PATH_MAX]; - - snprintf(cmdline_path, sizeof(cmdline_path), - "/proc/%s/cmdline", cinfo->pid_info); - fd = open(cmdline_path, O_RDONLY); - if (fd < 0) { - _E("Failed to open %s", cmdline_path); - return -1; - } - - ret = read(fd, cmdline, sizeof(cmdline) - 1); - if (ret <= 0) { - _E("Failed to read %s", cmdline_path); - goto error; - } - - cmdline[ret] = '\0'; - - strncpy(cinfo->cmd_line, cmdline, sizeof(cinfo->cmd_line)); - - snprintf(exe_link, sizeof(exe_link), - "/proc/%s/exe", cinfo->pid_info); - - ret = readlink(exe_link, cinfo->cmd_path, - sizeof(cinfo->cmd_path) - 1); - if (ret <= 0) { - _E("Failed to read link %s", exe_link); - goto error; - } - - cinfo->cmd_path[ret] = '\0'; - - if (access(cinfo->cmd_path, F_OK) == -1) { - _E("Invalid path %s", cinfo->cmd_path); - ret = -1; - goto error; - } - -error: - close(fd); - return ret; + return get_cmd_line(cinfo->pid_info, cinfo->cmd_line, sizeof(cinfo->cmd_path)) && + get_exe_path(cinfo->pid_info, cinfo->cmd_path, sizeof(cinfo->cmd_path)); } static int set_prstatus() diff --git a/src/crash-stack/CMakeLists.txt b/src/crash-stack/CMakeLists.txt index ddeba3a..fd26fac 100644 --- a/src/crash-stack/CMakeLists.txt +++ b/src/crash-stack/CMakeLists.txt @@ -4,7 +4,7 @@ set(CRASH_STACK_BIN "crash-stack") # Common source code files INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src) -set(CRASH_STACK_SRCS crash-stack.c crash-stack-libunw.c unwind.c proc.c mem_map.c) +set(CRASH_STACK_SRCS crash-stack.c crash-stack-libunw.c unwind.c proc.c mem_map.c ${CMAKE_SOURCE_DIR}/src/shared/util.c) # Add architecture dependent source files if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64") diff --git a/src/crash-stack/crash-stack.c b/src/crash-stack/crash-stack.c index 5979c2f..2f65b7c 100644 --- a/src/crash-stack/crash-stack.c +++ b/src/crash-stack/crash-stack.c @@ -50,6 +50,7 @@ #define LOG_TAG "CRASH_STACK" #include "shared/log.h" +#include "shared/util.h" #define BUF_SIZE (BUFSIZ) #define HEXA 16 @@ -257,22 +258,15 @@ void callstack_destructor(Callstack *callstack) */ static void __crash_stack_print_exe(FILE* outputfile, pid_t pid) { - int fd, ret; char file_path[PATH_MAX]; - char cmd_path[PATH_MAX]; + char pid_str[11]; - snprintf(cmd_path, PATH_MAX, "/proc/%d/cmdline", pid); - if ((fd = open(cmd_path, O_RDONLY)) < 0) - return; + snprintf(pid_str, sizeof(pid_str), "%d", pid); - if ((ret = read(fd, file_path, sizeof(file_path) - 1)) <= 0) { - close(fd); - return; - } - file_path[ret] = '\0'; + if (get_exe_path(pid_str, file_path, sizeof(file_path)) == 0) + snprintf(file_path, sizeof(file_path), ""); fprintf(outputfile, "Executable File Path: %s\n", file_path); - close(fd); } /** diff --git a/src/shared/util.c b/src/shared/util.c index 3795415..2eb4399 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -14,8 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif #include #include #include @@ -817,6 +818,56 @@ int find_crash_tid(int pid) } return -1; } + +int get_cmd_line(char* pid, char *cmd, size_t len) +{ + char cmdline_path[PATH_MAX]; + + snprintf(cmdline_path, sizeof(cmdline_path), + "/proc/%s/cmdline", pid); + + int fd = open(cmdline_path, O_RDONLY); + if (fd < 0) { + _E("Failed to open %s: %m", cmdline_path); + return 0; + } + + ssize_t ret = read(fd, cmd, len - 1); + close(fd); + + if (ret <= 0) { + _E("Failed to read %s: %m", cmdline_path); + return 0; + } + + cmd[ret] = '\0'; + + return 1; +} + +int get_exe_path(char* pid, char *path, size_t len) +{ + char exe_link[PATH_MAX]; + + snprintf(exe_link, sizeof(exe_link), + "/proc/%s/exe", pid); + + ssize_t ret = readlink(exe_link, path, len - 1); + + if (ret <= 0) { + _E("Failed to read link %s: %m", exe_link); + return 0; + } + + path[ret] = '\0'; + + if (access(path, F_OK) == -1) { + _E("Invalid path %s", path); + return 0; + } + + return 1; +} /** * @} */ diff --git a/src/shared/util.h b/src/shared/util.h index 301b4e6..0cb35f2 100644 --- a/src/shared/util.h +++ b/src/shared/util.h @@ -62,6 +62,10 @@ int validate_file_name(char *name, int len); int log_kmsg(char *fmt, ...); int find_crash_tid(int pid); + +int get_exe_path(char* pid, char *path, size_t len); + +int get_cmd_line(char* pid, char *cmd, size_t len); /** * @} */ -- 2.7.4