00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <config.h>
00025 #include "dbus-connection.h"
00026 #include "dbus-list.h"
00027 #include "dbus-timeout.h"
00028 #include "dbus-transport.h"
00029 #include "dbus-watch.h"
00030 #include "dbus-connection-internal.h"
00031 #include "dbus-list.h"
00032 #include "dbus-hash.h"
00033 #include "dbus-message-internal.h"
00034 #include "dbus-message-handler.h"
00035 #include "dbus-threads.h"
00036 #include "dbus-protocol.h"
00037 #include "dbus-dataslot.h"
00038
00039 #if 0
00040 #define CONNECTION_LOCK(connection) do { \
00041 _dbus_verbose (" LOCK: %s\n", _DBUS_FUNCTION_NAME); \
00042 dbus_mutex_lock ((connection)->mutex); \
00043 } while (0)
00044 #define CONNECTION_UNLOCK(connection) do { \
00045 _dbus_verbose (" UNLOCK: %s\n", _DBUS_FUNCTION_NAME); \
00046 dbus_mutex_unlock ((connection)->mutex); \
00047 } while (0)
00048 #else
00049 #define CONNECTION_LOCK(connection) dbus_mutex_lock ((connection)->mutex)
00050 #define CONNECTION_UNLOCK(connection) dbus_mutex_unlock ((connection)->mutex)
00051 #endif
00052
00126 #define DEFAULT_TIMEOUT_VALUE (15 * 1000)
00127
00128 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
00129
00133 struct DBusConnection
00134 {
00135 DBusAtomic refcount;
00137 DBusMutex *mutex;
00139 dbus_bool_t dispatch_acquired;
00140 DBusCondVar *dispatch_cond;
00142 dbus_bool_t io_path_acquired;
00143 DBusCondVar *io_path_cond;
00145 DBusList *outgoing_messages;
00146 DBusList *incoming_messages;
00148 DBusMessage *message_borrowed;
00149 DBusCondVar *message_returned_cond;
00151 int n_outgoing;
00152 int n_incoming;
00154 DBusCounter *outgoing_counter;
00156 DBusTransport *transport;
00157 DBusWatchList *watches;
00158 DBusTimeoutList *timeouts;
00160 DBusHashTable *handler_table;
00161 DBusList *filter_list;
00163 DBusDataSlotList slot_list;
00165 DBusHashTable *pending_replies;
00167 dbus_uint32_t client_serial;
00168 DBusList *disconnect_message_link;
00170 DBusWakeupMainFunction wakeup_main_function;
00171 void *wakeup_main_data;
00172 DBusFreeFunction free_wakeup_main_data;
00174 DBusDispatchStatusFunction dispatch_status_function;
00175 void *dispatch_status_data;
00176 DBusFreeFunction free_dispatch_status_data;
00178 DBusDispatchStatus last_dispatch_status;
00180 DBusList *link_cache;
00183 };
00184
00185 typedef struct
00186 {
00187 DBusConnection *connection;
00188 DBusMessageHandler *handler;
00189 DBusTimeout *timeout;
00190 int serial;
00191
00192 DBusList *timeout_link;
00193
00194 dbus_bool_t timeout_added;
00195 dbus_bool_t connection_added;
00196 } ReplyHandlerData;
00197
00198 static void reply_handler_data_free (ReplyHandlerData *data);
00199
00200 static void _dbus_connection_remove_timeout_locked (DBusConnection *connection,
00201 DBusTimeout *timeout);
00202 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
00203 static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
00204 DBusDispatchStatus new_status);
00205
00206
00207
00213 void
00214 _dbus_connection_lock (DBusConnection *connection)
00215 {
00216 CONNECTION_LOCK (connection);
00217 }
00218
00224 void
00225 _dbus_connection_unlock (DBusConnection *connection)
00226 {
00227 CONNECTION_UNLOCK (connection);
00228 }
00229
00237 static void
00238 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
00239 {
00240 if (connection->wakeup_main_function)
00241 (*connection->wakeup_main_function) (connection->wakeup_main_data);
00242 }
00243
00244 #ifdef DBUS_BUILD_TESTS
00245
00255 dbus_bool_t
00256 _dbus_connection_queue_received_message (DBusConnection *connection,
00257 DBusMessage *message)
00258 {
00259 DBusList *link;
00260
00261 link = _dbus_list_alloc_link (message);
00262 if (link == NULL)
00263 return FALSE;
00264
00265 dbus_message_ref (message);
00266 _dbus_connection_queue_received_message_link (connection, link);
00267
00268 return TRUE;
00269 }
00270 #endif
00271
00280 void
00281 _dbus_connection_queue_received_message_link (DBusConnection *connection,
00282 DBusList *link)
00283 {
00284 ReplyHandlerData *reply_handler_data;
00285 dbus_int32_t reply_serial;
00286 DBusMessage *message;
00287
00288 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
00289
00290 _dbus_list_append_link (&connection->incoming_messages,
00291 link);
00292 message = link->data;
00293
00294
00295 reply_serial = dbus_message_get_reply_serial (message);
00296 if (reply_serial != -1)
00297 {
00298 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
00299 reply_serial);
00300 if (reply_handler_data != NULL)
00301 {
00302 if (reply_handler_data->timeout_added)
00303 _dbus_connection_remove_timeout_locked (connection,
00304 reply_handler_data->timeout);
00305 reply_handler_data->timeout_added = FALSE;
00306 }
00307 }
00308
00309 connection->n_incoming += 1;
00310
00311 _dbus_connection_wakeup_mainloop (connection);
00312
00313 _dbus_assert (dbus_message_get_name (message) != NULL);
00314 _dbus_verbose ("Message %p (%s) added to incoming queue %p, %d incoming\n",
00315 message, dbus_message_get_name (message),
00316 connection,
00317 connection->n_incoming);
00318 }
00319
00330 static void
00331 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
00332 DBusList *link)
00333 {
00334 _dbus_list_append_link (&connection->incoming_messages, link);
00335
00336 connection->n_incoming += 1;
00337
00338 _dbus_connection_wakeup_mainloop (connection);
00339
00340 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
00341 link->data, connection, connection->n_incoming);
00342 }
00343
00344
00351 dbus_bool_t
00352 _dbus_connection_have_messages_to_send (DBusConnection *connection)
00353 {
00354 return connection->outgoing_messages != NULL;
00355 }
00356
00364 DBusMessage*
00365 _dbus_connection_get_message_to_send (DBusConnection *connection)
00366 {
00367 return _dbus_list_get_last (&connection->outgoing_messages);
00368 }
00369
00378 void
00379 _dbus_connection_message_sent (DBusConnection *connection,
00380 DBusMessage *message)
00381 {
00382 DBusList *link;
00383
00384 _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
00385
00386 link = _dbus_list_get_last_link (&connection->outgoing_messages);
00387 _dbus_assert (link != NULL);
00388 _dbus_assert (link->data == message);
00389
00390
00391 _dbus_list_unlink (&connection->outgoing_messages,
00392 link);
00393 _dbus_list_prepend_link (&connection->link_cache, link);
00394
00395 connection->n_outgoing -= 1;
00396
00397 _dbus_verbose ("Message %p (%s) removed from outgoing queue %p, %d left to send\n",
00398 message, dbus_message_get_name (message),
00399 connection, connection->n_outgoing);
00400
00401
00402 _dbus_message_remove_size_counter (message, connection->outgoing_counter,
00403 &link);
00404 _dbus_list_prepend_link (&connection->link_cache, link);
00405
00406 dbus_message_unref (message);
00407
00408 if (connection->n_outgoing == 0)
00409 _dbus_transport_messages_pending (connection->transport,
00410 connection->n_outgoing);
00411 }
00412
00423 dbus_bool_t
00424 _dbus_connection_add_watch (DBusConnection *connection,
00425 DBusWatch *watch)
00426 {
00427 if (connection->watches)
00428 return _dbus_watch_list_add_watch (connection->watches,
00429 watch);
00430 else
00431 return FALSE;
00432 }
00433
00442 void
00443 _dbus_connection_remove_watch (DBusConnection *connection,
00444 DBusWatch *watch)
00445 {
00446 if (connection->watches)
00447 _dbus_watch_list_remove_watch (connection->watches,
00448 watch);
00449 }
00450
00460 void
00461 _dbus_connection_toggle_watch (DBusConnection *connection,
00462 DBusWatch *watch,
00463 dbus_bool_t enabled)
00464 {
00465 if (connection->watches)
00466 _dbus_watch_list_toggle_watch (connection->watches,
00467 watch, enabled);
00468 }
00469
00481 dbus_bool_t
00482 _dbus_connection_add_timeout (DBusConnection *connection,
00483 DBusTimeout *timeout)
00484 {
00485 if (connection->timeouts)
00486 return _dbus_timeout_list_add_timeout (connection->timeouts,
00487 timeout);
00488 else
00489 return FALSE;
00490 }
00491
00500 void
00501 _dbus_connection_remove_timeout (DBusConnection *connection,
00502 DBusTimeout *timeout)
00503 {
00504 if (connection->timeouts)
00505 _dbus_timeout_list_remove_timeout (connection->timeouts,
00506 timeout);
00507 }
00508
00509 static void
00510 _dbus_connection_remove_timeout_locked (DBusConnection *connection,
00511 DBusTimeout *timeout)
00512 {
00513 CONNECTION_LOCK (connection);
00514 _dbus_connection_remove_timeout (connection, timeout);
00515 CONNECTION_UNLOCK (connection);
00516 }
00517
00527 void
00528 _dbus_connection_toggle_timeout (DBusConnection *connection,
00529 DBusTimeout *timeout,
00530 dbus_bool_t enabled)
00531 {
00532 if (connection->timeouts)
00533 _dbus_timeout_list_toggle_timeout (connection->timeouts,
00534 timeout, enabled);
00535 }
00536
00544 void
00545 _dbus_connection_notify_disconnected (DBusConnection *connection)
00546 {
00547 if (connection->disconnect_message_link)
00548 {
00549
00550 _dbus_connection_queue_synthesized_message_link (connection,
00551 connection->disconnect_message_link);
00552 connection->disconnect_message_link = NULL;
00553 }
00554 }
00555
00556
00566 static dbus_bool_t
00567 _dbus_connection_acquire_io_path (DBusConnection *connection,
00568 int timeout_milliseconds)
00569 {
00570 dbus_bool_t res = TRUE;
00571
00572 if (connection->io_path_acquired)
00573 {
00574 if (timeout_milliseconds != -1)
00575 res = dbus_condvar_wait_timeout (connection->io_path_cond,
00576 connection->mutex,
00577 timeout_milliseconds);
00578 else
00579 dbus_condvar_wait (connection->io_path_cond, connection->mutex);
00580 }
00581
00582 if (res)
00583 {
00584 _dbus_assert (!connection->io_path_acquired);
00585
00586 connection->io_path_acquired = TRUE;
00587 }
00588
00589 return res;
00590 }
00591
00599 static void
00600 _dbus_connection_release_io_path (DBusConnection *connection)
00601 {
00602 _dbus_assert (connection->io_path_acquired);
00603
00604 connection->io_path_acquired = FALSE;
00605 dbus_condvar_wake_one (connection->io_path_cond);
00606 }
00607
00608
00635 void
00636 _dbus_connection_do_iteration (DBusConnection *connection,
00637 unsigned int flags,
00638 int timeout_milliseconds)
00639 {
00640 if (connection->n_outgoing == 0)
00641 flags &= ~DBUS_ITERATION_DO_WRITING;
00642
00643 if (_dbus_connection_acquire_io_path (connection,
00644 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
00645 {
00646 _dbus_transport_do_iteration (connection->transport,
00647 flags, timeout_milliseconds);
00648 _dbus_connection_release_io_path (connection);
00649 }
00650 }
00651
00661 DBusConnection*
00662 _dbus_connection_new_for_transport (DBusTransport *transport)
00663 {
00664 DBusConnection *connection;
00665 DBusWatchList *watch_list;
00666 DBusTimeoutList *timeout_list;
00667 DBusHashTable *handler_table, *pending_replies;
00668 DBusMutex *mutex;
00669 DBusCondVar *message_returned_cond;
00670 DBusCondVar *dispatch_cond;
00671 DBusCondVar *io_path_cond;
00672 DBusList *disconnect_link;
00673 DBusMessage *disconnect_message;
00674 DBusCounter *outgoing_counter;
00675
00676 watch_list = NULL;
00677 connection = NULL;
00678 handler_table = NULL;
00679 pending_replies = NULL;
00680 timeout_list = NULL;
00681 mutex = NULL;
00682 message_returned_cond = NULL;
00683 dispatch_cond = NULL;
00684 io_path_cond = NULL;
00685 disconnect_link = NULL;
00686 disconnect_message = NULL;
00687 outgoing_counter = NULL;
00688
00689 watch_list = _dbus_watch_list_new ();
00690 if (watch_list == NULL)
00691 goto error;
00692
00693 timeout_list = _dbus_timeout_list_new ();
00694 if (timeout_list == NULL)
00695 goto error;
00696
00697 handler_table =
00698 _dbus_hash_table_new (DBUS_HASH_STRING,
00699 dbus_free, NULL);
00700 if (handler_table == NULL)
00701 goto error;
00702
00703 pending_replies =
00704 _dbus_hash_table_new (DBUS_HASH_INT,
00705 NULL, (DBusFreeFunction)reply_handler_data_free);
00706 if (pending_replies == NULL)
00707 goto error;
00708
00709 connection = dbus_new0 (DBusConnection, 1);
00710 if (connection == NULL)
00711 goto error;
00712
00713 mutex = dbus_mutex_new ();
00714 if (mutex == NULL)
00715 goto error;
00716
00717 message_returned_cond = dbus_condvar_new ();
00718 if (message_returned_cond == NULL)
00719 goto error;
00720
00721 dispatch_cond = dbus_condvar_new ();
00722 if (dispatch_cond == NULL)
00723 goto error;
00724
00725 io_path_cond = dbus_condvar_new ();
00726 if (io_path_cond == NULL)
00727 goto error;
00728
00729 disconnect_message = dbus_message_new (DBUS_MESSAGE_LOCAL_DISCONNECT, NULL);
00730 if (disconnect_message == NULL)
00731 goto error;
00732
00733 disconnect_link = _dbus_list_alloc_link (disconnect_message);
00734 if (disconnect_link == NULL)
00735 goto error;
00736
00737 outgoing_counter = _dbus_counter_new ();
00738 if (outgoing_counter == NULL)
00739 goto error;
00740
00741 if (_dbus_modify_sigpipe)
00742 _dbus_disable_sigpipe ();
00743
00744 connection->refcount.value = 1;
00745 connection->mutex = mutex;
00746 connection->dispatch_cond = dispatch_cond;
00747 connection->io_path_cond = io_path_cond;
00748 connection->message_returned_cond = message_returned_cond;
00749 connection->transport = transport;
00750 connection->watches = watch_list;
00751 connection->timeouts = timeout_list;
00752 connection->handler_table = handler_table;
00753 connection->pending_replies = pending_replies;
00754 connection->outgoing_counter = outgoing_counter;
00755 connection->filter_list = NULL;
00756 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE;
00757
00758 _dbus_data_slot_list_init (&connection->slot_list);
00759
00760 connection->client_serial = 1;
00761
00762 connection->disconnect_message_link = disconnect_link;
00763
00764 if (!_dbus_transport_set_connection (transport, connection))
00765 goto error;
00766
00767 _dbus_transport_ref (transport);
00768
00769 return connection;
00770
00771 error:
00772 if (disconnect_message != NULL)
00773 dbus_message_unref (disconnect_message);
00774
00775 if (disconnect_link != NULL)
00776 _dbus_list_free_link (disconnect_link);
00777
00778 if (io_path_cond != NULL)
00779 dbus_condvar_free (io_path_cond);
00780
00781 if (dispatch_cond != NULL)
00782 dbus_condvar_free (dispatch_cond);
00783
00784 if (message_returned_cond != NULL)
00785 dbus_condvar_free (message_returned_cond);
00786
00787 if (mutex != NULL)
00788 dbus_mutex_free (mutex);
00789
00790 if (connection != NULL)
00791 dbus_free (connection);
00792
00793 if (handler_table)
00794 _dbus_hash_table_unref (handler_table);
00795
00796 if (pending_replies)
00797 _dbus_hash_table_unref (pending_replies);
00798
00799 if (watch_list)
00800 _dbus_watch_list_free (watch_list);
00801
00802 if (timeout_list)
00803 _dbus_timeout_list_free (timeout_list);
00804
00805 if (outgoing_counter)
00806 _dbus_counter_unref (outgoing_counter);
00807
00808 return NULL;
00809 }
00810
00817 void
00818 _dbus_connection_ref_unlocked (DBusConnection *connection)
00819 {
00820 #ifdef DBUS_HAVE_ATOMIC_INT
00821 _dbus_atomic_inc (&connection->refcount);
00822 #else
00823 _dbus_assert (connection->refcount.value > 0);
00824 connection->refcount.value += 1;
00825 #endif
00826 }
00827
00828 static dbus_uint32_t
00829 _dbus_connection_get_next_client_serial (DBusConnection *connection)
00830 {
00831 int serial;
00832
00833 serial = connection->client_serial++;
00834
00835 if (connection->client_serial < 0)
00836 connection->client_serial = 1;
00837
00838 return serial;
00839 }
00840
00852 void
00853 _dbus_connection_handler_destroyed_locked (DBusConnection *connection,
00854 DBusMessageHandler *handler)
00855 {
00856 DBusHashIter iter;
00857 DBusList *link;
00858
00859 CONNECTION_LOCK (connection);
00860
00861 _dbus_hash_iter_init (connection->handler_table, &iter);
00862 while (_dbus_hash_iter_next (&iter))
00863 {
00864 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
00865
00866 if (h == handler)
00867 _dbus_hash_iter_remove_entry (&iter);
00868 }
00869
00870 link = _dbus_list_get_first_link (&connection->filter_list);
00871 while (link != NULL)
00872 {
00873 DBusMessageHandler *h = link->data;
00874 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
00875
00876 if (h == handler)
00877 _dbus_list_remove_link (&connection->filter_list,
00878 link);
00879
00880 link = next;
00881 }
00882 CONNECTION_UNLOCK (connection);
00883 }
00884
00898 dbus_bool_t
00899 _dbus_connection_handle_watch (DBusWatch *watch,
00900 unsigned int condition,
00901 void *data)
00902 {
00903 DBusConnection *connection;
00904 dbus_bool_t retval;
00905 DBusDispatchStatus status;
00906
00907 connection = data;
00908
00909 CONNECTION_LOCK (connection);
00910 _dbus_connection_acquire_io_path (connection, -1);
00911 retval = _dbus_transport_handle_watch (connection->transport,
00912 watch, condition);
00913 _dbus_connection_release_io_path (connection);
00914
00915 status = _dbus_connection_get_dispatch_status_unlocked (connection);
00916
00917
00918 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
00919
00920 return retval;
00921 }
00922
00947 DBusConnection*
00948 dbus_connection_open (const char *address,
00949 DBusError *error)
00950 {
00951 DBusConnection *connection;
00952 DBusTransport *transport;
00953
00954 _dbus_return_val_if_fail (address != NULL, NULL);
00955 _dbus_return_val_if_error_is_set (error, NULL);
00956
00957 transport = _dbus_transport_open (address, error);
00958 if (transport == NULL)
00959 {
00960 _DBUS_ASSERT_ERROR_IS_SET (error);
00961 return NULL;
00962 }
00963
00964 connection = _dbus_connection_new_for_transport (transport);
00965
00966 _dbus_transport_unref (transport);
00967
00968 if (connection == NULL)
00969 {
00970 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
00971 return NULL;
00972 }
00973
00974 return connection;
00975 }
00976
00982 void
00983 dbus_connection_ref (DBusConnection *connection)
00984 {
00985 _dbus_return_if_fail (connection != NULL);
00986
00987
00988
00989
00990
00991 #ifdef DBUS_HAVE_ATOMIC_INT
00992 _dbus_atomic_inc (&connection->refcount);
00993 #else
00994 CONNECTION_LOCK (connection);
00995 _dbus_assert (connection->refcount.value > 0);
00996
00997 connection->refcount.value += 1;
00998 CONNECTION_UNLOCK (connection);
00999 #endif
01000 }
01001
01002 static void
01003 free_outgoing_message (void *element,
01004 void *data)
01005 {
01006 DBusMessage *message = element;
01007 DBusConnection *connection = data;
01008
01009 _dbus_message_remove_size_counter (message,
01010 connection->outgoing_counter,
01011 NULL);
01012 dbus_message_unref (message);
01013 }
01014
01015
01016
01017
01018
01019 static void
01020 _dbus_connection_last_unref (DBusConnection *connection)
01021 {
01022 DBusHashIter iter;
01023 DBusList *link;
01024
01025 _dbus_verbose ("Finalizing connection %p\n", connection);
01026
01027 _dbus_assert (connection->refcount.value == 0);
01028
01029
01030
01031
01032 _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
01033
01034
01035 dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
01036 dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
01037 dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
01038
01039 _dbus_watch_list_free (connection->watches);
01040 connection->watches = NULL;
01041
01042 _dbus_timeout_list_free (connection->timeouts);
01043 connection->timeouts = NULL;
01044
01045 _dbus_data_slot_list_free (&connection->slot_list);
01046
01047
01048 _dbus_hash_iter_init (connection->handler_table, &iter);
01049 while (_dbus_hash_iter_next (&iter))
01050 {
01051 DBusMessageHandler *h = _dbus_hash_iter_get_value (&iter);
01052
01053 _dbus_message_handler_remove_connection (h, connection);
01054 }
01055
01056 link = _dbus_list_get_first_link (&connection->filter_list);
01057 while (link != NULL)
01058 {
01059 DBusMessageHandler *h = link->data;
01060 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
01061
01062 _dbus_message_handler_remove_connection (h, connection);
01063
01064 link = next;
01065 }
01066
01067 _dbus_hash_table_unref (connection->handler_table);
01068 connection->handler_table = NULL;
01069
01070 _dbus_hash_table_unref (connection->pending_replies);
01071 connection->pending_replies = NULL;
01072
01073 _dbus_list_clear (&connection->filter_list);
01074
01075 _dbus_list_foreach (&connection->outgoing_messages,
01076 free_outgoing_message,
01077 connection);
01078 _dbus_list_clear (&connection->outgoing_messages);
01079
01080 _dbus_list_foreach (&connection->incoming_messages,
01081 (DBusForeachFunction) dbus_message_unref,
01082 NULL);
01083 _dbus_list_clear (&connection->incoming_messages);
01084
01085 _dbus_counter_unref (connection->outgoing_counter);
01086
01087 _dbus_transport_unref (connection->transport);
01088
01089 if (connection->disconnect_message_link)
01090 {
01091 DBusMessage *message = connection->disconnect_message_link->data;
01092 dbus_message_unref (message);
01093 _dbus_list_free_link (connection->disconnect_message_link);
01094 }
01095
01096 _dbus_list_clear (&connection->link_cache);
01097
01098 dbus_condvar_free (connection->dispatch_cond);
01099 dbus_condvar_free (connection->io_path_cond);
01100 dbus_condvar_free (connection->message_returned_cond);
01101
01102 dbus_mutex_free (connection->mutex);
01103
01104 dbus_free (connection);
01105 }
01106
01118 void
01119 dbus_connection_unref (DBusConnection *connection)
01120 {
01121 dbus_bool_t last_unref;
01122
01123 _dbus_return_if_fail (connection != NULL);
01124
01125
01126
01127
01128
01129 #ifdef DBUS_HAVE_ATOMIC_INT
01130 last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
01131 #else
01132 CONNECTION_LOCK (connection);
01133
01134 _dbus_assert (connection->refcount.value > 0);
01135
01136 connection->refcount.value -= 1;
01137 last_unref = (connection->refcount.value == 0);
01138
01139 #if 0
01140 printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
01141 #endif
01142
01143 CONNECTION_UNLOCK (connection);
01144 #endif
01145
01146 if (last_unref)
01147 _dbus_connection_last_unref (connection);
01148 }
01149
01160 void
01161 dbus_connection_disconnect (DBusConnection *connection)
01162 {
01163 _dbus_return_if_fail (connection != NULL);
01164
01165 CONNECTION_LOCK (connection);
01166 _dbus_transport_disconnect (connection->transport);
01167 CONNECTION_UNLOCK (connection);
01168 }
01169
01170 static dbus_bool_t
01171 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
01172 {
01173 return _dbus_transport_get_is_connected (connection->transport);
01174 }
01175
01186 dbus_bool_t
01187 dbus_connection_get_is_connected (DBusConnection *connection)
01188 {
01189 dbus_bool_t res;
01190
01191 _dbus_return_val_if_fail (connection != NULL, FALSE);
01192
01193 CONNECTION_LOCK (connection);
01194 res = _dbus_connection_get_is_connected_unlocked (connection);
01195 CONNECTION_UNLOCK (connection);
01196
01197 return res;
01198 }
01199
01208 dbus_bool_t
01209 dbus_connection_get_is_authenticated (DBusConnection *connection)
01210 {
01211 dbus_bool_t res;
01212
01213 _dbus_return_val_if_fail (connection != NULL, FALSE);
01214
01215 CONNECTION_LOCK (connection);
01216 res = _dbus_transport_get_is_authenticated (connection->transport);
01217 CONNECTION_UNLOCK (connection);
01218
01219 return res;
01220 }
01221
01222 struct DBusPreallocatedSend
01223 {
01224 DBusConnection *connection;
01225 DBusList *queue_link;
01226 DBusList *counter_link;
01227 };
01228
01229 static DBusPreallocatedSend*
01230 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
01231 {
01232 DBusPreallocatedSend *preallocated;
01233
01234 _dbus_return_val_if_fail (connection != NULL, NULL);
01235
01236 preallocated = dbus_new (DBusPreallocatedSend, 1);
01237 if (preallocated == NULL)
01238 return NULL;
01239
01240 if (connection->link_cache != NULL)
01241 {
01242 preallocated->queue_link =
01243 _dbus_list_pop_first_link (&connection->link_cache);
01244 preallocated->queue_link->data = NULL;
01245 }
01246 else
01247 {
01248 preallocated->queue_link = _dbus_list_alloc_link (NULL);
01249 if (preallocated->queue_link == NULL)
01250 goto failed_0;
01251 }
01252
01253 if (connection->link_cache != NULL)
01254 {
01255 preallocated->counter_link =
01256 _dbus_list_pop_first_link (&connection->link_cache);
01257 preallocated->counter_link->data = connection->outgoing_counter;
01258 }
01259 else
01260 {
01261 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
01262 if (preallocated->counter_link == NULL)
01263 goto failed_1;
01264 }
01265
01266 _dbus_counter_ref (preallocated->counter_link->data);
01267
01268 preallocated->connection = connection;
01269
01270 return preallocated;
01271
01272 failed_1:
01273 _dbus_list_free_link (preallocated->queue_link);
01274 failed_0:
01275 dbus_free (preallocated);
01276
01277 return NULL;
01278 }
01279
01289 DBusPreallocatedSend*
01290 dbus_connection_preallocate_send (DBusConnection *connection)
01291 {
01292 DBusPreallocatedSend *preallocated;
01293
01294 _dbus_return_val_if_fail (connection != NULL, NULL);
01295
01296 CONNECTION_LOCK (connection);
01297
01298 preallocated =
01299 _dbus_connection_preallocate_send_unlocked (connection);
01300
01301 CONNECTION_UNLOCK (connection);
01302
01303 return preallocated;
01304 }
01305
01315 void
01316 dbus_connection_free_preallocated_send (DBusConnection *connection,
01317 DBusPreallocatedSend *preallocated)
01318 {
01319 _dbus_return_if_fail (connection != NULL);
01320 _dbus_return_if_fail (preallocated != NULL);
01321 _dbus_return_if_fail (connection == preallocated->connection);
01322
01323 _dbus_list_free_link (preallocated->queue_link);
01324 _dbus_counter_unref (preallocated->counter_link->data);
01325 _dbus_list_free_link (preallocated->counter_link);
01326 dbus_free (preallocated);
01327 }
01328
01329 static void
01330 _dbus_connection_send_preallocated_unlocked (DBusConnection *connection,
01331 DBusPreallocatedSend *preallocated,
01332 DBusMessage *message,
01333 dbus_uint32_t *client_serial)
01334 {
01335 dbus_uint32_t serial;
01336
01337 preallocated->queue_link->data = message;
01338 _dbus_list_prepend_link (&connection->outgoing_messages,
01339 preallocated->queue_link);
01340
01341 _dbus_message_add_size_counter_link (message,
01342 preallocated->counter_link);
01343
01344 dbus_free (preallocated);
01345 preallocated = NULL;
01346
01347 dbus_message_ref (message);
01348
01349 connection->n_outgoing += 1;
01350
01351 _dbus_verbose ("Message %p (%s) added to outgoing queue %p, %d pending to send\n",
01352 message,
01353 dbus_message_get_name (message),
01354 connection,
01355 connection->n_outgoing);
01356
01357 if (dbus_message_get_serial (message) == 0)
01358 {
01359 serial = _dbus_connection_get_next_client_serial (connection);
01360 _dbus_message_set_serial (message, serial);
01361 if (client_serial)
01362 *client_serial = serial;
01363 }
01364 else
01365 {
01366 if (client_serial)
01367 *client_serial = dbus_message_get_serial (message);
01368 }
01369
01370 _dbus_message_lock (message);
01371
01372 if (connection->n_outgoing == 1)
01373 _dbus_transport_messages_pending (connection->transport,
01374 connection->n_outgoing);
01375
01376 _dbus_connection_wakeup_mainloop (connection);
01377 }
01378
01391 void
01392 dbus_connection_send_preallocated (DBusConnection *connection,
01393 DBusPreallocatedSend *preallocated,
01394 DBusMessage *message,
01395 dbus_uint32_t *client_serial)
01396 {
01397 _dbus_return_if_fail (connection != NULL);
01398 _dbus_return_if_fail (preallocated != NULL);
01399 _dbus_return_if_fail (message != NULL);
01400 _dbus_return_if_fail (preallocated->connection == connection);
01401 _dbus_return_if_fail (dbus_message_get_name (message) != NULL);
01402
01403 CONNECTION_LOCK (connection);
01404 _dbus_connection_send_preallocated_unlocked (connection,
01405 preallocated,
01406 message, client_serial);
01407 CONNECTION_UNLOCK (connection);
01408 }
01409
01428 dbus_bool_t
01429 dbus_connection_send (DBusConnection *connection,
01430 DBusMessage *message,
01431 dbus_uint32_t *client_serial)
01432 {
01433 DBusPreallocatedSend *preallocated;
01434
01435 _dbus_return_val_if_fail (connection != NULL, FALSE);
01436 _dbus_return_val_if_fail (message != NULL, FALSE);
01437
01438 CONNECTION_LOCK (connection);
01439
01440 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
01441 if (preallocated == NULL)
01442 {
01443 CONNECTION_UNLOCK (connection);
01444 return FALSE;
01445 }
01446 else
01447 {
01448 _dbus_connection_send_preallocated_unlocked (connection,
01449 preallocated,
01450 message,
01451 client_serial);
01452 CONNECTION_UNLOCK (connection);
01453 return TRUE;
01454 }
01455 }
01456
01457 static dbus_bool_t
01458 reply_handler_timeout (void *data)
01459 {
01460 DBusConnection *connection;
01461 ReplyHandlerData *reply_handler_data = data;
01462 DBusDispatchStatus status;
01463
01464 connection = reply_handler_data->connection;
01465
01466 CONNECTION_LOCK (connection);
01467 if (reply_handler_data->timeout_link)
01468 {
01469 _dbus_connection_queue_synthesized_message_link (connection,
01470 reply_handler_data->timeout_link);
01471 reply_handler_data->timeout_link = NULL;
01472 }
01473
01474 _dbus_connection_remove_timeout (connection,
01475 reply_handler_data->timeout);
01476 reply_handler_data->timeout_added = FALSE;
01477
01478 status = _dbus_connection_get_dispatch_status_unlocked (connection);
01479
01480
01481 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
01482
01483 return TRUE;
01484 }
01485
01486 static void
01487 reply_handler_data_free (ReplyHandlerData *data)
01488 {
01489 if (!data)
01490 return;
01491
01492 if (data->timeout_added)
01493 _dbus_connection_remove_timeout_locked (data->connection,
01494 data->timeout);
01495
01496 if (data->connection_added)
01497 _dbus_message_handler_remove_connection (data->handler,
01498 data->connection);
01499
01500 if (data->timeout_link)
01501 {
01502 dbus_message_unref ((DBusMessage *)data->timeout_link->data);
01503 _dbus_list_free_link (data->timeout_link);
01504 }
01505
01506 dbus_message_handler_unref (data->handler);
01507
01508 dbus_free (data);
01509 }
01510
01546 dbus_bool_t
01547 dbus_connection_send_with_reply (DBusConnection *connection,
01548 DBusMessage *message,
01549 DBusMessageHandler *reply_handler,
01550 int timeout_milliseconds)
01551 {
01552 DBusTimeout *timeout;
01553 ReplyHandlerData *data;
01554 DBusMessage *reply;
01555 DBusList *reply_link;
01556 dbus_int32_t serial = -1;
01557
01558 _dbus_return_val_if_fail (connection != NULL, FALSE);
01559 _dbus_return_val_if_fail (message != NULL, FALSE);
01560 _dbus_return_val_if_fail (reply_handler != NULL, FALSE);
01561 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
01562
01563 if (timeout_milliseconds == -1)
01564 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
01565
01566 data = dbus_new0 (ReplyHandlerData, 1);
01567
01568 if (!data)
01569 return FALSE;
01570
01571 timeout = _dbus_timeout_new (timeout_milliseconds, reply_handler_timeout,
01572 data, NULL);
01573
01574 if (!timeout)
01575 {
01576 reply_handler_data_free (data);
01577 return FALSE;
01578 }
01579
01580 CONNECTION_LOCK (connection);
01581
01582
01583 if (!_dbus_connection_add_timeout (connection, timeout))
01584 {
01585 reply_handler_data_free (data);
01586 _dbus_timeout_unref (timeout);
01587 CONNECTION_UNLOCK (connection);
01588 return FALSE;
01589 }
01590
01591
01592 _dbus_timeout_unref (timeout);
01593
01594 data->timeout_added = TRUE;
01595 data->timeout = timeout;
01596 data->connection = connection;
01597
01598 if (!_dbus_message_handler_add_connection (reply_handler, connection))
01599 {
01600 CONNECTION_UNLOCK (connection);
01601 reply_handler_data_free (data);
01602 return FALSE;
01603 }
01604 data->connection_added = TRUE;
01605
01606
01607 if (dbus_message_get_serial (message) == 0)
01608 {
01609 serial = _dbus_connection_get_next_client_serial (connection);
01610 _dbus_message_set_serial (message, serial);
01611 }
01612
01613 data->handler = reply_handler;
01614 data->serial = serial;
01615
01616 dbus_message_handler_ref (reply_handler);
01617
01618 reply = dbus_message_new_error_reply (message, DBUS_ERROR_NO_REPLY,
01619 "No reply within specified time");
01620 if (!reply)
01621 {
01622 CONNECTION_UNLOCK (connection);
01623 reply_handler_data_free (data);
01624 return FALSE;
01625 }
01626
01627 reply_link = _dbus_list_alloc_link (reply);
01628 if (!reply)
01629 {
01630 CONNECTION_UNLOCK (connection);
01631 dbus_message_unref (reply);
01632 reply_handler_data_free (data);
01633 return FALSE;
01634 }
01635
01636 data->timeout_link = reply_link;
01637
01638
01639 if (!_dbus_hash_table_insert_int (connection->pending_replies, serial, data))
01640 {
01641 CONNECTION_UNLOCK (connection);
01642 reply_handler_data_free (data);
01643 return FALSE;
01644 }
01645
01646 CONNECTION_UNLOCK (connection);
01647
01648 if (!dbus_connection_send (connection, message, NULL))
01649 {
01650
01651 _dbus_hash_table_remove_int (connection->pending_replies, serial);
01652 return FALSE;
01653 }
01654
01655 return TRUE;
01656 }
01657
01658
01659 static DBusMessage*
01660 check_for_reply_unlocked (DBusConnection *connection,
01661 dbus_uint32_t client_serial)
01662 {
01663 DBusList *link;
01664
01665 link = _dbus_list_get_first_link (&connection->incoming_messages);
01666
01667 while (link != NULL)
01668 {
01669 DBusMessage *reply = link->data;
01670
01671 if (dbus_message_get_reply_serial (reply) == client_serial)
01672 {
01673 _dbus_list_remove_link (&connection->incoming_messages, link);
01674 connection->n_incoming -= 1;
01675 dbus_message_ref (reply);
01676 return reply;
01677 }
01678 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
01679 }
01680
01681 return NULL;
01682 }
01683
01705 DBusMessage *
01706 dbus_connection_send_with_reply_and_block (DBusConnection *connection,
01707 DBusMessage *message,
01708 int timeout_milliseconds,
01709 DBusError *error)
01710 {
01711 dbus_uint32_t client_serial;
01712 long start_tv_sec, start_tv_usec;
01713 long end_tv_sec, end_tv_usec;
01714 long tv_sec, tv_usec;
01715 DBusDispatchStatus status;
01716
01717 _dbus_return_val_if_fail (connection != NULL, NULL);
01718 _dbus_return_val_if_fail (message != NULL, NULL);
01719 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
01720 _dbus_return_val_if_error_is_set (error, NULL);
01721
01722 if (timeout_milliseconds == -1)
01723 timeout_milliseconds = DEFAULT_TIMEOUT_VALUE;
01724
01725
01726
01727
01728
01729 if (timeout_milliseconds > _DBUS_ONE_HOUR_IN_MILLISECONDS * 6)
01730 timeout_milliseconds = _DBUS_ONE_HOUR_IN_MILLISECONDS * 6;
01731
01732 if (!dbus_connection_send (connection, message, &client_serial))
01733 {
01734 _DBUS_SET_OOM (error);
01735 return NULL;
01736 }
01737
01738 message = NULL;
01739
01740
01741 dbus_connection_flush (connection);
01742
01743 CONNECTION_LOCK (connection);
01744
01745 _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
01746 end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
01747 end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
01748 end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
01749 end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
01750
01751 _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",
01752 timeout_milliseconds,
01753 client_serial,
01754 start_tv_sec, start_tv_usec,
01755 end_tv_sec, end_tv_usec);
01756
01757
01758
01759
01760
01761
01762 _dbus_connection_do_iteration (connection,
01763 DBUS_ITERATION_DO_READING |
01764 DBUS_ITERATION_BLOCK,
01765 timeout_milliseconds);
01766
01767 recheck_status:
01768
01769
01770 status = _dbus_connection_get_dispatch_status_unlocked (connection);
01771
01772 if (status == DBUS_DISPATCH_DATA_REMAINS)
01773 {
01774 DBusMessage *reply;
01775
01776 reply = check_for_reply_unlocked (connection, client_serial);
01777 if (reply != NULL)
01778 {
01779 status = _dbus_connection_get_dispatch_status_unlocked (connection);
01780
01781 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply %s\n",
01782 dbus_message_get_name (reply));
01783
01784
01785 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
01786
01787 return reply;
01788 }
01789 }
01790
01791 _dbus_get_current_time (&tv_sec, &tv_usec);
01792
01793 if (tv_sec < start_tv_sec)
01794 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
01795 else if (connection->disconnect_message_link == NULL)
01796 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
01797 else if (tv_sec < end_tv_sec ||
01798 (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
01799 {
01800 timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
01801 (end_tv_usec - tv_usec) / 1000;
01802 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
01803 _dbus_assert (timeout_milliseconds >= 0);
01804
01805 if (status == DBUS_DISPATCH_NEED_MEMORY)
01806 {
01807
01808
01809
01810
01811 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
01812
01813 if (timeout_milliseconds < 100)
01814 ;
01815 else if (timeout_milliseconds <= 1000)
01816 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
01817 else
01818 _dbus_sleep_milliseconds (1000);
01819 }
01820 else
01821 {
01822
01823 _dbus_connection_do_iteration (connection,
01824 DBUS_ITERATION_DO_READING |
01825 DBUS_ITERATION_BLOCK,
01826 timeout_milliseconds);
01827 }
01828
01829 goto recheck_status;
01830 }
01831
01832 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
01833 (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
01834
01835 if (dbus_connection_get_is_connected (connection))
01836 dbus_set_error (error, DBUS_ERROR_NO_REPLY, "Message did not receive a reply");
01837 else
01838 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Disconnected prior to receiving a reply");
01839
01840
01841 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
01842
01843 return NULL;
01844 }
01845
01851 void
01852 dbus_connection_flush (DBusConnection *connection)
01853 {
01854
01855
01856
01857
01858
01859 DBusDispatchStatus status;
01860
01861 _dbus_return_if_fail (connection != NULL);
01862
01863 CONNECTION_LOCK (connection);
01864 while (connection->n_outgoing > 0 &&
01865 _dbus_connection_get_is_connected_unlocked (connection))
01866 _dbus_connection_do_iteration (connection,
01867 DBUS_ITERATION_DO_READING |
01868 DBUS_ITERATION_DO_WRITING |
01869 DBUS_ITERATION_BLOCK,
01870 -1);
01871
01872 status = _dbus_connection_get_dispatch_status_unlocked (connection);
01873
01874
01875 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
01876 }
01877
01878
01879
01880
01881 static void
01882 _dbus_connection_wait_for_borrowed (DBusConnection *connection)
01883 {
01884 _dbus_assert (connection->message_borrowed != NULL);
01885
01886 while (connection->message_borrowed != NULL)
01887 dbus_condvar_wait (connection->message_returned_cond, connection->mutex);
01888 }
01889
01904 DBusMessage*
01905 dbus_connection_borrow_message (DBusConnection *connection)
01906 {
01907 DBusMessage *message;
01908 DBusDispatchStatus status;
01909
01910 _dbus_return_val_if_fail (connection != NULL, NULL);
01911
01912
01913
01914
01915 status = dbus_connection_get_dispatch_status (connection);
01916 if (status != DBUS_DISPATCH_DATA_REMAINS)
01917 return NULL;
01918
01919 CONNECTION_LOCK (connection);
01920
01921 if (connection->message_borrowed != NULL)
01922 _dbus_connection_wait_for_borrowed (connection);
01923
01924 message = _dbus_list_get_first (&connection->incoming_messages);
01925
01926 if (message)
01927 connection->message_borrowed = message;
01928
01929 CONNECTION_UNLOCK (connection);
01930 return message;
01931 }
01932
01940 void
01941 dbus_connection_return_message (DBusConnection *connection,
01942 DBusMessage *message)
01943 {
01944 _dbus_return_if_fail (connection != NULL);
01945 _dbus_return_if_fail (message != NULL);
01946
01947 CONNECTION_LOCK (connection);
01948
01949 _dbus_assert (message == connection->message_borrowed);
01950
01951 connection->message_borrowed = NULL;
01952 dbus_condvar_wake_all (connection->message_returned_cond);
01953
01954 CONNECTION_UNLOCK (connection);
01955 }
01956
01966 void
01967 dbus_connection_steal_borrowed_message (DBusConnection *connection,
01968 DBusMessage *message)
01969 {
01970 DBusMessage *pop_message;
01971
01972 _dbus_return_if_fail (connection != NULL);
01973 _dbus_return_if_fail (message != NULL);
01974
01975 CONNECTION_LOCK (connection);
01976
01977 _dbus_assert (message == connection->message_borrowed);
01978
01979 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
01980 _dbus_assert (message == pop_message);
01981
01982 connection->n_incoming -= 1;
01983
01984 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
01985 message, connection->n_incoming);
01986
01987 connection->message_borrowed = NULL;
01988 dbus_condvar_wake_all (connection->message_returned_cond);
01989
01990 CONNECTION_UNLOCK (connection);
01991 }
01992
01993
01994
01995
01996 static DBusList*
01997 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
01998 {
01999 if (connection->message_borrowed != NULL)
02000 _dbus_connection_wait_for_borrowed (connection);
02001
02002 if (connection->n_incoming > 0)
02003 {
02004 DBusList *link;
02005
02006 link = _dbus_list_pop_first_link (&connection->incoming_messages);
02007 connection->n_incoming -= 1;
02008
02009 _dbus_verbose ("Message %p (%s) removed from incoming queue %p, %d incoming\n",
02010 link->data, dbus_message_get_name (link->data),
02011 connection, connection->n_incoming);
02012
02013 return link;
02014 }
02015 else
02016 return NULL;
02017 }
02018
02019
02020
02021
02022 static DBusMessage*
02023 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
02024 {
02025 DBusList *link;
02026
02027 link = _dbus_connection_pop_message_link_unlocked (connection);
02028
02029 if (link != NULL)
02030 {
02031 DBusMessage *message;
02032
02033 message = link->data;
02034
02035 _dbus_list_free_link (link);
02036
02037 return message;
02038 }
02039 else
02040 return NULL;
02041 }
02042
02043
02058 DBusMessage*
02059 dbus_connection_pop_message (DBusConnection *connection)
02060 {
02061 DBusMessage *message;
02062 DBusDispatchStatus status;
02063
02064
02065
02066
02067 status = dbus_connection_get_dispatch_status (connection);
02068 if (status != DBUS_DISPATCH_DATA_REMAINS)
02069 return NULL;
02070
02071 CONNECTION_LOCK (connection);
02072
02073 message = _dbus_connection_pop_message_unlocked (connection);
02074
02075 _dbus_verbose ("Returning popped message %p\n", message);
02076
02077 CONNECTION_UNLOCK (connection);
02078
02079 return message;
02080 }
02081
02090 static void
02091 _dbus_connection_acquire_dispatch (DBusConnection *connection)
02092 {
02093 if (connection->dispatch_acquired)
02094 dbus_condvar_wait (connection->dispatch_cond, connection->mutex);
02095 _dbus_assert (!connection->dispatch_acquired);
02096
02097 connection->dispatch_acquired = TRUE;
02098 }
02099
02107 static void
02108 _dbus_connection_release_dispatch (DBusConnection *connection)
02109 {
02110 _dbus_assert (connection->dispatch_acquired);
02111
02112 connection->dispatch_acquired = FALSE;
02113 dbus_condvar_wake_one (connection->dispatch_cond);
02114 }
02115
02116 static void
02117 _dbus_connection_failed_pop (DBusConnection *connection,
02118 DBusList *message_link)
02119 {
02120 _dbus_list_prepend_link (&connection->incoming_messages,
02121 message_link);
02122 connection->n_incoming += 1;
02123 }
02124
02125 static DBusDispatchStatus
02126 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
02127 {
02128 if (connection->n_incoming > 0)
02129 return DBUS_DISPATCH_DATA_REMAINS;
02130 else if (!_dbus_transport_queue_messages (connection->transport))
02131 return DBUS_DISPATCH_NEED_MEMORY;
02132 else
02133 {
02134 DBusDispatchStatus status;
02135
02136 status = _dbus_transport_get_dispatch_status (connection->transport);
02137
02138 if (status != DBUS_DISPATCH_COMPLETE)
02139 return status;
02140 else if (connection->n_incoming > 0)
02141 return DBUS_DISPATCH_DATA_REMAINS;
02142 else
02143 return DBUS_DISPATCH_COMPLETE;
02144 }
02145 }
02146
02147 static void
02148 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
02149 DBusDispatchStatus new_status)
02150 {
02151 dbus_bool_t changed;
02152 DBusDispatchStatusFunction function;
02153 void *data;
02154
02155
02156
02157 _dbus_connection_ref_unlocked (connection);
02158
02159 changed = new_status != connection->last_dispatch_status;
02160
02161 connection->last_dispatch_status = new_status;
02162
02163 function = connection->dispatch_status_function;
02164 data = connection->dispatch_status_data;
02165
02166
02167 CONNECTION_UNLOCK (connection);
02168
02169 if (changed && function)
02170 {
02171 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
02172 connection, new_status,
02173 new_status == DBUS_DISPATCH_COMPLETE ? "complete" :
02174 new_status == DBUS_DISPATCH_DATA_REMAINS ? "data remains" :
02175 new_status == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :
02176 "???");
02177 (* function) (connection, new_status, data);
02178 }
02179
02180 dbus_connection_unref (connection);
02181 }
02182
02191 DBusDispatchStatus
02192 dbus_connection_get_dispatch_status (DBusConnection *connection)
02193 {
02194 DBusDispatchStatus status;
02195
02196 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
02197
02198 CONNECTION_LOCK (connection);
02199
02200 status = _dbus_connection_get_dispatch_status_unlocked (connection);
02201
02202 CONNECTION_UNLOCK (connection);
02203
02204 return status;
02205 }
02206
02221 DBusDispatchStatus
02222 dbus_connection_dispatch (DBusConnection *connection)
02223 {
02224 DBusMessageHandler *handler;
02225 DBusMessage *message;
02226 DBusList *link, *filter_list_copy, *message_link;
02227 DBusHandlerResult result;
02228 ReplyHandlerData *reply_handler_data;
02229 const char *name;
02230 dbus_int32_t reply_serial;
02231 DBusDispatchStatus status;
02232
02233 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
02234
02235 CONNECTION_LOCK (connection);
02236 status = _dbus_connection_get_dispatch_status_unlocked (connection);
02237 if (status != DBUS_DISPATCH_DATA_REMAINS)
02238 {
02239
02240 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02241 return status;
02242 }
02243
02244
02245
02246
02247 _dbus_connection_ref_unlocked (connection);
02248
02249 _dbus_connection_acquire_dispatch (connection);
02250
02251
02252
02253
02254
02255
02256
02257 message_link = _dbus_connection_pop_message_link_unlocked (connection);
02258 if (message_link == NULL)
02259 {
02260
02261
02262 _dbus_connection_release_dispatch (connection);
02263
02264 status = _dbus_connection_get_dispatch_status_unlocked (connection);
02265
02266 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02267
02268 dbus_connection_unref (connection);
02269
02270 return status;
02271 }
02272
02273 message = message_link->data;
02274
02275 result = DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS;
02276
02277 reply_serial = dbus_message_get_reply_serial (message);
02278 reply_handler_data = _dbus_hash_table_lookup_int (connection->pending_replies,
02279 reply_serial);
02280
02281 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
02282 {
02283 _dbus_connection_release_dispatch (connection);
02284
02285 _dbus_connection_failed_pop (connection, message_link);
02286
02287
02288 _dbus_connection_update_dispatch_status_and_unlock (connection,
02289 DBUS_DISPATCH_NEED_MEMORY);
02290
02291 dbus_connection_unref (connection);
02292
02293 return DBUS_DISPATCH_NEED_MEMORY;
02294 }
02295
02296 _dbus_list_foreach (&filter_list_copy,
02297 (DBusForeachFunction)dbus_message_handler_ref,
02298 NULL);
02299
02300
02301
02302
02303 CONNECTION_UNLOCK (connection);
02304
02305 link = _dbus_list_get_first_link (&filter_list_copy);
02306 while (link != NULL)
02307 {
02308 DBusMessageHandler *handler = link->data;
02309 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
02310
02311 _dbus_verbose (" running filter on message %p\n", message);
02312 result = _dbus_message_handler_handle_message (handler, connection,
02313 message);
02314
02315 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
02316 break;
02317
02318 link = next;
02319 }
02320
02321 _dbus_list_foreach (&filter_list_copy,
02322 (DBusForeachFunction)dbus_message_handler_unref,
02323 NULL);
02324 _dbus_list_clear (&filter_list_copy);
02325
02326 CONNECTION_LOCK (connection);
02327
02328
02329 if (reply_handler_data && result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
02330 {
02331
02332 if (reply_handler_data->timeout_link)
02333 {
02334 _dbus_connection_queue_synthesized_message_link (connection,
02335 reply_handler_data->timeout_link);
02336 reply_handler_data->timeout_link = NULL;
02337 }
02338 else
02339 {
02340
02341 _dbus_warn ("The timeout error with reply serial %d was filtered, so the reply handler will never be called.\n", reply_serial);
02342 }
02343 }
02344
02345 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
02346 goto out;
02347
02348 if (reply_handler_data)
02349 {
02350 CONNECTION_UNLOCK (connection);
02351
02352 _dbus_verbose (" running reply handler on message %p\n", message);
02353
02354 result = _dbus_message_handler_handle_message (reply_handler_data->handler,
02355 connection, message);
02356 reply_handler_data_free (reply_handler_data);
02357 CONNECTION_LOCK (connection);
02358 goto out;
02359 }
02360
02361 name = dbus_message_get_name (message);
02362 if (name != NULL)
02363 {
02364 handler = _dbus_hash_table_lookup_string (connection->handler_table,
02365 name);
02366 if (handler != NULL)
02367 {
02368
02369
02370
02371 CONNECTION_UNLOCK (connection);
02372
02373 _dbus_verbose (" running app handler on message %p (%s)\n",
02374 message, dbus_message_get_name (message));
02375
02376 result = _dbus_message_handler_handle_message (handler, connection,
02377 message);
02378 CONNECTION_LOCK (connection);
02379 if (result == DBUS_HANDLER_RESULT_REMOVE_MESSAGE)
02380 goto out;
02381 }
02382 }
02383
02384 _dbus_verbose (" done dispatching %p (%s) on connection %p\n", message,
02385 dbus_message_get_name (message), connection);
02386
02387 out:
02388 _dbus_connection_release_dispatch (connection);
02389
02390 _dbus_list_free_link (message_link);
02391 dbus_message_unref (message);
02392
02393
02394
02395 status = _dbus_connection_get_dispatch_status_unlocked (connection);
02396
02397
02398 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02399
02400 dbus_connection_unref (connection);
02401
02402 return status;
02403 }
02404
02463 dbus_bool_t
02464 dbus_connection_set_watch_functions (DBusConnection *connection,
02465 DBusAddWatchFunction add_function,
02466 DBusRemoveWatchFunction remove_function,
02467 DBusWatchToggledFunction toggled_function,
02468 void *data,
02469 DBusFreeFunction free_data_function)
02470 {
02471 dbus_bool_t retval;
02472
02473 _dbus_return_val_if_fail (connection != NULL, FALSE);
02474
02475 CONNECTION_LOCK (connection);
02476
02477 _dbus_connection_ref_unlocked (connection);
02478
02479
02480
02481
02482 retval = _dbus_watch_list_set_functions (connection->watches,
02483 add_function, remove_function,
02484 toggled_function,
02485 data, free_data_function);
02486
02487 CONNECTION_UNLOCK (connection);
02488
02489 dbus_connection_unref (connection);
02490
02491 return retval;
02492 }
02493
02527 dbus_bool_t
02528 dbus_connection_set_timeout_functions (DBusConnection *connection,
02529 DBusAddTimeoutFunction add_function,
02530 DBusRemoveTimeoutFunction remove_function,
02531 DBusTimeoutToggledFunction toggled_function,
02532 void *data,
02533 DBusFreeFunction free_data_function)
02534 {
02535 dbus_bool_t retval;
02536
02537 _dbus_return_val_if_fail (connection != NULL, FALSE);
02538
02539 CONNECTION_LOCK (connection);
02540
02541 _dbus_connection_ref_unlocked (connection);
02542
02543 retval = _dbus_timeout_list_set_functions (connection->timeouts,
02544 add_function, remove_function,
02545 toggled_function,
02546 data, free_data_function);
02547
02548 CONNECTION_UNLOCK (connection);
02549
02550 dbus_connection_unref (connection);
02551
02552 return retval;
02553 }
02554
02569 void
02570 dbus_connection_set_wakeup_main_function (DBusConnection *connection,
02571 DBusWakeupMainFunction wakeup_main_function,
02572 void *data,
02573 DBusFreeFunction free_data_function)
02574 {
02575 void *old_data;
02576 DBusFreeFunction old_free_data;
02577
02578 _dbus_return_if_fail (connection != NULL);
02579
02580 CONNECTION_LOCK (connection);
02581 old_data = connection->wakeup_main_data;
02582 old_free_data = connection->free_wakeup_main_data;
02583
02584 connection->wakeup_main_function = wakeup_main_function;
02585 connection->wakeup_main_data = data;
02586 connection->free_wakeup_main_data = free_data_function;
02587
02588 CONNECTION_UNLOCK (connection);
02589
02590
02591 if (old_free_data)
02592 (*old_free_data) (old_data);
02593 }
02594
02611 void
02612 dbus_connection_set_dispatch_status_function (DBusConnection *connection,
02613 DBusDispatchStatusFunction function,
02614 void *data,
02615 DBusFreeFunction free_data_function)
02616 {
02617 void *old_data;
02618 DBusFreeFunction old_free_data;
02619
02620 _dbus_return_if_fail (connection != NULL);
02621
02622 CONNECTION_LOCK (connection);
02623 old_data = connection->dispatch_status_data;
02624 old_free_data = connection->free_dispatch_status_data;
02625
02626 connection->dispatch_status_function = function;
02627 connection->dispatch_status_data = data;
02628 connection->free_dispatch_status_data = free_data_function;
02629
02630 CONNECTION_UNLOCK (connection);
02631
02632
02633 if (old_free_data)
02634 (*old_free_data) (old_data);
02635 }
02636
02648 dbus_bool_t
02649 dbus_connection_get_unix_user (DBusConnection *connection,
02650 unsigned long *uid)
02651 {
02652 dbus_bool_t result;
02653
02654 _dbus_return_val_if_fail (connection != NULL, FALSE);
02655 _dbus_return_val_if_fail (uid != NULL, FALSE);
02656
02657 CONNECTION_LOCK (connection);
02658
02659 if (!_dbus_transport_get_is_authenticated (connection->transport))
02660 result = FALSE;
02661 else
02662 result = _dbus_transport_get_unix_user (connection->transport,
02663 uid);
02664 CONNECTION_UNLOCK (connection);
02665
02666 return result;
02667 }
02668
02685 void
02686 dbus_connection_set_unix_user_function (DBusConnection *connection,
02687 DBusAllowUnixUserFunction function,
02688 void *data,
02689 DBusFreeFunction free_data_function)
02690 {
02691 void *old_data = NULL;
02692 DBusFreeFunction old_free_function = NULL;
02693
02694 _dbus_return_if_fail (connection != NULL);
02695
02696 CONNECTION_LOCK (connection);
02697 _dbus_transport_set_unix_user_function (connection->transport,
02698 function, data, free_data_function,
02699 &old_data, &old_free_function);
02700 CONNECTION_UNLOCK (connection);
02701
02702 if (old_free_function != NULL)
02703 (* old_free_function) (old_data);
02704 }
02705
02725 dbus_bool_t
02726 dbus_connection_add_filter (DBusConnection *connection,
02727 DBusMessageHandler *handler)
02728 {
02729 _dbus_return_val_if_fail (connection != NULL, FALSE);
02730 _dbus_return_val_if_fail (handler != NULL, FALSE);
02731
02732 CONNECTION_LOCK (connection);
02733 if (!_dbus_message_handler_add_connection (handler, connection))
02734 {
02735 CONNECTION_UNLOCK (connection);
02736 return FALSE;
02737 }
02738
02739 if (!_dbus_list_append (&connection->filter_list,
02740 handler))
02741 {
02742 _dbus_message_handler_remove_connection (handler, connection);
02743 CONNECTION_UNLOCK (connection);
02744 return FALSE;
02745 }
02746
02747 CONNECTION_UNLOCK (connection);
02748 return TRUE;
02749 }
02750
02762 void
02763 dbus_connection_remove_filter (DBusConnection *connection,
02764 DBusMessageHandler *handler)
02765 {
02766 _dbus_return_if_fail (connection != NULL);
02767 _dbus_return_if_fail (handler != NULL);
02768
02769 CONNECTION_LOCK (connection);
02770 if (!_dbus_list_remove_last (&connection->filter_list, handler))
02771 {
02772 _dbus_warn ("Tried to remove a DBusConnection filter that had not been added\n");
02773 CONNECTION_UNLOCK (connection);
02774 return;
02775 }
02776
02777 _dbus_message_handler_remove_connection (handler, connection);
02778
02779 CONNECTION_UNLOCK (connection);
02780 }
02781
02808 dbus_bool_t
02809 dbus_connection_register_handler (DBusConnection *connection,
02810 DBusMessageHandler *handler,
02811 const char **messages_to_handle,
02812 int n_messages)
02813 {
02814 int i;
02815
02816 _dbus_return_val_if_fail (connection != NULL, FALSE);
02817 _dbus_return_val_if_fail (handler != NULL, FALSE);
02818 _dbus_return_val_if_fail (n_messages >= 0, FALSE);
02819 _dbus_return_val_if_fail (n_messages == 0 || messages_to_handle != NULL, FALSE);
02820
02821 CONNECTION_LOCK (connection);
02822 i = 0;
02823 while (i < n_messages)
02824 {
02825 DBusHashIter iter;
02826 char *key;
02827
02828 key = _dbus_strdup (messages_to_handle[i]);
02829 if (key == NULL)
02830 goto failed;
02831
02832 if (!_dbus_hash_iter_lookup (connection->handler_table,
02833 key, TRUE,
02834 &iter))
02835 {
02836 dbus_free (key);
02837 goto failed;
02838 }
02839
02840 if (_dbus_hash_iter_get_value (&iter) != NULL)
02841 {
02842 _dbus_warn ("Bug in application: attempted to register a second handler for %s\n",
02843 messages_to_handle[i]);
02844 dbus_free (key);
02845 goto failed;
02846 }
02847
02848 if (!_dbus_message_handler_add_connection (handler, connection))
02849 {
02850 _dbus_hash_iter_remove_entry (&iter);
02851
02852 goto failed;
02853 }
02854
02855 _dbus_hash_iter_set_value (&iter, handler);
02856
02857 ++i;
02858 }
02859
02860 CONNECTION_UNLOCK (connection);
02861 return TRUE;
02862
02863 failed:
02864
02865
02866
02867 dbus_connection_unregister_handler (connection,
02868 handler,
02869 messages_to_handle,
02870 i);
02871
02872 CONNECTION_UNLOCK (connection);
02873 return FALSE;
02874 }
02875
02886 void
02887 dbus_connection_unregister_handler (DBusConnection *connection,
02888 DBusMessageHandler *handler,
02889 const char **messages_to_handle,
02890 int n_messages)
02891 {
02892 int i;
02893
02894 _dbus_return_if_fail (connection != NULL);
02895 _dbus_return_if_fail (handler != NULL);
02896 _dbus_return_if_fail (n_messages >= 0);
02897 _dbus_return_if_fail (n_messages == 0 || messages_to_handle != NULL);
02898
02899 CONNECTION_LOCK (connection);
02900 i = 0;
02901 while (i < n_messages)
02902 {
02903 DBusHashIter iter;
02904
02905 if (!_dbus_hash_iter_lookup (connection->handler_table,
02906 (char*) messages_to_handle[i], FALSE,
02907 &iter))
02908 {
02909 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was not registered\n",
02910 messages_to_handle[i]);
02911 }
02912 else if (_dbus_hash_iter_get_value (&iter) != handler)
02913 {
02914 _dbus_warn ("Bug in application: attempted to unregister handler for %s which was registered by a different handler\n",
02915 messages_to_handle[i]);
02916 }
02917 else
02918 {
02919 _dbus_hash_iter_remove_entry (&iter);
02920 _dbus_message_handler_remove_connection (handler, connection);
02921 }
02922
02923 ++i;
02924 }
02925
02926 CONNECTION_UNLOCK (connection);
02927 }
02928
02929 static DBusDataSlotAllocator slot_allocator;
02930 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
02931
02946 dbus_bool_t
02947 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
02948 {
02949 return _dbus_data_slot_allocator_alloc (&slot_allocator,
02950 _DBUS_LOCK_NAME (connection_slots),
02951 slot_p);
02952 }
02953
02965 void
02966 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
02967 {
02968 _dbus_return_if_fail (*slot_p >= 0);
02969
02970 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
02971 }
02972
02986 dbus_bool_t
02987 dbus_connection_set_data (DBusConnection *connection,
02988 dbus_int32_t slot,
02989 void *data,
02990 DBusFreeFunction free_data_func)
02991 {
02992 DBusFreeFunction old_free_func;
02993 void *old_data;
02994 dbus_bool_t retval;
02995
02996 _dbus_return_val_if_fail (connection != NULL, FALSE);
02997 _dbus_return_val_if_fail (slot >= 0, FALSE);
02998
02999 CONNECTION_LOCK (connection);
03000
03001 retval = _dbus_data_slot_list_set (&slot_allocator,
03002 &connection->slot_list,
03003 slot, data, free_data_func,
03004 &old_free_func, &old_data);
03005
03006 CONNECTION_UNLOCK (connection);
03007
03008 if (retval)
03009 {
03010
03011 if (old_free_func)
03012 (* old_free_func) (old_data);
03013 }
03014
03015 return retval;
03016 }
03017
03026 void*
03027 dbus_connection_get_data (DBusConnection *connection,
03028 dbus_int32_t slot)
03029 {
03030 void *res;
03031
03032 _dbus_return_val_if_fail (connection != NULL, NULL);
03033
03034 CONNECTION_LOCK (connection);
03035
03036 res = _dbus_data_slot_list_get (&slot_allocator,
03037 &connection->slot_list,
03038 slot);
03039
03040 CONNECTION_UNLOCK (connection);
03041
03042 return res;
03043 }
03044
03051 void
03052 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
03053 {
03054 _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
03055 }
03056
03065 void
03066 dbus_connection_set_max_message_size (DBusConnection *connection,
03067 long size)
03068 {
03069 _dbus_return_if_fail (connection != NULL);
03070
03071 CONNECTION_LOCK (connection);
03072 _dbus_transport_set_max_message_size (connection->transport,
03073 size);
03074 CONNECTION_UNLOCK (connection);
03075 }
03076
03083 long
03084 dbus_connection_get_max_message_size (DBusConnection *connection)
03085 {
03086 long res;
03087
03088 _dbus_return_val_if_fail (connection != NULL, 0);
03089
03090 CONNECTION_LOCK (connection);
03091 res = _dbus_transport_get_max_message_size (connection->transport);
03092 CONNECTION_UNLOCK (connection);
03093 return res;
03094 }
03095
03121 void
03122 dbus_connection_set_max_received_size (DBusConnection *connection,
03123 long size)
03124 {
03125 _dbus_return_if_fail (connection != NULL);
03126
03127 CONNECTION_LOCK (connection);
03128 _dbus_transport_set_max_received_size (connection->transport,
03129 size);
03130 CONNECTION_UNLOCK (connection);
03131 }
03132
03139 long
03140 dbus_connection_get_max_received_size (DBusConnection *connection)
03141 {
03142 long res;
03143
03144 _dbus_return_val_if_fail (connection != NULL, 0);
03145
03146 CONNECTION_LOCK (connection);
03147 res = _dbus_transport_get_max_received_size (connection->transport);
03148 CONNECTION_UNLOCK (connection);
03149 return res;
03150 }
03151
03162 long
03163 dbus_connection_get_outgoing_size (DBusConnection *connection)
03164 {
03165 long res;
03166
03167 _dbus_return_val_if_fail (connection != NULL, 0);
03168
03169 CONNECTION_LOCK (connection);
03170 res = _dbus_counter_get_value (connection->outgoing_counter);
03171 CONNECTION_UNLOCK (connection);
03172 return res;
03173 }
03174