int uid_info;
int gid_info;
int sig_info;
- char cmd_line[PATH_MAX];
- char cmd_path[PATH_MAX];
+ char *cmd_line;
+ char *cmd_path;
time_t time_info;
- char temp_dir[PATH_MAX];
- char name[FILENAME_MAX];
- char result_path[PATH_MAX];
- char pfx[PATH_MAX];
- char info_path[PATH_MAX];
- char core_path[PATH_MAX];
- char log_path[PATH_MAX];
+ char *temp_dir;
+ char *name;
+ char *result_path;
+ char *pfx;
+ char *info_path;
+ char *core_path;
+ char *log_path;
char appid[APPID_MAX];
char pkgid[PKGNAME_MAX];
#ifdef SYS_ASSERT
- char sysassert_cs_path[PATH_MAX];
+ char *sysassert_cs_path;
bool have_sysassert_report;
#endif
int prstatus_fd;
static int get_cmd_info(struct crash_info *cinfo)
{
- 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));
+ cinfo->cmd_line = get_cmd_line(cinfo->pid_info);
+ cinfo->cmd_path = get_exe_path(cinfo->pid_info);
+
+ return (cinfo->cmd_line != NULL && cinfo->cmd_path != NULL);
}
static int set_prstatus(struct crash_info *cinfo)
localtime_r(&cinfo->time_info, &loc_tm);
strftime(date, sizeof(date), "%Y%m%d%H%M%S", &loc_tm);
- ret = snprintf(cinfo->temp_dir, sizeof(cinfo->temp_dir),
- "%s/crash.XXXXXX", crash_temp_path);
- if (ret < 0) {
- _E("Failed to snprintf for temp_dir");
+ if (asprintf(&cinfo->temp_dir, "%s/crash.XXXXXX", crash_temp_path) == -1) {
+ _E("Failed to asprintf for temp_dir");
+ cinfo->temp_dir = NULL;
return -1;
}
+
temp_dir_ret = mkdtemp(cinfo->temp_dir);
if (!temp_dir_ret || access(temp_dir_ret, F_OK)) {
_E("Failed to mkdtemp for temp_dir");
return -1;
}
- ret = snprintf(cinfo->name, sizeof(cinfo->name), "%s_%d_%s",
- basename(cinfo->cmd_line),
- cinfo->pid_info,
- date);
- if (ret < 0) {
+ if (asprintf(&cinfo->name, "%s_%d_%s", basename(cinfo->cmd_line),
+ cinfo->pid_info, date) == -1) {
_E("Failed to snprintf for name");
+ cinfo->name = NULL;
goto rm_temp;
}
if (allow_zip)
- ret = snprintf(cinfo->result_path,
- sizeof(cinfo->result_path),
+ ret = asprintf(&cinfo->result_path,
"%s/%s.zip", crash_crash_path, cinfo->name);
else
- ret = snprintf(cinfo->result_path,
- sizeof(cinfo->result_path),
+ ret = asprintf(&cinfo->result_path,
"%s/%s", crash_crash_path, cinfo->name);
- if (ret < 0) {
- _E("Failed to snprintf for result path");
+ if (ret == -1) {
+ _E("Failed to asprintf for result path");
+ cinfo->result_path = NULL;
goto rm_temp;
}
- ret = snprintf(cinfo->pfx, sizeof(cinfo->pfx), "%s/%s",
- cinfo->temp_dir, cinfo->name);
- if (ret < 0) {
- _E("Failed to snprintf for pfx");
+ if (asprintf(&cinfo->pfx, "%s/%s", cinfo->temp_dir, cinfo->name) == -1) {
+ _E("Failed to asprintf for pfx");
+ cinfo->pfx = NULL;
goto rm_temp;
}
ret = mkdir(cinfo->pfx, 0775);
goto rm_temp;
}
- ret = snprintf(cinfo->info_path, sizeof(cinfo->info_path),
- "%s/%s.info", cinfo->pfx, cinfo->name);
- if (ret < 0) {
- _E("Failed to snprintf for info path");
+ if (asprintf(&cinfo->info_path, "%s/%s.info", cinfo->pfx, cinfo->name) == -1) {
+ _E("Failed to asprintf for info path");
+ cinfo->info_path = NULL;
goto rm_temp;
}
- ret = snprintf(cinfo->core_path, sizeof(cinfo->core_path),
- "%s/%s.coredump", cinfo->pfx, cinfo->name);
- if (ret < 0) {
- _E("Failed to snprintf for core path");
+ if (asprintf(&cinfo->core_path, "%s/%s.coredump", cinfo->pfx, cinfo->name) == -1) {
+ _E("Failed to asprintf for core path");
+ cinfo->core_path = NULL;
goto rm_temp;
}
- ret = snprintf(cinfo->log_path, sizeof(cinfo->log_path),
- "%s/%s.log", cinfo->pfx, cinfo->name);
- if (ret < 0) {
- _E("Failed to snprintf for log path");
+ if (asprintf(&cinfo->log_path, "%s/%s.log", cinfo->pfx, cinfo->name) == -1) {
+ _E("Failed to asprintf for log path");
+ cinfo->log_path = NULL;
goto rm_temp;
}
#ifdef SYS_ASSERT
- ret = snprintf(cinfo->sysassert_cs_path,
- sizeof(cinfo->sysassert_cs_path),
- "/tmp/crash_stack/%s_%d.info",
- basename(cinfo->cmd_line), cinfo->pid_info);
- if (ret < 0) {
+ if (asprintf(&cinfo->sysassert_cs_path, "/tmp/crash_stack/%s_%d.info",
+ basename(cinfo->cmd_line), cinfo->pid_info) == -1) {
_E("Failed to snprintf for sys-assert callstack path");
+ cinfo->sysassert_cs_path = NULL;
goto rm_temp;
}
#endif
return 1;
}
+static void free_crash_info(struct crash_info *cinfo)
+{
+ free(cinfo->cmd_line);
+ free(cinfo->cmd_path);
+ free(cinfo->temp_dir);
+ free(cinfo->result_path);
+ free(cinfo->pfx);
+ free(cinfo->info_path);
+ free(cinfo->core_path);
+ free(cinfo->log_path);
+
+#ifdef SYS_ASSERT
+ free(cinfo->sysassert_cs_path);
+#endif
+}
+
int main(int argc, char *argv[])
{
- struct crash_info cinfo;
+ struct crash_info cinfo = {0};
/* Execute dump_systemstate in parallel */
static int dump_state_pid;
else
move_dump_data(cinfo.pfx, &cinfo);
} else {
- snprintf(cinfo.result_path, sizeof(cinfo.result_path), "%s/%s.info",
- crash_crash_path, cinfo.name);
+ free(cinfo.result_path);
+ if (asprintf(&cinfo.result_path, "%s/%s.info",
+ crash_crash_path, cinfo.name) == -1) {
+ cinfo.result_path = NULL;
+ _E("asprintf() error: %m");
+ res = EXIT_FAILURE;
+ goto exit;
+ }
move_dump_data(cinfo.info_path, &cinfo);
}
/* launch crash-popup only if the .debugmode file is exist*/
free(crash_root_path);
free(crash_crash_path);
+ free_crash_info(&cinfo);
+
return res;
}
return -1;
}
-int get_cmd_line(pid_t pid, char *cmd, size_t len)
+char* get_cmd_line(pid_t pid)
{
char cmdline_path[PATH_MAX];
int fd = open(cmdline_path, O_RDONLY);
if (fd < 0) {
- _E("Failed to open %s: %m", cmdline_path);
- return 0;
+ _E("Failed to open %s: %m\n", cmdline_path);
+ return NULL;
}
- ssize_t ret = read(fd, cmd, len - 1);
+ char buffer[PATH_MAX];
+ ssize_t ret = read(fd, buffer, sizeof(buffer) - 1);
+ buffer[ret] = '\0';
close(fd);
+
if (ret <= 0) {
- _E("Failed to read %s: %m", cmdline_path);
- return 0;
+ _E("Failed to read %s: %m\n", cmdline_path);
+ return NULL;
}
- cmd[ret] = '\0';
+ char *result;
+ if (asprintf(&result, "%s", buffer) == -1) {
+ _E("asprintf() error: %m\n");
+ return NULL;
+ }
- return 1;
+ return result;
}
-int get_exe_path(pid_t pid, char *path, size_t len)
+char* get_exe_path(pid_t pid)
{
char exe_link[PATH_MAX];
+ char buffer[PATH_MAX];
snprintf(exe_link, sizeof(exe_link),
"/proc/%d/exe", pid);
- ssize_t ret = readlink(exe_link, path, len - 1);
+ ssize_t ret = readlink(exe_link, buffer, sizeof(buffer) - 1);
+ buffer[ret] = '\0';
if (ret <= 0) {
_E("Failed to read link %s: %m", exe_link);
- return 0;
+ return NULL;
}
- path[ret] = '\0';
+ if (access(buffer, F_OK) == -1) {
+ _E("Invalid path %s", buffer);
+ return NULL;
+ }
- if (access(path, F_OK) == -1) {
- _E("Invalid path %s", path);
- return 0;
+ char *result;
+ if (asprintf(&result, "%s", buffer) == -1) {
+ _E("asprintf() error: %m\n");
+ return NULL;
}
- return 1;
+ return result;
}
/**
* @}