From 8f6a49db8997b1296eafa639920e039f87fb1989 Mon Sep 17 00:00:00 2001
From: Martin Milata <mmilata@redhat.com>
Date: Mon, 8 Dec 2014 17:01:56 +0100
Subject: [PATCH] abrt-install-ccpp-hook check configuration

Check that either full coredump or core backtrace are configured to be
saved, fail init script if neither is.

Related to #829.

Signed-off-by: Martin Milata <mmilata@redhat.com>
---
 src/hooks/abrt-hook-ccpp.c          | 63 +++++++++++++++++++++++--------------
 src/hooks/abrt-install-ccpp-hook.in |  5 +++
 2 files changed, 44 insertions(+), 24 deletions(-)

diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
index 57c56a7..f8f97ad 100644
--- a/src/hooks/abrt-hook-ccpp.c
+++ b/src/hooks/abrt-hook-ccpp.c
@@ -501,6 +501,18 @@ static int create_user_core(int user_core_fd, pid_t pid, off_t ulimit_c)
     return 0;
 }
 
+static int test_configuration(bool setting_SaveFullCore, bool setting_CreateCoreBacktrace)
+{
+    if (!setting_SaveFullCore && !setting_CreateCoreBacktrace)
+    {
+        fprintf(stderr, "Both SaveFullCore and CreateCoreBacktrace are disabled - "
+                        "at least one of them is needed for useful report.\n");
+        return 1;
+    }
+
+    return 0;
+}
+
 int main(int argc, char** argv)
 {
     /* Kernel starts us with all fd's closed.
@@ -510,31 +522,9 @@ int main(int argc, char** argv)
      */
     int fd = xopen("/dev/null", O_RDWR);
     while (fd < 2)
-	fd = xdup(fd);
+        fd = xdup(fd);
     if (fd > 2)
-	close(fd);
-
-    if (argc < 8)
-    {
-        /* percent specifier:         %s   %c              %p  %u  %g  %t   %e          %i */
-        /* argv:                  [0] [1]  [2]             [3] [4] [5] [6]  [7]         [8]*/
-        error_msg_and_die("Usage: %s SIGNO CORE_SIZE_LIMIT PID UID GID TIME BINARY_NAME [TID]", argv[0]);
-    }
-
-    /* Not needed on 2.6.30.
-     * At least 2.6.18 has a bug where
-     * argv[1] = "SIGNO CORE_SIZE_LIMIT PID ..."
-     * argv[2] = "CORE_SIZE_LIMIT PID ..."
-     * and so on. Fixing it:
-     */
-    if (strchr(argv[1], ' '))
-    {
-        int i;
-        for (i = 1; argv[i]; i++)
-        {
-            strchrnul(argv[i], ' ')[0] = '\0';
-        }
-    }
+        close(fd);
 
     logmode = LOGMODE_JOURNAL;
 
@@ -563,6 +553,31 @@ int main(int argc, char** argv)
         free_map_string(settings);
     }
 
+    if (argc == 2 && strcmp(argv[1], "--config-test"))
+        return test_configuration(setting_SaveFullCore, setting_CreateCoreBacktrace);
+
+    if (argc < 8)
+    {
+        /* percent specifier:         %s   %c              %p  %u  %g  %t   %e          %i */
+        /* argv:                  [0] [1]  [2]             [3] [4] [5] [6]  [7]         [8]*/
+        error_msg_and_die("Usage: %s SIGNO CORE_SIZE_LIMIT PID UID GID TIME BINARY_NAME [TID]", argv[0]);
+    }
+
+    /* Not needed on 2.6.30.
+     * At least 2.6.18 has a bug where
+     * argv[1] = "SIGNO CORE_SIZE_LIMIT PID ..."
+     * argv[2] = "CORE_SIZE_LIMIT PID ..."
+     * and so on. Fixing it:
+     */
+    if (strchr(argv[1], ' '))
+    {
+        int i;
+        for (i = 1; argv[i]; i++)
+        {
+            strchrnul(argv[i], ' ')[0] = '\0';
+        }
+    }
+
     errno = 0;
     const char* signal_str = argv[1];
     int signal_no = xatoi_positive(signal_str);
diff --git a/src/hooks/abrt-install-ccpp-hook.in b/src/hooks/abrt-install-ccpp-hook.in
index d4ed4a5..fff0a33 100755
--- a/src/hooks/abrt-install-ccpp-hook.in
+++ b/src/hooks/abrt-install-ccpp-hook.in
@@ -31,6 +31,11 @@ CORE_PIPE_LIMIT_FILE="/proc/sys/kernel/core_pipe_limit"
 CORE_PIPE_LIMIT="4"
 
 start() {
+	if ! $HOOK_BIN --test-config; then
+		echo "Invalid configuration."
+		exit 1
+	fi
+
 	cur=`cat "$PATTERN_FILE"`
 	cur_first=`printf "%s" "$cur" | sed 's/ .*//'`
 
-- 
2.1.0

