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