Fixed crash scenario, redid wrapper scanning
authorWilliam Douglas <william.douglas@linux.intel.com>
Fri, 4 Mar 2011 21:24:27 +0000 (13:24 -0800)
committerWilliam Douglas <william.douglas@linux.intel.com>
Fri, 4 Mar 2011 21:24:27 +0000 (13:24 -0800)
coredump.c

index c76e4e0..046eb3a 100644 (file)
@@ -82,9 +82,10 @@ static char *get_release(void) {
        return line;
 }
 
-char *set_wrapped_app(char *line)
+void set_wrapped_app(char *line)
 {
-       char *dline = NULL, *app = NULL, delim[] = " '";
+       char *dline = NULL, *app = NULL, *ret = NULL;
+       char delim[] = " '";
 
        dline = strdup(line);
 
@@ -96,13 +97,13 @@ char *set_wrapped_app(char *line)
                }
                app = strtok(NULL, delim);
        }
-
+       ret = find_executable(app);
        free(dline);
        /*
-        * This may return NULL if app isn't on the path
-        * but we aren't going to worry about that here.
+        * May have got NULL from find_executable if app
+        * isn't on the path but it doesn't matter as we
+        * don't change the filename in that case.
         */
-       return find_executable(app);
 }
 
 void get_package_info(char *appfile) {
@@ -116,52 +117,50 @@ void get_package_info(char *appfile) {
                package = strdup("Unknown");
        } else {
                file = popen(command, "r");
+               free(command);
                ret = getline(&line, &size, file);
                if ((!size) || (ret < 0)) {
-                       free(line);
-                       line = strdup("Unknown");
+                       package = strdup("Unknown");
+               } else {
+                       c = strchr(line, '\n');
+                       if (c) *c = 0;
+                       package = strdup(line);
                }
-               c = strchr(line, '\n');
-               if (c) *c = 0;
                pclose(file);
-               package = strdup(line);
        }
-       free(line);
-       free(command);
 
        if (asprintf(&command, "rpm -q --whatprovides %s --queryformat \"%%{NAME}\"", appfile) < 0) {
                component = strdup("Unknown");
        } else {
                file = popen(command, "r");
+               free(command);
                ret = getline(&line, &size, file);
                if ((!size) || (ret < 0)) {
-                       free(line);
-                       line = strdup("Unknown");
+                       component = strdup("Unknown");
+               } else {
+                       c = strchr(line, '\n');
+                       if (c) *c = 0;
+                       component = strdup(line);
                }
-               c = strchr(line, '\n');
-               if (c) *c = 0;
                pclose(file);
-               component = strdup(line);
        }
-       free(line);
-       free(command);
 
        if (asprintf(&command, "rpm -q --whatprovides %s --queryformat \"%%{ARCH}\"", appfile) < 0) {
                arch = strdup("Unknown");
        } else {
                file = popen(command, "r");
+               free(command);
                ret = getline(&line, &size, file);
                if ((!size) || (ret < 0)) {
-                       free(line);
-                       line = strdup("Unknown");
+                       arch = strdup("Unknown");
+               } else {
+                       c = strchr(line, '\n');
+                       if (c) *c = 0;
+                       arch = strdup(line);
                }
-               c = strchr(line, '\n');
-               if (c) *c = 0;
                pclose(file);
-               arch = strdup(line);
        }
        free(line);
-       free(command);
 }
 
 char *signame(int sig)
@@ -253,39 +252,20 @@ char *build_core_header(char *appfile, char *corefile) {
        return result;
 }
 
-char *extract_core(char *corefile)
+/*
+ * Scan core dump in case a wrapper was used
+ * to run the process and get the actual binary name
+ */
+void wrapper_scan(char *command)
 {
-       char *command = NULL, *c1 = NULL, *c2 = NULL, *line;
-       char *coredump = NULL;
-       char *appfile;
+       char *line = NULL;
        FILE *file;
 
-       coredump = find_coredump(corefile);
-       if (!coredump) {
-               free(coredump);
-               return NULL;
-       }
-       appfile = find_executable(coredump);
-       /* coredump no longer used, so free it as it was strdup'd */
-       free(coredump);
-       if (!appfile)
-               return NULL;
-
-       if (asprintf(&command, "LANG=C gdb --batch -f %s %s -x /var/lib/corewatcher/gdb.command 2> /dev/null", appfile, corefile) < 0) {
-               free(command);
-               return NULL;
-       }
-
-       /*
-        * Scan core dump in case meego-tablet-wrapper was used
-        * to run the process and get the actual app name
-        */
        file = popen(command, "r");
        while (!feof(file)) {
                size_t size = 0;
                int ret;
-
-               line = NULL;
+               free(line);
                ret = getline(&line, &size, file);
                if (!size)
                        break;
@@ -295,13 +275,37 @@ char *extract_core(char *corefile)
                if (strstr(line, "Core was generated by") &&
                    strstr(line, "--app")) {
                        /* attempt to update appfile */
-                       appfile = set_wrapped_app(line);
+                       set_wrapped_app(line);
                        break;
                }
        }
        free(line);
        pclose(file);
+}
+
+char *extract_core(char *corefile)
+{
+       char *command = NULL, *c1 = NULL, *c2 = NULL, *line;
+       char *coredump = NULL;
+       char *appfile;
+       FILE *file;
 
+       coredump = find_coredump(corefile);
+       if (!coredump)
+               return NULL;
+
+       appfile = find_executable(coredump);
+       /* coredump no longer used, so free it as it was strdup'd */
+       free(coredump);
+       if (!appfile)
+               return NULL;
+
+       if (asprintf(&command, "LANG=C gdb --batch -f %s %s -x /var/lib/corewatcher/gdb.command 2> /dev/null", appfile, corefile) < 0) {
+               free(command);
+               return NULL;
+       }
+
+       wrapper_scan(command);
        c1 = build_core_header(appfile, corefile);
 
        file = popen(command, "r");
@@ -310,6 +314,7 @@ char *extract_core(char *corefile)
                int ret;
 
                c2 = c1;
+               free(line);
                ret = getline(&line, &size, file);
                if (!size)
                        break;
@@ -329,10 +334,7 @@ char *extract_core(char *corefile)
                                continue;
                        free(c2);
                } else {
-                       free(c1);
-                       c1 = NULL;
-                       if (asprintf(&c1, "%s", line) < 0)
-                               continue;
+                       asprintf(&c1, "%s", line);
                }
        }
        free(line);