dp_ptask.c revision e89c2cb5ec77d57ed93952dae08df51738834faf
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes/*
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes Authors:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes Pavel Březina <pbrezina@redhat.com>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes Copyright (C) 2013 Red Hat
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes This program is free software; you can redistribute it and/or modify
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes it under the terms of the GNU General Public License as published by
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes the Free Software Foundation; either version 3 of the License, or
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes (at your option) any later version.
0662ed52e814f8f08ef0e09956413a792584eddffuankg
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes This program is distributed in the hope that it will be useful,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes but WITHOUT ANY WARRANTY; without even the implied warranty of
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes GNU General Public License for more details.
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes You should have received a copy of the GNU General Public License
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes along with this program. If not, see <http://www.gnu.org/licenses/>.
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes*/
70953fb44a7140fe206c3a5f011e24209c8c5c6abnicholes
16b55a35cff91315d261d1baa776138af465c4e4fuankg#include <tevent.h>
16b55a35cff91315d261d1baa776138af465c4e4fuankg#include <talloc.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include <time.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include <string.h>
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include "util/util.h"
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include "providers/dp_backend.h"
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include "providers/dp_ptask_private.h"
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#include "providers/dp_ptask.h"
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes#define backoff_allowed(ptask) (ptask->max_backoff != 0)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesenum be_ptask_schedule {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_SCHEDULE_FROM_NOW,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_SCHEDULE_FROM_LAST
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes};
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesenum be_ptask_delay {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_FIRST_DELAY,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_ENABLED_DELAY,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_PERIOD
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes};
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_schedule(struct be_ptask *task,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes enum be_ptask_delay delay_type,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes enum be_ptask_schedule from);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic int be_ptask_destructor(void *pvt)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct be_ptask *task;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task = talloc_get_type(pvt, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (task == NULL) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_FATAL_FAILURE, "BUG: task is NULL\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return 0;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg DEBUG(SSSDBG_TRACE_FUNC, "Terminating periodic task [%s]\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return 0;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_online_cb(void *pvt)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct be_ptask *task = NULL;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task = talloc_get_type(pvt, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (task == NULL) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_FATAL_FAILURE, "BUG: task is NULL\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Back end is online\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_enable(task);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_offline_cb(void *pvt)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct be_ptask *task = NULL;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task = talloc_get_type(pvt, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Back end is offline\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_disable(task);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_timeout(struct tevent_context *ev,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct tevent_timer *tt,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct timeval tv,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes void *pvt)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct be_ptask *task = NULL;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task = talloc_get_type(pvt, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: timed out\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes talloc_zfree(task->req);
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
0a39e7683f6611d66c55712f50bb240428d832a1bnicholesstatic void be_ptask_done(struct tevent_req *req);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_execute(struct tevent_context *ev,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct tevent_timer *tt,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct timeval tv,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes void *pvt)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct be_ptask *task = NULL;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct tevent_timer *timeout = NULL;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
0662ed52e814f8f08ef0e09956413a792584eddffuankg task = talloc_get_type(pvt, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->timer = NULL; /* timer is freed by tevent */
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (be_is_offline(task->be_ctx)) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Back end is offline\n");
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes switch (task->offline) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case BE_PTASK_OFFLINE_SKIP:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case BE_PTASK_OFFLINE_DISABLE:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* This case is normally handled by offline callback but we
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * should handle it here as well since we can get here in some
0662ed52e814f8f08ef0e09956413a792584eddffuankg * special cases for example unit tests or tevent events order. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_disable(task);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case BE_PTASK_OFFLINE_EXECUTE:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* continue */
0662ed52e814f8f08ef0e09956413a792584eddffuankg break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: executing task, timeout %lu "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes "seconds\n", task->name, task->timeout);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->last_execution = tv.tv_sec;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->req = task->send_fn(task, task->ev, task->be_ctx, task, task->pvt);
0662ed52e814f8f08ef0e09956413a792584eddffuankg if (task->req == NULL) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* skip this iteration and try again later */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed to execute task, "
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg "will try again later\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes tevent_req_set_callback(task->req, be_ptask_done, task);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* schedule timeout */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (task->timeout > 0) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes tv = tevent_timeval_current_ofs(task->timeout, 0);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes timeout = tevent_add_timer(task->ev, task->req, tv,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_timeout, task);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (timeout == NULL) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* If we can't guarantee a timeout,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes * we need to cancel the request. */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes talloc_zfree(task->req);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed to set timeout, "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes "the task will be rescheduled\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_done(struct tevent_req *req)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct be_ptask *task = NULL;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg errno_t ret;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task = tevent_req_callback_data(req, struct be_ptask);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes ret = task->recv_fn(req);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes talloc_zfree(req);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->req = NULL;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes switch (ret) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case EOK:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: finished successfully\n",
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_LAST);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes default:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed with [%d]: %s\n",
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg task->name, ret, sss_strerror(ret));
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes be_ptask_schedule(task, BE_PTASK_PERIOD, BE_PTASK_SCHEDULE_FROM_NOW);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes}
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholesstatic void be_ptask_schedule(struct be_ptask *task,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes enum be_ptask_delay delay_type,
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes enum be_ptask_schedule from)
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes{
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes struct timeval tv = { 0, };
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes time_t delay = 0;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (!task->enabled) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: disabled\n", task->name);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes return;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg switch (delay_type) {
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg case BE_PTASK_FIRST_DELAY:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes delay = task->first_delay;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case BE_PTASK_ENABLED_DELAY:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes delay = task->enabled_delay;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg case BE_PTASK_PERIOD:
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg delay = task->period;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (backoff_allowed(task) && task->period * 2 <= task->max_backoff) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* double the period for the next execution */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->period *= 2;
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes /* add random offset */
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes if (task->random_offset != 0) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes delay = delay + (rand_r(&task->ro_seed) % task->random_offset);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes switch (from) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case BE_PTASK_SCHEDULE_FROM_NOW:
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg tv = tevent_timeval_current_ofs(delay, 0);
0662ed52e814f8f08ef0e09956413a792584eddffuankg
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: scheduling task %lu seconds "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes "from now [%lu]\n", task->name, delay, tv.tv_sec);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes case BE_PTASK_SCHEDULE_FROM_LAST:
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes tv = tevent_timeval_set(task->last_execution + delay, 0);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: scheduling task %lu seconds "
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes "from last execution time [%lu]\n",
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes task->name, delay, tv.tv_sec);
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes break;
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes }
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes
0662ed52e814f8f08ef0e09956413a792584eddffuankg if (task->timer != NULL) {
3c937b528ca923d5b51e63def9f888af4a77bb40bnicholes DEBUG(SSSDBG_MINOR_FAILURE, "Task [%s]: another timer is already "
ac7985784d08a3655291f24f711812b4d8b1cbcffuankg "active?\n", task->name);
talloc_zfree(task->timer);
}
task->timer = tevent_add_timer(task->ev, task, tv, be_ptask_execute, task);
if (task->timer == NULL) {
/* nothing we can do about it */
DEBUG(SSSDBG_CRIT_FAILURE, "FATAL: Unable to schedule task [%s]\n",
task->name);
be_ptask_disable(task);
}
task->next_execution = tv.tv_sec;
}
errno_t be_ptask_create(TALLOC_CTX *mem_ctx,
struct be_ctx *be_ctx,
time_t period,
time_t first_delay,
time_t enabled_delay,
time_t random_offset,
time_t timeout,
enum be_ptask_offline offline,
time_t max_backoff,
be_ptask_send_t send_fn,
be_ptask_recv_t recv_fn,
void *pvt,
const char *name,
struct be_ptask **_task)
{
struct be_ptask *task = NULL;
errno_t ret;
if (be_ctx == NULL || period == 0 || send_fn == NULL || recv_fn == NULL
|| name == NULL) {
return EINVAL;
}
task = talloc_zero(mem_ctx, struct be_ptask);
if (task == NULL) {
ret = ENOMEM;
goto done;
}
task->ev = be_ctx->ev;
task->be_ctx = be_ctx;
task->period = period;
task->orig_period = period;
task->first_delay = first_delay;
task->enabled_delay = enabled_delay;
task->random_offset = random_offset;
task->ro_seed = time(NULL) * getpid();
task->max_backoff = max_backoff;
task->timeout = timeout;
task->offline = offline;
task->send_fn = send_fn;
task->recv_fn = recv_fn;
task->pvt = pvt;
task->name = talloc_strdup(task, name);
if (task->name == NULL) {
ret = ENOMEM;
goto done;
}
task->enabled = true;
talloc_set_destructor((TALLOC_CTX*)task, be_ptask_destructor);
if (offline == BE_PTASK_OFFLINE_DISABLE) {
/* install offline and online callbacks */
ret = be_add_online_cb(task, be_ctx, be_ptask_online_cb, task, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
"Unable to install online callback [%d]: %s\n",
ret, sss_strerror(ret));
goto done;
}
ret = be_add_offline_cb(task, be_ctx, be_ptask_offline_cb, task, NULL);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE,
"Unable to install offline callback [%d]: %s\n",
ret, sss_strerror(ret));
goto done;
}
}
DEBUG(SSSDBG_TRACE_FUNC, "Periodic task [%s] was created\n", task->name);
be_ptask_schedule(task, BE_PTASK_FIRST_DELAY, BE_PTASK_SCHEDULE_FROM_NOW);
if (_task != NULL) {
*_task = task;
}
ret = EOK;
done:
if (ret != EOK) {
talloc_free(task);
}
return ret;
}
void be_ptask_enable(struct be_ptask *task)
{
if (task->enabled) {
DEBUG(SSSDBG_MINOR_FAILURE, "Task [%s]: already enabled\n",
task->name);
return;
}
DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: enabling task\n", task->name);
task->enabled = true;
be_ptask_schedule(task, BE_PTASK_ENABLED_DELAY, BE_PTASK_SCHEDULE_FROM_NOW);
}
/* Disable the task, but if a request already in progress, let it finish. */
void be_ptask_disable(struct be_ptask *task)
{
DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: disabling task\n", task->name);
talloc_zfree(task->timer);
task->enabled = false;
task->period = task->orig_period;
}
void be_ptask_destroy(struct be_ptask **task)
{
talloc_zfree(*task);
}
time_t be_ptask_get_period(struct be_ptask *task)
{
return task->period;
}
time_t be_ptask_get_timeout(struct be_ptask *task)
{
return task->timeout;
}
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 random_offset,
time_t timeout,
enum be_ptask_offline offline,
time_t max_backoff,
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, random_offset, timeout, offline,
max_backoff, be_ptask_sync_send, be_ptask_sync_recv,
ctx, name, _task);
if (ret != EOK) {
goto done;
}
ret = EOK;
done:
if (ret != EOK) {
talloc_free(ctx);
}
return ret;
}