summaryrefslogtreecommitdiff
path: root/server/sbus/sssd_dbus_common.c
blob: 76cb3c205912c414e366037a70b8c74428426bf5 (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
#include <sys/time.h>
#include "tevent.h"
#include "dbus/dbus.h"
#include "util/util.h"
#include "util/btreemap.h"
#include "sbus/sssd_dbus.h"
#include "sbus/sssd_dbus_private.h"

/* =Watches=============================================================== */

/*
 * watch_handler
 * Callback for D-BUS to handle messages on a file-descriptor
 */
static void sbus_watch_handler(struct tevent_context *ev,
                               struct tevent_fd *fde,
                               uint16_t flags, void *data)
{
    struct sbus_watch_ctx *watch = talloc_get_type(data,
                                                   struct sbus_watch_ctx);
    enum dbus_conn_type type;
    union dbus_conn_pointer dbus_p;

    /* conn may get freed inside a handle, save the data we need for later */
    type = watch->conn->type;
    dbus_p = watch->conn->dbus;

    /* Take a reference while handling watch */
    if (type == SBUS_SERVER) {
        dbus_server_ref(dbus_p.server);
    } else {
        dbus_connection_ref(dbus_p.conn);
    }

    /* Fire if readable */
    if (flags & TEVENT_FD_READ) {
        dbus_watch_handle(watch->dbus_watch, DBUS_WATCH_READABLE);
    }

    /* Fire if writeable */
    if (flags & TEVENT_FD_WRITE) {
        dbus_watch_handle(watch->dbus_watch, DBUS_WATCH_WRITABLE);
    }

    /* Release reference once done */
    if (type == SBUS_SERVER) {
        dbus_server_unref(dbus_p.server);
    } else {
        dbus_connection_unref(dbus_p.conn);
    }
}

/*
 * add_watch
 * Set up hooks into the libevents mainloop for
 * D-BUS to add file descriptor-based events
 */
dbus_bool_t sbus_add_watch(DBusWatch *dbus_watch, void *data)
{
    unsigned int flags;
    uint16_t event_flags;
    struct sbus_connection *conn;
    struct sbus_watch_ctx *watch;
    int fd;

    conn = talloc_get_type(data, struct sbus_connection);

    watch = talloc_zero(conn, struct sbus_watch_ctx);
    if (!watch) {
        DEBUG(0, ("Outr of Memory!\n"));
        return FALSE;
    }
    watch->dbus_watch = dbus_watch;
    watch->conn = conn;

#ifdef HAVE_DBUS_WATCH_GET_UNIX_FD
    fd = dbus_watch_get_unix_fd(dbus_watch);
#else
    fd = dbus_watch_get_fd(dbus_watch);
#endif

    flags = dbus_watch_get_flags(dbus_watch);
    event_flags = 0;

    if (dbus_watch_get_enabled(dbus_watch)) {
        if (flags & DBUS_WATCH_READABLE) {
            event_flags |= TEVENT_FD_READ;
        }
        if (flags & DBUS_WATCH_WRITABLE) {
            event_flags |= TEVENT_FD_WRITE;
        }
    }

    DEBUG(8, ("%p: %d, %d=%s/%s\n",
              dbus_watch, fd, flags,
              ((event_flags & TEVENT_FD_READ)?"R":"-"),
              ((event_flags & TEVENT_FD_WRITE)?"W":"-")));

    /* Add the file descriptor to the event loop */
    watch->fde = tevent_add_fd(conn->ev,
                               watch, fd, event_flags,
                               sbus_watch_handler, watch);
    if (!watch->fde) {
        DEBUG(0, ("Failed to set up fd event!\n"));
        return FALSE;
    }

    /* Save the event to the watch object so it can be removed later */
    dbus_watch_set_data(dbus_watch, watch, NULL);

    return TRUE;
}

/*
 * toggle_watch
 * Hook for D-BUS to toggle the enabled/disabled state of
 * an event in the mainloop
 */
void sbus_toggle_watch(DBusWatch *dbus_watch, void *data)
{
    struct sbus_watch_ctx *watch;
    uint16_t event_flags = 0;
    unsigned int flags;
    void *watch_data;

    watch_data = dbus_watch_get_data(dbus_watch);
    watch = talloc_get_type(watch_data, struct sbus_watch_ctx);
    if (!watch) {
        DEBUG(0, ("Watch does not carry watch context?!\n"));
        /* TODO: abort ? */
        return;
    }

    flags = dbus_watch_get_flags(dbus_watch);

    if (dbus_watch_get_enabled(dbus_watch)) {
        if (flags & DBUS_WATCH_READABLE) {
            TEVENT_FD_READABLE(watch->fde);
        }
        if (flags & DBUS_WATCH_WRITABLE) {
            TEVENT_FD_WRITEABLE(watch->fde);
        }
    } else {
        if (flags & DBUS_WATCH_READABLE) {
            TEVENT_FD_NOT_READABLE(watch->fde);
        }
        if (flags & DBUS_WATCH_WRITABLE) {
            TEVENT_FD_NOT_WRITEABLE(watch->fde);
        }
    }

    if (debug_level >= 8) {
        event_flags = tevent_fd_get_flags(watch->fde);
    }
    DEBUG(8, ("%p: %p, %d=%s/%s\n",
              dbus_watch, watch, flags,
              ((event_flags & TEVENT_FD_READ)?"R":"-"),
              ((event_flags & TEVENT_FD_WRITE)?"W":"-")));
}

/*
 * sbus_remove_watch
 * Hook for D-BUS to remove file descriptor-based events
 * from the libevents mainloop
 */
void sbus_remove_watch(DBusWatch *dbus_watch, void *data)
{
    void *watch;

    DEBUG(8, ("%p\n", dbus_watch));

    watch = dbus_watch_get_data(dbus_watch);

    /* remove dbus watch data */
    dbus_watch_set_data(dbus_watch, NULL, NULL);

    /* Freeing the event object will remove it from the event loop */
    talloc_free(watch);
}

/* =Timeouts============================================================== */

static struct timeval _get_interval_tv(int interval) {
    struct timeval tv;
    struct timeval rightnow;

    gettimeofday(&rightnow,NULL);

    tv.tv_sec = interval / 1000 + rightnow.tv_sec;
    tv.tv_usec = (interval % 1000) * 1000 + rightnow.tv_usec;
    return tv;
}

/*
 * timeout_handler
 * Callback for D-BUS to handle timed events
 */
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);
}

/*
 * add_timeout
 * Hook for D-BUS to add time-based events to the mainloop
 */
dbus_bool_t sbus_add_timeout(DBusTimeout *dbus_timeout, void *data)
{
    struct sbus_connection *conn;
    struct sbus_timeout_ctx *timeout;
    struct timeval tv;

    DEBUG(8, ("%p\n", dbus_timeout));

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

    conn = talloc_get_type(data, struct sbus_connection);

    timeout = talloc_zero(conn, struct sbus_timeout_ctx);
    if (!timeout) {
        DEBUG(0, ("Outr of Memory!\n"));
        return FALSE;
    }
    timeout->dbus_timeout = dbus_timeout;

    tv = _get_interval_tv(dbus_timeout_get_interval(dbus_timeout));
    timeout->te = tevent_add_timer(conn->ev, timeout, tv,
                                   sbus_timeout_handler, timeout);
    if (!timeout->te) {
        DEBUG(0, ("Failed to set up timeout event!\n"));
        return FALSE;
    }

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

    return TRUE;
}

/*
 * sbus_toggle_timeout
 * Hook for D-BUS to toggle the enabled/disabled state of a mainloop
 * event
 */
void sbus_toggle_timeout(DBusTimeout *dbus_timeout, void *data)
{
    DEBUG(8, ("%p\n", dbus_timeout));

    if (dbus_timeout_get_enabled(dbus_timeout)) {
        sbus_add_timeout(dbus_timeout, data);
    } else {
        sbus_remove_timeout(dbus_timeout, data);
    }
}

/*
 * sbus_remove_timeout
 * Hook for D-BUS to remove time-based events from the mainloop
 */
void sbus_remove_timeout(DBusTimeout *dbus_timeout, void *data)
{
    void *timeout;

    DEBUG(8, ("%p\n", dbus_timeout));

    timeout = dbus_timeout_get_data(dbus_timeout);

    /* remove dbus timeout data */
    dbus_timeout_set_data(dbus_timeout, NULL, NULL);

    /* Freeing the event object will remove it from the event loop */
    talloc_free(timeout);

}

/* =Helpers=============================================================== */

int sbus_is_dbus_fixed_type(int dbus_type)
{
    switch (dbus_type) {
    case DBUS_TYPE_BYTE:
    case DBUS_TYPE_BOOLEAN:
    case DBUS_TYPE_INT16:
    case DBUS_TYPE_UINT16:
    case DBUS_TYPE_INT32:
    case DBUS_TYPE_UINT32:
    case DBUS_TYPE_INT64:
    case DBUS_TYPE_UINT64:
    case DBUS_TYPE_DOUBLE:
        return true;
    }
    return false;
}

int sbus_is_dbus_string_type(int dbus_type)
{
    switch(dbus_type) {
    case DBUS_TYPE_STRING:
    case DBUS_TYPE_OBJECT_PATH:
    case DBUS_TYPE_SIGNATURE:
        return true;
    }
    return false;
}

size_t sbus_get_dbus_type_size(int dbus_type)
{
    size_t ret;

    switch(dbus_type) {
    /* 1-byte types */
    case DBUS_TYPE_BYTE:
        ret = 1;
        break;

     /* 2-byte types */
    case DBUS_TYPE_INT16:
    case DBUS_TYPE_UINT16:
        ret = 2;
        break;

    /* 4-byte types */
    case DBUS_TYPE_BOOLEAN:
    case DBUS_TYPE_INT32:
    case DBUS_TYPE_UINT32:
        ret = 4;
        break;

    /* 8-byte types */
    case DBUS_TYPE_INT64:
    case DBUS_TYPE_UINT64:
    case DBUS_TYPE_DOUBLE:
        ret = 8;
        break;

    default:
        ret = 0;
    }
    return ret;
}