Move the function to read /proc/<pid>/exe symlink to util.c 18/187918/2
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Tue, 28 Aug 2018 13:27:30 +0000 (15:27 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 29 Aug 2018 11:29:17 +0000 (13:29 +0200)
Change-Id: Ieb533fb66dd3d06eb5bbc827aa016a308cf892a3

src/crash-manager/crash-manager.c
src/crash-stack/CMakeLists.txt
src/crash-stack/crash-stack.c
src/shared/util.c
src/shared/util.h

index 3f3e089..53b0175 100644 (file)
@@ -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()
index ddeba3a..fd26fac 100644 (file)
@@ -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")
index 5979c2f..2f65b7c 100644 (file)
@@ -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), "<unknown>");
 
        fprintf(outputfile, "Executable File Path: %s\n", file_path);
-       close(fd);
 }
 
 /**
index 3795415..2eb4399 100644 (file)
@@ -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>
@@ -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;
+}
 /**
  * @}
  */
index 301b4e6..0cb35f2 100644 (file)
@@ -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);
 /**
  * @}
  */