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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
|
/* Implementation of normal Python-accessible methods on the _dbus_bindings
* Connection type; separated out to keep the file size manageable.
*
* 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"
static void
_object_path_unregister(DBusConnection *conn, void *user_data)
{
PyGILState_STATE gil = PyGILState_Ensure();
PyObject *tuple = NULL;
Connection *conn_obj = NULL;
PyObject *callable;
conn_obj = (Connection *)DBusPyConnection_ExistingFromDBusConnection(conn);
if (!conn_obj) goto out;
TRACE(conn_obj);
DBG("Connection at %p unregistering object path %s",
conn_obj, PyBytes_AS_STRING((PyObject *)user_data));
tuple = DBusPyConnection_GetObjectPathHandlers(
(PyObject *)conn_obj, (PyObject *)user_data);
if (!tuple) goto out;
if (tuple == Py_None) goto out;
DBG("%s", "... yes we have handlers for that object path");
/* 0'th item is the unregisterer (if that's a word) */
callable = PyTuple_GetItem(tuple, 0);
if (callable && callable != Py_None) {
DBG("%s", "... and we even have an unregisterer");
/* any return from the unregisterer is ignored */
Py_XDECREF(PyObject_CallFunctionObjArgs(callable, conn_obj, NULL));
}
out:
Py_CLEAR(conn_obj);
Py_CLEAR(tuple);
/* the user_data (a Python str) is no longer ref'd by the DBusConnection */
Py_CLEAR(user_data);
if (PyErr_Occurred()) {
PyErr_Print();
}
PyGILState_Release(gil);
}
static DBusHandlerResult
_object_path_message(DBusConnection *conn, DBusMessage *message,
void *user_data)
{
DBusHandlerResult ret;
PyGILState_STATE gil = PyGILState_Ensure();
Connection *conn_obj = NULL;
PyObject *tuple = NULL;
PyObject *msg_obj;
PyObject *callable; /* borrowed */
dbus_message_ref(message);
msg_obj = DBusPyMessage_ConsumeDBusMessage(message);
if (!msg_obj) {
ret = DBUS_HANDLER_RESULT_NEED_MEMORY;
goto out;
}
conn_obj = (Connection *)DBusPyConnection_ExistingFromDBusConnection(conn);
if (!conn_obj) {
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
goto out;
}
TRACE(conn_obj);
DBG("Connection at %p messaging object path %s",
conn_obj, PyBytes_AS_STRING((PyObject *)user_data));
DBG_DUMP_MESSAGE(message);
tuple = DBusPyConnection_GetObjectPathHandlers(
(PyObject *)conn_obj, (PyObject *)user_data);
if (!tuple || tuple == Py_None) {
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
goto out;
}
DBG("%s", "... yes we have handlers for that object path");
/* 1st item (0-based) is the message callback */
callable = PyTuple_GetItem(tuple, 1);
if (!callable) {
DBG("%s", "... error getting message handler from tuple");
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
else if (callable == Py_None) {
/* there was actually no handler after all */
DBG("%s", "... but those handlers don't do messages");
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
else {
DBG("%s", "... and we have a message handler for that object path");
ret = DBusPyConnection_HandleMessage(conn_obj, msg_obj, callable);
}
out:
Py_CLEAR(msg_obj);
Py_CLEAR(conn_obj);
Py_CLEAR(tuple);
if (PyErr_Occurred()) {
PyErr_Print();
}
PyGILState_Release(gil);
return ret;
}
static const DBusObjectPathVTable _object_path_vtable = {
_object_path_unregister,
_object_path_message,
};
static DBusHandlerResult
_filter_message(DBusConnection *conn, DBusMessage *message, void *user_data)
{
DBusHandlerResult ret;
PyGILState_STATE gil = PyGILState_Ensure();
Connection *conn_obj = NULL;
PyObject *callable = NULL;
PyObject *msg_obj;
#ifndef DBUS_PYTHON_DISABLE_CHECKS
Py_ssize_t i, size;
#endif
dbus_message_ref(message);
msg_obj = DBusPyMessage_ConsumeDBusMessage(message);
if (!msg_obj) {
DBG("%s", "OOM while trying to construct Message");
ret = DBUS_HANDLER_RESULT_NEED_MEMORY;
goto out;
}
conn_obj = (Connection *)DBusPyConnection_ExistingFromDBusConnection(conn);
if (!conn_obj) {
DBG("%s", "failed to traverse DBusConnection -> Connection weakref");
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
goto out;
}
TRACE(conn_obj);
/* The user_data is a pointer to a Python object. To avoid
* cross-library reference cycles, the DBusConnection isn't allowed
* to reference it. However, as long as the Connection is still
* alive, its ->filters list owns a reference to the same Python
* object, so the object should also still be alive.
*
* To ensure that this works, be careful whenever manipulating the
* filters list! (always put things in the list *before* giving
* them to libdbus, etc.)
*/
#ifdef DBUS_PYTHON_DISABLE_CHECKS
callable = (PyObject *)user_data;
#else
size = PyList_GET_SIZE(conn_obj->filters);
for (i = 0; i < size; i++) {
callable = PyList_GET_ITEM(conn_obj->filters, i);
if (callable == user_data) {
Py_INCREF(callable);
break;
}
else {
callable = NULL;
}
}
if (!callable) {
DBG("... filter %p has vanished from ->filters, so not calling it",
user_data);
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
goto out;
}
#endif
ret = DBusPyConnection_HandleMessage(conn_obj, msg_obj, callable);
out:
Py_CLEAR(msg_obj);
Py_CLEAR(conn_obj);
Py_CLEAR(callable);
PyGILState_Release(gil);
return ret;
}
PyDoc_STRVAR(Connection__require_main_loop__doc__,
"_require_main_loop()\n\n"
"Raise an exception if this Connection is not bound to any main loop -\n"
"in this state, asynchronous calls, receiving signals and exporting objects\n"
"will not work.\n"
"\n"
"`dbus.mainloop.NULL_MAIN_LOOP` is treated like a valid main loop - if you're\n"
"using that, you presumably know what you're doing.\n");
static PyObject *
Connection__require_main_loop (Connection *self, PyObject *args UNUSED)
{
if (!self->has_mainloop) {
PyErr_SetString(PyExc_RuntimeError,
"To make asynchronous calls, receive signals or "
"export objects, D-Bus connections must be attached "
"to a main loop by passing mainloop=... to the "
"constructor or calling "
"dbus.set_default_main_loop(...)");
return NULL;
}
Py_RETURN_NONE;
}
PyDoc_STRVAR(Connection_close__doc__,
"close()\n\n"
"Close the connection.");
static PyObject *
Connection_close (Connection *self, PyObject *args UNUSED)
{
TRACE(self);
/* Because the user explicitly asked to close the connection, we'll even
let them close shared connections. */
if (self->conn) {
Py_BEGIN_ALLOW_THREADS
dbus_connection_close(self->conn);
Py_END_ALLOW_THREADS
}
Py_RETURN_NONE;
}
PyDoc_STRVAR(Connection_get_is_connected__doc__,
"get_is_connected() -> bool\n\n"
"Return true if this Connection is connected.\n");
static PyObject *
Connection_get_is_connected (Connection *self, PyObject *args UNUSED)
{
dbus_bool_t ret;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
ret = dbus_connection_get_is_connected(self->conn);
Py_END_ALLOW_THREADS
return PyBool_FromLong(ret);
}
PyDoc_STRVAR(Connection_get_is_authenticated__doc__,
"get_is_authenticated() -> bool\n\n"
"Return true if this Connection was ever authenticated.\n");
static PyObject *
Connection_get_is_authenticated (Connection *self, PyObject *args UNUSED)
{
dbus_bool_t ret;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
ret = dbus_connection_get_is_authenticated(self->conn);
Py_END_ALLOW_THREADS
return PyBool_FromLong(ret);
}
PyDoc_STRVAR(Connection_set_exit_on_disconnect__doc__,
"set_exit_on_disconnect(bool)\n\n"
"Set whether the C function ``_exit`` will be called when this Connection\n"
"becomes disconnected. This will cause the program to exit without calling\n"
"any cleanup code or exit handlers.\n"
"\n"
"The default is for this feature to be disabled for Connections and enabled\n"
"for Buses.\n");
static PyObject *
Connection_set_exit_on_disconnect (Connection *self, PyObject *args)
{
int exit_on_disconnect;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTuple(args, "i:set_exit_on_disconnect",
&exit_on_disconnect)) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
dbus_connection_set_exit_on_disconnect(self->conn,
exit_on_disconnect ? 1 : 0);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
PyDoc_STRVAR(Connection_send_message__doc__,
"send_message(msg) -> long\n\n"
"Queue the given message for sending, and return the message serial number.\n"
"\n"
":Parameters:\n"
" `msg` : dbus.lowlevel.Message\n"
" The message to be sent.\n"
);
static PyObject *
Connection_send_message(Connection *self, PyObject *args)
{
dbus_bool_t ok;
PyObject *obj;
DBusMessage *msg;
dbus_uint32_t serial;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTuple(args, "O", &obj)) return NULL;
msg = DBusPyMessage_BorrowDBusMessage(obj);
if (!msg) return NULL;
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_send(self->conn, msg, &serial);
Py_END_ALLOW_THREADS
if (!ok) {
return PyErr_NoMemory();
}
return PyLong_FromUnsignedLong(serial);
}
PyDoc_STRVAR(Connection_set_allow_anonymous__doc__,
"set_allow_anonymous(bool)\n\n"
"Allows anonymous clients. Call this on the server side of a connection in a on_connection_added callback"
);
static PyObject *
Connection_set_allow_anonymous(Connection *self, PyObject *args)
{
dbus_bool_t t;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTuple(args, "i", &t)) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
dbus_connection_set_allow_anonymous(self->conn, t ? 1 : 0);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
/* The timeout is in seconds here, since that's conventional in Python. */
PyDoc_STRVAR(Connection_send_message_with_reply__doc__,
"send_message_with_reply(msg, reply_handler, timeout_s=-1, "
"require_main_loop=False) -> dbus.lowlevel.PendingCall\n\n"
"Queue the message for sending; expect a reply via the returned PendingCall,\n"
"which can also be used to cancel the pending call.\n"
"\n"
":Parameters:\n"
" `msg` : dbus.lowlevel.Message\n"
" The message to be sent\n"
" `reply_handler` : callable\n"
" Asynchronous reply handler: will be called with one positional\n"
" parameter, a Message instance representing the reply.\n"
" `timeout_s` : float\n"
" If the reply takes more than this many seconds, a timeout error\n"
" will be created locally and raised instead. If this timeout is\n"
" negative (default), a sane default (supplied by libdbus) is used.\n"
" `require_main_loop` : bool\n"
" If True, raise RuntimeError if this Connection does not have a main\n"
" loop configured. If False (default) and there is no main loop, you are\n"
" responsible for calling block() on the PendingCall.\n"
"\n"
);
static PyObject *
Connection_send_message_with_reply(Connection *self, PyObject *args, PyObject *kw)
{
dbus_bool_t ok;
double timeout_s = -1.0;
int timeout_ms;
PyObject *obj, *callable;
DBusMessage *msg;
DBusPendingCall *pending;
int require_main_loop = 0;
static char *argnames[] = {"msg", "reply_handler", "timeout_s",
"require_main_loop", NULL};
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTupleAndKeywords(args, kw,
"OO|di:send_message_with_reply",
argnames,
&obj, &callable, &timeout_s,
&require_main_loop)) {
return NULL;
}
if (require_main_loop && !Connection__require_main_loop(self, NULL)) {
return NULL;
}
msg = DBusPyMessage_BorrowDBusMessage(obj);
if (!msg) return NULL;
if (timeout_s < 0) {
timeout_ms = -1;
}
else {
if (timeout_s > ((double)INT_MAX) / 1000.0) {
PyErr_SetString(PyExc_ValueError, "Timeout too long");
return NULL;
}
timeout_ms = (int)(timeout_s * 1000.0);
}
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_send_with_reply(self->conn, msg, &pending,
timeout_ms);
Py_END_ALLOW_THREADS
if (!ok) {
return PyErr_NoMemory();
}
if (!pending) {
/* connection is disconnected (doesn't return FALSE!) */
return DBusPyException_SetString ("Connection is disconnected - "
"unable to make method call");
}
return DBusPyPendingCall_ConsumeDBusPendingCall(pending, callable);
}
/* Again, the timeout is in seconds, since that's conventional in Python. */
PyDoc_STRVAR(Connection_send_message_with_reply_and_block__doc__,
"send_message_with_reply_and_block(msg, timeout_s=-1)"
" -> dbus.lowlevel.Message\n\n"
"Send the message and block while waiting for a reply.\n"
"\n"
"This does not re-enter the main loop, so it can lead to a deadlock, if\n"
"the called method tries to make a synchronous call to a method in this\n"
"application. As such, it's probably a bad idea.\n"
"\n"
":Parameters:\n"
" `msg` : dbus.lowlevel.Message\n"
" The message to be sent\n"
" `timeout_s` : float\n"
" If the reply takes more than this many seconds, a timeout error\n"
" will be created locally and raised instead. If this timeout is\n"
" negative (default), a sane default (supplied by libdbus) is used.\n"
":Returns:\n"
" A `dbus.lowlevel.Message` instance (probably a `dbus.lowlevel.MethodReturnMessage`) on success\n"
":Raises dbus.DBusException:\n"
" On error (including if the reply arrives but is an\n"
" error message)\n"
"\n"
);
static PyObject *
Connection_send_message_with_reply_and_block(Connection *self, PyObject *args)
{
double timeout_s = -1.0;
int timeout_ms;
PyObject *obj;
DBusMessage *msg, *reply;
DBusError error;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTuple(args, "O|d:send_message_with_reply_and_block", &obj,
&timeout_s)) {
return NULL;
}
msg = DBusPyMessage_BorrowDBusMessage(obj);
if (!msg) return NULL;
if (timeout_s < 0) {
timeout_ms = -1;
}
else {
if (timeout_s > ((double)INT_MAX) / 1000.0) {
PyErr_SetString(PyExc_ValueError, "Timeout too long");
return NULL;
}
timeout_ms = (int)(timeout_s * 1000.0);
}
dbus_error_init(&error);
Py_BEGIN_ALLOW_THREADS
reply = dbus_connection_send_with_reply_and_block(self->conn, msg,
timeout_ms, &error);
Py_END_ALLOW_THREADS
/* FIXME: if we instead used send_with_reply and blocked on the resulting
* PendingCall, then we could get all args from the error, not just
* the first */
if (!reply) {
return DBusPyException_ConsumeError(&error);
}
return DBusPyMessage_ConsumeDBusMessage(reply);
}
PyDoc_STRVAR(Connection_flush__doc__,
"flush()\n\n"
"Block until the outgoing message queue is empty.\n");
static PyObject *
Connection_flush (Connection *self, PyObject *args UNUSED)
{
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
dbus_connection_flush (self->conn);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
/* Unsupported:
* dbus_connection_preallocate_send
* dbus_connection_free_preallocated_send
* dbus_connection_send_preallocated
* dbus_connection_borrow_message
* dbus_connection_return_message
* dbus_connection_steal_borrowed_message
* dbus_connection_pop_message
*/
/* Non-main-loop handling not yet implemented: */
/* dbus_connection_read_write_dispatch */
/* dbus_connection_read_write */
/* Main loop handling not yet implemented: */
/* dbus_connection_get_dispatch_status */
/* dbus_connection_dispatch */
/* dbus_connection_set_watch_functions */
/* dbus_connection_set_timeout_functions */
/* dbus_connection_set_wakeup_main_function */
/* dbus_connection_set_dispatch_status_function */
/* Normally in Python this would be called fileno(), but I don't want to
* encourage people to select() on it */
PyDoc_STRVAR(Connection_get_unix_fd__doc__,
"get_unix_fd() -> int or None\n\n"
"Get the connection's UNIX file descriptor, if any.\n\n"
"This can be used for SELinux access control checks with ``getpeercon()``\n"
"for example. **Do not** read or write to the file descriptor, or try to\n"
"``select()`` on it.\n");
static PyObject *
Connection_get_unix_fd (Connection *self, PyObject *unused UNUSED)
{
int fd;
dbus_bool_t ok;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_get_unix_fd (self->conn, &fd);
Py_END_ALLOW_THREADS
if (!ok) Py_RETURN_NONE;
return PyLong_FromLong(fd);
}
PyDoc_STRVAR(Connection_get_peer_unix_user__doc__,
"get_peer_unix_user() -> long or None\n\n"
"Get the UNIX user ID at the other end of the connection, if it has been\n"
"authenticated. Return None if this is a non-UNIX platform or the\n"
"connection has not been authenticated.\n");
static PyObject *
Connection_get_peer_unix_user (Connection *self, PyObject *unused UNUSED)
{
unsigned long uid;
dbus_bool_t ok;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_get_unix_user (self->conn, &uid);
Py_END_ALLOW_THREADS
if (!ok) Py_RETURN_NONE;
return PyLong_FromUnsignedLong (uid);
}
PyDoc_STRVAR(Connection_get_peer_unix_process_id__doc__,
"get_peer_unix_process_id() -> long or None\n\n"
"Get the UNIX process ID at the other end of the connection, if it has been\n"
"authenticated. Return None if this is a non-UNIX platform or the\n"
"connection has not been authenticated.\n");
static PyObject *
Connection_get_peer_unix_process_id (Connection *self, PyObject *unused UNUSED)
{
unsigned long pid;
dbus_bool_t ok;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_get_unix_process_id (self->conn, &pid);
Py_END_ALLOW_THREADS
if (!ok) Py_RETURN_NONE;
return PyLong_FromUnsignedLong (pid);
}
/* TODO: wrap dbus_connection_set_unix_user_function Pythonically */
PyDoc_STRVAR(Connection_add_message_filter__doc__,
"add_message_filter(callable)\n\n"
"Add the given message filter to the internal list.\n\n"
"Filters are handlers that are run on all incoming messages, prior to the\n"
"objects registered to handle object paths.\n"
"\n"
"Filters are run in the order that they were added. The same handler can\n"
"be added as a filter more than once, in which case it will be run more\n"
"than once. Filters added during a filter callback won't be run on the\n"
"message being processed.\n"
);
static PyObject *
Connection_add_message_filter(Connection *self, PyObject *callable)
{
dbus_bool_t ok;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
/* The callable must be referenced by ->filters *before* it is
* given to libdbus, which does not own a reference to it.
*/
if (PyList_Append(self->filters, callable) < 0) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_add_filter(self->conn, _filter_message, callable,
NULL);
Py_END_ALLOW_THREADS
if (!ok) {
Py_XDECREF(PyObject_CallMethod(self->filters, "remove", "(O)",
callable));
PyErr_NoMemory();
return NULL;
}
Py_RETURN_NONE;
}
PyDoc_STRVAR(Connection_remove_message_filter__doc__,
"remove_message_filter(callable)\n\n"
"Remove the given message filter (see `add_message_filter` for details).\n"
"\n"
":Raises LookupError:\n"
" The given callable is not among the registered filters\n");
static PyObject *
Connection_remove_message_filter(Connection *self, PyObject *callable)
{
PyObject *obj;
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
/* It's safe to do this before removing it from libdbus, because
* the presence of callable in our arguments means we have a ref
* to it. */
obj = PyObject_CallMethod(self->filters, "remove", "(O)", callable);
if (!obj) return NULL;
Py_CLEAR(obj);
Py_BEGIN_ALLOW_THREADS
dbus_connection_remove_filter(self->conn, _filter_message, callable);
Py_END_ALLOW_THREADS
Py_RETURN_NONE;
}
PyDoc_STRVAR(Connection__register_object_path__doc__,
"register_object_path(path, on_message, on_unregister=None, fallback=False)\n"
"\n"
"Register a callback to be called when messages arrive at the given\n"
"object-path. Used to export objects' methods on the bus in a low-level\n"
"way. For the high-level interface to this functionality (usually\n"
"recommended) see the `dbus.service.Object` base class.\n"
"\n"
":Parameters:\n"
" `path` : str\n"
" Object path to be acted on\n"
" `on_message` : callable\n"
" Called when a message arrives at the given object-path, with\n"
" two positional parameters: the first is this Connection,\n"
" the second is the incoming `dbus.lowlevel.Message`.\n"
" `on_unregister` : callable or None\n"
" If not None, called when the callback is unregistered.\n"
" `fallback` : bool\n"
" If True (the default is False), when a message arrives for a\n"
" 'subdirectory' of the given path and there is no more specific\n"
" handler, use this handler. Normally this handler is only run if\n"
" the paths match exactly.\n"
);
static PyObject *
Connection__register_object_path(Connection *self, PyObject *args,
PyObject *kwargs)
{
dbus_bool_t ok;
int fallback = 0;
char *path_bytes;
PyObject *callbacks, *path, *tuple, *on_message, *on_unregister = Py_None;
static char *argnames[] = {"path", "on_message", "on_unregister",
"fallback", NULL};
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!Connection__require_main_loop(self, NULL)) {
return NULL;
}
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"OO|Oi:_register_object_path",
argnames,
&path,
&on_message, &on_unregister,
&fallback)) return NULL;
/* Take a reference to path, which we give away to libdbus in a moment.
Also, path needs to be a string (not a subclass which could do something
mad) to preserve the desirable property that the DBusConnection can
never strongly reference the Connection, even indirectly.
*/
if (PyBytes_CheckExact(path)) {
Py_INCREF(path);
}
else if (PyUnicode_Check(path)) {
path = PyUnicode_AsUTF8String(path);
if (!path) return NULL;
}
else if (PyBytes_Check(path)) {
path = PyBytes_FromString(PyBytes_AS_STRING(path));
if (!path) return NULL;
}
else {
PyErr_SetString(PyExc_TypeError,
"path must be a str, bytes, or unicode object");
return NULL;
}
path_bytes = PyBytes_AS_STRING(path);
if (!dbus_py_validate_object_path(path_bytes)) {
Py_CLEAR(path);
return NULL;
}
tuple = Py_BuildValue("(OO)", on_unregister, on_message);
if (!tuple) {
Py_CLEAR(path);
return NULL;
}
/* Guard against registering a handler that already exists. */
callbacks = PyDict_GetItem(self->object_paths, path);
if (callbacks && callbacks != Py_None) {
PyErr_Format(PyExc_KeyError, "Can't register the object-path "
"handler for '%s': there is already a handler",
path_bytes);
Py_CLEAR(tuple);
Py_CLEAR(path);
return NULL;
}
/* Pre-allocate a slot in the dictionary, so we know we'll be able
* to replace it with the callbacks without OOM.
* This ensures we can keep libdbus' opinion of whether those
* paths are handled in sync with our own. */
if (PyDict_SetItem(self->object_paths, path, Py_None) < 0) {
Py_CLEAR(tuple);
Py_CLEAR(path);
return NULL;
}
Py_BEGIN_ALLOW_THREADS
if (fallback) {
ok = dbus_connection_register_fallback(self->conn,
path_bytes,
&_object_path_vtable,
path);
}
else {
ok = dbus_connection_register_object_path(self->conn,
path_bytes,
&_object_path_vtable,
path);
}
Py_END_ALLOW_THREADS
if (ok) {
if (PyDict_SetItem(self->object_paths, path, tuple) < 0) {
/* That shouldn't have happened, we already allocated enough
memory for it. Oh well, try to undo the registration to keep
things in sync. If this fails too, we've leaked a bit of
memory in libdbus, but tbh we should never get here anyway. */
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_unregister_object_path(self->conn,
path_bytes);
Py_END_ALLOW_THREADS
return NULL;
}
/* don't DECREF path: libdbus owns a ref now */
Py_CLEAR(tuple);
Py_RETURN_NONE;
}
else {
/* Oops, OOM. Tidy up, if we can, ignoring any error. */
PyDict_DelItem(self->object_paths, path);
PyErr_Clear();
Py_CLEAR(tuple);
Py_CLEAR(path);
PyErr_NoMemory();
return NULL;
}
}
PyDoc_STRVAR(Connection__unregister_object_path__doc__,
"unregister_object_path(path)\n\n"
"Remove a previously registered handler for the given object path.\n"
"\n"
":Parameters:\n"
" `path` : str\n"
" The object path whose handler is to be removed\n"
":Raises KeyError: if there is no handler registered for exactly that\n"
" object path.\n"
);
static PyObject *
Connection__unregister_object_path(Connection *self, PyObject *args,
PyObject *kwargs)
{
dbus_bool_t ok;
char *path_bytes;
PyObject *path;
PyObject *callbacks;
static char *argnames[] = {"path", NULL};
TRACE(self);
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTupleAndKeywords(args, kwargs,
"O:_unregister_object_path",
argnames, &path)) return NULL;
/* Take a ref to the path. Same comments as for _register_object_path. */
if (PyBytes_CheckExact(path)) {
Py_INCREF(path);
}
else if (PyUnicode_Check(path)) {
path = PyUnicode_AsUTF8String(path);
if (!path) return NULL;
}
else if (PyBytes_Check(path)) {
path = PyBytes_FromString(PyBytes_AS_STRING(path));
if (!path) return NULL;
}
else {
PyErr_SetString(PyExc_TypeError,
"path must be a str, bytes, or unicode object");
return NULL;
}
path_bytes = PyBytes_AS_STRING(path);
/* Guard against unregistering a handler that doesn't, in fact, exist,
or whose unregistration is already in progress. */
callbacks = PyDict_GetItem(self->object_paths, path);
if (!callbacks || callbacks == Py_None) {
PyErr_Format(PyExc_KeyError, "Can't unregister the object-path "
"handler for '%s': there is no such handler",
path_bytes);
Py_CLEAR(path);
return NULL;
}
/* Hang on to a reference to the callbacks for the moment. */
Py_INCREF(callbacks);
/* Get rid of the object-path while we still have the GIL, to
guard against unregistering twice from different threads (which
causes undefined behaviour in libdbus).
Because deletion would make it possible for the re-insertion below
to fail, we instead set the handler to None as a placeholder.
*/
if (PyDict_SetItem(self->object_paths, path, Py_None) < 0) {
/* If that failed, there's no need to be paranoid as below - the
callbacks are still set, so we failed, but at least everything
is in sync. */
Py_CLEAR(callbacks);
Py_CLEAR(path);
return NULL;
}
/* BEGIN PARANOIA
This is something of a critical section - the dict of object-paths
and libdbus' internal structures are out of sync for a bit. We have
to be able to cope with that.
It's really annoying that dbus_connection_unregister_object_path
can fail, *and* has undefined behaviour if the object path has
already been unregistered. Either/or would be fine.
*/
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_unregister_object_path(self->conn, path_bytes);
Py_END_ALLOW_THREADS
if (ok) {
Py_CLEAR(callbacks);
PyDict_DelItem(self->object_paths, path);
/* END PARANOIA on successful code path */
/* The above can't fail unless by some strange trickery the key is no
longer present. Ignore any errors. */
Py_CLEAR(path);
PyErr_Clear();
Py_RETURN_NONE;
}
else {
/* Oops, OOM. Put the callbacks back in the dict so
* we'll have another go if/when the user frees some memory
* and tries calling this method again. */
PyDict_SetItem(self->object_paths, path, callbacks);
/* END PARANOIA on failing code path */
/* If the SetItem failed, there's nothing we can do about it - but
since we know it's an existing entry, it shouldn't be able to fail
anyway. */
Py_CLEAR(path);
Py_CLEAR(callbacks);
return PyErr_NoMemory();
}
}
PyDoc_STRVAR(Connection_list_exported_child_objects__doc__,
"list_exported_child_objects(path: str) -> list of str\n\n"
"Return a list of the names of objects exported on this Connection as\n"
"direct children of the given object path.\n"
"\n"
"Each name returned may be converted to a valid object path using\n"
"``dbus.ObjectPath('%s%s%s' % (path, (path != '/' and '/' or ''), name))``.\n"
"For the purposes of this function, every parent or ancestor of an exported\n"
"object is considered to be an exported object, even if it's only an object\n"
"synthesized by the library to support introspection.\n");
static PyObject *
Connection_list_exported_child_objects (Connection *self, PyObject *args,
PyObject *kwargs)
{
const char *path;
char **kids, **kid_ptr;
dbus_bool_t ok;
PyObject *ret;
static char *argnames[] = {"path", NULL};
DBUS_PY_RAISE_VIA_NULL_IF_FAIL(self->conn);
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", argnames, &path)) {
return NULL;
}
if (!dbus_py_validate_object_path(path)) {
return NULL;
}
Py_BEGIN_ALLOW_THREADS
ok = dbus_connection_list_registered(self->conn, path, &kids);
Py_END_ALLOW_THREADS
if (!ok) {
return PyErr_NoMemory();
}
ret = PyList_New(0);
if (!ret) {
return NULL;
}
for (kid_ptr = kids; *kid_ptr; kid_ptr++) {
PyObject *tmp = PyUnicode_FromString(*kid_ptr);
if (!tmp) {
Py_CLEAR(ret);
return NULL;
}
if (PyList_Append(ret, tmp) < 0) {
Py_CLEAR(tmp);
Py_CLEAR(ret);
return NULL;
}
Py_CLEAR(tmp);
}
dbus_free_string_array(kids);
return ret;
}
/* dbus_connection_get_object_path_data - not useful to Python,
* the object path data is just a PyBytes containing the path */
/* dbus_connection_list_registered could be useful, though */
/* dbus_connection_set_change_sigpipe - sets global state */
/* Maxima. Does Python code ever need to manipulate these?
* OTOH they're easy to wrap */
/* dbus_connection_set_max_message_size */
/* dbus_connection_get_max_message_size */
/* dbus_connection_set_max_received_size */
/* dbus_connection_get_max_received_size */
/* dbus_connection_get_outgoing_size - almost certainly unneeded */
PyDoc_STRVAR(new_for_bus__doc__,
"Connection._new_for_bus([address: str or int]) -> Connection\n"
"\n"
"If the address is an int it must be one of the constants BUS_SESSION,\n"
"BUS_SYSTEM, BUS_STARTER; if a string, it must be a D-Bus address.\n"
"The default is BUS_SESSION.\n"
);
PyDoc_STRVAR(get_unique_name__doc__,
"get_unique_name() -> str\n\n"
"Return this application's unique name on this bus.\n"
"\n"
":Raises DBusException: if the connection has no unique name yet\n"
" (for Bus objects this can't happen, for peer-to-peer connections\n"
" this means you haven't called `set_unique_name`)\n");
PyDoc_STRVAR(set_unique_name__doc__,
"set_unique_name(str)\n\n"
"Set this application's unique name on this bus. Raise ValueError if it has\n"
"already been set.\n");
struct PyMethodDef DBusPyConnection_tp_methods[] = {
#define ENTRY(name, flags) {\
#name, (PyCFunction) (void (*)(void)) Connection_##name, \
flags, Connection_##name##__doc__ \
}
ENTRY(_require_main_loop, METH_NOARGS),
ENTRY(close, METH_NOARGS),
ENTRY(flush, METH_NOARGS),
ENTRY(get_is_connected, METH_NOARGS),
ENTRY(get_is_authenticated, METH_NOARGS),
ENTRY(set_exit_on_disconnect, METH_VARARGS),
ENTRY(get_unix_fd, METH_NOARGS),
ENTRY(get_peer_unix_user, METH_NOARGS),
ENTRY(get_peer_unix_process_id, METH_NOARGS),
ENTRY(add_message_filter, METH_O),
ENTRY(_register_object_path, METH_VARARGS|METH_KEYWORDS),
ENTRY(remove_message_filter, METH_O),
ENTRY(send_message, METH_VARARGS),
ENTRY(send_message_with_reply, METH_VARARGS|METH_KEYWORDS),
ENTRY(send_message_with_reply_and_block, METH_VARARGS),
ENTRY(_unregister_object_path, METH_VARARGS|METH_KEYWORDS),
ENTRY(list_exported_child_objects, METH_VARARGS|METH_KEYWORDS),
{"_new_for_bus", (PyCFunction) (void (*)(void)) DBusPyConnection_NewForBus,
METH_CLASS|METH_VARARGS|METH_KEYWORDS,
new_for_bus__doc__},
{"get_unique_name", (PyCFunction) (void (*)(void)) DBusPyConnection_GetUniqueName,
METH_NOARGS,
get_unique_name__doc__},
{"set_unique_name", (PyCFunction) (void (*)(void)) DBusPyConnection_SetUniqueName,
METH_VARARGS,
set_unique_name__doc__},
ENTRY(set_allow_anonymous, METH_VARARGS),
{NULL},
#undef ENTRY
};
/* vim:set ft=c cino< sw=4 sts=4 et: */
|