Blob Blame History Raw
From 484507bf20d27afd700d52c67651e6f08d1da1a3 Mon Sep 17 00:00:00 2001
From: Alexey Tikhonov <atikhono@redhat.com>
Date: Wed, 8 Jul 2020 11:34:12 +0200
Subject: [PATCH 35/35] mem-cache: always cleanup old content

(Try to) cleanup old files even if currently mem-cache is disabled.

Reviewed-by: Sumit Bose <sbose@redhat.com>
---
 src/responder/nss/nsssrv.c            | 98 ++++++++++-----------------
 src/responder/nss/nsssrv_mmap_cache.c | 74 ++++++++++++--------
 2 files changed, 79 insertions(+), 93 deletions(-)

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