Blame SOURCES/0239-dump_dir-allow-semi-recursive-locking.patch

28bab8
From d4b89774005c569d7fd576d0d0efa6b3dc877a4f Mon Sep 17 00:00:00 2001
28bab8
From: Jakub Filak <jfilak@redhat.com>
28bab8
Date: Mon, 18 Mar 2019 12:26:37 +0100
28bab8
Subject: [PATCH] dump_dir: allow (semi)recursive locking
28bab8
28bab8
This patch only tries to mitigate the consequences of a bug in code
28bab8
where someone tries to lock a dump directory while it is already locked
28bab8
by the same process. This usually happens when a callee accepts a path
28bab8
to directory and opens it on its own or when someone forgets to call
28bab8
dd_unlock() or in all the unpredictable circumstance we usually have to
28bab8
face in ABRT.
28bab8
28bab8
It is not possible to implement the lock counter using only a symbolic
28bab8
link and file system functions, thus I've decided to put the
28bab8
responsibility of unlocking to the first dd_lock() caller and disallow
28bab8
the consecutive callers to unlock the dump directory.
28bab8
28bab8
Related to abrt/abrt#898
28bab8
28bab8
Signed-off-by: Jakub Filak <jfilak@redhat.com>
28bab8
---
28bab8
 src/include/dump_dir.h |  6 ++++++
28bab8
 src/lib/dump_dir.c     | 21 +++++++++++++-----
28bab8
 tests/dump_dir.at      | 49 ++++++++++++++++++++++++++++++++++++++++++
28bab8
 3 files changed, 71 insertions(+), 5 deletions(-)
28bab8
28bab8
diff --git a/src/include/dump_dir.h b/src/include/dump_dir.h
28bab8
index badef17..b617c6c 100644
28bab8
--- a/src/include/dump_dir.h
28bab8
+++ b/src/include/dump_dir.h
28bab8
@@ -71,6 +71,12 @@ struct dump_dir {
28bab8
     time_t dd_time;
28bab8
     char *dd_type;
28bab8
     int dd_fd;
28bab8
+
28bab8
+    /* In case of recursive locking the first caller owns the lock and is
28bab8
+     * responsible for unlocking. The consecutive dd_lock() callers acquire the
28bab8
+     * lock but are not able to unlock the dump directory.
28bab8
+     */
28bab8
+    int owns_lock;
28bab8
 };
28bab8
 
28bab8
 void dd_close(struct dump_dir *dd);
28bab8
diff --git a/src/lib/dump_dir.c b/src/lib/dump_dir.c
28bab8
index eb0c176..5e32c08 100644
28bab8
--- a/src/lib/dump_dir.c
28bab8
+++ b/src/lib/dump_dir.c
28bab8
@@ -277,6 +277,7 @@ int create_symlink_lockfile_at(int dir_fd, const char* lock_file,
28bab8
         if (strcmp(pid_buf, pid) == 0)
28bab8
         {
28bab8
             log("Lock file '%s' is already locked by us", lock_file);
28bab8
+            errno = EALREADY;
28bab8
             return 0;
28bab8
         }
28bab8
         if (isdigit_str(pid_buf))
28bab8
@@ -359,7 +360,7 @@ static int dd_lock(struct dump_dir *dd, unsigned sleep_usec, int flags)
28bab8
         int r = create_symlink_lockfile_at(dd->dd_fd, ".lock", pid_buf, log_all_warnings);
28bab8
         if (r < 0)
28bab8
             return r; /* error */
28bab8
-        if (r > 0)
28bab8
+        if (r > 0 || EALREADY == errno)
28bab8
             break; /* locked successfully */
28bab8
         if (flags & DD_DONT_WAIT_FOR_LOCK)
28bab8
         {
28bab8
@@ -371,6 +372,12 @@ static int dd_lock(struct dump_dir *dd, unsigned sleep_usec, int flags)
28bab8
         log_all_warnings = false;
28bab8
     }
28bab8
 
28bab8
+    /* Reset errno to 0 only if errno is EALREADY (used by
28bab8
+     * create_symlink_lockfile() to signal that the dump directory is already
28bab8
+     * locked by us) */
28bab8
+    if (!(dd->owns_lock = (errno != EALREADY)))
28bab8
+        errno = 0;
28bab8
+
28bab8
     /* Are we called by dd_opendir (as opposed to dd_create)? */
28bab8
     if (sleep_usec == WAIT_FOR_OTHER_PROCESS_USLEEP) /* yes */
28bab8
     {
28bab8
@@ -382,8 +389,10 @@ static int dd_lock(struct dump_dir *dd, unsigned sleep_usec, int flags)
28bab8
          */
28bab8
         if (missing_file)
28bab8
         {
28bab8
-            xunlinkat(dd->dd_fd, ".lock", /*only files*/0);
28bab8
-            log_notice("Unlocked '%s' (no or corrupted '%s' file)", dd->dd_dirname, missing_file);
28bab8
+            if (dd->owns_lock)
28bab8
+                xunlinkat(dd->dd_fd, ".lock", /*only files*/0);
28bab8
+
28bab8
+            log_warning("Unlocked '%s/.lock' (no or corrupted '%s' file)", dd->dd_dirname, missing_file);
28bab8
             if (--count == 0 || flags & DD_DONT_WAIT_FOR_LOCK)
28bab8
             {
28bab8
                 errno = EISDIR; /* "this is an ordinary dir, not dump dir" */
28bab8
@@ -402,9 +411,11 @@ static void dd_unlock(struct dump_dir *dd)
28bab8
 {
28bab8
     if (dd->locked)
28bab8
     {
28bab8
-        dd->locked = 0;
28bab8
+        if (dd->owns_lock)
28bab8
+            xunlinkat(dd->dd_fd, ".lock", /*only files*/0);
28bab8
 
28bab8
-        xunlinkat(dd->dd_fd, ".lock", /*only files*/0);
28bab8
+        dd->owns_lock = 0;
28bab8
+        dd->locked = 0;
28bab8
 
28bab8
         log_info("Unlocked '%s/.lock'", dd->dd_dirname);
28bab8
     }
28bab8
diff --git a/tests/dump_dir.at b/tests/dump_dir.at
28bab8
index dc95e5b..98d564c 100644
28bab8
--- a/tests/dump_dir.at
28bab8
+++ b/tests/dump_dir.at
28bab8
@@ -566,3 +566,52 @@ TS_MAIN
28bab8
 }
28bab8
 TS_RETURN_MAIN
28bab8
 ]])
28bab8
+
28bab8
+
28bab8
+## -------------- ##
28bab8
+## recursive_lock ##
28bab8
+## -------------- ##
28bab8
+
28bab8
+AT_TESTFUN([recursive_lock],
28bab8
+[[
28bab8
+#include "internal_libreport.h"
28bab8
+#include <errno.h>
28bab8
+#include <assert.h>
28bab8
+
28bab8
+int main(int argc, char **argv)
28bab8
+{
28bab8
+    g_verbose = 3;
28bab8
+
28bab8
+    char *path = tmpnam(NULL);
28bab8
+    struct dump_dir *dd = dd_create(path, -1L, DEFAULT_DUMP_DIR_MODE);
28bab8
+
28bab8
+    char *lock_path = concat_path_file(path, ".lock");
28bab8
+    struct stat buf;
28bab8
+
28bab8
+    assert(dd);
28bab8
+
28bab8
+    assert(lstat(lock_path, &buf) == 0 && S_ISLNK(buf.st_mode));
28bab8
+
28bab8
+    dd_create_basic_files(dd, -1L, "/");
28bab8
+    dd_save_text(dd, "type", "custom");
28bab8
+
28bab8
+    struct dump_dir *dd2 = dd_opendir(path, DD_OPEN_READONLY);
28bab8
+    assert(dd2->owns_lock == 0);
28bab8
+
28bab8
+    struct dump_dir *dd3 = dd_opendir(path, 0);
28bab8
+    assert(dd3->owns_lock == 0);
28bab8
+
28bab8
+    dd_close(dd2);
28bab8
+    assert(lstat(lock_path, &buf) == 0 && S_ISLNK(buf.st_mode));
28bab8
+
28bab8
+    dd_close(dd3);
28bab8
+    assert(lstat(lock_path, &buf) == 0 && S_ISLNK(buf.st_mode));
28bab8
+
28bab8
+    dd_close(dd);
28bab8
+
28bab8
+    assert(stat(lock_path, &buf) != 0 && errno == ENOENT);
28bab8
+    free(lock_path);
28bab8
+
28bab8
+    return 0;
28bab8
+}
28bab8
+]])
28bab8
-- 
28bab8
2.21.0
28bab8