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
|
/* Implementation of Bus, a subtype of Connection.
*
* Copyright (C) 2006 Collabora Ltd. <http://www.collabora.co.uk/>
*
* SPDX-License-Identifier: MIT
*
* 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"
#include "conn-internal.h"
PyObject *
DBusPyConnection_NewForBus(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
PyObject *first = NULL, *mainloop = NULL;
DBusConnection *conn;
DBusError error;
Connection *self;
static char *argnames[] = {"address_or_type", "mainloop", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|OO", argnames,
&first, &mainloop)) {
return NULL;
}
dbus_error_init(&error);
if (first && PyUnicode_Check(first))
{
dbus_bool_t ret;
/* It's a custom address. First connect to it, then register. */
self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, args, kwargs);
if (!self) return NULL;
TRACE(self);
Py_BEGIN_ALLOW_THREADS
ret = dbus_bus_register(self->conn, &error);
Py_END_ALLOW_THREADS
if (!ret) {
DBusPyException_ConsumeError(&error);
Py_CLEAR(self);
return NULL;
}
return (PyObject *)self;
}
else if (!first || PyLong_Check(first))
{
long type;
PyObject *libdbusconn;
PyObject *new_args;
PyObject *new_kwargs;
/* If the first argument isn't a string, it must be an integer
representing one of the well-known bus types. The default is
DBUS_BUS_SESSION. */
if (first) {
/* on Python 2 this accepts either int or long */
type = PyLong_AsLong(first);
if (type == -1 && PyErr_Occurred())
return NULL;
if (type != DBUS_BUS_SESSION && type != DBUS_BUS_SYSTEM
&& type != DBUS_BUS_STARTER) {
PyErr_Format(PyExc_ValueError, "Unknown bus type %ld", type);
return NULL;
}
}
else {
type = DBUS_BUS_SESSION;
}
Py_BEGIN_ALLOW_THREADS
conn = dbus_bus_get_private(type, &error);
Py_END_ALLOW_THREADS
if (!conn) {
DBusPyException_ConsumeError(&error);
return NULL;
}
libdbusconn = DBusPyLibDBusConnection_New (conn);
dbus_connection_unref (conn);
if (!libdbusconn)
return NULL;
new_args = PyTuple_Pack(2, libdbusconn, mainloop ? mainloop : Py_None);
Py_CLEAR(libdbusconn);
if (!new_args) {
return NULL;
}
new_kwargs = PyDict_New();
if (!new_kwargs) {
Py_CLEAR(new_args);
return NULL;
}
self = (Connection *)(DBusPyConnection_Type.tp_new)(cls, new_args,
new_kwargs);
Py_CLEAR(new_args);
Py_CLEAR(new_kwargs);
return (PyObject *)self; /* whether NULL or not */
}
else {
PyErr_SetString(PyExc_TypeError, "A string address or an integer "
"bus type is required");
return NULL;
}
}
PyObject *
DBusPyConnection_GetUniqueName(Connection *self, PyObject *args UNUSED)
{
const char *name;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
name = dbus_bus_get_unique_name(self->conn);
Py_END_ALLOW_THREADS
if (!name) {
return DBusPyException_SetString("This connection has no unique name "
"yet");
}
return PyUnicode_FromString(name);
}
PyObject *
DBusPyConnection_SetUniqueName(Connection *self, PyObject *args)
{
const char *old_name, *new_name;
if (!PyArg_ParseTuple(args, "s:set_unique_name", &new_name)) {
return NULL;
}
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
/* libdbus will assert if we try to set a unique name when there's
* already one, so we need to make sure that can't happen.
* (Thanks, libdbus.)
*
* The things that can set the unique name are:
* - this function - but we don't release the GIL, so only one instance of
* this function can run
* - dbus_bus_get - but this is only called in a __new__ or __new__-like
* function, so the new connection isn't available to other code yet
* and this function can't be called on it
* - dbus_bus_register - same as dbus_bus_get
*
* Code outside dbus-python shouldn't be setting the unique name, because
* we're using a private connection; we have to trust the authors
* of mainloop bindings not to do silly things like that.
*/
old_name = dbus_bus_get_unique_name(self->conn);
if (old_name != NULL) {
PyErr_Format(PyExc_ValueError, "This connection already has a "
"unique name: '%s'", old_name);
return NULL;
}
dbus_bus_set_unique_name(self->conn, new_name);
Py_RETURN_NONE;
}
/* vim:set ft=c cino< sw=4 sts=4 et: */
|