summaryrefslogtreecommitdiffstats
path: root/src/sbus/router/sbus_router_hash.c
blob: 2d407b2fba12b6b57eed896e7154f85b0a65dcde (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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2017 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 <dhash.h>
#include <string.h>
#include <talloc.h>

#include "util/util.h"
#include "util/dlinklist.h"
#include "sbus/sbus_opath.h"
#include "sbus/sbus_private.h"
#include "util/sss_ptr_hash.h"

static struct sbus_interface *
sbus_interface_list_lookup(struct sbus_interface_list *list,
                           const char *name)
{
    struct sbus_interface_list *item;

    DLIST_FOR_EACH(item, list) {
        if (strcmp(item->interface->name, name) == 0) {
            return item->interface;
        }
    }

    return NULL;
}

static errno_t
sbus_interface_list_copy(TALLOC_CTX *mem_ctx,
                         struct sbus_interface_list *list,
                         struct sbus_interface_list **_copy)
{
    TALLOC_CTX *list_ctx;
    struct sbus_interface_list *list_copy;
    struct sbus_interface_list *item_copy;
    struct sbus_interface_list *item;
    struct sbus_interface *iface;
    errno_t ret;

    if (list == NULL) {
        *_copy = NULL;
        return EOK;
    }

    /* Create a memory context that will be used as a parent for copies. */
    list_ctx = talloc_new(mem_ctx);
    if (list_ctx == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
        return ENOMEM;
    }

    /* Start with an empty list. */
    list_copy = NULL;
    DLIST_FOR_EACH(item, list) {
        iface = sbus_interface_list_lookup(list_copy, item->interface->name);
        if (iface != NULL) {
            /* This interface already exist in the list. */
            continue;
        }

        /* Create a copy of this item and insert it into the list. */
        item_copy = talloc_zero(list_ctx, struct sbus_interface_list);
        if (item_copy == NULL) {
            ret = ENOMEM;
            goto done;
        }

        item_copy->interface = item->interface;
        DLIST_ADD(list_copy, item_copy);
    }

    *_copy = list_copy;
    ret = EOK;

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

    return ret;
}

hash_table_t *
sbus_router_paths_init(TALLOC_CTX *mem_ctx)
{
    return sss_ptr_hash_create(mem_ctx, NULL, NULL);
}

errno_t
sbus_router_paths_add(hash_table_t *table,
                      const char *path,
                      struct sbus_interface *iface)
{
    TALLOC_CTX *tmp_ctx;
    struct sbus_interface_list *list;
    struct sbus_interface_list *item;
    errno_t ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
        return ENOMEM;
    }

    item = talloc_zero(tmp_ctx, struct sbus_interface_list);
    if (item == NULL) {
        ret = ENOMEM;
        goto done;
    }

    item->interface = sbus_interface_copy(item, iface);
    if (item->interface == NULL) {
        ret = ENOMEM;
        goto done;
    }

    /* First, check if the path already exist and just append the interface
     * to the list if it does (but only if the interface does not exist). */
    list = sss_ptr_hash_lookup(table, path, struct sbus_interface_list);
    if (list != NULL) {
        if (sbus_interface_list_lookup(list, iface->name) != NULL) {
            DEBUG(SSSDBG_MINOR_FAILURE, "Trying to register the same interface"
                  " twice: iface=%s, opath=%s\n", iface->name, path);
            ret = EEXIST;
            goto done;
        }

        DLIST_ADD_END(list, item, struct sbus_interface_list *);
        ret = EOK;
        goto done;
    }

    /* Otherwise create new hash entry and new list. */
    list = item;

    ret = sss_ptr_hash_add(table, path, list, struct sbus_interface_list);

done:
    if (ret == EOK) {
        talloc_steal(table, item);
    }

    talloc_free(tmp_ctx);

    return ret;
}

/**
 * First @object_path is looked up in @table, if it is not found it steps up
 * in the path hierarchy and try to lookup the parent node. This continues
 * until the root is reached.
 */
struct sbus_interface *
sbus_router_paths_lookup(hash_table_t *table,
                         const char *path,
                         const char *iface_name)
{
    TALLOC_CTX *tmp_ctx;
    struct sbus_interface_list *list;
    struct sbus_interface *iface;
    const char *lookup_path;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        return NULL;
    }

    iface = NULL;
    lookup_path = path;
    while (lookup_path != NULL) {
        list = sss_ptr_hash_lookup(table, lookup_path,
                                   struct sbus_interface_list);
        if (list != NULL) {
            iface = sbus_interface_list_lookup(list, iface_name);
            if (iface != NULL) {
                goto done;
            }
        }

        /* We will not free lookup path since it is freed with tmp_ctx
         * and the object paths are supposed to be small. */
        lookup_path = sbus_opath_subtree_parent(tmp_ctx, lookup_path);
    }

done:
    talloc_free(tmp_ctx);
    return iface;
}

/**
 * Acquire list of all interfaces that are supported on given object path.
 */
errno_t
sbus_router_paths_supported(TALLOC_CTX *mem_ctx,
                            hash_table_t *table,
                            const char *path,
                            struct sbus_interface_list **_list)
{
    TALLOC_CTX *tmp_ctx;
    TALLOC_CTX *list_ctx;
    struct sbus_interface_list *list;
    struct sbus_interface_list *list_copy;
    struct sbus_interface_list *list_output;
    const char *lookup_path;
    errno_t ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        return ENOMEM;
    }

    list_ctx = talloc_new(tmp_ctx);
    if (list_ctx == NULL) {
        ret = ENOMEM;
        goto done;
    }

    /* Start with an empty list. */
    list_output = NULL;
    lookup_path = path;
    while (lookup_path != NULL) {
        list = sss_ptr_hash_lookup(table, lookup_path,
                                   struct sbus_interface_list);
        if (list != NULL) {
            ret = sbus_interface_list_copy(list_ctx, list, &list_copy);
            if (ret != EOK) {
                goto done;
            }

            DLIST_CONCATENATE(list_output, list_copy,
                              struct sbus_interface_list *);
        }

        /* We will not free lookup path since it is freed with tmp_ctx
         * and the object paths are supposed to be small. */
        lookup_path = sbus_opath_subtree_parent(tmp_ctx, lookup_path);
    }

    talloc_steal(mem_ctx, list_ctx);
    *_list = list_output;
    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

const char **
sbus_router_paths_nodes(TALLOC_CTX *mem_ctx,
                        hash_table_t *table)
{
    const char **paths = NULL;
    hash_key_t *keys;
    unsigned long count;
    unsigned long i, j;
    char *basepath;
    errno_t ret;
    int hret;

    hret = hash_keys(table, &count, &keys);
    if (hret != HASH_SUCCESS) {
        return NULL;
    }

    paths = talloc_zero_array(mem_ctx, const char *, count + 2);
    if (paths == NULL) {
        ret = ENOMEM;
        goto done;
    }

    for (i = 0, j = 0; i < count; i++) {
        /* Do not include subtree paths. The must have node factory. */
        basepath = keys[i].str;
        if (sbus_opath_is_subtree(basepath)) {
            basepath = sbus_opath_subtree_base(paths, basepath);
            if (basepath == NULL) {
                ret = ENOMEM;
                goto done;
            }

            if (sbus_router_paths_exist(table, basepath)) {
                talloc_free(basepath);
                continue;
            }
        }

        if (strcmp(basepath, "/") == 0) {
            continue;
        }

        /* All paths starts with / that is not part of the node name. */
        paths[j] = basepath + 1;
        j++;
    }

    ret = EOK;

done:
    talloc_free(keys);

    if (ret != EOK) {
        talloc_zfree(paths);
    }

    return paths;
}

bool
sbus_router_paths_exist(hash_table_t *table,
                        const char *object_path)
{
    return sss_ptr_hash_has_key(table, object_path);
}

static struct sbus_listener *
sbus_listener_list_lookup(struct sbus_listener_list *list,
                          struct sbus_listener *a)
{
    struct sbus_listener_list *item;
    struct sbus_listener *b;

    /* We know that interface and signal name already match. We need to check
     * handlers and object paths. */
    DLIST_FOR_EACH(item, list) {
        b = item->listener;

        if (memcmp(&a->handler, &b->handler, sizeof(struct sbus_handler)) != 0) {
            continue;
        }

        if (a->object_path == NULL && b->object_path == NULL) {
            return b;
        }

        if (a->object_path == NULL && b->object_path != NULL) {
            continue;
        }

        if (a->object_path != NULL && b->object_path == NULL) {
            continue;
        }

        if (strcmp(a->object_path, b->object_path) != 0) {
            continue;
        }

        return b;
    }

    return NULL;
}

static void
sbus_router_listeners_delete_cb(hash_entry_t *item,
                                hash_destroy_enum deltype,
                                void *pvt)
{
    struct sbus_connection *conn;
    char *signal_name;
    char *interface;
    char *rule;
    errno_t ret;

    conn = talloc_get_type(pvt, struct sbus_connection);
    if (conn->connection == NULL) {
        return;
    }

    if (conn->disconnecting) {
        return;
    }

    /* If we still have the D-Bus connection available, we try to unregister
     * the previously registered listener when its removed from table. */

    ret = sbus_router_signal_parse(NULL, item->key.str,
                                   &interface, &signal_name);
    if (ret != EOK) {
        /* There is nothing we can do. */
        return;
    }

    rule = sbus_router_signal_rule(NULL, interface, signal_name);
    talloc_free(interface);
    talloc_free(signal_name);
    if (rule == NULL) {
        /* There is nothing we can do. */
        return;
    }

    dbus_bus_remove_match(conn->connection, rule, NULL);

    talloc_free(rule);
}

hash_table_t *
sbus_router_listeners_init(TALLOC_CTX *mem_ctx,
                           struct sbus_connection *conn)
{
    return sss_ptr_hash_create(mem_ctx, sbus_router_listeners_delete_cb, conn);
}

errno_t
sbus_router_listeners_add(hash_table_t *table,
                          const char *interface,
                          const char *signal_name,
                          struct sbus_listener *listener,
                          bool *_signal_known)
{
    TALLOC_CTX *tmp_ctx;
    struct sbus_listener_list *list;
    struct sbus_listener_list *item;
    bool signal_known = false;
    const char *key;
    errno_t ret;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
        return ENOMEM;
    }

    key = talloc_asprintf(tmp_ctx, "%s.%s", interface, signal_name);
    if (key == NULL) {
        ret = ENOMEM;
        goto done;
    }

    item = talloc_zero(tmp_ctx, struct sbus_listener_list);
    if (item == NULL) {
        ret = ENOMEM;
        goto done;
    }

    item->listener = sbus_listener_copy(item, listener);
    if (item->listener == NULL) {
        ret = ENOMEM;
        goto done;
    }

    /* First, check if the listener already exist and just append it to the
     * list if it does (but only if this listener doesn't already exist. */
    list = sss_ptr_hash_lookup(table, key, struct sbus_listener_list);
    if (list != NULL) {
        signal_known = true;

        if (sbus_listener_list_lookup(list, listener) != NULL) {
            DEBUG(SSSDBG_MINOR_FAILURE, "Trying to register the same listener"
                  " twice: iface=%s, signal=%s, path=%s\n",
                  interface, signal_name, (listener->object_path == NULL ?
                                           "<null>": listener->object_path));
            ret = EEXIST;
            goto done;
        }

        DLIST_ADD_END(list, item, struct sbus_listener_list *);
        ret = EOK;
        goto done;
    }

    /* Otherwise create new hash entry and new list. */
    signal_known = false;
    list = item;

    ret = sss_ptr_hash_add(table, key, list, struct sbus_listener_list);

done:
    if (ret == EOK) {
        talloc_steal(table, item);
        *_signal_known = signal_known;
    }

    talloc_free(tmp_ctx);

    return ret;
}

struct sbus_listener_list *
sbus_router_listeners_lookup(hash_table_t *table,
                             const char *interface,
                             const char *signal_name)
{
    struct sbus_listener_list *list;
    char *key;

    key = talloc_asprintf(NULL, "%s.%s", interface, signal_name);
    if (key == NULL) {
        return NULL;
    }

    list = sss_ptr_hash_lookup(table, key, struct sbus_listener_list);
    talloc_free(key);

    return list;
}

hash_table_t *
sbus_router_nodes_init(TALLOC_CTX *mem_ctx)
{
    return sss_ptr_hash_create(mem_ctx, NULL, NULL);
}

errno_t
sbus_router_nodes_add(hash_table_t *table,
                      struct sbus_node *node)
{
    struct sbus_node *copy;
    errno_t ret;

    copy = sbus_node_copy(table, node);
    if (copy == NULL) {
        return ENOMEM;
    }

    ret = sss_ptr_hash_add(table, copy->path, copy, struct sbus_node);
    if (ret != EOK) {
        talloc_free(copy);
        return ret;
    }

    return EOK;
}

struct sbus_node *
sbus_router_nodes_lookup(hash_table_t *table,
                         const char *path)
{
    return sss_ptr_hash_lookup(table, path, struct sbus_node);
}