From 512faff745cd45df3c68fd232f206ffb96328d43 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Wed, 12 Feb 2020 16:00:15 +0100 Subject: [PATCH] Use package id as prefix for report files Change-Id: I37ea45e2c0a262e9bcd2bb3bcdb97edba83cbc7d --- src/crash-manager/crash-manager.c | 89 +++++++++++++++------------------------ src/crash-manager/crash-manager.h | 1 + 2 files changed, 34 insertions(+), 56 deletions(-) diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c index 33f1820..29337b7 100644 --- a/src/crash-manager/crash-manager.c +++ b/src/crash-manager/crash-manager.c @@ -363,25 +363,23 @@ bool set_crash_info(struct crash_info *cinfo) return false; } - if (asprintf(&cinfo->name, "%s_%d_%s", basename(cinfo->cmd_path), + bool is_app = false; + if (get_appid(cinfo->cmd_line, cinfo->appid, sizeof(cinfo->appid)) < 0) { + snprintf(cinfo->appid, sizeof(cinfo->appid), "%s", basename(cinfo->cmd_line)); + snprintf(cinfo->pkgid, sizeof(cinfo->pkgid), "%s", basename(cinfo->cmd_line)); + } else { + is_app = true; + if (get_pkgid(cinfo->appid, cinfo->pkgid, sizeof(cinfo->pkgid)) < 0) + snprintf(cinfo->pkgid, sizeof(cinfo->pkgid), "%s", cinfo->appid); + } + + if (asprintf(&cinfo->name, "%s_%d_%s", is_app ? cinfo->pkgid : basename(cinfo->cmd_path), cinfo->pid_info, date) == -1) { _E("Failed to snprintf for name"); cinfo->name = NULL; goto rm_temp; } - if (config.allow_zip) - ret = asprintf(&cinfo->result_path, - "%s/%s.zip", crash_dump_path, cinfo->name); - else - ret = asprintf(&cinfo->result_path, - "%s/%s", crash_dump_path, cinfo->name); - if (ret == -1) { - _E("Failed to asprintf for result path"); - cinfo->result_path = NULL; - goto rm_temp; - } - if (asprintf(&cinfo->pfx, "%s/%s", cinfo->temp_dir, cinfo->name) == -1) { _E("Failed to asprintf for pfx"); cinfo->pfx = NULL; @@ -411,17 +409,21 @@ bool set_crash_info(struct crash_info *cinfo) goto rm_temp; } - if (set_prstatus(cinfo) < 0) + if (asprintf(&cinfo->zip_path, "%s/report.zip", cinfo->temp_dir) == -1) { + _E("Out of memory"); goto rm_temp; - - if (get_appid(cinfo->cmd_line, cinfo->appid, sizeof(cinfo->appid)) < 0) { - snprintf(cinfo->appid, sizeof(cinfo->appid), "%s", basename(cinfo->cmd_line)); - snprintf(cinfo->pkgid, sizeof(cinfo->pkgid), "%s", basename(cinfo->cmd_line)); - } else { - if (get_pkgid(cinfo->appid, cinfo->pkgid, sizeof(cinfo->pkgid)) < 0) - snprintf(cinfo->pkgid, sizeof(cinfo->pkgid), "%s", cinfo->appid); } + bool is_fullreport = config.report_type >= REP_TYPE_FULL; + const char *suffix = is_fullreport ? (config.allow_zip ? ".zip" : "") : ".info"; + + ret = asprintf(&cinfo->result_path, "%s/%s%s", crash_dump_path, cinfo->name, suffix); + if (ret == -1) + goto rm_temp; + + if (set_prstatus(cinfo) < 0) + goto rm_temp; + return true; rm_temp: @@ -978,31 +980,10 @@ static void clean_dump(void) static void compress(struct crash_info *cinfo) { - int ret, lock_fd; - char zip_path[PATH_MAX]; - - ret = snprintf(zip_path, sizeof(zip_path), "%s/report.zip", - cinfo->temp_dir); - if (ret < 0) { - _E("Failed to snprintf for zip path"); - return; - } - - char *args[] = {"/bin/zip", "-qyr", zip_path, cinfo->name, NULL}; + char *args[] = {"/bin/zip", "-qyr", cinfo->zip_path, cinfo->name, NULL}; spawn_param_s param1 = { .fn = spawn_nullstdfds }; spawn_param_s param0 = { .fn = spawn_chdir, .u.char_ptr = cinfo->temp_dir, .next = ¶m1 }; (void)spawn_wait(args, NULL, ¶m0, ZIP_TIMEOUT_MS, NULL); - - if ((lock_fd = lock_dumpdir()) < 0) - return; - if (!rename(zip_path, cinfo->result_path)) - clean_dump(); - else - _E("Failed to move %s to %s", zip_path, cinfo->result_path); - unlock_dumpdir(lock_fd); - - if (remove_dir(cinfo->temp_dir, 1) < 0) - _E("Failed to delete temp directory"); } static void move_dump_data(const char *from_path, const struct crash_info *cinfo) @@ -1050,6 +1031,7 @@ static void free_crash_info(struct crash_info *cinfo) free(cinfo->info_path); free(cinfo->core_path); free(cinfo->log_path); + free(cinfo->zip_path); } void crash_info_init(struct crash_info *cinfo) @@ -1069,6 +1051,7 @@ void crash_info_init(struct crash_info *cinfo) cinfo->info_path = NULL; cinfo->core_path = NULL; cinfo->log_path = NULL; + cinfo->zip_path = NULL; } static bool run(struct crash_info *cinfo) @@ -1094,6 +1077,7 @@ static bool run(struct crash_info *cinfo) return false; } + char *temp_report; if (config.report_type >= REP_TYPE_FULL) { /* Save shared objects info (file names, bulid IDs, rpm package names) */ save_so_info(cinfo); @@ -1103,21 +1087,14 @@ static bool run(struct crash_info *cinfo) if (extra_script_pid > 0) wait_for_pid(extra_script_pid, NULL); - /* Tar compression */ if (config.allow_zip) compress(cinfo); - else - move_dump_data(cinfo->pfx, cinfo); - } else { - free(cinfo->result_path); - if (asprintf(&cinfo->result_path, "%s/%s.info", - crash_dump_path, cinfo->name) == -1) { - cinfo->result_path = NULL; - _E("asprintf() error: %m"); - return false; - } - move_dump_data(cinfo->info_path, cinfo); - } + + temp_report = config.allow_zip ? cinfo->zip_path : cinfo->pfx; + } else + temp_report = cinfo->info_path; + + move_dump_data(temp_report, cinfo); if (cinfo->print_result_path) printf("REPORT_PATH=%s\n", cinfo->result_path); diff --git a/src/crash-manager/crash-manager.h b/src/crash-manager/crash-manager.h index 1e4dbe5..275fb5a 100644 --- a/src/crash-manager/crash-manager.h +++ b/src/crash-manager/crash-manager.h @@ -46,6 +46,7 @@ struct crash_info { char *info_path; char *core_path; char *log_path; + char *zip_path; char *output_path; char appid[APPID_MAX]; char pkgid[PKGNAME_MAX]; -- 2.7.4