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

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