summaryrefslogtreecommitdiffstats
path: root/src/responder/kcm/kcmsrv_op_queue.c
blob: ee1aa47ab629022bb726c4d5deb1eb1456124df1 (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
/*
   SSSD

   KCM Server - the KCM operations wait queue

   Copyright (C) Red Hat, 2017

   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 "util/util.h"
#include "util/util_creds.h"
#include "responder/kcm/kcmsrv_pvt.h"

#define QUEUE_HASH_SIZE      32

struct kcm_ops_queue_entry {
    struct tevent_req *req;

    struct kcm_ops_queue *queue;

    struct kcm_ops_queue_entry *next;
    struct kcm_ops_queue_entry *prev;
};

struct kcm_ops_queue {
    uid_t uid;
    struct tevent_context *ev;
    struct kcm_ops_queue_ctx *qctx;

    struct kcm_ops_queue_entry *head;
};

struct kcm_ops_queue_ctx {
    /* UID:kcm_ops_queue */
    hash_table_t *wait_queue_hash;
};

/*
 * Per-UID wait queue
 *
 * They key in the hash table is the UID of the peer. The value of each
 * hash table entry is kcm_ops_queue structure which in turn contains a
 * linked list of kcm_ops_queue_entry structures * which primarily hold the
 * tevent request being queued.
 */
struct kcm_ops_queue_ctx *kcm_ops_queue_create(TALLOC_CTX *mem_ctx)
{
    errno_t ret;
    struct kcm_ops_queue_ctx *queue_ctx;

    queue_ctx = talloc_zero(mem_ctx, struct kcm_ops_queue_ctx);
    if (queue_ctx == NULL) {
        return NULL;
    }

    ret = sss_hash_create_ex(mem_ctx, QUEUE_HASH_SIZE,
                             &queue_ctx->wait_queue_hash, 0, 0, 0, 0,
                             NULL, NULL);
    if (ret != EOK) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "sss_hash_create failed [%d]: %s\n", ret, sss_strerror(ret));
        talloc_free(queue_ctx);
        return NULL;
    }

    return queue_ctx;
}

void queue_removal_cb(struct tevent_context *ctx,
                      struct tevent_immediate *imm,
                      void *private_data)
{
    struct kcm_ops_queue *kq = talloc_get_type(private_data,
                                               struct kcm_ops_queue);
    int ret;
    hash_key_t key;

    talloc_free(imm);

    if (kq->head != NULL) {
        DEBUG(SSSDBG_TRACE_LIBS, "The queue is no longer empty\n");
        return;
    }

    key.type = HASH_KEY_ULONG;
    key.ul = kq->uid;

    /* If this was the last entry, remove the key (the UID) from the
     * hash table to signal the queue is empty
     */
    ret = hash_delete(kq->qctx->wait_queue_hash, &key);
    if (ret != HASH_SUCCESS) {
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Failed to remove wait queue for user %"SPRIuid"\n",
              kq->uid);
        return;
    }

    DEBUG(SSSDBG_FUNC_DATA,
          "Removed queue for %"SPRIuid" \n", kq->uid);
    talloc_free(kq);
}

static int kcm_op_queue_entry_destructor(struct kcm_ops_queue_entry *entry)
{
    struct kcm_ops_queue_entry *next_entry;
    struct tevent_immediate *imm;

    if (entry == NULL) {
        return 1;
    }

    /* Take the next entry from the queue */
    next_entry = entry->next;

    /* Remove the current entry from the queue */
    DLIST_REMOVE(entry->queue->head, entry);

    if (next_entry == NULL) {
        /* If there was no other entry, schedule removal of the queue. Do it
         * in another tevent tick to avoid issues with callbacks invoking
         * the descructor while another request is touching the queue
         */
        imm = tevent_create_immediate(entry->queue);
        if (imm == NULL) {
            return 1;
        }

        tevent_schedule_immediate(imm, entry->queue->ev, queue_removal_cb, entry->queue);
        return 0;
    }

    /* Otherwise, mark the current head as done to run the next request */
    tevent_req_done(next_entry->req);
    return 0;
}

static struct kcm_ops_queue *kcm_op_queue_get(struct kcm_ops_queue_ctx *qctx,
                                              struct tevent_context *ev,
                                              uid_t uid)
{
    errno_t ret;
    hash_key_t key;
    hash_value_t value;
    struct kcm_ops_queue *kq;

    key.type = HASH_KEY_ULONG;
    key.ul = uid;

    ret = hash_lookup(qctx->wait_queue_hash, &key, &value);
    switch (ret) {
    case HASH_SUCCESS:
        if (value.type != HASH_VALUE_PTR) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Unexpected hash value type.\n");
            return NULL;
        }

        kq = talloc_get_type(value.ptr, struct kcm_ops_queue);
        if (kq == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Invalid queue pointer\n");
            return NULL;
        }

        DEBUG(SSSDBG_TRACE_LIBS, "Found existing queue for this ID\n");
        break;

    case HASH_ERROR_KEY_NOT_FOUND:
        /* No request for this UID yet. Enqueue this request in case
         * another one comes in and return EOK to run the current request
         * immediately
         */
        DEBUG(SSSDBG_TRACE_LIBS, "No existing queue for this ID\n");

        kq = talloc_zero(qctx->wait_queue_hash, struct kcm_ops_queue);
        if (kq == NULL) {
            return NULL;
        }
        kq->uid = uid;
        kq->qctx = qctx;
        kq->ev = ev;

        value.type = HASH_VALUE_PTR;
        value.ptr = kq;

        ret = hash_enter(qctx->wait_queue_hash, &key, &value);
        if (ret != HASH_SUCCESS) {
            DEBUG(SSSDBG_CRIT_FAILURE, "hash_enter failed.\n");
            return NULL;
        }
        break;

    default:
        DEBUG(SSSDBG_CRIT_FAILURE, "hash_lookup failed.\n");
        return NULL;
    }

    return kq;
}

struct kcm_op_queue_state {
    struct kcm_ops_queue_entry *entry;
};

static errno_t kcm_op_queue_add_req(struct kcm_ops_queue *kq,
                                    struct tevent_req *req);

/*
 * Enqueue a request.
 *
 * If the request queue /for the given ID/ is empty, that is, if this
 * request is the first one in the queue, run the request immediately.
 *
 * Otherwise just add it to the queue and wait until the previous request
 * finishes and only at that point mark the current request as done, which
 * will trigger calling the recv function and allow the request to continue.
 */
struct tevent_req *kcm_op_queue_send(TALLOC_CTX *mem_ctx,
                                     struct tevent_context *ev,
                                     struct kcm_ops_queue_ctx *qctx,
                                     struct cli_creds *client)
{
    errno_t ret;
    struct tevent_req *req;
    struct kcm_ops_queue *kq;
    struct kcm_op_queue_state *state;
    uid_t uid;

    uid = cli_creds_get_uid(client);

    req = tevent_req_create(mem_ctx, &state, struct kcm_op_queue_state);
    if (req == NULL) {
        return NULL;
    }

    DEBUG(SSSDBG_FUNC_DATA,
          "Adding request by %"SPRIuid" to the wait queue\n", uid);

    kq = kcm_op_queue_get(qctx, ev, uid);
    if (kq == NULL) {
        ret = EIO;
        DEBUG(SSSDBG_OP_FAILURE,
              "Cannot get queue [%d]: %s\n", ret, sss_strerror(ret));
        goto immediate;
    }

    ret = kcm_op_queue_add_req(kq, req);
    if (ret == EOK) {
        DEBUG(SSSDBG_TRACE_LIBS,
              "Queue was empty, running the request immediately\n");
        goto immediate;
    } else if (ret != EAGAIN) {
        DEBUG(SSSDBG_OP_FAILURE,
              "Cannot enqueue request [%d]: %s\n", ret, sss_strerror(ret));
        goto immediate;
    }

    DEBUG(SSSDBG_TRACE_LIBS, "Waiting our turn in the queue\n");
    return req;

immediate:
    if (ret == EOK) {
        tevent_req_done(req);
    } else {
        tevent_req_error(req, ret);
    }
    tevent_req_post(req, ev);
    return req;
}

static errno_t kcm_op_queue_add_req(struct kcm_ops_queue *kq,
                                    struct tevent_req *req)
{
    errno_t ret;
    struct kcm_op_queue_state *state = tevent_req_data(req,
                                                struct kcm_op_queue_state);

    state->entry = talloc_zero(kq->qctx->wait_queue_hash, struct kcm_ops_queue_entry);
    if (state->entry == NULL) {
        return ENOMEM;
    }
    state->entry->req = req;
    state->entry->queue = kq;
    talloc_set_destructor(state->entry, kcm_op_queue_entry_destructor);

    if (kq->head == NULL) {
        /* First entry, will run callback at once */
        ret = EOK;
    } else {
        /* Will wait for the previous callbacks to finish */
        ret = EAGAIN;
    }

    DLIST_ADD_END(kq->head, state->entry, struct kcm_ops_queue_entry *);
    return ret;
}

/*
 * The queue recv function is called when this request is 'activated'. The queue
 * entry should be allocated on the same memory context as the enqueued request
 * to trigger freeing the kcm_ops_queue_entry structure destructor when the
 * parent request is done and its tevent_req freed. This would in turn unblock
 * the next request in the queue
 */
errno_t kcm_op_queue_recv(struct tevent_req *req,
                          TALLOC_CTX *mem_ctx,
                          struct kcm_ops_queue_entry **_entry)
{
    struct kcm_op_queue_state *state = tevent_req_data(req,
                                                struct kcm_op_queue_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);
    *_entry = talloc_steal(mem_ctx, state->entry);
    return EOK;
}