summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>2018-08-28 15:27:30 +0200
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>2018-08-29 13:27:36 +0200
commitf413310474a680cbec372108264fa15162e9e1fd (patch)
tree90758fe92652e028d05d54790dc0bd93ad3dedab
parent7cadf4da37165e84efb29c2d6cadac8ac18c2ce4 (diff)
downloadcrash-worker-f413310474a680cbec372108264fa15162e9e1fd.tar.gz
crash-worker-f413310474a680cbec372108264fa15162e9e1fd.tar.bz2
crash-worker-f413310474a680cbec372108264fa15162e9e1fd.zip
Move the function to read /proc/<pid>/exe symlink to util.c
Change-Id: Ieb533fb66dd3d06eb5bbc827aa016a308cf892a3
-rw-r--r--src/crash-manager/crash-manager.c45
-rw-r--r--src/crash-stack/CMakeLists.txt8
-rw-r--r--src/crash-stack/crash-stack.c16
-rw-r--r--src/shared/util.c53
-rw-r--r--src/shared/util.h4
5 files changed, 68 insertions, 58 deletions
diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c
index f7a78ef..2627b06 100644
--- a/src/crash-manager/crash-manager.c
+++ b/src/crash-manager/crash-manager.c
@@ -406,49 +406,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 3e41269..0c1a8a4 100644
--- a/src/crash-stack/CMakeLists.txt
+++ b/src/crash-stack/CMakeLists.txt
@@ -3,7 +3,8 @@ cmake_minimum_required(VERSION 2.8.11)
set(CRASH_STACK_BIN "crash-stack")
# Common source code files
-set(CRASH_STACK_SRCS crash-stack.c crash-stack-libunw.c)
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
+set(CRASH_STACK_SRCS crash-stack.c crash-stack-libunw.c ${CMAKE_SOURCE_DIR}/src/shared/util.c)
# Add architecture dependent source files
if (${CMAKE_SYSTEM_PROCESSOR} STREQUAL "aarch64")
@@ -28,10 +29,11 @@ add_executable(${CRASH_STACK_BIN} ${CRASH_STACK_SRCS})
include(FindPkgConfig)
pkg_check_modules(LIBUNWIND_PTRACE REQUIRED libunwind-ptrace)
-set_property(TARGET ${CRASH_STACK_BIN} APPEND_STRING PROPERTY COMPILE_FLAGS ${LIBUNWIND_PTRACE_CFLAGS_OTHER})
+pkg_check_modules(DLOG REQUIRED dlog)
+set_property(TARGET ${CRASH_STACK_BIN} APPEND_STRING PROPERTY COMPILE_FLAGS ${LIBUNWIND_PTRACE_CFLAGS_OTHER} ${DLOG_CFLAGS})
# Linking
-target_link_libraries(${CRASH_STACK_BIN} ${LIBUNWIND_PTRACE_LIBRARIES} ${EBL_LIBRARY} dw elf dl stdc++)
+target_link_libraries(${CRASH_STACK_BIN} ${DLOG_LIBRARIES} ${LIBUNWIND_PTRACE_LIBRARIES} ${EBL_LIBRARY} dw elf dl stdc++)
# Installing
install(TARGETS ${CRASH_STACK_BIN} DESTINATION libexec)
diff --git a/src/crash-stack/crash-stack.c b/src/crash-stack/crash-stack.c
index e0e0a56..653a064 100644
--- a/src/crash-stack/crash-stack.c
+++ b/src/crash-stack/crash-stack.c
@@ -50,6 +50,7 @@
#include <elfutils/version.h>
#include <elfutils/libdwfl.h>
+#include "shared/util.h"
#define BUF_SIZE (BUFSIZ)
#define HEXA 16
@@ -551,22 +552,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), "<unknown>");
fprintf(outputfile, "Executable File Path: %s\n", file_path);
- close(fd);
}
/**
diff --git a/src/shared/util.c b/src/shared/util.c
index f8982dc..7ed63cd 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 <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -592,6 +593,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 ddd90be..4471789 100644
--- a/src/shared/util.h
+++ b/src/shared/util.h
@@ -58,6 +58,10 @@ int validate_env_name(char *name, int len);
int validate_file_name(char *name, int len);
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);
/**
* @}
*/