Add ability to set report path in crash-manager.conf 19/185419/10
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Thu, 19 Jul 2018 07:42:54 +0000 (09:42 +0200)
committerKarol Lewandowski <k.lewandowsk@samsung.com>
Wed, 22 Aug 2018 05:51:37 +0000 (07:51 +0200)
To change default report path (/opt/usr/crash/dump/) modify the value in
crash-manager.conf:

    [CrashManager]
    ...
    CrashRootPath=/var/crash
    ...

Change-Id: I39b9bc178e989a1aad5727f5becdee74b1e1b13b

src/crash-manager/crash-manager.c
src/crash-manager/crash-manager.conf

index 2c351fe..2a102c5 100644 (file)
@@ -68,7 +68,9 @@
 #define APPID_MAX                   128
 #define PKGNAME_MAX                 128
 
-#define CRASH_CHECK_DISK_PATH "/opt/usr"
+#define CRASH_TEMP_SUBDIR    "/temp/"
+#define CRASH_PATH_SUBDIR    "/dump/"
+
 #define WAIT_FOR_OPT_TIMEOUT_SEC 60
 
 #define MINICOREDUMPER_TIMEOUT 12*60
@@ -93,6 +95,9 @@ static int system_keep_free;
 static int max_retention_sec;
 static int max_crash_dump;
 static bool allow_zip;
+static char* crash_root_path;
+static char* crash_crash_path;
+static char* crash_temp_path;
 
 /* Paths and variables */
 static struct crash_info {
@@ -205,22 +210,49 @@ out:
        return ret;
 }
 
-static void get_config(void)
+static int prepare_paths(void)
+{
+       int tmp_len;
+       tmp_len = strlen(crash_root_path) + strlen(CRASH_PATH_SUBDIR);
+       crash_crash_path = (char*)malloc(tmp_len + 1);
+       if (crash_crash_path == NULL) {
+               _E("Couldn't allocate memory for crash_crash_path: %m\n");
+               return 0;
+       }
+       snprintf(crash_crash_path, tmp_len, "%s%s", crash_root_path, CRASH_PATH_SUBDIR);
+
+       tmp_len = strlen(crash_root_path) + strlen(CRASH_TEMP_SUBDIR);
+       crash_temp_path = (char*)malloc(tmp_len + 1);
+       if (crash_temp_path == NULL) {
+               _E("Couldn't allocate memory for crash_temp_path: %m\n");
+               return 0;
+       }
+       snprintf(crash_temp_path, tmp_len, "%s%s", crash_root_path, CRASH_TEMP_SUBDIR);
+       return 1;
+}
+
+static int get_config(void)
 {
        dictionary *ini = NULL;
        char key[KEY_MAX];
        int value;
+       char *value_str;
 
        system_max_use = SYSTEM_MAX_USE;
        system_keep_free = SYSTEM_KEEP_FREE;
        max_retention_sec = MAX_RETENTION_SEC;
        max_crash_dump = MAX_CRASH_DUMP;
        allow_zip = ALLOW_ZIP;
+       crash_root_path = strdup(CRASH_ROOT_PATH);
+       if (crash_root_path == NULL) {
+               _E("strdup error: %m\n");
+               return -1;
+       }
 
        ini = iniparser_load(CRASH_CONF_FILE);
        if (!ini) {
-               _E("Failed to load conf file");
-               return;
+               _E("Failed to load conf file %s", CRASH_CONF_FILE);
+               return 0;
        }
 
        snprintf(key, sizeof(key), "%s:%s", CRASH_SECTION, "SystemMaxUse");
@@ -274,33 +306,49 @@ static void get_config(void)
                allow_zip = value;
        }
 
+       snprintf(key, sizeof(key), "%s:%s", CRASH_SECTION, "CrashRootPath");
+       value_str = iniparser_getstring(ini, key, NULL);
+       if (value_str == NULL) {
+               _D("Invalid value for CrashRootPath. Use default value [ %s ]",
+                               CRASH_ROOT_PATH);
+       } else {
+               _D("CrashRootPath [ %s ]", value_str);
+               free(crash_root_path);
+               crash_root_path = strdup(value_str);
+               if (crash_root_path == NULL) {
+                       _E("strdup error: %m\n");
+                       return -1;
+               }
+       }
+
        iniparser_freedict(ini);
+       return 1;
 }
 
 static int make_dump_dir(void)
 {
        struct stat st;
 
-       if (!stat(CRASH_PATH, &st)) {
+       if (!stat(crash_crash_path, &st)) {
                if (!(st.st_mode & S_IFDIR)) {
-                       _E("%s (not DIR) is already exist", CRASH_PATH);
+                       _E("%s (not DIR) is already exist", crash_crash_path);
                        return -1;
                }
        } else {
-               if (mkdir(CRASH_PATH, 0775) < 0) {
-                       _E("Failed to mkdir for %s", CRASH_PATH);
+               if (mkdir(crash_crash_path, 0775) < 0) {
+                       _E("Failed to mkdir for %s", crash_crash_path);
                        return -1;
                }
        }
 
-       if (!stat(CRASH_TEMP, &st)) {
+       if (!stat(crash_temp_path, &st)) {
                if (!(st.st_mode & S_IFDIR)) {
-                       _E("%s (not DIR) is already exist", CRASH_TEMP);
+                       _E("%s (not DIR) is already exist", crash_temp_path);
                        return -1;
                }
        } else {
-               if (mkdir(CRASH_TEMP, 0775) < 0) {
-                       _E("Failed to mkdir for %s", CRASH_TEMP);
+               if (mkdir(crash_temp_path, 0775) < 0) {
+                       _E("Failed to mkdir for %s", crash_temp_path);
                        return -1;
                }
        }
@@ -430,7 +478,7 @@ static int set_crash_info(int argc, char *argv[])
                        "%Y%m%d%H%M%S", &loc_tm);
 
        ret = snprintf(crash_info.temp_dir, sizeof(crash_info.temp_dir),
-                       "%s/crash.XXXXXX", CRASH_TEMP);
+                       "%s/crash.XXXXXX", crash_temp_path);
        if (ret < 0) {
                _E("Failed to snprintf for temp_dir");
                return -1;
@@ -453,11 +501,11 @@ static int set_crash_info(int argc, char *argv[])
        if (allow_zip)
                ret = snprintf(crash_info.result_path,
                                sizeof(crash_info.result_path),
-                               "%s/%s.zip", CRASH_PATH, crash_info.name);
+                               "%s/%s.zip", crash_crash_path, crash_info.name);
        else
                ret = snprintf(crash_info.result_path,
                                sizeof(crash_info.result_path),
-                               "%s/%s", CRASH_PATH, crash_info.name);
+                               "%s/%s", crash_crash_path, crash_info.name);
        if (ret < 0) {
                _E("Failed to snprintf for result path");
                goto rm_temp;
@@ -768,8 +816,8 @@ static int lock_dumpdir(void)
 {
        int fd;
 
-       if ((fd = open(CRASH_PATH, O_RDONLY | O_DIRECTORY)) < 0) {
-               _E("Failed to open %s", CRASH_PATH);
+       if ((fd = open(crash_crash_path, O_RDONLY | O_DIRECTORY)) < 0) {
+               _E("Failed to open %s", crash_crash_path);
                return -1;
        }
 
@@ -816,12 +864,12 @@ static int scan_dump(struct file_info **dump_list, size_t *usage)
        int i, scan_num, dump_num = 0;
        int fd;
 
-       if ((fd = open(CRASH_PATH, O_DIRECTORY)) < 0) {
-               _E("Failed to open %s", CRASH_PATH);
+       if ((fd = open(crash_crash_path, O_DIRECTORY)) < 0) {
+               _E("Failed to open %s", crash_crash_path);
                return -1;
        }
 
-       scan_num = scandir(CRASH_PATH, &scan_list, &dump_filter, NULL);
+       scan_num = scandir(crash_crash_path, &scan_list, &dump_filter, NULL);
        if (scan_num < 0) {
                close(fd);
                return -1;
@@ -841,7 +889,7 @@ static int scan_dump(struct file_info **dump_list, size_t *usage)
                }
 
                if (asprintf(&(temp_list[dump_num].path), "%s/%s",
-                                       CRASH_PATH, scan_list[i]->d_name) < 0) {
+                                       crash_crash_path, scan_list[i]->d_name) < 0) {
                        _E("Failed to asprintf");
                        continue;
                }
@@ -988,7 +1036,7 @@ static void clean_dump(void)
 
        /* Check disk free space to keep */
        if (system_keep_free &&
-                       check_disk_available(CRASH_CHECK_DISK_PATH,
+                       check_disk_available(crash_root_path,
                                             system_keep_free) < 0) {
                _I("Disk is not available! so set the maximum number of dump to 1");
                max_crash_dump = 1;
@@ -1053,14 +1101,14 @@ static int wait_for_opt(unsigned int timeout)
 {
        unsigned int count = 0;
 
-       while (check_disk_available(CRASH_ROOT_PATH, 0) < 0 && count < timeout) {
-               log_kmsg("crash-manager: path %s is not available\n", CRASH_ROOT_PATH);
+       while (check_disk_available(crash_root_path, 0) < 0 && count < timeout) {
+               log_kmsg("crash-manager: path %s is not available\n", crash_root_path);
                sleep(1);
                count++;
        }
        if (count >= timeout) {
                log_kmsg("crash-manager: timeout (%ds) while waiting for %s."
-                       "Probably /opt is not mounted.\n", timeout, CRASH_ROOT_PATH);
+                       "Probably /opt is not mounted.\n", timeout, crash_root_path);
                return 0;
        }
 
@@ -1074,6 +1122,7 @@ int main(int argc, char *argv[])
 #if defined(TIZEN_ENABLE_COREDUMP) || defined(TIZEN_ENABLE_MINICOREDUMP)
        int debug_mode = access(DEBUGMODE_PATH, F_OK) == 0;
 #endif
+       int res = 0;
 
        /*
         * prctl(PR_SET_DUMPABLE, 0) is not neccessary. Kernel runs the
@@ -1081,19 +1130,34 @@ int main(int argc, char *argv[])
         * value that prevents from running crash-manager recursively.
         */
 
-       if (!wait_for_opt(WAIT_FOR_OPT_TIMEOUT_SEC))
-               exit(EXIT_FAILURE);
-
        /* Get Configuration */
-       get_config();
+       if (get_config() < 0) {
+               res = EXIT_FAILURE;
+               goto exit;
+       }
+
+       /* Prepare paths */
+       if (!prepare_paths()) {
+               res = EXIT_FAILURE;
+               goto exit;
+       }
+
+       if (!wait_for_opt(WAIT_FOR_OPT_TIMEOUT_SEC)) {
+               res = EXIT_FAILURE;
+               goto exit;
+       }
 
        /* Create crash directories */
-       if (make_dump_dir() < 0)
-               exit(EXIT_FAILURE);
+       if (make_dump_dir() < 0) {
+               res = EXIT_FAILURE;
+               goto exit;
+       }
 
        /* Set crash info */
-       if (set_crash_info(argc, argv) < 0)
-               exit(EXIT_FAILURE);
+       if (set_crash_info(argc, argv) < 0) {
+               res = EXIT_FAILURE;
+               goto exit;
+       }
 
 #ifdef SYS_ASSERT
        /* Fetch callstack of sys-assert */
@@ -1140,6 +1204,11 @@ int main(int argc, char *argv[])
 
        send_notify(&notify_params);
 
+exit:
        close(crash_info.prstatus_fd);
-       return 0;
+       free(crash_temp_path);
+       free(crash_root_path);
+       free(crash_crash_path);
+
+       return res;
 }
index 9ddac78..08131cc 100644 (file)
@@ -4,3 +4,6 @@ SystemKeepFree=0
 MaxRetentionSec=0
 MaxCrashDump=0
 AllowZip=yes
+
+# Crash report path must exist for the reports to be created
+# CrashRootPath=/usr/opt/share/crash/