dpward / rpms / sssd

Forked from rpms/sssd 3 years ago
Clone

Blame SOURCES/0035-mem-cache-always-cleanup-old-content.patch

1bb595
From 484507bf20d27afd700d52c67651e6f08d1da1a3 Mon Sep 17 00:00:00 2001
1bb595
From: Alexey Tikhonov <atikhono@redhat.com>
1bb595
Date: Wed, 8 Jul 2020 11:34:12 +0200
1bb595
Subject: [PATCH 35/35] mem-cache: always cleanup old content
1bb595
1bb595
(Try to) cleanup old files even if currently mem-cache is disabled.
1bb595
1bb595
Reviewed-by: Sumit Bose <sbose@redhat.com>
1bb595
---
1bb595
 src/responder/nss/nsssrv.c            | 98 ++++++++++-----------------
1bb595
 src/responder/nss/nsssrv_mmap_cache.c | 74 ++++++++++++--------
1bb595
 2 files changed, 79 insertions(+), 93 deletions(-)
1bb595
1bb595
diff --git a/src/responder/nss/nsssrv.c b/src/responder/nss/nsssrv.c
1bb595
index 741e94aaa..ffb1ca29d 100644
1bb595
--- a/src/responder/nss/nsssrv.c
1bb595
+++ b/src/responder/nss/nsssrv.c
1bb595
@@ -242,12 +242,6 @@ static int setup_memcaches(struct nss_ctx *nctx)
1bb595
         return ret;
1bb595
     }
1bb595
 
1bb595
-    if (memcache_timeout == 0) {
1bb595
-        DEBUG(SSSDBG_CONF_SETTINGS,
1bb595
-              "Fast in-memory cache will not be initialized.");
1bb595
-        return EOK;
1bb595
-    }
1bb595
-
1bb595
     /* Get all memcache sizes from confdb (pwd, grp, initgr) */
1bb595
 
1bb595
     ret = confdb_get_int(nctx->rctx->cdb,
1bb595
@@ -288,64 +282,40 @@ static int setup_memcaches(struct nss_ctx *nctx)
1bb595
 
1bb595
     /* Initialize the fast in-memory caches if they were not disabled */
1bb595
 
1bb595
-    if (mc_size_passwd != 0) {
1bb595
-        ret = sss_mmap_cache_init(nctx, "passwd",
1bb595
-                                  nctx->mc_uid, nctx->mc_gid,
1bb595
-                                  SSS_MC_PASSWD,
1bb595
-                                  mc_size_passwd * SSS_MC_CACHE_SLOTS_PER_MB,
1bb595
-                                  (time_t)memcache_timeout,
1bb595
-                                  &nctx->pwd_mc_ctx);
1bb595
-        if (ret) {
1bb595
-            DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
-                  "Failed to initialize passwd mmap cache: '%s'\n",
1bb595
-                  sss_strerror(ret));
1bb595
-        } else {
1bb595
-            DEBUG(SSSDBG_CONF_SETTINGS, "Passwd mmap cache size is %d\n",
1bb595
-                  mc_size_passwd);
1bb595
-        }
1bb595
-    } else {
1bb595
-        DEBUG(SSSDBG_IMPORTANT_INFO,
1bb595
-              "Passwd mmap cache is explicitly DISABLED\n");
1bb595
-    }
1bb595
-
1bb595
-    if (mc_size_group != 0) {
1bb595
-        ret = sss_mmap_cache_init(nctx, "group",
1bb595
-                                  nctx->mc_uid, nctx->mc_gid,
1bb595
-                                  SSS_MC_GROUP,
1bb595
-                                  mc_size_group * SSS_MC_CACHE_SLOTS_PER_MB,
1bb595
-                                  (time_t)memcache_timeout,
1bb595
-                                  &nctx->grp_mc_ctx);
1bb595
-        if (ret) {
1bb595
-            DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
-                  "Failed to initialize group mmap cache: '%s'\n",
1bb595
-                  sss_strerror(ret));
1bb595
-        } else {
1bb595
-            DEBUG(SSSDBG_CONF_SETTINGS, "Group mmap cache size is %d\n",
1bb595
-                  mc_size_group);
1bb595
-        }
1bb595
-    } else {
1bb595
-        DEBUG(SSSDBG_IMPORTANT_INFO,
1bb595
-              "Group mmap cache is explicitly DISABLED\n");
1bb595
-    }
1bb595
-
1bb595
-    if (mc_size_initgroups != 0) {
1bb595
-        ret = sss_mmap_cache_init(nctx, "initgroups",
1bb595
-                                  nctx->mc_uid, nctx->mc_gid,
1bb595
-                                  SSS_MC_INITGROUPS,
1bb595
-                                  mc_size_initgroups * SSS_MC_CACHE_SLOTS_PER_MB,
1bb595
-                                  (time_t)memcache_timeout,
1bb595
-                                  &nctx->initgr_mc_ctx);
1bb595
-        if (ret) {
1bb595
-            DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
-                  "Failed to initialize initgroups mmap cache: '%s'\n",
1bb595
-                  sss_strerror(ret));
1bb595
-        } else {
1bb595
-            DEBUG(SSSDBG_CONF_SETTINGS, "Initgroups mmap cache size is %d\n",
1bb595
-                  mc_size_initgroups);
1bb595
-        }
1bb595
-    } else {
1bb595
-        DEBUG(SSSDBG_IMPORTANT_INFO,
1bb595
-              "Initgroups mmap cache is explicitly DISABLED\n");
1bb595
+    ret = sss_mmap_cache_init(nctx, "passwd",
1bb595
+                              nctx->mc_uid, nctx->mc_gid,
1bb595
+                              SSS_MC_PASSWD,
1bb595
+                              mc_size_passwd * SSS_MC_CACHE_SLOTS_PER_MB,
1bb595
+                              (time_t)memcache_timeout,
1bb595
+                              &nctx->pwd_mc_ctx);
1bb595
+    if (ret) {
1bb595
+        DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
+              "Failed to initialize passwd mmap cache: '%s'\n",
1bb595
+              sss_strerror(ret));
1bb595
+    }
1bb595
+
1bb595
+    ret = sss_mmap_cache_init(nctx, "group",
1bb595
+                              nctx->mc_uid, nctx->mc_gid,
1bb595
+                              SSS_MC_GROUP,
1bb595
+                              mc_size_group * SSS_MC_CACHE_SLOTS_PER_MB,
1bb595
+                              (time_t)memcache_timeout,
1bb595
+                              &nctx->grp_mc_ctx);
1bb595
+    if (ret) {
1bb595
+        DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
+              "Failed to initialize group mmap cache: '%s'\n",
1bb595
+              sss_strerror(ret));
1bb595
+    }
1bb595
+
1bb595
+    ret = sss_mmap_cache_init(nctx, "initgroups",
1bb595
+                              nctx->mc_uid, nctx->mc_gid,
1bb595
+                              SSS_MC_INITGROUPS,
1bb595
+                              mc_size_initgroups * SSS_MC_CACHE_SLOTS_PER_MB,
1bb595
+                              (time_t)memcache_timeout,
1bb595
+                              &nctx->initgr_mc_ctx);
1bb595
+    if (ret) {
1bb595
+        DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
+              "Failed to initialize initgroups mmap cache: '%s'\n",
1bb595
+              sss_strerror(ret));
1bb595
     }
1bb595
 
1bb595
     return EOK;
1bb595
diff --git a/src/responder/nss/nsssrv_mmap_cache.c b/src/responder/nss/nsssrv_mmap_cache.c
1bb595
index 71919e4ac..f66e76ce4 100644
1bb595
--- a/src/responder/nss/nsssrv_mmap_cache.c
1bb595
+++ b/src/responder/nss/nsssrv_mmap_cache.c
1bb595
@@ -1108,48 +1108,48 @@ static errno_t sss_mc_set_recycled(int fd)
1bb595
     return EOK;
1bb595
 }
1bb595
 
1bb595
-/*
1bb595
- * When we (re)create a new file we must mark the current file as recycled
1bb595
- * so active clients will abandon its use ASAP.
1bb595
- * We unlink the current file and make a new one.
1bb595
- */
1bb595
-static errno_t sss_mc_create_file(struct sss_mc_ctx *mc_ctx)
1bb595
+static void sss_mc_destroy_file(const char *filename)
1bb595
 {
1bb595
-    mode_t old_mask;
1bb595
+    const useconds_t t = 50000;
1bb595
+    const int retries = 3;
1bb595
     int ofd;
1bb595
-    int ret, uret;
1bb595
-    useconds_t t = 50000;
1bb595
-    int retries = 3;
1bb595
+    int ret;
1bb595
 
1bb595
-    ofd = open(mc_ctx->file, O_RDWR);
1bb595
+    ofd = open(filename, O_RDWR);
1bb595
     if (ofd != -1) {
1bb595
         ret = sss_br_lock_file(ofd, 0, 1, retries, t);
1bb595
         if (ret != EOK) {
1bb595
-            DEBUG(SSSDBG_FATAL_FAILURE,
1bb595
-                  "Failed to lock file %s.\n", mc_ctx->file);
1bb595
+            DEBUG(SSSDBG_FATAL_FAILURE, "Failed to lock file %s.\n", filename);
1bb595
         }
1bb595
         ret = sss_mc_set_recycled(ofd);
1bb595
         if (ret) {
1bb595
             DEBUG(SSSDBG_FATAL_FAILURE, "Failed to mark mmap file %s as"
1bb595
-                                         " recycled: %d(%s)\n",
1bb595
-                                         mc_ctx->file, ret, strerror(ret));
1bb595
+                                         " recycled: %d (%s)\n",
1bb595
+                                         filename, ret, strerror(ret));
1bb595
         }
1bb595
-
1bb595
         close(ofd);
1bb595
     } else if (errno != ENOENT) {
1bb595
         ret = errno;
1bb595
         DEBUG(SSSDBG_CRIT_FAILURE,
1bb595
-              "Failed to open old memory cache file %s: %d(%s).\n",
1bb595
-               mc_ctx->file, ret, strerror(ret));
1bb595
+              "Failed to open old memory cache file %s: %d (%s)\n",
1bb595
+               filename, ret, strerror(ret));
1bb595
     }
1bb595
 
1bb595
     errno = 0;
1bb595
-    ret = unlink(mc_ctx->file);
1bb595
+    ret = unlink(filename);
1bb595
     if (ret == -1 && errno != ENOENT) {
1bb595
         ret = errno;
1bb595
-        DEBUG(SSSDBG_TRACE_FUNC, "Failed to rm mmap file %s: %d(%s)\n",
1bb595
-                                  mc_ctx->file, ret, strerror(ret));
1bb595
+        DEBUG(SSSDBG_TRACE_FUNC, "Failed to delete mmap file %s: %d (%s)\n",
1bb595
+                                  filename, ret, strerror(ret));
1bb595
     }
1bb595
+}
1bb595
+
1bb595
+static errno_t sss_mc_create_file(struct sss_mc_ctx *mc_ctx)
1bb595
+{
1bb595
+    const useconds_t t = 50000;
1bb595
+    const int retries = 3;
1bb595
+    mode_t old_mask;
1bb595
+    int ret, uret;
1bb595
 
1bb595
     /* temporarily relax umask as we need the file to be readable
1bb595
      * by everyone for now */
1bb595
@@ -1276,9 +1276,32 @@ errno_t sss_mmap_cache_init(TALLOC_CTX *mem_ctx, const char *name,
1bb595
 
1bb595
     struct sss_mc_ctx *mc_ctx = NULL;
1bb595
     int ret, dret;
1bb595
+    char *filename;
1bb595
+
1bb595
+    filename = talloc_asprintf(mem_ctx, "%s/%s", SSS_NSS_MCACHE_DIR, name);
1bb595
+    if (!filename) {
1bb595
+        return ENOMEM;
1bb595
+    }
1bb595
+    /*
1bb595
+     * First of all mark the current file as recycled
1bb595
+     * and unlink so active clients will abandon its use ASAP
1bb595
+     */
1bb595
+    sss_mc_destroy_file(filename);
1bb595
+
1bb595
+    if ((timeout == 0) || (n_elem == 0)) {
1bb595
+        DEBUG(SSSDBG_IMPORTANT_INFO,
1bb595
+              "Fast '%s' mmap cache is explicitly DISABLED\n",
1bb595
+              mc_type_to_str(type));
1bb595
+        *mcc = NULL;
1bb595
+        return EOK;
1bb595
+    }
1bb595
+    DEBUG(SSSDBG_CONF_SETTINGS,
1bb595
+          "Fast '%s' mmap cache: timeout = %d, slots = %zu\n",
1bb595
+          mc_type_to_str(type), (int)timeout, n_elem);
1bb595
 
1bb595
     mc_ctx = talloc_zero(mem_ctx, struct sss_mc_ctx);
1bb595
     if (!mc_ctx) {
1bb595
+        talloc_free(filename);
1bb595
         return ENOMEM;
1bb595
     }
1bb595
     mc_ctx->fd = -1;
1bb595
@@ -1297,12 +1320,7 @@ errno_t sss_mmap_cache_init(TALLOC_CTX *mem_ctx, const char *name,
1bb595
 
1bb595
     mc_ctx->valid_time_slot = timeout;
1bb595
 
1bb595
-    mc_ctx->file = talloc_asprintf(mc_ctx, "%s/%s",
1bb595
-                                   SSS_NSS_MCACHE_DIR, name);
1bb595
-    if (!mc_ctx->file) {
1bb595
-        ret = ENOMEM;
1bb595
-        goto done;
1bb595
-    }
1bb595
+    mc_ctx->file = talloc_steal(mc_ctx, filename);
1bb595
 
1bb595
     /* elements must always be multiple of 8 to make things easier to handle,
1bb595
      * so we increase by the necessary amount if they are not a multiple */
1bb595
@@ -1320,8 +1338,6 @@ errno_t sss_mmap_cache_init(TALLOC_CTX *mem_ctx, const char *name,
1bb595
                         MC_ALIGN64(mc_ctx->ht_size);
1bb595
 
1bb595
 
1bb595
-    /* for now ALWAYS create a new file on restart */
1bb595
-
1bb595
     ret = sss_mc_create_file(mc_ctx);
1bb595
     if (ret) {
1bb595
         goto done;
1bb595
-- 
1bb595
2.21.3
1bb595