Blame SOURCES/001-crm_report-log-search.patch

60de42
From 083488ce76ee7ab83adb51c230687a9fd67b54a7 Mon Sep 17 00:00:00 2001
60de42
From: Ken Gaillot <kgaillot@redhat.com>
60de42
Date: Fri, 16 Dec 2016 17:56:30 -0600
60de42
Subject: [PATCH] Fix: tools: avoid grep crashes in crm_report when looking for
60de42
 system logs
60de42
60de42
Grepping large binary files can exhaust memory. This avoids that and speeds up
60de42
crm_report's log scan by: skipping things that aren't files, aren't readable,
60de42
or are empty; skipping files with more than 256 nonprintable bytes in the first
60de42
1024; and using LC_ALL="C" to speed up grep.
60de42
---
60de42
 tools/report.common.in | 22 ++++++++++++++++++++--
60de42
 1 file changed, 20 insertions(+), 2 deletions(-)
60de42
60de42
diff --git a/tools/report.common.in b/tools/report.common.in
60de42
index 9b43486..70c920c 100644
60de42
--- a/tools/report.common.in
60de42
+++ b/tools/report.common.in
60de42
@@ -421,8 +421,26 @@ findmsg() {
60de42
     # Check each log file for matches.
60de42
     logfiles=""
60de42
     for f in $candidates; do
60de42
-        local cat=$(find_decompressor "$f")
60de42
-        $cat "$f" 2>/dev/null | grep -q -e "$pattern"
60de42
+        local cat=""
60de42
+
60de42
+        # We only care about readable files with something in them.
60de42
+        if [ ! -f "$f" ] || [ ! -r "$f" ] || [ ! -s "$f" ] ; then
60de42
+            continue
60de42
+        fi
60de42
+
60de42
+        cat=$(find_decompressor "$f")
60de42
+
60de42
+        # We want to avoid grepping through potentially huge binary logs such
60de42
+        # as lastlog. However, control characters sometimes find their way into
60de42
+        # text logs, so we use a heuristic of more than 256 nonprintable
60de42
+        # characters in the file's first kilobyte.
60de42
+        if [ $($cat "$f" 2>/dev/null | head -c 1024 | tr -d '[:print:][:space:]' | wc -c) -gt 256 ]
60de42
+        then
60de42
+            continue
60de42
+        fi
60de42
+
60de42
+        # Our patterns are ASCII, so we can use LC_ALL="C" to speed up grep
60de42
+        $cat "$f" 2>/dev/null | LC_ALL="C" grep -q -e "$pattern"
60de42
         if [ $? -eq 0 ]; then
60de42
 
60de42
             # Add this file to the list of hits
60de42
-- 
60de42
1.8.3.1
60de42