From bc0d2a41b8e7b8549ddb3161596013bfef91fd77 Mon Sep 17 00:00:00 2001 From: Pavel Březina Date: Thu, 24 Oct 2013 11:30:09 +0200 Subject: be_ptask: add be_ptask_create_sync() This is a wrapper around be_ptask_create() that allows to create synchronous periodic tasks. Resolves: https://fedorahosted.org/sssd/ticket/1968 --- src/providers/dp_ptask.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++ src/providers/dp_ptask.h | 23 ++++++++++++ 2 files changed, 117 insertions(+) (limited to 'src/providers') diff --git a/src/providers/dp_ptask.c b/src/providers/dp_ptask.c index d0f7c6d97..44a5e148a 100644 --- a/src/providers/dp_ptask.c +++ b/src/providers/dp_ptask.c @@ -352,3 +352,97 @@ time_t be_ptask_get_period(struct be_ptask *task) { return task->period; } + +struct be_ptask_sync_ctx { + be_ptask_sync_t fn; + void *pvt; +}; + +struct be_ptask_sync_state { + int dummy; +}; + +/* This is not an asynchronous request so there is not any _done function. */ +static struct tevent_req * +be_ptask_sync_send(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt) +{ + struct be_ptask_sync_ctx *ctx = NULL; + struct be_ptask_sync_state *state = NULL; + struct tevent_req *req = NULL; + errno_t ret; + + req = tevent_req_create(mem_ctx, &state, struct be_ptask_sync_state); + if (req == NULL) { + DEBUG(SSSDBG_CRIT_FAILURE, ("tevent_req_create() failed\n")); + return NULL; + } + + ctx = talloc_get_type(pvt, struct be_ptask_sync_ctx); + ret = ctx->fn(mem_ctx, ev, be_ctx, be_ptask, ctx->pvt); + + if (ret == EOK) { + tevent_req_done(req); + } else { + tevent_req_error(req, ret); + } + tevent_req_post(req, ev); + + return req; +} + +static errno_t be_ptask_sync_recv(struct tevent_req *req) +{ + TEVENT_REQ_RETURN_ON_ERROR(req); + + return EOK; +} + +errno_t be_ptask_create_sync(TALLOC_CTX *mem_ctx, + struct be_ctx *be_ctx, + time_t period, + time_t first_delay, + time_t enabled_delay, + time_t timeout, + enum be_ptask_offline offline, + be_ptask_sync_t fn, + void *pvt, + const char *name, + struct be_ptask **_task) +{ + errno_t ret; + struct be_ptask_sync_ctx *ctx = NULL; + + ctx = talloc_zero(mem_ctx, struct be_ptask_sync_ctx); + if (ctx == NULL) { + ret = ENOMEM; + goto done; + } + + ctx->fn = fn; + ctx->pvt = pvt; + + ret = be_ptask_create(mem_ctx, be_ctx, period, first_delay, + enabled_delay, timeout, offline, + be_ptask_sync_send, be_ptask_sync_recv, + ctx, name, _task); + if (ret != EOK) { + goto done; + } + + if (_task != NULL) { + talloc_steal(*_task, ctx); + } + + ret = EOK; + +done: + if (ret != EOK) { + talloc_free(ctx); + } + + return ret; +} diff --git a/src/providers/dp_ptask.h b/src/providers/dp_ptask.h index 6a241fb7a..11324db74 100644 --- a/src/providers/dp_ptask.h +++ b/src/providers/dp_ptask.h @@ -60,6 +60,17 @@ typedef struct tevent_req * typedef errno_t (*be_ptask_recv_t)(struct tevent_req *req); +/** + * If EOK, task will be scheduled again to 'last_execution_time + period'. + * If other error code, task will be rescheduled to 'now + period'. + */ +typedef errno_t +(*be_ptask_sync_t)(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct be_ctx *be_ctx, + struct be_ptask *be_ptask, + void *pvt); + /** * The first execution is scheduled first_delay seconds after the task is * created. @@ -85,6 +96,18 @@ errno_t be_ptask_create(TALLOC_CTX *mem_ctx, const char *name, struct be_ptask **_task); +errno_t be_ptask_create_sync(TALLOC_CTX *mem_ctx, + struct be_ctx *be_ctx, + time_t period, + time_t first_delay, + time_t enabled_delay, + time_t timeout, + enum be_ptask_offline offline, + be_ptask_sync_t fn, + void *pvt, + const char *name, + struct be_ptask **_task); + void be_ptask_enable(struct be_ptask *task); void be_ptask_disable(struct be_ptask *task); void be_ptask_destroy(struct be_ptask **task); -- cgit