summaryrefslogtreecommitdiffstats
path: root/src/sbus/connection/sbus_watch.c
blob: d1b55e9633ac04d2c2b079e69756b854ebe69cb5 (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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>
        Stephen Gallagher <sgallagh@redhat.com>
        Simo Sorce <ssorce@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 <tevent.h>
#include <dbus/dbus.h>

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

#ifdef HAVE_DBUS_WATCH_GET_UNIX_FD
#define sbus_watch_get_fd(dbus_watch) dbus_watch_get_unix_fd(dbus_watch)
#else
#define sbus_watch_get_fd(dbus_watch) dbus_watch_get_fd(dbus_watch)
#endif

/**
 * These types definition are here so we can manipulate both D-Bus
 * connection and server with one API.
 */

typedef dbus_bool_t
(*sbus_dbus_set_watch_fn)(void *conn_or_server,
                          DBusAddWatchFunction,
                          DBusRemoveWatchFunction,
                          DBusWatchToggledFunction,
                          void *data,
                          DBusFreeFunction);

typedef dbus_bool_t
(*sbus_dbus_set_timeout_fn)(void *conn_or_server,
                            DBusAddTimeoutFunction,
                            DBusRemoveTimeoutFunction,
                            DBusTimeoutToggledFunction,
                            void *data,
                            DBusFreeFunction);

typedef void *
(*sbus_dbus_ref_fn)(void *conn_or_server);

typedef void
(*sbus_dbus_unref_fn)(void *conn_or_server);

/**
 * D-Bus watch is a mechanism to notify D-Bus every time a read or write event
 * occurs on D-Bus connection file descriptor.
 *
 * D-Bus provides add, remove and toggle function to create/remove a file
 * descriptor event listener and to switch between enabled and disabled
 * states when a file descriptor is kept opened for a longer period of time
 * to safe allocations.
 *
 * We incorporate this watch mechanism into a tevent file handler.
 */

enum sbus_watch_type {
    SBUS_WATCH_CONNECTION,
    SBUS_WATCH_SERVER
};

struct sbus_watch_control {
    void *dbus_ctx;
    sbus_dbus_set_watch_fn set_watch;
    sbus_dbus_set_timeout_fn set_timeout;
    sbus_dbus_ref_fn ref;
    sbus_dbus_unref_fn unref;
};

struct sbus_watch {
    struct tevent_context *ev;
    enum sbus_watch_type type;
    struct sbus_watch_control control;
    struct sbus_watch_fd *watch_list;
};

struct sbus_watch_fd {
    struct sbus_watch *sbus_watch;

    struct {
        DBusWatch *read;
        DBusWatch *write;
    } dbus_watch;

    int fd;
    struct tevent_fd *fdevent;
    struct tevent_immediate *im_event;

    struct sbus_watch_fd *prev;
    struct sbus_watch_fd *next;
};

static void
sbus_watch_handler(struct tevent_context *ev,
                   struct tevent_fd *fde,
                   uint16_t flags,
                   void *data)
{
    struct sbus_watch_control control;
    struct sbus_watch_fd *watch_fd;

    watch_fd = talloc_get_type(data, struct sbus_watch_fd);

    /**
     * Watch context may get freed if it's associated memory context
     * (connection or server) is freed inside a handle. We need to remember
     * it and increase reference to the connection or server so we can safely
     * issue both read and write handlers.
     */
    control = watch_fd->sbus_watch->control;

    control.ref(control.dbus_ctx);

    /* Fire if readable */
    if (flags & TEVENT_FD_READ && watch_fd->dbus_watch.read != NULL) {
        dbus_watch_handle(watch_fd->dbus_watch.read, DBUS_WATCH_READABLE);
    }

    /* Fire if writable */
    if (flags & TEVENT_FD_WRITE && watch_fd->dbus_watch.write != NULL) {
        dbus_watch_handle(watch_fd->dbus_watch.write, DBUS_WATCH_WRITABLE);
    }

    control.unref(control.dbus_ctx);
}

static int
sbus_watch_fd_destructor(struct sbus_watch_fd *watch_fd)
{
    if (watch_fd->sbus_watch == NULL) {
        return 0;
    }

    DLIST_REMOVE(watch_fd->sbus_watch->watch_list, watch_fd);

    return 0;
}

static struct sbus_watch_fd *
sbus_watch_get_by_fd(TALLOC_CTX *mem_ctx,
                     struct sbus_watch *watch,
                     int fd)
{
    struct sbus_watch_fd *watch_fd;

    /**
     * D-Bus may ask us to add a watch to a file descriptor that already had
     * a watch associated. If this is the case we return the existing context.
     */
    DLIST_FOR_EACH(watch_fd, watch->watch_list) {
        if (watch_fd->fd == fd) {
            return watch_fd;
        }
    }

    /* Create new one otherwise. */
    watch_fd = talloc_zero(mem_ctx, struct sbus_watch_fd);
    if (watch_fd == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!\n");
        return NULL;
    }

    watch_fd->im_event = tevent_create_immediate(watch_fd);
    if (watch_fd->im_event == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of Memory!\n");
        talloc_free(watch_fd);
        return NULL;
    }

    talloc_set_destructor(watch_fd, sbus_watch_fd_destructor);

    watch_fd->sbus_watch = watch;
    watch_fd->fd = fd;

    return watch_fd;
}

static void
sbus_watch_toggle(DBusWatch *dbus_watch, void *data);

static dbus_bool_t
sbus_watch_add(DBusWatch *dbus_watch, void *data)
{
    struct sbus_watch *sbus_watch;
    struct sbus_watch_fd *watch_fd;
    unsigned int watch_flags;
    dbus_bool_t is_enabled;
    uint16_t ev_flags;
    int fd;

    sbus_watch = talloc_get_type(data, struct sbus_watch);
    fd = sbus_watch_get_fd(dbus_watch);

    watch_fd = sbus_watch_get_by_fd(sbus_watch, sbus_watch, fd);
    if (watch_fd == NULL) {
        return FALSE;
    }

    is_enabled = dbus_watch_get_enabled(dbus_watch);
    watch_flags = dbus_watch_get_flags(dbus_watch);
    ev_flags = 0;

    /* Remember the D-Bus watch and its context. */
    if (watch_flags & DBUS_WATCH_READABLE) {
        watch_fd->dbus_watch.read = dbus_watch;
        if (is_enabled) {
            ev_flags |= TEVENT_FD_READ;
        }
    }

    if (watch_flags & DBUS_WATCH_WRITABLE) {
        watch_fd->dbus_watch.write = dbus_watch;
        if (is_enabled) {
            ev_flags |= TEVENT_FD_WRITE;
        }
    }

    dbus_watch_set_data(dbus_watch, watch_fd, NULL);

    /* Just update flags if an event handler already exists. */
    if (watch_fd->fdevent) {
        sbus_watch_toggle(dbus_watch, data);
        return TRUE;
    }

    /* Create new one otherwise. */
    watch_fd->fdevent = tevent_add_fd(sbus_watch->ev, watch_fd, fd, ev_flags,
                                      sbus_watch_handler, watch_fd);
    if (watch_fd->fdevent == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Failed to set up fd event!\n");
        talloc_zfree(watch_fd);
        return FALSE;
    }

    DLIST_ADD(sbus_watch->watch_list, watch_fd);

    DEBUG(SSSDBG_TRACE_INTERNAL, "Created a %s %s/%s watch on %d\n",
          is_enabled ? "enabled" : "disabled",
          (watch_flags & DBUS_WATCH_READABLE) ? "R" : "-",
          (watch_flags & DBUS_WATCH_WRITABLE) ? "W" : "-",
          fd);

    return TRUE;
}

static void
free_sbus_watch(struct tevent_context *ev, struct tevent_immediate *im,
                void *data)
{
    struct sbus_watch_fd *w = talloc_get_type(data, struct sbus_watch_fd);
    talloc_free(w); /* this will free attached 'im' as well */
}

static void
sbus_watch_remove(DBusWatch *dbus_watch, void *data)
{
    struct sbus_watch_fd *watch_fd;

    watch_fd = talloc_get_type(dbus_watch_get_data(dbus_watch),
                               struct sbus_watch_fd);
    if (watch_fd == NULL) {
        DEBUG(SSSDBG_OP_FAILURE,
              "D-Bus is trying to remove an unknown watch!\n");
        return;
    }

    /* Remove D-Bus watch data. */
    dbus_watch_set_data(dbus_watch, NULL, NULL);

    /* Check which watch to remove, or free if none left. */
    if (watch_fd->dbus_watch.read == dbus_watch) {
        watch_fd->dbus_watch.read = NULL;
    }

    if (watch_fd->dbus_watch.write == dbus_watch) {
        watch_fd->dbus_watch.write = NULL;
    }

    if (watch_fd->dbus_watch.read == NULL
            && watch_fd->dbus_watch.write == NULL) {
        /* libdbus doesn't need this watch{fd} anymore, so associated
         * tevent_fd should be removed from monitoring at the spot.
         */
        talloc_zfree(watch_fd->fdevent);
        /* watch_fd itself can't be freed yet as it still may be referenced
         * in the current context (for example in sbus_watch_handler())
         * so instead schedule immediate event to delete it.
         */
        tevent_schedule_immediate(watch_fd->im_event, watch_fd->sbus_watch->ev,
                                  free_sbus_watch, watch_fd);
    }
}

static void
sbus_watch_toggle(DBusWatch *dbus_watch, void *data)
{
    struct sbus_watch_fd *watch_fd;
    dbus_bool_t is_enabled;
    unsigned int flags;
    int fd;

    is_enabled = dbus_watch_get_enabled(dbus_watch);
    flags = dbus_watch_get_flags(dbus_watch);

    watch_fd = talloc_get_type(dbus_watch_get_data(dbus_watch),
                               struct sbus_watch_fd);
    if (watch_fd == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "D-Bus watch [%p] does not carry "
              "a watch context?\n", dbus_watch);
        return;
    }

    /* Toggle state. */
    if (is_enabled) {
        if (flags & DBUS_WATCH_READABLE) {
            TEVENT_FD_READABLE(watch_fd->fdevent);
        }
        if (flags & DBUS_WATCH_WRITABLE) {
            TEVENT_FD_WRITEABLE(watch_fd->fdevent);
        }
    } else {
        if (flags & DBUS_WATCH_READABLE) {
            TEVENT_FD_NOT_READABLE(watch_fd->fdevent);
        }
        if (flags & DBUS_WATCH_WRITABLE) {
            TEVENT_FD_NOT_WRITEABLE(watch_fd->fdevent);
        }
    }

    fd = sbus_watch_get_fd(dbus_watch);

    DEBUG(SSSDBG_TRACE_ALL, "Toggle to %s %s/%s watch on %d\n",
          is_enabled ? "enabled" : "disabled",
          (flags & DBUS_WATCH_READABLE) ? "R" : "-",
          (flags & DBUS_WATCH_WRITABLE) ? "W" : "-",
          fd);
}

/**
 * D-Bus timeout is a mechanism to notify D-Bus every time a requested timeout
 * is reached for a D-Bus connection.
 *
 * D-Bus provides add, remove and toggle function to create/remove a timer
 * event and to switch between enabled and disabled states when a specific
 * timer is being reused.
 *
 * We incorporate this watch mechanism into a tevent timer.
 */

struct sbus_timeout_ctx {
    DBusTimeout *dbus_timeout;
    struct tevent_timer *timed_event;
};

static void sbus_timeout_handler(struct tevent_context *ev,
                                 struct tevent_timer *te,
                                 struct timeval t,
                                 void *data)
{
    struct sbus_timeout_ctx *timeout;

    timeout = talloc_get_type(data, struct sbus_timeout_ctx);

    dbus_timeout_handle(timeout->dbus_timeout);
}

static errno_t
sbus_timer_schedule(TALLOC_CTX *mem_ctx,
                    struct tevent_context *ev,
                    struct sbus_timeout_ctx *timeout_ctx)
{
    struct timeval tv;
    int interval;

    /* Get interval in milliseconds and use it to compute timeval. */
    interval = dbus_timeout_get_interval(timeout_ctx->dbus_timeout);
    tv = tevent_timeval_current_ofs(interval / 1000, interval % 1000);

    timeout_ctx->timed_event = tevent_add_timer(ev, mem_ctx, tv,
                                                sbus_timeout_handler,
                                                timeout_ctx);
    if (timeout_ctx->timed_event == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Failed to set up timeout event!\n");
        return ENOMEM;
    }

    return EOK;
}

static dbus_bool_t
sbus_timer_add(DBusTimeout *dbus_timeout, void *data)
{
    struct sbus_watch *watch;
    struct sbus_timeout_ctx *timeout_ctx;
    errno_t ret;

    if (!dbus_timeout_get_enabled(dbus_timeout)) {
        return TRUE;
    }

    watch = talloc_get_type(data, struct sbus_watch);

    /* Create a timeout context. */
    timeout_ctx = talloc_zero(watch, struct sbus_timeout_ctx);
    if (timeout_ctx == NULL) {
        DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory!\n");
        return FALSE;
    }

    timeout_ctx->dbus_timeout = dbus_timeout;

    ret = sbus_timer_schedule(timeout_ctx, watch->ev, timeout_ctx);
    if (ret != EOK) {
        return FALSE;
    }

    /* Save the event to the watch object so it can be removed later. */
    dbus_timeout_set_data(timeout_ctx->dbus_timeout, timeout_ctx, NULL);

    return TRUE;
}

static void
sbus_timer_remove(DBusTimeout *dbus_timeout, void *data)
{
    void *timeout = dbus_timeout_get_data(dbus_timeout);

    /* Remove D-Bus timeout data. */
    dbus_timeout_set_data(dbus_timeout, NULL, NULL);

    /* Free the event object. */
    talloc_free(timeout);
}

static void
sbus_timer_toggle(DBusTimeout *dbus_timeout, void *data)
{
    if (dbus_timeout_get_enabled(dbus_timeout)) {
        sbus_timer_add(dbus_timeout, data);
    } else {
        sbus_timer_remove(dbus_timeout, data);
    }
}

/**
 * Setup tevent integration on sbus connection and server.
 */

static struct sbus_watch_control
sbus_watch_control_setup(enum sbus_watch_type type,
                         DBusConnection *conn,
                         DBusServer *server)
{
    struct sbus_watch_control control;

    switch (type) {
    case SBUS_WATCH_CONNECTION:
        control.dbus_ctx = conn;
        control.set_watch = (sbus_dbus_set_watch_fn)dbus_connection_set_watch_functions;
        control.set_timeout = (sbus_dbus_set_timeout_fn)dbus_connection_set_timeout_functions;
        control.ref = (sbus_dbus_ref_fn)dbus_connection_ref;
        control.unref = (sbus_dbus_unref_fn)dbus_connection_unref;
        break;
    case SBUS_WATCH_SERVER:
        control.dbus_ctx = server;
        control.set_watch = (sbus_dbus_set_watch_fn)dbus_server_set_watch_functions;
        control.set_timeout = (sbus_dbus_set_timeout_fn)dbus_server_set_timeout_functions;
        control.ref = (sbus_dbus_ref_fn)dbus_server_ref;
        control.unref = (sbus_dbus_unref_fn)dbus_server_unref;
        break;
    }

    return control;
}

static int
sbus_watch_destructor(struct sbus_watch *watch)
{
    struct sbus_watch_control control = watch->control;

    /* Disable watch. */
    control.set_timeout(control.dbus_ctx, NULL, NULL, NULL, NULL, NULL);
    control.set_watch(control.dbus_ctx, NULL, NULL, NULL, NULL, NULL);

    return 0;
}


static struct sbus_watch *
sbus_watch_create(TALLOC_CTX *mem_ctx,
                  struct tevent_context *ev,
                  enum sbus_watch_type type,
                  DBusConnection *conn,
                  DBusServer *server)
{
    struct sbus_watch *watch;

    switch (type) {
    case SBUS_WATCH_CONNECTION:
        if (conn == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Bug: conn pointer is NULL!\n");
            return NULL;
        }
        break;
    case SBUS_WATCH_SERVER:
        if (server == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Bug: server pointer is NULL!\n");
            return NULL;
        }
        break;
    default:
        DEBUG(SSSDBG_CRIT_FAILURE, "Unknown watch type!\n");
        return NULL;
    }

    watch = talloc_zero(mem_ctx, struct sbus_watch);
    if (watch == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Out of memory!\n");
        return NULL;
    }

    watch->ev = ev;
    watch->type = type;
    watch->control = sbus_watch_control_setup(type, conn, server);

    talloc_set_destructor(watch, sbus_watch_destructor);

    return watch;
}

static errno_t
sbus_watch_setup(TALLOC_CTX *mem_ctx,
                 struct tevent_context *ev,
                 enum sbus_watch_type type,
                 DBusConnection *conn,
                 DBusServer *server,
                 struct sbus_watch **_watch)
{
    struct sbus_watch *watch;
    dbus_bool_t dbret;
    errno_t ret;

    if (_watch == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "No context pointer to set!\n");
        return ERR_INTERNAL;
    }

    if (*_watch != NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Watch context is already set!\n");
        return ERR_INTERNAL;
    }

    watch = sbus_watch_create(mem_ctx, ev, type, conn, server);
    if (watch == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to create watch context!\n");
        return ENOMEM;
    }

    dbret = watch->control.set_watch(watch->control.dbus_ctx,
                                     sbus_watch_add,
                                     sbus_watch_remove,
                                     sbus_watch_toggle,
                                     watch, NULL);
    if (!dbret) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Unable to setup D-Bus watch functions!\n");
        ret = EIO;
        goto done;
    }

    dbret = watch->control.set_timeout(watch->control.dbus_ctx,
                                       sbus_timer_add,
                                       sbus_timer_remove,
                                       sbus_timer_toggle,
                                       watch, NULL);
    if (!dbret) {
        DEBUG(SSSDBG_CRIT_FAILURE,"Unable to setup D-Bus timeout functions\n");
        ret = EIO;
        goto done;
    }

    *_watch = watch;

    ret = EOK;

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

    return ret;
}

errno_t
sbus_watch_connection(TALLOC_CTX *mem_ctx,
                      struct tevent_context *ev,
                      DBusConnection *conn,
                      struct sbus_watch **_watch)
{
    return sbus_watch_setup(mem_ctx, ev, SBUS_WATCH_CONNECTION,
                            conn, NULL, _watch);
}

errno_t
sbus_watch_server(TALLOC_CTX *mem_ctx,
                  struct tevent_context *ev,
                  DBusServer *server,
                  struct sbus_watch **_watch)
{
    return sbus_watch_setup(mem_ctx, ev, SBUS_WATCH_SERVER,
                            NULL, server, _watch);
}