shared: Simplify get_exe_path 21/211721/2
authorKarol Lewandowski <k.lewandowsk@samsung.com>
Thu, 8 Aug 2019 08:49:27 +0000 (10:49 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Fri, 9 Aug 2019 11:55:59 +0000 (11:55 +0000)
Change-Id: I26839430a4836454f96c1f273a1500899dc6b607

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

index fedd35f..b01253c 100644 (file)
@@ -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);
index c0f9604..0445d9c 100644 (file)
@@ -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("<unknown>");
-
-       fprintf(outputfile, "Executable File Path: %s\n", file_path);
-
-       free(file_path);
+       fprintf(outputfile, "Executable File Path: %s\n", has_path ? path : "<unknown>");
 }
 
 /**
index 84fa25e..1860bab 100644 (file)
@@ -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
index 0f81ca7..f3177e8 100644 (file)
@@ -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);