From 8e8d2edd5c5eac60b33cb36cc6012d076ddb9b13 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Thu, 11 Dec 2014 16:17:21 +0100
Subject: [PATCH] ccpp-hook: move utility functions to hooklib

Related to #548

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/hooks/abrt-hook-ccpp.c | 49 ++----------------------------------------
 src/include/hooklib.h      |  6 ++++++
 src/lib/hooklib.c          | 53 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 61 insertions(+), 47 deletions(-)

diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
index 7fd9520..37a9a74 100644
--- a/src/hooks/abrt-hook-ccpp.c
+++ b/src/hooks/abrt-hook-ccpp.c
@@ -26,9 +26,6 @@
 #include <satyr/utils.h>
 #endif /* ENABLE_DUMP_TIME_UNWIND */
 
-#define  DUMP_SUID_UNSAFE 1
-#define  DUMP_SUID_SAFE 2
-
 
 /* I want to use -Werror, but gcc-4.4 throws a curveball:
  * "warning: ignoring return value of 'ftruncate', declared with attribute warn_unused_result"
@@ -157,33 +154,6 @@ static char *core_basename = (char*) "core";
  */
 static char *full_core_basename;
 
-static int dump_suid_policy()
-{
-    /*
-     - values are:
-       0 - don't dump suided programs - in this case the hook is not called by kernel
-       1 - create coredump readable by fs_uid
-       2 - create coredump readable by root only
-    */
-    int c;
-    int suid_dump_policy = 0;
-    const char *filename = "/proc/sys/fs/suid_dumpable";
-    FILE *f  = fopen(filename, "r");
-    if (!f)
-    {
-        log("Can't open %s", filename);
-        return suid_dump_policy;
-    }
-
-    c = fgetc(f);
-    fclose(f);
-    if (c != EOF)
-        suid_dump_policy = c - '0';
-
-    //log("suid dump policy is: %i", suid_dump_policy);
-    return suid_dump_policy;
-}
-
 static int open_user_core(uid_t uid, uid_t fsuid, pid_t pid, char **percent_values)
 {
     errno = 0;
@@ -564,23 +534,8 @@ int main(int argc, char** argv)
     }
 
     const char *signame = NULL;
-    switch (signal_no)
-    {
-        case SIGILL : signame = "ILL" ; break;
-        case SIGFPE : signame = "FPE" ; break;
-        case SIGSEGV: signame = "SEGV"; break;
-        case SIGBUS : signame = "BUS" ; break; //Bus error (bad memory access)
-        case SIGABRT: signame = "ABRT"; break; //usually when abort() was called
-    // We have real-world reports from users who see buggy programs
-    // dying with SIGTRAP, uncommented it too:
-        case SIGTRAP: signame = "TRAP"; break; //Trace/breakpoint trap
-    // These usually aren't caused by bugs:
-      //case SIGQUIT: signame = "QUIT"; break; //Quit from keyboard
-      //case SIGSYS : signame = "SYS" ; break; //Bad argument to routine (SVr4)
-      //case SIGXCPU: signame = "XCPU"; break; //CPU time limit exceeded (4.2BSD)
-      //case SIGXFSZ: signame = "XFSZ"; break; //File size limit exceeded (4.2BSD)
-        default: return create_user_core(user_core_fd, pid, ulimit_c); // not a signal we care about
-    }
+    if (!signal_is_fatal(signal_no, &signame))
+        return create_user_core(user_core_fd, pid, ulimit_c); // not a signal we care about
 
     if (!daemon_is_ok())
     {
diff --git a/src/include/hooklib.h b/src/include/hooklib.h
index 4edd4ea..1ede5e4 100644
--- a/src/include/hooklib.h
+++ b/src/include/hooklib.h
@@ -29,3 +29,9 @@
  stored data, but it's not guaranteed)
  */
 char *problem_data_save(problem_data_t *pd);
+
+#define  DUMP_SUID_UNSAFE 1
+#define  DUMP_SUID_SAFE 2
+
+int dump_suid_policy();
+int signal_is_fatal(int signal_no, const char **name);
diff --git a/src/lib/hooklib.c b/src/lib/hooklib.c
index 1d45cdd..2be4e80 100644
--- a/src/lib/hooklib.c
+++ b/src/lib/hooklib.c
@@ -422,3 +422,56 @@ char* problem_data_save(problem_data_t *pd)
     log_info("problem id: '%s'", problem_id);
     return problem_id;
 }
+
+int dump_suid_policy()
+{
+    /*
+     - values are:
+       0 - don't dump suided programs - in this case the hook is not called by kernel
+       1 - create coredump readable by fs_uid
+       2 - create coredump readable by root only
+    */
+    int c;
+    int suid_dump_policy = 0;
+    const char *filename = "/proc/sys/fs/suid_dumpable";
+    FILE *f  = fopen(filename, "r");
+    if (!f)
+    {
+        log("Can't open %s", filename);
+        return suid_dump_policy;
+    }
+
+    c = fgetc(f);
+    fclose(f);
+    if (c != EOF)
+        suid_dump_policy = c - '0';
+
+    //log("suid dump policy is: %i", suid_dump_policy);
+    return suid_dump_policy;
+}
+
+int signal_is_fatal(int signal_no, const char **name)
+{
+    const char *signame = NULL;
+    switch (signal_no)
+    {
+        case SIGILL : signame = "ILL" ; break;
+        case SIGFPE : signame = "FPE" ; break;
+        case SIGSEGV: signame = "SEGV"; break;
+        case SIGBUS : signame = "BUS" ; break; //Bus error (bad memory access)
+        case SIGABRT: signame = "ABRT"; break; //usually when abort() was called
+    // We have real-world reports from users who see buggy programs
+    // dying with SIGTRAP, uncommented it too:
+        case SIGTRAP: signame = "TRAP"; break; //Trace/breakpoint trap
+    // These usually aren't caused by bugs:
+      //case SIGQUIT: signame = "QUIT"; break; //Quit from keyboard
+      //case SIGSYS : signame = "SYS" ; break; //Bad argument to routine (SVr4)
+      //case SIGXCPU: signame = "XCPU"; break; //CPU time limit exceeded (4.2BSD)
+      //case SIGXFSZ: signame = "XFSZ"; break; //File size limit exceeded (4.2BSD)
+    }
+
+    if (name != NULL)
+        *name = signame;
+
+    return signame != NULL;
+}
-- 
2.1.0

