bcfb1b5
From 94dc5d2783133cea27b1ae804d5b8a2da06808c5 Mon Sep 17 00:00:00 2001
bcfb1b5
From: Matej Habrnal <mhabrnal@redhat.com>
bcfb1b5
Date: Wed, 6 Jun 2018 14:04:09 +0200
bcfb1b5
Subject: [PATCH] ccpp: add %h and %e parameter into abrt-hook-ccpp
bcfb1b5
bcfb1b5
Without this commit core_pattern's parameter %h and %e was not
bcfb1b5
translated at all.
bcfb1b5
bcfb1b5
If there is a white space in executable filename, %e replaced only by
bcfb1b5
the first part of executable name (till the space). Hence we decided
bcfb1b5
to get executable name from /proc/PID/exe symlink exist.
bcfb1b5
bcfb1b5
Example:
bcfb1b5
If 'core_pattern = core.%h.%p.%t.%e' the result was
bcfb1b5
core.%h.26284.1469805542.sleep not
bcfb1b5
core.myshostmane.26284.1469805542.sleep with spaces
bcfb1b5
bcfb1b5
Related to #1587891
bcfb1b5
bcfb1b5
Signed-off-by: Matej Habrnal <mhabrnal@redhat.com>
bcfb1b5
---
bcfb1b5
 src/hooks/abrt-hook-ccpp.c          | 36 +++++++++++++++++++----------
bcfb1b5
 src/hooks/abrt-install-ccpp-hook.in |  2 +-
bcfb1b5
 2 files changed, 25 insertions(+), 13 deletions(-)
bcfb1b5
bcfb1b5
diff --git a/src/hooks/abrt-hook-ccpp.c b/src/hooks/abrt-hook-ccpp.c
bcfb1b5
index 1c4e45e5..40117fc8 100644
bcfb1b5
--- a/src/hooks/abrt-hook-ccpp.c
bcfb1b5
+++ b/src/hooks/abrt-hook-ccpp.c
bcfb1b5
@@ -65,13 +65,13 @@ static struct dump_dir *dd;
bcfb1b5
  * %t - UNIX time of dump
bcfb1b5
  * %P - global pid
bcfb1b5
  * %I - crash thread tid
bcfb1b5
- * %e - executable filename (can contain white spaces)
bcfb1b5
+ * %h - hostname
bcfb1b5
+ * %e - executable filename (can contain white spaces, must be placed at the end)
bcfb1b5
  * %% - output one "%"
bcfb1b5
  */
bcfb1b5
 /* Hook must be installed with exactly the same sequence of %c specifiers.
bcfb1b5
- * Last one, %h, may be omitted (we can find it out).
bcfb1b5
  */
bcfb1b5
-static const char percent_specifiers[] = "%scpugtePi";
bcfb1b5
+static const char percent_specifiers[] = "%scpugtPIhe";
bcfb1b5
 static char *core_basename = (char*) "core";
bcfb1b5
 
bcfb1b5
 static DIR *open_cwd(pid_t pid)
bcfb1b5
@@ -146,7 +146,8 @@ static int setfscreatecon_raw(security_context_t context)
bcfb1b5
 }
bcfb1b5
 #endif
bcfb1b5
 
bcfb1b5
-static int open_user_core(uid_t uid, uid_t fsuid, gid_t fsgid, pid_t pid, char **percent_values)
bcfb1b5
+static int open_user_core(uid_t uid, uid_t fsuid, gid_t fsgid, pid_t pid,
bcfb1b5
+                          char **percent_values, const char *executable_filename)
bcfb1b5
 {
bcfb1b5
     proc_cwd = open_cwd(pid);
bcfb1b5
     if (proc_cwd == NULL)
bcfb1b5
@@ -196,7 +197,13 @@ static int open_user_core(uid_t uid, uid_t fsuid, gid_t fsgid, pid_t pid, char *
bcfb1b5
             {
bcfb1b5
                 const char *val = "%";
bcfb1b5
                 if (specifier_num > 0) /* not %% */
bcfb1b5
+                {
bcfb1b5
                     val = percent_values[specifier_num - 1];
bcfb1b5
+                    /* if %e (executable filename), use executable from
bcfb1b5
+                     * /proc/PID/exe symlink if exists */
bcfb1b5
+                    if (percent_specifiers[specifier_num] == 'e' && executable_filename)
bcfb1b5
+                        val = executable_filename;
bcfb1b5
+                }
bcfb1b5
                 //log_warning("c:'%c'", c);
bcfb1b5
                 //log_warning("val:'%s'", val);
bcfb1b5
 
bcfb1b5
@@ -917,9 +924,9 @@ int main(int argc, char** argv)
bcfb1b5
 
bcfb1b5
     if (argc < 8)
bcfb1b5
     {
bcfb1b5
-        /* percent specifier:         %s   %c              %p  %u  %g  %t   %P         %T        */
bcfb1b5
-        /* argv:                  [0] [1]  [2]             [3] [4] [5] [6]  [7]        [8]       */
bcfb1b5
-        error_msg_and_die("Usage: %s SIGNO CORE_SIZE_LIMIT PID UID GID TIME GLOBAL_PID GLOBAL_TID", argv[0]);
bcfb1b5
+        /* percent specifier:         %s   %c              %p  %u  %g  %t   %P         %I         %h       %e   */
bcfb1b5
+        /* argv:                  [0] [1]  [2]             [3] [4] [5] [6]  [7]        [8]        [9]      [10] */
bcfb1b5
+        error_msg_and_die("Usage: %s SIGNO CORE_SIZE_LIMIT PID UID GID TIME GLOBAL_PID GLOBAL_TID HOSTNAME BINARY_NAME", argv[0]);
bcfb1b5
     }
bcfb1b5
 
bcfb1b5
     /* Not needed on 2.6.30.
bcfb1b5
@@ -1016,13 +1023,21 @@ int main(int argc, char** argv)
bcfb1b5
 
bcfb1b5
     snprintf(path, sizeof(path), "%s/last-ccpp", g_settings_dump_location);
bcfb1b5
 
bcfb1b5
+    char *executable = get_executable_at(pid_proc_fd);
bcfb1b5
+    const char *last_slash = NULL;
bcfb1b5
+    if (executable)
bcfb1b5
+    {
bcfb1b5
+        last_slash = strrchr(executable, '/');
bcfb1b5
+        /* if the last_slash was found, skip it */
bcfb1b5
+        if (last_slash) ++last_slash;
bcfb1b5
+    }
bcfb1b5
+
bcfb1b5
     /* Open a fd to compat coredump, if requested and is possible */
bcfb1b5
     int user_core_fd = -1;
bcfb1b5
     if (setting_MakeCompatCore && ulimit_c != 0)
bcfb1b5
         /* note: checks "user_pwd == NULL" inside; updates core_basename */
bcfb1b5
-        user_core_fd = open_user_core(uid, fsuid, fsgid, pid, &argv[1]);
bcfb1b5
+        user_core_fd = open_user_core(uid, fsuid, fsgid, pid, &argv[1], (const char *)last_slash);
bcfb1b5
 
bcfb1b5
-    char *executable = get_executable_at(pid_proc_fd);
bcfb1b5
     if (executable == NULL)
bcfb1b5
     {
bcfb1b5
         /* readlink on /proc/$PID/exe failed, don't create abrt dump dir */
bcfb1b5
@@ -1031,9 +1046,6 @@ int main(int argc, char** argv)
bcfb1b5
         return create_user_core(user_core_fd, pid, ulimit_c);
bcfb1b5
     }
bcfb1b5
 
bcfb1b5
-    const char *last_slash = strrchr(executable, '/');
bcfb1b5
-    /* if the last_slash was found, skip it */
bcfb1b5
-    if (last_slash) ++last_slash;
bcfb1b5
 
bcfb1b5
     /* ignoring crashes */
bcfb1b5
     if (executable && is_path_ignored(setting_ignored_paths, executable))
bcfb1b5
diff --git a/src/hooks/abrt-install-ccpp-hook.in b/src/hooks/abrt-install-ccpp-hook.in
bcfb1b5
index 660c2091..f8c0c610 100755
bcfb1b5
--- a/src/hooks/abrt-install-ccpp-hook.in
bcfb1b5
+++ b/src/hooks/abrt-install-ccpp-hook.in
bcfb1b5
@@ -11,7 +11,7 @@ SAVED_PATTERN_DIR="@VAR_RUN@/abrt"
bcfb1b5
 SAVED_PATTERN_FILE="@VAR_RUN@/abrt/saved_core_pattern"
bcfb1b5
 HOOK_BIN="@libexecdir@/abrt-hook-ccpp"
bcfb1b5
 # Must match percent_specifiers[] order in abrt-hook-ccpp.c:
bcfb1b5
-PATTERN="|$HOOK_BIN %s %c %p %u %g %t %P %I"
bcfb1b5
+PATTERN="|$HOOK_BIN %s %c %p %u %g %t %P %I %h %e"
bcfb1b5
 
bcfb1b5
 # core_pipe_limit specifies how many dump_helpers can run at the same time
bcfb1b5
 # 0 - means unlimited, but it's not guaranteed that /proc/<pid> of crashing
bcfb1b5
-- 
bcfb1b5
2.19.0
bcfb1b5