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