Blame SOURCES/0011-Add-support-for-journald-and-syslog.patch

b35d98
From 73a5eb055170e761f1815a3d68d0931bae2cc405 Mon Sep 17 00:00:00 2001
b35d98
From: Jakub Filak <jfilak@redhat.com>
b35d98
Date: Tue, 29 Oct 2013 18:39:20 +0100
b35d98
Subject: [PATCH 11/39] Add support for journald and syslog
b35d98
b35d98
Two new command line arguments:
b35d98
    syslog=(on|yes)
b35d98
        - disabled by default
b35d98
        - logs a stack trace to syslog with LOG_ERR level
b35d98
b35d98
    journald=(off|no)
b35d98
        - disabled by default
b35d98
        - logs a stack trace to journald with LOG_ERR level
b35d98
        - the stack trace is saved in STACK_TRACE field
b35d98
b35d98
Related to rhbz#1023081
b35d98
Related to rhbz#1055581
b35d98
b35d98
Signed-off-by: Jakub Filak <jfilak@redhat.com>
b35d98
---
b35d98
 README                           | 13 ++++++
b35d98
 package/abrt-java-connector.spec |  1 +
b35d98
 src/CMakeLists.txt               |  3 ++
b35d98
 src/abrt-checker.c               | 92 ++++++++++++++++++++++++++++++++--------
b35d98
 4 files changed, 91 insertions(+), 18 deletions(-)
b35d98
b35d98
diff --git a/README b/README
b35d98
index abbed4f..f9545bd 100644
b35d98
--- a/README
b35d98
+++ b/README
b35d98
@@ -50,3 +50,16 @@ Example4:
b35d98
 - user must provide a colon separated list of exception type names
b35d98
 
b35d98
 $  java -agentlib:abrt-java-connector=caught=java.io.FileNotFoundException:java.io.FileNotFoundException $MyClass -platform.jvmtiSupported true
b35d98
+
b35d98
+Example5:
b35d98
+- this example shows hot to enable syslog and disable journald
b35d98
+- abrt-java-connector reports detected problems to journald by default
b35d98
+- problems reported to journald has stack trace stored in STACK_TRACE field
b35d98
+- problems reported to syslog are written to syslog with entire backtrace
b35d98
+
b35d98
+- disable journald
b35d98
+$  java -agentlib:abrt-java-connector=journald=off $MyClass -platform.jvmtiSupported true
b35d98
+
b35d98
+
b35d98
+- enable syslog
b35d98
+$  java -agentlib:abrt-java-connector=syslog=on $MyClass -platform.jvmtiSupported true
b35d98
diff --git a/package/abrt-java-connector.spec b/package/abrt-java-connector.spec
b35d98
index abb0fee..88ecfc6 100644
b35d98
--- a/package/abrt-java-connector.spec
b35d98
+++ b/package/abrt-java-connector.spec
b35d98
@@ -14,6 +14,7 @@ Source0:	https://github.com/jfilak/%{name}/archive/%{commit}/%{name}-%{version}-
b35d98
 BuildRequires:	cmake
b35d98
 BuildRequires:	libreport-devel
b35d98
 BuildRequires:	java-1.7.0-openjdk-devel
b35d98
+BuildRequires:	systemd-devel
b35d98
 
b35d98
 Requires:	abrt
b35d98
 
b35d98
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
b35d98
index e7c8a8e..a00fe77 100644
b35d98
--- a/src/CMakeLists.txt
b35d98
+++ b/src/CMakeLists.txt
b35d98
@@ -3,7 +3,9 @@ include_directories(${JAVA_INCLUDE_PATH} ${JAVA_INCLUDE_PATH2})
b35d98
 
b35d98
 include(FindPkgConfig)
b35d98
 pkg_check_modules(PC_ABRT REQUIRED libreport)
b35d98
+pkg_check_modules(PC_JOURNALD REQUIRED libsystemd-journal)
b35d98
 include_directories(${PC_ABRT_INCLUDE_DIRS})
b35d98
+include_directories(${PC_JOURNALD_INCLUDE_DIRS})
b35d98
 
b35d98
 add_definitions(-D_GNU_SOURCE)
b35d98
 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -std=c99 -pedantic")
b35d98
@@ -19,5 +21,6 @@ set_target_properties(
b35d98
         OUTPUT_NAME abrt-java-connector)
b35d98
 
b35d98
 target_link_libraries(AbrtChecker ${PC_ABRT_LIBRARIES})
b35d98
+target_link_libraries(AbrtChecker ${PC_JOURNALD_LIBRARIES})
b35d98
 
b35d98
 install(TARGETS AbrtChecker DESTINATION ${LIB_INSTALL_DIR})
b35d98
diff --git a/src/abrt-checker.c b/src/abrt-checker.c
b35d98
index 3eac971..26665d6 100644
b35d98
--- a/src/abrt-checker.c
b35d98
+++ b/src/abrt-checker.c
b35d98
@@ -39,6 +39,8 @@
b35d98
 #include <linux/limits.h>
b35d98
 #include <sys/stat.h>
b35d98
 #include <errno.h>
b35d98
+#include <systemd/sd-journal.h>
b35d98
+#include <syslog.h>
b35d98
 
b35d98
 /* Shared macros and so on */
b35d98
 #include "abrt-checker.h"
b35d98
@@ -168,6 +170,8 @@ typedef struct {
b35d98
 typedef enum {
b35d98
     ED_TERMINAL = 1,                ///< Report errors to the terminal
b35d98
     ED_ABRT     = ED_TERMINAL << 1, ///< Submit error reports to ABRT
b35d98
+    ED_SYSLOG   = ED_ABRT << 1,     ///< Submit error reports to syslog
b35d98
+    ED_JOURNALD = ED_SYSLOG << 1,   ///< Submit error reports to journald
b35d98
 } T_errorDestination;
b35d98
 
b35d98
 /* Global monitor lock */
b35d98
@@ -191,7 +195,7 @@ T_jvmEnvironment jvmEnvironment;
b35d98
 T_processProperties processProperties;
b35d98
 
b35d98
 /* Global configuration of report destination */
b35d98
-T_errorDestination reportErrosTo;
b35d98
+T_errorDestination reportErrosTo = ED_JOURNALD;
b35d98
 
b35d98
 /* Path (not necessary absolute) to output file */
b35d98
 char *outputFileName = DISABLED_LOG_OUTPUT;
b35d98
@@ -471,7 +475,10 @@ static void add_process_properties_data(problem_data_t *pd)
b35d98
  * Register new ABRT event using given message and a method name.
b35d98
  * If reportErrosTo global flags doesn't contain ED_ABRT, this function does nothing.
b35d98
  */
b35d98
-static void register_abrt_event(char * executable, char * message, char * backtrace)
b35d98
+static void register_abrt_event(
b35d98
+        const char *executable,
b35d98
+        const char *message,
b35d98
+        const char *backtrace)
b35d98
 {
b35d98
     if ((reportErrosTo & ED_ABRT) == 0)
b35d98
     {
b35d98
@@ -510,6 +517,46 @@ static void register_abrt_event(char * executable, char * message, char * backtr
b35d98
 
b35d98
 
b35d98
 /*
b35d98
+ * Report a stack trace to all systems
b35d98
+ */
b35d98
+static void report_stacktrace(
b35d98
+        const char *message,
b35d98
+        const char *stacktrace,
b35d98
+        int sure_unique)
b35d98
+{
b35d98
+    if (reportErrosTo & ED_SYSLOG)
b35d98
+    {
b35d98
+        VERBOSE_PRINT("Reporting stack trace to syslog\n");
b35d98
+        syslog(LOG_ERR, "%s\n%s", message, stacktrace);
b35d98
+    }
b35d98
+
b35d98
+    if (reportErrosTo & ED_JOURNALD)
b35d98
+    {
b35d98
+        VERBOSE_PRINT("Reporting stack trace to JournalD\n");
b35d98
+        sd_journal_send("MESSAGE=%s", message,
b35d98
+                        "PRIORITY=%d", LOG_ERR,
b35d98
+                        "STACK_TRACE=%s", stacktrace ? stacktrace : "no stack trace",
b35d98
+                        NULL);
b35d98
+
b35d98
+    }
b35d98
+
b35d98
+    log_print("%s\n", message);
b35d98
+
b35d98
+    if (stacktrace)
b35d98
+    {
b35d98
+        log_print("%s", stacktrace);
b35d98
+    }
b35d98
+
b35d98
+    if (NULL != stacktrace && sure_unique)
b35d98
+    {
b35d98
+        VERBOSE_PRINT("Reporting stack trace to ABRT");
b35d98
+        register_abrt_event(processProperties.main_class, message, stacktrace);
b35d98
+    }
b35d98
+}
b35d98
+
b35d98
+
b35d98
+
b35d98
+/*
b35d98
  * Print a message when any JVM TI error occurs.
b35d98
  */
b35d98
 static void print_jvmti_error(
b35d98
@@ -2026,23 +2073,16 @@ static void JNICALL callback_on_exception(
b35d98
             char *message = format_exception_reason_message(/*caught?*/NULL != catch_method,
b35d98
                     updated_exception_name_ptr, class_name_ptr, method_name_ptr);
b35d98
 
b35d98
-            if (NULL != message)
b35d98
-            {
b35d98
-                log_print("%s\n", message);
b35d98
+            char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object);
b35d98
 
b35d98
-                //char *stack_trace_str = generate_stack_trace(jvmti_env, jni_env, thr, tname, updated_exception_name_ptr);
b35d98
-                char *stack_trace_str = generate_thread_stack_trace(jvmti_env, jni_env, tname, exception_object);
b35d98
-                if (NULL != stack_trace_str)
b35d98
-                {
b35d98
-                    log_print("%s", stack_trace_str);
b35d98
-                    if (NULL != threads_exc_buf)
b35d98
-                    {
b35d98
-                        register_abrt_event(processProperties.main_class, message, stack_trace_str);
b35d98
-                    }
b35d98
-                    free(stack_trace_str);
b35d98
-                }
b35d98
-                free(message);
b35d98
-            }
b35d98
+            const char *report_message = message;
b35d98
+            if (NULL == report_message)
b35d98
+                report_message = (NULL != catch_method) ? "Caught exception" : "Uncaught exception";
b35d98
+
b35d98
+            report_stacktrace(report_message, stack_trace_str, NULL != threads_exc_buf);
b35d98
+
b35d98
+            free(message);
b35d98
+            free(stack_trace_str);
b35d98
         }
b35d98
         else
b35d98
         {
b35d98
@@ -2602,6 +2642,22 @@ void parse_commandline_options(char *options)
b35d98
                 reportErrosTo |= ED_ABRT;
b35d98
             }
b35d98
         }
b35d98
+        else if (strcmp("syslog", key) == 0)
b35d98
+        {
b35d98
+            if (value != NULL && (strcasecmp("on", value) == 0 || strcasecmp("yes", value) == 0))
b35d98
+            {
b35d98
+                VERBOSE_PRINT("Enabling errors reporting to syslog\n");
b35d98
+                reportErrosTo |= ED_SYSLOG;
b35d98
+            }
b35d98
+        }
b35d98
+        else if (strcmp("journald", key) == 0)
b35d98
+        {
b35d98
+            if (value != NULL && (strcasecmp("off", value) == 0 || strcasecmp("no", value) == 0))
b35d98
+            {
b35d98
+                VERBOSE_PRINT("Disable errors reporting to JournalD\n");
b35d98
+                reportErrosTo &= ~ED_JOURNALD;
b35d98
+            }
b35d98
+        }
b35d98
         else if(strcmp("output", key) == 0)
b35d98
         {
b35d98
             if (DISABLED_LOG_OUTPUT != outputFileName)
b35d98
-- 
b35d98
1.8.3.1
b35d98