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

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