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

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