summaryrefslogtreecommitdiffstats
path: root/src/providers/dp_ptask.c
blob: 5a14e60b246b6c5de8c4290810b5b73bc7a4c3d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2013 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <tevent.h>
#include <talloc.h>
#include <time.h>
#include <string.h>

#include "util/util.h"
#include "providers/dp_backend.h"
#include "providers/dp_ptask_private.h"
#include "providers/dp_ptask.h"

enum be_ptask_schedule {
    BE_PTASK_SCHEDULE_FROM_NOW,
    BE_PTASK_SCHEDULE_FROM_LAST
};

static void be_ptask_schedule(struct be_ptask *task,
                              time_t delay,
                              enum be_ptask_schedule from);

static int be_ptask_destructor(void *pvt)
{
    struct be_ptask *task;

    task = talloc_get_type(pvt, struct be_ptask);
    if (task == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "BUG: task is NULL\n");
        return 0;
    }

    DEBUG(SSSDBG_TRACE_FUNC, "Terminating periodic task [%s]\n", task->name);

    return 0;
}

static void be_ptask_online_cb(void *pvt)
{
    struct be_ptask *task = NULL;

    task = talloc_get_type(pvt, struct be_ptask);
    if (task == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "BUG: task is NULL\n");
        return;
    }

    DEBUG(SSSDBG_TRACE_FUNC, "Back end is online\n");
    be_ptask_enable(task);
}

static void be_ptask_offline_cb(void *pvt)
{
    struct be_ptask *task = NULL;
    task = talloc_get_type(pvt, struct be_ptask);

    DEBUG(SSSDBG_TRACE_FUNC, "Back end is offline\n");
    be_ptask_disable(task);
}

static void be_ptask_timeout(struct tevent_context *ev,
                             struct tevent_timer *tt,
                             struct timeval tv,
                             void *pvt)
{
    struct be_ptask *task = NULL;
    task = talloc_get_type(pvt, struct be_ptask);

    DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: timed out\n", task->name);

    talloc_zfree(task->req);
    be_ptask_schedule(task, task->period, BE_PTASK_SCHEDULE_FROM_NOW);
}

static void be_ptask_done(struct tevent_req *req);

static void be_ptask_execute(struct tevent_context *ev,
                             struct tevent_timer *tt,
                             struct timeval tv,
                             void *pvt)
{
    struct be_ptask *task = NULL;
    struct tevent_timer *timeout = NULL;

    task = talloc_get_type(pvt, struct be_ptask);
    task->timer = NULL; /* timer is freed by tevent */

    if (be_is_offline(task->be_ctx)) {
        DEBUG(SSSDBG_TRACE_FUNC, "Back end is offline\n");
        switch (task->offline) {
        case BE_PTASK_OFFLINE_SKIP:
            be_ptask_schedule(task, task->period, BE_PTASK_SCHEDULE_FROM_NOW);
            return;
        case BE_PTASK_OFFLINE_DISABLE:
            /* This case is normally handled by offline callback but we
             * should handle it here as well since we can get here in some
             * special cases for example unit tests or tevent events order. */
            be_ptask_disable(task);
            return;
        case BE_PTASK_OFFLINE_EXECUTE:
            /* continue */
            break;
        }
    }

    DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: executing task, timeout %lu "
                              "seconds\n", task->name, task->timeout);

    task->last_execution = time(NULL);

    task->req = task->send_fn(task, task->ev, task->be_ctx, task, task->pvt);
    if (task->req == NULL) {
        /* skip this iteration and try again later */
        DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed to execute task, "
              "will try again later\n", task->name);

        be_ptask_schedule(task, task->period, BE_PTASK_SCHEDULE_FROM_NOW);
        return;
    }

    tevent_req_set_callback(task->req, be_ptask_done, task);

    /* schedule timeout */
    if (task->timeout > 0) {
        tv = tevent_timeval_current_ofs(task->timeout, 0);
        timeout = tevent_add_timer(task->ev, task->req, tv,
                                   be_ptask_timeout, task);
        if (timeout == NULL) {
            /* If we can't guarantee a timeout,
             * we need to cancel the request. */
            talloc_zfree(task->req);

            DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed to set timeout, "
                  "the task will be rescheduled\n", task->name);

            be_ptask_schedule(task, task->period, BE_PTASK_SCHEDULE_FROM_NOW);
        }
    }

    return;
}

static void be_ptask_done(struct tevent_req *req)
{
    struct be_ptask *task = NULL;
    errno_t ret;

    task = tevent_req_callback_data(req, struct be_ptask);

    ret = task->recv_fn(req);
    talloc_zfree(req);
    task->req = NULL;
    switch (ret) {
    case EOK:
        DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: finished successfully\n",
                                  task->name);

        be_ptask_schedule(task, task->period, BE_PTASK_SCHEDULE_FROM_LAST);
        break;
    default:
        DEBUG(SSSDBG_OP_FAILURE, "Task [%s]: failed with [%d]: %s\n",
                                  task->name, ret, sss_strerror(ret));

        be_ptask_schedule(task, task->period, BE_PTASK_SCHEDULE_FROM_NOW);
        break;
    }
}

static void be_ptask_schedule(struct be_ptask *task,
                              time_t delay,
                              enum be_ptask_schedule from)
{
    struct timeval tv;

    if (!task->enabled) {
        DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: disabled\n", task->name);
        return;
    }

    if (task->allow_backoff) {
        if (task->backoff_delay == 0) {
            task->backoff_delay = task->period;
        } else {
            task->backoff_delay = (task->backoff_delay * 2 > task->max_backoff)
                                  ? task->max_backoff
                                  : task->backoff_delay * 2;
            delay = task->backoff_delay;
        }
    }

    if (task->random_offset != 0) {
        delay = delay + (rand_r(&task->ro_seed) % task->random_offset);
    }

    switch (from) {
    case BE_PTASK_SCHEDULE_FROM_NOW:
        tv = tevent_timeval_current_ofs(delay, 0);

        DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: scheduling task %lu seconds "
              "from now [%lu]\n", task->name, delay, tv.tv_sec);
        break;
    case BE_PTASK_SCHEDULE_FROM_LAST:
        tv = tevent_timeval_set(task->last_execution + delay, 0);

        DEBUG(SSSDBG_TRACE_FUNC, "Task [%s]: scheduling task %lu seconds "
                                  "from last execution time [%lu]\n",
                                  task->name, delay, tv.tv_sec);
        break;
    }

    if (task->timer != NULL) {
        DEBUG(SSSDBG_MINOR_FAILURE, "Task [%s]: another timer is already "
                                     "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);
    }
}

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->enabled_delay = enabled_delay;
    task->random_offset = random_offset;
    task->ro_seed = time(NULL) * getpid();
    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;
    }

    if (max_backoff != 0) {
        task->max_backoff = max_backoff;
        task->allow_backoff = true;
    }

    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", 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", ret, sss_strerror(ret));
            goto done;
        }
    }

    DEBUG(SSSDBG_TRACE_FUNC, "Periodic task [%s] was created\n", task->name);

    be_ptask_schedule(task, 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, task->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;

    if (task->allow_backoff) {
        task->backoff_delay = 0;
    }
}

void be_ptask_destroy(struct be_ptask **task)
{
    talloc_zfree(*task);
}

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 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;
    }

    if (_task != NULL) {
        talloc_steal(*_task, ctx);
    }

    ret = EOK;

done:
    if (ret != EOK) {
        talloc_free(ctx);
    }

    return ret;
}