summaryrefslogtreecommitdiff
path: root/_dbus_bindings/server.c
blob: 7fc4f708d119d8253f2604fd477e2fd727e47ba1 (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
/* Implementation of the _dbus_bindings Server type, a Python wrapper
 * for DBusServer.
 *
 * Copyright (C) 2008 Openismus GmbH <http://openismus.com/>
 * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "dbus_bindings-internal.h"

/* Server definition ================================================ */

typedef struct {
    PyObject_HEAD
    DBusServer *server;

    /* The Connection subtype for which this Server is a factory */
    PyObject *conn_class;

    /* Weak-references list to make server weakly referenceable */
    PyObject *weaklist;

    PyObject *mainloop;
} Server;

PyDoc_STRVAR(Server_tp_doc,
"A D-Bus server.\n"
"\n"
"::\n"
"\n"
"   Server(address, connection_subtype, mainloop=None, auth_mechanisms=None)\n"
"     -> Server\n"
);

/* D-Bus Server user data slot, containing an owned reference to either
 * the Server, or a weakref to the Server.
 */
static dbus_int32_t _server_python_slot;

/* C API for main-loop hooks ======================================== */

/* Return a borrowed reference to the DBusServer which underlies this
 * Server. */
DBusServer *
DBusPyServer_BorrowDBusServer(PyObject *self)
{
    DBusServer *dbs;

    TRACE(self);
    if (!DBusPyServer_Check(self)) {
        PyErr_SetString(PyExc_TypeError, "A dbus.server.Server is required");
        return NULL;
    }
    dbs = ((Server *)self)->server;
    if (!dbs) {
        PyErr_SetString(PyExc_RuntimeError, "Server is in an invalid "
                        "state: no DBusServer");
        return NULL;
    }
    return dbs;
}

/* Internal C API =================================================== */

static dbus_bool_t
DBusPyServer_set_auth_mechanisms(Server *self,
                                 PyObject *auth_mechanisms)
{
    PyObject *fast_seq;
    Py_ssize_t length;
    Py_ssize_t i;

    fast_seq = PySequence_Fast(auth_mechanisms,
            "Expecting sequence for auth_mechanisms parameter");

    if (!fast_seq)
        return FALSE;

    length = PySequence_Fast_GET_SIZE(fast_seq);

    /* scope for list */
    {
        const char *list[length + 1];

        for (i = 0; i < length; ++i) {
            PyObject *am;

            am = PySequence_Fast_GET_ITEM(auth_mechanisms, i);
            /* this supports either str or unicode, raising TypeError
             * on failure */
            list[i] = PyString_AsString(am);

            if (!list[i])
                return FALSE;
        }

        list[length] = NULL;

        Py_BEGIN_ALLOW_THREADS
        dbus_server_set_auth_mechanisms(self->server, list);
        Py_END_ALLOW_THREADS
    }

    return TRUE;
}

/* Return a new reference to a Python Server or subclass corresponding
 * to the DBusServer server. For use in callbacks.
 *
 * Raises AssertionError if the DBusServer does not have a Server.
 */
static PyObject *
DBusPyServer_ExistingFromDBusServer(DBusServer *server)
{
    PyObject *self, *ref;

    Py_BEGIN_ALLOW_THREADS
    ref = (PyObject *)dbus_server_get_data(server,
                                           _server_python_slot);
    Py_END_ALLOW_THREADS
    if (ref) {
        DBG("(DBusServer *)%p has weak reference at %p", server, ref);
        self = PyWeakref_GetObject(ref);   /* still a borrowed ref */
        if (self && self != Py_None && DBusPyServer_Check(self)) {
            DBG("(DBusServer *)%p has weak reference at %p pointing to %p",
                server, ref, self);
            TRACE(self);
            Py_INCREF(self);
            TRACE(self);
            return self;
        }
    }

    PyErr_SetString(PyExc_AssertionError,
                    "D-Bus server does not have a Server "
                    "instance associated with it");
    return NULL;
}

static void
DBusPyServer_new_connection_cb(DBusServer *server,
                               DBusConnection *conn,
                               void *data UNUSED)
{
    PyGILState_STATE gil = PyGILState_Ensure();
    PyObject *self = NULL;
    PyObject *method = NULL;

    self = DBusPyServer_ExistingFromDBusServer(server);
    if (!self) goto out;
    TRACE(self);

    method = PyObject_GetAttrString(self, "_on_new_connection");
    TRACE(method);

    if (method) {
        PyObject *conn_class = ((Server *)self)->conn_class;
        PyObject *wrapper = DBusPyLibDBusConnection_New(conn);
        PyObject *conn_obj;
        PyObject *result;

        if (!wrapper)
            goto out;

        conn_obj = PyObject_CallFunctionObjArgs((PyObject *)conn_class,
                wrapper, ((Server*) self)->mainloop, NULL);
        Py_DECREF(wrapper);

        if (!conn_obj)
            goto out;

        result = PyObject_CallFunctionObjArgs(method, conn_obj, NULL);
        Py_XDECREF (conn_obj);

        /* discard result if not NULL, and fall through regardless */
        Py_XDECREF(result);
    }

out:
    Py_XDECREF(method);
    Py_XDECREF(self);

    if (PyErr_Occurred())
        PyErr_Print();

    PyGILState_Release(gil);
}

/* Return a new reference to a Python Server or subclass (given by cls)
 * corresponding to the DBusServer server, which must have been newly
 * created. For use by the Server constructor.
 *
 * Raises AssertionError if the DBusServer already has a Server.
 *
 * One reference to server is stolen - either the returned DBusPyServer
 * claims it, or it's unreffed.
 */
static PyObject *
DBusPyServer_NewConsumingDBusServer(PyTypeObject *cls,
                                    DBusServer *server,
                                    PyObject *conn_class,
                                    PyObject *mainloop,
                                    PyObject *auth_mechanisms)
{
    Server *self = NULL;
    PyObject *ref;
    dbus_bool_t ok;

    DBG("%s(cls=%p, server=%p, mainloop=%p, auth_mechanisms=%p)",
        __func__, cls, server, mainloop, auth_mechanisms);
    DBUS_PY_RAISE_VIA_NULL_IF_FAIL(server);

    Py_BEGIN_ALLOW_THREADS
    ref = (PyObject *)dbus_server_get_data(server,
                                           _server_python_slot);
    Py_END_ALLOW_THREADS
    if (ref) {
        self = (Server *)PyWeakref_GetObject(ref);
        ref = NULL;
        if (self && (PyObject *)self != Py_None) {
            self = NULL;
            PyErr_SetString(PyExc_AssertionError,
                            "Newly created D-Bus server already has a "
                            "Server instance associated with it");
            DBG("%s() fail - assertion failed, DBusPyServer has a DBusServer already", __func__);
            DBG_WHEREAMI;
            return NULL;
        }
    }
    ref = NULL;

    /* Change mainloop from a borrowed reference to an owned reference */
    if (!mainloop || mainloop == Py_None) {
        mainloop = dbus_py_get_default_main_loop();

        if (!mainloop || mainloop == Py_None) {
            PyErr_SetString(PyExc_RuntimeError,
                            "To run a D-Bus server, you need to either "
                            "pass mainloop=... to the constructor or call "
                            "dbus.set_default_main_loop(...)");
            goto err;
        }
    }
    else {
        Py_INCREF(mainloop);
    }

    DBG("Constructing Server from DBusServer at %p", server);

    self = (Server *)(cls->tp_alloc(cls, 0));
    if (!self) goto err;
    TRACE(self);

    DBG_WHEREAMI;

    self->server = NULL;

    Py_INCREF(conn_class);
    self->conn_class = conn_class;

    self->mainloop = mainloop;
    mainloop = NULL;    /* don't DECREF it - the DBusServer owns it now */

    ref = PyWeakref_NewRef((PyObject *)self, NULL);
    if (!ref) goto err;
    DBG("Created weak ref %p to (Server *)%p for (DBusServer *)%p",
        ref, self, server);

    Py_BEGIN_ALLOW_THREADS
    ok = dbus_server_set_data(server, _server_python_slot,
                              (void *)ref,
                              (DBusFreeFunction)dbus_py_take_gil_and_xdecref);
    Py_END_ALLOW_THREADS

    if (ok) {
        DBG("Attached weak ref %p ((Server *)%p) to (DBusServer *)%p",
            ref, self, server);

        ref = NULL;     /* don't DECREF it - the DBusServer owns it now */
    }
    else {
        DBG("Failed to attached weak ref %p ((Server *)%p) to "
            "(DBusServer *)%p - will dispose of it", ref, self, server);
        PyErr_NoMemory();
        goto err;
    }

    DBUS_PY_RAISE_VIA_GOTO_IF_FAIL(server, err);
    self->server = server;
    /* the DBusPyServer will close it now */
    server = NULL;

    if (self->mainloop != Py_None &&
        !dbus_py_set_up_server((PyObject *)self, self->mainloop))
        goto err;

    if (auth_mechanisms && auth_mechanisms != Py_None &&
        !DBusPyServer_set_auth_mechanisms(self, auth_mechanisms))
        goto err;

    Py_BEGIN_ALLOW_THREADS
    dbus_server_set_new_connection_function(self->server,
                                            DBusPyServer_new_connection_cb,
                                            NULL, NULL);
    Py_END_ALLOW_THREADS

    DBG("%s() -> %p", __func__, self);
    TRACE(self);
    return (PyObject *)self;

err:
    DBG("Failed to construct Server from DBusServer at %p", server);
    Py_XDECREF(mainloop);
    Py_XDECREF(self);
    Py_XDECREF(ref);

    if (server) {
        Py_BEGIN_ALLOW_THREADS
        dbus_server_disconnect(server);
        dbus_server_unref(server);
        Py_END_ALLOW_THREADS
    }

    DBG("%s() fail", __func__);
    DBG_WHEREAMI;
    return NULL;
}

/* Server type-methods ============================================== */

static PyObject *
Server_tp_new(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
    DBusServer *server;
    const char *address;
    DBusError error;
    PyObject *self, *conn_class, *mainloop = NULL, *auth_mechanisms = NULL;
    static char *argnames[] = { "address", "connection_class", "mainloop",
        "auth_mechanisms", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sO|OO", argnames,
            &address, &conn_class, &mainloop, &auth_mechanisms)) {
        return NULL;
    }

    if (!PyType_Check(conn_class) ||
        !PyType_IsSubtype((PyTypeObject *) conn_class, &DBusPyConnection_Type)) {
        /* strictly speaking, it can be any subtype of
         * _dbus_bindings._Connection - but nobody else should be subtyping
         * that, so let's keep this slightly inaccurate message */
        PyErr_SetString(PyExc_TypeError, "connection_class must be "
                "dbus.connection.Connection or a subtype");
        return NULL;
    }

    dbus_error_init(&error);

    Py_BEGIN_ALLOW_THREADS
    server = dbus_server_listen(address, &error);
    Py_END_ALLOW_THREADS

    if (!server) {
        DBusPyException_ConsumeError(&error);
        return NULL;
    }

    self = DBusPyServer_NewConsumingDBusServer(cls, server, conn_class,
            mainloop, auth_mechanisms);
    TRACE(self);

    return self;
}

/* Destructor */
static void Server_tp_dealloc(Server *self)
{
    DBusServer *server = self->server;
    PyObject *et, *ev, *etb;

    /* avoid clobbering any pending exception */
    PyErr_Fetch(&et, &ev, &etb);

    if (self->weaklist) {
        PyObject_ClearWeakRefs((PyObject *)self);
    }

    TRACE(self);
    DBG("Deallocating Server at %p (DBusServer at %p)", self, server);
    DBG_WHEREAMI;

    if (server) {
        DBG("Server at %p has a server, disconnecting it...", self);
        Py_BEGIN_ALLOW_THREADS
        dbus_server_disconnect(server);
        Py_END_ALLOW_THREADS
    }

    Py_DECREF(self->mainloop);

    /* make sure to do this last to preserve the invariant that
     * self->server is always non-NULL for any referenced Server.
     */
    DBG("Server at %p: nulling self->server", self);
    self->server = NULL;

    if (server) {
        DBG("Server at %p: unreffing server", self);
        dbus_server_unref(server);
    }

    DBG("Server at %p: freeing self", self);
    PyErr_Restore(et, ev, etb);
    (self->ob_type->tp_free)((PyObject *)self);
}

PyDoc_STRVAR(Server_disconnect__doc__,
"disconnect()\n\n"
"Releases the server's address and stops listening for new clients.\n\n"
"If called more than once, only the first call has an effect.");
static PyObject *
Server_disconnect (Server *self, PyObject *args UNUSED)
{
    TRACE(self);
    if (self->server) {
        Py_BEGIN_ALLOW_THREADS
        dbus_server_disconnect(self->server);
        Py_END_ALLOW_THREADS
    }
    Py_RETURN_NONE;
}

PyDoc_STRVAR(Server_get_address__doc__,
"get_address() -> str\n\n"
"Returns the address of the server.");
static PyObject *
Server_get_address(Server *self, PyObject *args UNUSED)
{
    const char *address;

    TRACE(self);
    DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
    Py_BEGIN_ALLOW_THREADS
    address = dbus_server_get_address(self->server);
    Py_END_ALLOW_THREADS

    return PyString_FromString(address);
}

PyDoc_STRVAR(Server_get_id__doc__,
"get_id() -> str\n\n"
"Returns the unique ID of the server.");
static PyObject *
Server_get_id(Server *self, PyObject *args UNUSED)
{
    const char *id;

    TRACE(self);
    DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
    Py_BEGIN_ALLOW_THREADS
    id = dbus_server_get_id(self->server);
    Py_END_ALLOW_THREADS

    return PyString_FromString(id);
}

PyDoc_STRVAR(Server_get_is_connected__doc__,
"get_is_connected() -> bool\n\n"
"Return true if this Server is still listening for new connections.\n");
static PyObject *
Server_get_is_connected (Server *self, PyObject *args UNUSED)
{
    dbus_bool_t ret;

    TRACE(self);
    DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->server);
    Py_BEGIN_ALLOW_THREADS
    ret = dbus_server_get_is_connected(self->server);
    Py_END_ALLOW_THREADS
    return PyBool_FromLong(ret);
}

/* Server type object =============================================== */

struct PyMethodDef DBusPyServer_tp_methods[] = {
#define ENTRY(name, flags) {#name, (PyCFunction)Server_##name, flags, Server_##name##__doc__}
    ENTRY(disconnect,       METH_NOARGS),
    ENTRY(get_address,      METH_NOARGS),
    ENTRY(get_id,           METH_NOARGS),
    ENTRY(get_is_connected, METH_NOARGS),
    {NULL},
#undef ENTRY
};

PyTypeObject DBusPyServer_Type = {
    PyObject_HEAD_INIT(NULL)
    0,                      /*ob_size*/
    "_dbus_bindings._Server",/*tp_name*/
    sizeof(Server),         /*tp_basicsize*/
    0,                      /*tp_itemsize*/
    /* methods */
    (destructor)Server_tp_dealloc,
    0,                      /*tp_print*/
    0,                      /*tp_getattr*/
    0,                      /*tp_setattr*/
    0,                      /*tp_compare*/
    0,                      /*tp_repr*/
    0,                      /*tp_as_number*/
    0,                      /*tp_as_sequence*/
    0,                      /*tp_as_mapping*/
    0,                      /*tp_hash*/
    0,                      /*tp_call*/
    0,                      /*tp_str*/
    0,                      /*tp_getattro*/
    0,                      /*tp_setattro*/
    0,                      /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_WEAKREFS | Py_TPFLAGS_BASETYPE,
    Server_tp_doc,          /*tp_doc*/
    0,                      /*tp_traverse*/
    0,                      /*tp_clear*/
    0,                      /*tp_richcompare*/
    offsetof(Server, weaklist),   /*tp_weaklistoffset*/
    0,                      /*tp_iter*/
    0,                      /*tp_iternext*/
    DBusPyServer_tp_methods,/*tp_methods*/
    0,                      /*tp_members*/
    0,                      /*tp_getset*/
    0,                      /*tp_base*/
    0,                      /*tp_dict*/
    0,                      /*tp_descr_get*/
    0,                      /*tp_descr_set*/
    0,                      /*tp_dictoffset*/
    0,                      /*tp_init*/
    0,                      /*tp_alloc*/
    Server_tp_new,          /*tp_new*/
    0,                      /*tp_free*/
    0,                      /*tp_is_gc*/
};

dbus_bool_t
dbus_py_init_server_types(void)
{
    /* Get a slot to store our weakref on DBus Server */
    _server_python_slot = -1;
    if (!dbus_server_allocate_data_slot(&_server_python_slot))
        return FALSE;

    if (PyType_Ready(&DBusPyServer_Type) < 0)
        return FALSE;

    return TRUE;
}

dbus_bool_t
dbus_py_insert_server_types(PyObject *this_module)
{
    if (PyModule_AddObject(this_module, "_Server",
                           (PyObject *)&DBusPyServer_Type) < 0) return FALSE;

    return TRUE;
}

/* vim:set ft=c cino< sw=4 sts=4 et: */