dbus-connection.c revision b60c835d346b7e214e627abd8e0cdf06932313a7
1/* -*- mode: C; c-file-style: "gnu" -*- */ 2/* dbus-connection.c DBusConnection object 3 * 4 * Copyright (C) 2002, 2003, 2004 Red Hat Inc. 5 * 6 * Licensed under the Academic Free License version 2.1 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 * 22 */ 23 24#include <config.h> 25#include "dbus-shared.h" 26#include "dbus-connection.h" 27#include "dbus-list.h" 28#include "dbus-timeout.h" 29#include "dbus-transport.h" 30#include "dbus-watch.h" 31#include "dbus-connection-internal.h" 32#include "dbus-list.h" 33#include "dbus-hash.h" 34#include "dbus-message-internal.h" 35#include "dbus-threads.h" 36#include "dbus-protocol.h" 37#include "dbus-dataslot.h" 38#include "dbus-string.h" 39#include "dbus-pending-call.h" 40#include "dbus-object-tree.h" 41#include "dbus-marshal.h" 42 43#if 0 44#define CONNECTION_LOCK(connection) do { \ 45 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \ 46 dbus_mutex_lock ((connection)->mutex); \ 47 } while (0) 48#define CONNECTION_UNLOCK(connection) do { \ 49 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \ 50 dbus_mutex_unlock ((connection)->mutex); \ 51 } while (0) 52#else 53#define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex) 54#define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex) 55#endif 56 57#define DISPATCH_STATUS_NAME(s) \ 58 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \ 59 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \ 60 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \ 61 "???") 62 63/** 64 * @defgroup DBusConnection DBusConnection 65 * @ingroup DBus 66 * @brief Connection to another application 67 * 68 * A DBusConnection represents a connection to another 69 * application. Messages can be sent and received via this connection. 70 * The other application may be a message bus; for convenience, the 71 * function dbus_bus_get() is provided to automatically open a 72 * connection to the well-known message buses. 73 * 74 * In brief a DBusConnection is a message queue associated with some 75 * message transport mechanism such as a socket. The connection 76 * maintains a queue of incoming messages and a queue of outgoing 77 * messages. 78 * 79 * Incoming messages are normally processed by calling 80 * dbus_connection_dispatch(). dbus_connection_dispatch() runs any 81 * handlers registered for the topmost message in the message queue, 82 * then discards the message, then returns. 83 * 84 * dbus_connection_get_dispatch_status() indicates whether 85 * messages are currently in the queue that need dispatching. 86 * dbus_connection_set_dispatch_status_function() allows 87 * you to set a function to be used to monitor the dispatch status. 88 * 89 * If you're using GLib or Qt add-on libraries for D-BUS, there are 90 * special convenience APIs in those libraries that hide 91 * all the details of dispatch and watch/timeout monitoring. 92 * For example, dbus_connection_setup_with_g_main(). 93 * 94 * If you aren't using these add-on libraries, you have to manually 95 * call dbus_connection_set_dispatch_status_function(), 96 * dbus_connection_set_watch_functions(), 97 * dbus_connection_set_timeout_functions() providing appropriate 98 * functions to integrate the connection with your application's main 99 * loop. 100 * 101 * When you use dbus_connection_send() or one of its variants to send 102 * a message, the message is added to the outgoing queue. It's 103 * actually written to the network later; either in 104 * dbus_watch_handle() invoked by your main loop, or in 105 * dbus_connection_flush() which blocks until it can write out the 106 * entire outgoing queue. The GLib/Qt add-on libraries again 107 * handle the details here for you by setting up watch functions. 108 * 109 * When a connection is disconnected, you are guaranteed to get a 110 * signal "Disconnected" from the interface 111 * #DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, path 112 * #DBUS_PATH_ORG_FREEDESKTOP_LOCAL. 113 * 114 * You may not drop the last reference to a #DBusConnection 115 * until that connection has been disconnected. 116 * 117 * You may dispatch the unprocessed incoming message queue even if the 118 * connection is disconnected. However, "Disconnected" will always be 119 * the last message in the queue (obviously no messages are received 120 * after disconnection). 121 * 122 * #DBusConnection has thread locks and drops them when invoking user 123 * callbacks, so in general is transparently threadsafe. However, 124 * #DBusMessage does NOT have thread locks; you must not send the same 125 * message to multiple #DBusConnection that will be used from 126 * different threads. 127 */ 128 129/** 130 * @defgroup DBusConnectionInternals DBusConnection implementation details 131 * @ingroup DBusInternals 132 * @brief Implementation details of DBusConnection 133 * 134 * @{ 135 */ 136 137/** 138 * Internal struct representing a message filter function 139 */ 140typedef struct DBusMessageFilter DBusMessageFilter; 141 142/** 143 * Internal struct representing a message filter function 144 */ 145struct DBusMessageFilter 146{ 147 DBusAtomic refcount; /**< Reference count */ 148 DBusHandleMessageFunction function; /**< Function to call to filter */ 149 void *user_data; /**< User data for the function */ 150 DBusFreeFunction free_user_data_function; /**< Function to free the user data */ 151}; 152 153 154/** 155 * Internals of DBusPreallocatedSend 156 */ 157struct DBusPreallocatedSend 158{ 159 DBusConnection *connection; /**< Connection we'd send the message to */ 160 DBusList *queue_link; /**< Preallocated link in the queue */ 161 DBusList *counter_link; /**< Preallocated link in the resource counter */ 162}; 163 164static dbus_bool_t _dbus_modify_sigpipe = TRUE; 165 166/** 167 * Implementation details of DBusConnection. All fields are private. 168 */ 169struct DBusConnection 170{ 171 DBusAtomic refcount; /**< Reference count. */ 172 173 DBusMutex *mutex; /**< Lock on the entire DBusConnection */ 174 175 dbus_bool_t dispatch_acquired; /**< Protects dispatch() */ 176 DBusCondVar *dispatch_cond; /**< Protects dispatch() */ 177 178 dbus_bool_t io_path_acquired; /**< Protects transport io path */ 179 DBusCondVar *io_path_cond; /**< Protects transport io path */ 180 181 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */ 182 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */ 183 184 DBusMessage *message_borrowed; /**< True if the first incoming message has been borrowed */ 185 DBusCondVar *message_returned_cond; /**< Used with dbus_connection_borrow_message() */ 186 187 int n_outgoing; /**< Length of outgoing queue. */ 188 int n_incoming; /**< Length of incoming queue. */ 189 190 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */ 191 192 DBusTransport *transport; /**< Object that sends/receives messages over network. */ 193 DBusWatchList *watches; /**< Stores active watches. */ 194 DBusTimeoutList *timeouts; /**< Stores active timeouts. */ 195 196 DBusList *filter_list; /**< List of filters. */ 197 198 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */ 199 200 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */ 201 202 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */ 203 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */ 204 205 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */ 206 void *wakeup_main_data; /**< Application data for wakeup_main_function */ 207 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */ 208 209 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */ 210 void *dispatch_status_data; /**< Application data for dispatch_status_function */ 211 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */ 212 213 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */ 214 215 DBusList *link_cache; /**< A cache of linked list links to prevent contention 216 * for the global linked list mempool lock 217 */ 218 DBusObjectTree *objects; /**< Object path handlers registered with this connection */ 219 220 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */ 221 222#ifndef DBUS_DISABLE_CHECKS 223 int generation; /**< _dbus_current_generation that should correspond to this connection */ 224#endif 225}; 226 227static void _dbus_connection_remove_timeout_locked (DBusConnection *connection, 228 DBusTimeout *timeout); 229static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection); 230static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection, 231 DBusDispatchStatus new_status); 232static void _dbus_connection_last_unref (DBusConnection *connection); 233 234static DBusMessageFilter * 235_dbus_message_filter_ref (DBusMessageFilter *filter) 236{ 237 _dbus_assert (filter->refcount.value > 0); 238 _dbus_atomic_inc (&filter->refcount); 239 240 return filter; 241} 242 243static void 244_dbus_message_filter_unref (DBusMessageFilter *filter) 245{ 246 _dbus_assert (filter->refcount.value > 0); 247 248 if (_dbus_atomic_dec (&filter->refcount) == 1) 249 { 250 if (filter->free_user_data_function) 251 (* filter->free_user_data_function) (filter->user_data); 252 253 dbus_free (filter); 254 } 255} 256 257/** 258 * Acquires the connection lock. 259 * 260 * @param connection the connection. 261 */ 262void 263_dbus_connection_lock (DBusConnection *connection) 264{ 265 CONNECTION_LOCK (connection); 266} 267 268/** 269 * Releases the connection lock. 270 * 271 * @param connection the connection. 272 */ 273void 274_dbus_connection_unlock (DBusConnection *connection) 275{ 276 CONNECTION_UNLOCK (connection); 277} 278 279/** 280 * Wakes up the main loop if it is sleeping 281 * Needed if we're e.g. queueing outgoing messages 282 * on a thread while the mainloop sleeps. 283 * 284 * @param connection the connection. 285 */ 286static void 287_dbus_connection_wakeup_mainloop (DBusConnection *connection) 288{ 289 if (connection->wakeup_main_function) 290 (*connection->wakeup_main_function) (connection->wakeup_main_data); 291} 292 293#ifdef DBUS_BUILD_TESTS 294/* For now this function isn't used */ 295/** 296 * Adds a message to the incoming message queue, returning #FALSE 297 * if there's insufficient memory to queue the message. 298 * Does not take over refcount of the message. 299 * 300 * @param connection the connection. 301 * @param message the message to queue. 302 * @returns #TRUE on success. 303 */ 304dbus_bool_t 305_dbus_connection_queue_received_message (DBusConnection *connection, 306 DBusMessage *message) 307{ 308 DBusList *link; 309 310 link = _dbus_list_alloc_link (message); 311 if (link == NULL) 312 return FALSE; 313 314 dbus_message_ref (message); 315 _dbus_connection_queue_received_message_link (connection, link); 316 317 return TRUE; 318} 319#endif 320 321/** 322 * Adds a message-containing list link to the incoming message queue, 323 * taking ownership of the link and the message's current refcount. 324 * Cannot fail due to lack of memory. 325 * 326 * @param connection the connection. 327 * @param link the message link to queue. 328 */ 329void 330_dbus_connection_queue_received_message_link (DBusConnection *connection, 331 DBusList *link) 332{ 333 DBusPendingCall *pending; 334 dbus_int32_t reply_serial; 335 DBusMessage *message; 336 337 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport)); 338 339 _dbus_list_append_link (&connection->incoming_messages, 340 link); 341 message = link->data; 342 343 /* If this is a reply we're waiting on, remove timeout for it */ 344 reply_serial = dbus_message_get_reply_serial (message); 345 if (reply_serial != -1) 346 { 347 pending = _dbus_hash_table_lookup_int (connection->pending_replies, 348 reply_serial); 349 if (pending != NULL) 350 { 351 if (pending->timeout_added) 352 _dbus_connection_remove_timeout_locked (connection, 353 pending->timeout); 354 355 pending->timeout_added = FALSE; 356 } 357 } 358 359 connection->n_incoming += 1; 360 361 _dbus_connection_wakeup_mainloop (connection); 362 363 _dbus_verbose ("Message %p (%d %s %s '%s') added to incoming queue %p, %d incoming\n", 364 message, 365 dbus_message_get_type (message), 366 dbus_message_get_interface (message) ? 367 dbus_message_get_interface (message) : 368 "no interface", 369 dbus_message_get_member (message) ? 370 dbus_message_get_member (message) : 371 "no member", 372 dbus_message_get_signature (message), 373 connection, 374 connection->n_incoming); 375} 376 377/** 378 * Adds a link + message to the incoming message queue. 379 * Can't fail. Takes ownership of both link and message. 380 * 381 * @param connection the connection. 382 * @param link the list node and message to queue. 383 * 384 * @todo This needs to wake up the mainloop if it is in 385 * a poll/select and this is a multithreaded app. 386 */ 387static void 388_dbus_connection_queue_synthesized_message_link (DBusConnection *connection, 389 DBusList *link) 390{ 391 _dbus_list_append_link (&connection->incoming_messages, link); 392 393 connection->n_incoming += 1; 394 395 _dbus_connection_wakeup_mainloop (connection); 396 397 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n", 398 link->data, connection, connection->n_incoming); 399} 400 401 402/** 403 * Checks whether there are messages in the outgoing message queue. 404 * Called with connection lock held. 405 * 406 * @param connection the connection. 407 * @returns #TRUE if the outgoing queue is non-empty. 408 */ 409dbus_bool_t 410_dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection) 411{ 412 return connection->outgoing_messages != NULL; 413} 414 415/** 416 * Checks whether there are messages in the outgoing message queue. 417 * 418 * @param connection the connection. 419 * @returns #TRUE if the outgoing queue is non-empty. 420 */ 421dbus_bool_t 422dbus_connection_has_messages_to_send (DBusConnection *connection) 423{ 424 dbus_bool_t v; 425 426 _dbus_return_val_if_fail (connection != NULL, FALSE); 427 428 CONNECTION_LOCK (connection); 429 v = _dbus_connection_has_messages_to_send_unlocked (connection); 430 CONNECTION_UNLOCK (connection); 431 432 return v; 433} 434 435/** 436 * Gets the next outgoing message. The message remains in the 437 * queue, and the caller does not own a reference to it. 438 * 439 * @param connection the connection. 440 * @returns the message to be sent. 441 */ 442DBusMessage* 443_dbus_connection_get_message_to_send (DBusConnection *connection) 444{ 445 return _dbus_list_get_last (&connection->outgoing_messages); 446} 447 448/** 449 * Notifies the connection that a message has been sent, so the 450 * message can be removed from the outgoing queue. 451 * Called with the connection lock held. 452 * 453 * @param connection the connection. 454 * @param message the message that was sent. 455 */ 456void 457_dbus_connection_message_sent (DBusConnection *connection, 458 DBusMessage *message) 459{ 460 DBusList *link; 461 462 /* This can be called before we even complete authentication, since 463 * it's called on disconnect to clean up the outgoing queue. 464 * It's also called as we successfully send each message. 465 */ 466 467 link = _dbus_list_get_last_link (&connection->outgoing_messages); 468 _dbus_assert (link != NULL); 469 _dbus_assert (link->data == message); 470 471 /* Save this link in the link cache */ 472 _dbus_list_unlink (&connection->outgoing_messages, 473 link); 474 _dbus_list_prepend_link (&connection->link_cache, link); 475 476 connection->n_outgoing -= 1; 477 478 _dbus_verbose ("Message %p (%d %s %s '%s') removed from outgoing queue %p, %d left to send\n", 479 message, 480 dbus_message_get_type (message), 481 dbus_message_get_interface (message) ? 482 dbus_message_get_interface (message) : 483 "no interface", 484 dbus_message_get_member (message) ? 485 dbus_message_get_member (message) : 486 "no member", 487 dbus_message_get_signature (message), 488 connection, connection->n_outgoing); 489 490 /* Save this link in the link cache also */ 491 _dbus_message_remove_size_counter (message, connection->outgoing_counter, 492 &link); 493 _dbus_list_prepend_link (&connection->link_cache, link); 494 495 dbus_message_unref (message); 496} 497 498/** 499 * Adds a watch using the connection's DBusAddWatchFunction if 500 * available. Otherwise records the watch to be added when said 501 * function is available. Also re-adds the watch if the 502 * DBusAddWatchFunction changes. May fail due to lack of memory. 503 * 504 * @param connection the connection. 505 * @param watch the watch to add. 506 * @returns #TRUE on success. 507 */ 508dbus_bool_t 509_dbus_connection_add_watch (DBusConnection *connection, 510 DBusWatch *watch) 511{ 512 if (connection->watches) /* null during finalize */ 513 return _dbus_watch_list_add_watch (connection->watches, 514 watch); 515 else 516 return FALSE; 517} 518 519/** 520 * Removes a watch using the connection's DBusRemoveWatchFunction 521 * if available. It's an error to call this function on a watch 522 * that was not previously added. 523 * 524 * @param connection the connection. 525 * @param watch the watch to remove. 526 */ 527void 528_dbus_connection_remove_watch (DBusConnection *connection, 529 DBusWatch *watch) 530{ 531 if (connection->watches) /* null during finalize */ 532 _dbus_watch_list_remove_watch (connection->watches, 533 watch); 534} 535 536/** 537 * Toggles a watch and notifies app via connection's 538 * DBusWatchToggledFunction if available. It's an error to call this 539 * function on a watch that was not previously added. 540 * Connection lock should be held when calling this. 541 * 542 * @param connection the connection. 543 * @param watch the watch to toggle. 544 * @param enabled whether to enable or disable 545 */ 546void 547_dbus_connection_toggle_watch (DBusConnection *connection, 548 DBusWatch *watch, 549 dbus_bool_t enabled) 550{ 551 _dbus_assert (watch != NULL); 552 553 if (connection->watches) /* null during finalize */ 554 _dbus_watch_list_toggle_watch (connection->watches, 555 watch, enabled); 556} 557 558/** 559 * Adds a timeout using the connection's DBusAddTimeoutFunction if 560 * available. Otherwise records the timeout to be added when said 561 * function is available. Also re-adds the timeout if the 562 * DBusAddTimeoutFunction changes. May fail due to lack of memory. 563 * The timeout will fire repeatedly until removed. 564 * 565 * @param connection the connection. 566 * @param timeout the timeout to add. 567 * @returns #TRUE on success. 568 */ 569dbus_bool_t 570_dbus_connection_add_timeout (DBusConnection *connection, 571 DBusTimeout *timeout) 572{ 573 if (connection->timeouts) /* null during finalize */ 574 return _dbus_timeout_list_add_timeout (connection->timeouts, 575 timeout); 576 else 577 return FALSE; 578} 579 580/** 581 * Removes a timeout using the connection's DBusRemoveTimeoutFunction 582 * if available. It's an error to call this function on a timeout 583 * that was not previously added. 584 * 585 * @param connection the connection. 586 * @param timeout the timeout to remove. 587 */ 588void 589_dbus_connection_remove_timeout (DBusConnection *connection, 590 DBusTimeout *timeout) 591{ 592 if (connection->timeouts) /* null during finalize */ 593 _dbus_timeout_list_remove_timeout (connection->timeouts, 594 timeout); 595} 596 597static void 598_dbus_connection_remove_timeout_locked (DBusConnection *connection, 599 DBusTimeout *timeout) 600{ 601 CONNECTION_LOCK (connection); 602 _dbus_connection_remove_timeout (connection, timeout); 603 CONNECTION_UNLOCK (connection); 604} 605 606/** 607 * Toggles a timeout and notifies app via connection's 608 * DBusTimeoutToggledFunction if available. It's an error to call this 609 * function on a timeout that was not previously added. 610 * 611 * @param connection the connection. 612 * @param timeout the timeout to toggle. 613 * @param enabled whether to enable or disable 614 */ 615void 616_dbus_connection_toggle_timeout (DBusConnection *connection, 617 DBusTimeout *timeout, 618 dbus_bool_t enabled) 619{ 620 if (connection->timeouts) /* null during finalize */ 621 _dbus_timeout_list_toggle_timeout (connection->timeouts, 622 timeout, enabled); 623} 624 625static dbus_bool_t 626_dbus_connection_attach_pending_call_unlocked (DBusConnection *connection, 627 DBusPendingCall *pending) 628{ 629 _dbus_assert (pending->reply_serial != 0); 630 631 if (!_dbus_connection_add_timeout (connection, pending->timeout)) 632 return FALSE; 633 634 if (!_dbus_hash_table_insert_int (connection->pending_replies, 635 pending->reply_serial, 636 pending)) 637 { 638 _dbus_connection_remove_timeout (connection, pending->timeout); 639 return FALSE; 640 } 641 642 pending->timeout_added = TRUE; 643 pending->connection = connection; 644 645 dbus_pending_call_ref (pending); 646 647 return TRUE; 648} 649 650static void 651free_pending_call_on_hash_removal (void *data) 652{ 653 DBusPendingCall *pending; 654 655 if (data == NULL) 656 return; 657 658 pending = data; 659 660 if (pending->connection) 661 { 662 if (pending->timeout_added) 663 { 664 _dbus_connection_remove_timeout (pending->connection, 665 pending->timeout); 666 pending->timeout_added = FALSE; 667 } 668 669 pending->connection = NULL; 670 671 dbus_pending_call_unref (pending); 672 } 673} 674 675static void 676_dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection, 677 DBusPendingCall *pending) 678{ 679 /* The idea here is to avoid finalizing the pending call 680 * with the lock held, since there's a destroy notifier 681 * in pending call that goes out to application code. 682 */ 683 dbus_pending_call_ref (pending); 684 _dbus_hash_table_remove_int (connection->pending_replies, 685 pending->reply_serial); 686 CONNECTION_UNLOCK (connection); 687 dbus_pending_call_unref (pending); 688} 689 690/** 691 * Removes a pending call from the connection, such that 692 * the pending reply will be ignored. May drop the last 693 * reference to the pending call. 694 * 695 * @param connection the connection 696 * @param pending the pending call 697 */ 698void 699_dbus_connection_remove_pending_call (DBusConnection *connection, 700 DBusPendingCall *pending) 701{ 702 CONNECTION_LOCK (connection); 703 _dbus_connection_detach_pending_call_and_unlock (connection, pending); 704} 705 706/** 707 * Completes a pending call with the given message, 708 * or if the message is #NULL, by timing out the pending call. 709 * 710 * @param pending the pending call 711 * @param message the message to complete the call with, or #NULL 712 * to time out the call 713 */ 714void 715_dbus_pending_call_complete_and_unlock (DBusPendingCall *pending, 716 DBusMessage *message) 717{ 718 if (message == NULL) 719 { 720 message = pending->timeout_link->data; 721 _dbus_list_clear (&pending->timeout_link); 722 } 723 else 724 dbus_message_ref (message); 725 726 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n", 727 message, 728 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ? 729 "method return" : 730 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? 731 "error" : "other type", 732 pending->reply_serial); 733 734 _dbus_assert (pending->reply == NULL); 735 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message)); 736 pending->reply = message; 737 738 dbus_pending_call_ref (pending); /* in case there's no app with a ref held */ 739 _dbus_connection_detach_pending_call_and_unlock (pending->connection, pending); 740 741 /* Must be called unlocked since it invokes app callback */ 742 _dbus_pending_call_notify (pending); 743 dbus_pending_call_unref (pending); 744} 745 746/** 747 * Acquire the transporter I/O path. This must be done before 748 * doing any I/O in the transporter. May sleep and drop the 749 * connection mutex while waiting for the I/O path. 750 * 751 * @param connection the connection. 752 * @param timeout_milliseconds maximum blocking time, or -1 for no limit. 753 * @returns TRUE if the I/O path was acquired. 754 */ 755static dbus_bool_t 756_dbus_connection_acquire_io_path (DBusConnection *connection, 757 int timeout_milliseconds) 758{ 759 dbus_bool_t res = TRUE; 760 761 if (connection->io_path_acquired) 762 { 763 if (timeout_milliseconds != -1) 764 res = dbus_condvar_wait_timeout (connection->io_path_cond, 765 connection->mutex, 766 timeout_milliseconds); 767 else 768 dbus_condvar_wait (connection->io_path_cond, connection->mutex); 769 } 770 771 if (res) 772 { 773 _dbus_assert (!connection->io_path_acquired); 774 775 connection->io_path_acquired = TRUE; 776 } 777 778 return res; 779} 780 781/** 782 * Release the I/O path when you're done with it. Only call 783 * after you've acquired the I/O. Wakes up at most one thread 784 * currently waiting to acquire the I/O path. 785 * 786 * @param connection the connection. 787 */ 788static void 789_dbus_connection_release_io_path (DBusConnection *connection) 790{ 791 _dbus_assert (connection->io_path_acquired); 792 793 connection->io_path_acquired = FALSE; 794 dbus_condvar_wake_one (connection->io_path_cond); 795} 796 797 798/** 799 * Queues incoming messages and sends outgoing messages for this 800 * connection, optionally blocking in the process. Each call to 801 * _dbus_connection_do_iteration() will call select() or poll() one 802 * time and then read or write data if possible. 803 * 804 * The purpose of this function is to be able to flush outgoing 805 * messages or queue up incoming messages without returning 806 * control to the application and causing reentrancy weirdness. 807 * 808 * The flags parameter allows you to specify whether to 809 * read incoming messages, write outgoing messages, or both, 810 * and whether to block if no immediate action is possible. 811 * 812 * The timeout_milliseconds parameter does nothing unless the 813 * iteration is blocking. 814 * 815 * If there are no outgoing messages and DBUS_ITERATION_DO_READING 816 * wasn't specified, then it's impossible to block, even if 817 * you specify DBUS_ITERATION_BLOCK; in that case the function 818 * returns immediately. 819 * 820 * Called with connection lock held. 821 * 822 * @param connection the connection. 823 * @param flags iteration flags. 824 * @param timeout_milliseconds maximum blocking time, or -1 for no limit. 825 */ 826void 827_dbus_connection_do_iteration (DBusConnection *connection, 828 unsigned int flags, 829 int timeout_milliseconds) 830{ 831 if (connection->n_outgoing == 0) 832 flags &= ~DBUS_ITERATION_DO_WRITING; 833 834 if (_dbus_connection_acquire_io_path (connection, 835 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0)) 836 { 837 _dbus_transport_do_iteration (connection->transport, 838 flags, timeout_milliseconds); 839 _dbus_connection_release_io_path (connection); 840 } 841} 842 843/** 844 * Creates a new connection for the given transport. A transport 845 * represents a message stream that uses some concrete mechanism, such 846 * as UNIX domain sockets. May return #NULL if insufficient 847 * memory exists to create the connection. 848 * 849 * @param transport the transport. 850 * @returns the new connection, or #NULL on failure. 851 */ 852DBusConnection* 853_dbus_connection_new_for_transport (DBusTransport *transport) 854{ 855 DBusConnection *connection; 856 DBusWatchList *watch_list; 857 DBusTimeoutList *timeout_list; 858 DBusHashTable *pending_replies; 859 DBusMutex *mutex; 860 DBusCondVar *message_returned_cond; 861 DBusCondVar *dispatch_cond; 862 DBusCondVar *io_path_cond; 863 DBusList *disconnect_link; 864 DBusMessage *disconnect_message; 865 DBusCounter *outgoing_counter; 866 DBusObjectTree *objects; 867 868 watch_list = NULL; 869 connection = NULL; 870 pending_replies = NULL; 871 timeout_list = NULL; 872 mutex = NULL; 873 message_returned_cond = NULL; 874 dispatch_cond = NULL; 875 io_path_cond = NULL; 876 disconnect_link = NULL; 877 disconnect_message = NULL; 878 outgoing_counter = NULL; 879 objects = NULL; 880 881 watch_list = _dbus_watch_list_new (); 882 if (watch_list == NULL) 883 goto error; 884 885 timeout_list = _dbus_timeout_list_new (); 886 if (timeout_list == NULL) 887 goto error; 888 889 pending_replies = 890 _dbus_hash_table_new (DBUS_HASH_INT, 891 NULL, 892 (DBusFreeFunction)free_pending_call_on_hash_removal); 893 if (pending_replies == NULL) 894 goto error; 895 896 connection = dbus_new0 (DBusConnection, 1); 897 if (connection == NULL) 898 goto error; 899 900 mutex = dbus_mutex_new (); 901 if (mutex == NULL) 902 goto error; 903 904 message_returned_cond = dbus_condvar_new (); 905 if (message_returned_cond == NULL) 906 goto error; 907 908 dispatch_cond = dbus_condvar_new (); 909 if (dispatch_cond == NULL) 910 goto error; 911 912 io_path_cond = dbus_condvar_new (); 913 if (io_path_cond == NULL) 914 goto error; 915 916 disconnect_message = dbus_message_new_signal (DBUS_PATH_ORG_FREEDESKTOP_LOCAL, 917 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, 918 "Disconnected"); 919 920 if (disconnect_message == NULL) 921 goto error; 922 923 disconnect_link = _dbus_list_alloc_link (disconnect_message); 924 if (disconnect_link == NULL) 925 goto error; 926 927 outgoing_counter = _dbus_counter_new (); 928 if (outgoing_counter == NULL) 929 goto error; 930 931 objects = _dbus_object_tree_new (connection); 932 if (objects == NULL) 933 goto error; 934 935 if (_dbus_modify_sigpipe) 936 _dbus_disable_sigpipe (); 937 938 connection->refcount.value = 1; 939 connection->mutex = mutex; 940 connection->dispatch_cond = dispatch_cond; 941 connection->io_path_cond = io_path_cond; 942 connection->message_returned_cond = message_returned_cond; 943 connection->transport = transport; 944 connection->watches = watch_list; 945 connection->timeouts = timeout_list; 946 connection->pending_replies = pending_replies; 947 connection->outgoing_counter = outgoing_counter; 948 connection->filter_list = NULL; 949 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */ 950 connection->objects = objects; 951 connection->exit_on_disconnect = FALSE; 952#ifndef DBUS_DISABLE_CHECKS 953 connection->generation = _dbus_current_generation; 954#endif 955 956 _dbus_data_slot_list_init (&connection->slot_list); 957 958 connection->client_serial = 1; 959 960 connection->disconnect_message_link = disconnect_link; 961 962 if (!_dbus_transport_set_connection (transport, connection)) 963 goto error; 964 965 _dbus_transport_ref (transport); 966 967 return connection; 968 969 error: 970 if (disconnect_message != NULL) 971 dbus_message_unref (disconnect_message); 972 973 if (disconnect_link != NULL) 974 _dbus_list_free_link (disconnect_link); 975 976 if (io_path_cond != NULL) 977 dbus_condvar_free (io_path_cond); 978 979 if (dispatch_cond != NULL) 980 dbus_condvar_free (dispatch_cond); 981 982 if (message_returned_cond != NULL) 983 dbus_condvar_free (message_returned_cond); 984 985 if (mutex != NULL) 986 dbus_mutex_free (mutex); 987 988 if (connection != NULL) 989 dbus_free (connection); 990 991 if (pending_replies) 992 _dbus_hash_table_unref (pending_replies); 993 994 if (watch_list) 995 _dbus_watch_list_free (watch_list); 996 997 if (timeout_list) 998 _dbus_timeout_list_free (timeout_list); 999 1000 if (outgoing_counter) 1001 _dbus_counter_unref (outgoing_counter); 1002 1003 if (objects) 1004 _dbus_object_tree_unref (objects); 1005 1006 return NULL; 1007} 1008 1009/** 1010 * Increments the reference count of a DBusConnection. 1011 * Requires that the caller already holds the connection lock. 1012 * 1013 * @param connection the connection. 1014 * @returns the connection. 1015 */ 1016DBusConnection * 1017_dbus_connection_ref_unlocked (DBusConnection *connection) 1018{ 1019 _dbus_return_val_if_fail (connection != NULL, NULL); 1020 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL); 1021 1022#ifdef DBUS_HAVE_ATOMIC_INT 1023 _dbus_atomic_inc (&connection->refcount); 1024#else 1025 _dbus_assert (connection->refcount.value > 0); 1026 connection->refcount.value += 1; 1027#endif 1028 1029 return connection; 1030} 1031 1032/** 1033 * Decrements the reference count of a DBusConnection. 1034 * Requires that the caller already holds the connection lock. 1035 * 1036 * @param connection the connection. 1037 */ 1038void 1039_dbus_connection_unref_unlocked (DBusConnection *connection) 1040{ 1041 dbus_bool_t last_unref; 1042 1043 _dbus_return_if_fail (connection != NULL); 1044 1045 /* The connection lock is better than the global 1046 * lock in the atomic increment fallback 1047 */ 1048 1049#ifdef DBUS_HAVE_ATOMIC_INT 1050 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1); 1051#else 1052 _dbus_assert (connection->refcount.value > 0); 1053 1054 connection->refcount.value -= 1; 1055 last_unref = (connection->refcount.value == 0); 1056#if 0 1057 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value); 1058#endif 1059#endif 1060 1061 if (last_unref) 1062 _dbus_connection_last_unref (connection); 1063} 1064 1065static dbus_uint32_t 1066_dbus_connection_get_next_client_serial (DBusConnection *connection) 1067{ 1068 int serial; 1069 1070 serial = connection->client_serial++; 1071 1072 if (connection->client_serial < 0) 1073 connection->client_serial = 1; 1074 1075 return serial; 1076} 1077 1078/** 1079 * A callback for use with dbus_watch_new() to create a DBusWatch. 1080 * 1081 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch() 1082 * and the virtual handle_watch in DBusTransport if we got rid of it. 1083 * The reason this is some work is threading, see the _dbus_connection_handle_watch() 1084 * implementation. 1085 * 1086 * @param watch the watch. 1087 * @param condition the current condition of the file descriptors being watched. 1088 * @param data must be a pointer to a #DBusConnection 1089 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory 1090 */ 1091dbus_bool_t 1092_dbus_connection_handle_watch (DBusWatch *watch, 1093 unsigned int condition, 1094 void *data) 1095{ 1096 DBusConnection *connection; 1097 dbus_bool_t retval; 1098 DBusDispatchStatus status; 1099 1100 connection = data; 1101 1102 CONNECTION_LOCK (connection); 1103 _dbus_connection_acquire_io_path (connection, -1); 1104 retval = _dbus_transport_handle_watch (connection->transport, 1105 watch, condition); 1106 _dbus_connection_release_io_path (connection); 1107 1108 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1109 1110 /* this calls out to user code */ 1111 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 1112 1113 return retval; 1114} 1115 1116/** @} */ 1117 1118/** 1119 * @addtogroup DBusConnection 1120 * 1121 * @{ 1122 */ 1123 1124/** 1125 * Opens a new connection to a remote address. 1126 * 1127 * @todo specify what the address parameter is. Right now 1128 * it's just the name of a UNIX domain socket. It should be 1129 * something more complex that encodes which transport to use. 1130 * 1131 * If the open fails, the function returns #NULL, and provides 1132 * a reason for the failure in the result parameter. Pass 1133 * #NULL for the result parameter if you aren't interested 1134 * in the reason for failure. 1135 * 1136 * @param address the address. 1137 * @param error address where an error can be returned. 1138 * @returns new connection, or #NULL on failure. 1139 */ 1140DBusConnection* 1141dbus_connection_open (const char *address, 1142 DBusError *error) 1143{ 1144 DBusConnection *connection; 1145 DBusTransport *transport; 1146 1147 _dbus_return_val_if_fail (address != NULL, NULL); 1148 _dbus_return_val_if_error_is_set (error, NULL); 1149 1150 transport = _dbus_transport_open (address, error); 1151 if (transport == NULL) 1152 { 1153 _DBUS_ASSERT_ERROR_IS_SET (error); 1154 return NULL; 1155 } 1156 1157 connection = _dbus_connection_new_for_transport (transport); 1158 1159 _dbus_transport_unref (transport); 1160 1161 if (connection == NULL) 1162 { 1163 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL); 1164 return NULL; 1165 } 1166 1167 return connection; 1168} 1169 1170/** 1171 * Increments the reference count of a DBusConnection. 1172 * 1173 * @param connection the connection. 1174 * @returns the connection. 1175 */ 1176DBusConnection * 1177dbus_connection_ref (DBusConnection *connection) 1178{ 1179 _dbus_return_val_if_fail (connection != NULL, NULL); 1180 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL); 1181 1182 /* The connection lock is better than the global 1183 * lock in the atomic increment fallback 1184 */ 1185 1186#ifdef DBUS_HAVE_ATOMIC_INT 1187 _dbus_atomic_inc (&connection->refcount); 1188#else 1189 CONNECTION_LOCK (connection); 1190 _dbus_assert (connection->refcount.value > 0); 1191 1192 connection->refcount.value += 1; 1193 CONNECTION_UNLOCK (connection); 1194#endif 1195 1196 return connection; 1197} 1198 1199static void 1200free_outgoing_message (void *element, 1201 void *data) 1202{ 1203 DBusMessage *message = element; 1204 DBusConnection *connection = data; 1205 1206 _dbus_message_remove_size_counter (message, 1207 connection->outgoing_counter, 1208 NULL); 1209 dbus_message_unref (message); 1210} 1211 1212/* This is run without the mutex held, but after the last reference 1213 * to the connection has been dropped we should have no thread-related 1214 * problems 1215 */ 1216static void 1217_dbus_connection_last_unref (DBusConnection *connection) 1218{ 1219 DBusList *link; 1220 1221 _dbus_verbose ("Finalizing connection %p\n", connection); 1222 1223 _dbus_assert (connection->refcount.value == 0); 1224 1225 /* You have to disconnect the connection before unref:ing it. Otherwise 1226 * you won't get the disconnected message. 1227 */ 1228 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport)); 1229 1230 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */ 1231 _dbus_object_tree_free_all_unlocked (connection->objects); 1232 1233 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL); 1234 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL); 1235 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL); 1236 1237 _dbus_watch_list_free (connection->watches); 1238 connection->watches = NULL; 1239 1240 _dbus_timeout_list_free (connection->timeouts); 1241 connection->timeouts = NULL; 1242 1243 _dbus_data_slot_list_free (&connection->slot_list); 1244 1245 link = _dbus_list_get_first_link (&connection->filter_list); 1246 while (link != NULL) 1247 { 1248 DBusMessageFilter *filter = link->data; 1249 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link); 1250 1251 filter->function = NULL; 1252 _dbus_message_filter_unref (filter); /* calls app callback */ 1253 link->data = NULL; 1254 1255 link = next; 1256 } 1257 _dbus_list_clear (&connection->filter_list); 1258 1259 /* ---- Done with stuff that invokes application callbacks */ 1260 1261 _dbus_object_tree_unref (connection->objects); 1262 1263 _dbus_hash_table_unref (connection->pending_replies); 1264 connection->pending_replies = NULL; 1265 1266 _dbus_list_clear (&connection->filter_list); 1267 1268 _dbus_list_foreach (&connection->outgoing_messages, 1269 free_outgoing_message, 1270 connection); 1271 _dbus_list_clear (&connection->outgoing_messages); 1272 1273 _dbus_list_foreach (&connection->incoming_messages, 1274 (DBusForeachFunction) dbus_message_unref, 1275 NULL); 1276 _dbus_list_clear (&connection->incoming_messages); 1277 1278 _dbus_counter_unref (connection->outgoing_counter); 1279 1280 _dbus_transport_unref (connection->transport); 1281 1282 if (connection->disconnect_message_link) 1283 { 1284 DBusMessage *message = connection->disconnect_message_link->data; 1285 dbus_message_unref (message); 1286 _dbus_list_free_link (connection->disconnect_message_link); 1287 } 1288 1289 _dbus_list_clear (&connection->link_cache); 1290 1291 dbus_condvar_free (connection->dispatch_cond); 1292 dbus_condvar_free (connection->io_path_cond); 1293 dbus_condvar_free (connection->message_returned_cond); 1294 1295 dbus_mutex_free (connection->mutex); 1296 1297 dbus_free (connection); 1298} 1299 1300/** 1301 * Decrements the reference count of a DBusConnection, and finalizes 1302 * it if the count reaches zero. It is a bug to drop the last reference 1303 * to a connection that has not been disconnected. 1304 * 1305 * @todo in practice it can be quite tricky to never unref a connection 1306 * that's still connected; maybe there's some way we could avoid 1307 * the requirement. 1308 * 1309 * @param connection the connection. 1310 */ 1311void 1312dbus_connection_unref (DBusConnection *connection) 1313{ 1314 dbus_bool_t last_unref; 1315 1316 _dbus_return_if_fail (connection != NULL); 1317 _dbus_return_if_fail (connection->generation == _dbus_current_generation); 1318 1319 /* The connection lock is better than the global 1320 * lock in the atomic increment fallback 1321 */ 1322 1323#ifdef DBUS_HAVE_ATOMIC_INT 1324 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1); 1325#else 1326 CONNECTION_LOCK (connection); 1327 1328 _dbus_assert (connection->refcount.value > 0); 1329 1330 connection->refcount.value -= 1; 1331 last_unref = (connection->refcount.value == 0); 1332 1333#if 0 1334 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value); 1335#endif 1336 1337 CONNECTION_UNLOCK (connection); 1338#endif 1339 1340 if (last_unref) 1341 _dbus_connection_last_unref (connection); 1342} 1343 1344/** 1345 * Closes the connection, so no further data can be sent or received. 1346 * Any further attempts to send data will result in errors. This 1347 * function does not affect the connection's reference count. It's 1348 * safe to disconnect a connection more than once; all calls after the 1349 * first do nothing. It's impossible to "reconnect" a connection, a 1350 * new connection must be created. This function may result in a call 1351 * to the DBusDispatchStatusFunction set with 1352 * dbus_connection_set_dispatch_status_function(), as the disconnect 1353 * message it generates needs to be dispatched. 1354 * 1355 * @param connection the connection. 1356 */ 1357void 1358dbus_connection_disconnect (DBusConnection *connection) 1359{ 1360 DBusDispatchStatus status; 1361 1362 _dbus_return_if_fail (connection != NULL); 1363 _dbus_return_if_fail (connection->generation == _dbus_current_generation); 1364 1365 _dbus_verbose ("Disconnecting %p\n", connection); 1366 1367 CONNECTION_LOCK (connection); 1368 _dbus_transport_disconnect (connection->transport); 1369 1370 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1371 1372 /* this calls out to user code */ 1373 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 1374} 1375 1376static dbus_bool_t 1377_dbus_connection_get_is_connected_unlocked (DBusConnection *connection) 1378{ 1379 return _dbus_transport_get_is_connected (connection->transport); 1380} 1381 1382/** 1383 * Gets whether the connection is currently connected. All 1384 * connections are connected when they are opened. A connection may 1385 * become disconnected when the remote application closes its end, or 1386 * exits; a connection may also be disconnected with 1387 * dbus_connection_disconnect(). 1388 * 1389 * @param connection the connection. 1390 * @returns #TRUE if the connection is still alive. 1391 */ 1392dbus_bool_t 1393dbus_connection_get_is_connected (DBusConnection *connection) 1394{ 1395 dbus_bool_t res; 1396 1397 _dbus_return_val_if_fail (connection != NULL, FALSE); 1398 1399 CONNECTION_LOCK (connection); 1400 res = _dbus_connection_get_is_connected_unlocked (connection); 1401 CONNECTION_UNLOCK (connection); 1402 1403 return res; 1404} 1405 1406/** 1407 * Gets whether the connection was authenticated. (Note that 1408 * if the connection was authenticated then disconnected, 1409 * this function still returns #TRUE) 1410 * 1411 * @param connection the connection 1412 * @returns #TRUE if the connection was ever authenticated 1413 */ 1414dbus_bool_t 1415dbus_connection_get_is_authenticated (DBusConnection *connection) 1416{ 1417 dbus_bool_t res; 1418 1419 _dbus_return_val_if_fail (connection != NULL, FALSE); 1420 1421 CONNECTION_LOCK (connection); 1422 res = _dbus_transport_get_is_authenticated (connection->transport); 1423 CONNECTION_UNLOCK (connection); 1424 1425 return res; 1426} 1427 1428/** 1429 * Set whether _exit() should be called when the connection receives a 1430 * disconnect signal. The call to _exit() comes after any handlers for 1431 * the disconnect signal run; handlers can cancel the exit by calling 1432 * this function. 1433 * 1434 * By default, exit_on_disconnect is #FALSE; but for message bus 1435 * connections returned from dbus_bus_get() it will be toggled on 1436 * by default. 1437 * 1438 * @param connection the connection 1439 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal 1440 */ 1441void 1442dbus_connection_set_exit_on_disconnect (DBusConnection *connection, 1443 dbus_bool_t exit_on_disconnect) 1444{ 1445 _dbus_return_if_fail (connection != NULL); 1446 1447 CONNECTION_LOCK (connection); 1448 connection->exit_on_disconnect = exit_on_disconnect != FALSE; 1449 CONNECTION_UNLOCK (connection); 1450} 1451 1452static DBusPreallocatedSend* 1453_dbus_connection_preallocate_send_unlocked (DBusConnection *connection) 1454{ 1455 DBusPreallocatedSend *preallocated; 1456 1457 _dbus_return_val_if_fail (connection != NULL, NULL); 1458 1459 preallocated = dbus_new (DBusPreallocatedSend, 1); 1460 if (preallocated == NULL) 1461 return NULL; 1462 1463 if (connection->link_cache != NULL) 1464 { 1465 preallocated->queue_link = 1466 _dbus_list_pop_first_link (&connection->link_cache); 1467 preallocated->queue_link->data = NULL; 1468 } 1469 else 1470 { 1471 preallocated->queue_link = _dbus_list_alloc_link (NULL); 1472 if (preallocated->queue_link == NULL) 1473 goto failed_0; 1474 } 1475 1476 if (connection->link_cache != NULL) 1477 { 1478 preallocated->counter_link = 1479 _dbus_list_pop_first_link (&connection->link_cache); 1480 preallocated->counter_link->data = connection->outgoing_counter; 1481 } 1482 else 1483 { 1484 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter); 1485 if (preallocated->counter_link == NULL) 1486 goto failed_1; 1487 } 1488 1489 _dbus_counter_ref (preallocated->counter_link->data); 1490 1491 preallocated->connection = connection; 1492 1493 return preallocated; 1494 1495 failed_1: 1496 _dbus_list_free_link (preallocated->queue_link); 1497 failed_0: 1498 dbus_free (preallocated); 1499 1500 return NULL; 1501} 1502 1503/** 1504 * Preallocates resources needed to send a message, allowing the message 1505 * to be sent without the possibility of memory allocation failure. 1506 * Allows apps to create a future guarantee that they can send 1507 * a message regardless of memory shortages. 1508 * 1509 * @param connection the connection we're preallocating for. 1510 * @returns the preallocated resources, or #NULL 1511 */ 1512DBusPreallocatedSend* 1513dbus_connection_preallocate_send (DBusConnection *connection) 1514{ 1515 DBusPreallocatedSend *preallocated; 1516 1517 _dbus_return_val_if_fail (connection != NULL, NULL); 1518 1519 CONNECTION_LOCK (connection); 1520 1521 preallocated = 1522 _dbus_connection_preallocate_send_unlocked (connection); 1523 1524 CONNECTION_UNLOCK (connection); 1525 1526 return preallocated; 1527} 1528 1529/** 1530 * Frees preallocated message-sending resources from 1531 * dbus_connection_preallocate_send(). Should only 1532 * be called if the preallocated resources are not used 1533 * to send a message. 1534 * 1535 * @param connection the connection 1536 * @param preallocated the resources 1537 */ 1538void 1539dbus_connection_free_preallocated_send (DBusConnection *connection, 1540 DBusPreallocatedSend *preallocated) 1541{ 1542 _dbus_return_if_fail (connection != NULL); 1543 _dbus_return_if_fail (preallocated != NULL); 1544 _dbus_return_if_fail (connection == preallocated->connection); 1545 1546 _dbus_list_free_link (preallocated->queue_link); 1547 _dbus_counter_unref (preallocated->counter_link->data); 1548 _dbus_list_free_link (preallocated->counter_link); 1549 dbus_free (preallocated); 1550} 1551 1552static void 1553_dbus_connection_send_preallocated_unlocked (DBusConnection *connection, 1554 DBusPreallocatedSend *preallocated, 1555 DBusMessage *message, 1556 dbus_uint32_t *client_serial) 1557{ 1558 dbus_uint32_t serial; 1559 const char *sig; 1560 1561 preallocated->queue_link->data = message; 1562 _dbus_list_prepend_link (&connection->outgoing_messages, 1563 preallocated->queue_link); 1564 1565 _dbus_message_add_size_counter_link (message, 1566 preallocated->counter_link); 1567 1568 dbus_free (preallocated); 1569 preallocated = NULL; 1570 1571 dbus_message_ref (message); 1572 1573 connection->n_outgoing += 1; 1574 1575 sig = dbus_message_get_signature (message); 1576#ifndef DBUS_DISABLE_ASSERT 1577 { 1578 DBusString foo; 1579 _dbus_verbose (" validating signature '%s'\n", sig); 1580 _dbus_string_init_const (&foo, sig); 1581 _dbus_assert (_dbus_string_validate_signature (&foo, 0, 1582 _dbus_string_get_length (&foo))); 1583 } 1584#endif 1585 1586 _dbus_verbose ("Message %p (%d %s %s '%s') added to outgoing queue %p, %d pending to send\n", 1587 message, 1588 dbus_message_get_type (message), 1589 dbus_message_get_interface (message) ? 1590 dbus_message_get_interface (message) : 1591 "no interface", 1592 dbus_message_get_member (message) ? 1593 dbus_message_get_member (message) : 1594 "no member", 1595 sig, 1596 connection, 1597 connection->n_outgoing); 1598 1599 if (dbus_message_get_serial (message) == 0) 1600 { 1601 serial = _dbus_connection_get_next_client_serial (connection); 1602 _dbus_message_set_serial (message, serial); 1603 if (client_serial) 1604 *client_serial = serial; 1605 } 1606 else 1607 { 1608 if (client_serial) 1609 *client_serial = dbus_message_get_serial (message); 1610 } 1611 1612 _dbus_message_lock (message); 1613 1614 /* Now we need to run an iteration to hopefully just write the messages 1615 * out immediately, and otherwise get them queued up 1616 */ 1617 _dbus_connection_do_iteration (connection, 1618 DBUS_ITERATION_DO_WRITING, 1619 -1); 1620 1621 /* If stuff is still queued up, be sure we wake up the main loop */ 1622 if (connection->n_outgoing > 0) 1623 _dbus_connection_wakeup_mainloop (connection); 1624} 1625 1626/** 1627 * Sends a message using preallocated resources. This function cannot fail. 1628 * It works identically to dbus_connection_send() in other respects. 1629 * Preallocated resources comes from dbus_connection_preallocate_send(). 1630 * This function "consumes" the preallocated resources, they need not 1631 * be freed separately. 1632 * 1633 * @param connection the connection 1634 * @param preallocated the preallocated resources 1635 * @param message the message to send 1636 * @param client_serial return location for client serial assigned to the message 1637 */ 1638void 1639dbus_connection_send_preallocated (DBusConnection *connection, 1640 DBusPreallocatedSend *preallocated, 1641 DBusMessage *message, 1642 dbus_uint32_t *client_serial) 1643{ 1644 _dbus_return_if_fail (connection != NULL); 1645 _dbus_return_if_fail (preallocated != NULL); 1646 _dbus_return_if_fail (message != NULL); 1647 _dbus_return_if_fail (preallocated->connection == connection); 1648 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL || 1649 (dbus_message_get_interface (message) != NULL && 1650 dbus_message_get_member (message) != NULL)); 1651 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL || 1652 (dbus_message_get_interface (message) != NULL && 1653 dbus_message_get_member (message) != NULL)); 1654 1655 CONNECTION_LOCK (connection); 1656 _dbus_connection_send_preallocated_unlocked (connection, 1657 preallocated, 1658 message, client_serial); 1659 CONNECTION_UNLOCK (connection); 1660} 1661 1662static dbus_bool_t 1663_dbus_connection_send_unlocked (DBusConnection *connection, 1664 DBusMessage *message, 1665 dbus_uint32_t *client_serial) 1666{ 1667 DBusPreallocatedSend *preallocated; 1668 1669 _dbus_assert (connection != NULL); 1670 _dbus_assert (message != NULL); 1671 1672 preallocated = _dbus_connection_preallocate_send_unlocked (connection); 1673 if (preallocated == NULL) 1674 return FALSE; 1675 1676 1677 _dbus_connection_send_preallocated_unlocked (connection, 1678 preallocated, 1679 message, 1680 client_serial); 1681 return TRUE; 1682} 1683 1684/** 1685 * Adds a message to the outgoing message queue. Does not block to 1686 * write the message to the network; that happens asynchronously. To 1687 * force the message to be written, call dbus_connection_flush(). 1688 * Because this only queues the message, the only reason it can 1689 * fail is lack of memory. Even if the connection is disconnected, 1690 * no error will be returned. 1691 * 1692 * If the function fails due to lack of memory, it returns #FALSE. 1693 * The function will never fail for other reasons; even if the 1694 * connection is disconnected, you can queue an outgoing message, 1695 * though obviously it won't be sent. 1696 * 1697 * @param connection the connection. 1698 * @param message the message to write. 1699 * @param client_serial return location for client serial. 1700 * @returns #TRUE on success. 1701 */ 1702dbus_bool_t 1703dbus_connection_send (DBusConnection *connection, 1704 DBusMessage *message, 1705 dbus_uint32_t *client_serial) 1706{ 1707 _dbus_return_val_if_fail (connection != NULL, FALSE); 1708 _dbus_return_val_if_fail (message != NULL, FALSE); 1709 1710 CONNECTION_LOCK (connection); 1711 1712 if (!_dbus_connection_send_unlocked (connection, message, client_serial)) 1713 { 1714 CONNECTION_UNLOCK (connection); 1715 return FALSE; 1716 } 1717 1718 CONNECTION_UNLOCK (connection); 1719 return TRUE; 1720} 1721 1722static dbus_bool_t 1723reply_handler_timeout (void *data) 1724{ 1725 DBusConnection *connection; 1726 DBusDispatchStatus status; 1727 DBusPendingCall *pending = data; 1728 1729 connection = pending->connection; 1730 1731 CONNECTION_LOCK (connection); 1732 if (pending->timeout_link) 1733 { 1734 _dbus_connection_queue_synthesized_message_link (connection, 1735 pending->timeout_link); 1736 pending->timeout_link = NULL; 1737 } 1738 1739 _dbus_connection_remove_timeout (connection, 1740 pending->timeout); 1741 pending->timeout_added = FALSE; 1742 1743 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1744 1745 /* Unlocks, and calls out to user code */ 1746 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 1747 1748 return TRUE; 1749} 1750 1751/** 1752 * Queues a message to send, as with dbus_connection_send_message(), 1753 * but also returns a #DBusPendingCall used to receive a reply to the 1754 * message. If no reply is received in the given timeout_milliseconds, 1755 * this function expires the pending reply and generates a synthetic 1756 * error reply (generated in-process, not by the remote application) 1757 * indicating that a timeout occurred. 1758 * 1759 * A #DBusPendingCall will see a reply message after any filters, but 1760 * before any object instances or other handlers. A #DBusPendingCall 1761 * will always see exactly one reply message, unless it's cancelled 1762 * with dbus_pending_call_cancel(). 1763 * 1764 * If a filter filters out the reply before the handler sees it, the 1765 * reply is immediately timed out and a timeout error reply is 1766 * generated. If a filter removes the timeout error reply then the 1767 * #DBusPendingCall will get confused. Filtering the timeout error 1768 * is thus considered a bug and will print a warning. 1769 * 1770 * If #NULL is passed for the pending_return, the #DBusPendingCall 1771 * will still be generated internally, and used to track 1772 * the message reply timeout. This means a timeout error will 1773 * occur if no reply arrives, unlike with dbus_connection_send(). 1774 * 1775 * If -1 is passed for the timeout, a sane default timeout is used. -1 1776 * is typically the best value for the timeout for this reason, unless 1777 * you want a very short or very long timeout. There is no way to 1778 * avoid a timeout entirely, other than passing INT_MAX for the 1779 * timeout to postpone it indefinitely. 1780 * 1781 * @param connection the connection 1782 * @param message the message to send 1783 * @param pending_return return location for a #DBusPendingCall object, or #NULL 1784 * @param timeout_milliseconds timeout in milliseconds or -1 for default 1785 * @returns #TRUE if the message is successfully queued, #FALSE if no memory. 1786 * 1787 */ 1788dbus_bool_t 1789dbus_connection_send_with_reply (DBusConnection *connection, 1790 DBusMessage *message, 1791 DBusPendingCall **pending_return, 1792 int timeout_milliseconds) 1793{ 1794 DBusPendingCall *pending; 1795 DBusMessage *reply; 1796 DBusList *reply_link; 1797 dbus_int32_t serial = -1; 1798 1799 _dbus_return_val_if_fail (connection != NULL, FALSE); 1800 _dbus_return_val_if_fail (message != NULL, FALSE); 1801 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); 1802 1803 if (pending_return) 1804 *pending_return = NULL; 1805 1806 pending = _dbus_pending_call_new (connection, 1807 timeout_milliseconds, 1808 reply_handler_timeout); 1809 1810 if (pending == NULL) 1811 return FALSE; 1812 1813 CONNECTION_LOCK (connection); 1814 1815 /* Assign a serial to the message */ 1816 if (dbus_message_get_serial (message) == 0) 1817 { 1818 serial = _dbus_connection_get_next_client_serial (connection); 1819 _dbus_message_set_serial (message, serial); 1820 } 1821 1822 pending->reply_serial = serial; 1823 1824 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY, 1825 "No reply within specified time"); 1826 if (reply == NULL) 1827 goto error; 1828 1829 reply_link = _dbus_list_alloc_link (reply); 1830 if (reply_link == NULL) 1831 { 1832 CONNECTION_UNLOCK (connection); 1833 dbus_message_unref (reply); 1834 goto error_unlocked; 1835 } 1836 1837 pending->timeout_link = reply_link; 1838 1839 /* Insert the serial in the pending replies hash; 1840 * hash takes a refcount on DBusPendingCall. 1841 * Also, add the timeout. 1842 */ 1843 if (!_dbus_connection_attach_pending_call_unlocked (connection, 1844 pending)) 1845 goto error; 1846 1847 if (!_dbus_connection_send_unlocked (connection, message, NULL)) 1848 { 1849 _dbus_connection_detach_pending_call_and_unlock (connection, 1850 pending); 1851 goto error_unlocked; 1852 } 1853 1854 if (pending_return) 1855 *pending_return = pending; 1856 else 1857 dbus_pending_call_unref (pending); 1858 1859 CONNECTION_UNLOCK (connection); 1860 1861 return TRUE; 1862 1863 error: 1864 CONNECTION_UNLOCK (connection); 1865 error_unlocked: 1866 dbus_pending_call_unref (pending); 1867 return FALSE; 1868} 1869 1870static DBusMessage* 1871check_for_reply_unlocked (DBusConnection *connection, 1872 dbus_uint32_t client_serial) 1873{ 1874 DBusList *link; 1875 1876 link = _dbus_list_get_first_link (&connection->incoming_messages); 1877 1878 while (link != NULL) 1879 { 1880 DBusMessage *reply = link->data; 1881 1882 if (dbus_message_get_reply_serial (reply) == client_serial) 1883 { 1884 _dbus_list_remove_link (&connection->incoming_messages, link); 1885 connection->n_incoming -= 1; 1886 return reply; 1887 } 1888 link = _dbus_list_get_next_link (&connection->incoming_messages, link); 1889 } 1890 1891 return NULL; 1892} 1893 1894/** 1895 * Blocks a certain time period while waiting for a reply. 1896 * If no reply arrives, returns #NULL. 1897 * 1898 * @todo could use performance improvements (it keeps scanning 1899 * the whole message queue for example) and has thread issues, 1900 * see comments in source 1901 * 1902 * Does not re-enter the main loop or run filter/path-registered 1903 * callbacks. The reply to the message will not be seen by 1904 * filter callbacks. 1905 * 1906 * @param connection the connection 1907 * @param client_serial the reply serial to wait for 1908 * @param timeout_milliseconds timeout in milliseconds or -1 for default 1909 * @returns the message that is the reply or #NULL if no reply 1910 */ 1911DBusMessage* 1912_dbus_connection_block_for_reply (DBusConnection *connection, 1913 dbus_uint32_t client_serial, 1914 int timeout_milliseconds) 1915{ 1916 long start_tv_sec, start_tv_usec; 1917 long end_tv_sec, end_tv_usec; 1918 long tv_sec, tv_usec; 1919 DBusDispatchStatus status; 1920 1921 _dbus_return_val_if_fail (connection != NULL, NULL); 1922 _dbus_return_val_if_fail (client_serial != 0, NULL); 1923 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); 1924 1925 if (timeout_milliseconds == -1) 1926 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE; 1927 1928 /* it would probably seem logical to pass in _DBUS_INT_MAX 1929 * for infinite timeout, but then math below would get 1930 * all overflow-prone, so smack that down. 1931 */ 1932 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6) 1933 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6; 1934 1935 /* Flush message queue */ 1936 dbus_connection_flush (connection); 1937 1938 CONNECTION_LOCK (connection); 1939 1940 _dbus_get_current_time (&start_tv_sec, &start_tv_usec); 1941 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000; 1942 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000; 1943 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND; 1944 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND; 1945 1946 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec to %ld sec %ld usec\n", 1947 timeout_milliseconds, 1948 client_serial, 1949 start_tv_sec, start_tv_usec, 1950 end_tv_sec, end_tv_usec); 1951 1952 /* Now we wait... */ 1953 /* THREAD TODO: This is busted. What if a dispatch() or pop_message 1954 * gets the message before we do? 1955 */ 1956 /* always block at least once as we know we don't have the reply yet */ 1957 _dbus_connection_do_iteration (connection, 1958 DBUS_ITERATION_DO_READING | 1959 DBUS_ITERATION_BLOCK, 1960 timeout_milliseconds); 1961 1962 recheck_status: 1963 1964 /* queue messages and get status */ 1965 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1966 1967 if (status == DBUS_DISPATCH_DATA_REMAINS) 1968 { 1969 DBusMessage *reply; 1970 1971 reply = check_for_reply_unlocked (connection, client_serial); 1972 if (reply != NULL) 1973 { 1974 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1975 1976 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n"); 1977 1978 /* Unlocks, and calls out to user code */ 1979 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 1980 1981 return reply; 1982 } 1983 } 1984 1985 _dbus_get_current_time (&tv_sec, &tv_usec); 1986 1987 if (!_dbus_connection_get_is_connected_unlocked (connection)) 1988 return NULL; 1989 else if (tv_sec < start_tv_sec) 1990 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n"); 1991 else if (connection->disconnect_message_link == NULL) 1992 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n"); 1993 else if (tv_sec < end_tv_sec || 1994 (tv_sec == end_tv_sec && tv_usec < end_tv_usec)) 1995 { 1996 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 + 1997 (end_tv_usec - tv_usec) / 1000; 1998 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds); 1999 _dbus_assert (timeout_milliseconds >= 0); 2000 2001 if (status == DBUS_DISPATCH_NEED_MEMORY) 2002 { 2003 /* Try sleeping a bit, as we aren't sure we need to block for reading, 2004 * we may already have a reply in the buffer and just can't process 2005 * it. 2006 */ 2007 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n"); 2008 2009 if (timeout_milliseconds < 100) 2010 ; /* just busy loop */ 2011 else if (timeout_milliseconds <= 1000) 2012 _dbus_sleep_milliseconds (timeout_milliseconds / 3); 2013 else 2014 _dbus_sleep_milliseconds (1000); 2015 } 2016 else 2017 { 2018 /* block again, we don't have the reply buffered yet. */ 2019 _dbus_connection_do_iteration (connection, 2020 DBUS_ITERATION_DO_READING | 2021 DBUS_ITERATION_BLOCK, 2022 timeout_milliseconds); 2023 } 2024 2025 goto recheck_status; 2026 } 2027 2028 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n", 2029 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000); 2030 2031 /* unlocks and calls out to user code */ 2032 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2033 2034 return NULL; 2035} 2036 2037/** 2038 * Sends a message and blocks a certain time period while waiting for 2039 * a reply. This function does not reenter the main loop, 2040 * i.e. messages other than the reply are queued up but not 2041 * processed. This function is used to do non-reentrant "method 2042 * calls." 2043 * 2044 * If a normal reply is received, it is returned, and removed from the 2045 * incoming message queue. If it is not received, #NULL is returned 2046 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is 2047 * received, it is converted to a #DBusError and returned as an error, 2048 * then the reply message is deleted. If something else goes wrong, 2049 * result is set to whatever is appropriate, such as 2050 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED. 2051 * 2052 * @param connection the connection 2053 * @param message the message to send 2054 * @param timeout_milliseconds timeout in milliseconds or -1 for default 2055 * @param error return location for error message 2056 * @returns the message that is the reply or #NULL with an error code if the 2057 * function fails. 2058 */ 2059DBusMessage * 2060dbus_connection_send_with_reply_and_block (DBusConnection *connection, 2061 DBusMessage *message, 2062 int timeout_milliseconds, 2063 DBusError *error) 2064{ 2065 dbus_uint32_t client_serial; 2066 DBusMessage *reply; 2067 2068 _dbus_return_val_if_fail (connection != NULL, NULL); 2069 _dbus_return_val_if_fail (message != NULL, NULL); 2070 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); 2071 _dbus_return_val_if_error_is_set (error, NULL); 2072 2073 if (!dbus_connection_send (connection, message, &client_serial)) 2074 { 2075 _DBUS_SET_OOM (error); 2076 return NULL; 2077 } 2078 2079 reply = _dbus_connection_block_for_reply (connection, 2080 client_serial, 2081 timeout_milliseconds); 2082 2083 if (reply == NULL) 2084 { 2085 if (dbus_connection_get_is_connected (connection)) 2086 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply"); 2087 else 2088 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply"); 2089 2090 return NULL; 2091 } 2092 else if (dbus_set_error_from_message (error, reply)) 2093 { 2094 dbus_message_unref (reply); 2095 return NULL; 2096 } 2097 else 2098 return reply; 2099} 2100 2101/** 2102 * Blocks until the outgoing message queue is empty. 2103 * 2104 * @param connection the connection. 2105 */ 2106void 2107dbus_connection_flush (DBusConnection *connection) 2108{ 2109 /* We have to specify DBUS_ITERATION_DO_READING here because 2110 * otherwise we could have two apps deadlock if they are both doing 2111 * a flush(), and the kernel buffers fill up. This could change the 2112 * dispatch status. 2113 */ 2114 DBusDispatchStatus status; 2115 2116 _dbus_return_if_fail (connection != NULL); 2117 2118 CONNECTION_LOCK (connection); 2119 while (connection->n_outgoing > 0 && 2120 _dbus_connection_get_is_connected_unlocked (connection)) 2121 _dbus_connection_do_iteration (connection, 2122 DBUS_ITERATION_DO_READING | 2123 DBUS_ITERATION_DO_WRITING | 2124 DBUS_ITERATION_BLOCK, 2125 -1); 2126 2127 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2128 2129 /* Unlocks and calls out to user code */ 2130 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2131} 2132 2133/* Call with mutex held. Will drop it while waiting and re-acquire 2134 * before returning 2135 */ 2136static void 2137_dbus_connection_wait_for_borrowed (DBusConnection *connection) 2138{ 2139 _dbus_assert (connection->message_borrowed != NULL); 2140 2141 while (connection->message_borrowed != NULL) 2142 dbus_condvar_wait (connection->message_returned_cond, connection->mutex); 2143} 2144 2145/** 2146 * Returns the first-received message from the incoming message queue, 2147 * leaving it in the queue. If the queue is empty, returns #NULL. 2148 * 2149 * The caller does not own a reference to the returned message, and 2150 * must either return it using dbus_connection_return_message() or 2151 * keep it after calling dbus_connection_steal_borrowed_message(). No 2152 * one can get at the message while its borrowed, so return it as 2153 * quickly as possible and don't keep a reference to it after 2154 * returning it. If you need to keep the message, make a copy of it. 2155 * 2156 * @param connection the connection. 2157 * @returns next message in the incoming queue. 2158 */ 2159DBusMessage* 2160dbus_connection_borrow_message (DBusConnection *connection) 2161{ 2162 DBusMessage *message; 2163 DBusDispatchStatus status; 2164 2165 _dbus_return_val_if_fail (connection != NULL, NULL); 2166 /* can't borrow during dispatch */ 2167 _dbus_return_val_if_fail (!connection->dispatch_acquired, NULL); 2168 2169 /* this is called for the side effect that it queues 2170 * up any messages from the transport 2171 */ 2172 status = dbus_connection_get_dispatch_status (connection); 2173 if (status != DBUS_DISPATCH_DATA_REMAINS) 2174 return NULL; 2175 2176 CONNECTION_LOCK (connection); 2177 2178 if (connection->message_borrowed != NULL) 2179 _dbus_connection_wait_for_borrowed (connection); 2180 2181 message = _dbus_list_get_first (&connection->incoming_messages); 2182 2183 if (message) 2184 connection->message_borrowed = message; 2185 2186 CONNECTION_UNLOCK (connection); 2187 return message; 2188} 2189 2190/** 2191 * Used to return a message after peeking at it using 2192 * dbus_connection_borrow_message(). 2193 * 2194 * @param connection the connection 2195 * @param message the message from dbus_connection_borrow_message() 2196 */ 2197void 2198dbus_connection_return_message (DBusConnection *connection, 2199 DBusMessage *message) 2200{ 2201 _dbus_return_if_fail (connection != NULL); 2202 _dbus_return_if_fail (message != NULL); 2203 /* can't borrow during dispatch */ 2204 _dbus_return_if_fail (!connection->dispatch_acquired); 2205 2206 CONNECTION_LOCK (connection); 2207 2208 _dbus_assert (message == connection->message_borrowed); 2209 2210 connection->message_borrowed = NULL; 2211 dbus_condvar_wake_all (connection->message_returned_cond); 2212 2213 CONNECTION_UNLOCK (connection); 2214} 2215 2216/** 2217 * Used to keep a message after peeking at it using 2218 * dbus_connection_borrow_message(). Before using this function, see 2219 * the caveats/warnings in the documentation for 2220 * dbus_connection_pop_message(). 2221 * 2222 * @param connection the connection 2223 * @param message the message from dbus_connection_borrow_message() 2224 */ 2225void 2226dbus_connection_steal_borrowed_message (DBusConnection *connection, 2227 DBusMessage *message) 2228{ 2229 DBusMessage *pop_message; 2230 2231 _dbus_return_if_fail (connection != NULL); 2232 _dbus_return_if_fail (message != NULL); 2233 /* can't borrow during dispatch */ 2234 _dbus_return_if_fail (!connection->dispatch_acquired); 2235 2236 CONNECTION_LOCK (connection); 2237 2238 _dbus_assert (message == connection->message_borrowed); 2239 2240 pop_message = _dbus_list_pop_first (&connection->incoming_messages); 2241 _dbus_assert (message == pop_message); 2242 2243 connection->n_incoming -= 1; 2244 2245 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n", 2246 message, connection->n_incoming); 2247 2248 connection->message_borrowed = NULL; 2249 dbus_condvar_wake_all (connection->message_returned_cond); 2250 2251 CONNECTION_UNLOCK (connection); 2252} 2253 2254/* See dbus_connection_pop_message, but requires the caller to own 2255 * the lock before calling. May drop the lock while running. 2256 */ 2257static DBusList* 2258_dbus_connection_pop_message_link_unlocked (DBusConnection *connection) 2259{ 2260 if (connection->message_borrowed != NULL) 2261 _dbus_connection_wait_for_borrowed (connection); 2262 2263 if (connection->n_incoming > 0) 2264 { 2265 DBusList *link; 2266 2267 link = _dbus_list_pop_first_link (&connection->incoming_messages); 2268 connection->n_incoming -= 1; 2269 2270 _dbus_verbose ("Message %p (%d %s %s '%s') removed from incoming queue %p, %d incoming\n", 2271 link->data, 2272 dbus_message_get_type (link->data), 2273 dbus_message_get_interface (link->data) ? 2274 dbus_message_get_interface (link->data) : 2275 "no interface", 2276 dbus_message_get_member (link->data) ? 2277 dbus_message_get_member (link->data) : 2278 "no member", 2279 dbus_message_get_signature (link->data), 2280 connection, connection->n_incoming); 2281 2282 return link; 2283 } 2284 else 2285 return NULL; 2286} 2287 2288/* See dbus_connection_pop_message, but requires the caller to own 2289 * the lock before calling. May drop the lock while running. 2290 */ 2291static DBusMessage* 2292_dbus_connection_pop_message_unlocked (DBusConnection *connection) 2293{ 2294 DBusList *link; 2295 2296 link = _dbus_connection_pop_message_link_unlocked (connection); 2297 2298 if (link != NULL) 2299 { 2300 DBusMessage *message; 2301 2302 message = link->data; 2303 2304 _dbus_list_free_link (link); 2305 2306 return message; 2307 } 2308 else 2309 return NULL; 2310} 2311 2312static void 2313_dbus_connection_putback_message_link_unlocked (DBusConnection *connection, 2314 DBusList *message_link) 2315{ 2316 _dbus_assert (message_link != NULL); 2317 /* You can't borrow a message while a link is outstanding */ 2318 _dbus_assert (connection->message_borrowed == NULL); 2319 2320 _dbus_list_prepend_link (&connection->incoming_messages, 2321 message_link); 2322 connection->n_incoming += 1; 2323 2324 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n", 2325 message_link->data, 2326 dbus_message_get_type (message_link->data), 2327 dbus_message_get_interface (message_link->data) ? 2328 dbus_message_get_interface (message_link->data) : 2329 "no interface", 2330 dbus_message_get_member (message_link->data) ? 2331 dbus_message_get_member (message_link->data) : 2332 "no member", 2333 dbus_message_get_signature (message_link->data), 2334 connection, connection->n_incoming); 2335} 2336 2337/** 2338 * Returns the first-received message from the incoming message queue, 2339 * removing it from the queue. The caller owns a reference to the 2340 * returned message. If the queue is empty, returns #NULL. 2341 * 2342 * This function bypasses any message handlers that are registered, 2343 * and so using it is usually wrong. Instead, let the main loop invoke 2344 * dbus_connection_dispatch(). Popping messages manually is only 2345 * useful in very simple programs that don't share a #DBusConnection 2346 * with any libraries or other modules. 2347 * 2348 * @param connection the connection. 2349 * @returns next message in the incoming queue. 2350 */ 2351DBusMessage* 2352dbus_connection_pop_message (DBusConnection *connection) 2353{ 2354 DBusMessage *message; 2355 DBusDispatchStatus status; 2356 2357 /* this is called for the side effect that it queues 2358 * up any messages from the transport 2359 */ 2360 status = dbus_connection_get_dispatch_status (connection); 2361 if (status != DBUS_DISPATCH_DATA_REMAINS) 2362 return NULL; 2363 2364 CONNECTION_LOCK (connection); 2365 2366 message = _dbus_connection_pop_message_unlocked (connection); 2367 2368 _dbus_verbose ("Returning popped message %p\n", message); 2369 2370 CONNECTION_UNLOCK (connection); 2371 2372 return message; 2373} 2374 2375/** 2376 * Acquire the dispatcher. This must be done before dispatching 2377 * messages in order to guarantee the right order of 2378 * message delivery. May sleep and drop the connection mutex 2379 * while waiting for the dispatcher. 2380 * 2381 * @param connection the connection. 2382 */ 2383static void 2384_dbus_connection_acquire_dispatch (DBusConnection *connection) 2385{ 2386 if (connection->dispatch_acquired) 2387 dbus_condvar_wait (connection->dispatch_cond, connection->mutex); 2388 _dbus_assert (!connection->dispatch_acquired); 2389 2390 connection->dispatch_acquired = TRUE; 2391} 2392 2393/** 2394 * Release the dispatcher when you're done with it. Only call 2395 * after you've acquired the dispatcher. Wakes up at most one 2396 * thread currently waiting to acquire the dispatcher. 2397 * 2398 * @param connection the connection. 2399 */ 2400static void 2401_dbus_connection_release_dispatch (DBusConnection *connection) 2402{ 2403 _dbus_assert (connection->dispatch_acquired); 2404 2405 connection->dispatch_acquired = FALSE; 2406 dbus_condvar_wake_one (connection->dispatch_cond); 2407} 2408 2409static void 2410_dbus_connection_failed_pop (DBusConnection *connection, 2411 DBusList *message_link) 2412{ 2413 _dbus_list_prepend_link (&connection->incoming_messages, 2414 message_link); 2415 connection->n_incoming += 1; 2416} 2417 2418static DBusDispatchStatus 2419_dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection) 2420{ 2421 if (connection->n_incoming > 0) 2422 return DBUS_DISPATCH_DATA_REMAINS; 2423 else if (!_dbus_transport_queue_messages (connection->transport)) 2424 return DBUS_DISPATCH_NEED_MEMORY; 2425 else 2426 { 2427 DBusDispatchStatus status; 2428 dbus_bool_t is_connected; 2429 2430 status = _dbus_transport_get_dispatch_status (connection->transport); 2431 is_connected = _dbus_transport_get_is_connected (connection->transport); 2432 2433 _dbus_verbose ("dispatch status = %s is_connected = %d\n", 2434 DISPATCH_STATUS_NAME (status), is_connected); 2435 2436 if (!is_connected) 2437 { 2438 if (status == DBUS_DISPATCH_COMPLETE && 2439 connection->disconnect_message_link) 2440 { 2441 _dbus_verbose ("Sending disconnect message from %s\n", 2442 _DBUS_FUNCTION_NAME); 2443 2444 /* We haven't sent the disconnect message already, 2445 * and all real messages have been queued up. 2446 */ 2447 _dbus_connection_queue_synthesized_message_link (connection, 2448 connection->disconnect_message_link); 2449 connection->disconnect_message_link = NULL; 2450 } 2451 2452 /* Dump the outgoing queue, we aren't going to be able to 2453 * send it now, and we'd like accessors like 2454 * dbus_connection_get_outgoing_size() to be accurate. 2455 */ 2456 if (connection->n_outgoing > 0) 2457 { 2458 DBusList *link; 2459 2460 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n", 2461 connection->n_outgoing); 2462 2463 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages))) 2464 { 2465 _dbus_connection_message_sent (connection, link->data); 2466 } 2467 } 2468 } 2469 2470 if (status != DBUS_DISPATCH_COMPLETE) 2471 return status; 2472 else if (connection->n_incoming > 0) 2473 return DBUS_DISPATCH_DATA_REMAINS; 2474 else 2475 return DBUS_DISPATCH_COMPLETE; 2476 } 2477} 2478 2479static void 2480_dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection, 2481 DBusDispatchStatus new_status) 2482{ 2483 dbus_bool_t changed; 2484 DBusDispatchStatusFunction function; 2485 void *data; 2486 2487 /* We have the lock */ 2488 2489 _dbus_connection_ref_unlocked (connection); 2490 2491 changed = new_status != connection->last_dispatch_status; 2492 2493 connection->last_dispatch_status = new_status; 2494 2495 function = connection->dispatch_status_function; 2496 data = connection->dispatch_status_data; 2497 2498 /* We drop the lock */ 2499 CONNECTION_UNLOCK (connection); 2500 2501 if (changed && function) 2502 { 2503 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n", 2504 connection, new_status, 2505 DISPATCH_STATUS_NAME (new_status)); 2506 (* function) (connection, new_status, data); 2507 } 2508 2509 dbus_connection_unref (connection); 2510} 2511 2512/** 2513 * Gets the current state (what we would currently return 2514 * from dbus_connection_dispatch()) but doesn't actually 2515 * dispatch any messages. 2516 * 2517 * @param connection the connection. 2518 * @returns current dispatch status 2519 */ 2520DBusDispatchStatus 2521dbus_connection_get_dispatch_status (DBusConnection *connection) 2522{ 2523 DBusDispatchStatus status; 2524 2525 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE); 2526 2527 CONNECTION_LOCK (connection); 2528 2529 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2530 2531 CONNECTION_UNLOCK (connection); 2532 2533 return status; 2534} 2535 2536/** 2537 * Processes data buffered while handling watches, queueing zero or 2538 * more incoming messages. Then pops the first-received message from 2539 * the current incoming message queue, runs any handlers for it, and 2540 * unrefs the message. Returns a status indicating whether messages/data 2541 * remain, more memory is needed, or all data has been processed. 2542 * 2543 * Even if the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, 2544 * does not necessarily dispatch a message, as the data may 2545 * be part of authentication or the like. 2546 * 2547 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY 2548 * 2549 * @todo right now a message filter gets run on replies to a pending 2550 * call in here, but not in the case where we block without entering 2551 * the main loop. Simple solution might be to just have the pending 2552 * call stuff run before the filters. 2553 * 2554 * @todo FIXME what if we call out to application code to handle a 2555 * message, holding the dispatch lock, and the application code runs 2556 * the main loop and dispatches again? Probably deadlocks at the 2557 * moment. Maybe we want a dispatch status of DBUS_DISPATCH_IN_PROGRESS, 2558 * and then the GSource etc. could handle the situation? 2559 * 2560 * @param connection the connection 2561 * @returns dispatch status 2562 */ 2563DBusDispatchStatus 2564dbus_connection_dispatch (DBusConnection *connection) 2565{ 2566 DBusMessage *message; 2567 DBusList *link, *filter_list_copy, *message_link; 2568 DBusHandlerResult result; 2569 DBusPendingCall *pending; 2570 dbus_int32_t reply_serial; 2571 DBusDispatchStatus status; 2572 2573 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE); 2574 2575 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME); 2576 2577 CONNECTION_LOCK (connection); 2578 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2579 if (status != DBUS_DISPATCH_DATA_REMAINS) 2580 { 2581 /* unlocks and calls out to user code */ 2582 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2583 return status; 2584 } 2585 2586 /* We need to ref the connection since the callback could potentially 2587 * drop the last ref to it 2588 */ 2589 _dbus_connection_ref_unlocked (connection); 2590 2591 _dbus_connection_acquire_dispatch (connection); 2592 2593 /* This call may drop the lock during the execution (if waiting for 2594 * borrowed messages to be returned) but the order of message 2595 * dispatch if several threads call dispatch() is still 2596 * protected by the lock, since only one will get the lock, and that 2597 * one will finish the message dispatching 2598 */ 2599 message_link = _dbus_connection_pop_message_link_unlocked (connection); 2600 if (message_link == NULL) 2601 { 2602 /* another thread dispatched our stuff */ 2603 2604 _dbus_verbose ("another thread dispatched message\n"); 2605 2606 _dbus_connection_release_dispatch (connection); 2607 2608 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2609 2610 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2611 2612 dbus_connection_unref (connection); 2613 2614 return status; 2615 } 2616 2617 message = message_link->data; 2618 2619 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n", 2620 message, 2621 dbus_message_get_type (message), 2622 dbus_message_get_interface (message) ? 2623 dbus_message_get_interface (message) : 2624 "no interface", 2625 dbus_message_get_member (message) ? 2626 dbus_message_get_member (message) : 2627 "no member", 2628 dbus_message_get_signature (message)); 2629 2630 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 2631 2632 reply_serial = dbus_message_get_reply_serial (message); 2633 pending = _dbus_hash_table_lookup_int (connection->pending_replies, 2634 reply_serial); 2635 2636 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy)) 2637 { 2638 _dbus_connection_release_dispatch (connection); 2639 2640 _dbus_connection_failed_pop (connection, message_link); 2641 2642 /* unlocks and calls user code */ 2643 _dbus_connection_update_dispatch_status_and_unlock (connection, 2644 DBUS_DISPATCH_NEED_MEMORY); 2645 2646 dbus_connection_unref (connection); 2647 2648 return DBUS_DISPATCH_NEED_MEMORY; 2649 } 2650 2651 _dbus_list_foreach (&filter_list_copy, 2652 (DBusForeachFunction)_dbus_message_filter_ref, 2653 NULL); 2654 2655 /* We're still protected from dispatch() reentrancy here 2656 * since we acquired the dispatcher 2657 */ 2658 CONNECTION_UNLOCK (connection); 2659 2660 link = _dbus_list_get_first_link (&filter_list_copy); 2661 while (link != NULL) 2662 { 2663 DBusMessageFilter *filter = link->data; 2664 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link); 2665 2666 _dbus_verbose (" running filter on message %p\n", message); 2667 result = (* filter->function) (connection, message, filter->user_data); 2668 2669 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED) 2670 break; 2671 2672 link = next; 2673 } 2674 2675 _dbus_list_foreach (&filter_list_copy, 2676 (DBusForeachFunction)_dbus_message_filter_unref, 2677 NULL); 2678 _dbus_list_clear (&filter_list_copy); 2679 2680 CONNECTION_LOCK (connection); 2681 2682 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY) 2683 { 2684 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME); 2685 goto out; 2686 } 2687 2688 /* Did a reply we were waiting on get filtered? */ 2689 if (pending && result == DBUS_HANDLER_RESULT_HANDLED) 2690 { 2691 /* Queue the timeout immediately! */ 2692 if (pending->timeout_link) 2693 { 2694 _dbus_connection_queue_synthesized_message_link (connection, 2695 pending->timeout_link); 2696 pending->timeout_link = NULL; 2697 } 2698 else 2699 { 2700 /* We already queued the timeout? Then it was filtered! */ 2701 _dbus_warn ("The timeout error with reply serial %d was filtered, so the DBusPendingCall will never stop pending.\n", reply_serial); 2702 } 2703 } 2704 2705 if (result == DBUS_HANDLER_RESULT_HANDLED) 2706 { 2707 _dbus_verbose ("filter handled message in dispatch\n"); 2708 goto out; 2709 } 2710 2711 if (pending) 2712 { 2713 _dbus_pending_call_complete_and_unlock (pending, message); 2714 2715 pending = NULL; 2716 2717 CONNECTION_LOCK (connection); 2718 _dbus_verbose ("pending call completed in dispatch\n"); 2719 goto out; 2720 } 2721 2722 /* We're still protected from dispatch() reentrancy here 2723 * since we acquired the dispatcher 2724 */ 2725 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n", 2726 message, 2727 dbus_message_get_type (message), 2728 dbus_message_get_interface (message) ? 2729 dbus_message_get_interface (message) : 2730 "no interface", 2731 dbus_message_get_member (message) ? 2732 dbus_message_get_member (message) : 2733 "no member", 2734 dbus_message_get_signature (message)); 2735 2736 result = _dbus_object_tree_dispatch_and_unlock (connection->objects, 2737 message); 2738 2739 CONNECTION_LOCK (connection); 2740 2741 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED) 2742 { 2743 _dbus_verbose ("object tree handled message in dispatch\n"); 2744 goto out; 2745 } 2746 2747 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL) 2748 { 2749 DBusMessage *reply; 2750 DBusString str; 2751 DBusPreallocatedSend *preallocated; 2752 2753 _dbus_verbose (" sending error %s\n", 2754 DBUS_ERROR_UNKNOWN_METHOD); 2755 2756 if (!_dbus_string_init (&str)) 2757 { 2758 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 2759 _dbus_verbose ("no memory for error string in dispatch\n"); 2760 goto out; 2761 } 2762 2763 if (!_dbus_string_append_printf (&str, 2764 "Method \"%s\" on interface \"%s\" doesn't exist\n", 2765 dbus_message_get_member (message), 2766 dbus_message_get_interface (message))) 2767 { 2768 _dbus_string_free (&str); 2769 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 2770 _dbus_verbose ("no memory for error string in dispatch\n"); 2771 goto out; 2772 } 2773 2774 reply = dbus_message_new_error (message, 2775 DBUS_ERROR_UNKNOWN_METHOD, 2776 _dbus_string_get_const_data (&str)); 2777 _dbus_string_free (&str); 2778 2779 if (reply == NULL) 2780 { 2781 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 2782 _dbus_verbose ("no memory for error reply in dispatch\n"); 2783 goto out; 2784 } 2785 2786 preallocated = _dbus_connection_preallocate_send_unlocked (connection); 2787 2788 if (preallocated == NULL) 2789 { 2790 dbus_message_unref (reply); 2791 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 2792 _dbus_verbose ("no memory for error send in dispatch\n"); 2793 goto out; 2794 } 2795 2796 _dbus_connection_send_preallocated_unlocked (connection, preallocated, 2797 reply, NULL); 2798 2799 dbus_message_unref (reply); 2800 2801 result = DBUS_HANDLER_RESULT_HANDLED; 2802 } 2803 2804 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message, 2805 dbus_message_get_type (message), 2806 dbus_message_get_interface (message) ? 2807 dbus_message_get_interface (message) : 2808 "no interface", 2809 dbus_message_get_member (message) ? 2810 dbus_message_get_member (message) : 2811 "no member", 2812 dbus_message_get_signature (message), 2813 connection); 2814 2815 out: 2816 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY) 2817 { 2818 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME); 2819 2820 /* Put message back, and we'll start over. 2821 * Yes this means handlers must be idempotent if they 2822 * don't return HANDLED; c'est la vie. 2823 */ 2824 _dbus_connection_putback_message_link_unlocked (connection, 2825 message_link); 2826 } 2827 else 2828 { 2829 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME); 2830 2831 if (connection->exit_on_disconnect && 2832 dbus_message_is_signal (message, 2833 DBUS_INTERFACE_ORG_FREEDESKTOP_LOCAL, 2834 "Disconnected")) 2835 { 2836 _dbus_verbose ("Exiting on Disconnected signal\n"); 2837 CONNECTION_UNLOCK (connection); 2838 _dbus_exit (1); 2839 _dbus_assert_not_reached ("Call to exit() returned"); 2840 } 2841 2842 _dbus_list_free_link (message_link); 2843 dbus_message_unref (message); /* don't want the message to count in max message limits 2844 * in computing dispatch status below 2845 */ 2846 } 2847 2848 _dbus_connection_release_dispatch (connection); 2849 2850 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2851 2852 /* unlocks and calls user code */ 2853 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2854 2855 dbus_connection_unref (connection); 2856 2857 return status; 2858} 2859 2860/** 2861 * Sets the watch functions for the connection. These functions are 2862 * responsible for making the application's main loop aware of file 2863 * descriptors that need to be monitored for events, using select() or 2864 * poll(). When using Qt, typically the DBusAddWatchFunction would 2865 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction 2866 * could call g_io_add_watch(), or could be used as part of a more 2867 * elaborate GSource. Note that when a watch is added, it may 2868 * not be enabled. 2869 * 2870 * The DBusWatchToggledFunction notifies the application that the 2871 * watch has been enabled or disabled. Call dbus_watch_get_enabled() 2872 * to check this. A disabled watch should have no effect, and enabled 2873 * watch should be added to the main loop. This feature is used 2874 * instead of simply adding/removing the watch because 2875 * enabling/disabling can be done without memory allocation. The 2876 * toggled function may be NULL if a main loop re-queries 2877 * dbus_watch_get_enabled() every time anyway. 2878 * 2879 * The DBusWatch can be queried for the file descriptor to watch using 2880 * dbus_watch_get_fd(), and for the events to watch for using 2881 * dbus_watch_get_flags(). The flags returned by 2882 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and 2883 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; 2884 * all watches implicitly include a watch for hangups, errors, and 2885 * other exceptional conditions. 2886 * 2887 * Once a file descriptor becomes readable or writable, or an exception 2888 * occurs, dbus_watch_handle() should be called to 2889 * notify the connection of the file descriptor's condition. 2890 * 2891 * dbus_watch_handle() cannot be called during the 2892 * DBusAddWatchFunction, as the connection will not be ready to handle 2893 * that watch yet. 2894 * 2895 * It is not allowed to reference a DBusWatch after it has been passed 2896 * to remove_function. 2897 * 2898 * If #FALSE is returned due to lack of memory, the failure may be due 2899 * to a #FALSE return from the new add_function. If so, the 2900 * add_function may have been called successfully one or more times, 2901 * but the remove_function will also have been called to remove any 2902 * successful adds. i.e. if #FALSE is returned the net result 2903 * should be that dbus_connection_set_watch_functions() has no effect, 2904 * but the add_function and remove_function may have been called. 2905 * 2906 * @todo We need to drop the lock when we call the 2907 * add/remove/toggled functions which can be a side effect 2908 * of setting the watch functions. 2909 * 2910 * @param connection the connection. 2911 * @param add_function function to begin monitoring a new descriptor. 2912 * @param remove_function function to stop monitoring a descriptor. 2913 * @param toggled_function function to notify of enable/disable 2914 * @param data data to pass to add_function and remove_function. 2915 * @param free_data_function function to be called to free the data. 2916 * @returns #FALSE on failure (no memory) 2917 */ 2918dbus_bool_t 2919dbus_connection_set_watch_functions (DBusConnection *connection, 2920 DBusAddWatchFunction add_function, 2921 DBusRemoveWatchFunction remove_function, 2922 DBusWatchToggledFunction toggled_function, 2923 void *data, 2924 DBusFreeFunction free_data_function) 2925{ 2926 dbus_bool_t retval; 2927 2928 _dbus_return_val_if_fail (connection != NULL, FALSE); 2929 2930 CONNECTION_LOCK (connection); 2931 /* ref connection for slightly better reentrancy */ 2932 _dbus_connection_ref_unlocked (connection); 2933 2934 /* FIXME this can call back into user code, and we need to drop the 2935 * connection lock when it does. 2936 */ 2937 retval = _dbus_watch_list_set_functions (connection->watches, 2938 add_function, remove_function, 2939 toggled_function, 2940 data, free_data_function); 2941 2942 CONNECTION_UNLOCK (connection); 2943 /* drop our paranoid refcount */ 2944 dbus_connection_unref (connection); 2945 2946 return retval; 2947} 2948 2949/** 2950 * Sets the timeout functions for the connection. These functions are 2951 * responsible for making the application's main loop aware of timeouts. 2952 * When using Qt, typically the DBusAddTimeoutFunction would create a 2953 * QTimer. When using GLib, the DBusAddTimeoutFunction would call 2954 * g_timeout_add. 2955 * 2956 * The DBusTimeoutToggledFunction notifies the application that the 2957 * timeout has been enabled or disabled. Call 2958 * dbus_timeout_get_enabled() to check this. A disabled timeout should 2959 * have no effect, and enabled timeout should be added to the main 2960 * loop. This feature is used instead of simply adding/removing the 2961 * timeout because enabling/disabling can be done without memory 2962 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used 2963 * to enable and disable. The toggled function may be NULL if a main 2964 * loop re-queries dbus_timeout_get_enabled() every time anyway. 2965 * Whenever a timeout is toggled, its interval may change. 2966 * 2967 * The DBusTimeout can be queried for the timer interval using 2968 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called 2969 * repeatedly, each time the interval elapses, starting after it has 2970 * elapsed once. The timeout stops firing when it is removed with the 2971 * given remove_function. The timer interval may change whenever the 2972 * timeout is added, removed, or toggled. 2973 * 2974 * @param connection the connection. 2975 * @param add_function function to add a timeout. 2976 * @param remove_function function to remove a timeout. 2977 * @param toggled_function function to notify of enable/disable 2978 * @param data data to pass to add_function and remove_function. 2979 * @param free_data_function function to be called to free the data. 2980 * @returns #FALSE on failure (no memory) 2981 */ 2982dbus_bool_t 2983dbus_connection_set_timeout_functions (DBusConnection *connection, 2984 DBusAddTimeoutFunction add_function, 2985 DBusRemoveTimeoutFunction remove_function, 2986 DBusTimeoutToggledFunction toggled_function, 2987 void *data, 2988 DBusFreeFunction free_data_function) 2989{ 2990 dbus_bool_t retval; 2991 2992 _dbus_return_val_if_fail (connection != NULL, FALSE); 2993 2994 CONNECTION_LOCK (connection); 2995 /* ref connection for slightly better reentrancy */ 2996 _dbus_connection_ref_unlocked (connection); 2997 2998 retval = _dbus_timeout_list_set_functions (connection->timeouts, 2999 add_function, remove_function, 3000 toggled_function, 3001 data, free_data_function); 3002 3003 CONNECTION_UNLOCK (connection); 3004 /* drop our paranoid refcount */ 3005 dbus_connection_unref (connection); 3006 3007 return retval; 3008} 3009 3010/** 3011 * Sets the mainloop wakeup function for the connection. Thi function is 3012 * responsible for waking up the main loop (if its sleeping) when some some 3013 * change has happened to the connection that the mainloop needs to reconsiders 3014 * (e.g. a message has been queued for writing). 3015 * When using Qt, this typically results in a call to QEventLoop::wakeUp(). 3016 * When using GLib, it would call g_main_context_wakeup(). 3017 * 3018 * 3019 * @param connection the connection. 3020 * @param wakeup_main_function function to wake up the mainloop 3021 * @param data data to pass wakeup_main_function 3022 * @param free_data_function function to be called to free the data. 3023 */ 3024void 3025dbus_connection_set_wakeup_main_function (DBusConnection *connection, 3026 DBusWakeupMainFunction wakeup_main_function, 3027 void *data, 3028 DBusFreeFunction free_data_function) 3029{ 3030 void *old_data; 3031 DBusFreeFunction old_free_data; 3032 3033 _dbus_return_if_fail (connection != NULL); 3034 3035 CONNECTION_LOCK (connection); 3036 old_data = connection->wakeup_main_data; 3037 old_free_data = connection->free_wakeup_main_data; 3038 3039 connection->wakeup_main_function = wakeup_main_function; 3040 connection->wakeup_main_data = data; 3041 connection->free_wakeup_main_data = free_data_function; 3042 3043 CONNECTION_UNLOCK (connection); 3044 3045 /* Callback outside the lock */ 3046 if (old_free_data) 3047 (*old_free_data) (old_data); 3048} 3049 3050/** 3051 * Set a function to be invoked when the dispatch status changes. 3052 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then 3053 * dbus_connection_dispatch() needs to be called to process incoming 3054 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED 3055 * from inside the DBusDispatchStatusFunction. Indeed, almost 3056 * any reentrancy in this function is a bad idea. Instead, 3057 * the DBusDispatchStatusFunction should simply save an indication 3058 * that messages should be dispatched later, when the main loop 3059 * is re-entered. 3060 * 3061 * @param connection the connection 3062 * @param function function to call on dispatch status changes 3063 * @param data data for function 3064 * @param free_data_function free the function data 3065 */ 3066void 3067dbus_connection_set_dispatch_status_function (DBusConnection *connection, 3068 DBusDispatchStatusFunction function, 3069 void *data, 3070 DBusFreeFunction free_data_function) 3071{ 3072 void *old_data; 3073 DBusFreeFunction old_free_data; 3074 3075 _dbus_return_if_fail (connection != NULL); 3076 3077 CONNECTION_LOCK (connection); 3078 old_data = connection->dispatch_status_data; 3079 old_free_data = connection->free_dispatch_status_data; 3080 3081 connection->dispatch_status_function = function; 3082 connection->dispatch_status_data = data; 3083 connection->free_dispatch_status_data = free_data_function; 3084 3085 CONNECTION_UNLOCK (connection); 3086 3087 /* Callback outside the lock */ 3088 if (old_free_data) 3089 (*old_free_data) (old_data); 3090} 3091 3092/** 3093 * Get the UNIX file descriptor of the connection, if any. This can 3094 * be used for SELinux access control checks with getpeercon() for 3095 * example. DO NOT read or write to the file descriptor, or try to 3096 * select() on it; use DBusWatch for main loop integration. Not all 3097 * connections will have a file descriptor. So for adding descriptors 3098 * to the main loop, use dbus_watch_get_fd() and so forth. 3099 * 3100 * @param connection the connection 3101 * @param fd return location for the file descriptor. 3102 * @returns #TRUE if fd is successfully obtained. 3103 */ 3104dbus_bool_t 3105dbus_connection_get_unix_fd (DBusConnection *connection, 3106 int *fd) 3107{ 3108 dbus_bool_t retval; 3109 3110 _dbus_return_val_if_fail (connection != NULL, FALSE); 3111 _dbus_return_val_if_fail (connection->transport != NULL, FALSE); 3112 3113 CONNECTION_LOCK (connection); 3114 3115 retval = _dbus_transport_get_unix_fd (connection->transport, 3116 fd); 3117 3118 CONNECTION_UNLOCK (connection); 3119 3120 return retval; 3121} 3122 3123/** 3124 * Gets the UNIX user ID of the connection if any. 3125 * Returns #TRUE if the uid is filled in. 3126 * Always returns #FALSE on non-UNIX platforms. 3127 * Always returns #FALSE prior to authenticating the 3128 * connection. 3129 * 3130 * @param connection the connection 3131 * @param uid return location for the user ID 3132 * @returns #TRUE if uid is filled in with a valid user ID 3133 */ 3134dbus_bool_t 3135dbus_connection_get_unix_user (DBusConnection *connection, 3136 unsigned long *uid) 3137{ 3138 dbus_bool_t result; 3139 3140 _dbus_return_val_if_fail (connection != NULL, FALSE); 3141 _dbus_return_val_if_fail (uid != NULL, FALSE); 3142 3143 CONNECTION_LOCK (connection); 3144 3145 if (!_dbus_transport_get_is_authenticated (connection->transport)) 3146 result = FALSE; 3147 else 3148 result = _dbus_transport_get_unix_user (connection->transport, 3149 uid); 3150 CONNECTION_UNLOCK (connection); 3151 3152 return result; 3153} 3154 3155/** 3156 * Gets the process ID of the connection if any. 3157 * Returns #TRUE if the uid is filled in. 3158 * Always returns #FALSE prior to authenticating the 3159 * connection. 3160 * 3161 * @param connection the connection 3162 * @param pid return location for the process ID 3163 * @returns #TRUE if uid is filled in with a valid process ID 3164 */ 3165dbus_bool_t 3166dbus_connection_get_unix_process_id (DBusConnection *connection, 3167 unsigned long *pid) 3168{ 3169 dbus_bool_t result; 3170 3171 _dbus_return_val_if_fail (connection != NULL, FALSE); 3172 _dbus_return_val_if_fail (pid != NULL, FALSE); 3173 3174 CONNECTION_LOCK (connection); 3175 3176 if (!_dbus_transport_get_is_authenticated (connection->transport)) 3177 result = FALSE; 3178 else 3179 result = _dbus_transport_get_unix_process_id (connection->transport, 3180 pid); 3181 CONNECTION_UNLOCK (connection); 3182 3183 return result; 3184} 3185 3186/** 3187 * Sets a predicate function used to determine whether a given user ID 3188 * is allowed to connect. When an incoming connection has 3189 * authenticated with a particular user ID, this function is called; 3190 * if it returns #TRUE, the connection is allowed to proceed, 3191 * otherwise the connection is disconnected. 3192 * 3193 * If the function is set to #NULL (as it is by default), then 3194 * only the same UID as the server process will be allowed to 3195 * connect. 3196 * 3197 * @param connection the connection 3198 * @param function the predicate 3199 * @param data data to pass to the predicate 3200 * @param free_data_function function to free the data 3201 */ 3202void 3203dbus_connection_set_unix_user_function (DBusConnection *connection, 3204 DBusAllowUnixUserFunction function, 3205 void *data, 3206 DBusFreeFunction free_data_function) 3207{ 3208 void *old_data = NULL; 3209 DBusFreeFunction old_free_function = NULL; 3210 3211 _dbus_return_if_fail (connection != NULL); 3212 3213 CONNECTION_LOCK (connection); 3214 _dbus_transport_set_unix_user_function (connection->transport, 3215 function, data, free_data_function, 3216 &old_data, &old_free_function); 3217 CONNECTION_UNLOCK (connection); 3218 3219 if (old_free_function != NULL) 3220 (* old_free_function) (old_data); 3221} 3222 3223/** 3224 * Adds a message filter. Filters are handlers that are run on all 3225 * incoming messages, prior to the objects registered with 3226 * dbus_connection_register_object_path(). Filters are run in the 3227 * order that they were added. The same handler can be added as a 3228 * filter more than once, in which case it will be run more than once. 3229 * Filters added during a filter callback won't be run on the message 3230 * being processed. 3231 * 3232 * @todo we don't run filters on messages while blocking without 3233 * entering the main loop, since filters are run as part of 3234 * dbus_connection_dispatch(). This is probably a feature, as filters 3235 * could create arbitrary reentrancy. But kind of sucks if you're 3236 * trying to filter METHOD_RETURN for some reason. 3237 * 3238 * @param connection the connection 3239 * @param function function to handle messages 3240 * @param user_data user data to pass to the function 3241 * @param free_data_function function to use for freeing user data 3242 * @returns #TRUE on success, #FALSE if not enough memory. 3243 */ 3244dbus_bool_t 3245dbus_connection_add_filter (DBusConnection *connection, 3246 DBusHandleMessageFunction function, 3247 void *user_data, 3248 DBusFreeFunction free_data_function) 3249{ 3250 DBusMessageFilter *filter; 3251 3252 _dbus_return_val_if_fail (connection != NULL, FALSE); 3253 _dbus_return_val_if_fail (function != NULL, FALSE); 3254 3255 filter = dbus_new0 (DBusMessageFilter, 1); 3256 if (filter == NULL) 3257 return FALSE; 3258 3259 filter->refcount.value = 1; 3260 3261 CONNECTION_LOCK (connection); 3262 3263 if (!_dbus_list_append (&connection->filter_list, 3264 filter)) 3265 { 3266 _dbus_message_filter_unref (filter); 3267 CONNECTION_UNLOCK (connection); 3268 return FALSE; 3269 } 3270 3271 /* Fill in filter after all memory allocated, 3272 * so we don't run the free_user_data_function 3273 * if the add_filter() fails 3274 */ 3275 3276 filter->function = function; 3277 filter->user_data = user_data; 3278 filter->free_user_data_function = free_data_function; 3279 3280 CONNECTION_UNLOCK (connection); 3281 return TRUE; 3282} 3283 3284/** 3285 * Removes a previously-added message filter. It is a programming 3286 * error to call this function for a handler that has not been added 3287 * as a filter. If the given handler was added more than once, only 3288 * one instance of it will be removed (the most recently-added 3289 * instance). 3290 * 3291 * @param connection the connection 3292 * @param function the handler to remove 3293 * @param user_data user data for the handler to remove 3294 * 3295 */ 3296void 3297dbus_connection_remove_filter (DBusConnection *connection, 3298 DBusHandleMessageFunction function, 3299 void *user_data) 3300{ 3301 DBusList *link; 3302 DBusMessageFilter *filter; 3303 3304 _dbus_return_if_fail (connection != NULL); 3305 _dbus_return_if_fail (function != NULL); 3306 3307 CONNECTION_LOCK (connection); 3308 3309 filter = NULL; 3310 3311 link = _dbus_list_get_last_link (&connection->filter_list); 3312 while (link != NULL) 3313 { 3314 filter = link->data; 3315 3316 if (filter->function == function && 3317 filter->user_data == user_data) 3318 { 3319 _dbus_list_remove_link (&connection->filter_list, link); 3320 filter->function = NULL; 3321 3322 break; 3323 } 3324 3325 link = _dbus_list_get_prev_link (&connection->filter_list, link); 3326 } 3327 3328 CONNECTION_UNLOCK (connection); 3329 3330#ifndef DBUS_DISABLE_CHECKS 3331 if (filter == NULL) 3332 { 3333 _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n", 3334 function, user_data); 3335 return; 3336 } 3337#endif 3338 3339 /* Call application code */ 3340 if (filter->free_user_data_function) 3341 (* filter->free_user_data_function) (filter->user_data); 3342 3343 filter->free_user_data_function = NULL; 3344 filter->user_data = NULL; 3345 3346 _dbus_message_filter_unref (filter); 3347} 3348 3349/** 3350 * Registers a handler for a given path in the object hierarchy. 3351 * The given vtable handles messages sent to exactly the given path. 3352 * 3353 * 3354 * @param connection the connection 3355 * @param path a '/' delimited string of path elements 3356 * @param vtable the virtual table 3357 * @param user_data data to pass to functions in the vtable 3358 * @returns #FALSE if not enough memory 3359 */ 3360dbus_bool_t 3361dbus_connection_register_object_path (DBusConnection *connection, 3362 const char *path, 3363 const DBusObjectPathVTable *vtable, 3364 void *user_data) 3365{ 3366 char **decomposed_path; 3367 dbus_bool_t retval; 3368 3369 _dbus_return_val_if_fail (connection != NULL, FALSE); 3370 _dbus_return_val_if_fail (path != NULL, FALSE); 3371 _dbus_return_val_if_fail (path[0] == '/', FALSE); 3372 _dbus_return_val_if_fail (vtable != NULL, FALSE); 3373 3374 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 3375 return FALSE; 3376 3377 CONNECTION_LOCK (connection); 3378 3379 retval = _dbus_object_tree_register (connection->objects, 3380 FALSE, 3381 (const char **) decomposed_path, vtable, 3382 user_data); 3383 3384 CONNECTION_UNLOCK (connection); 3385 3386 dbus_free_string_array (decomposed_path); 3387 3388 return retval; 3389} 3390 3391/** 3392 * Registers a fallback handler for a given subsection of the object 3393 * hierarchy. The given vtable handles messages at or below the given 3394 * path. You can use this to establish a default message handling 3395 * policy for a whole "subdirectory." 3396 * 3397 * @param connection the connection 3398 * @param path a '/' delimited string of path elements 3399 * @param vtable the virtual table 3400 * @param user_data data to pass to functions in the vtable 3401 * @returns #FALSE if not enough memory 3402 */ 3403dbus_bool_t 3404dbus_connection_register_fallback (DBusConnection *connection, 3405 const char *path, 3406 const DBusObjectPathVTable *vtable, 3407 void *user_data) 3408{ 3409 char **decomposed_path; 3410 dbus_bool_t retval; 3411 3412 _dbus_return_val_if_fail (connection != NULL, FALSE); 3413 _dbus_return_val_if_fail (path != NULL, FALSE); 3414 _dbus_return_val_if_fail (path[0] == '/', FALSE); 3415 _dbus_return_val_if_fail (vtable != NULL, FALSE); 3416 3417 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 3418 return FALSE; 3419 3420 CONNECTION_LOCK (connection); 3421 3422 retval = _dbus_object_tree_register (connection->objects, 3423 TRUE, 3424 (const char **) decomposed_path, vtable, 3425 user_data); 3426 3427 CONNECTION_UNLOCK (connection); 3428 3429 dbus_free_string_array (decomposed_path); 3430 3431 return retval; 3432} 3433 3434/** 3435 * Unregisters the handler registered with exactly the given path. 3436 * It's a bug to call this function for a path that isn't registered. 3437 * Can unregister both fallback paths and object paths. 3438 * 3439 * @param connection the connection 3440 * @param path a '/' delimited string of path elements 3441 * @returns #FALSE if not enough memory 3442 */ 3443dbus_bool_t 3444dbus_connection_unregister_object_path (DBusConnection *connection, 3445 const char *path) 3446{ 3447 char **decomposed_path; 3448 3449 _dbus_return_val_if_fail (connection != NULL, FALSE); 3450 _dbus_return_val_if_fail (path != NULL, FALSE); 3451 _dbus_return_val_if_fail (path[0] == '/', FALSE); 3452 3453 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 3454 return FALSE; 3455 3456 CONNECTION_LOCK (connection); 3457 3458 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path); 3459 3460 dbus_free_string_array (decomposed_path); 3461 3462 return TRUE; 3463} 3464 3465/** 3466 * Lists the registered fallback handlers and object path handlers at 3467 * the given parent_path. The returned array should be freed with 3468 * dbus_free_string_array(). 3469 * 3470 * @param connection the connection 3471 * @param parent_path the path to list the child handlers of 3472 * @param child_entries returns #NULL-terminated array of children 3473 * @returns #FALSE if no memory to allocate the child entries 3474 */ 3475dbus_bool_t 3476dbus_connection_list_registered (DBusConnection *connection, 3477 const char *parent_path, 3478 char ***child_entries) 3479{ 3480 char **decomposed_path; 3481 dbus_bool_t retval; 3482 _dbus_return_val_if_fail (connection != NULL, FALSE); 3483 _dbus_return_val_if_fail (parent_path != NULL, FALSE); 3484 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE); 3485 _dbus_return_val_if_fail (child_entries != NULL, FALSE); 3486 3487 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL)) 3488 return FALSE; 3489 3490 CONNECTION_LOCK (connection); 3491 3492 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects, 3493 (const char **) decomposed_path, 3494 child_entries); 3495 dbus_free_string_array (decomposed_path); 3496 3497 return retval; 3498} 3499 3500static DBusDataSlotAllocator slot_allocator; 3501_DBUS_DEFINE_GLOBAL_LOCK (connection_slots); 3502 3503/** 3504 * Allocates an integer ID to be used for storing application-specific 3505 * data on any DBusConnection. The allocated ID may then be used 3506 * with dbus_connection_set_data() and dbus_connection_get_data(). 3507 * The passed-in slot must be initialized to -1, and is filled in 3508 * with the slot ID. If the passed-in slot is not -1, it's assumed 3509 * to be already allocated, and its refcount is incremented. 3510 * 3511 * The allocated slot is global, i.e. all DBusConnection objects will 3512 * have a slot with the given integer ID reserved. 3513 * 3514 * @param slot_p address of a global variable storing the slot 3515 * @returns #FALSE on failure (no memory) 3516 */ 3517dbus_bool_t 3518dbus_connection_allocate_data_slot (dbus_int32_t *slot_p) 3519{ 3520 return _dbus_data_slot_allocator_alloc (&slot_allocator, 3521 _DBUS_LOCK_NAME (connection_slots), 3522 slot_p); 3523} 3524 3525/** 3526 * Deallocates a global ID for connection data slots. 3527 * dbus_connection_get_data() and dbus_connection_set_data() may no 3528 * longer be used with this slot. Existing data stored on existing 3529 * DBusConnection objects will be freed when the connection is 3530 * finalized, but may not be retrieved (and may only be replaced if 3531 * someone else reallocates the slot). When the refcount on the 3532 * passed-in slot reaches 0, it is set to -1. 3533 * 3534 * @param slot_p address storing the slot to deallocate 3535 */ 3536void 3537dbus_connection_free_data_slot (dbus_int32_t *slot_p) 3538{ 3539 _dbus_return_if_fail (*slot_p >= 0); 3540 3541 _dbus_data_slot_allocator_free (&slot_allocator, slot_p); 3542} 3543 3544/** 3545 * Stores a pointer on a DBusConnection, along 3546 * with an optional function to be used for freeing 3547 * the data when the data is set again, or when 3548 * the connection is finalized. The slot number 3549 * must have been allocated with dbus_connection_allocate_data_slot(). 3550 * 3551 * @param connection the connection 3552 * @param slot the slot number 3553 * @param data the data to store 3554 * @param free_data_func finalizer function for the data 3555 * @returns #TRUE if there was enough memory to store the data 3556 */ 3557dbus_bool_t 3558dbus_connection_set_data (DBusConnection *connection, 3559 dbus_int32_t slot, 3560 void *data, 3561 DBusFreeFunction free_data_func) 3562{ 3563 DBusFreeFunction old_free_func; 3564 void *old_data; 3565 dbus_bool_t retval; 3566 3567 _dbus_return_val_if_fail (connection != NULL, FALSE); 3568 _dbus_return_val_if_fail (slot >= 0, FALSE); 3569 3570 CONNECTION_LOCK (connection); 3571 3572 retval = _dbus_data_slot_list_set (&slot_allocator, 3573 &connection->slot_list, 3574 slot, data, free_data_func, 3575 &old_free_func, &old_data); 3576 3577 CONNECTION_UNLOCK (connection); 3578 3579 if (retval) 3580 { 3581 /* Do the actual free outside the connection lock */ 3582 if (old_free_func) 3583 (* old_free_func) (old_data); 3584 } 3585 3586 return retval; 3587} 3588 3589/** 3590 * Retrieves data previously set with dbus_connection_set_data(). 3591 * The slot must still be allocated (must not have been freed). 3592 * 3593 * @param connection the connection 3594 * @param slot the slot to get data from 3595 * @returns the data, or #NULL if not found 3596 */ 3597void* 3598dbus_connection_get_data (DBusConnection *connection, 3599 dbus_int32_t slot) 3600{ 3601 void *res; 3602 3603 _dbus_return_val_if_fail (connection != NULL, NULL); 3604 3605 CONNECTION_LOCK (connection); 3606 3607 res = _dbus_data_slot_list_get (&slot_allocator, 3608 &connection->slot_list, 3609 slot); 3610 3611 CONNECTION_UNLOCK (connection); 3612 3613 return res; 3614} 3615 3616/** 3617 * This function sets a global flag for whether dbus_connection_new() 3618 * will set SIGPIPE behavior to SIG_IGN. 3619 * 3620 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN 3621 */ 3622void 3623dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe) 3624{ 3625 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE; 3626} 3627 3628/** 3629 * Specifies the maximum size message this connection is allowed to 3630 * receive. Larger messages will result in disconnecting the 3631 * connection. 3632 * 3633 * @param connection a #DBusConnection 3634 * @param size maximum message size the connection can receive, in bytes 3635 */ 3636void 3637dbus_connection_set_max_message_size (DBusConnection *connection, 3638 long size) 3639{ 3640 _dbus_return_if_fail (connection != NULL); 3641 3642 CONNECTION_LOCK (connection); 3643 _dbus_transport_set_max_message_size (connection->transport, 3644 size); 3645 CONNECTION_UNLOCK (connection); 3646} 3647 3648/** 3649 * Gets the value set by dbus_connection_set_max_message_size(). 3650 * 3651 * @param connection the connection 3652 * @returns the max size of a single message 3653 */ 3654long 3655dbus_connection_get_max_message_size (DBusConnection *connection) 3656{ 3657 long res; 3658 3659 _dbus_return_val_if_fail (connection != NULL, 0); 3660 3661 CONNECTION_LOCK (connection); 3662 res = _dbus_transport_get_max_message_size (connection->transport); 3663 CONNECTION_UNLOCK (connection); 3664 return res; 3665} 3666 3667/** 3668 * Sets the maximum total number of bytes that can be used for all messages 3669 * received on this connection. Messages count toward the maximum until 3670 * they are finalized. When the maximum is reached, the connection will 3671 * not read more data until some messages are finalized. 3672 * 3673 * The semantics of the maximum are: if outstanding messages are 3674 * already above the maximum, additional messages will not be read. 3675 * The semantics are not: if the next message would cause us to exceed 3676 * the maximum, we don't read it. The reason is that we don't know the 3677 * size of a message until after we read it. 3678 * 3679 * Thus, the max live messages size can actually be exceeded 3680 * by up to the maximum size of a single message. 3681 * 3682 * Also, if we read say 1024 bytes off the wire in a single read(), 3683 * and that contains a half-dozen small messages, we may exceed the 3684 * size max by that amount. But this should be inconsequential. 3685 * 3686 * This does imply that we can't call read() with a buffer larger 3687 * than we're willing to exceed this limit by. 3688 * 3689 * @param connection the connection 3690 * @param size the maximum size in bytes of all outstanding messages 3691 */ 3692void 3693dbus_connection_set_max_received_size (DBusConnection *connection, 3694 long size) 3695{ 3696 _dbus_return_if_fail (connection != NULL); 3697 3698 CONNECTION_LOCK (connection); 3699 _dbus_transport_set_max_received_size (connection->transport, 3700 size); 3701 CONNECTION_UNLOCK (connection); 3702} 3703 3704/** 3705 * Gets the value set by dbus_connection_set_max_received_size(). 3706 * 3707 * @param connection the connection 3708 * @returns the max size of all live messages 3709 */ 3710long 3711dbus_connection_get_max_received_size (DBusConnection *connection) 3712{ 3713 long res; 3714 3715 _dbus_return_val_if_fail (connection != NULL, 0); 3716 3717 CONNECTION_LOCK (connection); 3718 res = _dbus_transport_get_max_received_size (connection->transport); 3719 CONNECTION_UNLOCK (connection); 3720 return res; 3721} 3722 3723/** 3724 * Gets the approximate size in bytes of all messages in the outgoing 3725 * message queue. The size is approximate in that you shouldn't use 3726 * it to decide how many bytes to read off the network or anything 3727 * of that nature, as optimizations may choose to tell small white lies 3728 * to avoid performance overhead. 3729 * 3730 * @param connection the connection 3731 * @returns the number of bytes that have been queued up but not sent 3732 */ 3733long 3734dbus_connection_get_outgoing_size (DBusConnection *connection) 3735{ 3736 long res; 3737 3738 _dbus_return_val_if_fail (connection != NULL, 0); 3739 3740 CONNECTION_LOCK (connection); 3741 res = _dbus_counter_get_value (connection->outgoing_counter); 3742 CONNECTION_UNLOCK (connection); 3743 return res; 3744} 3745 3746/** @} */ 3747