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