#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;
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");
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;
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