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);
*/
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>");
}
/**
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
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);