Blame SOURCES/0284-vmcore-use-libreport-dd-API-in-the-harvestor.patch

a60cd7
From 3f8c7bd84c33a2281857becaf6e177d74de1da42 Mon Sep 17 00:00:00 2001
a60cd7
From: Jakub Filak <jfilak@redhat.com>
a60cd7
Date: Mon, 8 Jun 2015 19:39:24 +0200
a60cd7
Subject: [PATCH] vmcore: use libreport dd API in the harvestor
a60cd7
a60cd7
The dd API ensure correct permissions and owner.
a60cd7
a60cd7
Signed-off-by: Jakub Filak <jfilak@redhat.com>
a60cd7
---
a60cd7
 src/hooks/abrt_harvest_vmcore.py.in | 155 ++++++++++++++----------------------
a60cd7
 1 file changed, 59 insertions(+), 96 deletions(-)
a60cd7
a60cd7
diff --git a/src/hooks/abrt_harvest_vmcore.py.in b/src/hooks/abrt_harvest_vmcore.py.in
a60cd7
index 990a2b1..c85db8c 100644
a60cd7
--- a/src/hooks/abrt_harvest_vmcore.py.in
a60cd7
+++ b/src/hooks/abrt_harvest_vmcore.py.in
a60cd7
@@ -16,6 +16,7 @@ import augeas
a60cd7
 from subprocess import Popen, PIPE
a60cd7
 
a60cd7
 import problem
a60cd7
+import report
a60cd7
 
a60cd7
 
a60cd7
 def errx(message, code=1):
a60cd7
@@ -105,85 +106,39 @@ def parse_kdump():
a60cd7
     return path
a60cd7
 
a60cd7
 
a60cd7
-def write_to_file(path, content):
a60cd7
+def create_abrtd_info(dest, uuid):
a60cd7
     """
a60cd7
-    A function for writing into a file
a60cd7
-
a60cd7
-    path - path to the file
a60cd7
-    content - content to write into the file
a60cd7
-    """
a60cd7
-
a60cd7
-    with open(path, 'w') as wfile:
a60cd7
-        wfile.write(content)
a60cd7
-
a60cd7
-
a60cd7
-def change_owner_rec(dest):
a60cd7
-    """
a60cd7
-    A simple function to recursively change file mode for a directory.
a60cd7
-
a60cd7
-    dest - path to the directory
a60cd7
-    """
a60cd7
-
a60cd7
-    os.chown(dest, 0, 0)
a60cd7
-    for root, dirs, files in os.walk(dest):
a60cd7
-        for i in dirs:
a60cd7
-            os.chown(os.path.join(root, i), 0, 0)
a60cd7
-        for i in files:
a60cd7
-            os.chown(os.path.join(root, i), 0, 0)
a60cd7
-
a60cd7
+    A simple function to write important information for the abrt daemon into
a60cd7
+    the vmcore directory to let abrtd know what kind of problem it is.
a60cd7
 
a60cd7
-def change_mode_rec(dest):
a60cd7
+    dest - path to the vmcore directory
a60cd7
+    uuid - unique indentifier of the vmcore
a60cd7
     """
a60cd7
-    A simple function to recursively change file mode for a directory.
a60cd7
 
a60cd7
-    dest - path to the directory
a60cd7
-    """
a60cd7
+    dd = report.dd_create(dest, 0)
a60cd7
+    if dd is None:
a60cd7
+        return None
a60cd7
 
a60cd7
-    os.chmod(dest, 0700)
a60cd7
-    for root, dirs, files in os.walk(dest):
a60cd7
-        for i in dirs:
a60cd7
-            os.chmod(os.path.join(root, i), 0700)
a60cd7
-        for i in files:
a60cd7
-            os.chmod(os.path.join(root, i), 0600)
a60cd7
+    dd.create_basic_files(0)
a60cd7
+    dd.save_text('analyzer', 'abrt-vmcore')
a60cd7
+    dd.save_text('type', 'vmcore')
a60cd7
+    dd.save_text('component', 'kernel')
a60cd7
+    dd.save_text('uuid', uuid)
a60cd7
+    return dd
a60cd7
 
a60cd7
 
a60cd7
-def create_abrtd_info(dest):
a60cd7
+def delete_and_close(dd, dd_dirname):
a60cd7
     """
a60cd7
-    A simple function to write important information for the abrt daemon into
a60cd7
-    the vmcore directory to let abrtd know what kind of problem it is.
a60cd7
+    Deletes the given dump directory and closes it.
a60cd7
 
a60cd7
-    dest - path to the vmcore directory
a60cd7
+    dd - dump directory object
a60cd7
+    dd_dirname - full path to dump directory
a60cd7
     """
a60cd7
+    if not dd.delete() == 0:
a60cd7
+        sys.stderr.write("Unable to delete '%s'\n" % (dd_dirname))
a60cd7
+        return
a60cd7
 
a60cd7
-    write_to_file(os.path.join(dest, 'analyzer'), 'vmcore')
a60cd7
-    write_to_file(os.path.join(dest, 'type'), 'vmcore')
a60cd7
-    write_to_file(os.path.join(dest, 'component'), 'kernel')
a60cd7
-    write_to_file(os.path.join(dest, 'time'), str(time.time()).split('.')[0])
a60cd7
-    shutil.copy(os.path.join(dest, 'time'),
a60cd7
-                os.path.join(dest, 'last_occurrence'))
a60cd7
-    write_to_file(os.path.join(dest, 'architecture'), os.uname()[4])
a60cd7
-    write_to_file(os.path.join(dest, 'uid'), '0')
a60cd7
-
a60cd7
-    # TODO: need to generate *real* UUID,
a60cd7
-    # one which has a real chance of catching dups!
a60cd7
-    # This one generates different hashes even for similar cores:
a60cd7
-    hashobj = hashlib.sha1()
a60cd7
-    # Iterate over the file a line at a time in order to not load the whole
a60cd7
-    # vmcore file
a60cd7
-    with open(os.path.join(dest, 'vmcore'), 'r') as corefile:
a60cd7
-        for line in corefile:
a60cd7
-            hashobj.update(line)
a60cd7
-    write_to_file(os.path.join(dest, 'uuid'), hashobj.hexdigest())
a60cd7
-
a60cd7
-    # Write os info into the vmcore directory
a60cd7
-    if os.path.exists('/etc/system-release'):
a60cd7
-        shutil.copy('/etc/system-release', os.path.join(dest, 'os_release'))
a60cd7
-    elif os.path.exists('/etc/redhat-release'):
a60cd7
-        shutil.copy('/etc/redhat-release', os.path.join(dest, 'os_release'))
a60cd7
-    elif os.path.exists('/etc/SuSE-release'):
a60cd7
-        shutil.copy('/etc/SuSE-release', os.path.join(dest, 'os_release'))
a60cd7
-    if os.path.exists('/etc/os-release'):
a60cd7
-        shutil.copy('/etc/os-release', os.path.join(dest, 'os_info'))
a60cd7
+    dd.close()
a60cd7
 
a60cd7
 
a60cd7
 def harvest_vmcore():
a60cd7
@@ -212,8 +167,6 @@ def harvest_vmcore():
a60cd7
         else:
a60cd7
             break
a60cd7
 
a60cd7
-    os.umask(077)
a60cd7
-
a60cd7
     # Check abrt config files for copy/move settings and
a60cd7
     try:
a60cd7
         conf = problem.load_plugin_conf_file("vmcore.conf")
a60cd7
@@ -257,6 +210,8 @@ def harvest_vmcore():
a60cd7
                     "VMCore dir '%s' doesn't contain 'vmcore' file.\n" % f_full)
a60cd7
                 continue
a60cd7
 
a60cd7
+        # We use .new suffix - we must make sure abrtd doesn't try
a60cd7
+        # to process partially-copied directory.
a60cd7
         destdir = os.path.join(abrtdumpdir, ('vmcore-' + cfile))
a60cd7
         destdirnew = destdir + '.new'
a60cd7
         # Did we already copy it last time we booted?
a60cd7
@@ -264,38 +219,46 @@ def harvest_vmcore():
a60cd7
             continue
a60cd7
         if os.path.isdir(destdirnew):
a60cd7
             continue
a60cd7
-        # Copy/move vmcore directory to abrt spool dir.
a60cd7
-        # We use .new suffix - we must make sure abrtd doesn't try
a60cd7
-        # to process partially-copied directory.
a60cd7
-
a60cd7
-        try:
a60cd7
-            shutil.copytree(f_full, destdirnew)
a60cd7
-        except (OSError, shutil.Error):
a60cd7
-            sys.stderr.write("Unable to copy '%s' to '%s'. Skipping\n"
a60cd7
-                             % (f_full, destdirnew))
a60cd7
 
a60cd7
-            # delete .new dir so we don't create mess
a60cd7
-            shutil.rmtree(destdirnew)
a60cd7
+        # TODO: need to generate *real* UUID,
a60cd7
+        # one which has a real chance of catching dups!
a60cd7
+        # This one generates different hashes even for similar cores:
a60cd7
+        hashobj = hashlib.sha1()
a60cd7
+        # Iterate over the file a line at a time in order to not load the whole
a60cd7
+        # vmcore file
a60cd7
+        with open(os.path.join(f_full, 'vmcore'), 'r') as corefile:
a60cd7
+            for line in corefile:
a60cd7
+                hashobj.update(line)
a60cd7
+
a60cd7
+        dd = create_abrtd_info(destdirnew, hashobj.hexdigest())
a60cd7
+        if dd is None:
a60cd7
+            sys.stderr.write("Unable to create problem directory info")
a60cd7
             continue
a60cd7
 
a60cd7
-        try:
a60cd7
-            # Let abrtd know what type of problem it is:
a60cd7
-            create_abrtd_info(destdirnew)
a60cd7
-        except EnvironmentError as ex:
a60cd7
-            sys.stderr.write("Unable to create problem directory info: " + str(ex))
a60cd7
+        # Copy/move vmcore directory to abrt spool dir.
a60cd7
+        for name in os.listdir(f_full):
a60cd7
+            full_name = os.path.join(f_full, name)
a60cd7
+
a60cd7
+            # Skip sub-directories, abrt ignores them in its processing anyway
a60cd7
+            if not os.path.isfile(full_name):
a60cd7
+                continue
a60cd7
+
a60cd7
             try:
a60cd7
-                shutil.rmtree(destdirnew)
a60cd7
-            except Exception as ex:
a60cd7
-                sys.stderr.write("Unable to remove incomplete problem directory: " + str(ex))
a60cd7
-            continue
a60cd7
+                if not dd.copy_file(name, full_name) == 0:
a60cd7
+                    raise OSError
a60cd7
+            except (OSError, shutil.Error):
a60cd7
+                sys.stderr.write("Unable to copy '%s' to '%s'. Skipping\n"
a60cd7
+                                 % (full_name, destdirnew))
a60cd7
+                delete_and_close(dd)
a60cd7
+                continue
a60cd7
 
a60cd7
-        # chown -R 0:0
a60cd7
-        change_owner_rec(destdirnew)
a60cd7
-        # chmod -R u+rwX,go-rwxst
a60cd7
-        change_mode_rec(destdirnew)
a60cd7
+        # Get rid of the .new suffix
a60cd7
+        if not dd.rename(destdir) == 0:
a60cd7
+            sys.stderr.write("Unable to rename '%s' to '%s'. Skipping\n" % (destdirnew, destdir))
a60cd7
+            delete_and_close(dd)
a60cd7
+            continue
a60cd7
 
a60cd7
-        # Get rid of  the .new suffix
a60cd7
-        shutil.move(destdirnew, destdir)
a60cd7
+        dd.close()
a60cd7
 
a60cd7
         if copyvmcore == 'no':
a60cd7
             try:
a60cd7
-- 
a60cd7
1.8.3.1
a60cd7