diff options
author | Karol Lewandowski <k.lewandowsk@samsung.com> | 2020-02-13 18:51:36 +0100 |
---|---|---|
committer | Karol Lewandowski <k.lewandowsk@samsung.com> | 2020-02-13 18:51:36 +0100 |
commit | ec37a73dd429b6eaae7fc29f26b221d11d29fa59 (patch) | |
tree | 6594593f36cd62eec5d7c71b5f2602c329bdcf6e | |
parent | dfb7ee3ab27349e9c34f8d5bf2b73085c0ceb715 (diff) | |
download | crash-worker-sandbox/klewandowski/set-crash-info-rewrite.tar.gz crash-worker-sandbox/klewandowski/set-crash-info-rewrite.tar.bz2 crash-worker-sandbox/klewandowski/set-crash-info-rewrite.zip |
wip: Rework set_crash_infosandbox/klewandowski/set-crash-info-rewrite
Fix info result path too
Change-Id: I67ee51716c86846a35ce316eceb9020b4a4d2323
-rw-r--r-- | src/crash-manager/crash-manager.c | 240 | ||||
-rw-r--r-- | src/crash-manager/crash-manager.h | 9 | ||||
-rw-r--r-- | src/crash-manager/main.c | 5 | ||||
-rw-r--r-- | src/crash-service/crash-service.c | 5 |
4 files changed, 111 insertions, 148 deletions
diff --git a/src/crash-manager/crash-manager.c b/src/crash-manager/crash-manager.c index 3e41145..0511b63 100644 --- a/src/crash-manager/crash-manager.c +++ b/src/crash-manager/crash-manager.c @@ -85,6 +85,11 @@ config_t config; static char* crash_dump_path; static char* crash_temp_path; +bool have_livecoredumper(void) +{ + return !!file_exists(LIVEDUMPER_BIN_PATH); +} + /* pkgmgrinfo filter list function for getting application ID */ static int appinfo_get_appid_func(pkgmgrinfo_appinfo_h handle, void *user_data) @@ -98,76 +103,73 @@ static int appinfo_get_appid_func(pkgmgrinfo_appinfo_h handle, return ret; } -/* get application ID by pkgmgrinfo filter */ -static int get_appid(char *exepath, char *appid, int len) +static bool get_appid(char *exepath, char **appid) { pkgmgrinfo_appinfo_filter_h handle = NULL; - int count, ret; - char *aid = NULL; + bool have_appid = false; - ret = pkgmgrinfo_appinfo_filter_create(&handle); - if (ret != PMINFO_R_OK) { - ret = -1; - goto out; - } + if (pkgmgrinfo_appinfo_filter_create(&handle) != PMINFO_R_OK) + return false; - ret = pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_EXEC, exepath); - if (ret != PMINFO_R_OK) { - ret = -1; + if (pkgmgrinfo_appinfo_filter_add_string(handle, PMINFO_APPINFO_PROP_APP_EXEC, exepath) != PMINFO_R_OK) goto out_free; - } - ret = pkgmgrinfo_appinfo_filter_count(handle, &count); - if (ret != PMINFO_R_OK) { - ret = -1; + int count = 0; + if (pkgmgrinfo_appinfo_filter_count(handle, &count) || count < 1) goto out_free; - } - if (count < 1) { - ret = -1; + char *aid = NULL; + if (pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, appinfo_get_appid_func, &aid) != PMINFO_R_OK) goto out_free; - } - ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(handle, appinfo_get_appid_func, &aid); - if (ret != PMINFO_R_OK) { - ret = -1; - goto out_free; - } - if (aid) { - snprintf(appid, len, "%s", aid); - ret = 0; - free(aid); - } + have_appid = aid != NULL; + if (have_appid) + *appid = aid; out_free: pkgmgrinfo_appinfo_filter_destroy(handle); -out: - return ret; + return have_appid; } -/* get package ID by appid */ -static int get_pkgid(char *appid, char *pkgid, int len) +static bool get_pkgid(char *appid, char **pkgid) { pkgmgrinfo_appinfo_h handle = NULL; - int ret; - char *pkid = NULL; - ret = pkgmgrinfo_appinfo_get_appinfo(appid, &handle); - if (ret != PMINFO_R_OK) { - ret = -1; - goto out; - } - ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkid); - if (ret != PMINFO_R_OK) { - ret = -1; + if (pkgmgrinfo_appinfo_get_appinfo(appid, &handle) != PMINFO_R_OK) + return false; + + bool have_pkgid = false; + char *p = NULL; + if (pkgmgrinfo_appinfo_get_pkgid(handle, &p) != PMINFO_R_OK) goto out_free; - } - snprintf(pkgid, len, "%s", pkid); + + have_pkgid = p != NULL; + if (have_pkgid) + *pkgid = p; out_free: pkgmgrinfo_appinfo_destroy_appinfo(handle); -out: - return ret; + return have_pkgid; +} + +static bool set_appinfo(char *exepath, char **appid, char **pkgid) +{ + char *a = NULL, *p = NULL; + if (get_appid(exepath, &a)) { + *appid = a; + + if (get_pkgid(exepath, &p)) + *pkgid = p; + else + *pkgid = strdup(a); + + return true; + } + + char *bn = basename(exepath); + *appid = strdup(bn); + *pkgid = strdup(bn); + return false; } static int prepare_paths(struct crash_info* cinfo) @@ -308,127 +310,75 @@ close_fd: return -1; } -bool set_crash_info(struct crash_info *cinfo) -{ - int ret; - char *temp_dir_ret = NULL; - char date[80]; - struct tm loc_tm; +static void set_crash_info_defaults(struct crash_info *cinfo) +{ if (cinfo->livedump) { - if (cinfo->kill) - cinfo->sig_info = 9; - else - cinfo->sig_info = 0; - } - - if (cinfo->livedump && !file_exists(LIVEDUMPER_BIN_PATH)) { - fprintf(stderr, "Error: %s doesn't exist - can not perform livedump. Terminating.\n", LIVEDUMPER_BIN_PATH); - _E("Error: %s doesn't exist - can not perform livedump. Terminating.\n", LIVEDUMPER_BIN_PATH); - return false; - } + cinfo->sig_info = cinfo->kill ? 9 : 0; + } else if (cinfo->tid_info == -1) + cinfo->tid_info = find_crash_tid(cinfo->pid_info); if (cinfo->tid_info == -1) { - if (cinfo->livedump) { - cinfo->tid_info = cinfo->pid_info; - } else { - cinfo->tid_info = find_crash_tid(cinfo->pid_info); - if (cinfo->tid_info < 0) { - _W("TID not found. Assuming TID = PID."); - cinfo->tid_info = cinfo->pid_info; - } - } + _W("TID not known. Assuming TID = PID."); + cinfo->tid_info = cinfo->pid_info; } +} + +bool set_crash_info(struct crash_info *cinfo) +{ + set_crash_info_defaults(cinfo); if (!get_cmd_info(cinfo)) { _E("Failed to get command info"); return false; } - if (cinfo->time_info == 0) - cinfo->time_info = time(NULL); - + char date[80]; + struct tm loc_tm; + cinfo->time_info = time(NULL); localtime_r(&cinfo->time_info, &loc_tm); strftime(date, sizeof(date), "%Y%m%d%H%M%S", &loc_tm); - if (asprintf(&cinfo->temp_dir, "%s/crash.XXXXXX", crash_temp_path) == -1) { - _E("Failed to asprintf for temp_dir"); - cinfo->temp_dir = NULL; - return false; - } + bool is_fullreport = config.report_type >= REP_TYPE_FULL; + const char *suffix = is_fullreport ? (config.allow_zip ? ".zip" : "") : ".info"; - temp_dir_ret = mkdtemp(cinfo->temp_dir); - if (!temp_dir_ret || access(temp_dir_ret, F_OK)) { - _E("Failed to mkdtemp for temp_dir"); + char *appid, *pkgid; + bool is_app = set_appinfo(cinfo->cmd_line, &appid, &pkgid); + if (!appid || !pkgid) + goto out_oom; + + if (asprintf(&cinfo->temp_dir, "%s/crash.XXXXXX", crash_temp_path) == -1 + || asprintf(&cinfo->name, "%s_%d_%s", is_app ? cinfo->pkgid : basename(cinfo->cmd_path), cinfo->pid_info, date) == -1 + || asprintf(&cinfo->pfx, "%s/%s", cinfo->temp_dir, cinfo->name) == -1 + || asprintf(&cinfo->info_path, "%s/%s.info", cinfo->pfx, cinfo->name) == -1 + || asprintf(&cinfo->core_path, "%s/%s.coredump", cinfo->pfx, cinfo->name) == -1 + || asprintf(&cinfo->log_path, "%s/%s.log", cinfo->pfx, cinfo->name) == -1 + || asprintf(&cinfo->zip_path, "%s/report.zip", cinfo->temp_dir) == -1 + || asprintf(&cinfo->result_path, "%s/%s%s", crash_dump_path, cinfo->name, suffix) == -1) + goto out_oom; + + if (mkdtemp(cinfo->temp_dir) == NULL || access(cinfo->temp_dir, F_OK)) { + _E("Failed to create temporary directory %s: %m", cinfo->temp_dir); return false; } - 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 (mkdir(cinfo->pfx, 0775) < 0) { + _E("Failed to mkdir %s: %m", cinfo->pfx); + goto out_rm_temp; } - 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 (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); - if (ret < 0) { - _E("Failed to mkdir for %s", cinfo->pfx); - goto rm_temp; - } - - 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; - } - - 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; - } - - 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; - } - - if (asprintf(&cinfo->zip_path, "%s/report.zip", cinfo->temp_dir) == -1) { - _E("Out of memory"); - goto rm_temp; - } - - 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; + goto out_rm_temp; return true; -rm_temp: +out_rm_temp: remove_dir(cinfo->temp_dir, 1); return false; + +out_oom: + _E("Out of memory"); + return false; } static void launch_crash_popup(struct crash_info *cinfo) @@ -1032,6 +982,8 @@ static void free_crash_info(struct crash_info *cinfo) free(cinfo->core_path); free(cinfo->log_path); free(cinfo->zip_path); + free(cinfo->appid); + free(cinfo->pkgid); } void crash_info_init(struct crash_info *cinfo) @@ -1053,6 +1005,8 @@ void crash_info_init(struct crash_info *cinfo) cinfo->core_path = NULL; cinfo->log_path = NULL; cinfo->zip_path = NULL; + cinfo->appid = NULL; + cinfo->pkgid = NULL; } static bool run(struct crash_info *cinfo) diff --git a/src/crash-manager/crash-manager.h b/src/crash-manager/crash-manager.h index c2cea87..dc9218a 100644 --- a/src/crash-manager/crash-manager.h +++ b/src/crash-manager/crash-manager.h @@ -23,9 +23,6 @@ #include <unistd.h> #include <stdbool.h> -#define APPID_MAX 128 -#define PKGNAME_MAX 128 - /* Paths and variables */ struct crash_info { bool livedump; @@ -49,8 +46,8 @@ struct crash_info { char *log_path; char *zip_path; char *output_path; - char appid[APPID_MAX]; - char pkgid[PKGNAME_MAX]; + char *appid; + char *pkgid; time_t time_info; }; @@ -58,4 +55,6 @@ bool crash_manager_direct(struct crash_info *cinfo); bool crash_manager_livedump_pid(pid_t pid, const char *dump_reason, char *report_path, size_t report_path_len); void crash_info_init(struct crash_info *cinfo); void crash_manager_free(struct crash_info *cinfo); + +bool have_livecoredumper(void); #endif diff --git a/src/crash-manager/main.c b/src/crash-manager/main.c index 2cacc2b..9aa8d5e 100644 --- a/src/crash-manager/main.c +++ b/src/crash-manager/main.c @@ -117,6 +117,11 @@ static bool parse_args(struct crash_info *cinfo, int argc, char *argv[]) } } + if (cinfo->livedump && !have_livecoredumper()) { + fprintf(stderr, "Error: livecoredumper not available - can not perform livedump. Terminating.\n"); + return false; + } + if (!pid_set || (!cinfo->livedump && (!gid_set || !uid_set || !sig_set))) { printf("Not enough parameters.\n\n"); print_help(argv[0]); diff --git a/src/crash-service/crash-service.c b/src/crash-service/crash-service.c index 7b0a798..0917fcb 100644 --- a/src/crash-service/crash-service.c +++ b/src/crash-service/crash-service.c @@ -315,6 +315,11 @@ static bool dbus_init(void) int main(void) { + if (!have_livecoredumper()) { + _E("livecoredumper not available - can not provide livedump API. Terminating.\n"); + return EXIT_FAILURE; + } + loop = g_main_loop_new(NULL, false); if (!dbus_init()) { |