Add timeout to run the crash-stack 41/188141/5
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 31 Aug 2018 12:04:49 +0000 (14:04 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Wed, 5 Sep 2018 07:32:11 +0000 (09:32 +0200)
In case the crash-stack hangs, the crash-manager will still be able to
create a raport.

Change-Id: I62b3022dc1aec6507821473619c73cf90337643f

src/crash-manager/crash-manager.c

index 3aa16d6..4c760bf 100644 (file)
@@ -74,6 +74,7 @@
 #define WAIT_FOR_OPT_TIMEOUT_SEC 60
 
 #define MINICOREDUMPER_TIMEOUT 12*60
+#define CRASH_STACK_TIMEOUT 12*60
 #define TEMP_MAPS_FILENAME   "maps.temp"
 
 enum {
@@ -741,10 +742,12 @@ static void save_so_info(const struct crash_info *cinfo)
        get_and_save_so_info(maps_path, so_info_path);
 }
 
+// These macros are used in execute_minicoredump() and execute_crash_stack()
+#define SNPRINTF_OR_EXIT_W(name, format, member) if (snprintf(name##_str, sizeof(name##_str), format, cinfo->member) < 0) goto out;
+#define SNPRINTF_OR_EXIT(name, format) SNPRINTF_OR_EXIT_W(name, format, name##_info)
+
 static int execute_minicoredump(struct crash_info *cinfo)
 {
-#define SNPRINTF_OR_EXIT(name, format) if (snprintf(name##_str, sizeof(name##_str), format, cinfo->name##_info) < 0) goto out;
-
        char *coredump_name = NULL;
        char *prstatus_fd_str = NULL;
        int ret = -1;
@@ -818,33 +821,56 @@ out:
        free(prstatus_fd_str);
 
        return ret;
-#undef SNPRINTF_OR_EXIT
 }
 
-static void execute_crash_stack(const struct crash_info *cinfo)
+static int execute_crash_stack(const struct crash_info *cinfo)
 {
-       int ret;
-       char command[PATH_MAX];
+       int ret = -1;
+       int fd = -1;
+
+       char pid_str[11], tid_str[11], sig_str[11], prstatus_fd_str[11];
+
+       SNPRINTF_OR_EXIT(pid, "%d")
+       SNPRINTF_OR_EXIT(tid, "%d")
+       SNPRINTF_OR_EXIT(sig, "%d")
+       SNPRINTF_OR_EXIT_W(prstatus_fd, "%d", prstatus_fd)
 
        /* Execute crash-stack */
-       ret = snprintf(command, sizeof(command),
-                       "%s --pid %d --tid %d --sig %d --prstatus_fd %d > %s",
+       char *args[] = {
                        CRASH_STACK_PATH,
-                       cinfo->pid_info,
-                       cinfo->tid_info,
-                       cinfo->sig_info,
-                       cinfo->prstatus_fd,
-                       cinfo->info_path);
+                       "--pid",
+                       pid_str,
+                       "--tid",
+                       tid_str,
+                       "--sig",
+                       sig_str,
+                       "--prstatus_fd",
+                       prstatus_fd_str,
+                       NULL
+       };
 
-       _D("    %s", command);
-       if (ret < 0) {
-               _E("Failed to snprintf for crash-stack command");
-               return;
+       _D("    %s %s %s %s %s %s %s %s %s",
+               args[0], args[1], args[2], args[3],
+               args[4], args[5], args[6], args[7], args[8]);
+
+       fd = open(cinfo->info_path, O_WRONLY | O_CREAT, 0600);
+       if (fd < 0) {
+               _E("open %s error: %m", cinfo->info_path);
+               goto out;
        }
 
-       system_command(command);
+       ret = run_command_write_fd_timeout(CRASH_STACK_PATH, args, NULL, fd, NULL, 0, CRASH_STACK_TIMEOUT);
+
+out:
+       if (fd >= 0)
+               close(fd);
+
+       return ret;
 }
 
+#undef SNPRINTF_OR_EXIT
+#undef SNPRINTF_OR_EXIT_W
+
 static int execute_crash_modules(struct crash_info *cinfo)
 {
        _D("Execute crash module: ");