1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 2 /* dbus-pending-call.c Object representing a call in progress. 3 * 4 * Copyright (C) 2002, 2003 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24 #include <config.h> 25 #include "dbus-internals.h" 26 #include "dbus-connection-internal.h" 27 #include "dbus-pending-call-internal.h" 28 #include "dbus-pending-call.h" 29 #include "dbus-list.h" 30 #include "dbus-threads.h" 31 #include "dbus-test.h" 32 33 /** 34 * @defgroup DBusPendingCallInternals DBusPendingCall implementation details 35 * @ingroup DBusInternals 36 * @brief DBusPendingCall private implementation details. 37 * 38 * The guts of DBusPendingCall and its methods. 39 * 40 * @{ 41 */ 42 43 /** 44 * @brief Internals of DBusPendingCall 45 * 46 * Opaque object representing a reply message that we're waiting for. 47 */ 48 49 /** 50 * shorter and more visible way to write _dbus_connection_lock() 51 */ 52 #define CONNECTION_LOCK(connection) _dbus_connection_lock(connection) 53 /** 54 * shorter and more visible way to write _dbus_connection_unlock() 55 */ 56 #define CONNECTION_UNLOCK(connection) _dbus_connection_unlock(connection) 57 58 /** 59 * Implementation details of #DBusPendingCall - all fields are private. 60 */ 61 struct DBusPendingCall 62 { 63 DBusAtomic refcount; /**< reference count */ 64 65 DBusDataSlotList slot_list; /**< Data stored by allocated integer ID */ 66 67 DBusPendingCallNotifyFunction function; /**< Notifier when reply arrives. */ 68 69 DBusConnection *connection; /**< Connections we're associated with */ 70 DBusMessage *reply; /**< Reply (after we've received it) */ 71 DBusTimeout *timeout; /**< Timeout */ 72 73 DBusList *timeout_link; /**< Preallocated timeout response */ 74 75 dbus_uint32_t reply_serial; /**< Expected serial of reply */ 76 77 unsigned int completed : 1; /**< TRUE if completed */ 78 unsigned int timeout_added : 1; /**< Have added the timeout */ 79 }; 80 81 static dbus_int32_t notify_user_data_slot = -1; 82 83 /** 84 * Creates a new pending reply object. 85 * 86 * @param connection connection where reply will arrive 87 * @param timeout_milliseconds length of timeout, -1 for default, INT_MAX for no timeout 88 * @param timeout_handler timeout handler, takes pending call as data 89 * @returns a new #DBusPendingCall or #NULL if no memory. 90 */ 91 DBusPendingCall* 92 _dbus_pending_call_new_unlocked (DBusConnection *connection, 93 int timeout_milliseconds, 94 DBusTimeoutHandler timeout_handler) 95 { 96 DBusPendingCall *pending; 97 DBusTimeout *timeout; 98 99 _dbus_assert (timeout_milliseconds >= 0 || timeout_milliseconds == -1); 100 101 if (timeout_milliseconds == -1) 102 timeout_milliseconds = _DBUS_DEFAULT_TIMEOUT_VALUE; 103 104 if (!dbus_pending_call_allocate_data_slot (¬ify_user_data_slot)) 105 return NULL; 106 107 pending = dbus_new0 (DBusPendingCall, 1); 108 109 if (pending == NULL) 110 { 111 dbus_pending_call_free_data_slot (¬ify_user_data_slot); 112 return NULL; 113 } 114 115 if (timeout_milliseconds != _DBUS_INT_MAX) 116 { 117 timeout = _dbus_timeout_new (timeout_milliseconds, 118 timeout_handler, 119 pending, NULL); 120 121 if (timeout == NULL) 122 { 123 dbus_pending_call_free_data_slot (¬ify_user_data_slot); 124 dbus_free (pending); 125 return NULL; 126 } 127 128 pending->timeout = timeout; 129 } 130 else 131 { 132 pending->timeout = NULL; 133 } 134 135 _dbus_atomic_inc (&pending->refcount); 136 pending->connection = connection; 137 _dbus_connection_ref_unlocked (pending->connection); 138 139 _dbus_data_slot_list_init (&pending->slot_list); 140 141 return pending; 142 } 143 144 /** 145 * Sets the reply of a pending call with the given message, 146 * or if the message is #NULL, by timing out the pending call. 147 * 148 * @param pending the pending call 149 * @param message the message to complete the call with, or #NULL 150 * to time out the call 151 */ 152 void 153 _dbus_pending_call_set_reply_unlocked (DBusPendingCall *pending, 154 DBusMessage *message) 155 { 156 if (message == NULL) 157 { 158 message = pending->timeout_link->data; 159 _dbus_list_clear (&pending->timeout_link); 160 } 161 else 162 dbus_message_ref (message); 163 164 _dbus_verbose (" handing message %p (%s) to pending call serial %u\n", 165 message, 166 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_RETURN ? 167 "method return" : 168 dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_ERROR ? 169 "error" : "other type", 170 pending->reply_serial); 171 172 _dbus_assert (pending->reply == NULL); 173 _dbus_assert (pending->reply_serial == dbus_message_get_reply_serial (message)); 174 pending->reply = message; 175 } 176 177 /** 178 * Calls notifier function for the pending call 179 * and sets the call to completed. 180 * 181 * @param pending the pending call 182 * 183 */ 184 void 185 _dbus_pending_call_complete (DBusPendingCall *pending) 186 { 187 _dbus_assert (!pending->completed); 188 189 pending->completed = TRUE; 190 191 if (pending->function) 192 { 193 void *user_data; 194 user_data = dbus_pending_call_get_data (pending, 195 notify_user_data_slot); 196 197 (* pending->function) (pending, user_data); 198 } 199 } 200 201 /** 202 * If the pending call hasn't been timed out, add its timeout 203 * error reply to the connection's incoming message queue. 204 * 205 * @param pending the pending call 206 * @param connection the connection the call was sent to 207 */ 208 void 209 _dbus_pending_call_queue_timeout_error_unlocked (DBusPendingCall *pending, 210 DBusConnection *connection) 211 { 212 _dbus_assert (connection == pending->connection); 213 214 if (pending->timeout_link) 215 { 216 _dbus_connection_queue_synthesized_message_link (connection, 217 pending->timeout_link); 218 pending->timeout_link = NULL; 219 } 220 } 221 222 /** 223 * Checks to see if a timeout has been added 224 * 225 * @param pending the pending_call 226 * @returns #TRUE if there is a timeout or #FALSE if not 227 */ 228 dbus_bool_t 229 _dbus_pending_call_is_timeout_added_unlocked (DBusPendingCall *pending) 230 { 231 _dbus_assert (pending != NULL); 232 233 return pending->timeout_added; 234 } 235 236 237 /** 238 * Sets wether the timeout has been added 239 * 240 * @param pending the pending_call 241 * @param is_added whether or not a timeout is added 242 */ 243 void 244 _dbus_pending_call_set_timeout_added_unlocked (DBusPendingCall *pending, 245 dbus_bool_t is_added) 246 { 247 _dbus_assert (pending != NULL); 248 249 pending->timeout_added = is_added; 250 } 251 252 253 /** 254 * Retrives the timeout 255 * 256 * @param pending the pending_call 257 * @returns a timeout object or NULL if call has no timeout 258 */ 259 DBusTimeout * 260 _dbus_pending_call_get_timeout_unlocked (DBusPendingCall *pending) 261 { 262 _dbus_assert (pending != NULL); 263 264 return pending->timeout; 265 } 266 267 /** 268 * Gets the reply's serial number 269 * 270 * @param pending the pending_call 271 * @returns a serial number for the reply or 0 272 */ 273 dbus_uint32_t 274 _dbus_pending_call_get_reply_serial_unlocked (DBusPendingCall *pending) 275 { 276 _dbus_assert (pending != NULL); 277 278 return pending->reply_serial; 279 } 280 281 /** 282 * Sets the reply's serial number 283 * 284 * @param pending the pending_call 285 * @param serial the serial number 286 */ 287 void 288 _dbus_pending_call_set_reply_serial_unlocked (DBusPendingCall *pending, 289 dbus_uint32_t serial) 290 { 291 _dbus_assert (pending != NULL); 292 _dbus_assert (pending->reply_serial == 0); 293 294 pending->reply_serial = serial; 295 } 296 297 /** 298 * Gets the connection associated with this pending call. 299 * 300 * @param pending the pending_call 301 * @returns the connection associated with the pending call 302 */ 303 DBusConnection * 304 _dbus_pending_call_get_connection_and_lock (DBusPendingCall *pending) 305 { 306 _dbus_assert (pending != NULL); 307 308 CONNECTION_LOCK (pending->connection); 309 return pending->connection; 310 } 311 312 /** 313 * Gets the connection associated with this pending call. 314 * 315 * @param pending the pending_call 316 * @returns the connection associated with the pending call 317 */ 318 DBusConnection * 319 _dbus_pending_call_get_connection_unlocked (DBusPendingCall *pending) 320 { 321 _dbus_assert (pending != NULL); 322 323 return pending->connection; 324 } 325 326 /** 327 * Sets the reply message associated with the pending call to a timeout error 328 * 329 * @param pending the pending_call 330 * @param message the message we are sending the error reply to 331 * @param serial serial number for the reply 332 * @return #FALSE on OOM 333 */ 334 dbus_bool_t 335 _dbus_pending_call_set_timeout_error_unlocked (DBusPendingCall *pending, 336 DBusMessage *message, 337 dbus_uint32_t serial) 338 { 339 DBusList *reply_link; 340 DBusMessage *reply; 341 342 reply = dbus_message_new_error (message, DBUS_ERROR_NO_REPLY, 343 "Did not receive a reply. Possible causes include: " 344 "the remote application did not send a reply, " 345 "the message bus security policy blocked the reply, " 346 "the reply timeout expired, or " 347 "the network connection was broken."); 348 if (reply == NULL) 349 return FALSE; 350 351 reply_link = _dbus_list_alloc_link (reply); 352 if (reply_link == NULL) 353 { 354 dbus_message_unref (reply); 355 return FALSE; 356 } 357 358 pending->timeout_link = reply_link; 359 360 _dbus_pending_call_set_reply_serial_unlocked (pending, serial); 361 362 return TRUE; 363 } 364 365 /** 366 * Increments the reference count on a pending call, 367 * while the lock on its connection is already held. 368 * 369 * @param pending the pending call object 370 * @returns the pending call object 371 */ 372 DBusPendingCall * 373 _dbus_pending_call_ref_unlocked (DBusPendingCall *pending) 374 { 375 _dbus_atomic_inc (&pending->refcount); 376 377 return pending; 378 } 379 380 381 static void 382 _dbus_pending_call_last_unref (DBusPendingCall *pending) 383 { 384 DBusConnection *connection; 385 386 /* If we get here, we should be already detached 387 * from the connection, or never attached. 388 */ 389 _dbus_assert (!pending->timeout_added); 390 391 connection = pending->connection; 392 393 /* this assumes we aren't holding connection lock... */ 394 _dbus_data_slot_list_free (&pending->slot_list); 395 396 if (pending->timeout != NULL) 397 _dbus_timeout_unref (pending->timeout); 398 399 if (pending->timeout_link) 400 { 401 dbus_message_unref ((DBusMessage *)pending->timeout_link->data); 402 _dbus_list_free_link (pending->timeout_link); 403 pending->timeout_link = NULL; 404 } 405 406 if (pending->reply) 407 { 408 dbus_message_unref (pending->reply); 409 pending->reply = NULL; 410 } 411 412 dbus_free (pending); 413 414 dbus_pending_call_free_data_slot (¬ify_user_data_slot); 415 416 /* connection lock should not be held. */ 417 /* Free the connection last to avoid a weird state while 418 * calling out to application code where the pending exists 419 * but not the connection. 420 */ 421 dbus_connection_unref (connection); 422 } 423 424 /** 425 * Decrements the reference count on a pending call, 426 * freeing it if the count reaches 0. Assumes 427 * connection lock is already held. 428 * 429 * @param pending the pending call object 430 */ 431 void 432 _dbus_pending_call_unref_and_unlock (DBusPendingCall *pending) 433 { 434 dbus_int32_t old_refcount; 435 436 old_refcount = _dbus_atomic_dec (&pending->refcount); 437 _dbus_assert (old_refcount > 0); 438 439 CONNECTION_UNLOCK (pending->connection); 440 441 if (old_refcount == 1) 442 _dbus_pending_call_last_unref (pending); 443 } 444 445 /** 446 * Checks whether the pending call has received a reply 447 * yet, or not. Assumes connection lock is held. 448 * 449 * @param pending the pending call 450 * @returns #TRUE if a reply has been received 451 */ 452 dbus_bool_t 453 _dbus_pending_call_get_completed_unlocked (DBusPendingCall *pending) 454 { 455 return pending->completed; 456 } 457 458 static DBusDataSlotAllocator slot_allocator; 459 _DBUS_DEFINE_GLOBAL_LOCK (pending_call_slots); 460 461 /** 462 * Stores a pointer on a #DBusPendingCall, along 463 * with an optional function to be used for freeing 464 * the data when the data is set again, or when 465 * the pending call is finalized. The slot number 466 * must have been allocated with dbus_pending_call_allocate_data_slot(). 467 * 468 * @param pending the pending_call 469 * @param slot the slot number 470 * @param data the data to store 471 * @param free_data_func finalizer function for the data 472 * @returns #TRUE if there was enough memory to store the data 473 */ 474 dbus_bool_t 475 _dbus_pending_call_set_data_unlocked (DBusPendingCall *pending, 476 dbus_int32_t slot, 477 void *data, 478 DBusFreeFunction free_data_func) 479 { 480 DBusFreeFunction old_free_func; 481 void *old_data; 482 dbus_bool_t retval; 483 484 retval = _dbus_data_slot_list_set (&slot_allocator, 485 &pending->slot_list, 486 slot, data, free_data_func, 487 &old_free_func, &old_data); 488 489 /* Drop locks to call out to app code */ 490 CONNECTION_UNLOCK (pending->connection); 491 492 if (retval) 493 { 494 if (old_free_func) 495 (* old_free_func) (old_data); 496 } 497 498 CONNECTION_LOCK (pending->connection); 499 500 return retval; 501 } 502 503 /** @} */ 504 505 /** 506 * @defgroup DBusPendingCall DBusPendingCall 507 * @ingroup DBus 508 * @brief Pending reply to a method call message 509 * 510 * A DBusPendingCall is an object representing an 511 * expected reply. A #DBusPendingCall can be created 512 * when you send a message that should have a reply. 513 * 514 * @{ 515 */ 516 517 /** 518 * @typedef DBusPendingCall 519 * 520 * Opaque data type representing a message pending. 521 */ 522 523 /** 524 * Increments the reference count on a pending call. 525 * 526 * @param pending the pending call object 527 * @returns the pending call object 528 */ 529 DBusPendingCall * 530 dbus_pending_call_ref (DBusPendingCall *pending) 531 { 532 _dbus_return_val_if_fail (pending != NULL, NULL); 533 534 _dbus_atomic_inc (&pending->refcount); 535 536 return pending; 537 } 538 539 /** 540 * Decrements the reference count on a pending call, 541 * freeing it if the count reaches 0. 542 * 543 * @param pending the pending call object 544 */ 545 void 546 dbus_pending_call_unref (DBusPendingCall *pending) 547 { 548 dbus_bool_t last_unref; 549 550 _dbus_return_if_fail (pending != NULL); 551 552 last_unref = (_dbus_atomic_dec (&pending->refcount) == 1); 553 554 if (last_unref) 555 _dbus_pending_call_last_unref(pending); 556 } 557 558 /** 559 * Sets a notification function to be called when the reply is 560 * received or the pending call times out. 561 * 562 * @param pending the pending call 563 * @param function notifier function 564 * @param user_data data to pass to notifier function 565 * @param free_user_data function to free the user data 566 * @returns #FALSE if not enough memory 567 */ 568 dbus_bool_t 569 dbus_pending_call_set_notify (DBusPendingCall *pending, 570 DBusPendingCallNotifyFunction function, 571 void *user_data, 572 DBusFreeFunction free_user_data) 573 { 574 _dbus_return_val_if_fail (pending != NULL, FALSE); 575 576 CONNECTION_LOCK (pending->connection); 577 578 /* could invoke application code! */ 579 if (!_dbus_pending_call_set_data_unlocked (pending, notify_user_data_slot, 580 user_data, free_user_data)) 581 return FALSE; 582 583 pending->function = function; 584 585 CONNECTION_UNLOCK (pending->connection); 586 587 return TRUE; 588 } 589 590 /** 591 * Cancels the pending call, such that any reply or error received 592 * will just be ignored. Drops the dbus library's internal reference 593 * to the #DBusPendingCall so will free the call if nobody else is 594 * holding a reference. However you usually get a reference from 595 * dbus_connection_send_with_reply() so probably your app owns a ref 596 * also. 597 * 598 * Note that canceling a pending call will <em>not</em> simulate a 599 * timed-out call; if a call times out, then a timeout error reply is 600 * received. If you cancel the call, no reply is received unless the 601 * the reply was already received before you canceled. 602 * 603 * @param pending the pending call 604 */ 605 void 606 dbus_pending_call_cancel (DBusPendingCall *pending) 607 { 608 _dbus_return_if_fail (pending != NULL); 609 610 _dbus_connection_remove_pending_call (pending->connection, 611 pending); 612 } 613 614 /** 615 * Checks whether the pending call has received a reply 616 * yet, or not. 617 * 618 * @param pending the pending call 619 * @returns #TRUE if a reply has been received 620 */ 621 dbus_bool_t 622 dbus_pending_call_get_completed (DBusPendingCall *pending) 623 { 624 dbus_bool_t completed; 625 626 _dbus_return_val_if_fail (pending != NULL, FALSE); 627 628 CONNECTION_LOCK (pending->connection); 629 completed = pending->completed; 630 CONNECTION_UNLOCK (pending->connection); 631 632 return completed; 633 } 634 635 /** 636 * Gets the reply, or returns #NULL if none has been received 637 * yet. Ownership of the reply message passes to the caller. This 638 * function can only be called once per pending call, since the reply 639 * message is tranferred to the caller. 640 * 641 * @param pending the pending call 642 * @returns the reply message or #NULL. 643 */ 644 DBusMessage* 645 dbus_pending_call_steal_reply (DBusPendingCall *pending) 646 { 647 DBusMessage *message; 648 649 _dbus_return_val_if_fail (pending != NULL, NULL); 650 _dbus_return_val_if_fail (pending->completed, NULL); 651 _dbus_return_val_if_fail (pending->reply != NULL, NULL); 652 653 CONNECTION_LOCK (pending->connection); 654 655 message = pending->reply; 656 pending->reply = NULL; 657 658 CONNECTION_UNLOCK (pending->connection); 659 660 return message; 661 } 662 663 /** 664 * Block until the pending call is completed. The blocking is as with 665 * dbus_connection_send_with_reply_and_block(); it does not enter the 666 * main loop or process other messages, it simply waits for the reply 667 * in question. 668 * 669 * If the pending call is already completed, this function returns 670 * immediately. 671 * 672 * @todo when you start blocking, the timeout is reset, but it should 673 * really only use time remaining since the pending call was created. 674 * This requires storing timestamps instead of intervals in the timeout 675 * 676 * @param pending the pending call 677 */ 678 void 679 dbus_pending_call_block (DBusPendingCall *pending) 680 { 681 _dbus_return_if_fail (pending != NULL); 682 683 _dbus_connection_block_pending_call (pending); 684 } 685 686 /** 687 * Allocates an integer ID to be used for storing application-specific 688 * data on any DBusPendingCall. The allocated ID may then be used 689 * with dbus_pending_call_set_data() and dbus_pending_call_get_data(). 690 * The passed-in slot must be initialized to -1, and is filled in 691 * with the slot ID. If the passed-in slot is not -1, it's assumed 692 * to be already allocated, and its refcount is incremented. 693 * 694 * The allocated slot is global, i.e. all DBusPendingCall objects will 695 * have a slot with the given integer ID reserved. 696 * 697 * @param slot_p address of a global variable storing the slot 698 * @returns #FALSE on failure (no memory) 699 */ 700 dbus_bool_t 701 dbus_pending_call_allocate_data_slot (dbus_int32_t *slot_p) 702 { 703 _dbus_return_val_if_fail (slot_p != NULL, FALSE); 704 705 return _dbus_data_slot_allocator_alloc (&slot_allocator, 706 &_DBUS_LOCK_NAME (pending_call_slots), 707 slot_p); 708 } 709 710 /** 711 * Deallocates a global ID for #DBusPendingCall data slots. 712 * dbus_pending_call_get_data() and dbus_pending_call_set_data() may 713 * no longer be used with this slot. Existing data stored on existing 714 * DBusPendingCall objects will be freed when the #DBusPendingCall is 715 * finalized, but may not be retrieved (and may only be replaced if 716 * someone else reallocates the slot). When the refcount on the 717 * passed-in slot reaches 0, it is set to -1. 718 * 719 * @param slot_p address storing the slot to deallocate 720 */ 721 void 722 dbus_pending_call_free_data_slot (dbus_int32_t *slot_p) 723 { 724 _dbus_return_if_fail (slot_p != NULL); 725 _dbus_return_if_fail (*slot_p >= 0); 726 727 _dbus_data_slot_allocator_free (&slot_allocator, slot_p); 728 } 729 730 /** 731 * Stores a pointer on a #DBusPendingCall, along 732 * with an optional function to be used for freeing 733 * the data when the data is set again, or when 734 * the pending call is finalized. The slot number 735 * must have been allocated with dbus_pending_call_allocate_data_slot(). 736 * 737 * @param pending the pending_call 738 * @param slot the slot number 739 * @param data the data to store 740 * @param free_data_func finalizer function for the data 741 * @returns #TRUE if there was enough memory to store the data 742 */ 743 dbus_bool_t 744 dbus_pending_call_set_data (DBusPendingCall *pending, 745 dbus_int32_t slot, 746 void *data, 747 DBusFreeFunction free_data_func) 748 { 749 dbus_bool_t retval; 750 751 _dbus_return_val_if_fail (pending != NULL, FALSE); 752 _dbus_return_val_if_fail (slot >= 0, FALSE); 753 754 755 CONNECTION_LOCK (pending->connection); 756 retval = _dbus_pending_call_set_data_unlocked (pending, slot, data, free_data_func); 757 CONNECTION_UNLOCK (pending->connection); 758 return retval; 759 } 760 761 /** 762 * Retrieves data previously set with dbus_pending_call_set_data(). 763 * The slot must still be allocated (must not have been freed). 764 * 765 * @param pending the pending_call 766 * @param slot the slot to get data from 767 * @returns the data, or #NULL if not found 768 */ 769 void* 770 dbus_pending_call_get_data (DBusPendingCall *pending, 771 dbus_int32_t slot) 772 { 773 void *res; 774 775 _dbus_return_val_if_fail (pending != NULL, NULL); 776 777 CONNECTION_LOCK (pending->connection); 778 res = _dbus_data_slot_list_get (&slot_allocator, 779 &pending->slot_list, 780 slot); 781 CONNECTION_UNLOCK (pending->connection); 782 783 return res; 784 } 785 786 /** @} */ 787 788 #ifdef DBUS_BUILD_TESTS 789 790 /** 791 * @ingroup DBusPendingCallInternals 792 * Unit test for DBusPendingCall. 793 * 794 * @returns #TRUE on success. 795 */ 796 dbus_bool_t 797 _dbus_pending_call_test (const char *test_data_dir) 798 { 799 800 return TRUE; 801 } 802 #endif /* DBUS_BUILD_TESTS */ 803