Use inotify to watch the coredump directory
authorArjan van de Ven <arjan@linux.intel.com>
Sat, 17 Jan 2009 14:36:42 +0000 (16:36 +0200)
committerArjan van de Ven <arjan@linux.intel.com>
Sat, 17 Jan 2009 14:36:42 +0000 (16:36 +0200)
Makefile
extract_core.c

index b44bfe8..4b67301 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
 all: extract_core
 
-CFLAGS = -O0 -g -Wall -W -D_FORTIFY_SOURCE=2 -fstack-protector
+CFLAGS = -O2 -g -Wall -W -D_FORTIFY_SOURCE=2 -fstack-protector
 
-extract_core: extract_core.o find_file.o coredumper.h
+extract_core: extract_core.o find_file.o coredumper.h Makefile
        gcc $(CFLAGS) extract_core.o find_file.o -o extract_core
        
 clean:
index 42ade99..d29382d 100644 (file)
@@ -3,12 +3,19 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
+
+#include <sys/inotify.h>
+
 
 #include "coredumper.h"
 
+int inotifd, inotify_descriptor;
+
+
 char *extract_core(char *corefile)
 {
-       char *command = NULL, *c1 = NULL, *c2 = NULL, *line;
+       char *command = NULL, *c1 = NULL, *c2 = NULL, *line, *c3;
        char *appfile;
        FILE *file;
 
@@ -16,7 +23,10 @@ char *extract_core(char *corefile)
        if (!appfile)
                return NULL;
 
-       if (asprintf(&command, "gdb --batch -f %s %s -x gdb.command 2> /dev/null", appfile, corefile) < 0)
+       if (asprintf(&c1, "Program: %s\n", appfile) < 0)
+               return NULL;
+
+       if (asprintf(&command, "LANG=C gdb --batch -f %s %s -x gdb.command 2> /dev/null", appfile, corefile) < 0)
                return NULL;
                
        file = popen(command, "r");
@@ -36,6 +46,15 @@ char *extract_core(char *corefile)
                        free(line);
                        continue;
                }
+               if (strstr(line, "Core was generated by `")) {
+                       free(line);
+                       continue;
+               }
+               if (strstr(line,              "Program terminated with signal")) {
+                       c3 = strchr(line, ',');
+                       if (c3)
+                               sprintf(line, "Type:%s", c3+1);
+               }
 
                if (c1) {
                        c1 = NULL;
@@ -54,12 +73,50 @@ char *extract_core(char *corefile)
        return c1;
 }
 
+void process_corefile(char *filename)
+{
+       char *ptr;
+       ptr = extract_core(filename);
+
+       if (!ptr)
+               return;
+
+       printf("-%s-\n", ptr);
+
+       free(ptr);
+}
+
+void wait_for_corefile(void)
+{
+       char buffer[8192];
+       char fullpath[8192];
+       struct inotify_event *event = (struct inotify_event *)&buffer;
+
+       while (1) {
+               if (read(inotifd, &buffer, 8192) < 0)
+                       return;
+               sprintf(fullpath,"/var/cores/%s", event->name);
+               process_corefile(fullpath);
+       }
+
+}
+
 int main(int argc, char **argv)
 {
-       if (argc <= 1) {
-               printf("Usage:\n\textract_core <core file>\n");
+       inotifd = inotify_init();
+       if (inotifd < 0) {
+               printf("No inotify support in the kernel... aborting\n");
                return EXIT_FAILURE;
        }
-       printf("%s\n", extract_core(argv[1]));
+       inotify_descriptor = inotify_add_watch(inotifd, "/var/cores/", IN_CLOSE_WRITE);
+
+       if (argc > 1)
+               process_corefile(argv[1]);
+       else
+               wait_for_corefile();
+
+
+       inotify_rm_watch(inotifd, inotify_descriptor);
+       close(inotifd);
        return EXIT_SUCCESS;
 }
\ No newline at end of file