dbus-connection.c revision 65fcbd62876f435f08bbcb6ff4596545b8c87bbd
1/* -*- mode: C; c-file-style: "gnu" -*- */ 2/* dbus-connection.c DBusConnection object 3 * 4 * Copyright (C) 2002-2006 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-pending-call-internal.h" 33#include "dbus-list.h" 34#include "dbus-hash.h" 35#include "dbus-message-internal.h" 36#include "dbus-threads.h" 37#include "dbus-protocol.h" 38#include "dbus-dataslot.h" 39#include "dbus-string.h" 40#include "dbus-pending-call.h" 41#include "dbus-object-tree.h" 42#include "dbus-threads-internal.h" 43#include "dbus-bus.h" 44 45#ifdef DBUS_DISABLE_CHECKS 46#define TOOK_LOCK_CHECK(connection) 47#define RELEASING_LOCK_CHECK(connection) 48#define HAVE_LOCK_CHECK(connection) 49#else 50#define TOOK_LOCK_CHECK(connection) do { \ 51 _dbus_assert (!(connection)->have_connection_lock); \ 52 (connection)->have_connection_lock = TRUE; \ 53 } while (0) 54#define RELEASING_LOCK_CHECK(connection) do { \ 55 _dbus_assert ((connection)->have_connection_lock); \ 56 (connection)->have_connection_lock = FALSE; \ 57 } while (0) 58#define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock) 59/* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */ 60#endif 61 62#define TRACE_LOCKS 1 63 64#define CONNECTION_LOCK(connection) do { \ 65 if (TRACE_LOCKS) { _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); } \ 66 _dbus_mutex_lock ((connection)->mutex); \ 67 TOOK_LOCK_CHECK (connection); \ 68 } while (0) 69 70#define CONNECTION_UNLOCK(connection) do { \ 71 if (TRACE_LOCKS) { _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); } \ 72 RELEASING_LOCK_CHECK (connection); \ 73 _dbus_mutex_unlock ((connection)->mutex); \ 74 } while (0) 75 76#define DISPATCH_STATUS_NAME(s) \ 77 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \ 78 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \ 79 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \ 80 "???") 81 82/** 83 * @defgroup DBusConnection DBusConnection 84 * @ingroup DBus 85 * @brief Connection to another application 86 * 87 * A DBusConnection represents a connection to another 88 * application. Messages can be sent and received via this connection. 89 * The other application may be a message bus; for convenience, the 90 * function dbus_bus_get() is provided to automatically open a 91 * connection to the well-known message buses. 92 * 93 * In brief a DBusConnection is a message queue associated with some 94 * message transport mechanism such as a socket. The connection 95 * maintains a queue of incoming messages and a queue of outgoing 96 * messages. 97 * 98 * Several functions use the following terms: 99 * <ul> 100 * <li><b>read</b> means to fill the incoming message queue by reading from the socket</li> 101 * <li><b>write</b> means to drain the outgoing queue by writing to the socket</li> 102 * <li><b>dispatch</b> means to drain the incoming queue by invoking application-provided message handlers</li> 103 * </ul> 104 * 105 * The function dbus_connection_read_write_dispatch() for example does all 106 * three of these things, offering a simple alternative to a main loop. 107 * 108 * In an application with a main loop, the read/write/dispatch 109 * operations are usually separate. 110 * 111 * The connection provides #DBusWatch and #DBusTimeout objects to 112 * the main loop. These are used to know when reading, writing, or 113 * dispatching should be performed. 114 * 115 * Incoming messages are processed 116 * by calling dbus_connection_dispatch(). dbus_connection_dispatch() 117 * runs any handlers registered for the topmost message in the message 118 * queue, then discards the message, then returns. 119 * 120 * dbus_connection_get_dispatch_status() indicates whether 121 * messages are currently in the queue that need dispatching. 122 * dbus_connection_set_dispatch_status_function() allows 123 * you to set a function to be used to monitor the dispatch status. 124 * 125 * If you're using GLib or Qt add-on libraries for D-Bus, there are 126 * special convenience APIs in those libraries that hide 127 * all the details of dispatch and watch/timeout monitoring. 128 * For example, dbus_connection_setup_with_g_main(). 129 * 130 * If you aren't using these add-on libraries, but want to process 131 * messages asynchronously, you must manually call 132 * dbus_connection_set_dispatch_status_function(), 133 * dbus_connection_set_watch_functions(), 134 * dbus_connection_set_timeout_functions() providing appropriate 135 * functions to integrate the connection with your application's main 136 * loop. This can be tricky to get right; main loops are not simple. 137 * 138 * If you don't need to be asynchronous, you can ignore #DBusWatch, 139 * #DBusTimeout, and dbus_connection_dispatch(). Instead, 140 * dbus_connection_read_write_dispatch() can be used. 141 * 142 * Or, in <em>very</em> simple applications, 143 * dbus_connection_pop_message() may be all you need, allowing you to 144 * avoid setting up any handler functions (see 145 * dbus_connection_add_filter(), 146 * dbus_connection_register_object_path() for more on handlers). 147 * 148 * When you use dbus_connection_send() or one of its variants to send 149 * a message, the message is added to the outgoing queue. It's 150 * actually written to the network later; either in 151 * dbus_watch_handle() invoked by your main loop, or in 152 * dbus_connection_flush() which blocks until it can write out the 153 * entire outgoing queue. The GLib/Qt add-on libraries again 154 * handle the details here for you by setting up watch functions. 155 * 156 * When a connection is disconnected, you are guaranteed to get a 157 * signal "Disconnected" from the interface 158 * #DBUS_INTERFACE_LOCAL, path 159 * #DBUS_PATH_LOCAL. 160 * 161 * You may not drop the last reference to a #DBusConnection 162 * until that connection has been disconnected. 163 * 164 * You may dispatch the unprocessed incoming message queue even if the 165 * connection is disconnected. However, "Disconnected" will always be 166 * the last message in the queue (obviously no messages are received 167 * after disconnection). 168 * 169 * After calling dbus_threads_init(), #DBusConnection has thread 170 * locks and drops them when invoking user callbacks, so in general is 171 * transparently threadsafe. However, #DBusMessage does NOT have 172 * thread locks; you must not send the same message to multiple 173 * #DBusConnection if those connections will be used from different threads, 174 * for example. 175 * 176 * Also, if you dispatch or pop messages from multiple threads, it 177 * may work in the sense that it won't crash, but it's tough to imagine 178 * sane results; it will be completely unpredictable which messages 179 * go to which threads. 180 * 181 * It's recommended to dispatch from a single thread. 182 * 183 * The most useful function to call from multiple threads at once 184 * is dbus_connection_send_with_reply_and_block(). That is, 185 * multiple threads can make method calls at the same time. 186 * 187 * If you aren't using threads, you can use a main loop and 188 * dbus_pending_call_set_notify() to achieve a similar result. 189 */ 190 191/** 192 * @defgroup DBusConnectionInternals DBusConnection implementation details 193 * @ingroup DBusInternals 194 * @brief Implementation details of DBusConnection 195 * 196 * @{ 197 */ 198 199/** 200 * Internal struct representing a message filter function 201 */ 202typedef struct DBusMessageFilter DBusMessageFilter; 203 204/** 205 * Internal struct representing a message filter function 206 */ 207struct DBusMessageFilter 208{ 209 DBusAtomic refcount; /**< Reference count */ 210 DBusHandleMessageFunction function; /**< Function to call to filter */ 211 void *user_data; /**< User data for the function */ 212 DBusFreeFunction free_user_data_function; /**< Function to free the user data */ 213}; 214 215 216/** 217 * Internals of DBusPreallocatedSend 218 */ 219struct DBusPreallocatedSend 220{ 221 DBusConnection *connection; /**< Connection we'd send the message to */ 222 DBusList *queue_link; /**< Preallocated link in the queue */ 223 DBusList *counter_link; /**< Preallocated link in the resource counter */ 224}; 225 226static dbus_bool_t _dbus_modify_sigpipe = TRUE; 227 228/** 229 * Implementation details of DBusConnection. All fields are private. 230 */ 231struct DBusConnection 232{ 233 DBusAtomic refcount; /**< Reference count. */ 234 235 DBusMutex *mutex; /**< Lock on the entire DBusConnection */ 236 237 DBusMutex *dispatch_mutex; /**< Protects dispatch_acquired */ 238 DBusCondVar *dispatch_cond; /**< Notify when dispatch_acquired is available */ 239 DBusMutex *io_path_mutex; /**< Protects io_path_acquired */ 240 DBusCondVar *io_path_cond; /**< Notify when io_path_acquired is available */ 241 242 DBusList *outgoing_messages; /**< Queue of messages we need to send, send the end of the list first. */ 243 DBusList *incoming_messages; /**< Queue of messages we have received, end of the list received most recently. */ 244 245 DBusMessage *message_borrowed; /**< Filled in if the first incoming message has been borrowed; 246 * dispatch_acquired will be set by the borrower 247 */ 248 249 int n_outgoing; /**< Length of outgoing queue. */ 250 int n_incoming; /**< Length of incoming queue. */ 251 252 DBusCounter *outgoing_counter; /**< Counts size of outgoing messages. */ 253 254 DBusTransport *transport; /**< Object that sends/receives messages over network. */ 255 DBusWatchList *watches; /**< Stores active watches. */ 256 DBusTimeoutList *timeouts; /**< Stores active timeouts. */ 257 258 DBusList *filter_list; /**< List of filters. */ 259 260 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */ 261 262 DBusHashTable *pending_replies; /**< Hash of message serials to #DBusPendingCall. */ 263 264 dbus_uint32_t client_serial; /**< Client serial. Increments each time a message is sent */ 265 DBusList *disconnect_message_link; /**< Preallocated list node for queueing the disconnection message */ 266 267 DBusWakeupMainFunction wakeup_main_function; /**< Function to wake up the mainloop */ 268 void *wakeup_main_data; /**< Application data for wakeup_main_function */ 269 DBusFreeFunction free_wakeup_main_data; /**< free wakeup_main_data */ 270 271 DBusDispatchStatusFunction dispatch_status_function; /**< Function on dispatch status changes */ 272 void *dispatch_status_data; /**< Application data for dispatch_status_function */ 273 DBusFreeFunction free_dispatch_status_data; /**< free dispatch_status_data */ 274 275 DBusDispatchStatus last_dispatch_status; /**< The last dispatch status we reported to the application. */ 276 277 DBusList *link_cache; /**< A cache of linked list links to prevent contention 278 * for the global linked list mempool lock 279 */ 280 DBusObjectTree *objects; /**< Object path handlers registered with this connection */ 281 282 char *server_guid; /**< GUID of server if we are in shared_connections, #NULL if server GUID is unknown or connection is private */ 283 284 unsigned int shareable : 1; /**< #TRUE if libdbus owns a reference to the connection and can return it from dbus_connection_open() more than once */ 285 286 unsigned int dispatch_acquired : 1; /**< Someone has dispatch path (can drain incoming queue) */ 287 unsigned int io_path_acquired : 1; /**< Someone has transport io path (can use the transport to read/write messages) */ 288 289 unsigned int exit_on_disconnect : 1; /**< If #TRUE, exit after handling disconnect signal */ 290 291 unsigned int route_peer_messages : 1; /**< If #TRUE, if org.freedesktop.DBus.Peer messages have a bus name, don't handle them automatically */ 292 293 unsigned int disconnected_message_arrived : 1; /**< We popped or are dispatching the disconnected message. 294 * if the disconnect_message_link is NULL then we queued it, but 295 * this flag is whether it got to the head of the queue. 296 */ 297 unsigned int disconnected_message_processed : 1; /**< We did our default handling of the disconnected message, 298 * such as closing the connection. 299 */ 300 301#ifndef DBUS_DISABLE_CHECKS 302 unsigned int have_connection_lock : 1; /**< Used to check locking */ 303#endif 304 305#ifndef DBUS_DISABLE_CHECKS 306 int generation; /**< _dbus_current_generation that should correspond to this connection */ 307#endif 308}; 309 310static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection); 311static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection, 312 DBusDispatchStatus new_status); 313static void _dbus_connection_last_unref (DBusConnection *connection); 314static void _dbus_connection_acquire_dispatch (DBusConnection *connection); 315static void _dbus_connection_release_dispatch (DBusConnection *connection); 316static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection); 317static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection); 318static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection); 319 320static DBusMessageFilter * 321_dbus_message_filter_ref (DBusMessageFilter *filter) 322{ 323 _dbus_assert (filter->refcount.value > 0); 324 _dbus_atomic_inc (&filter->refcount); 325 326 return filter; 327} 328 329static void 330_dbus_message_filter_unref (DBusMessageFilter *filter) 331{ 332 _dbus_assert (filter->refcount.value > 0); 333 334 if (_dbus_atomic_dec (&filter->refcount) == 1) 335 { 336 if (filter->free_user_data_function) 337 (* filter->free_user_data_function) (filter->user_data); 338 339 dbus_free (filter); 340 } 341} 342 343/** 344 * Acquires the connection lock. 345 * 346 * @param connection the connection. 347 */ 348void 349_dbus_connection_lock (DBusConnection *connection) 350{ 351 CONNECTION_LOCK (connection); 352} 353 354/** 355 * Releases the connection lock. 356 * 357 * @param connection the connection. 358 */ 359void 360_dbus_connection_unlock (DBusConnection *connection) 361{ 362 CONNECTION_UNLOCK (connection); 363} 364 365/** 366 * Wakes up the main loop if it is sleeping 367 * Needed if we're e.g. queueing outgoing messages 368 * on a thread while the mainloop sleeps. 369 * 370 * @param connection the connection. 371 */ 372static void 373_dbus_connection_wakeup_mainloop (DBusConnection *connection) 374{ 375 if (connection->wakeup_main_function) 376 (*connection->wakeup_main_function) (connection->wakeup_main_data); 377} 378 379#ifdef DBUS_BUILD_TESTS 380/* For now this function isn't used */ 381/** 382 * Adds a message to the incoming message queue, returning #FALSE 383 * if there's insufficient memory to queue the message. 384 * Does not take over refcount of the message. 385 * 386 * @param connection the connection. 387 * @param message the message to queue. 388 * @returns #TRUE on success. 389 */ 390dbus_bool_t 391_dbus_connection_queue_received_message (DBusConnection *connection, 392 DBusMessage *message) 393{ 394 DBusList *link; 395 396 link = _dbus_list_alloc_link (message); 397 if (link == NULL) 398 return FALSE; 399 400 dbus_message_ref (message); 401 _dbus_connection_queue_received_message_link (connection, link); 402 403 return TRUE; 404} 405 406/** 407 * Gets the locks so we can examine them 408 * 409 * @param connection the connection. 410 * @param mutex_loc return for the location of the main mutex pointer 411 * @param dispatch_mutex_loc return location of the dispatch mutex pointer 412 * @param io_path_mutex_loc return location of the io_path mutex pointer 413 * @param dispatch_cond_loc return location of the dispatch conditional 414 * variable pointer 415 * @param io_path_cond_loc return location of the io_path conditional 416 * variable pointer 417 */ 418void 419_dbus_connection_test_get_locks (DBusConnection *connection, 420 DBusMutex **mutex_loc, 421 DBusMutex **dispatch_mutex_loc, 422 DBusMutex **io_path_mutex_loc, 423 DBusCondVar **dispatch_cond_loc, 424 DBusCondVar **io_path_cond_loc) 425{ 426 *mutex_loc = connection->mutex; 427 *dispatch_mutex_loc = connection->dispatch_mutex; 428 *io_path_mutex_loc = connection->io_path_mutex; 429 *dispatch_cond_loc = connection->dispatch_cond; 430 *io_path_cond_loc = connection->io_path_cond; 431} 432#endif 433 434/** 435 * Adds a message-containing list link to the incoming message queue, 436 * taking ownership of the link and the message's current refcount. 437 * Cannot fail due to lack of memory. 438 * 439 * @param connection the connection. 440 * @param link the message link to queue. 441 */ 442void 443_dbus_connection_queue_received_message_link (DBusConnection *connection, 444 DBusList *link) 445{ 446 DBusPendingCall *pending; 447 dbus_int32_t reply_serial; 448 DBusMessage *message; 449 450 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport)); 451 452 _dbus_list_append_link (&connection->incoming_messages, 453 link); 454 message = link->data; 455 456 /* If this is a reply we're waiting on, remove timeout for it */ 457 reply_serial = dbus_message_get_reply_serial (message); 458 if (reply_serial != -1) 459 { 460 pending = _dbus_hash_table_lookup_int (connection->pending_replies, 461 reply_serial); 462 if (pending != NULL) 463 { 464 if (_dbus_pending_call_is_timeout_added_unlocked (pending)) 465 _dbus_connection_remove_timeout_unlocked (connection, 466 _dbus_pending_call_get_timeout_unlocked (pending)); 467 468 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE); 469 } 470 } 471 472 473 474 connection->n_incoming += 1; 475 476 _dbus_connection_wakeup_mainloop (connection); 477 478 _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n", 479 message, 480 dbus_message_get_type (message), 481 dbus_message_get_path (message) ? 482 dbus_message_get_path (message) : 483 "no path", 484 dbus_message_get_interface (message) ? 485 dbus_message_get_interface (message) : 486 "no interface", 487 dbus_message_get_member (message) ? 488 dbus_message_get_member (message) : 489 "no member", 490 dbus_message_get_signature (message), 491 dbus_message_get_reply_serial (message), 492 connection, 493 connection->n_incoming);} 494 495/** 496 * Adds a link + message to the incoming message queue. 497 * Can't fail. Takes ownership of both link and message. 498 * 499 * @param connection the connection. 500 * @param link the list node and message to queue. 501 * 502 */ 503void 504_dbus_connection_queue_synthesized_message_link (DBusConnection *connection, 505 DBusList *link) 506{ 507 HAVE_LOCK_CHECK (connection); 508 509 _dbus_list_append_link (&connection->incoming_messages, link); 510 511 connection->n_incoming += 1; 512 513 _dbus_connection_wakeup_mainloop (connection); 514 515 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n", 516 link->data, connection, connection->n_incoming); 517} 518 519 520/** 521 * Checks whether there are messages in the outgoing message queue. 522 * Called with connection lock held. 523 * 524 * @param connection the connection. 525 * @returns #TRUE if the outgoing queue is non-empty. 526 */ 527dbus_bool_t 528_dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection) 529{ 530 HAVE_LOCK_CHECK (connection); 531 return connection->outgoing_messages != NULL; 532} 533 534/** 535 * Checks whether there are messages in the outgoing message queue. 536 * 537 * @param connection the connection. 538 * @returns #TRUE if the outgoing queue is non-empty. 539 */ 540dbus_bool_t 541dbus_connection_has_messages_to_send (DBusConnection *connection) 542{ 543 dbus_bool_t v; 544 545 _dbus_return_val_if_fail (connection != NULL, FALSE); 546 547 CONNECTION_LOCK (connection); 548 v = _dbus_connection_has_messages_to_send_unlocked (connection); 549 CONNECTION_UNLOCK (connection); 550 551 return v; 552} 553 554/** 555 * Gets the next outgoing message. The message remains in the 556 * queue, and the caller does not own a reference to it. 557 * 558 * @param connection the connection. 559 * @returns the message to be sent. 560 */ 561DBusMessage* 562_dbus_connection_get_message_to_send (DBusConnection *connection) 563{ 564 HAVE_LOCK_CHECK (connection); 565 566 return _dbus_list_get_last (&connection->outgoing_messages); 567} 568 569/** 570 * Notifies the connection that a message has been sent, so the 571 * message can be removed from the outgoing queue. 572 * Called with the connection lock held. 573 * 574 * @param connection the connection. 575 * @param message the message that was sent. 576 */ 577void 578_dbus_connection_message_sent (DBusConnection *connection, 579 DBusMessage *message) 580{ 581 DBusList *link; 582 583 HAVE_LOCK_CHECK (connection); 584 585 /* This can be called before we even complete authentication, since 586 * it's called on disconnect to clean up the outgoing queue. 587 * It's also called as we successfully send each message. 588 */ 589 590 link = _dbus_list_get_last_link (&connection->outgoing_messages); 591 _dbus_assert (link != NULL); 592 _dbus_assert (link->data == message); 593 594 /* Save this link in the link cache */ 595 _dbus_list_unlink (&connection->outgoing_messages, 596 link); 597 _dbus_list_prepend_link (&connection->link_cache, link); 598 599 connection->n_outgoing -= 1; 600 601 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n", 602 message, 603 dbus_message_get_type (message), 604 dbus_message_get_path (message) ? 605 dbus_message_get_path (message) : 606 "no path", 607 dbus_message_get_interface (message) ? 608 dbus_message_get_interface (message) : 609 "no interface", 610 dbus_message_get_member (message) ? 611 dbus_message_get_member (message) : 612 "no member", 613 dbus_message_get_signature (message), 614 connection, connection->n_outgoing); 615 616 /* Save this link in the link cache also */ 617 _dbus_message_remove_size_counter (message, connection->outgoing_counter, 618 &link); 619 _dbus_list_prepend_link (&connection->link_cache, link); 620 621 dbus_message_unref (message); 622} 623 624/** Function to be called in protected_change_watch() with refcount held */ 625typedef dbus_bool_t (* DBusWatchAddFunction) (DBusWatchList *list, 626 DBusWatch *watch); 627/** Function to be called in protected_change_watch() with refcount held */ 628typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list, 629 DBusWatch *watch); 630/** Function to be called in protected_change_watch() with refcount held */ 631typedef void (* DBusWatchToggleFunction) (DBusWatchList *list, 632 DBusWatch *watch, 633 dbus_bool_t enabled); 634 635static dbus_bool_t 636protected_change_watch (DBusConnection *connection, 637 DBusWatch *watch, 638 DBusWatchAddFunction add_function, 639 DBusWatchRemoveFunction remove_function, 640 DBusWatchToggleFunction toggle_function, 641 dbus_bool_t enabled) 642{ 643 DBusWatchList *watches; 644 dbus_bool_t retval; 645 646 HAVE_LOCK_CHECK (connection); 647 648 /* This isn't really safe or reasonable; a better pattern is the "do everything, then 649 * drop lock and call out" one; but it has to be propagated up through all callers 650 */ 651 652 watches = connection->watches; 653 if (watches) 654 { 655 connection->watches = NULL; 656 _dbus_connection_ref_unlocked (connection); 657 CONNECTION_UNLOCK (connection); 658 659 if (add_function) 660 retval = (* add_function) (watches, watch); 661 else if (remove_function) 662 { 663 retval = TRUE; 664 (* remove_function) (watches, watch); 665 } 666 else 667 { 668 retval = TRUE; 669 (* toggle_function) (watches, watch, enabled); 670 } 671 672 CONNECTION_LOCK (connection); 673 connection->watches = watches; 674 _dbus_connection_unref_unlocked (connection); 675 676 return retval; 677 } 678 else 679 return FALSE; 680} 681 682 683/** 684 * Adds a watch using the connection's DBusAddWatchFunction if 685 * available. Otherwise records the watch to be added when said 686 * function is available. Also re-adds the watch if the 687 * DBusAddWatchFunction changes. May fail due to lack of memory. 688 * Connection lock should be held when calling this. 689 * 690 * @param connection the connection. 691 * @param watch the watch to add. 692 * @returns #TRUE on success. 693 */ 694dbus_bool_t 695_dbus_connection_add_watch_unlocked (DBusConnection *connection, 696 DBusWatch *watch) 697{ 698 return protected_change_watch (connection, watch, 699 _dbus_watch_list_add_watch, 700 NULL, NULL, FALSE); 701} 702 703/** 704 * Removes a watch using the connection's DBusRemoveWatchFunction 705 * if available. It's an error to call this function on a watch 706 * that was not previously added. 707 * Connection lock should be held when calling this. 708 * 709 * @param connection the connection. 710 * @param watch the watch to remove. 711 */ 712void 713_dbus_connection_remove_watch_unlocked (DBusConnection *connection, 714 DBusWatch *watch) 715{ 716 protected_change_watch (connection, watch, 717 NULL, 718 _dbus_watch_list_remove_watch, 719 NULL, FALSE); 720} 721 722/** 723 * Toggles a watch and notifies app via connection's 724 * DBusWatchToggledFunction if available. It's an error to call this 725 * function on a watch that was not previously added. 726 * Connection lock should be held when calling this. 727 * 728 * @param connection the connection. 729 * @param watch the watch to toggle. 730 * @param enabled whether to enable or disable 731 */ 732void 733_dbus_connection_toggle_watch_unlocked (DBusConnection *connection, 734 DBusWatch *watch, 735 dbus_bool_t enabled) 736{ 737 _dbus_assert (watch != NULL); 738 739 protected_change_watch (connection, watch, 740 NULL, NULL, 741 _dbus_watch_list_toggle_watch, 742 enabled); 743} 744 745/** Function to be called in protected_change_timeout() with refcount held */ 746typedef dbus_bool_t (* DBusTimeoutAddFunction) (DBusTimeoutList *list, 747 DBusTimeout *timeout); 748/** Function to be called in protected_change_timeout() with refcount held */ 749typedef void (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list, 750 DBusTimeout *timeout); 751/** Function to be called in protected_change_timeout() with refcount held */ 752typedef void (* DBusTimeoutToggleFunction) (DBusTimeoutList *list, 753 DBusTimeout *timeout, 754 dbus_bool_t enabled); 755 756static dbus_bool_t 757protected_change_timeout (DBusConnection *connection, 758 DBusTimeout *timeout, 759 DBusTimeoutAddFunction add_function, 760 DBusTimeoutRemoveFunction remove_function, 761 DBusTimeoutToggleFunction toggle_function, 762 dbus_bool_t enabled) 763{ 764 DBusTimeoutList *timeouts; 765 dbus_bool_t retval; 766 767 HAVE_LOCK_CHECK (connection); 768 769 /* This isn't really safe or reasonable; a better pattern is the "do everything, then 770 * drop lock and call out" one; but it has to be propagated up through all callers 771 */ 772 773 timeouts = connection->timeouts; 774 if (timeouts) 775 { 776 connection->timeouts = NULL; 777 _dbus_connection_ref_unlocked (connection); 778 CONNECTION_UNLOCK (connection); 779 780 if (add_function) 781 retval = (* add_function) (timeouts, timeout); 782 else if (remove_function) 783 { 784 retval = TRUE; 785 (* remove_function) (timeouts, timeout); 786 } 787 else 788 { 789 retval = TRUE; 790 (* toggle_function) (timeouts, timeout, enabled); 791 } 792 793 CONNECTION_LOCK (connection); 794 connection->timeouts = timeouts; 795 _dbus_connection_unref_unlocked (connection); 796 797 return retval; 798 } 799 else 800 return FALSE; 801} 802 803/** 804 * Adds a timeout using the connection's DBusAddTimeoutFunction if 805 * available. Otherwise records the timeout to be added when said 806 * function is available. Also re-adds the timeout if the 807 * DBusAddTimeoutFunction changes. May fail due to lack of memory. 808 * The timeout will fire repeatedly until removed. 809 * Connection lock should be held when calling this. 810 * 811 * @param connection the connection. 812 * @param timeout the timeout to add. 813 * @returns #TRUE on success. 814 */ 815dbus_bool_t 816_dbus_connection_add_timeout_unlocked (DBusConnection *connection, 817 DBusTimeout *timeout) 818{ 819 return protected_change_timeout (connection, timeout, 820 _dbus_timeout_list_add_timeout, 821 NULL, NULL, FALSE); 822} 823 824/** 825 * Removes a timeout using the connection's DBusRemoveTimeoutFunction 826 * if available. It's an error to call this function on a timeout 827 * that was not previously added. 828 * Connection lock should be held when calling this. 829 * 830 * @param connection the connection. 831 * @param timeout the timeout to remove. 832 */ 833void 834_dbus_connection_remove_timeout_unlocked (DBusConnection *connection, 835 DBusTimeout *timeout) 836{ 837 protected_change_timeout (connection, timeout, 838 NULL, 839 _dbus_timeout_list_remove_timeout, 840 NULL, FALSE); 841} 842 843/** 844 * Toggles a timeout and notifies app via connection's 845 * DBusTimeoutToggledFunction if available. It's an error to call this 846 * function on a timeout that was not previously added. 847 * Connection lock should be held when calling this. 848 * 849 * @param connection the connection. 850 * @param timeout the timeout to toggle. 851 * @param enabled whether to enable or disable 852 */ 853void 854_dbus_connection_toggle_timeout_unlocked (DBusConnection *connection, 855 DBusTimeout *timeout, 856 dbus_bool_t enabled) 857{ 858 protected_change_timeout (connection, timeout, 859 NULL, NULL, 860 _dbus_timeout_list_toggle_timeout, 861 enabled); 862} 863 864static dbus_bool_t 865_dbus_connection_attach_pending_call_unlocked (DBusConnection *connection, 866 DBusPendingCall *pending) 867{ 868 dbus_uint32_t reply_serial; 869 DBusTimeout *timeout; 870 871 HAVE_LOCK_CHECK (connection); 872 873 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending); 874 875 _dbus_assert (reply_serial != 0); 876 877 timeout = _dbus_pending_call_get_timeout_unlocked (pending); 878 879 if (!_dbus_connection_add_timeout_unlocked (connection, timeout)) 880 return FALSE; 881 882 if (!_dbus_hash_table_insert_int (connection->pending_replies, 883 reply_serial, 884 pending)) 885 { 886 _dbus_connection_remove_timeout_unlocked (connection, timeout); 887 888 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE); 889 HAVE_LOCK_CHECK (connection); 890 return FALSE; 891 } 892 893 _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE); 894 895 _dbus_pending_call_ref_unlocked (pending); 896 897 HAVE_LOCK_CHECK (connection); 898 899 return TRUE; 900} 901 902static void 903free_pending_call_on_hash_removal (void *data) 904{ 905 DBusPendingCall *pending; 906 DBusConnection *connection; 907 908 if (data == NULL) 909 return; 910 911 pending = data; 912 913 connection = _dbus_pending_call_get_connection_unlocked (pending); 914 915 HAVE_LOCK_CHECK (connection); 916 917 if (_dbus_pending_call_is_timeout_added_unlocked (pending)) 918 { 919 _dbus_connection_remove_timeout_unlocked (connection, 920 _dbus_pending_call_get_timeout_unlocked (pending)); 921 922 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE); 923 } 924 925 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock 926 * here, but the pending call finalizer could in principle call out to 927 * application code so we pretty much have to... some larger code reorg 928 * might be needed. 929 */ 930 _dbus_connection_ref_unlocked (connection); 931 _dbus_pending_call_unref_and_unlock (pending); 932 CONNECTION_LOCK (connection); 933 _dbus_connection_unref_unlocked (connection); 934} 935 936static void 937_dbus_connection_detach_pending_call_unlocked (DBusConnection *connection, 938 DBusPendingCall *pending) 939{ 940 /* This ends up unlocking to call the pending call finalizer, which is unexpected to 941 * say the least. 942 */ 943 _dbus_hash_table_remove_int (connection->pending_replies, 944 _dbus_pending_call_get_reply_serial_unlocked (pending)); 945} 946 947static void 948_dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection, 949 DBusPendingCall *pending) 950{ 951 /* The idea here is to avoid finalizing the pending call 952 * with the lock held, since there's a destroy notifier 953 * in pending call that goes out to application code. 954 * 955 * There's an extra unlock inside the hash table 956 * "free pending call" function FIXME... 957 */ 958 _dbus_pending_call_ref_unlocked (pending); 959 _dbus_hash_table_remove_int (connection->pending_replies, 960 _dbus_pending_call_get_reply_serial_unlocked (pending)); 961 _dbus_pending_call_unref_and_unlock (pending); 962} 963 964/** 965 * Removes a pending call from the connection, such that 966 * the pending reply will be ignored. May drop the last 967 * reference to the pending call. 968 * 969 * @param connection the connection 970 * @param pending the pending call 971 */ 972void 973_dbus_connection_remove_pending_call (DBusConnection *connection, 974 DBusPendingCall *pending) 975{ 976 CONNECTION_LOCK (connection); 977 _dbus_connection_detach_pending_call_and_unlock (connection, pending); 978} 979 980/** 981 * Acquire the transporter I/O path. This must be done before 982 * doing any I/O in the transporter. May sleep and drop the 983 * IO path mutex while waiting for the I/O path. 984 * 985 * @param connection the connection. 986 * @param timeout_milliseconds maximum blocking time, or -1 for no limit. 987 * @returns TRUE if the I/O path was acquired. 988 */ 989static dbus_bool_t 990_dbus_connection_acquire_io_path (DBusConnection *connection, 991 int timeout_milliseconds) 992{ 993 dbus_bool_t we_acquired; 994 995 HAVE_LOCK_CHECK (connection); 996 997 /* We don't want the connection to vanish */ 998 _dbus_connection_ref_unlocked (connection); 999 1000 /* We will only touch io_path_acquired which is protected by our mutex */ 1001 CONNECTION_UNLOCK (connection); 1002 1003 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME); 1004 _dbus_mutex_lock (connection->io_path_mutex); 1005 1006 _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n", 1007 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds); 1008 1009 we_acquired = FALSE; 1010 1011 if (connection->io_path_acquired) 1012 { 1013 if (timeout_milliseconds != -1) 1014 { 1015 _dbus_verbose ("%s waiting %d for IO path to be acquirable\n", 1016 _DBUS_FUNCTION_NAME, timeout_milliseconds); 1017 _dbus_condvar_wait_timeout (connection->io_path_cond, 1018 connection->io_path_mutex, 1019 timeout_milliseconds); 1020 } 1021 else 1022 { 1023 while (connection->io_path_acquired) 1024 { 1025 _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME); 1026 _dbus_condvar_wait (connection->io_path_cond, 1027 connection->io_path_mutex); 1028 } 1029 } 1030 } 1031 1032 if (!connection->io_path_acquired) 1033 { 1034 we_acquired = TRUE; 1035 connection->io_path_acquired = TRUE; 1036 } 1037 1038 _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n", 1039 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired); 1040 1041 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME); 1042 _dbus_mutex_unlock (connection->io_path_mutex); 1043 1044 CONNECTION_LOCK (connection); 1045 1046 HAVE_LOCK_CHECK (connection); 1047 1048 _dbus_connection_unref_unlocked (connection); 1049 1050 return we_acquired; 1051} 1052 1053/** 1054 * Release the I/O path when you're done with it. Only call 1055 * after you've acquired the I/O. Wakes up at most one thread 1056 * currently waiting to acquire the I/O path. 1057 * 1058 * @param connection the connection. 1059 */ 1060static void 1061_dbus_connection_release_io_path (DBusConnection *connection) 1062{ 1063 HAVE_LOCK_CHECK (connection); 1064 1065 _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME); 1066 _dbus_mutex_lock (connection->io_path_mutex); 1067 1068 _dbus_assert (connection->io_path_acquired); 1069 1070 _dbus_verbose ("%s start connection->io_path_acquired = %d\n", 1071 _DBUS_FUNCTION_NAME, connection->io_path_acquired); 1072 1073 connection->io_path_acquired = FALSE; 1074 _dbus_condvar_wake_one (connection->io_path_cond); 1075 1076 _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME); 1077 _dbus_mutex_unlock (connection->io_path_mutex); 1078} 1079 1080/** 1081 * Queues incoming messages and sends outgoing messages for this 1082 * connection, optionally blocking in the process. Each call to 1083 * _dbus_connection_do_iteration_unlocked() will call select() or poll() one 1084 * time and then read or write data if possible. 1085 * 1086 * The purpose of this function is to be able to flush outgoing 1087 * messages or queue up incoming messages without returning 1088 * control to the application and causing reentrancy weirdness. 1089 * 1090 * The flags parameter allows you to specify whether to 1091 * read incoming messages, write outgoing messages, or both, 1092 * and whether to block if no immediate action is possible. 1093 * 1094 * The timeout_milliseconds parameter does nothing unless the 1095 * iteration is blocking. 1096 * 1097 * If there are no outgoing messages and DBUS_ITERATION_DO_READING 1098 * wasn't specified, then it's impossible to block, even if 1099 * you specify DBUS_ITERATION_BLOCK; in that case the function 1100 * returns immediately. 1101 * 1102 * Called with connection lock held. 1103 * 1104 * @param connection the connection. 1105 * @param flags iteration flags. 1106 * @param timeout_milliseconds maximum blocking time, or -1 for no limit. 1107 */ 1108void 1109_dbus_connection_do_iteration_unlocked (DBusConnection *connection, 1110 unsigned int flags, 1111 int timeout_milliseconds) 1112{ 1113 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME); 1114 1115 HAVE_LOCK_CHECK (connection); 1116 1117 if (connection->n_outgoing == 0) 1118 flags &= ~DBUS_ITERATION_DO_WRITING; 1119 1120 if (_dbus_connection_acquire_io_path (connection, 1121 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0)) 1122 { 1123 HAVE_LOCK_CHECK (connection); 1124 1125 _dbus_transport_do_iteration (connection->transport, 1126 flags, timeout_milliseconds); 1127 _dbus_connection_release_io_path (connection); 1128 } 1129 1130 HAVE_LOCK_CHECK (connection); 1131 1132 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME); 1133} 1134 1135/** 1136 * Creates a new connection for the given transport. A transport 1137 * represents a message stream that uses some concrete mechanism, such 1138 * as UNIX domain sockets. May return #NULL if insufficient 1139 * memory exists to create the connection. 1140 * 1141 * @param transport the transport. 1142 * @returns the new connection, or #NULL on failure. 1143 */ 1144DBusConnection* 1145_dbus_connection_new_for_transport (DBusTransport *transport) 1146{ 1147 DBusConnection *connection; 1148 DBusWatchList *watch_list; 1149 DBusTimeoutList *timeout_list; 1150 DBusHashTable *pending_replies; 1151 DBusList *disconnect_link; 1152 DBusMessage *disconnect_message; 1153 DBusCounter *outgoing_counter; 1154 DBusObjectTree *objects; 1155 1156 watch_list = NULL; 1157 connection = NULL; 1158 pending_replies = NULL; 1159 timeout_list = NULL; 1160 disconnect_link = NULL; 1161 disconnect_message = NULL; 1162 outgoing_counter = NULL; 1163 objects = NULL; 1164 1165 watch_list = _dbus_watch_list_new (); 1166 if (watch_list == NULL) 1167 goto error; 1168 1169 timeout_list = _dbus_timeout_list_new (); 1170 if (timeout_list == NULL) 1171 goto error; 1172 1173 pending_replies = 1174 _dbus_hash_table_new (DBUS_HASH_INT, 1175 NULL, 1176 (DBusFreeFunction)free_pending_call_on_hash_removal); 1177 if (pending_replies == NULL) 1178 goto error; 1179 1180 connection = dbus_new0 (DBusConnection, 1); 1181 if (connection == NULL) 1182 goto error; 1183 1184 _dbus_mutex_new_at_location (&connection->mutex); 1185 if (connection->mutex == NULL) 1186 goto error; 1187 1188 _dbus_mutex_new_at_location (&connection->io_path_mutex); 1189 if (connection->io_path_mutex == NULL) 1190 goto error; 1191 1192 _dbus_mutex_new_at_location (&connection->dispatch_mutex); 1193 if (connection->dispatch_mutex == NULL) 1194 goto error; 1195 1196 _dbus_condvar_new_at_location (&connection->dispatch_cond); 1197 if (connection->dispatch_cond == NULL) 1198 goto error; 1199 1200 _dbus_condvar_new_at_location (&connection->io_path_cond); 1201 if (connection->io_path_cond == NULL) 1202 goto error; 1203 1204 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL, 1205 DBUS_INTERFACE_LOCAL, 1206 "Disconnected"); 1207 1208 if (disconnect_message == NULL) 1209 goto error; 1210 1211 disconnect_link = _dbus_list_alloc_link (disconnect_message); 1212 if (disconnect_link == NULL) 1213 goto error; 1214 1215 outgoing_counter = _dbus_counter_new (); 1216 if (outgoing_counter == NULL) 1217 goto error; 1218 1219 objects = _dbus_object_tree_new (connection); 1220 if (objects == NULL) 1221 goto error; 1222 1223 if (_dbus_modify_sigpipe) 1224 _dbus_disable_sigpipe (); 1225 1226 connection->refcount.value = 1; 1227 connection->transport = transport; 1228 connection->watches = watch_list; 1229 connection->timeouts = timeout_list; 1230 connection->pending_replies = pending_replies; 1231 connection->outgoing_counter = outgoing_counter; 1232 connection->filter_list = NULL; 1233 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */ 1234 connection->objects = objects; 1235 connection->exit_on_disconnect = FALSE; 1236 connection->shareable = FALSE; 1237 connection->route_peer_messages = FALSE; 1238 connection->disconnected_message_arrived = FALSE; 1239 connection->disconnected_message_processed = FALSE; 1240 1241#ifndef DBUS_DISABLE_CHECKS 1242 connection->generation = _dbus_current_generation; 1243#endif 1244 1245 _dbus_data_slot_list_init (&connection->slot_list); 1246 1247 connection->client_serial = 1; 1248 1249 connection->disconnect_message_link = disconnect_link; 1250 1251 CONNECTION_LOCK (connection); 1252 1253 if (!_dbus_transport_set_connection (transport, connection)) 1254 { 1255 CONNECTION_UNLOCK (connection); 1256 1257 goto error; 1258 } 1259 1260 _dbus_transport_ref (transport); 1261 1262 CONNECTION_UNLOCK (connection); 1263 1264 return connection; 1265 1266 error: 1267 if (disconnect_message != NULL) 1268 dbus_message_unref (disconnect_message); 1269 1270 if (disconnect_link != NULL) 1271 _dbus_list_free_link (disconnect_link); 1272 1273 if (connection != NULL) 1274 { 1275 _dbus_condvar_free_at_location (&connection->io_path_cond); 1276 _dbus_condvar_free_at_location (&connection->dispatch_cond); 1277 _dbus_mutex_free_at_location (&connection->mutex); 1278 _dbus_mutex_free_at_location (&connection->io_path_mutex); 1279 _dbus_mutex_free_at_location (&connection->dispatch_mutex); 1280 dbus_free (connection); 1281 } 1282 if (pending_replies) 1283 _dbus_hash_table_unref (pending_replies); 1284 1285 if (watch_list) 1286 _dbus_watch_list_free (watch_list); 1287 1288 if (timeout_list) 1289 _dbus_timeout_list_free (timeout_list); 1290 1291 if (outgoing_counter) 1292 _dbus_counter_unref (outgoing_counter); 1293 1294 if (objects) 1295 _dbus_object_tree_unref (objects); 1296 1297 return NULL; 1298} 1299 1300/** 1301 * Increments the reference count of a DBusConnection. 1302 * Requires that the caller already holds the connection lock. 1303 * 1304 * @param connection the connection. 1305 * @returns the connection. 1306 */ 1307DBusConnection * 1308_dbus_connection_ref_unlocked (DBusConnection *connection) 1309{ 1310 _dbus_assert (connection != NULL); 1311 _dbus_assert (connection->generation == _dbus_current_generation); 1312 1313 HAVE_LOCK_CHECK (connection); 1314 1315#ifdef DBUS_HAVE_ATOMIC_INT 1316 _dbus_atomic_inc (&connection->refcount); 1317#else 1318 _dbus_assert (connection->refcount.value > 0); 1319 connection->refcount.value += 1; 1320#endif 1321 1322 return connection; 1323} 1324 1325/** 1326 * Decrements the reference count of a DBusConnection. 1327 * Requires that the caller already holds the connection lock. 1328 * 1329 * @param connection the connection. 1330 */ 1331void 1332_dbus_connection_unref_unlocked (DBusConnection *connection) 1333{ 1334 dbus_bool_t last_unref; 1335 1336 HAVE_LOCK_CHECK (connection); 1337 1338 _dbus_assert (connection != NULL); 1339 1340 /* The connection lock is better than the global 1341 * lock in the atomic increment fallback 1342 */ 1343 1344#ifdef DBUS_HAVE_ATOMIC_INT 1345 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1); 1346#else 1347 _dbus_assert (connection->refcount.value > 0); 1348 1349 connection->refcount.value -= 1; 1350 last_unref = (connection->refcount.value == 0); 1351#if 0 1352 printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value); 1353#endif 1354#endif 1355 1356 if (last_unref) 1357 _dbus_connection_last_unref (connection); 1358} 1359 1360static dbus_uint32_t 1361_dbus_connection_get_next_client_serial (DBusConnection *connection) 1362{ 1363 int serial; 1364 1365 serial = connection->client_serial++; 1366 1367 if (connection->client_serial < 0) 1368 connection->client_serial = 1; 1369 1370 return serial; 1371} 1372 1373/** 1374 * A callback for use with dbus_watch_new() to create a DBusWatch. 1375 * 1376 * @todo This is basically a hack - we could delete _dbus_transport_handle_watch() 1377 * and the virtual handle_watch in DBusTransport if we got rid of it. 1378 * The reason this is some work is threading, see the _dbus_connection_handle_watch() 1379 * implementation. 1380 * 1381 * @param watch the watch. 1382 * @param condition the current condition of the file descriptors being watched. 1383 * @param data must be a pointer to a #DBusConnection 1384 * @returns #FALSE if the IO condition may not have been fully handled due to lack of memory 1385 */ 1386dbus_bool_t 1387_dbus_connection_handle_watch (DBusWatch *watch, 1388 unsigned int condition, 1389 void *data) 1390{ 1391 DBusConnection *connection; 1392 dbus_bool_t retval; 1393 DBusDispatchStatus status; 1394 1395 connection = data; 1396 1397 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME); 1398 1399 CONNECTION_LOCK (connection); 1400 _dbus_connection_acquire_io_path (connection, -1); 1401 HAVE_LOCK_CHECK (connection); 1402 retval = _dbus_transport_handle_watch (connection->transport, 1403 watch, condition); 1404 1405 _dbus_connection_release_io_path (connection); 1406 1407 HAVE_LOCK_CHECK (connection); 1408 1409 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME); 1410 1411 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1412 1413 /* this calls out to user code */ 1414 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 1415 1416 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME); 1417 1418 return retval; 1419} 1420 1421_DBUS_DEFINE_GLOBAL_LOCK (shared_connections); 1422static DBusHashTable *shared_connections = NULL; 1423 1424static void 1425shared_connections_shutdown (void *data) 1426{ 1427 int n_entries; 1428 1429 _DBUS_LOCK (shared_connections); 1430 1431 /* This is a little bit unpleasant... better ideas? */ 1432 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0) 1433 { 1434 DBusConnection *connection; 1435 DBusMessage *message; 1436 DBusHashIter iter; 1437 1438 _dbus_hash_iter_init (shared_connections, &iter); 1439 _dbus_hash_iter_next (&iter); 1440 1441 connection = _dbus_hash_iter_get_value (&iter); 1442 1443 _DBUS_UNLOCK (shared_connections); 1444 1445 dbus_connection_ref (connection); 1446 _dbus_connection_close_possibly_shared (connection); 1447 1448 /* Churn through to the Disconnected message */ 1449 while ((message = dbus_connection_pop_message (connection))) 1450 { 1451 dbus_message_unref (message); 1452 } 1453 dbus_connection_unref (connection); 1454 1455 _DBUS_LOCK (shared_connections); 1456 1457 /* The connection should now be dead and not in our hash ... */ 1458 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries); 1459 } 1460 1461 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0); 1462 1463 _dbus_hash_table_unref (shared_connections); 1464 shared_connections = NULL; 1465 1466 _DBUS_UNLOCK (shared_connections); 1467} 1468 1469static dbus_bool_t 1470connection_lookup_shared (DBusAddressEntry *entry, 1471 DBusConnection **result) 1472{ 1473 _dbus_verbose ("checking for existing connection\n"); 1474 1475 *result = NULL; 1476 1477 _DBUS_LOCK (shared_connections); 1478 1479 if (shared_connections == NULL) 1480 { 1481 _dbus_verbose ("creating shared_connections hash table\n"); 1482 1483 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING, 1484 dbus_free, 1485 NULL); 1486 if (shared_connections == NULL) 1487 { 1488 _DBUS_UNLOCK (shared_connections); 1489 return FALSE; 1490 } 1491 1492 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL)) 1493 { 1494 _dbus_hash_table_unref (shared_connections); 1495 shared_connections = NULL; 1496 _DBUS_UNLOCK (shared_connections); 1497 return FALSE; 1498 } 1499 1500 _dbus_verbose (" successfully created shared_connections\n"); 1501 1502 _DBUS_UNLOCK (shared_connections); 1503 return TRUE; /* no point looking up in the hash we just made */ 1504 } 1505 else 1506 { 1507 const char *guid; 1508 1509 guid = dbus_address_entry_get_value (entry, "guid"); 1510 1511 if (guid != NULL) 1512 { 1513 DBusConnection *connection; 1514 1515 connection = _dbus_hash_table_lookup_string (shared_connections, 1516 guid); 1517 1518 if (connection) 1519 { 1520 /* The DBusConnection can't be finalized without taking 1521 * the shared_connections lock to remove it from the 1522 * hash. So it's safe to ref the connection here. 1523 * However, it may be disconnected if the Disconnected 1524 * message hasn't been processed yet, in which case we 1525 * want to pretend it isn't in the hash and avoid 1526 * returning it. 1527 * 1528 * The idea is to avoid ever returning a disconnected connection 1529 * from dbus_connection_open(). We could just synchronously 1530 * drop our shared ref to the connection on connection disconnect, 1531 * and then assert here that the connection is connected, but 1532 * that causes reentrancy headaches. 1533 */ 1534 CONNECTION_LOCK (connection); 1535 if (_dbus_connection_get_is_connected_unlocked (connection)) 1536 { 1537 _dbus_connection_ref_unlocked (connection); 1538 *result = connection; 1539 _dbus_verbose ("looked up existing connection to server guid %s\n", 1540 guid); 1541 } 1542 else 1543 { 1544 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n", 1545 guid); 1546 } 1547 CONNECTION_UNLOCK (connection); 1548 } 1549 } 1550 1551 _DBUS_UNLOCK (shared_connections); 1552 return TRUE; 1553 } 1554} 1555 1556static dbus_bool_t 1557connection_record_shared_unlocked (DBusConnection *connection, 1558 const char *guid) 1559{ 1560 char *guid_key; 1561 char *guid_in_connection; 1562 1563 HAVE_LOCK_CHECK (connection); 1564 _dbus_assert (connection->server_guid == NULL); 1565 _dbus_assert (connection->shareable); 1566 1567 /* get a hard ref on this connection, even if 1568 * we won't in fact store it in the hash, we still 1569 * need to hold a ref on it until it's disconnected. 1570 */ 1571 _dbus_connection_ref_unlocked (connection); 1572 1573 if (guid == NULL) 1574 return TRUE; /* don't store in the hash */ 1575 1576 /* A separate copy of the key is required in the hash table, because 1577 * we don't have a lock on the connection when we are doing a hash 1578 * lookup. 1579 */ 1580 1581 guid_key = _dbus_strdup (guid); 1582 if (guid_key == NULL) 1583 return FALSE; 1584 1585 guid_in_connection = _dbus_strdup (guid); 1586 if (guid_in_connection == NULL) 1587 { 1588 dbus_free (guid_key); 1589 return FALSE; 1590 } 1591 1592 _DBUS_LOCK (shared_connections); 1593 _dbus_assert (shared_connections != NULL); 1594 1595 if (!_dbus_hash_table_insert_string (shared_connections, 1596 guid_key, connection)) 1597 { 1598 dbus_free (guid_key); 1599 dbus_free (guid_in_connection); 1600 _DBUS_UNLOCK (shared_connections); 1601 return FALSE; 1602 } 1603 1604 connection->server_guid = guid_in_connection; 1605 1606 _dbus_verbose ("stored connection to %s to be shared\n", 1607 connection->server_guid); 1608 1609 _DBUS_UNLOCK (shared_connections); 1610 1611 _dbus_assert (connection->server_guid != NULL); 1612 1613 return TRUE; 1614} 1615 1616static void 1617connection_forget_shared_unlocked (DBusConnection *connection) 1618{ 1619 HAVE_LOCK_CHECK (connection); 1620 1621 if (!connection->shareable) 1622 return; 1623 1624 if (connection->server_guid != NULL) 1625 { 1626 _dbus_verbose ("dropping connection to %s out of the shared table\n", 1627 connection->server_guid); 1628 1629 _DBUS_LOCK (shared_connections); 1630 1631 if (!_dbus_hash_table_remove_string (shared_connections, 1632 connection->server_guid)) 1633 _dbus_assert_not_reached ("connection was not in the shared table"); 1634 1635 dbus_free (connection->server_guid); 1636 connection->server_guid = NULL; 1637 _DBUS_UNLOCK (shared_connections); 1638 } 1639 1640 /* remove our reference held on all shareable connections */ 1641 _dbus_connection_unref_unlocked (connection); 1642} 1643 1644static DBusConnection* 1645connection_try_from_address_entry (DBusAddressEntry *entry, 1646 DBusError *error) 1647{ 1648 DBusTransport *transport; 1649 DBusConnection *connection; 1650 1651 transport = _dbus_transport_open (entry, error); 1652 1653 if (transport == NULL) 1654 { 1655 _DBUS_ASSERT_ERROR_IS_SET (error); 1656 return NULL; 1657 } 1658 1659 connection = _dbus_connection_new_for_transport (transport); 1660 1661 _dbus_transport_unref (transport); 1662 1663 if (connection == NULL) 1664 { 1665 _DBUS_SET_OOM (error); 1666 return NULL; 1667 } 1668 1669#ifndef DBUS_DISABLE_CHECKS 1670 _dbus_assert (!connection->have_connection_lock); 1671#endif 1672 return connection; 1673} 1674 1675/* 1676 * If the shared parameter is true, then any existing connection will 1677 * be used (and if a new connection is created, it will be available 1678 * for use by others). If the shared parameter is false, a new 1679 * connection will always be created, and the new connection will 1680 * never be returned to other callers. 1681 * 1682 * @param address the address 1683 * @param shared whether the connection is shared or private 1684 * @param error error return 1685 * @returns the connection or #NULL on error 1686 */ 1687static DBusConnection* 1688_dbus_connection_open_internal (const char *address, 1689 dbus_bool_t shared, 1690 DBusError *error) 1691{ 1692 DBusConnection *connection; 1693 DBusAddressEntry **entries; 1694 DBusError tmp_error; 1695 DBusError first_error; 1696 int len, i; 1697 1698 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 1699 1700 _dbus_verbose ("opening %s connection to: %s\n", 1701 shared ? "shared" : "private", address); 1702 1703 if (!dbus_parse_address (address, &entries, &len, error)) 1704 return NULL; 1705 1706 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 1707 1708 connection = NULL; 1709 1710 dbus_error_init (&tmp_error); 1711 dbus_error_init (&first_error); 1712 for (i = 0; i < len; i++) 1713 { 1714 if (shared) 1715 { 1716 if (!connection_lookup_shared (entries[i], &connection)) 1717 _DBUS_SET_OOM (&tmp_error); 1718 } 1719 1720 if (connection == NULL) 1721 { 1722 connection = connection_try_from_address_entry (entries[i], 1723 &tmp_error); 1724 1725 if (connection != NULL) 1726 { 1727 CONNECTION_LOCK (connection); 1728 1729 if (shared) 1730 { 1731 const char *guid; 1732 1733 connection->shareable = TRUE; 1734 1735 /* guid may be NULL */ 1736 guid = dbus_address_entry_get_value (entries[i], "guid"); 1737 1738 if (!connection_record_shared_unlocked (connection, guid)) 1739 { 1740 _DBUS_SET_OOM (&tmp_error); 1741 _dbus_connection_close_possibly_shared_and_unlock (connection); 1742 dbus_connection_unref (connection); 1743 connection = NULL; 1744 } 1745 1746 /* Note: as of now the connection is possibly shared 1747 * since another thread could have pulled it from the table. 1748 * However, we still have it locked so that thread isn't 1749 * doing anything more than blocking on the lock. 1750 */ 1751 } 1752 } 1753 } 1754 1755 if (connection) 1756 { 1757 HAVE_LOCK_CHECK (connection); 1758 break; 1759 } 1760 1761 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error); 1762 1763 if (i == 0) 1764 dbus_move_error (&tmp_error, &first_error); 1765 else 1766 dbus_error_free (&tmp_error); 1767 } 1768 1769 _DBUS_ASSERT_ERROR_IS_CLEAR (error); 1770 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error); 1771 1772 if (connection == NULL) 1773 { 1774 _DBUS_ASSERT_ERROR_IS_SET (&first_error); 1775 dbus_move_error (&first_error, error); 1776 } 1777 else 1778 { 1779 dbus_error_free (&first_error); 1780 1781 CONNECTION_UNLOCK (connection); 1782 } 1783 1784 dbus_address_entries_free (entries); 1785 return connection; 1786} 1787 1788/** 1789 * Closes a shared OR private connection, while dbus_connection_close() can 1790 * only be used on private connections. Should only be called by the 1791 * dbus code that owns the connection - an owner must be known, 1792 * the open/close state is like malloc/free, not like ref/unref. 1793 * 1794 * @param connection the connection 1795 */ 1796void 1797_dbus_connection_close_possibly_shared (DBusConnection *connection) 1798{ 1799 _dbus_assert (connection != NULL); 1800 _dbus_assert (connection->generation == _dbus_current_generation); 1801 1802 CONNECTION_LOCK (connection); 1803 _dbus_connection_close_possibly_shared_and_unlock (connection); 1804} 1805 1806static DBusPreallocatedSend* 1807_dbus_connection_preallocate_send_unlocked (DBusConnection *connection) 1808{ 1809 DBusPreallocatedSend *preallocated; 1810 1811 HAVE_LOCK_CHECK (connection); 1812 1813 _dbus_assert (connection != NULL); 1814 1815 preallocated = dbus_new (DBusPreallocatedSend, 1); 1816 if (preallocated == NULL) 1817 return NULL; 1818 1819 if (connection->link_cache != NULL) 1820 { 1821 preallocated->queue_link = 1822 _dbus_list_pop_first_link (&connection->link_cache); 1823 preallocated->queue_link->data = NULL; 1824 } 1825 else 1826 { 1827 preallocated->queue_link = _dbus_list_alloc_link (NULL); 1828 if (preallocated->queue_link == NULL) 1829 goto failed_0; 1830 } 1831 1832 if (connection->link_cache != NULL) 1833 { 1834 preallocated->counter_link = 1835 _dbus_list_pop_first_link (&connection->link_cache); 1836 preallocated->counter_link->data = connection->outgoing_counter; 1837 } 1838 else 1839 { 1840 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter); 1841 if (preallocated->counter_link == NULL) 1842 goto failed_1; 1843 } 1844 1845 _dbus_counter_ref (preallocated->counter_link->data); 1846 1847 preallocated->connection = connection; 1848 1849 return preallocated; 1850 1851 failed_1: 1852 _dbus_list_free_link (preallocated->queue_link); 1853 failed_0: 1854 dbus_free (preallocated); 1855 1856 return NULL; 1857} 1858 1859/* Called with lock held, does not update dispatch status */ 1860static void 1861_dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection, 1862 DBusPreallocatedSend *preallocated, 1863 DBusMessage *message, 1864 dbus_uint32_t *client_serial) 1865{ 1866 dbus_uint32_t serial; 1867 const char *sig; 1868 1869 preallocated->queue_link->data = message; 1870 _dbus_list_prepend_link (&connection->outgoing_messages, 1871 preallocated->queue_link); 1872 1873 _dbus_message_add_size_counter_link (message, 1874 preallocated->counter_link); 1875 1876 dbus_free (preallocated); 1877 preallocated = NULL; 1878 1879 dbus_message_ref (message); 1880 1881 connection->n_outgoing += 1; 1882 1883 sig = dbus_message_get_signature (message); 1884 1885 _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n", 1886 message, 1887 dbus_message_get_type (message), 1888 dbus_message_get_path (message) ? 1889 dbus_message_get_path (message) : 1890 "no path", 1891 dbus_message_get_interface (message) ? 1892 dbus_message_get_interface (message) : 1893 "no interface", 1894 dbus_message_get_member (message) ? 1895 dbus_message_get_member (message) : 1896 "no member", 1897 sig, 1898 dbus_message_get_destination (message) ? 1899 dbus_message_get_destination (message) : 1900 "null", 1901 connection, 1902 connection->n_outgoing); 1903 1904 if (dbus_message_get_serial (message) == 0) 1905 { 1906 serial = _dbus_connection_get_next_client_serial (connection); 1907 _dbus_message_set_serial (message, serial); 1908 if (client_serial) 1909 *client_serial = serial; 1910 } 1911 else 1912 { 1913 if (client_serial) 1914 *client_serial = dbus_message_get_serial (message); 1915 } 1916 1917 _dbus_verbose ("Message %p serial is %u\n", 1918 message, dbus_message_get_serial (message)); 1919 1920 _dbus_message_lock (message); 1921 1922 /* Now we need to run an iteration to hopefully just write the messages 1923 * out immediately, and otherwise get them queued up 1924 */ 1925 _dbus_connection_do_iteration_unlocked (connection, 1926 DBUS_ITERATION_DO_WRITING, 1927 -1); 1928 1929 /* If stuff is still queued up, be sure we wake up the main loop */ 1930 if (connection->n_outgoing > 0) 1931 _dbus_connection_wakeup_mainloop (connection); 1932} 1933 1934static void 1935_dbus_connection_send_preallocated_and_unlock (DBusConnection *connection, 1936 DBusPreallocatedSend *preallocated, 1937 DBusMessage *message, 1938 dbus_uint32_t *client_serial) 1939{ 1940 DBusDispatchStatus status; 1941 1942 HAVE_LOCK_CHECK (connection); 1943 1944 _dbus_connection_send_preallocated_unlocked_no_update (connection, 1945 preallocated, 1946 message, client_serial); 1947 1948 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME); 1949 status = _dbus_connection_get_dispatch_status_unlocked (connection); 1950 1951 /* this calls out to user code */ 1952 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 1953} 1954 1955/** 1956 * Like dbus_connection_send(), but assumes the connection 1957 * is already locked on function entry, and unlocks before returning. 1958 * 1959 * @param connection the connection 1960 * @param message the message to send 1961 * @param client_serial return location for client serial of sent message 1962 * @returns #FALSE on out-of-memory 1963 */ 1964dbus_bool_t 1965_dbus_connection_send_and_unlock (DBusConnection *connection, 1966 DBusMessage *message, 1967 dbus_uint32_t *client_serial) 1968{ 1969 DBusPreallocatedSend *preallocated; 1970 1971 _dbus_assert (connection != NULL); 1972 _dbus_assert (message != NULL); 1973 1974 preallocated = _dbus_connection_preallocate_send_unlocked (connection); 1975 if (preallocated == NULL) 1976 { 1977 CONNECTION_UNLOCK (connection); 1978 return FALSE; 1979 } 1980 1981 _dbus_connection_send_preallocated_and_unlock (connection, 1982 preallocated, 1983 message, 1984 client_serial); 1985 return TRUE; 1986} 1987 1988/** 1989 * Used internally to handle the semantics of dbus_server_set_new_connection_function(). 1990 * If the new connection function does not ref the connection, we want to close it. 1991 * 1992 * A bit of a hack, probably the new connection function should have returned a value 1993 * for whether to close, or should have had to close the connection itself if it 1994 * didn't want it. 1995 * 1996 * But, this works OK as long as the new connection function doesn't do anything 1997 * crazy like keep the connection around without ref'ing it. 1998 * 1999 * We have to lock the connection across refcount check and close in case 2000 * the new connection function spawns a thread that closes and unrefs. 2001 * In that case, if the app thread 2002 * closes and unrefs first, we'll harmlessly close again; if the app thread 2003 * still has the ref, we'll close and then the app will close harmlessly. 2004 * If the app unrefs without closing, the app is broken since if the 2005 * app refs from the new connection function it is supposed to also close. 2006 * 2007 * If we didn't atomically check the refcount and close with the lock held 2008 * though, we could screw this up. 2009 * 2010 * @param connection the connection 2011 */ 2012void 2013_dbus_connection_close_if_only_one_ref (DBusConnection *connection) 2014{ 2015 CONNECTION_LOCK (connection); 2016 2017 _dbus_assert (connection->refcount.value > 0); 2018 2019 if (connection->refcount.value == 1) 2020 _dbus_connection_close_possibly_shared_and_unlock (connection); 2021 else 2022 CONNECTION_UNLOCK (connection); 2023} 2024 2025 2026/** 2027 * When a function that blocks has been called with a timeout, and we 2028 * run out of memory, the time to wait for memory is based on the 2029 * timeout. If the caller was willing to block a long time we wait a 2030 * relatively long time for memory, if they were only willing to block 2031 * briefly then we retry for memory at a rapid rate. 2032 * 2033 * @timeout_milliseconds the timeout requested for blocking 2034 */ 2035static void 2036_dbus_memory_pause_based_on_timeout (int timeout_milliseconds) 2037{ 2038 if (timeout_milliseconds == -1) 2039 _dbus_sleep_milliseconds (1000); 2040 else if (timeout_milliseconds < 100) 2041 ; /* just busy loop */ 2042 else if (timeout_milliseconds <= 1000) 2043 _dbus_sleep_milliseconds (timeout_milliseconds / 3); 2044 else 2045 _dbus_sleep_milliseconds (1000); 2046} 2047 2048static DBusMessage * 2049generate_local_error_message (dbus_uint32_t serial, 2050 char *error_name, 2051 char *error_msg) 2052{ 2053 DBusMessage *message; 2054 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR); 2055 if (!message) 2056 goto out; 2057 2058 if (!dbus_message_set_error_name (message, error_name)) 2059 { 2060 dbus_message_unref (message); 2061 message = NULL; 2062 goto out; 2063 } 2064 2065 dbus_message_set_no_reply (message, TRUE); 2066 2067 if (!dbus_message_set_reply_serial (message, 2068 serial)) 2069 { 2070 dbus_message_unref (message); 2071 message = NULL; 2072 goto out; 2073 } 2074 2075 if (error_msg != NULL) 2076 { 2077 DBusMessageIter iter; 2078 2079 dbus_message_iter_init_append (message, &iter); 2080 if (!dbus_message_iter_append_basic (&iter, 2081 DBUS_TYPE_STRING, 2082 &error_msg)) 2083 { 2084 dbus_message_unref (message); 2085 message = NULL; 2086 goto out; 2087 } 2088 } 2089 2090 out: 2091 return message; 2092} 2093 2094 2095/* This is slightly strange since we can pop a message here without 2096 * the dispatch lock. 2097 */ 2098static DBusMessage* 2099check_for_reply_unlocked (DBusConnection *connection, 2100 dbus_uint32_t client_serial) 2101{ 2102 DBusList *link; 2103 2104 HAVE_LOCK_CHECK (connection); 2105 2106 link = _dbus_list_get_first_link (&connection->incoming_messages); 2107 2108 while (link != NULL) 2109 { 2110 DBusMessage *reply = link->data; 2111 2112 if (dbus_message_get_reply_serial (reply) == client_serial) 2113 { 2114 _dbus_list_remove_link (&connection->incoming_messages, link); 2115 connection->n_incoming -= 1; 2116 return reply; 2117 } 2118 link = _dbus_list_get_next_link (&connection->incoming_messages, link); 2119 } 2120 2121 return NULL; 2122} 2123 2124static void 2125connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection) 2126{ 2127 /* We can't iterate over the hash in the normal way since we'll be 2128 * dropping the lock for each item. So we restart the 2129 * iter each time as we drain the hash table. 2130 */ 2131 2132 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0) 2133 { 2134 DBusPendingCall *pending; 2135 DBusHashIter iter; 2136 2137 _dbus_hash_iter_init (connection->pending_replies, &iter); 2138 _dbus_hash_iter_next (&iter); 2139 2140 pending = _dbus_hash_iter_get_value (&iter); 2141 _dbus_pending_call_ref_unlocked (pending); 2142 2143 _dbus_pending_call_queue_timeout_error_unlocked (pending, 2144 connection); 2145 _dbus_connection_remove_timeout_unlocked (connection, 2146 _dbus_pending_call_get_timeout_unlocked (pending)); 2147 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE); 2148 _dbus_hash_iter_remove_entry (&iter); 2149 2150 _dbus_pending_call_unref_and_unlock (pending); 2151 CONNECTION_LOCK (connection); 2152 } 2153 HAVE_LOCK_CHECK (connection); 2154} 2155 2156static void 2157complete_pending_call_and_unlock (DBusConnection *connection, 2158 DBusPendingCall *pending, 2159 DBusMessage *message) 2160{ 2161 _dbus_pending_call_set_reply_unlocked (pending, message); 2162 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */ 2163 _dbus_connection_detach_pending_call_and_unlock (connection, pending); 2164 2165 /* Must be called unlocked since it invokes app callback */ 2166 _dbus_pending_call_complete (pending); 2167 dbus_pending_call_unref (pending); 2168} 2169 2170static dbus_bool_t 2171check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection, 2172 DBusPendingCall *pending) 2173{ 2174 DBusMessage *reply; 2175 DBusDispatchStatus status; 2176 2177 reply = check_for_reply_unlocked (connection, 2178 _dbus_pending_call_get_reply_serial_unlocked (pending)); 2179 if (reply != NULL) 2180 { 2181 _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME); 2182 2183 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n"); 2184 2185 complete_pending_call_and_unlock (connection, pending, reply); 2186 dbus_message_unref (reply); 2187 2188 CONNECTION_LOCK (connection); 2189 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2190 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2191 dbus_pending_call_unref (pending); 2192 2193 return TRUE; 2194 } 2195 2196 return FALSE; 2197} 2198 2199/** 2200 * Blocks until a pending call times out or gets a reply. 2201 * 2202 * Does not re-enter the main loop or run filter/path-registered 2203 * callbacks. The reply to the message will not be seen by 2204 * filter callbacks. 2205 * 2206 * Returns immediately if pending call already got a reply. 2207 * 2208 * @todo could use performance improvements (it keeps scanning 2209 * the whole message queue for example) 2210 * 2211 * @param pending the pending call we block for a reply on 2212 */ 2213void 2214_dbus_connection_block_pending_call (DBusPendingCall *pending) 2215{ 2216 long start_tv_sec, start_tv_usec; 2217 long end_tv_sec, end_tv_usec; 2218 long tv_sec, tv_usec; 2219 DBusDispatchStatus status; 2220 DBusConnection *connection; 2221 dbus_uint32_t client_serial; 2222 int timeout_milliseconds; 2223 2224 _dbus_assert (pending != NULL); 2225 2226 if (dbus_pending_call_get_completed (pending)) 2227 return; 2228 2229 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */ 2230 2231 connection = _dbus_pending_call_get_connection_and_lock (pending); 2232 2233 /* Flush message queue - note, can affect dispatch status */ 2234 _dbus_connection_flush_unlocked (connection); 2235 2236 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending); 2237 2238 /* note that timeout_milliseconds is limited to a smallish value 2239 * in _dbus_pending_call_new() so overflows aren't possible 2240 * below 2241 */ 2242 timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending)); 2243 2244 _dbus_get_current_time (&start_tv_sec, &start_tv_usec); 2245 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000; 2246 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000; 2247 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND; 2248 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND; 2249 2250 _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", 2251 timeout_milliseconds, 2252 client_serial, 2253 start_tv_sec, start_tv_usec, 2254 end_tv_sec, end_tv_usec); 2255 2256 /* check to see if we already got the data off the socket */ 2257 /* from another blocked pending call */ 2258 if (check_for_reply_and_update_dispatch_unlocked (connection, pending)) 2259 return; 2260 2261 /* Now we wait... */ 2262 /* always block at least once as we know we don't have the reply yet */ 2263 _dbus_connection_do_iteration_unlocked (connection, 2264 DBUS_ITERATION_DO_READING | 2265 DBUS_ITERATION_BLOCK, 2266 timeout_milliseconds); 2267 2268 recheck_status: 2269 2270 _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME); 2271 2272 HAVE_LOCK_CHECK (connection); 2273 2274 /* queue messages and get status */ 2275 2276 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2277 2278 /* the get_completed() is in case a dispatch() while we were blocking 2279 * got the reply instead of us. 2280 */ 2281 if (_dbus_pending_call_get_completed_unlocked (pending)) 2282 { 2283 _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME); 2284 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2285 dbus_pending_call_unref (pending); 2286 return; 2287 } 2288 2289 if (status == DBUS_DISPATCH_DATA_REMAINS) { 2290 if (check_for_reply_and_update_dispatch_unlocked (connection, pending)) 2291 return; 2292 } 2293 2294 _dbus_get_current_time (&tv_sec, &tv_usec); 2295 2296 if (!_dbus_connection_get_is_connected_unlocked (connection)) 2297 { 2298 DBusMessage *error_msg; 2299 2300 error_msg = generate_local_error_message (client_serial, 2301 DBUS_ERROR_DISCONNECTED, 2302 "Connection was disconnected before a reply was received"); 2303 2304 /* on OOM error_msg is set to NULL */ 2305 complete_pending_call_and_unlock (connection, pending, error_msg); 2306 dbus_pending_call_unref (pending); 2307 return; 2308 } 2309 else if (tv_sec < start_tv_sec) 2310 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n"); 2311 else if (connection->disconnect_message_link == NULL) 2312 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n"); 2313 else if (tv_sec < end_tv_sec || 2314 (tv_sec == end_tv_sec && tv_usec < end_tv_usec)) 2315 { 2316 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 + 2317 (end_tv_usec - tv_usec) / 1000; 2318 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds); 2319 _dbus_assert (timeout_milliseconds >= 0); 2320 2321 if (status == DBUS_DISPATCH_NEED_MEMORY) 2322 { 2323 /* Try sleeping a bit, as we aren't sure we need to block for reading, 2324 * we may already have a reply in the buffer and just can't process 2325 * it. 2326 */ 2327 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n"); 2328 2329 _dbus_memory_pause_based_on_timeout (timeout_milliseconds); 2330 } 2331 else 2332 { 2333 /* block again, we don't have the reply buffered yet. */ 2334 _dbus_connection_do_iteration_unlocked (connection, 2335 DBUS_ITERATION_DO_READING | 2336 DBUS_ITERATION_BLOCK, 2337 timeout_milliseconds); 2338 } 2339 2340 goto recheck_status; 2341 } 2342 2343 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n", 2344 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000); 2345 2346 _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending)); 2347 2348 /* unlock and call user code */ 2349 complete_pending_call_and_unlock (connection, pending, NULL); 2350 2351 /* update user code on dispatch status */ 2352 CONNECTION_LOCK (connection); 2353 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2354 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2355 dbus_pending_call_unref (pending); 2356} 2357 2358/** @} */ 2359 2360/** 2361 * @addtogroup DBusConnection 2362 * 2363 * @{ 2364 */ 2365 2366/** 2367 * Gets a connection to a remote address. If a connection to the given 2368 * address already exists, returns the existing connection with its 2369 * reference count incremented. Otherwise, returns a new connection 2370 * and saves the new connection for possible re-use if a future call 2371 * to dbus_connection_open() asks to connect to the same server. 2372 * 2373 * Use dbus_connection_open_private() to get a dedicated connection 2374 * not shared with other callers of dbus_connection_open(). 2375 * 2376 * If the open fails, the function returns #NULL, and provides a 2377 * reason for the failure in the error parameter. Pass #NULL for the 2378 * error parameter if you aren't interested in the reason for 2379 * failure. 2380 * 2381 * Because this connection is shared, no user of the connection 2382 * may call dbus_connection_close(). However, when you are done with the 2383 * connection you should call dbus_connection_unref(). 2384 * 2385 * @param address the address. 2386 * @param error address where an error can be returned. 2387 * @returns new connection, or #NULL on failure. 2388 */ 2389DBusConnection* 2390dbus_connection_open (const char *address, 2391 DBusError *error) 2392{ 2393 DBusConnection *connection; 2394 2395 _dbus_return_val_if_fail (address != NULL, NULL); 2396 _dbus_return_val_if_error_is_set (error, NULL); 2397 2398 connection = _dbus_connection_open_internal (address, 2399 TRUE, 2400 error); 2401 2402 return connection; 2403} 2404 2405/** 2406 * Opens a new, dedicated connection to a remote address. Unlike 2407 * dbus_connection_open(), always creates a new connection. 2408 * This connection will not be saved or recycled by libdbus. 2409 * 2410 * If the open fails, the function returns #NULL, and provides a 2411 * reason for the failure in the error parameter. Pass #NULL for the 2412 * error parameter if you aren't interested in the reason for 2413 * failure. 2414 * 2415 * When you are done with this connection, you must 2416 * dbus_connection_close() to disconnect it, 2417 * and dbus_connection_unref() to free the connection object. 2418 * 2419 * (The dbus_connection_close() can be skipped if the 2420 * connection is already known to be disconnected, for example 2421 * if you are inside a handler for the Disconnected signal.) 2422 * 2423 * @param address the address. 2424 * @param error address where an error can be returned. 2425 * @returns new connection, or #NULL on failure. 2426 */ 2427DBusConnection* 2428dbus_connection_open_private (const char *address, 2429 DBusError *error) 2430{ 2431 DBusConnection *connection; 2432 2433 _dbus_return_val_if_fail (address != NULL, NULL); 2434 _dbus_return_val_if_error_is_set (error, NULL); 2435 2436 connection = _dbus_connection_open_internal (address, 2437 FALSE, 2438 error); 2439 2440 return connection; 2441} 2442 2443/** 2444 * Increments the reference count of a DBusConnection. 2445 * 2446 * @param connection the connection. 2447 * @returns the connection. 2448 */ 2449DBusConnection * 2450dbus_connection_ref (DBusConnection *connection) 2451{ 2452 _dbus_return_val_if_fail (connection != NULL, NULL); 2453 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL); 2454 2455 /* The connection lock is better than the global 2456 * lock in the atomic increment fallback 2457 */ 2458 2459#ifdef DBUS_HAVE_ATOMIC_INT 2460 _dbus_atomic_inc (&connection->refcount); 2461#else 2462 CONNECTION_LOCK (connection); 2463 _dbus_assert (connection->refcount.value > 0); 2464 2465 connection->refcount.value += 1; 2466 CONNECTION_UNLOCK (connection); 2467#endif 2468 2469 return connection; 2470} 2471 2472static void 2473free_outgoing_message (void *element, 2474 void *data) 2475{ 2476 DBusMessage *message = element; 2477 DBusConnection *connection = data; 2478 2479 _dbus_message_remove_size_counter (message, 2480 connection->outgoing_counter, 2481 NULL); 2482 dbus_message_unref (message); 2483} 2484 2485/* This is run without the mutex held, but after the last reference 2486 * to the connection has been dropped we should have no thread-related 2487 * problems 2488 */ 2489static void 2490_dbus_connection_last_unref (DBusConnection *connection) 2491{ 2492 DBusList *link; 2493 2494 _dbus_verbose ("Finalizing connection %p\n", connection); 2495 2496 _dbus_assert (connection->refcount.value == 0); 2497 2498 /* You have to disconnect the connection before unref:ing it. Otherwise 2499 * you won't get the disconnected message. 2500 */ 2501 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport)); 2502 _dbus_assert (connection->server_guid == NULL); 2503 2504 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */ 2505 _dbus_object_tree_free_all_unlocked (connection->objects); 2506 2507 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL); 2508 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL); 2509 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL); 2510 2511 _dbus_watch_list_free (connection->watches); 2512 connection->watches = NULL; 2513 2514 _dbus_timeout_list_free (connection->timeouts); 2515 connection->timeouts = NULL; 2516 2517 _dbus_data_slot_list_free (&connection->slot_list); 2518 2519 link = _dbus_list_get_first_link (&connection->filter_list); 2520 while (link != NULL) 2521 { 2522 DBusMessageFilter *filter = link->data; 2523 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link); 2524 2525 filter->function = NULL; 2526 _dbus_message_filter_unref (filter); /* calls app callback */ 2527 link->data = NULL; 2528 2529 link = next; 2530 } 2531 _dbus_list_clear (&connection->filter_list); 2532 2533 /* ---- Done with stuff that invokes application callbacks */ 2534 2535 _dbus_object_tree_unref (connection->objects); 2536 2537 _dbus_hash_table_unref (connection->pending_replies); 2538 connection->pending_replies = NULL; 2539 2540 _dbus_list_clear (&connection->filter_list); 2541 2542 _dbus_list_foreach (&connection->outgoing_messages, 2543 free_outgoing_message, 2544 connection); 2545 _dbus_list_clear (&connection->outgoing_messages); 2546 2547 _dbus_list_foreach (&connection->incoming_messages, 2548 (DBusForeachFunction) dbus_message_unref, 2549 NULL); 2550 _dbus_list_clear (&connection->incoming_messages); 2551 2552 _dbus_counter_unref (connection->outgoing_counter); 2553 2554 _dbus_transport_unref (connection->transport); 2555 2556 if (connection->disconnect_message_link) 2557 { 2558 DBusMessage *message = connection->disconnect_message_link->data; 2559 dbus_message_unref (message); 2560 _dbus_list_free_link (connection->disconnect_message_link); 2561 } 2562 2563 _dbus_list_clear (&connection->link_cache); 2564 2565 _dbus_condvar_free_at_location (&connection->dispatch_cond); 2566 _dbus_condvar_free_at_location (&connection->io_path_cond); 2567 2568 _dbus_mutex_free_at_location (&connection->io_path_mutex); 2569 _dbus_mutex_free_at_location (&connection->dispatch_mutex); 2570 2571 _dbus_mutex_free_at_location (&connection->mutex); 2572 2573 dbus_free (connection); 2574} 2575 2576/** 2577 * Decrements the reference count of a DBusConnection, and finalizes 2578 * it if the count reaches zero. 2579 * 2580 * Note: it is a bug to drop the last reference to a connection that 2581 * is still connected. 2582 * 2583 * For shared connections, libdbus will own a reference 2584 * as long as the connection is connected, so you can know that either 2585 * you don't have the last reference, or it's OK to drop the last reference. 2586 * Most connections are shared. dbus_connection_open() and dbus_bus_get() 2587 * return shared connections. 2588 * 2589 * For private connections, the creator of the connection must arrange for 2590 * dbus_connection_close() to be called prior to dropping the last reference. 2591 * Private connections come from dbus_connection_open_private() or dbus_bus_get_private(). 2592 * 2593 * @param connection the connection. 2594 */ 2595void 2596dbus_connection_unref (DBusConnection *connection) 2597{ 2598 dbus_bool_t last_unref; 2599 2600 _dbus_return_if_fail (connection != NULL); 2601 _dbus_return_if_fail (connection->generation == _dbus_current_generation); 2602 2603 /* The connection lock is better than the global 2604 * lock in the atomic increment fallback 2605 */ 2606 2607#ifdef DBUS_HAVE_ATOMIC_INT 2608 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1); 2609#else 2610 CONNECTION_LOCK (connection); 2611 2612 _dbus_assert (connection->refcount.value > 0); 2613 2614 connection->refcount.value -= 1; 2615 last_unref = (connection->refcount.value == 0); 2616 2617#if 0 2618 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value); 2619#endif 2620 2621 CONNECTION_UNLOCK (connection); 2622#endif 2623 2624 if (last_unref) 2625 { 2626#ifndef DBUS_DISABLE_CHECKS 2627 if (_dbus_transport_get_is_connected (connection->transport)) 2628 { 2629 _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s", 2630 connection->shareable ? 2631 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" : 2632 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n"); 2633 return; 2634 } 2635#endif 2636 _dbus_connection_last_unref (connection); 2637 } 2638} 2639 2640/* 2641 * Note that the transport can disconnect itself (other end drops us) 2642 * and in that case this function never runs. So this function must 2643 * not do anything more than disconnect the transport and update the 2644 * dispatch status. 2645 * 2646 * If the transport self-disconnects, then we assume someone will 2647 * dispatch the connection to cause the dispatch status update. 2648 */ 2649static void 2650_dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection) 2651{ 2652 DBusDispatchStatus status; 2653 2654 HAVE_LOCK_CHECK (connection); 2655 2656 _dbus_verbose ("Disconnecting %p\n", connection); 2657 2658 /* We need to ref because update_dispatch_status_and_unlock will unref 2659 * the connection if it was shared and libdbus was the only remaining 2660 * refcount holder. 2661 */ 2662 _dbus_connection_ref_unlocked (connection); 2663 2664 _dbus_transport_disconnect (connection->transport); 2665 2666 /* This has the side effect of queuing the disconnect message link 2667 * (unless we don't have enough memory, possibly, so don't assert it). 2668 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open 2669 * should never again return the newly-disconnected connection. 2670 * 2671 * However, we only unref the shared connection and exit_on_disconnect when 2672 * the disconnect message reaches the head of the message queue, 2673 * NOT when it's first queued. 2674 */ 2675 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2676 2677 /* This calls out to user code */ 2678 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2679 2680 /* Could also call out to user code */ 2681 dbus_connection_unref (connection); 2682} 2683 2684/** 2685 * Closes a private connection, so no further data can be sent or received. 2686 * This disconnects the transport (such as a socket) underlying the 2687 * connection. 2688 * 2689 * Attempts to send messages after closing a connection are safe, but will result in 2690 * error replies generated locally in libdbus. 2691 * 2692 * This function does not affect the connection's reference count. It's 2693 * safe to close a connection more than once; all calls after the 2694 * first do nothing. It's impossible to "reopen" a connection, a 2695 * new connection must be created. This function may result in a call 2696 * to the DBusDispatchStatusFunction set with 2697 * dbus_connection_set_dispatch_status_function(), as the disconnect 2698 * message it generates needs to be dispatched. 2699 * 2700 * If a connection is dropped by the remote application, it will 2701 * close itself. 2702 * 2703 * You must close a connection prior to releasing the last reference to 2704 * the connection. If you dbus_connection_unref() for the last time 2705 * without closing the connection, the results are undefined; it 2706 * is a bug in your program and libdbus will try to print a warning. 2707 * 2708 * You may not close a shared connection. Connections created with 2709 * dbus_connection_open() or dbus_bus_get() are shared. 2710 * These connections are owned by libdbus, and applications should 2711 * only unref them, never close them. Applications can know it is 2712 * safe to unref these connections because libdbus will be holding a 2713 * reference as long as the connection is open. Thus, either the 2714 * connection is closed and it is OK to drop the last reference, 2715 * or the connection is open and the app knows it does not have the 2716 * last reference. 2717 * 2718 * Connections created with dbus_connection_open_private() or 2719 * dbus_bus_get_private() are not kept track of or referenced by 2720 * libdbus. The creator of these connections is responsible for 2721 * calling dbus_connection_close() prior to releasing the last 2722 * reference, if the connection is not already disconnected. 2723 * 2724 * @param connection the private (unshared) connection to close 2725 */ 2726void 2727dbus_connection_close (DBusConnection *connection) 2728{ 2729 _dbus_return_if_fail (connection != NULL); 2730 _dbus_return_if_fail (connection->generation == _dbus_current_generation); 2731 2732 CONNECTION_LOCK (connection); 2733 2734#ifndef DBUS_DISABLE_CHECKS 2735 if (connection->shareable) 2736 { 2737 CONNECTION_UNLOCK (connection); 2738 2739 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n"); 2740 return; 2741 } 2742#endif 2743 2744 _dbus_connection_close_possibly_shared_and_unlock (connection); 2745} 2746 2747static dbus_bool_t 2748_dbus_connection_get_is_connected_unlocked (DBusConnection *connection) 2749{ 2750 HAVE_LOCK_CHECK (connection); 2751 return _dbus_transport_get_is_connected (connection->transport); 2752} 2753 2754/** 2755 * Gets whether the connection is currently open. A connection may 2756 * become disconnected when the remote application closes its end, or 2757 * exits; a connection may also be disconnected with 2758 * dbus_connection_close(). 2759 * 2760 * There are not separate states for "closed" and "disconnected," the two 2761 * terms are synonymous. This function should really be called 2762 * get_is_open() but for historical reasons is not. 2763 * 2764 * @param connection the connection. 2765 * @returns #TRUE if the connection is still alive. 2766 */ 2767dbus_bool_t 2768dbus_connection_get_is_connected (DBusConnection *connection) 2769{ 2770 dbus_bool_t res; 2771 2772 _dbus_return_val_if_fail (connection != NULL, FALSE); 2773 2774 CONNECTION_LOCK (connection); 2775 res = _dbus_connection_get_is_connected_unlocked (connection); 2776 CONNECTION_UNLOCK (connection); 2777 2778 return res; 2779} 2780 2781/** 2782 * Gets whether the connection was authenticated. (Note that 2783 * if the connection was authenticated then disconnected, 2784 * this function still returns #TRUE) 2785 * 2786 * @param connection the connection 2787 * @returns #TRUE if the connection was ever authenticated 2788 */ 2789dbus_bool_t 2790dbus_connection_get_is_authenticated (DBusConnection *connection) 2791{ 2792 dbus_bool_t res; 2793 2794 _dbus_return_val_if_fail (connection != NULL, FALSE); 2795 2796 CONNECTION_LOCK (connection); 2797 res = _dbus_transport_get_is_authenticated (connection->transport); 2798 CONNECTION_UNLOCK (connection); 2799 2800 return res; 2801} 2802 2803/** 2804 * Set whether _exit() should be called when the connection receives a 2805 * disconnect signal. The call to _exit() comes after any handlers for 2806 * the disconnect signal run; handlers can cancel the exit by calling 2807 * this function. 2808 * 2809 * By default, exit_on_disconnect is #FALSE; but for message bus 2810 * connections returned from dbus_bus_get() it will be toggled on 2811 * by default. 2812 * 2813 * @param connection the connection 2814 * @param exit_on_disconnect #TRUE if _exit() should be called after a disconnect signal 2815 */ 2816void 2817dbus_connection_set_exit_on_disconnect (DBusConnection *connection, 2818 dbus_bool_t exit_on_disconnect) 2819{ 2820 _dbus_return_if_fail (connection != NULL); 2821 2822 CONNECTION_LOCK (connection); 2823 connection->exit_on_disconnect = exit_on_disconnect != FALSE; 2824 CONNECTION_UNLOCK (connection); 2825} 2826 2827/** 2828 * Preallocates resources needed to send a message, allowing the message 2829 * to be sent without the possibility of memory allocation failure. 2830 * Allows apps to create a future guarantee that they can send 2831 * a message regardless of memory shortages. 2832 * 2833 * @param connection the connection we're preallocating for. 2834 * @returns the preallocated resources, or #NULL 2835 */ 2836DBusPreallocatedSend* 2837dbus_connection_preallocate_send (DBusConnection *connection) 2838{ 2839 DBusPreallocatedSend *preallocated; 2840 2841 _dbus_return_val_if_fail (connection != NULL, NULL); 2842 2843 CONNECTION_LOCK (connection); 2844 2845 preallocated = 2846 _dbus_connection_preallocate_send_unlocked (connection); 2847 2848 CONNECTION_UNLOCK (connection); 2849 2850 return preallocated; 2851} 2852 2853/** 2854 * Frees preallocated message-sending resources from 2855 * dbus_connection_preallocate_send(). Should only 2856 * be called if the preallocated resources are not used 2857 * to send a message. 2858 * 2859 * @param connection the connection 2860 * @param preallocated the resources 2861 */ 2862void 2863dbus_connection_free_preallocated_send (DBusConnection *connection, 2864 DBusPreallocatedSend *preallocated) 2865{ 2866 _dbus_return_if_fail (connection != NULL); 2867 _dbus_return_if_fail (preallocated != NULL); 2868 _dbus_return_if_fail (connection == preallocated->connection); 2869 2870 _dbus_list_free_link (preallocated->queue_link); 2871 _dbus_counter_unref (preallocated->counter_link->data); 2872 _dbus_list_free_link (preallocated->counter_link); 2873 dbus_free (preallocated); 2874} 2875 2876/** 2877 * Sends a message using preallocated resources. This function cannot fail. 2878 * It works identically to dbus_connection_send() in other respects. 2879 * Preallocated resources comes from dbus_connection_preallocate_send(). 2880 * This function "consumes" the preallocated resources, they need not 2881 * be freed separately. 2882 * 2883 * @param connection the connection 2884 * @param preallocated the preallocated resources 2885 * @param message the message to send 2886 * @param client_serial return location for client serial assigned to the message 2887 */ 2888void 2889dbus_connection_send_preallocated (DBusConnection *connection, 2890 DBusPreallocatedSend *preallocated, 2891 DBusMessage *message, 2892 dbus_uint32_t *client_serial) 2893{ 2894 _dbus_return_if_fail (connection != NULL); 2895 _dbus_return_if_fail (preallocated != NULL); 2896 _dbus_return_if_fail (message != NULL); 2897 _dbus_return_if_fail (preallocated->connection == connection); 2898 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL || 2899 dbus_message_get_member (message) != NULL); 2900 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL || 2901 (dbus_message_get_interface (message) != NULL && 2902 dbus_message_get_member (message) != NULL)); 2903 2904 CONNECTION_LOCK (connection); 2905 _dbus_connection_send_preallocated_and_unlock (connection, 2906 preallocated, 2907 message, client_serial); 2908} 2909 2910static dbus_bool_t 2911_dbus_connection_send_unlocked_no_update (DBusConnection *connection, 2912 DBusMessage *message, 2913 dbus_uint32_t *client_serial) 2914{ 2915 DBusPreallocatedSend *preallocated; 2916 2917 _dbus_assert (connection != NULL); 2918 _dbus_assert (message != NULL); 2919 2920 preallocated = _dbus_connection_preallocate_send_unlocked (connection); 2921 if (preallocated == NULL) 2922 return FALSE; 2923 2924 _dbus_connection_send_preallocated_unlocked_no_update (connection, 2925 preallocated, 2926 message, 2927 client_serial); 2928 return TRUE; 2929} 2930 2931/** 2932 * Adds a message to the outgoing message queue. Does not block to 2933 * write the message to the network; that happens asynchronously. To 2934 * force the message to be written, call dbus_connection_flush(). 2935 * Because this only queues the message, the only reason it can 2936 * fail is lack of memory. Even if the connection is disconnected, 2937 * no error will be returned. 2938 * 2939 * If the function fails due to lack of memory, it returns #FALSE. 2940 * The function will never fail for other reasons; even if the 2941 * connection is disconnected, you can queue an outgoing message, 2942 * though obviously it won't be sent. 2943 * 2944 * @param connection the connection. 2945 * @param message the message to write. 2946 * @param client_serial return location for client serial. 2947 * @returns #TRUE on success. 2948 */ 2949dbus_bool_t 2950dbus_connection_send (DBusConnection *connection, 2951 DBusMessage *message, 2952 dbus_uint32_t *client_serial) 2953{ 2954 _dbus_return_val_if_fail (connection != NULL, FALSE); 2955 _dbus_return_val_if_fail (message != NULL, FALSE); 2956 2957 CONNECTION_LOCK (connection); 2958 2959 return _dbus_connection_send_and_unlock (connection, 2960 message, 2961 client_serial); 2962} 2963 2964static dbus_bool_t 2965reply_handler_timeout (void *data) 2966{ 2967 DBusConnection *connection; 2968 DBusDispatchStatus status; 2969 DBusPendingCall *pending = data; 2970 2971 connection = _dbus_pending_call_get_connection_and_lock (pending); 2972 2973 _dbus_pending_call_queue_timeout_error_unlocked (pending, 2974 connection); 2975 _dbus_connection_remove_timeout_unlocked (connection, 2976 _dbus_pending_call_get_timeout_unlocked (pending)); 2977 _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE); 2978 2979 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME); 2980 status = _dbus_connection_get_dispatch_status_unlocked (connection); 2981 2982 /* Unlocks, and calls out to user code */ 2983 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 2984 2985 return TRUE; 2986} 2987 2988/** 2989 * Queues a message to send, as with dbus_connection_send_message(), 2990 * but also returns a #DBusPendingCall used to receive a reply to the 2991 * message. If no reply is received in the given timeout_milliseconds, 2992 * this function expires the pending reply and generates a synthetic 2993 * error reply (generated in-process, not by the remote application) 2994 * indicating that a timeout occurred. 2995 * 2996 * A #DBusPendingCall will see a reply message after any filters, but 2997 * before any object instances or other handlers. A #DBusPendingCall 2998 * will always see exactly one reply message, unless it's cancelled 2999 * with dbus_pending_call_cancel(). 3000 * 3001 * If a filter filters out the reply before the handler sees it, the 3002 * reply is immediately timed out and a timeout error reply is 3003 * generated. If a filter removes the timeout error reply then the 3004 * #DBusPendingCall will get confused. Filtering the timeout error 3005 * is thus considered a bug and will print a warning. 3006 * 3007 * If #NULL is passed for the pending_return, the #DBusPendingCall 3008 * will still be generated internally, and used to track 3009 * the message reply timeout. This means a timeout error will 3010 * occur if no reply arrives, unlike with dbus_connection_send(). 3011 * 3012 * If -1 is passed for the timeout, a sane default timeout is used. -1 3013 * is typically the best value for the timeout for this reason, unless 3014 * you want a very short or very long timeout. There is no way to 3015 * avoid a timeout entirely, other than passing INT_MAX for the 3016 * timeout to postpone it indefinitely. 3017 * 3018 * @param connection the connection 3019 * @param message the message to send 3020 * @param pending_return return location for a #DBusPendingCall object, or #NULL if connection is disconnected 3021 * @param timeout_milliseconds timeout in milliseconds or -1 for default 3022 * @returns #FALSE if no memory, #TRUE otherwise. 3023 * 3024 */ 3025dbus_bool_t 3026dbus_connection_send_with_reply (DBusConnection *connection, 3027 DBusMessage *message, 3028 DBusPendingCall **pending_return, 3029 int timeout_milliseconds) 3030{ 3031 DBusPendingCall *pending; 3032 dbus_int32_t serial = -1; 3033 DBusDispatchStatus status; 3034 3035 _dbus_return_val_if_fail (connection != NULL, FALSE); 3036 _dbus_return_val_if_fail (message != NULL, FALSE); 3037 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); 3038 3039 if (pending_return) 3040 *pending_return = NULL; 3041 3042 CONNECTION_LOCK (connection); 3043 3044 if (!_dbus_connection_get_is_connected_unlocked (connection)) 3045 { 3046 CONNECTION_UNLOCK (connection); 3047 3048 *pending_return = NULL; 3049 3050 return TRUE; 3051 } 3052 3053 pending = _dbus_pending_call_new_unlocked (connection, 3054 timeout_milliseconds, 3055 reply_handler_timeout); 3056 3057 if (pending == NULL) 3058 { 3059 CONNECTION_UNLOCK (connection); 3060 return FALSE; 3061 } 3062 3063 /* Assign a serial to the message */ 3064 serial = dbus_message_get_serial (message); 3065 if (serial == 0) 3066 { 3067 serial = _dbus_connection_get_next_client_serial (connection); 3068 _dbus_message_set_serial (message, serial); 3069 } 3070 3071 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial)) 3072 goto error; 3073 3074 /* Insert the serial in the pending replies hash; 3075 * hash takes a refcount on DBusPendingCall. 3076 * Also, add the timeout. 3077 */ 3078 if (!_dbus_connection_attach_pending_call_unlocked (connection, 3079 pending)) 3080 goto error; 3081 3082 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL)) 3083 { 3084 _dbus_connection_detach_pending_call_and_unlock (connection, 3085 pending); 3086 goto error_unlocked; 3087 } 3088 3089 if (pending_return) 3090 *pending_return = pending; /* hand off refcount */ 3091 else 3092 { 3093 _dbus_connection_detach_pending_call_unlocked (connection, pending); 3094 /* we still have a ref to the pending call in this case, we unref 3095 * after unlocking, below 3096 */ 3097 } 3098 3099 status = _dbus_connection_get_dispatch_status_unlocked (connection); 3100 3101 /* this calls out to user code */ 3102 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 3103 3104 if (pending_return == NULL) 3105 dbus_pending_call_unref (pending); 3106 3107 return TRUE; 3108 3109 error: 3110 CONNECTION_UNLOCK (connection); 3111 error_unlocked: 3112 dbus_pending_call_unref (pending); 3113 return FALSE; 3114} 3115 3116/** 3117 * Sends a message and blocks a certain time period while waiting for 3118 * a reply. This function does not reenter the main loop, 3119 * i.e. messages other than the reply are queued up but not 3120 * processed. This function is used to do non-reentrant "method 3121 * calls." 3122 * 3123 * If a normal reply is received, it is returned, and removed from the 3124 * incoming message queue. If it is not received, #NULL is returned 3125 * and the error is set to #DBUS_ERROR_NO_REPLY. If an error reply is 3126 * received, it is converted to a #DBusError and returned as an error, 3127 * then the reply message is deleted. If something else goes wrong, 3128 * result is set to whatever is appropriate, such as 3129 * #DBUS_ERROR_NO_MEMORY or #DBUS_ERROR_DISCONNECTED. 3130 * 3131 * @param connection the connection 3132 * @param message the message to send 3133 * @param timeout_milliseconds timeout in milliseconds or -1 for default 3134 * @param error return location for error message 3135 * @returns the message that is the reply or #NULL with an error code if the 3136 * function fails. 3137 */ 3138DBusMessage* 3139dbus_connection_send_with_reply_and_block (DBusConnection *connection, 3140 DBusMessage *message, 3141 int timeout_milliseconds, 3142 DBusError *error) 3143{ 3144 DBusMessage *reply; 3145 DBusPendingCall *pending; 3146 3147 _dbus_return_val_if_fail (connection != NULL, NULL); 3148 _dbus_return_val_if_fail (message != NULL, NULL); 3149 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL); 3150 _dbus_return_val_if_error_is_set (error, NULL); 3151 3152 if (!dbus_connection_send_with_reply (connection, message, 3153 &pending, timeout_milliseconds)) 3154 { 3155 _DBUS_SET_OOM (error); 3156 return NULL; 3157 } 3158 3159 if (pending == NULL) 3160 { 3161 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed"); 3162 return NULL; 3163 } 3164 3165 dbus_pending_call_block (pending); 3166 3167 reply = dbus_pending_call_steal_reply (pending); 3168 dbus_pending_call_unref (pending); 3169 3170 /* call_complete_and_unlock() called from pending_call_block() should 3171 * always fill this in. 3172 */ 3173 _dbus_assert (reply != NULL); 3174 3175 if (dbus_set_error_from_message (error, reply)) 3176 { 3177 dbus_message_unref (reply); 3178 return NULL; 3179 } 3180 else 3181 return reply; 3182} 3183 3184/** 3185 * Blocks until the outgoing message queue is empty. 3186 * Assumes connection lock already held. 3187 * 3188 * If you call this, you MUST call update_dispatch_status afterword... 3189 * 3190 * @param connection the connection. 3191 */ 3192DBusDispatchStatus 3193_dbus_connection_flush_unlocked (DBusConnection *connection) 3194{ 3195 /* We have to specify DBUS_ITERATION_DO_READING here because 3196 * otherwise we could have two apps deadlock if they are both doing 3197 * a flush(), and the kernel buffers fill up. This could change the 3198 * dispatch status. 3199 */ 3200 DBusDispatchStatus status; 3201 3202 HAVE_LOCK_CHECK (connection); 3203 3204 while (connection->n_outgoing > 0 && 3205 _dbus_connection_get_is_connected_unlocked (connection)) 3206 { 3207 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME); 3208 HAVE_LOCK_CHECK (connection); 3209 _dbus_connection_do_iteration_unlocked (connection, 3210 DBUS_ITERATION_DO_READING | 3211 DBUS_ITERATION_DO_WRITING | 3212 DBUS_ITERATION_BLOCK, 3213 -1); 3214 } 3215 3216 HAVE_LOCK_CHECK (connection); 3217 _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME); 3218 status = _dbus_connection_get_dispatch_status_unlocked (connection); 3219 3220 HAVE_LOCK_CHECK (connection); 3221 return status; 3222} 3223 3224/** 3225 * Blocks until the outgoing message queue is empty. 3226 * 3227 * @param connection the connection. 3228 */ 3229void 3230dbus_connection_flush (DBusConnection *connection) 3231{ 3232 /* We have to specify DBUS_ITERATION_DO_READING here because 3233 * otherwise we could have two apps deadlock if they are both doing 3234 * a flush(), and the kernel buffers fill up. This could change the 3235 * dispatch status. 3236 */ 3237 DBusDispatchStatus status; 3238 3239 _dbus_return_if_fail (connection != NULL); 3240 3241 CONNECTION_LOCK (connection); 3242 3243 status = _dbus_connection_flush_unlocked (connection); 3244 3245 HAVE_LOCK_CHECK (connection); 3246 /* Unlocks and calls out to user code */ 3247 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 3248 3249 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME); 3250} 3251 3252/** 3253 * This function implements dbus_connection_read_write_dispatch() and 3254 * dbus_connection_read_write() (they pass a different value for the 3255 * dispatch parameter). 3256 * 3257 * @param connection the connection 3258 * @param timeout_milliseconds max time to block or -1 for infinite 3259 * @param dispatch dispatch new messages or leave them on the incoming queue 3260 * @returns #TRUE if the disconnect message has not been processed 3261 */ 3262static dbus_bool_t 3263_dbus_connection_read_write_dispatch (DBusConnection *connection, 3264 int timeout_milliseconds, 3265 dbus_bool_t dispatch) 3266{ 3267 DBusDispatchStatus dstatus; 3268 dbus_bool_t no_progress_possible; 3269 3270 dstatus = dbus_connection_get_dispatch_status (connection); 3271 3272 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS) 3273 { 3274 _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME); 3275 dbus_connection_dispatch (connection); 3276 CONNECTION_LOCK (connection); 3277 } 3278 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY) 3279 { 3280 _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME); 3281 _dbus_memory_pause_based_on_timeout (timeout_milliseconds); 3282 CONNECTION_LOCK (connection); 3283 } 3284 else 3285 { 3286 CONNECTION_LOCK (connection); 3287 if (_dbus_connection_get_is_connected_unlocked (connection)) 3288 { 3289 _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME); 3290 _dbus_connection_do_iteration_unlocked (connection, 3291 DBUS_ITERATION_DO_READING | 3292 DBUS_ITERATION_DO_WRITING | 3293 DBUS_ITERATION_BLOCK, 3294 timeout_milliseconds); 3295 } 3296 } 3297 3298 HAVE_LOCK_CHECK (connection); 3299 /* If we can dispatch, we can make progress until the Disconnected message 3300 * has been processed; if we can only read/write, we can make progress 3301 * as long as the transport is open. 3302 */ 3303 if (dispatch) 3304 no_progress_possible = connection->n_incoming == 0 && 3305 connection->disconnect_message_link == NULL; 3306 else 3307 no_progress_possible = _dbus_connection_get_is_connected_unlocked (connection); 3308 CONNECTION_UNLOCK (connection); 3309 return !no_progress_possible; /* TRUE if we can make more progress */ 3310} 3311 3312 3313/** 3314 * This function is intended for use with applications that don't want 3315 * to write a main loop and deal with #DBusWatch and #DBusTimeout. An 3316 * example usage would be: 3317 * 3318 * @code 3319 * while (dbus_connection_read_write_dispatch (connection, -1)) 3320 * ; // empty loop body 3321 * @endcode 3322 * 3323 * In this usage you would normally have set up a filter function to look 3324 * at each message as it is dispatched. The loop terminates when the last 3325 * message from the connection (the disconnected signal) is processed. 3326 * 3327 * If there are messages to dispatch, this function will 3328 * dbus_connection_dispatch() once, and return. If there are no 3329 * messages to dispatch, this function will block until it can read or 3330 * write, then read or write, then return. 3331 * 3332 * The way to think of this function is that it either makes some sort 3333 * of progress, or it blocks. 3334 * 3335 * The return value indicates whether the disconnect message has been 3336 * processed, NOT whether the connection is connected. This is 3337 * important because even after disconnecting, you want to process any 3338 * messages you received prior to the disconnect. 3339 * 3340 * @param connection the connection 3341 * @param timeout_milliseconds max time to block or -1 for infinite 3342 * @returns #TRUE if the disconnect message has not been processed 3343 */ 3344dbus_bool_t 3345dbus_connection_read_write_dispatch (DBusConnection *connection, 3346 int timeout_milliseconds) 3347{ 3348 _dbus_return_val_if_fail (connection != NULL, FALSE); 3349 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); 3350 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE); 3351} 3352 3353/** 3354 * This function is intended for use with applications that don't want to 3355 * write a main loop and deal with #DBusWatch and #DBusTimeout. See also 3356 * dbus_connection_read_write_dispatch(). 3357 * 3358 * As long as the connection is open, this function will block until it can 3359 * read or write, then read or write, then return #TRUE. 3360 * 3361 * If the connection is closed, the function returns #FALSE. 3362 * 3363 * The return value indicates whether reading or writing is still 3364 * possible, i.e. whether the connection is connected. 3365 * 3366 * Note that even after disconnection, messages may remain in the 3367 * incoming queue that need to be 3368 * processed. dbus_connection_read_write_dispatch() dispatches 3369 * incoming messages for you; with dbus_connection_read_write() you 3370 * have to arrange to drain the incoming queue yourself. 3371 * 3372 * @param connection the connection 3373 * @param timeout_milliseconds max time to block or -1 for infinite 3374 * @returns #TRUE if still connected 3375 */ 3376dbus_bool_t 3377dbus_connection_read_write (DBusConnection *connection, 3378 int timeout_milliseconds) 3379{ 3380 _dbus_return_val_if_fail (connection != NULL, FALSE); 3381 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE); 3382 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE); 3383} 3384 3385/* We need to call this anytime we pop the head of the queue, and then 3386 * update_dispatch_status_and_unlock needs to be called afterward 3387 * which will "process" the disconnected message and set 3388 * disconnected_message_processed. 3389 */ 3390static void 3391check_disconnected_message_arrived_unlocked (DBusConnection *connection, 3392 DBusMessage *head_of_queue) 3393{ 3394 HAVE_LOCK_CHECK (connection); 3395 3396 /* checking that the link is NULL is an optimization to avoid the is_signal call */ 3397 if (connection->disconnect_message_link == NULL && 3398 dbus_message_is_signal (head_of_queue, 3399 DBUS_INTERFACE_LOCAL, 3400 "Disconnected")) 3401 { 3402 connection->disconnected_message_arrived = TRUE; 3403 } 3404} 3405 3406/** 3407 * Returns the first-received message from the incoming message queue, 3408 * leaving it in the queue. If the queue is empty, returns #NULL. 3409 * 3410 * The caller does not own a reference to the returned message, and 3411 * must either return it using dbus_connection_return_message() or 3412 * keep it after calling dbus_connection_steal_borrowed_message(). No 3413 * one can get at the message while its borrowed, so return it as 3414 * quickly as possible and don't keep a reference to it after 3415 * returning it. If you need to keep the message, make a copy of it. 3416 * 3417 * dbus_connection_dispatch() will block if called while a borrowed 3418 * message is outstanding; only one piece of code can be playing with 3419 * the incoming queue at a time. This function will block if called 3420 * during a dbus_connection_dispatch(). 3421 * 3422 * @param connection the connection. 3423 * @returns next message in the incoming queue. 3424 */ 3425DBusMessage* 3426dbus_connection_borrow_message (DBusConnection *connection) 3427{ 3428 DBusDispatchStatus status; 3429 DBusMessage *message; 3430 3431 _dbus_return_val_if_fail (connection != NULL, NULL); 3432 3433 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME); 3434 3435 /* this is called for the side effect that it queues 3436 * up any messages from the transport 3437 */ 3438 status = dbus_connection_get_dispatch_status (connection); 3439 if (status != DBUS_DISPATCH_DATA_REMAINS) 3440 return NULL; 3441 3442 CONNECTION_LOCK (connection); 3443 3444 _dbus_connection_acquire_dispatch (connection); 3445 3446 /* While a message is outstanding, the dispatch lock is held */ 3447 _dbus_assert (connection->message_borrowed == NULL); 3448 3449 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages); 3450 3451 message = connection->message_borrowed; 3452 3453 check_disconnected_message_arrived_unlocked (connection, message); 3454 3455 /* Note that we KEEP the dispatch lock until the message is returned */ 3456 if (message == NULL) 3457 _dbus_connection_release_dispatch (connection); 3458 3459 CONNECTION_UNLOCK (connection); 3460 3461 /* We don't update dispatch status until it's returned or stolen */ 3462 3463 return message; 3464} 3465 3466/** 3467 * Used to return a message after peeking at it using 3468 * dbus_connection_borrow_message(). Only called if 3469 * message from dbus_connection_borrow_message() was non-#NULL. 3470 * 3471 * @param connection the connection 3472 * @param message the message from dbus_connection_borrow_message() 3473 */ 3474void 3475dbus_connection_return_message (DBusConnection *connection, 3476 DBusMessage *message) 3477{ 3478 DBusDispatchStatus status; 3479 3480 _dbus_return_if_fail (connection != NULL); 3481 _dbus_return_if_fail (message != NULL); 3482 _dbus_return_if_fail (message == connection->message_borrowed); 3483 _dbus_return_if_fail (connection->dispatch_acquired); 3484 3485 CONNECTION_LOCK (connection); 3486 3487 _dbus_assert (message == connection->message_borrowed); 3488 3489 connection->message_borrowed = NULL; 3490 3491 _dbus_connection_release_dispatch (connection); 3492 3493 status = _dbus_connection_get_dispatch_status_unlocked (connection); 3494 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 3495} 3496 3497/** 3498 * Used to keep a message after peeking at it using 3499 * dbus_connection_borrow_message(). Before using this function, see 3500 * the caveats/warnings in the documentation for 3501 * dbus_connection_pop_message(). 3502 * 3503 * @param connection the connection 3504 * @param message the message from dbus_connection_borrow_message() 3505 */ 3506void 3507dbus_connection_steal_borrowed_message (DBusConnection *connection, 3508 DBusMessage *message) 3509{ 3510 DBusMessage *pop_message; 3511 DBusDispatchStatus status; 3512 3513 _dbus_return_if_fail (connection != NULL); 3514 _dbus_return_if_fail (message != NULL); 3515 _dbus_return_if_fail (message == connection->message_borrowed); 3516 _dbus_return_if_fail (connection->dispatch_acquired); 3517 3518 CONNECTION_LOCK (connection); 3519 3520 _dbus_assert (message == connection->message_borrowed); 3521 3522 pop_message = _dbus_list_pop_first (&connection->incoming_messages); 3523 _dbus_assert (message == pop_message); 3524 3525 connection->n_incoming -= 1; 3526 3527 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n", 3528 message, connection->n_incoming); 3529 3530 connection->message_borrowed = NULL; 3531 3532 _dbus_connection_release_dispatch (connection); 3533 3534 status = _dbus_connection_get_dispatch_status_unlocked (connection); 3535 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 3536} 3537 3538/* See dbus_connection_pop_message, but requires the caller to own 3539 * the lock before calling. May drop the lock while running. 3540 */ 3541static DBusList* 3542_dbus_connection_pop_message_link_unlocked (DBusConnection *connection) 3543{ 3544 HAVE_LOCK_CHECK (connection); 3545 3546 _dbus_assert (connection->message_borrowed == NULL); 3547 3548 if (connection->n_incoming > 0) 3549 { 3550 DBusList *link; 3551 3552 link = _dbus_list_pop_first_link (&connection->incoming_messages); 3553 connection->n_incoming -= 1; 3554 3555 _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n", 3556 link->data, 3557 dbus_message_get_type (link->data), 3558 dbus_message_get_path (link->data) ? 3559 dbus_message_get_path (link->data) : 3560 "no path", 3561 dbus_message_get_interface (link->data) ? 3562 dbus_message_get_interface (link->data) : 3563 "no interface", 3564 dbus_message_get_member (link->data) ? 3565 dbus_message_get_member (link->data) : 3566 "no member", 3567 dbus_message_get_signature (link->data), 3568 connection, connection->n_incoming); 3569 3570 check_disconnected_message_arrived_unlocked (connection, link->data); 3571 3572 return link; 3573 } 3574 else 3575 return NULL; 3576} 3577 3578/* See dbus_connection_pop_message, but requires the caller to own 3579 * the lock before calling. May drop the lock while running. 3580 */ 3581static DBusMessage* 3582_dbus_connection_pop_message_unlocked (DBusConnection *connection) 3583{ 3584 DBusList *link; 3585 3586 HAVE_LOCK_CHECK (connection); 3587 3588 link = _dbus_connection_pop_message_link_unlocked (connection); 3589 3590 if (link != NULL) 3591 { 3592 DBusMessage *message; 3593 3594 message = link->data; 3595 3596 _dbus_list_free_link (link); 3597 3598 return message; 3599 } 3600 else 3601 return NULL; 3602} 3603 3604static void 3605_dbus_connection_putback_message_link_unlocked (DBusConnection *connection, 3606 DBusList *message_link) 3607{ 3608 HAVE_LOCK_CHECK (connection); 3609 3610 _dbus_assert (message_link != NULL); 3611 /* You can't borrow a message while a link is outstanding */ 3612 _dbus_assert (connection->message_borrowed == NULL); 3613 /* We had to have the dispatch lock across the pop/putback */ 3614 _dbus_assert (connection->dispatch_acquired); 3615 3616 _dbus_list_prepend_link (&connection->incoming_messages, 3617 message_link); 3618 connection->n_incoming += 1; 3619 3620 _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n", 3621 message_link->data, 3622 dbus_message_get_type (message_link->data), 3623 dbus_message_get_interface (message_link->data) ? 3624 dbus_message_get_interface (message_link->data) : 3625 "no interface", 3626 dbus_message_get_member (message_link->data) ? 3627 dbus_message_get_member (message_link->data) : 3628 "no member", 3629 dbus_message_get_signature (message_link->data), 3630 connection, connection->n_incoming); 3631} 3632 3633/** 3634 * Returns the first-received message from the incoming message queue, 3635 * removing it from the queue. The caller owns a reference to the 3636 * returned message. If the queue is empty, returns #NULL. 3637 * 3638 * This function bypasses any message handlers that are registered, 3639 * and so using it is usually wrong. Instead, let the main loop invoke 3640 * dbus_connection_dispatch(). Popping messages manually is only 3641 * useful in very simple programs that don't share a #DBusConnection 3642 * with any libraries or other modules. 3643 * 3644 * There is a lock that covers all ways of accessing the incoming message 3645 * queue, so dbus_connection_dispatch(), dbus_connection_pop_message(), 3646 * dbus_connection_borrow_message(), etc. will all block while one of the others 3647 * in the group is running. 3648 * 3649 * @param connection the connection. 3650 * @returns next message in the incoming queue. 3651 */ 3652DBusMessage* 3653dbus_connection_pop_message (DBusConnection *connection) 3654{ 3655 DBusMessage *message; 3656 DBusDispatchStatus status; 3657 3658 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME); 3659 3660 /* this is called for the side effect that it queues 3661 * up any messages from the transport 3662 */ 3663 status = dbus_connection_get_dispatch_status (connection); 3664 if (status != DBUS_DISPATCH_DATA_REMAINS) 3665 return NULL; 3666 3667 CONNECTION_LOCK (connection); 3668 _dbus_connection_acquire_dispatch (connection); 3669 HAVE_LOCK_CHECK (connection); 3670 3671 message = _dbus_connection_pop_message_unlocked (connection); 3672 3673 _dbus_verbose ("Returning popped message %p\n", message); 3674 3675 _dbus_connection_release_dispatch (connection); 3676 3677 status = _dbus_connection_get_dispatch_status_unlocked (connection); 3678 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 3679 3680 return message; 3681} 3682 3683/** 3684 * Acquire the dispatcher. This is a separate lock so the main 3685 * connection lock can be dropped to call out to application dispatch 3686 * handlers. 3687 * 3688 * @param connection the connection. 3689 */ 3690static void 3691_dbus_connection_acquire_dispatch (DBusConnection *connection) 3692{ 3693 HAVE_LOCK_CHECK (connection); 3694 3695 _dbus_connection_ref_unlocked (connection); 3696 CONNECTION_UNLOCK (connection); 3697 3698 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME); 3699 _dbus_mutex_lock (connection->dispatch_mutex); 3700 3701 while (connection->dispatch_acquired) 3702 { 3703 _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME); 3704 _dbus_condvar_wait (connection->dispatch_cond, 3705 connection->dispatch_mutex); 3706 } 3707 3708 _dbus_assert (!connection->dispatch_acquired); 3709 3710 connection->dispatch_acquired = TRUE; 3711 3712 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME); 3713 _dbus_mutex_unlock (connection->dispatch_mutex); 3714 3715 CONNECTION_LOCK (connection); 3716 _dbus_connection_unref_unlocked (connection); 3717} 3718 3719/** 3720 * Release the dispatcher when you're done with it. Only call 3721 * after you've acquired the dispatcher. Wakes up at most one 3722 * thread currently waiting to acquire the dispatcher. 3723 * 3724 * @param connection the connection. 3725 */ 3726static void 3727_dbus_connection_release_dispatch (DBusConnection *connection) 3728{ 3729 HAVE_LOCK_CHECK (connection); 3730 3731 _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME); 3732 _dbus_mutex_lock (connection->dispatch_mutex); 3733 3734 _dbus_assert (connection->dispatch_acquired); 3735 3736 connection->dispatch_acquired = FALSE; 3737 _dbus_condvar_wake_one (connection->dispatch_cond); 3738 3739 _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME); 3740 _dbus_mutex_unlock (connection->dispatch_mutex); 3741} 3742 3743static void 3744_dbus_connection_failed_pop (DBusConnection *connection, 3745 DBusList *message_link) 3746{ 3747 _dbus_list_prepend_link (&connection->incoming_messages, 3748 message_link); 3749 connection->n_incoming += 1; 3750} 3751 3752/* Note this may be called multiple times since we don't track whether we already did it */ 3753static void 3754notify_disconnected_unlocked (DBusConnection *connection) 3755{ 3756 HAVE_LOCK_CHECK (connection); 3757 3758 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected 3759 * connection from dbus_bus_get(). We make the same guarantee for 3760 * dbus_connection_open() but in a different way since we don't want to 3761 * unref right here; we instead check for connectedness before returning 3762 * the connection from the hash. 3763 */ 3764 _dbus_bus_notify_shared_connection_disconnected_unlocked (connection); 3765 3766 /* Dump the outgoing queue, we aren't going to be able to 3767 * send it now, and we'd like accessors like 3768 * dbus_connection_get_outgoing_size() to be accurate. 3769 */ 3770 if (connection->n_outgoing > 0) 3771 { 3772 DBusList *link; 3773 3774 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n", 3775 connection->n_outgoing); 3776 3777 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages))) 3778 { 3779 _dbus_connection_message_sent (connection, link->data); 3780 } 3781 } 3782} 3783 3784/* Note this may be called multiple times since we don't track whether we already did it */ 3785static DBusDispatchStatus 3786notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection) 3787{ 3788 HAVE_LOCK_CHECK (connection); 3789 3790 if (connection->disconnect_message_link != NULL) 3791 { 3792 _dbus_verbose ("Sending disconnect message from %s\n", 3793 _DBUS_FUNCTION_NAME); 3794 3795 /* If we have pending calls, queue their timeouts - we want the Disconnected 3796 * to be the last message, after these timeouts. 3797 */ 3798 connection_timeout_and_complete_all_pending_calls_unlocked (connection); 3799 3800 /* We haven't sent the disconnect message already, 3801 * and all real messages have been queued up. 3802 */ 3803 _dbus_connection_queue_synthesized_message_link (connection, 3804 connection->disconnect_message_link); 3805 connection->disconnect_message_link = NULL; 3806 3807 return DBUS_DISPATCH_DATA_REMAINS; 3808 } 3809 3810 return DBUS_DISPATCH_COMPLETE; 3811} 3812 3813static DBusDispatchStatus 3814_dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection) 3815{ 3816 HAVE_LOCK_CHECK (connection); 3817 3818 if (connection->n_incoming > 0) 3819 return DBUS_DISPATCH_DATA_REMAINS; 3820 else if (!_dbus_transport_queue_messages (connection->transport)) 3821 return DBUS_DISPATCH_NEED_MEMORY; 3822 else 3823 { 3824 DBusDispatchStatus status; 3825 dbus_bool_t is_connected; 3826 3827 status = _dbus_transport_get_dispatch_status (connection->transport); 3828 is_connected = _dbus_transport_get_is_connected (connection->transport); 3829 3830 _dbus_verbose ("dispatch status = %s is_connected = %d\n", 3831 DISPATCH_STATUS_NAME (status), is_connected); 3832 3833 if (!is_connected) 3834 { 3835 /* It's possible this would be better done by having an explicit 3836 * notification from _dbus_transport_disconnect() that would 3837 * synchronously do this, instead of waiting for the next dispatch 3838 * status check. However, probably not good to change until it causes 3839 * a problem. 3840 */ 3841 notify_disconnected_unlocked (connection); 3842 3843 /* I'm not sure this is needed; the idea is that we want to 3844 * queue the Disconnected only after we've read all the 3845 * messages, but if we're disconnected maybe we are guaranteed 3846 * to have read them all ? 3847 */ 3848 if (status == DBUS_DISPATCH_COMPLETE) 3849 status = notify_disconnected_and_dispatch_complete_unlocked (connection); 3850 } 3851 3852 if (status != DBUS_DISPATCH_COMPLETE) 3853 return status; 3854 else if (connection->n_incoming > 0) 3855 return DBUS_DISPATCH_DATA_REMAINS; 3856 else 3857 return DBUS_DISPATCH_COMPLETE; 3858 } 3859} 3860 3861static void 3862_dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection, 3863 DBusDispatchStatus new_status) 3864{ 3865 dbus_bool_t changed; 3866 DBusDispatchStatusFunction function; 3867 void *data; 3868 3869 HAVE_LOCK_CHECK (connection); 3870 3871 _dbus_connection_ref_unlocked (connection); 3872 3873 changed = new_status != connection->last_dispatch_status; 3874 3875 connection->last_dispatch_status = new_status; 3876 3877 function = connection->dispatch_status_function; 3878 data = connection->dispatch_status_data; 3879 3880 if (connection->disconnected_message_arrived && 3881 !connection->disconnected_message_processed) 3882 { 3883 connection->disconnected_message_processed = TRUE; 3884 3885 /* this does an unref, but we have a ref 3886 * so we should not run the finalizer here 3887 * inside the lock. 3888 */ 3889 connection_forget_shared_unlocked (connection); 3890 3891 if (connection->exit_on_disconnect) 3892 { 3893 CONNECTION_UNLOCK (connection); 3894 3895 _dbus_verbose ("Exiting on Disconnected signal\n"); 3896 _dbus_exit (1); 3897 _dbus_assert_not_reached ("Call to exit() returned"); 3898 } 3899 } 3900 3901 /* We drop the lock */ 3902 CONNECTION_UNLOCK (connection); 3903 3904 if (changed && function) 3905 { 3906 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n", 3907 connection, new_status, 3908 DISPATCH_STATUS_NAME (new_status)); 3909 (* function) (connection, new_status, data); 3910 } 3911 3912 dbus_connection_unref (connection); 3913} 3914 3915/** 3916 * Gets the current state of the incoming message queue. 3917 * #DBUS_DISPATCH_DATA_REMAINS indicates that the message queue 3918 * may contain messages. #DBUS_DISPATCH_COMPLETE indicates that the 3919 * incoming queue is empty. #DBUS_DISPATCH_NEED_MEMORY indicates that 3920 * there could be data, but we can't know for sure without more 3921 * memory. 3922 * 3923 * To process the incoming message queue, use dbus_connection_dispatch() 3924 * or (in rare cases) dbus_connection_pop_message(). 3925 * 3926 * Note, #DBUS_DISPATCH_DATA_REMAINS really means that either we 3927 * have messages in the queue, or we have raw bytes buffered up 3928 * that need to be parsed. When these bytes are parsed, they 3929 * may not add up to an entire message. Thus, it's possible 3930 * to see a status of #DBUS_DISPATCH_DATA_REMAINS but not 3931 * have a message yet. 3932 * 3933 * In particular this happens on initial connection, because all sorts 3934 * of authentication protocol stuff has to be parsed before the 3935 * first message arrives. 3936 * 3937 * @param connection the connection. 3938 * @returns current dispatch status 3939 */ 3940DBusDispatchStatus 3941dbus_connection_get_dispatch_status (DBusConnection *connection) 3942{ 3943 DBusDispatchStatus status; 3944 3945 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE); 3946 3947 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME); 3948 3949 CONNECTION_LOCK (connection); 3950 3951 status = _dbus_connection_get_dispatch_status_unlocked (connection); 3952 3953 CONNECTION_UNLOCK (connection); 3954 3955 return status; 3956} 3957 3958/** 3959 * Filter funtion for handling the Peer standard interface. 3960 */ 3961static DBusHandlerResult 3962_dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection, 3963 DBusMessage *message) 3964{ 3965 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL) 3966 { 3967 /* This means we're letting the bus route this message */ 3968 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 3969 } 3970 else if (dbus_message_is_method_call (message, 3971 DBUS_INTERFACE_PEER, 3972 "Ping")) 3973 { 3974 DBusMessage *ret; 3975 dbus_bool_t sent; 3976 3977 ret = dbus_message_new_method_return (message); 3978 if (ret == NULL) 3979 return DBUS_HANDLER_RESULT_NEED_MEMORY; 3980 3981 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL); 3982 3983 dbus_message_unref (ret); 3984 3985 if (!sent) 3986 return DBUS_HANDLER_RESULT_NEED_MEMORY; 3987 3988 return DBUS_HANDLER_RESULT_HANDLED; 3989 } 3990 else if (dbus_message_is_method_call (message, 3991 DBUS_INTERFACE_PEER, 3992 "GetMachineId")) 3993 { 3994 DBusMessage *ret; 3995 dbus_bool_t sent; 3996 DBusString uuid; 3997 3998 ret = dbus_message_new_method_return (message); 3999 if (ret == NULL) 4000 return DBUS_HANDLER_RESULT_NEED_MEMORY; 4001 4002 sent = FALSE; 4003 _dbus_string_init (&uuid); 4004 if (_dbus_get_local_machine_uuid_encoded (&uuid)) 4005 { 4006 const char *v_STRING = _dbus_string_get_const_data (&uuid); 4007 if (dbus_message_append_args (ret, 4008 DBUS_TYPE_STRING, &v_STRING, 4009 DBUS_TYPE_INVALID)) 4010 { 4011 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL); 4012 } 4013 } 4014 _dbus_string_free (&uuid); 4015 4016 dbus_message_unref (ret); 4017 4018 if (!sent) 4019 return DBUS_HANDLER_RESULT_NEED_MEMORY; 4020 4021 return DBUS_HANDLER_RESULT_HANDLED; 4022 } 4023 else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER)) 4024 { 4025 /* We need to bounce anything else with this interface, otherwise apps 4026 * could start extending the interface and when we added extensions 4027 * here to DBusConnection we'd break those apps. 4028 */ 4029 4030 DBusMessage *ret; 4031 dbus_bool_t sent; 4032 4033 ret = dbus_message_new_error (message, 4034 DBUS_ERROR_UNKNOWN_METHOD, 4035 "Unknown method invoked on org.freedesktop.DBus.Peer interface"); 4036 if (ret == NULL) 4037 return DBUS_HANDLER_RESULT_NEED_MEMORY; 4038 4039 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL); 4040 4041 dbus_message_unref (ret); 4042 4043 if (!sent) 4044 return DBUS_HANDLER_RESULT_NEED_MEMORY; 4045 4046 return DBUS_HANDLER_RESULT_HANDLED; 4047 } 4048 else 4049 { 4050 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 4051 } 4052} 4053 4054/** 4055* Processes all builtin filter functions 4056* 4057* If the spec specifies a standard interface 4058* they should be processed from this method 4059**/ 4060static DBusHandlerResult 4061_dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection, 4062 DBusMessage *message) 4063{ 4064 /* We just run one filter for now but have the option to run more 4065 if the spec calls for it in the future */ 4066 4067 return _dbus_connection_peer_filter_unlocked_no_update (connection, message); 4068} 4069 4070/** 4071 * Processes any incoming data. 4072 * 4073 * If there are messages in the incoming queue, 4074 * dbus_connection_dispatch() removes one message from the queue and 4075 * runs any handlers for it (handlers are added with 4076 * dbus_connection_add_filter() or 4077 * dbus_connection_register_object_path() for example). 4078 * 4079 * If there's incoming raw data that has not yet been parsed, it is 4080 * parsed, which may or may not result in adding messages to the 4081 * incoming queue. 4082 * 4083 * The incoming message queue is filled when the connection 4084 * reads from its underlying transport (such as a socket). 4085 * Reading usually happens in dbus_watch_handle() or 4086 * dbus_connection_read_write(). 4087 * 4088 * If any data has been read from the underlying transport, but not 4089 * yet dispatched, the dispatch status will be 4090 * #DBUS_DISPATCH_DATA_REMAINS. See dbus_connection_get_dispatch_status() 4091 * for more on dispatch statuses. 4092 * 4093 * Be careful about calling dbus_connection_dispatch() from inside a 4094 * message handler, i.e. calling dbus_connection_dispatch() 4095 * recursively. If threads have been initialized with a recursive 4096 * mutex function, then this will not deadlock; however, it can 4097 * certainly confuse your application. 4098 * 4099 * @todo some FIXME in here about handling DBUS_HANDLER_RESULT_NEED_MEMORY 4100 * 4101 * @param connection the connection 4102 * @returns dispatch status, see dbus_connection_get_dispatch_status() 4103 */ 4104DBusDispatchStatus 4105dbus_connection_dispatch (DBusConnection *connection) 4106{ 4107 DBusMessage *message; 4108 DBusList *link, *filter_list_copy, *message_link; 4109 DBusHandlerResult result; 4110 DBusPendingCall *pending; 4111 dbus_int32_t reply_serial; 4112 DBusDispatchStatus status; 4113 4114 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE); 4115 4116 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME); 4117 4118 CONNECTION_LOCK (connection); 4119 status = _dbus_connection_get_dispatch_status_unlocked (connection); 4120 if (status != DBUS_DISPATCH_DATA_REMAINS) 4121 { 4122 /* unlocks and calls out to user code */ 4123 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 4124 return status; 4125 } 4126 4127 /* We need to ref the connection since the callback could potentially 4128 * drop the last ref to it 4129 */ 4130 _dbus_connection_ref_unlocked (connection); 4131 4132 _dbus_connection_acquire_dispatch (connection); 4133 HAVE_LOCK_CHECK (connection); 4134 4135 message_link = _dbus_connection_pop_message_link_unlocked (connection); 4136 if (message_link == NULL) 4137 { 4138 /* another thread dispatched our stuff */ 4139 4140 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n"); 4141 4142 _dbus_connection_release_dispatch (connection); 4143 4144 status = _dbus_connection_get_dispatch_status_unlocked (connection); 4145 4146 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 4147 4148 dbus_connection_unref (connection); 4149 4150 return status; 4151 } 4152 4153 message = message_link->data; 4154 4155 _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n", 4156 message, 4157 dbus_message_get_type (message), 4158 dbus_message_get_interface (message) ? 4159 dbus_message_get_interface (message) : 4160 "no interface", 4161 dbus_message_get_member (message) ? 4162 dbus_message_get_member (message) : 4163 "no member", 4164 dbus_message_get_signature (message)); 4165 4166 result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED; 4167 4168 /* Pending call handling must be first, because if you do 4169 * dbus_connection_send_with_reply_and_block() or 4170 * dbus_pending_call_block() then no handlers/filters will be run on 4171 * the reply. We want consistent semantics in the case where we 4172 * dbus_connection_dispatch() the reply. 4173 */ 4174 4175 reply_serial = dbus_message_get_reply_serial (message); 4176 pending = _dbus_hash_table_lookup_int (connection->pending_replies, 4177 reply_serial); 4178 if (pending) 4179 { 4180 _dbus_verbose ("Dispatching a pending reply\n"); 4181 complete_pending_call_and_unlock (connection, pending, message); 4182 pending = NULL; /* it's probably unref'd */ 4183 4184 CONNECTION_LOCK (connection); 4185 _dbus_verbose ("pending call completed in dispatch\n"); 4186 result = DBUS_HANDLER_RESULT_HANDLED; 4187 goto out; 4188 } 4189 4190 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message); 4191 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED) 4192 goto out; 4193 4194 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy)) 4195 { 4196 _dbus_connection_release_dispatch (connection); 4197 HAVE_LOCK_CHECK (connection); 4198 4199 _dbus_connection_failed_pop (connection, message_link); 4200 4201 /* unlocks and calls user code */ 4202 _dbus_connection_update_dispatch_status_and_unlock (connection, 4203 DBUS_DISPATCH_NEED_MEMORY); 4204 4205 if (pending) 4206 dbus_pending_call_unref (pending); 4207 dbus_connection_unref (connection); 4208 4209 return DBUS_DISPATCH_NEED_MEMORY; 4210 } 4211 4212 _dbus_list_foreach (&filter_list_copy, 4213 (DBusForeachFunction)_dbus_message_filter_ref, 4214 NULL); 4215 4216 /* We're still protected from dispatch() reentrancy here 4217 * since we acquired the dispatcher 4218 */ 4219 CONNECTION_UNLOCK (connection); 4220 4221 link = _dbus_list_get_first_link (&filter_list_copy); 4222 while (link != NULL) 4223 { 4224 DBusMessageFilter *filter = link->data; 4225 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link); 4226 4227 if (filter->function == NULL) 4228 { 4229 _dbus_verbose (" filter was removed in a callback function\n"); 4230 link = next; 4231 continue; 4232 } 4233 4234 _dbus_verbose (" running filter on message %p\n", message); 4235 result = (* filter->function) (connection, message, filter->user_data); 4236 4237 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED) 4238 break; 4239 4240 link = next; 4241 } 4242 4243 _dbus_list_foreach (&filter_list_copy, 4244 (DBusForeachFunction)_dbus_message_filter_unref, 4245 NULL); 4246 _dbus_list_clear (&filter_list_copy); 4247 4248 CONNECTION_LOCK (connection); 4249 4250 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY) 4251 { 4252 _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME); 4253 goto out; 4254 } 4255 else if (result == DBUS_HANDLER_RESULT_HANDLED) 4256 { 4257 _dbus_verbose ("filter handled message in dispatch\n"); 4258 goto out; 4259 } 4260 4261 /* We're still protected from dispatch() reentrancy here 4262 * since we acquired the dispatcher 4263 */ 4264 _dbus_verbose (" running object path dispatch on message %p (%d %s %s '%s')\n", 4265 message, 4266 dbus_message_get_type (message), 4267 dbus_message_get_interface (message) ? 4268 dbus_message_get_interface (message) : 4269 "no interface", 4270 dbus_message_get_member (message) ? 4271 dbus_message_get_member (message) : 4272 "no member", 4273 dbus_message_get_signature (message)); 4274 4275 HAVE_LOCK_CHECK (connection); 4276 result = _dbus_object_tree_dispatch_and_unlock (connection->objects, 4277 message); 4278 4279 CONNECTION_LOCK (connection); 4280 4281 if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED) 4282 { 4283 _dbus_verbose ("object tree handled message in dispatch\n"); 4284 goto out; 4285 } 4286 4287 if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL) 4288 { 4289 DBusMessage *reply; 4290 DBusString str; 4291 DBusPreallocatedSend *preallocated; 4292 4293 _dbus_verbose (" sending error %s\n", 4294 DBUS_ERROR_UNKNOWN_METHOD); 4295 4296 if (!_dbus_string_init (&str)) 4297 { 4298 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 4299 _dbus_verbose ("no memory for error string in dispatch\n"); 4300 goto out; 4301 } 4302 4303 if (!_dbus_string_append_printf (&str, 4304 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n", 4305 dbus_message_get_member (message), 4306 dbus_message_get_signature (message), 4307 dbus_message_get_interface (message))) 4308 { 4309 _dbus_string_free (&str); 4310 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 4311 _dbus_verbose ("no memory for error string in dispatch\n"); 4312 goto out; 4313 } 4314 4315 reply = dbus_message_new_error (message, 4316 DBUS_ERROR_UNKNOWN_METHOD, 4317 _dbus_string_get_const_data (&str)); 4318 _dbus_string_free (&str); 4319 4320 if (reply == NULL) 4321 { 4322 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 4323 _dbus_verbose ("no memory for error reply in dispatch\n"); 4324 goto out; 4325 } 4326 4327 preallocated = _dbus_connection_preallocate_send_unlocked (connection); 4328 4329 if (preallocated == NULL) 4330 { 4331 dbus_message_unref (reply); 4332 result = DBUS_HANDLER_RESULT_NEED_MEMORY; 4333 _dbus_verbose ("no memory for error send in dispatch\n"); 4334 goto out; 4335 } 4336 4337 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated, 4338 reply, NULL); 4339 4340 dbus_message_unref (reply); 4341 4342 result = DBUS_HANDLER_RESULT_HANDLED; 4343 } 4344 4345 _dbus_verbose (" done dispatching %p (%d %s %s '%s') on connection %p\n", message, 4346 dbus_message_get_type (message), 4347 dbus_message_get_interface (message) ? 4348 dbus_message_get_interface (message) : 4349 "no interface", 4350 dbus_message_get_member (message) ? 4351 dbus_message_get_member (message) : 4352 "no member", 4353 dbus_message_get_signature (message), 4354 connection); 4355 4356 out: 4357 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY) 4358 { 4359 _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME); 4360 4361 /* Put message back, and we'll start over. 4362 * Yes this means handlers must be idempotent if they 4363 * don't return HANDLED; c'est la vie. 4364 */ 4365 _dbus_connection_putback_message_link_unlocked (connection, 4366 message_link); 4367 } 4368 else 4369 { 4370 _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME); 4371 4372 _dbus_list_free_link (message_link); 4373 dbus_message_unref (message); /* don't want the message to count in max message limits 4374 * in computing dispatch status below 4375 */ 4376 } 4377 4378 _dbus_connection_release_dispatch (connection); 4379 HAVE_LOCK_CHECK (connection); 4380 4381 _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME); 4382 status = _dbus_connection_get_dispatch_status_unlocked (connection); 4383 4384 /* unlocks and calls user code */ 4385 _dbus_connection_update_dispatch_status_and_unlock (connection, status); 4386 4387 dbus_connection_unref (connection); 4388 4389 return status; 4390} 4391 4392/** 4393 * Sets the watch functions for the connection. These functions are 4394 * responsible for making the application's main loop aware of file 4395 * descriptors that need to be monitored for events, using select() or 4396 * poll(). When using Qt, typically the DBusAddWatchFunction would 4397 * create a QSocketNotifier. When using GLib, the DBusAddWatchFunction 4398 * could call g_io_add_watch(), or could be used as part of a more 4399 * elaborate GSource. Note that when a watch is added, it may 4400 * not be enabled. 4401 * 4402 * The DBusWatchToggledFunction notifies the application that the 4403 * watch has been enabled or disabled. Call dbus_watch_get_enabled() 4404 * to check this. A disabled watch should have no effect, and enabled 4405 * watch should be added to the main loop. This feature is used 4406 * instead of simply adding/removing the watch because 4407 * enabling/disabling can be done without memory allocation. The 4408 * toggled function may be NULL if a main loop re-queries 4409 * dbus_watch_get_enabled() every time anyway. 4410 * 4411 * The DBusWatch can be queried for the file descriptor to watch using 4412 * dbus_watch_get_fd(), and for the events to watch for using 4413 * dbus_watch_get_flags(). The flags returned by 4414 * dbus_watch_get_flags() will only contain DBUS_WATCH_READABLE and 4415 * DBUS_WATCH_WRITABLE, never DBUS_WATCH_HANGUP or DBUS_WATCH_ERROR; 4416 * all watches implicitly include a watch for hangups, errors, and 4417 * other exceptional conditions. 4418 * 4419 * Once a file descriptor becomes readable or writable, or an exception 4420 * occurs, dbus_watch_handle() should be called to 4421 * notify the connection of the file descriptor's condition. 4422 * 4423 * dbus_watch_handle() cannot be called during the 4424 * DBusAddWatchFunction, as the connection will not be ready to handle 4425 * that watch yet. 4426 * 4427 * It is not allowed to reference a DBusWatch after it has been passed 4428 * to remove_function. 4429 * 4430 * If #FALSE is returned due to lack of memory, the failure may be due 4431 * to a #FALSE return from the new add_function. If so, the 4432 * add_function may have been called successfully one or more times, 4433 * but the remove_function will also have been called to remove any 4434 * successful adds. i.e. if #FALSE is returned the net result 4435 * should be that dbus_connection_set_watch_functions() has no effect, 4436 * but the add_function and remove_function may have been called. 4437 * 4438 * @todo We need to drop the lock when we call the 4439 * add/remove/toggled functions which can be a side effect 4440 * of setting the watch functions. 4441 * 4442 * @param connection the connection. 4443 * @param add_function function to begin monitoring a new descriptor. 4444 * @param remove_function function to stop monitoring a descriptor. 4445 * @param toggled_function function to notify of enable/disable 4446 * @param data data to pass to add_function and remove_function. 4447 * @param free_data_function function to be called to free the data. 4448 * @returns #FALSE on failure (no memory) 4449 */ 4450dbus_bool_t 4451dbus_connection_set_watch_functions (DBusConnection *connection, 4452 DBusAddWatchFunction add_function, 4453 DBusRemoveWatchFunction remove_function, 4454 DBusWatchToggledFunction toggled_function, 4455 void *data, 4456 DBusFreeFunction free_data_function) 4457{ 4458 dbus_bool_t retval; 4459 DBusWatchList *watches; 4460 4461 _dbus_return_val_if_fail (connection != NULL, FALSE); 4462 4463 CONNECTION_LOCK (connection); 4464 4465#ifndef DBUS_DISABLE_CHECKS 4466 if (connection->watches == NULL) 4467 { 4468 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n", 4469 _DBUS_FUNCTION_NAME); 4470 return FALSE; 4471 } 4472#endif 4473 4474 /* ref connection for slightly better reentrancy */ 4475 _dbus_connection_ref_unlocked (connection); 4476 4477 /* This can call back into user code, and we need to drop the 4478 * connection lock when it does. This is kind of a lame 4479 * way to do it. 4480 */ 4481 watches = connection->watches; 4482 connection->watches = NULL; 4483 CONNECTION_UNLOCK (connection); 4484 4485 retval = _dbus_watch_list_set_functions (watches, 4486 add_function, remove_function, 4487 toggled_function, 4488 data, free_data_function); 4489 CONNECTION_LOCK (connection); 4490 connection->watches = watches; 4491 4492 CONNECTION_UNLOCK (connection); 4493 /* drop our paranoid refcount */ 4494 dbus_connection_unref (connection); 4495 4496 return retval; 4497} 4498 4499/** 4500 * Sets the timeout functions for the connection. These functions are 4501 * responsible for making the application's main loop aware of timeouts. 4502 * When using Qt, typically the DBusAddTimeoutFunction would create a 4503 * QTimer. When using GLib, the DBusAddTimeoutFunction would call 4504 * g_timeout_add. 4505 * 4506 * The DBusTimeoutToggledFunction notifies the application that the 4507 * timeout has been enabled or disabled. Call 4508 * dbus_timeout_get_enabled() to check this. A disabled timeout should 4509 * have no effect, and enabled timeout should be added to the main 4510 * loop. This feature is used instead of simply adding/removing the 4511 * timeout because enabling/disabling can be done without memory 4512 * allocation. With Qt, QTimer::start() and QTimer::stop() can be used 4513 * to enable and disable. The toggled function may be NULL if a main 4514 * loop re-queries dbus_timeout_get_enabled() every time anyway. 4515 * Whenever a timeout is toggled, its interval may change. 4516 * 4517 * The DBusTimeout can be queried for the timer interval using 4518 * dbus_timeout_get_interval(). dbus_timeout_handle() should be called 4519 * repeatedly, each time the interval elapses, starting after it has 4520 * elapsed once. The timeout stops firing when it is removed with the 4521 * given remove_function. The timer interval may change whenever the 4522 * timeout is added, removed, or toggled. 4523 * 4524 * @param connection the connection. 4525 * @param add_function function to add a timeout. 4526 * @param remove_function function to remove a timeout. 4527 * @param toggled_function function to notify of enable/disable 4528 * @param data data to pass to add_function and remove_function. 4529 * @param free_data_function function to be called to free the data. 4530 * @returns #FALSE on failure (no memory) 4531 */ 4532dbus_bool_t 4533dbus_connection_set_timeout_functions (DBusConnection *connection, 4534 DBusAddTimeoutFunction add_function, 4535 DBusRemoveTimeoutFunction remove_function, 4536 DBusTimeoutToggledFunction toggled_function, 4537 void *data, 4538 DBusFreeFunction free_data_function) 4539{ 4540 dbus_bool_t retval; 4541 DBusTimeoutList *timeouts; 4542 4543 _dbus_return_val_if_fail (connection != NULL, FALSE); 4544 4545 CONNECTION_LOCK (connection); 4546 4547#ifndef DBUS_DISABLE_CHECKS 4548 if (connection->timeouts == NULL) 4549 { 4550 _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n", 4551 _DBUS_FUNCTION_NAME); 4552 return FALSE; 4553 } 4554#endif 4555 4556 /* ref connection for slightly better reentrancy */ 4557 _dbus_connection_ref_unlocked (connection); 4558 4559 timeouts = connection->timeouts; 4560 connection->timeouts = NULL; 4561 CONNECTION_UNLOCK (connection); 4562 4563 retval = _dbus_timeout_list_set_functions (timeouts, 4564 add_function, remove_function, 4565 toggled_function, 4566 data, free_data_function); 4567 CONNECTION_LOCK (connection); 4568 connection->timeouts = timeouts; 4569 4570 CONNECTION_UNLOCK (connection); 4571 /* drop our paranoid refcount */ 4572 dbus_connection_unref (connection); 4573 4574 return retval; 4575} 4576 4577/** 4578 * Sets the mainloop wakeup function for the connection. This function is 4579 * responsible for waking up the main loop (if its sleeping) when some some 4580 * change has happened to the connection that the mainloop needs to reconsider 4581 * (e.g. a message has been queued for writing). 4582 * When using Qt, this typically results in a call to QEventLoop::wakeUp(). 4583 * When using GLib, it would call g_main_context_wakeup(). 4584 * 4585 * 4586 * @param connection the connection. 4587 * @param wakeup_main_function function to wake up the mainloop 4588 * @param data data to pass wakeup_main_function 4589 * @param free_data_function function to be called to free the data. 4590 */ 4591void 4592dbus_connection_set_wakeup_main_function (DBusConnection *connection, 4593 DBusWakeupMainFunction wakeup_main_function, 4594 void *data, 4595 DBusFreeFunction free_data_function) 4596{ 4597 void *old_data; 4598 DBusFreeFunction old_free_data; 4599 4600 _dbus_return_if_fail (connection != NULL); 4601 4602 CONNECTION_LOCK (connection); 4603 old_data = connection->wakeup_main_data; 4604 old_free_data = connection->free_wakeup_main_data; 4605 4606 connection->wakeup_main_function = wakeup_main_function; 4607 connection->wakeup_main_data = data; 4608 connection->free_wakeup_main_data = free_data_function; 4609 4610 CONNECTION_UNLOCK (connection); 4611 4612 /* Callback outside the lock */ 4613 if (old_free_data) 4614 (*old_free_data) (old_data); 4615} 4616 4617/** 4618 * Set a function to be invoked when the dispatch status changes. 4619 * If the dispatch status is #DBUS_DISPATCH_DATA_REMAINS, then 4620 * dbus_connection_dispatch() needs to be called to process incoming 4621 * messages. However, dbus_connection_dispatch() MUST NOT BE CALLED 4622 * from inside the DBusDispatchStatusFunction. Indeed, almost 4623 * any reentrancy in this function is a bad idea. Instead, 4624 * the DBusDispatchStatusFunction should simply save an indication 4625 * that messages should be dispatched later, when the main loop 4626 * is re-entered. 4627 * 4628 * @param connection the connection 4629 * @param function function to call on dispatch status changes 4630 * @param data data for function 4631 * @param free_data_function free the function data 4632 */ 4633void 4634dbus_connection_set_dispatch_status_function (DBusConnection *connection, 4635 DBusDispatchStatusFunction function, 4636 void *data, 4637 DBusFreeFunction free_data_function) 4638{ 4639 void *old_data; 4640 DBusFreeFunction old_free_data; 4641 4642 _dbus_return_if_fail (connection != NULL); 4643 4644 CONNECTION_LOCK (connection); 4645 old_data = connection->dispatch_status_data; 4646 old_free_data = connection->free_dispatch_status_data; 4647 4648 connection->dispatch_status_function = function; 4649 connection->dispatch_status_data = data; 4650 connection->free_dispatch_status_data = free_data_function; 4651 4652 CONNECTION_UNLOCK (connection); 4653 4654 /* Callback outside the lock */ 4655 if (old_free_data) 4656 (*old_free_data) (old_data); 4657} 4658 4659/** 4660 * Get the UNIX file descriptor of the connection, if any. This can 4661 * be used for SELinux access control checks with getpeercon() for 4662 * example. DO NOT read or write to the file descriptor, or try to 4663 * select() on it; use DBusWatch for main loop integration. Not all 4664 * connections will have a file descriptor. So for adding descriptors 4665 * to the main loop, use dbus_watch_get_fd() and so forth. 4666 * 4667 * If the connection is socket-based, you can also use 4668 * dbus_connection_get_socket(), which will work on Windows too. 4669 * This function always fails on Windows. 4670 * 4671 * Right now the returned descriptor is always a socket, but 4672 * that is not guaranteed. 4673 * 4674 * @param connection the connection 4675 * @param fd return location for the file descriptor. 4676 * @returns #TRUE if fd is successfully obtained. 4677 */ 4678dbus_bool_t 4679dbus_connection_get_unix_fd (DBusConnection *connection, 4680 int *fd) 4681{ 4682 _dbus_return_val_if_fail (connection != NULL, FALSE); 4683 _dbus_return_val_if_fail (connection->transport != NULL, FALSE); 4684 4685#ifdef DBUS_WIN 4686 /* FIXME do this on a lower level */ 4687 return FALSE; 4688#endif 4689 4690 return dbus_connection_get_socket(connection, fd); 4691} 4692 4693/** 4694 * Gets the underlying Windows or UNIX socket file descriptor 4695 * of the connection, if any. DO NOT read or write to the file descriptor, or try to 4696 * select() on it; use DBusWatch for main loop integration. Not all 4697 * connections will have a socket. So for adding descriptors 4698 * to the main loop, use dbus_watch_get_fd() and so forth. 4699 * 4700 * If the connection is not socket-based, this function will return FALSE, 4701 * even if the connection does have a file descriptor of some kind. 4702 * i.e. this function always returns specifically a socket file descriptor. 4703 * 4704 * @param connection the connection 4705 * @param fd return location for the file descriptor. 4706 * @returns #TRUE if fd is successfully obtained. 4707 */ 4708dbus_bool_t 4709dbus_connection_get_socket(DBusConnection *connection, 4710 int *fd) 4711{ 4712 dbus_bool_t retval; 4713 4714 _dbus_return_val_if_fail (connection != NULL, FALSE); 4715 _dbus_return_val_if_fail (connection->transport != NULL, FALSE); 4716 4717 CONNECTION_LOCK (connection); 4718 4719 retval = _dbus_transport_get_socket_fd (connection->transport, 4720 fd); 4721 4722 CONNECTION_UNLOCK (connection); 4723 4724 return retval; 4725} 4726 4727 4728/** 4729 * Gets the UNIX user ID of the connection if any. 4730 * Returns #TRUE if the uid is filled in. 4731 * Always returns #FALSE on non-UNIX platforms. 4732 * Always returns #FALSE prior to authenticating the 4733 * connection. 4734 * 4735 * @param connection the connection 4736 * @param uid return location for the user ID 4737 * @returns #TRUE if uid is filled in with a valid user ID 4738 */ 4739dbus_bool_t 4740dbus_connection_get_unix_user (DBusConnection *connection, 4741 unsigned long *uid) 4742{ 4743 dbus_bool_t result; 4744 4745 _dbus_return_val_if_fail (connection != NULL, FALSE); 4746 _dbus_return_val_if_fail (uid != NULL, FALSE); 4747 4748#ifdef DBUS_WIN 4749 /* FIXME this should be done at a lower level, but it's kind of hard, 4750 * just want to be sure we don't ship with this API returning 4751 * some weird internal fake uid for 1.0 4752 */ 4753 return FALSE; 4754#endif 4755 4756 CONNECTION_LOCK (connection); 4757 4758 if (!_dbus_transport_get_is_authenticated (connection->transport)) 4759 result = FALSE; 4760 else 4761 result = _dbus_transport_get_unix_user (connection->transport, 4762 uid); 4763 CONNECTION_UNLOCK (connection); 4764 4765 return result; 4766} 4767 4768/** 4769 * Gets the process ID of the connection if any. 4770 * Returns #TRUE if the uid is filled in. 4771 * Always returns #FALSE prior to authenticating the 4772 * connection. 4773 * 4774 * @param connection the connection 4775 * @param pid return location for the process ID 4776 * @returns #TRUE if uid is filled in with a valid process ID 4777 */ 4778dbus_bool_t 4779dbus_connection_get_unix_process_id (DBusConnection *connection, 4780 unsigned long *pid) 4781{ 4782 dbus_bool_t result; 4783 4784 _dbus_return_val_if_fail (connection != NULL, FALSE); 4785 _dbus_return_val_if_fail (pid != NULL, FALSE); 4786 4787#ifdef DBUS_WIN 4788 /* FIXME this should be done at a lower level, but it's kind of hard, 4789 * just want to be sure we don't ship with this API returning 4790 * some weird internal fake uid for 1.0 4791 */ 4792 return FALSE; 4793#endif 4794 4795 CONNECTION_LOCK (connection); 4796 4797 if (!_dbus_transport_get_is_authenticated (connection->transport)) 4798 result = FALSE; 4799 else 4800 result = _dbus_transport_get_unix_process_id (connection->transport, 4801 pid); 4802 CONNECTION_UNLOCK (connection); 4803 4804 return result; 4805} 4806 4807/** 4808 * Sets a predicate function used to determine whether a given user ID 4809 * is allowed to connect. When an incoming connection has 4810 * authenticated with a particular user ID, this function is called; 4811 * if it returns #TRUE, the connection is allowed to proceed, 4812 * otherwise the connection is disconnected. 4813 * 4814 * If the function is set to #NULL (as it is by default), then 4815 * only the same UID as the server process will be allowed to 4816 * connect. 4817 * 4818 * On Windows, the function will be set and its free_data_function will 4819 * be invoked when the connection is freed or a new function is set. 4820 * However, the function will never be called, because there are 4821 * no UNIX user ids to pass to it. 4822 * 4823 * @todo add a Windows API analogous to dbus_connection_set_unix_user_function() 4824 * 4825 * @param connection the connection 4826 * @param function the predicate 4827 * @param data data to pass to the predicate 4828 * @param free_data_function function to free the data 4829 */ 4830void 4831dbus_connection_set_unix_user_function (DBusConnection *connection, 4832 DBusAllowUnixUserFunction function, 4833 void *data, 4834 DBusFreeFunction free_data_function) 4835{ 4836 void *old_data = NULL; 4837 DBusFreeFunction old_free_function = NULL; 4838 4839 _dbus_return_if_fail (connection != NULL); 4840 4841 CONNECTION_LOCK (connection); 4842 _dbus_transport_set_unix_user_function (connection->transport, 4843 function, data, free_data_function, 4844 &old_data, &old_free_function); 4845 CONNECTION_UNLOCK (connection); 4846 4847 if (old_free_function != NULL) 4848 (* old_free_function) (old_data); 4849} 4850 4851/** 4852 * 4853 * Normally #DBusConnection automatically handles all messages to the 4854 * org.freedesktop.DBus.Peer interface. However, the message bus wants 4855 * to be able to route methods on that interface through the bus and 4856 * to other applications. If routing peer messages is enabled, then 4857 * messages with the org.freedesktop.DBus.Peer interface that also 4858 * have a bus destination name set will not be automatically 4859 * handled by the #DBusConnection and instead will be dispatched 4860 * normally to the application. 4861 * 4862 * 4863 * If a normal application sets this flag, it can break things badly. 4864 * So don't set this unless you are the message bus. 4865 * 4866 * @param connection the connection 4867 * @param value #TRUE to pass through org.freedesktop.DBus.Peer messages with a bus name set 4868 */ 4869void 4870dbus_connection_set_route_peer_messages (DBusConnection *connection, 4871 dbus_bool_t value) 4872{ 4873 _dbus_return_if_fail (connection != NULL); 4874 4875 CONNECTION_LOCK (connection); 4876 connection->route_peer_messages = TRUE; 4877 CONNECTION_UNLOCK (connection); 4878} 4879 4880/** 4881 * Adds a message filter. Filters are handlers that are run on all 4882 * incoming messages, prior to the objects registered with 4883 * dbus_connection_register_object_path(). Filters are run in the 4884 * order that they were added. The same handler can be added as a 4885 * filter more than once, in which case it will be run more than once. 4886 * Filters added during a filter callback won't be run on the message 4887 * being processed. 4888 * 4889 * @todo we don't run filters on messages while blocking without 4890 * entering the main loop, since filters are run as part of 4891 * dbus_connection_dispatch(). This is probably a feature, as filters 4892 * could create arbitrary reentrancy. But kind of sucks if you're 4893 * trying to filter METHOD_RETURN for some reason. 4894 * 4895 * @param connection the connection 4896 * @param function function to handle messages 4897 * @param user_data user data to pass to the function 4898 * @param free_data_function function to use for freeing user data 4899 * @returns #TRUE on success, #FALSE if not enough memory. 4900 */ 4901dbus_bool_t 4902dbus_connection_add_filter (DBusConnection *connection, 4903 DBusHandleMessageFunction function, 4904 void *user_data, 4905 DBusFreeFunction free_data_function) 4906{ 4907 DBusMessageFilter *filter; 4908 4909 _dbus_return_val_if_fail (connection != NULL, FALSE); 4910 _dbus_return_val_if_fail (function != NULL, FALSE); 4911 4912 filter = dbus_new0 (DBusMessageFilter, 1); 4913 if (filter == NULL) 4914 return FALSE; 4915 4916 filter->refcount.value = 1; 4917 4918 CONNECTION_LOCK (connection); 4919 4920 if (!_dbus_list_append (&connection->filter_list, 4921 filter)) 4922 { 4923 _dbus_message_filter_unref (filter); 4924 CONNECTION_UNLOCK (connection); 4925 return FALSE; 4926 } 4927 4928 /* Fill in filter after all memory allocated, 4929 * so we don't run the free_user_data_function 4930 * if the add_filter() fails 4931 */ 4932 4933 filter->function = function; 4934 filter->user_data = user_data; 4935 filter->free_user_data_function = free_data_function; 4936 4937 CONNECTION_UNLOCK (connection); 4938 return TRUE; 4939} 4940 4941/** 4942 * Removes a previously-added message filter. It is a programming 4943 * error to call this function for a handler that has not been added 4944 * as a filter. If the given handler was added more than once, only 4945 * one instance of it will be removed (the most recently-added 4946 * instance). 4947 * 4948 * @param connection the connection 4949 * @param function the handler to remove 4950 * @param user_data user data for the handler to remove 4951 * 4952 */ 4953void 4954dbus_connection_remove_filter (DBusConnection *connection, 4955 DBusHandleMessageFunction function, 4956 void *user_data) 4957{ 4958 DBusList *link; 4959 DBusMessageFilter *filter; 4960 4961 _dbus_return_if_fail (connection != NULL); 4962 _dbus_return_if_fail (function != NULL); 4963 4964 CONNECTION_LOCK (connection); 4965 4966 filter = NULL; 4967 4968 link = _dbus_list_get_last_link (&connection->filter_list); 4969 while (link != NULL) 4970 { 4971 filter = link->data; 4972 4973 if (filter->function == function && 4974 filter->user_data == user_data) 4975 { 4976 _dbus_list_remove_link (&connection->filter_list, link); 4977 filter->function = NULL; 4978 4979 break; 4980 } 4981 4982 link = _dbus_list_get_prev_link (&connection->filter_list, link); 4983 } 4984 4985 CONNECTION_UNLOCK (connection); 4986 4987#ifndef DBUS_DISABLE_CHECKS 4988 if (filter == NULL) 4989 { 4990 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added\n", 4991 function, user_data); 4992 return; 4993 } 4994#endif 4995 4996 /* Call application code */ 4997 if (filter->free_user_data_function) 4998 (* filter->free_user_data_function) (filter->user_data); 4999 5000 filter->free_user_data_function = NULL; 5001 filter->user_data = NULL; 5002 5003 _dbus_message_filter_unref (filter); 5004} 5005 5006/** 5007 * Registers a handler for a given path in the object hierarchy. 5008 * The given vtable handles messages sent to exactly the given path. 5009 * 5010 * 5011 * @param connection the connection 5012 * @param path a '/' delimited string of path elements 5013 * @param vtable the virtual table 5014 * @param user_data data to pass to functions in the vtable 5015 * @returns #FALSE if not enough memory 5016 */ 5017dbus_bool_t 5018dbus_connection_register_object_path (DBusConnection *connection, 5019 const char *path, 5020 const DBusObjectPathVTable *vtable, 5021 void *user_data) 5022{ 5023 char **decomposed_path; 5024 dbus_bool_t retval; 5025 5026 _dbus_return_val_if_fail (connection != NULL, FALSE); 5027 _dbus_return_val_if_fail (path != NULL, FALSE); 5028 _dbus_return_val_if_fail (path[0] == '/', FALSE); 5029 _dbus_return_val_if_fail (vtable != NULL, FALSE); 5030 5031 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 5032 return FALSE; 5033 5034 CONNECTION_LOCK (connection); 5035 5036 retval = _dbus_object_tree_register (connection->objects, 5037 FALSE, 5038 (const char **) decomposed_path, vtable, 5039 user_data); 5040 5041 CONNECTION_UNLOCK (connection); 5042 5043 dbus_free_string_array (decomposed_path); 5044 5045 return retval; 5046} 5047 5048/** 5049 * Registers a fallback handler for a given subsection of the object 5050 * hierarchy. The given vtable handles messages at or below the given 5051 * path. You can use this to establish a default message handling 5052 * policy for a whole "subdirectory." 5053 * 5054 * @param connection the connection 5055 * @param path a '/' delimited string of path elements 5056 * @param vtable the virtual table 5057 * @param user_data data to pass to functions in the vtable 5058 * @returns #FALSE if not enough memory 5059 */ 5060dbus_bool_t 5061dbus_connection_register_fallback (DBusConnection *connection, 5062 const char *path, 5063 const DBusObjectPathVTable *vtable, 5064 void *user_data) 5065{ 5066 char **decomposed_path; 5067 dbus_bool_t retval; 5068 5069 _dbus_return_val_if_fail (connection != NULL, FALSE); 5070 _dbus_return_val_if_fail (path != NULL, FALSE); 5071 _dbus_return_val_if_fail (path[0] == '/', FALSE); 5072 _dbus_return_val_if_fail (vtable != NULL, FALSE); 5073 5074 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 5075 return FALSE; 5076 5077 CONNECTION_LOCK (connection); 5078 5079 retval = _dbus_object_tree_register (connection->objects, 5080 TRUE, 5081 (const char **) decomposed_path, vtable, 5082 user_data); 5083 5084 CONNECTION_UNLOCK (connection); 5085 5086 dbus_free_string_array (decomposed_path); 5087 5088 return retval; 5089} 5090 5091/** 5092 * Unregisters the handler registered with exactly the given path. 5093 * It's a bug to call this function for a path that isn't registered. 5094 * Can unregister both fallback paths and object paths. 5095 * 5096 * @param connection the connection 5097 * @param path a '/' delimited string of path elements 5098 * @returns #FALSE if not enough memory 5099 */ 5100dbus_bool_t 5101dbus_connection_unregister_object_path (DBusConnection *connection, 5102 const char *path) 5103{ 5104 char **decomposed_path; 5105 5106 _dbus_return_val_if_fail (connection != NULL, FALSE); 5107 _dbus_return_val_if_fail (path != NULL, FALSE); 5108 _dbus_return_val_if_fail (path[0] == '/', FALSE); 5109 5110 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 5111 return FALSE; 5112 5113 CONNECTION_LOCK (connection); 5114 5115 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path); 5116 5117 dbus_free_string_array (decomposed_path); 5118 5119 return TRUE; 5120} 5121 5122/** 5123 * Gets the user data passed to dbus_connection_register_object_path() 5124 * or dbus_connection_register_fallback(). If nothing was registered 5125 * at this path, the data is filled in with #NULL. 5126 * 5127 * @param connection the connection 5128 * @param path the path you registered with 5129 * @param data_p location to store the user data, or #NULL 5130 * @returns #FALSE if not enough memory 5131 */ 5132dbus_bool_t 5133dbus_connection_get_object_path_data (DBusConnection *connection, 5134 const char *path, 5135 void **data_p) 5136{ 5137 char **decomposed_path; 5138 5139 _dbus_return_val_if_fail (connection != NULL, FALSE); 5140 _dbus_return_val_if_fail (path != NULL, FALSE); 5141 _dbus_return_val_if_fail (data_p != NULL, FALSE); 5142 5143 *data_p = NULL; 5144 5145 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL)) 5146 return FALSE; 5147 5148 CONNECTION_LOCK (connection); 5149 5150 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path); 5151 5152 CONNECTION_UNLOCK (connection); 5153 5154 dbus_free_string_array (decomposed_path); 5155 5156 return TRUE; 5157} 5158 5159/** 5160 * Lists the registered fallback handlers and object path handlers at 5161 * the given parent_path. The returned array should be freed with 5162 * dbus_free_string_array(). 5163 * 5164 * @param connection the connection 5165 * @param parent_path the path to list the child handlers of 5166 * @param child_entries returns #NULL-terminated array of children 5167 * @returns #FALSE if no memory to allocate the child entries 5168 */ 5169dbus_bool_t 5170dbus_connection_list_registered (DBusConnection *connection, 5171 const char *parent_path, 5172 char ***child_entries) 5173{ 5174 char **decomposed_path; 5175 dbus_bool_t retval; 5176 _dbus_return_val_if_fail (connection != NULL, FALSE); 5177 _dbus_return_val_if_fail (parent_path != NULL, FALSE); 5178 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE); 5179 _dbus_return_val_if_fail (child_entries != NULL, FALSE); 5180 5181 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL)) 5182 return FALSE; 5183 5184 CONNECTION_LOCK (connection); 5185 5186 retval = _dbus_object_tree_list_registered_and_unlock (connection->objects, 5187 (const char **) decomposed_path, 5188 child_entries); 5189 dbus_free_string_array (decomposed_path); 5190 5191 return retval; 5192} 5193 5194static DBusDataSlotAllocator slot_allocator; 5195_DBUS_DEFINE_GLOBAL_LOCK (connection_slots); 5196 5197/** 5198 * Allocates an integer ID to be used for storing application-specific 5199 * data on any DBusConnection. The allocated ID may then be used 5200 * with dbus_connection_set_data() and dbus_connection_get_data(). 5201 * The passed-in slot must be initialized to -1, and is filled in 5202 * with the slot ID. If the passed-in slot is not -1, it's assumed 5203 * to be already allocated, and its refcount is incremented. 5204 * 5205 * The allocated slot is global, i.e. all DBusConnection objects will 5206 * have a slot with the given integer ID reserved. 5207 * 5208 * @param slot_p address of a global variable storing the slot 5209 * @returns #FALSE on failure (no memory) 5210 */ 5211dbus_bool_t 5212dbus_connection_allocate_data_slot (dbus_int32_t *slot_p) 5213{ 5214 return _dbus_data_slot_allocator_alloc (&slot_allocator, 5215 &_DBUS_LOCK_NAME (connection_slots), 5216 slot_p); 5217} 5218 5219/** 5220 * Deallocates a global ID for connection data slots. 5221 * dbus_connection_get_data() and dbus_connection_set_data() may no 5222 * longer be used with this slot. Existing data stored on existing 5223 * DBusConnection objects will be freed when the connection is 5224 * finalized, but may not be retrieved (and may only be replaced if 5225 * someone else reallocates the slot). When the refcount on the 5226 * passed-in slot reaches 0, it is set to -1. 5227 * 5228 * @param slot_p address storing the slot to deallocate 5229 */ 5230void 5231dbus_connection_free_data_slot (dbus_int32_t *slot_p) 5232{ 5233 _dbus_return_if_fail (*slot_p >= 0); 5234 5235 _dbus_data_slot_allocator_free (&slot_allocator, slot_p); 5236} 5237 5238/** 5239 * Stores a pointer on a DBusConnection, along 5240 * with an optional function to be used for freeing 5241 * the data when the data is set again, or when 5242 * the connection is finalized. The slot number 5243 * must have been allocated with dbus_connection_allocate_data_slot(). 5244 * 5245 * @param connection the connection 5246 * @param slot the slot number 5247 * @param data the data to store 5248 * @param free_data_func finalizer function for the data 5249 * @returns #TRUE if there was enough memory to store the data 5250 */ 5251dbus_bool_t 5252dbus_connection_set_data (DBusConnection *connection, 5253 dbus_int32_t slot, 5254 void *data, 5255 DBusFreeFunction free_data_func) 5256{ 5257 DBusFreeFunction old_free_func; 5258 void *old_data; 5259 dbus_bool_t retval; 5260 5261 _dbus_return_val_if_fail (connection != NULL, FALSE); 5262 _dbus_return_val_if_fail (slot >= 0, FALSE); 5263 5264 CONNECTION_LOCK (connection); 5265 5266 retval = _dbus_data_slot_list_set (&slot_allocator, 5267 &connection->slot_list, 5268 slot, data, free_data_func, 5269 &old_free_func, &old_data); 5270 5271 CONNECTION_UNLOCK (connection); 5272 5273 if (retval) 5274 { 5275 /* Do the actual free outside the connection lock */ 5276 if (old_free_func) 5277 (* old_free_func) (old_data); 5278 } 5279 5280 return retval; 5281} 5282 5283/** 5284 * Retrieves data previously set with dbus_connection_set_data(). 5285 * The slot must still be allocated (must not have been freed). 5286 * 5287 * @param connection the connection 5288 * @param slot the slot to get data from 5289 * @returns the data, or #NULL if not found 5290 */ 5291void* 5292dbus_connection_get_data (DBusConnection *connection, 5293 dbus_int32_t slot) 5294{ 5295 void *res; 5296 5297 _dbus_return_val_if_fail (connection != NULL, NULL); 5298 5299 CONNECTION_LOCK (connection); 5300 5301 res = _dbus_data_slot_list_get (&slot_allocator, 5302 &connection->slot_list, 5303 slot); 5304 5305 CONNECTION_UNLOCK (connection); 5306 5307 return res; 5308} 5309 5310/** 5311 * This function sets a global flag for whether dbus_connection_new() 5312 * will set SIGPIPE behavior to SIG_IGN. 5313 * 5314 * @param will_modify_sigpipe #TRUE to allow sigpipe to be set to SIG_IGN 5315 */ 5316void 5317dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe) 5318{ 5319 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE; 5320} 5321 5322/** 5323 * Specifies the maximum size message this connection is allowed to 5324 * receive. Larger messages will result in disconnecting the 5325 * connection. 5326 * 5327 * @param connection a #DBusConnection 5328 * @param size maximum message size the connection can receive, in bytes 5329 */ 5330void 5331dbus_connection_set_max_message_size (DBusConnection *connection, 5332 long size) 5333{ 5334 _dbus_return_if_fail (connection != NULL); 5335 5336 CONNECTION_LOCK (connection); 5337 _dbus_transport_set_max_message_size (connection->transport, 5338 size); 5339 CONNECTION_UNLOCK (connection); 5340} 5341 5342/** 5343 * Gets the value set by dbus_connection_set_max_message_size(). 5344 * 5345 * @param connection the connection 5346 * @returns the max size of a single message 5347 */ 5348long 5349dbus_connection_get_max_message_size (DBusConnection *connection) 5350{ 5351 long res; 5352 5353 _dbus_return_val_if_fail (connection != NULL, 0); 5354 5355 CONNECTION_LOCK (connection); 5356 res = _dbus_transport_get_max_message_size (connection->transport); 5357 CONNECTION_UNLOCK (connection); 5358 return res; 5359} 5360 5361/** 5362 * Sets the maximum total number of bytes that can be used for all messages 5363 * received on this connection. Messages count toward the maximum until 5364 * they are finalized. When the maximum is reached, the connection will 5365 * not read more data until some messages are finalized. 5366 * 5367 * The semantics of the maximum are: if outstanding messages are 5368 * already above the maximum, additional messages will not be read. 5369 * The semantics are not: if the next message would cause us to exceed 5370 * the maximum, we don't read it. The reason is that we don't know the 5371 * size of a message until after we read it. 5372 * 5373 * Thus, the max live messages size can actually be exceeded 5374 * by up to the maximum size of a single message. 5375 * 5376 * Also, if we read say 1024 bytes off the wire in a single read(), 5377 * and that contains a half-dozen small messages, we may exceed the 5378 * size max by that amount. But this should be inconsequential. 5379 * 5380 * This does imply that we can't call read() with a buffer larger 5381 * than we're willing to exceed this limit by. 5382 * 5383 * @param connection the connection 5384 * @param size the maximum size in bytes of all outstanding messages 5385 */ 5386void 5387dbus_connection_set_max_received_size (DBusConnection *connection, 5388 long size) 5389{ 5390 _dbus_return_if_fail (connection != NULL); 5391 5392 CONNECTION_LOCK (connection); 5393 _dbus_transport_set_max_received_size (connection->transport, 5394 size); 5395 CONNECTION_UNLOCK (connection); 5396} 5397 5398/** 5399 * Gets the value set by dbus_connection_set_max_received_size(). 5400 * 5401 * @param connection the connection 5402 * @returns the max size of all live messages 5403 */ 5404long 5405dbus_connection_get_max_received_size (DBusConnection *connection) 5406{ 5407 long res; 5408 5409 _dbus_return_val_if_fail (connection != NULL, 0); 5410 5411 CONNECTION_LOCK (connection); 5412 res = _dbus_transport_get_max_received_size (connection->transport); 5413 CONNECTION_UNLOCK (connection); 5414 return res; 5415} 5416 5417/** 5418 * Gets the approximate size in bytes of all messages in the outgoing 5419 * message queue. The size is approximate in that you shouldn't use 5420 * it to decide how many bytes to read off the network or anything 5421 * of that nature, as optimizations may choose to tell small white lies 5422 * to avoid performance overhead. 5423 * 5424 * @param connection the connection 5425 * @returns the number of bytes that have been queued up but not sent 5426 */ 5427long 5428dbus_connection_get_outgoing_size (DBusConnection *connection) 5429{ 5430 long res; 5431 5432 _dbus_return_val_if_fail (connection != NULL, 0); 5433 5434 CONNECTION_LOCK (connection); 5435 res = _dbus_counter_get_value (connection->outgoing_counter); 5436 CONNECTION_UNLOCK (connection); 5437 return res; 5438} 5439 5440/** 5441 * Obtains the machine UUID of the machine this process is running on. 5442 * 5443 * The returned string must be freed with dbus_free(). 5444 * 5445 * This UUID is guaranteed to remain the same until the next reboot 5446 * (unless the sysadmin foolishly changes it and screws themselves). 5447 * It will usually remain the same across reboots also, but hardware 5448 * configuration changes or rebuilding the machine could break that. 5449 * 5450 * The idea is that two processes with the same machine ID should be 5451 * able to use shared memory, UNIX domain sockets, process IDs, and other 5452 * features of the OS that require both processes to be running 5453 * on the same OS kernel instance. 5454 * 5455 * The machine ID can also be used to create unique per-machine 5456 * instances. For example, you could use it in bus names or 5457 * X selection names. 5458 * 5459 * The machine ID is preferred over the machine hostname, because 5460 * the hostname is frequently set to "localhost.localdomain" and 5461 * may also change at runtime. 5462 * 5463 * You can get the machine ID of a remote application by invoking the 5464 * method GetMachineId from interface org.freedesktop.DBus.Peer. 5465 * 5466 * If the remote application has the same machine ID as the one 5467 * returned by this function, then the remote application is on the 5468 * same machine as your application. 5469 * 5470 * @returns a 32-byte-long hex-encoded UUID string, or #NULL if insufficient memory 5471 */ 5472char* 5473dbus_get_local_machine_id (void) 5474{ 5475 DBusString uuid; 5476 char *s; 5477 5478 s = NULL; 5479 _dbus_string_init (&uuid); 5480 if (!_dbus_get_local_machine_uuid_encoded (&uuid) || 5481 !_dbus_string_steal_data (&uuid, &s)) 5482 { 5483 _dbus_string_free (&uuid); 5484 return FALSE; 5485 } 5486 else 5487 { 5488 _dbus_string_free (&uuid); 5489 return s; 5490 } 5491 5492} 5493 5494/** @} */ 5495