dreport: Implementation of extracting pid from the core file name

Change-Id: I5605f6f5697ccd4bffa58917b3c6d660a301c6cd
Signed-off-by: Jayanth Othayoth <ojayanth@in.ibm.com>
diff --git a/tools/dreport b/tools/dreport
index 03f6e13..9f6afca 100755
--- a/tools/dreport
+++ b/tools/dreport
@@ -65,6 +65,7 @@
 declare -x summary_log=""
 declare -x cur_dump_size=0
 declare -a command_list=("")
+declare -x core_pid="0"
 
 # @brief Initialize user type command array
 function runlevel_user()
@@ -305,6 +306,29 @@
     fi
 }
 
+# @brief get pid from the file
+#        dreport "core" type user provides core file as optional file parameter.
+#        As per coredump source code systemd-coredump uses below format
+#        https://github.com/systemd/systemd/blob/master/src/coredump/coredump.c
+#        /var/lib/systemd/coredump/core.%s.%s." SD_ID128_FORMAT_STR “.
+#        <process ID>.%s000000"
+#        Added additional check to make sure that user provided in
+#        systemd-coredump format.
+function set_pid()
+{
+    #from coredump.c file.
+    CORE_BASE_STRING="/var/lib/systemd/coredump/core"
+
+    #Escape bash characters in file name
+    file=$(printf %q "$optional_file")
+
+    string=$(echo $file | awk -F . '{ print $1}')
+    if [ "$CORE_BASE_STRING" = "$string" ]; then
+        #matching systemd-coredump core file format.
+        core_pid=$(echo $file | awk -F . '{ print $5}')
+    fi
+}
+
 # @brief Capture debug data based on the input command array.
 #        and stores in to global temporary name specific location.
 # @param $1 Source array