>From 802b4a78611fd512fe7050a5cb339aa8c7d1fc09 Mon Sep 17 00:00:00 2001
From: Michal Zidek <mzidek@redhat.com>
Date: Tue, 20 Nov 2012 13:52:12 +0100
Subject: [PATCH] sss_cache: Small refactor.

The logic that checks if sssd_nss is running and then
sends SIGHUP to monitor or removes the caches was moved
to a function sss_memcache_clear_all() and made public in
tools_util.h.
---
 src/tools/sss_cache.c  | 61 +++------------------------------------------
 src/tools/tools_util.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/tools/tools_util.h |  2 ++
 3 files changed, 72 insertions(+), 58 deletions(-)

diff --git a/src/tools/sss_cache.c b/src/tools/sss_cache.c
index 368b1df..9305db5 100644
--- a/src/tools/sss_cache.c
+++ b/src/tools/sss_cache.c
@@ -92,7 +92,6 @@ errno_t invalidate_entry(TALLOC_CTX *ctx, struct sysdb_ctx *sysdb,
 bool invalidate_entries(TALLOC_CTX *ctx, struct sysdb_ctx *sysdb,
                         enum sss_cache_entry entry_type, const char *filter,
                         const char *name);
-static int clear_fastcache(bool *sssd_nss_is_off);
 
 int main(int argc, const char *argv[])
 {
@@ -100,9 +99,7 @@ int main(int argc, const char *argv[])
     struct cache_tool_ctx *tctx = NULL;
     struct sysdb_ctx *sysdb;
     int i;
-    bool sssd_nss_is_off;
     bool skipped = true;
-    FILE *clear_mc_flag;
 
     ret = init_context(argc, argv, &tctx);
     if (ret != EOK) {
@@ -145,71 +142,19 @@ int main(int argc, const char *argv[])
         ret = ENOENT;
         goto done;
     } else {
-        ret = clear_fastcache(&sssd_nss_is_off);
+        ret = sss_memcache_clear_all();
         if (ret != EOK) {
-            DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to clear caches.\n"));
+            DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to clear memory cache.\n"));
             goto done;
         }
-        if (!sssd_nss_is_off) {
-            /* sssd_nss is running -> signal monitor to invalidate fastcache */
-            clear_mc_flag = fopen(SSS_NSS_MCACHE_DIR"/"CLEAR_MC_FLAG, "w");
-            if (clear_mc_flag == NULL) {
-                DEBUG(SSSDBG_CRIT_FAILURE,
-                      ("Failed to create clear_mc_flag file. "
-                       "Memory cache will not be cleared.\n"));
-                goto done;
-            }
-            ret = fclose(clear_mc_flag);
-            if (ret != 0) {
-                ret = errno;
-                DEBUG(SSSDBG_CRIT_FAILURE,
-                      ("Unable to close file descriptor: %s\n",
-                       strerror(ret)));
-                goto done;
-            }
-
-            DEBUG(SSSDBG_TRACE_FUNC, ("Sending SIGHUP to monitor.\n"));
-            ret = signal_sssd(SIGHUP);
-            if (ret != EOK) {
-                DEBUG(SSSDBG_CRIT_FAILURE,
-                      ("Failed to send SIGHUP to monitor.\n"));
-                goto done;
-            }
-        }
     }
 
+    ret = EOK;
 done:
     if (tctx) talloc_free(tctx);
     return ret;
 }
 
-static int clear_fastcache(bool *sssd_nss_is_off)
-{
-    int ret;
-    ret = sss_memcache_invalidate(SSS_NSS_MCACHE_DIR"/passwd");
-    if (ret != EOK) {
-        if (ret == EACCES) {
-            *sssd_nss_is_off = false;
-            return EOK;
-        } else {
-            return ret;
-        }
-    }
-
-    ret = sss_memcache_invalidate(SSS_NSS_MCACHE_DIR"/group");
-    if (ret != EOK) {
-        if (ret == EACCES) {
-            *sssd_nss_is_off = false;
-            return EOK;
-        } else {
-            return ret;
-        }
-    }
-
-    *sssd_nss_is_off = true;
-    return EOK;
-}
-
 bool invalidate_entries(TALLOC_CTX *ctx, struct sysdb_ctx *sysdb,
                         enum sss_cache_entry entry_type, const char *filter,
                         const char *name)
diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c
index 73e9413..406077c 100644
--- a/src/tools/tools_util.c
+++ b/src/tools/tools_util.c
@@ -766,3 +766,70 @@ done:
     }
     return ret;
 }
+
+static int clear_fastcache(bool *sssd_nss_is_off)
+{
+    int ret;
+    ret = sss_memcache_invalidate(SSS_NSS_MCACHE_DIR"/passwd");
+    if (ret != EOK) {
+        if (ret == EACCES) {
+            *sssd_nss_is_off = false;
+            return EOK;
+        } else {
+            return ret;
+        }
+    }
+
+    ret = sss_memcache_invalidate(SSS_NSS_MCACHE_DIR"/group");
+    if (ret != EOK) {
+        if (ret == EACCES) {
+            *sssd_nss_is_off = false;
+            return EOK;
+        } else {
+            return ret;
+        }
+    }
+
+    *sssd_nss_is_off = true;
+    return EOK;
+}
+
+errno_t sss_memcache_clear_all(void)
+{
+    errno_t ret;
+    bool sssd_nss_is_off = false;
+    FILE *clear_mc_flag;
+
+    ret = clear_fastcache(&sssd_nss_is_off);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to clear caches.\n"));
+        return EIO;
+    }
+    if (!sssd_nss_is_off) {
+        /* sssd_nss is running -> signal monitor to invalidate fastcache */
+        clear_mc_flag = fopen(SSS_NSS_MCACHE_DIR"/"CLEAR_MC_FLAG, "w");
+        if (clear_mc_flag == NULL) {
+            DEBUG(SSSDBG_CRIT_FAILURE,
+                  ("Failed to create clear_mc_flag file. "
+                   "Memory cache will not be cleared.\n"));
+            return EIO;
+        }
+        ret = fclose(clear_mc_flag);
+        if (ret != 0) {
+            ret = errno;
+            DEBUG(SSSDBG_CRIT_FAILURE,
+                  ("Unable to close file descriptor: %s\n",
+                   strerror(ret)));
+            return EIO;
+        }
+        DEBUG(SSSDBG_TRACE_FUNC, ("Sending SIGHUP to monitor.\n"));
+        ret = signal_sssd(SIGHUP);
+        if (ret != EOK) {
+            DEBUG(SSSDBG_CRIT_FAILURE,
+                  ("Failed to send SIGHUP to monitor.\n"));
+            return EIO;
+        }
+    }
+
+    return EOK;
+}
diff --git a/src/tools/tools_util.h b/src/tools/tools_util.h
index a83c8ee..20a2a4a 100644
--- a/src/tools/tools_util.h
+++ b/src/tools/tools_util.h
@@ -106,6 +106,8 @@ errno_t signal_sssd(int signum);
 
 errno_t sss_memcache_invalidate(const char *mc_filename);
 
+errno_t sss_memcache_clear_all(void);
+
 /* from files.c */
 int remove_tree(const char *root);
 
-- 
1.7.11.2

