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

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