Add tool to convert info.json report 01/237601/6
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 26 Jun 2020 13:29:40 +0000 (15:29 +0200)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 10 Jul 2020 07:02:00 +0000 (09:02 +0200)
Change-Id: I4c10ef5880ae919c8b4bf7a8167344db6f8ed2c7

CMakeLists.txt
packaging/crash-worker.spec
src/crash-json2info/CMakeLists.txt [new file with mode: 0644]
src/crash-json2info/crash-json2info.c [new file with mode: 0644]

index e7579d2..c8b6ede 100644 (file)
@@ -12,6 +12,7 @@ INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/include ${CMAKE_SOURCE_DIR}/src/crash-se
 ADD_SUBDIRECTORY(src/crash-manager)
 ADD_SUBDIRECTORY(src/crash-stack)
 ADD_SUBDIRECTORY(src/dump_systemstate)
+ADD_SUBDIRECTORY(src/crash-json2info)
 
 IF("${LIVEDUMPER}" STREQUAL "ON")
        ADD_SUBDIRECTORY(src/livedumper)
index f5c3c58..f111ea2 100644 (file)
@@ -181,6 +181,7 @@ mkdir -p %{buildroot}%{crash_temp}
 %attr(-,root,root) %{_prefix}/lib/sysctl.d/70-crash-manager.conf
 %attr(0750,crash_worker,crash_worker) %{_bindir}/crash-manager
 %attr(0750,crash_worker,crash_worker) %{_bindir}/dump_systemstate
+%{_bindir}/crash-json2info
 %{_sysconfdir}/dump_systemstate.conf.d/files/*.conf*
 %{_sysconfdir}/dump_systemstate.conf.d/programs/*.conf*
 %{_libexecdir}/crash-stack
diff --git a/src/crash-json2info/CMakeLists.txt b/src/crash-json2info/CMakeLists.txt
new file mode 100644 (file)
index 0000000..c040248
--- /dev/null
@@ -0,0 +1,23 @@
+cmake_minimum_required(VERSION 2.8.11)
+
+set(CRASH_JSON_TO_INFO_BIN "crash-json2info")
+
+INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src)
+set(CRASH_JSON_TO_INFO_SRCS crash-json2info.c ${CMAKE_SOURCE_DIR}/src/crash-stack/report_info.c)
+
+set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -fPIE")
+set(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed -pie")
+
+# Binary
+add_executable(${CRASH_JSON_TO_INFO_BIN} ${CRASH_JSON_TO_INFO_SRCS})
+
+include(FindPkgConfig)
+pkg_check_modules(JSON REQUIRED json-c)
+pkg_check_modules(DLOG REQUIRED dlog)
+set_property(TARGET ${CRASH_JSON_TO_INFO_BIN} APPEND_STRING PROPERTY COMPILE_FLAGS "${JSON_CFLAGS} ${DLOG_CFLAGS}")
+
+# Linking
+target_link_libraries(${CRASH_JSON_TO_INFO_BIN} ${JSON_LIBRARIES} ${DLOG_LIBRARIES} stdc++)
+
+# Installing
+install(TARGETS ${CRASH_JSON_TO_INFO_BIN} DESTINATION bin)
diff --git a/src/crash-json2info/crash-json2info.c b/src/crash-json2info/crash-json2info.c
new file mode 100644 (file)
index 0000000..213a316
--- /dev/null
@@ -0,0 +1,122 @@
+#include <assert.h>
+#include <json-c/json.h>
+#include <getopt.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "crash-stack/report_info.h"
+
+#define OPT_OUTPUT_FILE 1
+#define OPT_HELP        0
+
+const struct option opts[] = {
+       { "help", no_argument, 0, OPT_HELP },
+       { "output", required_argument, 0, OPT_OUTPUT_FILE },
+       { 0, 0, 0, 0 }
+};
+
+static void help(const char *program)
+{
+       assert(program);
+
+       printf("Usage:\n"
+              "\t%s [-o|--output <output info>] <input info json>\n", program);
+}
+
+static long file_size(FILE *fp)
+{
+       assert(fp);
+
+       struct stat stat;
+       int fd = fileno(fp);
+       if (fd == -1 || fstat(fd, &stat) == -1) {
+               printf("Get input file size error: %m\n");
+               return -1;
+       }
+
+       return stat.st_size;
+}
+
+static json_object *parse_json(FILE *fp)
+{
+       assert(fp);
+
+       json_object *jobj = NULL;
+       char *buff = NULL;
+
+       long size = file_size(fp);
+       if (size == -1)
+               goto exit;
+
+       buff = malloc(size);
+       if (fread(buff, size, 1, fp) != 1) {
+               printf("Cannot read the file: %d\n", ferror(fp));
+               goto exit;
+       }
+
+       jobj = json_tokener_parse(buff);
+       if (jobj == NULL)
+               printf("JSON parsing error\n");
+
+exit:
+       free(buff);
+       return jobj;
+}
+
+static void save_report_info(json_object *jobj, FILE *fp)
+{
+       print_report_info(jobj, fp);
+}
+
+int main(int argc, char *argv[])
+{
+       int res = EXIT_FAILURE;
+       FILE *fp_input = NULL, *fp_output = stdout;
+
+       int c;
+       while ((c = getopt_long(argc, argv, "ho:", opts, NULL)) != -1) {
+               switch(c) {
+               case 'o':
+               case OPT_OUTPUT_FILE:
+                       fp_output = fopen(optarg, "w");
+                       break;
+               case 'h':
+               case OPT_HELP:
+                       res = EXIT_SUCCESS;
+                       help(argv[0]);
+                       goto exit;
+               default:
+                       help(argv[0]);
+                       goto exit;
+               }
+       }
+
+       if (optind < argc) {
+               fp_input = fopen(argv[optind], "r");
+               if (fp_input == NULL) {
+                       printf("Cannot open file for reading: %s", argv[optind]);
+                       goto exit;
+               }
+       }
+
+       if (fp_input == NULL) {
+               printf("No input file specified\n");
+               goto exit;
+       }
+
+       json_object *jobj = parse_json(fp_input);
+       if (jobj == NULL)
+               goto exit;
+
+       save_report_info(jobj, fp_output);
+       res = EXIT_SUCCESS;
+exit:
+       if (fp_input != NULL)
+               fclose(fp_input);
+       if (fp_output != NULL)
+               fclose(fp_output);
+       return res;
+}