From fcafeb7e3f15f3fda8156fa4b074415f4693b819 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Thu, 24 Oct 2013 11:17:43 +0200
Subject: [PATCH 08/10] dp: convert cleanup task to be_ptask

Resolves:
https://fedorahosted.org/sssd/ticket/1968
---
 src/providers/ldap/ldap_common.c     |  22 ++-----
 src/providers/ldap/ldap_common.h     |   6 +-
 src/providers/ldap/ldap_id_cleanup.c | 111 +++++++++++++++++------------------
 src/providers/ldap/sdap.h            |   2 +
 4 files changed, 66 insertions(+), 75 deletions(-)

diff --git a/src/providers/ldap/ldap_common.c b/src/providers/ldap/ldap_common.c
index b3a048c1cb7336cec0a5bbfebd3509c56637bed3..c0bd8186c992f98aa65acbe529ab1d262c280fa9 100644
--- a/src/providers/ldap/ldap_common.c
+++ b/src/providers/ldap/ldap_common.c
@@ -951,29 +951,19 @@ int sdap_id_setup_tasks(struct sdap_id_ctx *ctx,
                         be_ptask_send_t send_fn,
                         be_ptask_recv_t recv_fn)
 {
-    struct timeval tv;
-    int ret = EOK;
-    int delay;
+    int ret;
 
     /* set up enumeration task */
     if (sdom->dom->enumerate) {
-        DEBUG(SSSDBG_TRACE_FUNC, ("Setting up enumeration for %s\n", sdom->dom->name));
+        DEBUG(SSSDBG_TRACE_FUNC, ("Setting up enumeration for %s\n",
+                                  sdom->dom->name));
         ret = ldap_setup_enumeration(ctx, conn, sdom, send_fn, recv_fn);
     } else {
         /* the enumeration task, runs the cleanup process by itself,
          * but if enumeration is not running we need to schedule it */
-        delay = dp_opt_get_int(ctx->opts->basic, SDAP_CACHE_PURGE_TIMEOUT);
-        if (delay == 0) {
-            /* Cleanup has been explicitly disabled, so we won't
-             * schedule any cleanup tasks.
-             */
-            return EOK;
-        }
-
-        /* run the first one in a couple of seconds so that we have time to
-         * finish initializations first*/
-        tv = tevent_timeval_current_ofs(10, 0);
-        ret = ldap_id_cleanup_create_timer(ctx, sdom, tv);
+        DEBUG(SSSDBG_TRACE_FUNC, ("Setting up cleanup task for %s\n",
+                                  sdom->dom->name));
+        ret = ldap_setup_cleanup(ctx, sdom);
     }
 
     return ret;
diff --git a/src/providers/ldap/ldap_common.h b/src/providers/ldap/ldap_common.h
index 0d565fc63b7e949e628ae5cf079e54c134464e77..de87de057f54e3d36f1e585c63690b82f92cc642 100644
--- a/src/providers/ldap/ldap_common.h
+++ b/src/providers/ldap/ldap_common.h
@@ -193,11 +193,11 @@ ldap_enumeration_send(TALLOC_CTX *mem_ctx,
                       void *pvt);
 errno_t ldap_enumeration_recv(struct tevent_req *req);
 
+errno_t ldap_setup_cleanup(struct sdap_id_ctx *id_ctx,
+                           struct sdap_domain *sdom);
+
 errno_t ldap_id_cleanup(struct sdap_options *opts,
                         struct sdap_domain *sdom);
-int ldap_id_cleanup_create_timer(struct sdap_id_ctx *ctx,
-                                 struct sdap_domain *sdom,
-                                 struct timeval tv);
 
 void sdap_mark_offline(struct sdap_id_ctx *ctx);
 
diff --git a/src/providers/ldap/ldap_id_cleanup.c b/src/providers/ldap/ldap_id_cleanup.c
index 06cdae0a7181def2f3d782dc0b4b077274466dfe..36ef6f42036e500ebbf71b4e9a50a120610f241f 100644
--- a/src/providers/ldap/ldap_id_cleanup.c
+++ b/src/providers/ldap/ldap_id_cleanup.c
@@ -38,74 +38,73 @@ struct ldap_id_cleanup_ctx {
     struct sdap_domain *sdom;
 };
 
-static errno_t ldap_id_cleanup_set_timer(struct ldap_id_cleanup_ctx *cctx,
-                                         struct timeval tv);
+static errno_t ldap_cleanup_task(TALLOC_CTX *mem_ctx,
+                                 struct tevent_context *ev,
+                                 struct be_ctx *be_ctx,
+                                 struct be_ptask *be_ptask,
+                                 void *pvt)
+{
+    struct ldap_id_cleanup_ctx *cleanup_ctx = NULL;
+
+    cleanup_ctx = talloc_get_type(pvt, struct ldap_id_cleanup_ctx);
+    return ldap_id_cleanup(cleanup_ctx->ctx->opts, cleanup_ctx->sdom);
+}
 
-static void ldap_id_cleanup_timer(struct tevent_context *ev,
-                                  struct tevent_timer *tt,
-                                  struct timeval tv, void *pvt)
+errno_t ldap_setup_cleanup(struct sdap_id_ctx *id_ctx,
+                           struct sdap_domain *sdom)
 {
-    struct ldap_id_cleanup_ctx *cctx = talloc_get_type(pvt,
-                                                struct ldap_id_cleanup_ctx);
-    int delay;
     errno_t ret;
-
-    if (be_is_offline(cctx->ctx->be)) {
-        DEBUG(SSSDBG_TRACE_FUNC, ("Backend is marked offline, retry later!\n"));
-        /* schedule starting from now, not the last run */
-        delay = dp_opt_get_int(cctx->ctx->opts->basic,
-                               SDAP_CACHE_PURGE_TIMEOUT);
-        tv = tevent_timeval_current_ofs(delay, 0);
-        ldap_id_cleanup_set_timer(cctx, tv);
-        return;
-    }
-
-    ret = ldap_id_cleanup(cctx->ctx->opts, cctx->sdom);
-    if (ret != EOK) {
-        /* On error schedule starting from now, not the last run */
-        tv = tevent_timeval_current();
-    } else {
-        tv = cctx->sdom->last_purge;
+    time_t first_delay;
+    time_t period;
+    struct ldap_id_cleanup_ctx *cleanup_ctx = NULL;
+    char *name = NULL;
+
+    period = dp_opt_get_int(id_ctx->opts->basic, SDAP_CACHE_PURGE_TIMEOUT);
+    if (period == 0) {
+        /* Cleanup has been explicitly disabled, so we won't
+         * create any cleanup tasks. */
+        ret = EOK;
+        goto done;
     }
 
-    delay = dp_opt_get_int(cctx->ctx->opts->basic, SDAP_CACHE_PURGE_TIMEOUT);
-    tv = tevent_timeval_add(&tv, delay, 0);
-    ldap_id_cleanup_set_timer(cctx, tv);
-}
+    /* Run the first one in a couple of seconds so that we have time to
+     * finish initializations first. */
+    first_delay = 10;
 
-static errno_t ldap_id_cleanup_set_timer(struct ldap_id_cleanup_ctx *cctx,
-                                         struct timeval tv)
-{
-    struct tevent_timer *cleanup_task;
-
-    cleanup_task = tevent_add_timer(cctx->ctx->be->ev, cctx->ctx,
-                                    tv, ldap_id_cleanup_timer, cctx);
-    if (cleanup_task == NULL) {
-        DEBUG(SSSDBG_CRIT_FAILURE, ("FATAL: failed to setup cleanup task!\n"));
-        return EFAULT;
+    cleanup_ctx = talloc_zero(sdom, struct ldap_id_cleanup_ctx);
+    if (cleanup_ctx == NULL) {
+        ret = ENOMEM;
+        goto done;
     }
 
-    return EOK;
-}
-
-int ldap_id_cleanup_create_timer(struct sdap_id_ctx *ctx,
-                                 struct sdap_domain *sdom,
-                                 struct timeval tv)
-{
-    struct ldap_id_cleanup_ctx *cctx;
-
-    DEBUG(SSSDBG_FUNC_DATA,
-          ("Scheduling next cleanup at %ld.%ld\n",
-          (long)tv.tv_sec, (long)tv.tv_usec));
+    cleanup_ctx->ctx = id_ctx;
+    cleanup_ctx->sdom = sdom;
 
-    cctx = talloc(ctx, struct ldap_id_cleanup_ctx);
-    if (cctx == NULL) {
+    name = talloc_asprintf(cleanup_ctx, "Cleanup of %s", sdom->dom->name);
+    if (name == NULL) {
         return ENOMEM;
     }
-    cctx->ctx = ctx;
-    cctx->sdom = sdom;
 
-    return ldap_id_cleanup_set_timer(cctx, tv);
+    ret = be_ptask_create_sync(sdom, id_ctx->be, period, first_delay,
+                               5 /* enabled delay */, period /* timeout */,
+                               BE_PTASK_OFFLINE_SKIP, ldap_cleanup_task,
+                               cleanup_ctx, name, &sdom->cleanup_task);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_FATAL_FAILURE, ("Unable to initialize cleanup periodic "
+                                     "task for %s\n", sdom->dom->name));
+        goto done;
+    }
+
+    talloc_steal(sdom->cleanup_task, cleanup_ctx);
+    ret = EOK;
+
+done:
+    talloc_free(name);
+    if (ret != EOK) {
+        talloc_free(cleanup_ctx);
+    }
+
+    return ret;
 }
 
 static int cleanup_users(struct sdap_options *opts,
diff --git a/src/providers/ldap/sdap.h b/src/providers/ldap/sdap.h
index 0e886e9804f5ac8a1b782ee2d49b19973476079d..abc9052f64ee93cf9cc279fcdab423e576a98424 100644
--- a/src/providers/ldap/sdap.h
+++ b/src/providers/ldap/sdap.h
@@ -386,6 +386,8 @@ struct sdap_domain {
 
     /* Enumeration and cleanup periodic task */
     struct be_ptask *enum_task;
+    struct be_ptask *cleanup_task;
+
     /* enumeration loop timer */
     struct timeval last_enum;
     /* cleanup loop timer */
-- 
1.7.11.7

