dbus-connection.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-connection.c DBusConnection object
00003  *
00004  * Copyright (C) 2002-2006  Red Hat Inc.
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 #include <config.h>
00025 #include "dbus-shared.h"
00026 #include "dbus-connection.h"
00027 #include "dbus-list.h"
00028 #include "dbus-timeout.h"
00029 #include "dbus-transport.h"
00030 #include "dbus-watch.h"
00031 #include "dbus-connection-internal.h"
00032 #include "dbus-pending-call-internal.h"
00033 #include "dbus-list.h"
00034 #include "dbus-hash.h"
00035 #include "dbus-message-internal.h"
00036 #include "dbus-threads.h"
00037 #include "dbus-protocol.h"
00038 #include "dbus-dataslot.h"
00039 #include "dbus-string.h"
00040 #include "dbus-pending-call.h"
00041 #include "dbus-object-tree.h"
00042 #include "dbus-threads-internal.h"
00043 #include "dbus-bus.h"
00044 
00045 #ifdef DBUS_DISABLE_CHECKS
00046 #define TOOK_LOCK_CHECK(connection)
00047 #define RELEASING_LOCK_CHECK(connection)
00048 #define HAVE_LOCK_CHECK(connection)
00049 #else
00050 #define TOOK_LOCK_CHECK(connection) do {                \
00051     _dbus_assert (!(connection)->have_connection_lock); \
00052     (connection)->have_connection_lock = TRUE;          \
00053   } while (0)
00054 #define RELEASING_LOCK_CHECK(connection) do {            \
00055     _dbus_assert ((connection)->have_connection_lock);   \
00056     (connection)->have_connection_lock = FALSE;          \
00057   } while (0)
00058 #define HAVE_LOCK_CHECK(connection)        _dbus_assert ((connection)->have_connection_lock)
00059 /* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
00060 #endif
00061 
00062 #define TRACE_LOCKS 1
00063 
00064 #define CONNECTION_LOCK(connection)   do {                                      \
00065     if (TRACE_LOCKS) { _dbus_verbose ("  LOCK: %s\n", _DBUS_FUNCTION_NAME); }   \
00066     _dbus_mutex_lock ((connection)->mutex);                                      \
00067     TOOK_LOCK_CHECK (connection);                                               \
00068   } while (0)
00069 
00070 #define CONNECTION_UNLOCK(connection) do {                                              \
00071     if (TRACE_LOCKS) { _dbus_verbose ("  UNLOCK: %s\n", _DBUS_FUNCTION_NAME);  }        \
00072     RELEASING_LOCK_CHECK (connection);                                                  \
00073     _dbus_mutex_unlock ((connection)->mutex);                                            \
00074   } while (0)
00075 
00076 #define DISPATCH_STATUS_NAME(s)                                            \
00077                      ((s) == DBUS_DISPATCH_COMPLETE ? "complete" :         \
00078                       (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
00079                       (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" :   \
00080                       "???")
00081 
00202 typedef struct DBusMessageFilter DBusMessageFilter;
00203 
00207 struct DBusMessageFilter
00208 {
00209   DBusAtomic refcount; 
00210   DBusHandleMessageFunction function; 
00211   void *user_data; 
00212   DBusFreeFunction free_user_data_function; 
00213 };
00214 
00215 
00219 struct DBusPreallocatedSend
00220 {
00221   DBusConnection *connection; 
00222   DBusList *queue_link;       
00223   DBusList *counter_link;     
00224 };
00225 
00226 static dbus_bool_t _dbus_modify_sigpipe = TRUE;
00227 
00231 struct DBusConnection
00232 {
00233   DBusAtomic refcount; 
00235   DBusMutex *mutex; 
00237   DBusMutex *dispatch_mutex;     
00238   DBusCondVar *dispatch_cond;    
00239   DBusMutex *io_path_mutex;      
00240   DBusCondVar *io_path_cond;     
00242   DBusList *outgoing_messages; 
00243   DBusList *incoming_messages; 
00245   DBusMessage *message_borrowed; 
00249   int n_outgoing;              
00250   int n_incoming;              
00252   DBusCounter *outgoing_counter; 
00254   DBusTransport *transport;    
00255   DBusWatchList *watches;      
00256   DBusTimeoutList *timeouts;   
00258   DBusList *filter_list;        
00260   DBusDataSlotList slot_list;   
00262   DBusHashTable *pending_replies;  
00264   dbus_uint32_t client_serial;       
00265   DBusList *disconnect_message_link; 
00267   DBusWakeupMainFunction wakeup_main_function; 
00268   void *wakeup_main_data; 
00269   DBusFreeFunction free_wakeup_main_data; 
00271   DBusDispatchStatusFunction dispatch_status_function; 
00272   void *dispatch_status_data; 
00273   DBusFreeFunction free_dispatch_status_data; 
00275   DBusDispatchStatus last_dispatch_status; 
00277   DBusList *link_cache; 
00280   DBusObjectTree *objects; 
00282   char *server_guid; 
00284   /* These two MUST be bools and not bitfields, because they are protected by a separate lock
00285    * from connection->mutex and all bitfields in a word have to be read/written together.
00286    * So you can't have a different lock for different bitfields in the same word.
00287    */
00288   dbus_bool_t dispatch_acquired; 
00289   dbus_bool_t io_path_acquired;  
00291   unsigned int shareable : 1; 
00293   unsigned int exit_on_disconnect : 1; 
00295   unsigned int route_peer_messages : 1; 
00297   unsigned int disconnected_message_arrived : 1;   
00301   unsigned int disconnected_message_processed : 1; 
00305 #ifndef DBUS_DISABLE_CHECKS
00306   unsigned int have_connection_lock : 1; 
00307 #endif
00308   
00309 #ifndef DBUS_DISABLE_CHECKS
00310   int generation; 
00311 #endif 
00312 };
00313 
00314 static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked      (DBusConnection     *connection);
00315 static void               _dbus_connection_update_dispatch_status_and_unlock (DBusConnection     *connection,
00316                                                                               DBusDispatchStatus  new_status);
00317 static void               _dbus_connection_last_unref                        (DBusConnection     *connection);
00318 static void               _dbus_connection_acquire_dispatch                  (DBusConnection     *connection);
00319 static void               _dbus_connection_release_dispatch                  (DBusConnection     *connection);
00320 static DBusDispatchStatus _dbus_connection_flush_unlocked                    (DBusConnection     *connection);
00321 static void               _dbus_connection_close_possibly_shared_and_unlock  (DBusConnection     *connection);
00322 static dbus_bool_t        _dbus_connection_get_is_connected_unlocked         (DBusConnection     *connection);
00323 
00324 static DBusMessageFilter *
00325 _dbus_message_filter_ref (DBusMessageFilter *filter)
00326 {
00327   _dbus_assert (filter->refcount.value > 0);
00328   _dbus_atomic_inc (&filter->refcount);
00329 
00330   return filter;
00331 }
00332 
00333 static void
00334 _dbus_message_filter_unref (DBusMessageFilter *filter)
00335 {
00336   _dbus_assert (filter->refcount.value > 0);
00337 
00338   if (_dbus_atomic_dec (&filter->refcount) == 1)
00339     {
00340       if (filter->free_user_data_function)
00341         (* filter->free_user_data_function) (filter->user_data);
00342       
00343       dbus_free (filter);
00344     }
00345 }
00346 
00352 void
00353 _dbus_connection_lock (DBusConnection *connection)
00354 {
00355   CONNECTION_LOCK (connection);
00356 }
00357 
00363 void
00364 _dbus_connection_unlock (DBusConnection *connection)
00365 {
00366   CONNECTION_UNLOCK (connection);
00367 }
00368 
00376 static void
00377 _dbus_connection_wakeup_mainloop (DBusConnection *connection)
00378 {
00379   if (connection->wakeup_main_function)
00380     (*connection->wakeup_main_function) (connection->wakeup_main_data);
00381 }
00382 
00383 #ifdef DBUS_BUILD_TESTS
00384 /* For now this function isn't used */
00394 dbus_bool_t
00395 _dbus_connection_queue_received_message (DBusConnection *connection,
00396                                          DBusMessage    *message)
00397 {
00398   DBusList *link;
00399 
00400   link = _dbus_list_alloc_link (message);
00401   if (link == NULL)
00402     return FALSE;
00403 
00404   dbus_message_ref (message);
00405   _dbus_connection_queue_received_message_link (connection, link);
00406 
00407   return TRUE;
00408 }
00409 
00422 void 
00423 _dbus_connection_test_get_locks (DBusConnection *connection,
00424                                  DBusMutex     **mutex_loc,
00425                                  DBusMutex     **dispatch_mutex_loc,
00426                                  DBusMutex     **io_path_mutex_loc,
00427                                  DBusCondVar   **dispatch_cond_loc,
00428                                  DBusCondVar   **io_path_cond_loc)
00429 {
00430   *mutex_loc = connection->mutex;
00431   *dispatch_mutex_loc = connection->dispatch_mutex;
00432   *io_path_mutex_loc = connection->io_path_mutex; 
00433   *dispatch_cond_loc = connection->dispatch_cond;
00434   *io_path_cond_loc = connection->io_path_cond;
00435 }
00436 #endif
00437 
00446 void
00447 _dbus_connection_queue_received_message_link (DBusConnection  *connection,
00448                                               DBusList        *link)
00449 {
00450   DBusPendingCall *pending;
00451   dbus_int32_t reply_serial;
00452   DBusMessage *message;
00453   
00454   _dbus_assert (_dbus_transport_get_is_authenticated (connection->transport));
00455   
00456   _dbus_list_append_link (&connection->incoming_messages,
00457                           link);
00458   message = link->data;
00459 
00460   /* If this is a reply we're waiting on, remove timeout for it */
00461   reply_serial = dbus_message_get_reply_serial (message);
00462   if (reply_serial != -1)
00463     {
00464       pending = _dbus_hash_table_lookup_int (connection->pending_replies,
00465                                              reply_serial);
00466       if (pending != NULL)
00467         {
00468           if (_dbus_pending_call_is_timeout_added_unlocked (pending))
00469             _dbus_connection_remove_timeout_unlocked (connection,
00470                                                       _dbus_pending_call_get_timeout_unlocked (pending));
00471 
00472           _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
00473         }
00474     }
00475   
00476   
00477 
00478   connection->n_incoming += 1;
00479 
00480   _dbus_connection_wakeup_mainloop (connection);
00481   
00482   _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
00483                  message,
00484                  dbus_message_get_type (message),
00485                  dbus_message_get_path (message) ?
00486                  dbus_message_get_path (message) :
00487                  "no path",
00488                  dbus_message_get_interface (message) ?
00489                  dbus_message_get_interface (message) :
00490                  "no interface",
00491                  dbus_message_get_member (message) ?
00492                  dbus_message_get_member (message) :
00493                  "no member",
00494                  dbus_message_get_signature (message),
00495                  dbus_message_get_reply_serial (message),
00496                  connection,
00497                  connection->n_incoming);}
00498 
00507 void
00508 _dbus_connection_queue_synthesized_message_link (DBusConnection *connection,
00509                                                  DBusList *link)
00510 {
00511   HAVE_LOCK_CHECK (connection);
00512   
00513   _dbus_list_append_link (&connection->incoming_messages, link);
00514 
00515   connection->n_incoming += 1;
00516 
00517   _dbus_connection_wakeup_mainloop (connection);
00518   
00519   _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
00520                  link->data, connection, connection->n_incoming);
00521 }
00522 
00523 
00531 dbus_bool_t
00532 _dbus_connection_has_messages_to_send_unlocked (DBusConnection *connection)
00533 {
00534   HAVE_LOCK_CHECK (connection);
00535   return connection->outgoing_messages != NULL;
00536 }
00537 
00547 dbus_bool_t
00548 dbus_connection_has_messages_to_send (DBusConnection *connection)
00549 {
00550   dbus_bool_t v;
00551   
00552   _dbus_return_val_if_fail (connection != NULL, FALSE);
00553 
00554   CONNECTION_LOCK (connection);
00555   v = _dbus_connection_has_messages_to_send_unlocked (connection);
00556   CONNECTION_UNLOCK (connection);
00557 
00558   return v;
00559 }
00560 
00568 DBusMessage*
00569 _dbus_connection_get_message_to_send (DBusConnection *connection)
00570 {
00571   HAVE_LOCK_CHECK (connection);
00572   
00573   return _dbus_list_get_last (&connection->outgoing_messages);
00574 }
00575 
00584 void
00585 _dbus_connection_message_sent (DBusConnection *connection,
00586                                DBusMessage    *message)
00587 {
00588   DBusList *link;
00589 
00590   HAVE_LOCK_CHECK (connection);
00591   
00592   /* This can be called before we even complete authentication, since
00593    * it's called on disconnect to clean up the outgoing queue.
00594    * It's also called as we successfully send each message.
00595    */
00596   
00597   link = _dbus_list_get_last_link (&connection->outgoing_messages);
00598   _dbus_assert (link != NULL);
00599   _dbus_assert (link->data == message);
00600 
00601   /* Save this link in the link cache */
00602   _dbus_list_unlink (&connection->outgoing_messages,
00603                      link);
00604   _dbus_list_prepend_link (&connection->link_cache, link);
00605   
00606   connection->n_outgoing -= 1;
00607 
00608   _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
00609                  message,
00610                  dbus_message_get_type (message),
00611                  dbus_message_get_path (message) ?
00612                  dbus_message_get_path (message) :
00613                  "no path",
00614                  dbus_message_get_interface (message) ?
00615                  dbus_message_get_interface (message) :
00616                  "no interface",
00617                  dbus_message_get_member (message) ?
00618                  dbus_message_get_member (message) :
00619                  "no member",
00620                  dbus_message_get_signature (message),
00621                  connection, connection->n_outgoing);
00622 
00623   /* Save this link in the link cache also */
00624   _dbus_message_remove_size_counter (message, connection->outgoing_counter,
00625                                      &link);
00626   _dbus_list_prepend_link (&connection->link_cache, link);
00627   
00628   dbus_message_unref (message);
00629 }
00630 
00632 typedef dbus_bool_t (* DBusWatchAddFunction)     (DBusWatchList *list,
00633                                                   DBusWatch     *watch);
00635 typedef void        (* DBusWatchRemoveFunction)  (DBusWatchList *list,
00636                                                   DBusWatch     *watch);
00638 typedef void        (* DBusWatchToggleFunction)  (DBusWatchList *list,
00639                                                   DBusWatch     *watch,
00640                                                   dbus_bool_t    enabled);
00641 
00642 static dbus_bool_t
00643 protected_change_watch (DBusConnection         *connection,
00644                         DBusWatch              *watch,
00645                         DBusWatchAddFunction    add_function,
00646                         DBusWatchRemoveFunction remove_function,
00647                         DBusWatchToggleFunction toggle_function,
00648                         dbus_bool_t             enabled)
00649 {
00650   DBusWatchList *watches;
00651   dbus_bool_t retval;
00652   
00653   HAVE_LOCK_CHECK (connection);
00654 
00655   /* This isn't really safe or reasonable; a better pattern is the "do everything, then
00656    * drop lock and call out" one; but it has to be propagated up through all callers
00657    */
00658   
00659   watches = connection->watches;
00660   if (watches)
00661     {
00662       connection->watches = NULL;
00663       _dbus_connection_ref_unlocked (connection);
00664       CONNECTION_UNLOCK (connection);
00665 
00666       if (add_function)
00667         retval = (* add_function) (watches, watch);
00668       else if (remove_function)
00669         {
00670           retval = TRUE;
00671           (* remove_function) (watches, watch);
00672         }
00673       else
00674         {
00675           retval = TRUE;
00676           (* toggle_function) (watches, watch, enabled);
00677         }
00678       
00679       CONNECTION_LOCK (connection);
00680       connection->watches = watches;
00681       _dbus_connection_unref_unlocked (connection);
00682 
00683       return retval;
00684     }
00685   else
00686     return FALSE;
00687 }
00688      
00689 
00701 dbus_bool_t
00702 _dbus_connection_add_watch_unlocked (DBusConnection *connection,
00703                                      DBusWatch      *watch)
00704 {
00705   return protected_change_watch (connection, watch,
00706                                  _dbus_watch_list_add_watch,
00707                                  NULL, NULL, FALSE);
00708 }
00709 
00719 void
00720 _dbus_connection_remove_watch_unlocked (DBusConnection *connection,
00721                                         DBusWatch      *watch)
00722 {
00723   protected_change_watch (connection, watch,
00724                           NULL,
00725                           _dbus_watch_list_remove_watch,
00726                           NULL, FALSE);
00727 }
00728 
00739 void
00740 _dbus_connection_toggle_watch_unlocked (DBusConnection *connection,
00741                                         DBusWatch      *watch,
00742                                         dbus_bool_t     enabled)
00743 {
00744   _dbus_assert (watch != NULL);
00745 
00746   protected_change_watch (connection, watch,
00747                           NULL, NULL,
00748                           _dbus_watch_list_toggle_watch,
00749                           enabled);
00750 }
00751 
00753 typedef dbus_bool_t (* DBusTimeoutAddFunction)    (DBusTimeoutList *list,
00754                                                    DBusTimeout     *timeout);
00756 typedef void        (* DBusTimeoutRemoveFunction) (DBusTimeoutList *list,
00757                                                    DBusTimeout     *timeout);
00759 typedef void        (* DBusTimeoutToggleFunction) (DBusTimeoutList *list,
00760                                                    DBusTimeout     *timeout,
00761                                                    dbus_bool_t      enabled);
00762 
00763 static dbus_bool_t
00764 protected_change_timeout (DBusConnection           *connection,
00765                           DBusTimeout              *timeout,
00766                           DBusTimeoutAddFunction    add_function,
00767                           DBusTimeoutRemoveFunction remove_function,
00768                           DBusTimeoutToggleFunction toggle_function,
00769                           dbus_bool_t               enabled)
00770 {
00771   DBusTimeoutList *timeouts;
00772   dbus_bool_t retval;
00773   
00774   HAVE_LOCK_CHECK (connection);
00775 
00776   /* This isn't really safe or reasonable; a better pattern is the "do everything, then
00777    * drop lock and call out" one; but it has to be propagated up through all callers
00778    */
00779   
00780   timeouts = connection->timeouts;
00781   if (timeouts)
00782     {
00783       connection->timeouts = NULL;
00784       _dbus_connection_ref_unlocked (connection);
00785       CONNECTION_UNLOCK (connection);
00786 
00787       if (add_function)
00788         retval = (* add_function) (timeouts, timeout);
00789       else if (remove_function)
00790         {
00791           retval = TRUE;
00792           (* remove_function) (timeouts, timeout);
00793         }
00794       else
00795         {
00796           retval = TRUE;
00797           (* toggle_function) (timeouts, timeout, enabled);
00798         }
00799       
00800       CONNECTION_LOCK (connection);
00801       connection->timeouts = timeouts;
00802       _dbus_connection_unref_unlocked (connection);
00803 
00804       return retval;
00805     }
00806   else
00807     return FALSE;
00808 }
00809 
00822 dbus_bool_t
00823 _dbus_connection_add_timeout_unlocked (DBusConnection *connection,
00824                                        DBusTimeout    *timeout)
00825 {
00826   return protected_change_timeout (connection, timeout,
00827                                    _dbus_timeout_list_add_timeout,
00828                                    NULL, NULL, FALSE);
00829 }
00830 
00840 void
00841 _dbus_connection_remove_timeout_unlocked (DBusConnection *connection,
00842                                           DBusTimeout    *timeout)
00843 {
00844   protected_change_timeout (connection, timeout,
00845                             NULL,
00846                             _dbus_timeout_list_remove_timeout,
00847                             NULL, FALSE);
00848 }
00849 
00860 void
00861 _dbus_connection_toggle_timeout_unlocked (DBusConnection   *connection,
00862                                           DBusTimeout      *timeout,
00863                                           dbus_bool_t       enabled)
00864 {
00865   protected_change_timeout (connection, timeout,
00866                             NULL, NULL,
00867                             _dbus_timeout_list_toggle_timeout,
00868                             enabled);
00869 }
00870 
00871 static dbus_bool_t
00872 _dbus_connection_attach_pending_call_unlocked (DBusConnection  *connection,
00873                                                DBusPendingCall *pending)
00874 {
00875   dbus_uint32_t reply_serial;
00876   DBusTimeout *timeout;
00877 
00878   HAVE_LOCK_CHECK (connection);
00879 
00880   reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
00881 
00882   _dbus_assert (reply_serial != 0);
00883 
00884   timeout = _dbus_pending_call_get_timeout_unlocked (pending);
00885 
00886   if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
00887     return FALSE;
00888   
00889   if (!_dbus_hash_table_insert_int (connection->pending_replies,
00890                                     reply_serial,
00891                                     pending))
00892     {
00893       _dbus_connection_remove_timeout_unlocked (connection, timeout);
00894 
00895       _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
00896       HAVE_LOCK_CHECK (connection);
00897       return FALSE;
00898     }
00899   
00900   _dbus_pending_call_set_timeout_added_unlocked (pending, TRUE);
00901 
00902   _dbus_pending_call_ref_unlocked (pending);
00903 
00904   HAVE_LOCK_CHECK (connection);
00905   
00906   return TRUE;
00907 }
00908 
00909 static void
00910 free_pending_call_on_hash_removal (void *data)
00911 {
00912   DBusPendingCall *pending;
00913   DBusConnection  *connection;
00914   
00915   if (data == NULL)
00916     return;
00917 
00918   pending = data;
00919 
00920   connection = _dbus_pending_call_get_connection_unlocked (pending);
00921 
00922   HAVE_LOCK_CHECK (connection);
00923   
00924   if (_dbus_pending_call_is_timeout_added_unlocked (pending))
00925     {
00926       _dbus_connection_remove_timeout_unlocked (connection,
00927                                                 _dbus_pending_call_get_timeout_unlocked (pending));
00928       
00929       _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
00930     }
00931 
00932   /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock 
00933    * here, but the pending call finalizer could in principle call out to 
00934    * application code so we pretty much have to... some larger code reorg 
00935    * might be needed.
00936    */
00937   _dbus_connection_ref_unlocked (connection);
00938   _dbus_pending_call_unref_and_unlock (pending);
00939   CONNECTION_LOCK (connection);
00940   _dbus_connection_unref_unlocked (connection);
00941 }
00942 
00943 static void
00944 _dbus_connection_detach_pending_call_unlocked (DBusConnection  *connection,
00945                                                DBusPendingCall *pending)
00946 {
00947   /* This ends up unlocking to call the pending call finalizer, which is unexpected to
00948    * say the least.
00949    */
00950   _dbus_hash_table_remove_int (connection->pending_replies,
00951                                _dbus_pending_call_get_reply_serial_unlocked (pending));
00952 }
00953 
00954 static void
00955 _dbus_connection_detach_pending_call_and_unlock (DBusConnection  *connection,
00956                                                  DBusPendingCall *pending)
00957 {
00958   /* The idea here is to avoid finalizing the pending call
00959    * with the lock held, since there's a destroy notifier
00960    * in pending call that goes out to application code.
00961    *
00962    * There's an extra unlock inside the hash table
00963    * "free pending call" function FIXME...
00964    */
00965   _dbus_pending_call_ref_unlocked (pending);
00966   _dbus_hash_table_remove_int (connection->pending_replies,
00967                                _dbus_pending_call_get_reply_serial_unlocked (pending));
00968   _dbus_pending_call_unref_and_unlock (pending);
00969 }
00970 
00979 void
00980 _dbus_connection_remove_pending_call (DBusConnection  *connection,
00981                                       DBusPendingCall *pending)
00982 {
00983   CONNECTION_LOCK (connection);
00984   _dbus_connection_detach_pending_call_and_unlock (connection, pending);
00985 }
00986 
00996 static dbus_bool_t
00997 _dbus_connection_acquire_io_path (DBusConnection *connection,
00998                                   int             timeout_milliseconds)
00999 {
01000   dbus_bool_t we_acquired;
01001   
01002   HAVE_LOCK_CHECK (connection);
01003 
01004   /* We don't want the connection to vanish */
01005   _dbus_connection_ref_unlocked (connection);
01006 
01007   /* We will only touch io_path_acquired which is protected by our mutex */
01008   CONNECTION_UNLOCK (connection);
01009   
01010   _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
01011   _dbus_mutex_lock (connection->io_path_mutex);
01012 
01013   _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
01014                  _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
01015 
01016   we_acquired = FALSE;
01017   
01018   if (connection->io_path_acquired)
01019     {
01020       if (timeout_milliseconds != -1)
01021         {
01022           _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
01023                          _DBUS_FUNCTION_NAME, timeout_milliseconds);
01024 
01025           if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
01026                                            connection->io_path_mutex,
01027                                            timeout_milliseconds))
01028             {
01029               /* We timed out before anyone signaled. */
01030               /* (writing the loop to handle the !timedout case by
01031                * waiting longer if needed is a pain since dbus
01032                * wraps pthread_cond_timedwait to take a relative
01033                * time instead of absolute, something kind of stupid
01034                * on our part. for now it doesn't matter, we will just
01035                * end up back here eventually.)
01036                */
01037             }
01038         }
01039       else
01040         {
01041           while (connection->io_path_acquired)
01042             {
01043               _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
01044               _dbus_condvar_wait (connection->io_path_cond, 
01045                                   connection->io_path_mutex);
01046             }
01047         }
01048     }
01049   
01050   if (!connection->io_path_acquired)
01051     {
01052       we_acquired = TRUE;
01053       connection->io_path_acquired = TRUE;
01054     }
01055   
01056   _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
01057                  _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
01058 
01059   _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
01060   _dbus_mutex_unlock (connection->io_path_mutex);
01061 
01062   CONNECTION_LOCK (connection);
01063   
01064   HAVE_LOCK_CHECK (connection);
01065 
01066   _dbus_connection_unref_unlocked (connection);
01067   
01068   return we_acquired;
01069 }
01070 
01078 static void
01079 _dbus_connection_release_io_path (DBusConnection *connection)
01080 {
01081   HAVE_LOCK_CHECK (connection);
01082   
01083   _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
01084   _dbus_mutex_lock (connection->io_path_mutex);
01085   
01086   _dbus_assert (connection->io_path_acquired);
01087 
01088   _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
01089                  _DBUS_FUNCTION_NAME, connection->io_path_acquired);
01090   
01091   connection->io_path_acquired = FALSE;
01092   _dbus_condvar_wake_one (connection->io_path_cond);
01093 
01094   _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
01095   _dbus_mutex_unlock (connection->io_path_mutex);
01096 }
01097 
01126 void
01127 _dbus_connection_do_iteration_unlocked (DBusConnection *connection,
01128                                         unsigned int    flags,
01129                                         int             timeout_milliseconds)
01130 {
01131   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
01132   
01133   HAVE_LOCK_CHECK (connection);
01134   
01135   if (connection->n_outgoing == 0)
01136     flags &= ~DBUS_ITERATION_DO_WRITING;
01137 
01138   if (_dbus_connection_acquire_io_path (connection,
01139                                         (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
01140     {
01141       HAVE_LOCK_CHECK (connection);
01142       
01143       _dbus_transport_do_iteration (connection->transport,
01144                                     flags, timeout_milliseconds);
01145       _dbus_connection_release_io_path (connection);
01146     }
01147 
01148   HAVE_LOCK_CHECK (connection);
01149 
01150   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
01151 }
01152 
01162 DBusConnection*
01163 _dbus_connection_new_for_transport (DBusTransport *transport)
01164 {
01165   DBusConnection *connection;
01166   DBusWatchList *watch_list;
01167   DBusTimeoutList *timeout_list;
01168   DBusHashTable *pending_replies;
01169   DBusList *disconnect_link;
01170   DBusMessage *disconnect_message;
01171   DBusCounter *outgoing_counter;
01172   DBusObjectTree *objects;
01173   
01174   watch_list = NULL;
01175   connection = NULL;
01176   pending_replies = NULL;
01177   timeout_list = NULL;
01178   disconnect_link = NULL;
01179   disconnect_message = NULL;
01180   outgoing_counter = NULL;
01181   objects = NULL;
01182   
01183   watch_list = _dbus_watch_list_new ();
01184   if (watch_list == NULL)
01185     goto error;
01186 
01187   timeout_list = _dbus_timeout_list_new ();
01188   if (timeout_list == NULL)
01189     goto error;  
01190 
01191   pending_replies =
01192     _dbus_hash_table_new (DBUS_HASH_INT,
01193                           NULL,
01194                           (DBusFreeFunction)free_pending_call_on_hash_removal);
01195   if (pending_replies == NULL)
01196     goto error;
01197   
01198   connection = dbus_new0 (DBusConnection, 1);
01199   if (connection == NULL)
01200     goto error;
01201 
01202   _dbus_mutex_new_at_location (&connection->mutex);
01203   if (connection->mutex == NULL)
01204     goto error;
01205 
01206   _dbus_mutex_new_at_location (&connection->io_path_mutex);
01207   if (connection->io_path_mutex == NULL)
01208     goto error;
01209 
01210   _dbus_mutex_new_at_location (&connection->dispatch_mutex);
01211   if (connection->dispatch_mutex == NULL)
01212     goto error;
01213   
01214   _dbus_condvar_new_at_location (&connection->dispatch_cond);
01215   if (connection->dispatch_cond == NULL)
01216     goto error;
01217   
01218   _dbus_condvar_new_at_location (&connection->io_path_cond);
01219   if (connection->io_path_cond == NULL)
01220     goto error;
01221 
01222   disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
01223                                                 DBUS_INTERFACE_LOCAL,
01224                                                 "Disconnected");
01225   
01226   if (disconnect_message == NULL)
01227     goto error;
01228 
01229   disconnect_link = _dbus_list_alloc_link (disconnect_message);
01230   if (disconnect_link == NULL)
01231     goto error;
01232 
01233   outgoing_counter = _dbus_counter_new ();
01234   if (outgoing_counter == NULL)
01235     goto error;
01236 
01237   objects = _dbus_object_tree_new (connection);
01238   if (objects == NULL)
01239     goto error;
01240   
01241   if (_dbus_modify_sigpipe)
01242     _dbus_disable_sigpipe ();
01243   
01244   connection->refcount.value = 1;
01245   connection->transport = transport;
01246   connection->watches = watch_list;
01247   connection->timeouts = timeout_list;
01248   connection->pending_replies = pending_replies;
01249   connection->outgoing_counter = outgoing_counter;
01250   connection->filter_list = NULL;
01251   connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
01252   connection->objects = objects;
01253   connection->exit_on_disconnect = FALSE;
01254   connection->shareable = FALSE;
01255   connection->route_peer_messages = FALSE;
01256   connection->disconnected_message_arrived = FALSE;
01257   connection->disconnected_message_processed = FALSE;
01258   
01259 #ifndef DBUS_DISABLE_CHECKS
01260   connection->generation = _dbus_current_generation;
01261 #endif
01262   
01263   _dbus_data_slot_list_init (&connection->slot_list);
01264 
01265   connection->client_serial = 1;
01266 
01267   connection->disconnect_message_link = disconnect_link;
01268 
01269   CONNECTION_LOCK (connection);
01270   
01271   if (!_dbus_transport_set_connection (transport, connection))
01272     {
01273       CONNECTION_UNLOCK (connection);
01274 
01275       goto error;
01276     }
01277 
01278   _dbus_transport_ref (transport);
01279 
01280   CONNECTION_UNLOCK (connection);
01281   
01282   return connection;
01283   
01284  error:
01285   if (disconnect_message != NULL)
01286     dbus_message_unref (disconnect_message);
01287   
01288   if (disconnect_link != NULL)
01289     _dbus_list_free_link (disconnect_link);
01290   
01291   if (connection != NULL)
01292     {
01293       _dbus_condvar_free_at_location (&connection->io_path_cond);
01294       _dbus_condvar_free_at_location (&connection->dispatch_cond);
01295       _dbus_mutex_free_at_location (&connection->mutex);
01296       _dbus_mutex_free_at_location (&connection->io_path_mutex);
01297       _dbus_mutex_free_at_location (&connection->dispatch_mutex);
01298       dbus_free (connection);
01299     }
01300   if (pending_replies)
01301     _dbus_hash_table_unref (pending_replies);
01302   
01303   if (watch_list)
01304     _dbus_watch_list_free (watch_list);
01305 
01306   if (timeout_list)
01307     _dbus_timeout_list_free (timeout_list);
01308 
01309   if (outgoing_counter)
01310     _dbus_counter_unref (outgoing_counter);
01311 
01312   if (objects)
01313     _dbus_object_tree_unref (objects);
01314   
01315   return NULL;
01316 }
01317 
01325 DBusConnection *
01326 _dbus_connection_ref_unlocked (DBusConnection *connection)
01327 {  
01328   _dbus_assert (connection != NULL);
01329   _dbus_assert (connection->generation == _dbus_current_generation);
01330 
01331   HAVE_LOCK_CHECK (connection);
01332   
01333 #ifdef DBUS_HAVE_ATOMIC_INT
01334   _dbus_atomic_inc (&connection->refcount);
01335 #else
01336   _dbus_assert (connection->refcount.value > 0);
01337   connection->refcount.value += 1;
01338 #endif
01339 
01340   return connection;
01341 }
01342 
01349 void
01350 _dbus_connection_unref_unlocked (DBusConnection *connection)
01351 {
01352   dbus_bool_t last_unref;
01353 
01354   HAVE_LOCK_CHECK (connection);
01355   
01356   _dbus_assert (connection != NULL);
01357 
01358   /* The connection lock is better than the global
01359    * lock in the atomic increment fallback
01360    */
01361   
01362 #ifdef DBUS_HAVE_ATOMIC_INT
01363   last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
01364 #else
01365   _dbus_assert (connection->refcount.value > 0);
01366 
01367   connection->refcount.value -= 1;
01368   last_unref = (connection->refcount.value == 0);  
01369 #if 0
01370   printf ("unref_unlocked() connection %p count = %d\n", connection, connection->refcount.value);
01371 #endif
01372 #endif
01373   
01374   if (last_unref)
01375     _dbus_connection_last_unref (connection);
01376 }
01377 
01378 static dbus_uint32_t
01379 _dbus_connection_get_next_client_serial (DBusConnection *connection)
01380 {
01381   int serial;
01382 
01383   serial = connection->client_serial++;
01384 
01385   if (connection->client_serial < 0)
01386     connection->client_serial = 1;
01387   
01388   return serial;
01389 }
01390 
01404 dbus_bool_t
01405 _dbus_connection_handle_watch (DBusWatch                   *watch,
01406                                unsigned int                 condition,
01407                                void                        *data)
01408 {
01409   DBusConnection *connection;
01410   dbus_bool_t retval;
01411   DBusDispatchStatus status;
01412 
01413   connection = data;
01414 
01415   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
01416   
01417   CONNECTION_LOCK (connection);
01418   _dbus_connection_acquire_io_path (connection, -1);
01419   HAVE_LOCK_CHECK (connection);
01420   retval = _dbus_transport_handle_watch (connection->transport,
01421                                          watch, condition);
01422 
01423   _dbus_connection_release_io_path (connection);
01424 
01425   HAVE_LOCK_CHECK (connection);
01426 
01427   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
01428   
01429   status = _dbus_connection_get_dispatch_status_unlocked (connection);
01430 
01431   /* this calls out to user code */
01432   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
01433 
01434   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
01435   
01436   return retval;
01437 }
01438 
01439 _DBUS_DEFINE_GLOBAL_LOCK (shared_connections);
01440 static DBusHashTable *shared_connections = NULL;
01441 
01442 static void
01443 shared_connections_shutdown (void *data)
01444 {
01445   int n_entries;
01446   
01447   _DBUS_LOCK (shared_connections);
01448   
01449   /* This is a little bit unpleasant... better ideas? */
01450   while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
01451     {
01452       DBusConnection *connection;
01453       DBusMessage *message;
01454       DBusHashIter iter;
01455       
01456       _dbus_hash_iter_init (shared_connections, &iter);
01457       _dbus_hash_iter_next (&iter);
01458        
01459       connection = _dbus_hash_iter_get_value (&iter);
01460 
01461       _DBUS_UNLOCK (shared_connections);
01462 
01463       dbus_connection_ref (connection);
01464       _dbus_connection_close_possibly_shared (connection);
01465 
01466       /* Churn through to the Disconnected message */
01467       while ((message = dbus_connection_pop_message (connection)))
01468         {
01469           dbus_message_unref (message);
01470         }
01471       dbus_connection_unref (connection);
01472       
01473       _DBUS_LOCK (shared_connections);
01474 
01475       /* The connection should now be dead and not in our hash ... */
01476       _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
01477     }
01478 
01479   _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
01480   
01481   _dbus_hash_table_unref (shared_connections);
01482   shared_connections = NULL;
01483   
01484   _DBUS_UNLOCK (shared_connections);
01485 }
01486 
01487 static dbus_bool_t
01488 connection_lookup_shared (DBusAddressEntry  *entry,
01489                           DBusConnection   **result)
01490 {
01491   _dbus_verbose ("checking for existing connection\n");
01492   
01493   *result = NULL;
01494   
01495   _DBUS_LOCK (shared_connections);
01496 
01497   if (shared_connections == NULL)
01498     {
01499       _dbus_verbose ("creating shared_connections hash table\n");
01500       
01501       shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
01502                                                  dbus_free,
01503                                                  NULL);
01504       if (shared_connections == NULL)
01505         {
01506           _DBUS_UNLOCK (shared_connections);
01507           return FALSE;
01508         }
01509 
01510       if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
01511         {
01512           _dbus_hash_table_unref (shared_connections);
01513           shared_connections = NULL;
01514           _DBUS_UNLOCK (shared_connections);
01515           return FALSE;
01516         }
01517 
01518       _dbus_verbose ("  successfully created shared_connections\n");
01519       
01520       _DBUS_UNLOCK (shared_connections);
01521       return TRUE; /* no point looking up in the hash we just made */
01522     }
01523   else
01524     {
01525       const char *guid;
01526 
01527       guid = dbus_address_entry_get_value (entry, "guid");
01528       
01529       if (guid != NULL)
01530         {
01531           DBusConnection *connection;
01532           
01533           connection = _dbus_hash_table_lookup_string (shared_connections,
01534                                                        guid);
01535 
01536           if (connection)
01537             {
01538               /* The DBusConnection can't be finalized without taking
01539                * the shared_connections lock to remove it from the
01540                * hash.  So it's safe to ref the connection here.
01541                * However, it may be disconnected if the Disconnected
01542                * message hasn't been processed yet, in which case we
01543                * want to pretend it isn't in the hash and avoid
01544                * returning it.
01545                *
01546                * The idea is to avoid ever returning a disconnected connection
01547                * from dbus_connection_open(). We could just synchronously
01548                * drop our shared ref to the connection on connection disconnect,
01549                * and then assert here that the connection is connected, but
01550                * that causes reentrancy headaches.
01551                */
01552               CONNECTION_LOCK (connection);
01553               if (_dbus_connection_get_is_connected_unlocked (connection))
01554                 {
01555                   _dbus_connection_ref_unlocked (connection);
01556                   *result = connection;
01557                   _dbus_verbose ("looked up existing connection to server guid %s\n",
01558                                  guid);
01559                 }
01560               else
01561                 {
01562                   _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
01563                                  guid);
01564                 }
01565               CONNECTION_UNLOCK (connection);
01566             }
01567         }
01568       
01569       _DBUS_UNLOCK (shared_connections);
01570       return TRUE;
01571     }
01572 }
01573 
01574 static dbus_bool_t
01575 connection_record_shared_unlocked (DBusConnection *connection,
01576                                    const char     *guid)
01577 {
01578   char *guid_key;
01579   char *guid_in_connection;
01580 
01581   HAVE_LOCK_CHECK (connection);
01582   _dbus_assert (connection->server_guid == NULL);
01583   _dbus_assert (connection->shareable);
01584 
01585   /* get a hard ref on this connection, even if
01586    * we won't in fact store it in the hash, we still
01587    * need to hold a ref on it until it's disconnected.
01588    */
01589   _dbus_connection_ref_unlocked (connection);
01590 
01591   if (guid == NULL)
01592     return TRUE; /* don't store in the hash */
01593   
01594   /* A separate copy of the key is required in the hash table, because
01595    * we don't have a lock on the connection when we are doing a hash
01596    * lookup.
01597    */
01598   
01599   guid_key = _dbus_strdup (guid);
01600   if (guid_key == NULL)
01601     return FALSE;
01602 
01603   guid_in_connection = _dbus_strdup (guid);
01604   if (guid_in_connection == NULL)
01605     {
01606       dbus_free (guid_key);
01607       return FALSE;
01608     }
01609   
01610   _DBUS_LOCK (shared_connections);
01611   _dbus_assert (shared_connections != NULL);
01612   
01613   if (!_dbus_hash_table_insert_string (shared_connections,
01614                                        guid_key, connection))
01615     {
01616       dbus_free (guid_key);
01617       dbus_free (guid_in_connection);
01618       _DBUS_UNLOCK (shared_connections);
01619       return FALSE;
01620     }
01621 
01622   connection->server_guid = guid_in_connection;
01623 
01624   _dbus_verbose ("stored connection to %s to be shared\n",
01625                  connection->server_guid);
01626   
01627   _DBUS_UNLOCK (shared_connections);
01628 
01629   _dbus_assert (connection->server_guid != NULL);
01630   
01631   return TRUE;
01632 }
01633 
01634 static void
01635 connection_forget_shared_unlocked (DBusConnection *connection)
01636 {
01637   HAVE_LOCK_CHECK (connection);
01638 
01639   if (!connection->shareable)
01640     return;
01641   
01642   if (connection->server_guid != NULL)
01643     {
01644       _dbus_verbose ("dropping connection to %s out of the shared table\n",
01645                      connection->server_guid);
01646       
01647       _DBUS_LOCK (shared_connections);
01648       
01649       if (!_dbus_hash_table_remove_string (shared_connections,
01650                                            connection->server_guid))
01651         _dbus_assert_not_reached ("connection was not in the shared table");
01652       
01653       dbus_free (connection->server_guid);
01654       connection->server_guid = NULL;
01655       _DBUS_UNLOCK (shared_connections);
01656     }
01657   
01658   /* remove our reference held on all shareable connections */
01659   _dbus_connection_unref_unlocked (connection);
01660 }
01661 
01662 static DBusConnection*
01663 connection_try_from_address_entry (DBusAddressEntry *entry,
01664                                    DBusError        *error)
01665 {
01666   DBusTransport *transport;
01667   DBusConnection *connection;
01668 
01669   transport = _dbus_transport_open (entry, error);
01670 
01671   if (transport == NULL)
01672     {
01673       _DBUS_ASSERT_ERROR_IS_SET (error);
01674       return NULL;
01675     }
01676 
01677   connection = _dbus_connection_new_for_transport (transport);
01678 
01679   _dbus_transport_unref (transport);
01680   
01681   if (connection == NULL)
01682     {
01683       _DBUS_SET_OOM (error);
01684       return NULL;
01685     }
01686 
01687 #ifndef DBUS_DISABLE_CHECKS
01688   _dbus_assert (!connection->have_connection_lock);
01689 #endif
01690   return connection;
01691 }
01692 
01693 /*
01694  * If the shared parameter is true, then any existing connection will
01695  * be used (and if a new connection is created, it will be available
01696  * for use by others). If the shared parameter is false, a new
01697  * connection will always be created, and the new connection will
01698  * never be returned to other callers.
01699  *
01700  * @param address the address
01701  * @param shared whether the connection is shared or private
01702  * @param error error return
01703  * @returns the connection or #NULL on error
01704  */
01705 static DBusConnection*
01706 _dbus_connection_open_internal (const char     *address,
01707                                 dbus_bool_t     shared,
01708                                 DBusError      *error)
01709 {
01710   DBusConnection *connection;
01711   DBusAddressEntry **entries;
01712   DBusError tmp_error;
01713   DBusError first_error;
01714   int len, i;
01715 
01716   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
01717 
01718   _dbus_verbose ("opening %s connection to: %s\n",
01719                  shared ? "shared" : "private", address);
01720   
01721   if (!dbus_parse_address (address, &entries, &len, error))
01722     return NULL;
01723 
01724   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
01725   
01726   connection = NULL;
01727 
01728   dbus_error_init (&tmp_error);
01729   dbus_error_init (&first_error);
01730   for (i = 0; i < len; i++)
01731     {
01732       if (shared)
01733         {
01734           if (!connection_lookup_shared (entries[i], &connection))
01735             _DBUS_SET_OOM (&tmp_error);
01736         }
01737 
01738       if (connection == NULL)
01739         {
01740           connection = connection_try_from_address_entry (entries[i],
01741                                                           &tmp_error);
01742 
01743           if (connection != NULL && shared)
01744             {
01745               const char *guid;
01746                   
01747               connection->shareable = TRUE;
01748                   
01749               /* guid may be NULL */
01750               guid = dbus_address_entry_get_value (entries[i], "guid");
01751                   
01752               CONNECTION_LOCK (connection);
01753           
01754               if (!connection_record_shared_unlocked (connection, guid))
01755                 {
01756                   _DBUS_SET_OOM (&tmp_error);
01757                   _dbus_connection_close_possibly_shared_and_unlock (connection);
01758                   dbus_connection_unref (connection);
01759                   connection = NULL;
01760                 }
01761               else
01762                 CONNECTION_UNLOCK (connection);
01763             }
01764         }
01765       
01766       if (connection)
01767         break;
01768 
01769       _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
01770       
01771       if (i == 0)
01772         dbus_move_error (&tmp_error, &first_error);
01773       else
01774         dbus_error_free (&tmp_error);
01775     }
01776   
01777   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
01778   _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
01779   
01780   if (connection == NULL)
01781     {
01782       _DBUS_ASSERT_ERROR_IS_SET (&first_error);
01783       dbus_move_error (&first_error, error);
01784     }
01785   else
01786     dbus_error_free (&first_error);
01787   
01788   dbus_address_entries_free (entries);
01789   return connection;
01790 }
01791 
01800 void
01801 _dbus_connection_close_possibly_shared (DBusConnection *connection)
01802 {
01803   _dbus_assert (connection != NULL);
01804   _dbus_assert (connection->generation == _dbus_current_generation);
01805 
01806   CONNECTION_LOCK (connection);
01807   _dbus_connection_close_possibly_shared_and_unlock (connection);
01808 }
01809 
01810 static DBusPreallocatedSend*
01811 _dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
01812 {
01813   DBusPreallocatedSend *preallocated;
01814 
01815   HAVE_LOCK_CHECK (connection);
01816   
01817   _dbus_assert (connection != NULL);
01818   
01819   preallocated = dbus_new (DBusPreallocatedSend, 1);
01820   if (preallocated == NULL)
01821     return NULL;
01822 
01823   if (connection->link_cache != NULL)
01824     {
01825       preallocated->queue_link =
01826         _dbus_list_pop_first_link (&connection->link_cache);
01827       preallocated->queue_link->data = NULL;
01828     }
01829   else
01830     {
01831       preallocated->queue_link = _dbus_list_alloc_link (NULL);
01832       if (preallocated->queue_link == NULL)
01833         goto failed_0;
01834     }
01835   
01836   if (connection->link_cache != NULL)
01837     {
01838       preallocated->counter_link =
01839         _dbus_list_pop_first_link (&connection->link_cache);
01840       preallocated->counter_link->data = connection->outgoing_counter;
01841     }
01842   else
01843     {
01844       preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
01845       if (preallocated->counter_link == NULL)
01846         goto failed_1;
01847     }
01848 
01849   _dbus_counter_ref (preallocated->counter_link->data);
01850 
01851   preallocated->connection = connection;
01852   
01853   return preallocated;
01854   
01855  failed_1:
01856   _dbus_list_free_link (preallocated->queue_link);
01857  failed_0:
01858   dbus_free (preallocated);
01859   
01860   return NULL;
01861 }
01862 
01863 /* Called with lock held, does not update dispatch status */
01864 static void
01865 _dbus_connection_send_preallocated_unlocked_no_update (DBusConnection       *connection,
01866                                                        DBusPreallocatedSend *preallocated,
01867                                                        DBusMessage          *message,
01868                                                        dbus_uint32_t        *client_serial)
01869 {
01870   dbus_uint32_t serial;
01871   const char *sig;
01872 
01873   preallocated->queue_link->data = message;
01874   _dbus_list_prepend_link (&connection->outgoing_messages,
01875                            preallocated->queue_link);
01876 
01877   _dbus_message_add_size_counter_link (message,
01878                                        preallocated->counter_link);
01879 
01880   dbus_free (preallocated);
01881   preallocated = NULL;
01882   
01883   dbus_message_ref (message);
01884   
01885   connection->n_outgoing += 1;
01886 
01887   sig = dbus_message_get_signature (message);
01888   
01889   _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
01890                  message,
01891                  dbus_message_get_type (message),
01892                  dbus_message_get_path (message) ?
01893                  dbus_message_get_path (message) :
01894                  "no path",
01895                  dbus_message_get_interface (message) ?
01896                  dbus_message_get_interface (message) :
01897                  "no interface",
01898                  dbus_message_get_member (message) ?
01899                  dbus_message_get_member (message) :
01900                  "no member",
01901                  sig,
01902                  dbus_message_get_destination (message) ?
01903                  dbus_message_get_destination (message) :
01904                  "null",
01905                  connection,
01906                  connection->n_outgoing);
01907 
01908   if (dbus_message_get_serial (message) == 0)
01909     {
01910       serial = _dbus_connection_get_next_client_serial (connection);
01911       _dbus_message_set_serial (message, serial);
01912       if (client_serial)
01913         *client_serial = serial;
01914     }
01915   else
01916     {
01917       if (client_serial)
01918         *client_serial = dbus_message_get_serial (message);
01919     }
01920 
01921   _dbus_verbose ("Message %p serial is %u\n",
01922                  message, dbus_message_get_serial (message));
01923   
01924   _dbus_message_lock (message);
01925 
01926   /* Now we need to run an iteration to hopefully just write the messages
01927    * out immediately, and otherwise get them queued up
01928    */
01929   _dbus_connection_do_iteration_unlocked (connection,
01930                                           DBUS_ITERATION_DO_WRITING,
01931                                           -1);
01932 
01933   /* If stuff is still queued up, be sure we wake up the main loop */
01934   if (connection->n_outgoing > 0)
01935     _dbus_connection_wakeup_mainloop (connection);
01936 }
01937 
01938 static void
01939 _dbus_connection_send_preallocated_and_unlock (DBusConnection       *connection,
01940                                                DBusPreallocatedSend *preallocated,
01941                                                DBusMessage          *message,
01942                                                dbus_uint32_t        *client_serial)
01943 {
01944   DBusDispatchStatus status;
01945 
01946   HAVE_LOCK_CHECK (connection);
01947   
01948   _dbus_connection_send_preallocated_unlocked_no_update (connection,
01949                                                          preallocated,
01950                                                          message, client_serial);
01951 
01952   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
01953   status = _dbus_connection_get_dispatch_status_unlocked (connection);
01954 
01955   /* this calls out to user code */
01956   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
01957 }
01958 
01968 dbus_bool_t
01969 _dbus_connection_send_and_unlock (DBusConnection *connection,
01970                                   DBusMessage    *message,
01971                                   dbus_uint32_t  *client_serial)
01972 {
01973   DBusPreallocatedSend *preallocated;
01974 
01975   _dbus_assert (connection != NULL);
01976   _dbus_assert (message != NULL);
01977   
01978   preallocated = _dbus_connection_preallocate_send_unlocked (connection);
01979   if (preallocated == NULL)
01980     {
01981       CONNECTION_UNLOCK (connection);
01982       return FALSE;
01983     }
01984 
01985   _dbus_connection_send_preallocated_and_unlock (connection,
01986                                                  preallocated,
01987                                                  message,
01988                                                  client_serial);
01989   return TRUE;
01990 }
01991 
02016 void
02017 _dbus_connection_close_if_only_one_ref (DBusConnection *connection)
02018 {
02019   CONNECTION_LOCK (connection);
02020   
02021   _dbus_assert (connection->refcount.value > 0);
02022 
02023   if (connection->refcount.value == 1)
02024     _dbus_connection_close_possibly_shared_and_unlock (connection);
02025   else
02026     CONNECTION_UNLOCK (connection);
02027 }
02028 
02029 
02039 static void
02040 _dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
02041 {
02042   if (timeout_milliseconds == -1)
02043     _dbus_sleep_milliseconds (1000);
02044   else if (timeout_milliseconds < 100)
02045     ; /* just busy loop */
02046   else if (timeout_milliseconds <= 1000)
02047     _dbus_sleep_milliseconds (timeout_milliseconds / 3);
02048   else
02049     _dbus_sleep_milliseconds (1000);
02050 }
02051 
02052 static DBusMessage *
02053 generate_local_error_message (dbus_uint32_t serial, 
02054                               char *error_name, 
02055                               char *error_msg)
02056 {
02057   DBusMessage *message;
02058   message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
02059   if (!message)
02060     goto out;
02061 
02062   if (!dbus_message_set_error_name (message, error_name))
02063     {
02064       dbus_message_unref (message);
02065       message = NULL;
02066       goto out; 
02067     }
02068 
02069   dbus_message_set_no_reply (message, TRUE); 
02070 
02071   if (!dbus_message_set_reply_serial (message,
02072                                       serial))
02073     {
02074       dbus_message_unref (message);
02075       message = NULL;
02076       goto out;
02077     }
02078 
02079   if (error_msg != NULL)
02080     {
02081       DBusMessageIter iter;
02082 
02083       dbus_message_iter_init_append (message, &iter);
02084       if (!dbus_message_iter_append_basic (&iter,
02085                                            DBUS_TYPE_STRING,
02086                                            &error_msg))
02087         {
02088           dbus_message_unref (message);
02089           message = NULL;
02090           goto out;
02091         }
02092     }
02093 
02094  out:
02095   return message;
02096 }
02097 
02098 
02099 /* This is slightly strange since we can pop a message here without
02100  * the dispatch lock.
02101  */
02102 static DBusMessage*
02103 check_for_reply_unlocked (DBusConnection *connection,
02104                           dbus_uint32_t   client_serial)
02105 {
02106   DBusList *link;
02107 
02108   HAVE_LOCK_CHECK (connection);
02109   
02110   link = _dbus_list_get_first_link (&connection->incoming_messages);
02111 
02112   while (link != NULL)
02113     {
02114       DBusMessage *reply = link->data;
02115 
02116       if (dbus_message_get_reply_serial (reply) == client_serial)
02117         {
02118           _dbus_list_remove_link (&connection->incoming_messages, link);
02119           connection->n_incoming  -= 1;
02120           return reply;
02121         }
02122       link = _dbus_list_get_next_link (&connection->incoming_messages, link);
02123     }
02124 
02125   return NULL;
02126 }
02127 
02128 static void
02129 connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
02130 {
02131    /* We can't iterate over the hash in the normal way since we'll be
02132     * dropping the lock for each item. So we restart the
02133     * iter each time as we drain the hash table.
02134     */
02135    
02136    while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
02137     {
02138       DBusPendingCall *pending;
02139       DBusHashIter iter;
02140       
02141       _dbus_hash_iter_init (connection->pending_replies, &iter);
02142       _dbus_hash_iter_next (&iter);
02143        
02144       pending = _dbus_hash_iter_get_value (&iter);
02145       _dbus_pending_call_ref_unlocked (pending);
02146        
02147       _dbus_pending_call_queue_timeout_error_unlocked (pending, 
02148                                                        connection);
02149       _dbus_connection_remove_timeout_unlocked (connection,
02150                                                 _dbus_pending_call_get_timeout_unlocked (pending));
02151       _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);       
02152       _dbus_hash_iter_remove_entry (&iter);
02153 
02154       _dbus_pending_call_unref_and_unlock (pending);
02155       CONNECTION_LOCK (connection);
02156     }
02157   HAVE_LOCK_CHECK (connection);
02158 }
02159 
02160 static void
02161 complete_pending_call_and_unlock (DBusConnection  *connection,
02162                                   DBusPendingCall *pending,
02163                                   DBusMessage     *message)
02164 {
02165   _dbus_pending_call_set_reply_unlocked (pending, message);
02166   _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
02167   _dbus_connection_detach_pending_call_and_unlock (connection, pending);
02168  
02169   /* Must be called unlocked since it invokes app callback */
02170   _dbus_pending_call_complete (pending);
02171   dbus_pending_call_unref (pending);
02172 }
02173 
02174 static dbus_bool_t
02175 check_for_reply_and_update_dispatch_unlocked (DBusConnection  *connection,
02176                                               DBusPendingCall *pending)
02177 {
02178   DBusMessage *reply;
02179   DBusDispatchStatus status;
02180 
02181   reply = check_for_reply_unlocked (connection, 
02182                                     _dbus_pending_call_get_reply_serial_unlocked (pending));
02183   if (reply != NULL)
02184     {
02185       _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
02186 
02187       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
02188 
02189       complete_pending_call_and_unlock (connection, pending, reply);
02190       dbus_message_unref (reply);
02191 
02192       CONNECTION_LOCK (connection);
02193       status = _dbus_connection_get_dispatch_status_unlocked (connection);
02194       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02195       dbus_pending_call_unref (pending);
02196 
02197       return TRUE;
02198     }
02199 
02200   return FALSE;
02201 }
02202 
02217 void
02218 _dbus_connection_block_pending_call (DBusPendingCall *pending)
02219 {
02220   long start_tv_sec, start_tv_usec;
02221   long end_tv_sec, end_tv_usec;
02222   long tv_sec, tv_usec;
02223   DBusDispatchStatus status;
02224   DBusConnection *connection;
02225   dbus_uint32_t client_serial;
02226   int timeout_milliseconds;
02227 
02228   _dbus_assert (pending != NULL);
02229 
02230   if (dbus_pending_call_get_completed (pending))
02231     return;
02232 
02233   dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
02234 
02235   connection = _dbus_pending_call_get_connection_and_lock (pending);
02236   
02237   /* Flush message queue - note, can affect dispatch status */
02238   _dbus_connection_flush_unlocked (connection);
02239 
02240   client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
02241 
02242   /* note that timeout_milliseconds is limited to a smallish value
02243    * in _dbus_pending_call_new() so overflows aren't possible
02244    * below
02245    */
02246   timeout_milliseconds = dbus_timeout_get_interval (_dbus_pending_call_get_timeout_unlocked (pending));
02247   
02248   _dbus_get_current_time (&start_tv_sec, &start_tv_usec);
02249   end_tv_sec = start_tv_sec + timeout_milliseconds / 1000;
02250   end_tv_usec = start_tv_usec + (timeout_milliseconds % 1000) * 1000;
02251   end_tv_sec += end_tv_usec / _DBUS_USEC_PER_SECOND;
02252   end_tv_usec = end_tv_usec % _DBUS_USEC_PER_SECOND;
02253 
02254   _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",
02255                  timeout_milliseconds,
02256                  client_serial,
02257                  start_tv_sec, start_tv_usec,
02258                  end_tv_sec, end_tv_usec);
02259 
02260   /* check to see if we already got the data off the socket */
02261   /* from another blocked pending call */
02262   if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
02263     return;
02264 
02265   /* Now we wait... */
02266   /* always block at least once as we know we don't have the reply yet */
02267   _dbus_connection_do_iteration_unlocked (connection,
02268                                           DBUS_ITERATION_DO_READING |
02269                                           DBUS_ITERATION_BLOCK,
02270                                           timeout_milliseconds);
02271 
02272  recheck_status:
02273 
02274   _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
02275   
02276   HAVE_LOCK_CHECK (connection);
02277   
02278   /* queue messages and get status */
02279 
02280   status = _dbus_connection_get_dispatch_status_unlocked (connection);
02281 
02282   /* the get_completed() is in case a dispatch() while we were blocking
02283    * got the reply instead of us.
02284    */
02285   if (_dbus_pending_call_get_completed_unlocked (pending))
02286     {
02287       _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
02288       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02289       dbus_pending_call_unref (pending);
02290       return;
02291     }
02292   
02293   if (status == DBUS_DISPATCH_DATA_REMAINS) {
02294     if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
02295       return;
02296   }
02297   
02298   _dbus_get_current_time (&tv_sec, &tv_usec);
02299   
02300   if (!_dbus_connection_get_is_connected_unlocked (connection))
02301     {
02302       DBusMessage *error_msg;
02303 
02304       error_msg = generate_local_error_message (client_serial,
02305                                                 DBUS_ERROR_DISCONNECTED, 
02306                                                 "Connection was disconnected before a reply was received"); 
02307 
02308       /* on OOM error_msg is set to NULL */
02309       complete_pending_call_and_unlock (connection, pending, error_msg);
02310       dbus_pending_call_unref (pending);
02311       return;
02312     }
02313   else if (tv_sec < start_tv_sec)
02314     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
02315   else if (connection->disconnect_message_link == NULL)
02316     _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
02317   else if (tv_sec < end_tv_sec ||
02318            (tv_sec == end_tv_sec && tv_usec < end_tv_usec))
02319     {
02320       timeout_milliseconds = (end_tv_sec - tv_sec) * 1000 +
02321         (end_tv_usec - tv_usec) / 1000;
02322       _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds);
02323       _dbus_assert (timeout_milliseconds >= 0);
02324       
02325       if (status == DBUS_DISPATCH_NEED_MEMORY)
02326         {
02327           /* Try sleeping a bit, as we aren't sure we need to block for reading,
02328            * we may already have a reply in the buffer and just can't process
02329            * it.
02330            */
02331           _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
02332 
02333           _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
02334         }
02335       else
02336         {          
02337           /* block again, we don't have the reply buffered yet. */
02338           _dbus_connection_do_iteration_unlocked (connection,
02339                                                   DBUS_ITERATION_DO_READING |
02340                                                   DBUS_ITERATION_BLOCK,
02341                                                   timeout_milliseconds);
02342         }
02343 
02344       goto recheck_status;
02345     }
02346 
02347   _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %ld milliseconds and got no reply\n",
02348                  (tv_sec - start_tv_sec) * 1000 + (tv_usec - start_tv_usec) / 1000);
02349 
02350   _dbus_assert (!_dbus_pending_call_get_completed_unlocked (pending));
02351   
02352   /* unlock and call user code */
02353   complete_pending_call_and_unlock (connection, pending, NULL);
02354 
02355   /* update user code on dispatch status */
02356   CONNECTION_LOCK (connection);
02357   status = _dbus_connection_get_dispatch_status_unlocked (connection);
02358   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02359   dbus_pending_call_unref (pending);
02360 }
02361 
02398 DBusConnection*
02399 dbus_connection_open (const char     *address,
02400                       DBusError      *error)
02401 {
02402   DBusConnection *connection;
02403 
02404   _dbus_return_val_if_fail (address != NULL, NULL);
02405   _dbus_return_val_if_error_is_set (error, NULL);
02406 
02407   connection = _dbus_connection_open_internal (address,
02408                                                TRUE,
02409                                                error);
02410 
02411   return connection;
02412 }
02413 
02441 DBusConnection*
02442 dbus_connection_open_private (const char     *address,
02443                               DBusError      *error)
02444 {
02445   DBusConnection *connection;
02446 
02447   _dbus_return_val_if_fail (address != NULL, NULL);
02448   _dbus_return_val_if_error_is_set (error, NULL);
02449 
02450   connection = _dbus_connection_open_internal (address,
02451                                                FALSE,
02452                                                error);
02453 
02454   return connection;
02455 }
02456 
02463 DBusConnection *
02464 dbus_connection_ref (DBusConnection *connection)
02465 {
02466   _dbus_return_val_if_fail (connection != NULL, NULL);
02467   _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
02468   
02469   /* The connection lock is better than the global
02470    * lock in the atomic increment fallback
02471    */
02472   
02473 #ifdef DBUS_HAVE_ATOMIC_INT
02474   _dbus_atomic_inc (&connection->refcount);
02475 #else
02476   CONNECTION_LOCK (connection);
02477   _dbus_assert (connection->refcount.value > 0);
02478 
02479   connection->refcount.value += 1;
02480   CONNECTION_UNLOCK (connection);
02481 #endif
02482 
02483   return connection;
02484 }
02485 
02486 static void
02487 free_outgoing_message (void *element,
02488                        void *data)
02489 {
02490   DBusMessage *message = element;
02491   DBusConnection *connection = data;
02492 
02493   _dbus_message_remove_size_counter (message,
02494                                      connection->outgoing_counter,
02495                                      NULL);
02496   dbus_message_unref (message);
02497 }
02498 
02499 /* This is run without the mutex held, but after the last reference
02500  * to the connection has been dropped we should have no thread-related
02501  * problems
02502  */
02503 static void
02504 _dbus_connection_last_unref (DBusConnection *connection)
02505 {
02506   DBusList *link;
02507 
02508   _dbus_verbose ("Finalizing connection %p\n", connection);
02509   
02510   _dbus_assert (connection->refcount.value == 0);
02511   
02512   /* You have to disconnect the connection before unref:ing it. Otherwise
02513    * you won't get the disconnected message.
02514    */
02515   _dbus_assert (!_dbus_transport_get_is_connected (connection->transport));
02516   _dbus_assert (connection->server_guid == NULL);
02517   
02518   /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
02519   _dbus_object_tree_free_all_unlocked (connection->objects);
02520   
02521   dbus_connection_set_dispatch_status_function (connection, NULL, NULL, NULL);
02522   dbus_connection_set_wakeup_main_function (connection, NULL, NULL, NULL);
02523   dbus_connection_set_unix_user_function (connection, NULL, NULL, NULL);
02524   
02525   _dbus_watch_list_free (connection->watches);
02526   connection->watches = NULL;
02527   
02528   _dbus_timeout_list_free (connection->timeouts);
02529   connection->timeouts = NULL;
02530 
02531   _dbus_data_slot_list_free (&connection->slot_list);
02532   
02533   link = _dbus_list_get_first_link (&connection->filter_list);
02534   while (link != NULL)
02535     {
02536       DBusMessageFilter *filter = link->data;
02537       DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
02538 
02539       filter->function = NULL;
02540       _dbus_message_filter_unref (filter); /* calls app callback */
02541       link->data = NULL;
02542       
02543       link = next;
02544     }
02545   _dbus_list_clear (&connection->filter_list);
02546   
02547   /* ---- Done with stuff that invokes application callbacks */
02548 
02549   _dbus_object_tree_unref (connection->objects);  
02550 
02551   _dbus_hash_table_unref (connection->pending_replies);
02552   connection->pending_replies = NULL;
02553   
02554   _dbus_list_clear (&connection->filter_list);
02555   
02556   _dbus_list_foreach (&connection->outgoing_messages,
02557                       free_outgoing_message,
02558                       connection);
02559   _dbus_list_clear (&connection->outgoing_messages);
02560   
02561   _dbus_list_foreach (&connection->incoming_messages,
02562                       (DBusForeachFunction) dbus_message_unref,
02563                       NULL);
02564   _dbus_list_clear (&connection->incoming_messages);
02565 
02566   _dbus_counter_unref (connection->outgoing_counter);
02567 
02568   _dbus_transport_unref (connection->transport);
02569 
02570   if (connection->disconnect_message_link)
02571     {
02572       DBusMessage *message = connection->disconnect_message_link->data;
02573       dbus_message_unref (message);
02574       _dbus_list_free_link (connection->disconnect_message_link);
02575     }
02576 
02577   _dbus_list_clear (&connection->link_cache);
02578   
02579   _dbus_condvar_free_at_location (&connection->dispatch_cond);
02580   _dbus_condvar_free_at_location (&connection->io_path_cond);
02581 
02582   _dbus_mutex_free_at_location (&connection->io_path_mutex);
02583   _dbus_mutex_free_at_location (&connection->dispatch_mutex);
02584 
02585   _dbus_mutex_free_at_location (&connection->mutex);
02586   
02587   dbus_free (connection);
02588 }
02589 
02609 void
02610 dbus_connection_unref (DBusConnection *connection)
02611 {
02612   dbus_bool_t last_unref;
02613 
02614   _dbus_return_if_fail (connection != NULL);
02615   _dbus_return_if_fail (connection->generation == _dbus_current_generation);
02616   
02617   /* The connection lock is better than the global
02618    * lock in the atomic increment fallback
02619    */
02620   
02621 #ifdef DBUS_HAVE_ATOMIC_INT
02622   last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
02623 #else
02624   CONNECTION_LOCK (connection);
02625   
02626   _dbus_assert (connection->refcount.value > 0);
02627 
02628   connection->refcount.value -= 1;
02629   last_unref = (connection->refcount.value == 0);
02630 
02631 #if 0
02632   printf ("unref() connection %p count = %d\n", connection, connection->refcount.value);
02633 #endif
02634   
02635   CONNECTION_UNLOCK (connection);
02636 #endif
02637   
02638   if (last_unref)
02639     {
02640 #ifndef DBUS_DISABLE_CHECKS
02641       if (_dbus_transport_get_is_connected (connection->transport))
02642         {
02643           _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",
02644                                    connection->shareable ?
02645                                    "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection.\n" : 
02646                                     "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.\n");
02647           return;
02648         }
02649 #endif
02650       _dbus_connection_last_unref (connection);
02651     }
02652 }
02653 
02654 /*
02655  * Note that the transport can disconnect itself (other end drops us)
02656  * and in that case this function never runs. So this function must
02657  * not do anything more than disconnect the transport and update the
02658  * dispatch status.
02659  * 
02660  * If the transport self-disconnects, then we assume someone will
02661  * dispatch the connection to cause the dispatch status update.
02662  */
02663 static void
02664 _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
02665 {
02666   DBusDispatchStatus status;
02667 
02668   HAVE_LOCK_CHECK (connection);
02669   
02670   _dbus_verbose ("Disconnecting %p\n", connection);
02671 
02672   /* We need to ref because update_dispatch_status_and_unlock will unref
02673    * the connection if it was shared and libdbus was the only remaining
02674    * refcount holder.
02675    */
02676   _dbus_connection_ref_unlocked (connection);
02677   
02678   _dbus_transport_disconnect (connection->transport);
02679 
02680   /* This has the side effect of queuing the disconnect message link
02681    * (unless we don't have enough memory, possibly, so don't assert it).
02682    * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
02683    * should never again return the newly-disconnected connection.
02684    *
02685    * However, we only unref the shared connection and exit_on_disconnect when
02686    * the disconnect message reaches the head of the message queue,
02687    * NOT when it's first queued.
02688    */
02689   status = _dbus_connection_get_dispatch_status_unlocked (connection);
02690 
02691   /* This calls out to user code */
02692   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
02693 
02694   /* Could also call out to user code */
02695   dbus_connection_unref (connection);
02696 }
02697 
02740 void
02741 dbus_connection_close (DBusConnection *connection)
02742 {
02743   _dbus_return_if_fail (connection != NULL);
02744   _dbus_return_if_fail (connection->generation == _dbus_current_generation);
02745 
02746   CONNECTION_LOCK (connection);
02747 
02748 #ifndef DBUS_DISABLE_CHECKS
02749   if (connection->shareable)
02750     {
02751       CONNECTION_UNLOCK (connection);
02752 
02753       _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.\n");
02754       return;
02755     }
02756 #endif
02757   
02758   _dbus_connection_close_possibly_shared_and_unlock (connection);
02759 }
02760 
02761 static dbus_bool_t
02762 _dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
02763 {
02764   HAVE_LOCK_CHECK (connection);
02765   return _dbus_transport_get_is_connected (connection->transport);
02766 }
02767 
02781 dbus_bool_t
02782 dbus_connection_get_is_connected (DBusConnection *connection)
02783 {
02784   dbus_bool_t res;
02785 
02786   _dbus_return_val_if_fail (connection != NULL, FALSE);
02787   
02788   CONNECTION_LOCK (connection);
02789   res = _dbus_connection_get_is_connected_unlocked (connection);
02790   CONNECTION_UNLOCK (connection);
02791   
02792   return res;
02793 }
02794 
02803 dbus_bool_t
02804 dbus_connection_get_is_authenticated (DBusConnection *connection)
02805 {
02806   dbus_bool_t res;
02807 
02808   _dbus_return_val_if_fail (connection != NULL, FALSE);
02809   
02810   CONNECTION_LOCK (connection);
02811   res = _dbus_transport_get_is_authenticated (connection->transport);
02812   CONNECTION_UNLOCK (connection);
02813   
02814   return res;
02815 }
02816 
02830 void
02831 dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
02832                                         dbus_bool_t     exit_on_disconnect)
02833 {
02834   _dbus_return_if_fail (connection != NULL);
02835 
02836   CONNECTION_LOCK (connection);
02837   connection->exit_on_disconnect = exit_on_disconnect != FALSE;
02838   CONNECTION_UNLOCK (connection);
02839 }
02840 
02850 DBusPreallocatedSend*
02851 dbus_connection_preallocate_send (DBusConnection *connection)
02852 {
02853   DBusPreallocatedSend *preallocated;
02854 
02855   _dbus_return_val_if_fail (connection != NULL, NULL);
02856 
02857   CONNECTION_LOCK (connection);
02858   
02859   preallocated =
02860     _dbus_connection_preallocate_send_unlocked (connection);
02861 
02862   CONNECTION_UNLOCK (connection);
02863 
02864   return preallocated;
02865 }
02866 
02876 void
02877 dbus_connection_free_preallocated_send (DBusConnection       *connection,
02878                                         DBusPreallocatedSend *preallocated)
02879 {
02880   _dbus_return_if_fail (connection != NULL);
02881   _dbus_return_if_fail (preallocated != NULL);  
02882   _dbus_return_if_fail (connection == preallocated->connection);
02883 
02884   _dbus_list_free_link (preallocated->queue_link);
02885   _dbus_counter_unref (preallocated->counter_link->data);
02886   _dbus_list_free_link (preallocated->counter_link);
02887   dbus_free (preallocated);
02888 }
02889 
02902 void
02903 dbus_connection_send_preallocated (DBusConnection       *connection,
02904                                    DBusPreallocatedSend *preallocated,
02905                                    DBusMessage          *message,
02906                                    dbus_uint32_t        *client_serial)
02907 {
02908   _dbus_return_if_fail (connection != NULL);
02909   _dbus_return_if_fail (preallocated != NULL);
02910   _dbus_return_if_fail (message != NULL);
02911   _dbus_return_if_fail (preallocated->connection == connection);
02912   _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
02913                         dbus_message_get_member (message) != NULL);
02914   _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
02915                         (dbus_message_get_interface (message) != NULL &&
02916                          dbus_message_get_member (message) != NULL));
02917   
02918   CONNECTION_LOCK (connection);
02919   _dbus_connection_send_preallocated_and_unlock (connection,
02920                                                  preallocated,
02921                                                  message, client_serial);
02922 }
02923 
02924 static dbus_bool_t
02925 _dbus_connection_send_unlocked_no_update (DBusConnection *connection,
02926                                           DBusMessage    *message,
02927                                           dbus_uint32_t  *client_serial)
02928 {
02929   DBusPreallocatedSend *preallocated;
02930 
02931   _dbus_assert (connection != NULL);
02932   _dbus_assert (message != NULL);
02933   
02934   preallocated = _dbus_connection_preallocate_send_unlocked (connection);
02935   if (preallocated == NULL)
02936     return FALSE;
02937 
02938   _dbus_connection_send_preallocated_unlocked_no_update (connection,
02939                                                          preallocated,
02940                                                          message,
02941                                                          client_serial);
02942   return TRUE;
02943 }
02944 
02966 dbus_bool_t
02967 dbus_connection_send (DBusConnection *connection,
02968                       DBusMessage    *message,
02969                       dbus_uint32_t  *serial)
02970 {
02971   _dbus_return_val_if_fail (connection != NULL, FALSE);
02972   _dbus_return_val_if_fail (message != NULL, FALSE);
02973 
02974   CONNECTION_LOCK (connection);
02975 
02976   return _dbus_connection_send_and_unlock (connection,
02977                                            message,
02978                                            serial);
02979 }
02980 
02981 static dbus_bool_t
02982 reply_handler_timeout (void *data)
02983 {
02984   DBusConnection *connection;
02985   DBusDispatchStatus status;
02986   DBusPendingCall *pending = data;
02987 
02988   connection = _dbus_pending_call_get_connection_and_lock (pending);
02989 
02990   _dbus_pending_call_queue_timeout_error_unlocked (pending, 
02991                                                    connection);
02992   _dbus_connection_remove_timeout_unlocked (connection,
02993                                             _dbus_pending_call_get_timeout_unlocked (pending));
02994   _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
02995 
02996   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
02997   status = _dbus_connection_get_dispatch_status_unlocked (connection);
02998 
02999   /* Unlocks, and calls out to user code */
03000   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
03001   
03002   return TRUE;
03003 }
03004 
03042 dbus_bool_t
03043 dbus_connection_send_with_reply (DBusConnection     *connection,
03044                                  DBusMessage        *message,
03045                                  DBusPendingCall   **pending_return,
03046                                  int                 timeout_milliseconds)
03047 {
03048   DBusPendingCall *pending;
03049   dbus_int32_t serial = -1;
03050   DBusDispatchStatus status;
03051 
03052   _dbus_return_val_if_fail (connection != NULL, FALSE);
03053   _dbus_return_val_if_fail (message != NULL, FALSE);
03054   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
03055 
03056   if (pending_return)
03057     *pending_return = NULL;
03058 
03059   CONNECTION_LOCK (connection);
03060 
03061    if (!_dbus_connection_get_is_connected_unlocked (connection))
03062     {
03063       CONNECTION_UNLOCK (connection);
03064 
03065       *pending_return = NULL;
03066 
03067       return TRUE;
03068     }
03069 
03070   pending = _dbus_pending_call_new_unlocked (connection,
03071                                              timeout_milliseconds,
03072                                              reply_handler_timeout);
03073 
03074   if (pending == NULL)
03075     {
03076       CONNECTION_UNLOCK (connection);
03077       return FALSE;
03078     }
03079 
03080   /* Assign a serial to the message */
03081   serial = dbus_message_get_serial (message);
03082   if (serial == 0)
03083     {
03084       serial = _dbus_connection_get_next_client_serial (connection);
03085       _dbus_message_set_serial (message, serial);
03086     }
03087 
03088   if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
03089     goto error;
03090     
03091   /* Insert the serial in the pending replies hash;
03092    * hash takes a refcount on DBusPendingCall.
03093    * Also, add the timeout.
03094    */
03095   if (!_dbus_connection_attach_pending_call_unlocked (connection,
03096                                                       pending))
03097     goto error;
03098  
03099   if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
03100     {
03101       _dbus_connection_detach_pending_call_and_unlock (connection,
03102                                                        pending);
03103       goto error_unlocked;
03104     }
03105 
03106   if (pending_return)
03107     *pending_return = pending; /* hand off refcount */
03108   else
03109     {
03110       _dbus_connection_detach_pending_call_unlocked (connection, pending);
03111       /* we still have a ref to the pending call in this case, we unref
03112        * after unlocking, below
03113        */
03114     }
03115 
03116   status = _dbus_connection_get_dispatch_status_unlocked (connection);
03117 
03118   /* this calls out to user code */
03119   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
03120 
03121   if (pending_return == NULL)
03122     dbus_pending_call_unref (pending);
03123   
03124   return TRUE;
03125 
03126  error:
03127   CONNECTION_UNLOCK (connection);
03128  error_unlocked:
03129   dbus_pending_call_unref (pending);
03130   return FALSE;
03131 }
03132 
03163 DBusMessage*
03164 dbus_connection_send_with_reply_and_block (DBusConnection     *connection,
03165                                            DBusMessage        *message,
03166                                            int                 timeout_milliseconds,
03167                                            DBusError          *error)
03168 {
03169   DBusMessage *reply;
03170   DBusPendingCall *pending;
03171   
03172   _dbus_return_val_if_fail (connection != NULL, NULL);
03173   _dbus_return_val_if_fail (message != NULL, NULL);
03174   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
03175   _dbus_return_val_if_error_is_set (error, NULL);
03176   
03177   if (!dbus_connection_send_with_reply (connection, message,
03178                                         &pending, timeout_milliseconds))
03179     {
03180       _DBUS_SET_OOM (error);
03181       return NULL;
03182     }
03183 
03184   if (pending == NULL)
03185     {
03186       dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
03187       return NULL;
03188     }
03189   
03190   dbus_pending_call_block (pending);
03191 
03192   reply = dbus_pending_call_steal_reply (pending);
03193   dbus_pending_call_unref (pending);
03194 
03195   /* call_complete_and_unlock() called from pending_call_block() should
03196    * always fill this in.
03197    */
03198   _dbus_assert (reply != NULL);
03199   
03200    if (dbus_set_error_from_message (error, reply))
03201     {
03202       dbus_message_unref (reply);
03203       return NULL;
03204     }
03205   else
03206     return reply;
03207 }
03208 
03217 DBusDispatchStatus
03218 _dbus_connection_flush_unlocked (DBusConnection *connection)
03219 {
03220   /* We have to specify DBUS_ITERATION_DO_READING here because
03221    * otherwise we could have two apps deadlock if they are both doing
03222    * a flush(), and the kernel buffers fill up. This could change the
03223    * dispatch status.
03224    */
03225   DBusDispatchStatus status;
03226 
03227   HAVE_LOCK_CHECK (connection);
03228   
03229   while (connection->n_outgoing > 0 &&
03230          _dbus_connection_get_is_connected_unlocked (connection))
03231     {
03232       _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
03233       HAVE_LOCK_CHECK (connection);
03234       _dbus_connection_do_iteration_unlocked (connection,
03235                                               DBUS_ITERATION_DO_READING |
03236                                               DBUS_ITERATION_DO_WRITING |
03237                                               DBUS_ITERATION_BLOCK,
03238                                               -1);
03239     }
03240 
03241   HAVE_LOCK_CHECK (connection);
03242   _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
03243   status = _dbus_connection_get_dispatch_status_unlocked (connection);
03244 
03245   HAVE_LOCK_CHECK (connection);
03246   return status;
03247 }
03248 
03254 void
03255 dbus_connection_flush (DBusConnection *connection)
03256 {
03257   /* We have to specify DBUS_ITERATION_DO_READING here because
03258    * otherwise we could have two apps deadlock if they are both doing
03259    * a flush(), and the kernel buffers fill up. This could change the
03260    * dispatch status.
03261    */
03262   DBusDispatchStatus status;
03263 
03264   _dbus_return_if_fail (connection != NULL);
03265   
03266   CONNECTION_LOCK (connection);
03267 
03268   status = _dbus_connection_flush_unlocked (connection);
03269   
03270   HAVE_LOCK_CHECK (connection);
03271   /* Unlocks and calls out to user code */
03272   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
03273 
03274   _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
03275 }
03276 
03287 static dbus_bool_t
03288 _dbus_connection_read_write_dispatch (DBusConnection *connection,
03289                                      int             timeout_milliseconds, 
03290                                      dbus_bool_t     dispatch)
03291 {
03292   DBusDispatchStatus dstatus;
03293   dbus_bool_t no_progress_possible;
03294   
03295   dstatus = dbus_connection_get_dispatch_status (connection);
03296 
03297   if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
03298     {
03299       _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
03300       dbus_connection_dispatch (connection);
03301       CONNECTION_LOCK (connection);
03302     }
03303   else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
03304     {
03305       _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
03306       _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
03307       CONNECTION_LOCK (connection);
03308     }
03309   else
03310     {
03311       CONNECTION_LOCK (connection);
03312       if (_dbus_connection_get_is_connected_unlocked (connection))
03313         {
03314           _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
03315           _dbus_connection_do_iteration_unlocked (connection,
03316                                                   DBUS_ITERATION_DO_READING |
03317                                                   DBUS_ITERATION_DO_WRITING |
03318                                                   DBUS_ITERATION_BLOCK,
03319                                                   timeout_milliseconds);
03320         }
03321     }
03322   
03323   HAVE_LOCK_CHECK (connection);
03324   /* If we can dispatch, we can make progress until the Disconnected message
03325    * has been processed; if we can only read/write, we can make progress
03326    * as long as the transport is open.
03327    */
03328   if (dispatch)
03329     no_progress_possible = connection->n_incoming == 0 &&
03330       connection->disconnect_message_link == NULL;
03331   else
03332     no_progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
03333   CONNECTION_UNLOCK (connection);
03334   return !no_progress_possible; /* TRUE if we can make more progress */
03335 }
03336 
03337 
03372 dbus_bool_t
03373 dbus_connection_read_write_dispatch (DBusConnection *connection,
03374                                      int             timeout_milliseconds)
03375 {
03376   _dbus_return_val_if_fail (connection != NULL, FALSE);
03377   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
03378    return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
03379 }
03380 
03404 dbus_bool_t 
03405 dbus_connection_read_write (DBusConnection *connection, 
03406                             int             timeout_milliseconds) 
03407 { 
03408   _dbus_return_val_if_fail (connection != NULL, FALSE);
03409   _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
03410    return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
03411 }
03412 
03413 /* We need to call this anytime we pop the head of the queue, and then
03414  * update_dispatch_status_and_unlock needs to be called afterward
03415  * which will "process" the disconnected message and set
03416  * disconnected_message_processed.
03417  */
03418 static void
03419 check_disconnected_message_arrived_unlocked (DBusConnection *connection,
03420                                              DBusMessage    *head_of_queue)
03421 {
03422   HAVE_LOCK_CHECK (connection);
03423 
03424   /* checking that the link is NULL is an optimization to avoid the is_signal call */
03425   if (connection->disconnect_message_link == NULL &&
03426       dbus_message_is_signal (head_of_queue,
03427                               DBUS_INTERFACE_LOCAL,
03428                               "Disconnected"))
03429     {
03430       connection->disconnected_message_arrived = TRUE;
03431     }
03432 }
03433 
03453 DBusMessage*
03454 dbus_connection_borrow_message (DBusConnection *connection)
03455 {
03456   DBusDispatchStatus status;
03457   DBusMessage *message;
03458 
03459   _dbus_return_val_if_fail (connection != NULL, NULL);
03460 
03461   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
03462   
03463   /* this is called for the side effect that it queues
03464    * up any messages from the transport
03465    */
03466   status = dbus_connection_get_dispatch_status (connection);
03467   if (status != DBUS_DISPATCH_DATA_REMAINS)
03468     return NULL;
03469   
03470   CONNECTION_LOCK (connection);
03471 
03472   _dbus_connection_acquire_dispatch (connection);
03473 
03474   /* While a message is outstanding, the dispatch lock is held */
03475   _dbus_assert (connection->message_borrowed == NULL);
03476 
03477   connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
03478   
03479   message = connection->message_borrowed;
03480 
03481   check_disconnected_message_arrived_unlocked (connection, message);
03482   
03483   /* Note that we KEEP the dispatch lock until the message is returned */
03484   if (message == NULL)
03485     _dbus_connection_release_dispatch (connection);
03486 
03487   CONNECTION_UNLOCK (connection);
03488 
03489   /* We don't update dispatch status until it's returned or stolen */
03490   
03491   return message;
03492 }
03493 
03502 void
03503 dbus_connection_return_message (DBusConnection *connection,
03504                                 DBusMessage    *message)
03505 {
03506   DBusDispatchStatus status;
03507   
03508   _dbus_return_if_fail (connection != NULL);
03509   _dbus_return_if_fail (message != NULL);
03510   _dbus_return_if_fail (message == connection->message_borrowed);
03511   _dbus_return_if_fail (connection->dispatch_acquired);
03512   
03513   CONNECTION_LOCK (connection);
03514   
03515   _dbus_assert (message == connection->message_borrowed);
03516   
03517   connection->message_borrowed = NULL;
03518 
03519   _dbus_connection_release_dispatch (connection); 
03520 
03521   status = _dbus_connection_get_dispatch_status_unlocked (connection);
03522   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
03523 }
03524 
03534 void
03535 dbus_connection_steal_borrowed_message (DBusConnection *connection,
03536                                         DBusMessage    *message)
03537 {
03538   DBusMessage *pop_message;
03539   DBusDispatchStatus status;
03540 
03541   _dbus_return_if_fail (connection != NULL);
03542   _dbus_return_if_fail (message != NULL);
03543   _dbus_return_if_fail (message == connection->message_borrowed);
03544   _dbus_return_if_fail (connection->dispatch_acquired);
03545   
03546   CONNECTION_LOCK (connection);
03547  
03548   _dbus_assert (message == connection->message_borrowed);
03549 
03550   pop_message = _dbus_list_pop_first (&connection->incoming_messages);
03551   _dbus_assert (message == pop_message);
03552   
03553   connection->n_incoming -= 1;
03554  
03555   _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
03556                  message, connection->n_incoming);
03557  
03558   connection->message_borrowed = NULL;
03559 
03560   _dbus_connection_release_dispatch (connection);
03561 
03562   status = _dbus_connection_get_dispatch_status_unlocked (connection);
03563   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
03564 }
03565 
03566 /* See dbus_connection_pop_message, but requires the caller to own
03567  * the lock before calling. May drop the lock while running.
03568  */
03569 static DBusList*
03570 _dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
03571 {
03572   HAVE_LOCK_CHECK (connection);
03573   
03574   _dbus_assert (connection->message_borrowed == NULL);
03575   
03576   if (connection->n_incoming > 0)
03577     {
03578       DBusList *link;
03579 
03580       link = _dbus_list_pop_first_link (&connection->incoming_messages);
03581       connection->n_incoming -= 1;
03582 
03583       _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
03584                      link->data,
03585                      dbus_message_get_type (link->data),
03586                      dbus_message_get_path (link->data) ?
03587                      dbus_message_get_path (link->data) :
03588                      "no path",
03589                      dbus_message_get_interface (link->data) ?
03590                      dbus_message_get_interface (link->data) :
03591                      "no interface",
03592                      dbus_message_get_member (link->data) ?
03593                      dbus_message_get_member (link->data) :
03594                      "no member",
03595                      dbus_message_get_signature (link->data),
03596                      connection, connection->n_incoming);
03597 
03598       check_disconnected_message_arrived_unlocked (connection, link->data);
03599       
03600       return link;
03601     }
03602   else
03603     return NULL;
03604 }
03605 
03606 /* See dbus_connection_pop_message, but requires the caller to own
03607  * the lock before calling. May drop the lock while running.
03608  */
03609 static DBusMessage*
03610 _dbus_connection_pop_message_unlocked (DBusConnection *connection)
03611 {
03612   DBusList *link;
03613 
03614   HAVE_LOCK_CHECK (connection);
03615   
03616   link = _dbus_connection_pop_message_link_unlocked (connection);
03617 
03618   if (link != NULL)
03619     {
03620       DBusMessage *message;
03621       
03622       message = link->data;
03623       
03624       _dbus_list_free_link (link);
03625       
03626       return message;
03627     }
03628   else
03629     return NULL;
03630 }
03631 
03632 static void
03633 _dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
03634                                                 DBusList       *message_link)
03635 {
03636   HAVE_LOCK_CHECK (connection);
03637   
03638   _dbus_assert (message_link != NULL);
03639   /* You can't borrow a message while a link is outstanding */
03640   _dbus_assert (connection->message_borrowed == NULL);
03641   /* We had to have the dispatch lock across the pop/putback */
03642   _dbus_assert (connection->dispatch_acquired);
03643 
03644   _dbus_list_prepend_link (&connection->incoming_messages,
03645                            message_link);
03646   connection->n_incoming += 1;
03647 
03648   _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
03649                  message_link->data,
03650                  dbus_message_get_type (message_link->data),
03651                  dbus_message_get_interface (message_link->data) ?
03652                  dbus_message_get_interface (message_link->data) :
03653                  "no interface",
03654                  dbus_message_get_member (message_link->data) ?
03655                  dbus_message_get_member (message_link->data) :
03656                  "no member",
03657                  dbus_message_get_signature (message_link->data),
03658                  connection, connection->n_incoming);
03659 }
03660 
03680 DBusMessage*
03681 dbus_connection_pop_message (DBusConnection *connection)
03682 {
03683   DBusMessage *message;
03684   DBusDispatchStatus status;
03685 
03686   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
03687   
03688   /* this is called for the side effect that it queues
03689    * up any messages from the transport
03690    */
03691   status = dbus_connection_get_dispatch_status (connection);
03692   if (status != DBUS_DISPATCH_DATA_REMAINS)
03693     return NULL;
03694   
03695   CONNECTION_LOCK (connection);
03696   _dbus_connection_acquire_dispatch (connection);
03697   HAVE_LOCK_CHECK (connection);
03698   
03699   message = _dbus_connection_pop_message_unlocked (connection);
03700 
03701   _dbus_verbose ("Returning popped message %p\n", message);    
03702 
03703   _dbus_connection_release_dispatch (connection);
03704 
03705   status = _dbus_connection_get_dispatch_status_unlocked (connection);
03706   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
03707   
03708   return message;
03709 }
03710 
03718 static void
03719 _dbus_connection_acquire_dispatch (DBusConnection *connection)
03720 {
03721   HAVE_LOCK_CHECK (connection);
03722 
03723   _dbus_connection_ref_unlocked (connection);
03724   CONNECTION_UNLOCK (connection);
03725   
03726   _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
03727   _dbus_mutex_lock (connection->dispatch_mutex);
03728 
03729   while (connection->dispatch_acquired)
03730     {
03731       _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
03732       _dbus_condvar_wait (connection->dispatch_cond, 
03733                           connection->dispatch_mutex);
03734     }
03735   
03736   _dbus_assert (!connection->dispatch_acquired);
03737 
03738   connection->dispatch_acquired = TRUE;
03739 
03740   _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
03741   _dbus_mutex_unlock (connection->dispatch_mutex);
03742   
03743   CONNECTION_LOCK (connection);
03744   _dbus_connection_unref_unlocked (connection);
03745 }
03746 
03754 static void
03755 _dbus_connection_release_dispatch (DBusConnection *connection)
03756 {
03757   HAVE_LOCK_CHECK (connection);
03758   
03759   _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
03760   _dbus_mutex_lock (connection->dispatch_mutex);
03761   
03762   _dbus_assert (connection->dispatch_acquired);
03763 
03764   connection->dispatch_acquired = FALSE;
03765   _dbus_condvar_wake_one (connection->dispatch_cond);
03766 
03767   _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
03768   _dbus_mutex_unlock (connection->dispatch_mutex);
03769 }
03770 
03771 static void
03772 _dbus_connection_failed_pop (DBusConnection *connection,
03773                              DBusList       *message_link)
03774 {
03775   _dbus_list_prepend_link (&connection->incoming_messages,
03776                            message_link);
03777   connection->n_incoming += 1;
03778 }
03779 
03780 /* Note this may be called multiple times since we don't track whether we already did it */
03781 static void
03782 notify_disconnected_unlocked (DBusConnection *connection)
03783 {
03784   HAVE_LOCK_CHECK (connection);
03785 
03786   /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
03787    * connection from dbus_bus_get(). We make the same guarantee for
03788    * dbus_connection_open() but in a different way since we don't want to
03789    * unref right here; we instead check for connectedness before returning
03790    * the connection from the hash.
03791    */
03792   _dbus_bus_notify_shared_connection_disconnected_unlocked (connection);
03793 
03794   /* Dump the outgoing queue, we aren't going to be able to
03795    * send it now, and we'd like accessors like
03796    * dbus_connection_get_outgoing_size() to be accurate.
03797    */
03798   if (connection->n_outgoing > 0)
03799     {
03800       DBusList *link;
03801       
03802       _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
03803                      connection->n_outgoing);
03804       
03805       while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
03806         {
03807           _dbus_connection_message_sent (connection, link->data);
03808         }
03809     } 
03810 }
03811 
03812 /* Note this may be called multiple times since we don't track whether we already did it */
03813 static DBusDispatchStatus
03814 notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
03815 {
03816   HAVE_LOCK_CHECK (connection);
03817   
03818   if (connection->disconnect_message_link != NULL)
03819     {
03820       _dbus_verbose ("Sending disconnect message from %s\n",
03821                      _DBUS_FUNCTION_NAME);
03822       
03823       /* If we have pending calls, queue their timeouts - we want the Disconnected
03824        * to be the last message, after these timeouts.
03825        */
03826       connection_timeout_and_complete_all_pending_calls_unlocked (connection);
03827       
03828       /* We haven't sent the disconnect message already,
03829        * and all real messages have been queued up.
03830        */
03831       _dbus_connection_queue_synthesized_message_link (connection,
03832                                                        connection->disconnect_message_link);
03833       connection->disconnect_message_link = NULL;
03834 
03835       return DBUS_DISPATCH_DATA_REMAINS;
03836     }
03837 
03838   return DBUS_DISPATCH_COMPLETE;
03839 }
03840 
03841 static DBusDispatchStatus
03842 _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
03843 {
03844   HAVE_LOCK_CHECK (connection);
03845   
03846   if (connection->n_incoming > 0)
03847     return DBUS_DISPATCH_DATA_REMAINS;
03848   else if (!_dbus_transport_queue_messages (connection->transport))
03849     return DBUS_DISPATCH_NEED_MEMORY;
03850   else
03851     {
03852       DBusDispatchStatus status;
03853       dbus_bool_t is_connected;
03854       
03855       status = _dbus_transport_get_dispatch_status (connection->transport);
03856       is_connected = _dbus_transport_get_is_connected (connection->transport);
03857 
03858       _dbus_verbose ("dispatch status = %s is_connected = %d\n",
03859                      DISPATCH_STATUS_NAME (status), is_connected);
03860       
03861       if (!is_connected)
03862         {
03863           /* It's possible this would be better done by having an explicit
03864            * notification from _dbus_transport_disconnect() that would
03865            * synchronously do this, instead of waiting for the next dispatch
03866            * status check. However, probably not good to change until it causes
03867            * a problem.
03868            */
03869           notify_disconnected_unlocked (connection);
03870 
03871           /* I'm not sure this is needed; the idea is that we want to
03872            * queue the Disconnected only after we've read all the
03873            * messages, but if we're disconnected maybe we are guaranteed
03874            * to have read them all ?
03875            */
03876           if (status == DBUS_DISPATCH_COMPLETE)
03877             status = notify_disconnected_and_dispatch_complete_unlocked (connection);
03878         }
03879       
03880       if (status != DBUS_DISPATCH_COMPLETE)
03881         return status;
03882       else if (connection->n_incoming > 0)
03883         return DBUS_DISPATCH_DATA_REMAINS;
03884       else
03885         return DBUS_DISPATCH_COMPLETE;
03886     }
03887 }
03888 
03889 static void
03890 _dbus_connection_update_dispatch_status_and_unlock (DBusConnection    *connection,
03891                                                     DBusDispatchStatus new_status)
03892 {
03893   dbus_bool_t changed;
03894   DBusDispatchStatusFunction function;
03895   void *data;
03896 
03897   HAVE_LOCK_CHECK (connection);
03898 
03899   _dbus_connection_ref_unlocked (connection);
03900 
03901   changed = new_status != connection->last_dispatch_status;
03902 
03903   connection->last_dispatch_status = new_status;
03904 
03905   function = connection->dispatch_status_function;
03906   data = connection->dispatch_status_data;
03907 
03908   if (connection->disconnected_message_arrived &&
03909       !connection->disconnected_message_processed)
03910     {
03911       connection->disconnected_message_processed = TRUE;
03912       
03913       /* this does an unref, but we have a ref
03914        * so we should not run the finalizer here
03915        * inside the lock.
03916        */
03917       connection_forget_shared_unlocked (connection);
03918 
03919       if (connection->exit_on_disconnect)
03920         {
03921           CONNECTION_UNLOCK (connection);            
03922           
03923           _dbus_verbose ("Exiting on Disconnected signal\n");
03924           _dbus_exit (1);
03925           _dbus_assert_not_reached ("Call to exit() returned");
03926         }
03927     }
03928   
03929   /* We drop the lock */
03930   CONNECTION_UNLOCK (connection);
03931   
03932   if (changed && function)
03933     {
03934       _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
03935                      connection, new_status,
03936                      DISPATCH_STATUS_NAME (new_status));
03937       (* function) (connection, new_status, data);      
03938     }
03939   
03940   dbus_connection_unref (connection);
03941 }
03942 
03968 DBusDispatchStatus
03969 dbus_connection_get_dispatch_status (DBusConnection *connection)
03970 {
03971   DBusDispatchStatus status;
03972 
03973   _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
03974 
03975   _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
03976   
03977   CONNECTION_LOCK (connection);
03978 
03979   status = _dbus_connection_get_dispatch_status_unlocked (connection);
03980   
03981   CONNECTION_UNLOCK (connection);
03982 
03983   return status;
03984 }
03985 
03989 static DBusHandlerResult
03990 _dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
03991                                                  DBusMessage    *message)
03992 {
03993   if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
03994     {
03995       /* This means we're letting the bus route this message */
03996       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
03997     }
03998   else if (dbus_message_is_method_call (message,
03999                                         DBUS_INTERFACE_PEER,
04000                                         "Ping"))
04001     {
04002       DBusMessage *ret;
04003       dbus_bool_t sent;
04004       
04005       ret = dbus_message_new_method_return (message);
04006       if (ret == NULL)
04007         return DBUS_HANDLER_RESULT_NEED_MEMORY;
04008      
04009       sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
04010 
04011       dbus_message_unref (ret);
04012 
04013       if (!sent)
04014         return DBUS_HANDLER_RESULT_NEED_MEMORY;
04015       
04016       return DBUS_HANDLER_RESULT_HANDLED;
04017     }
04018   else if (dbus_message_is_method_call (message,
04019                                         DBUS_INTERFACE_PEER,
04020                                         "GetMachineId"))
04021     {
04022       DBusMessage *ret;
04023       dbus_bool_t sent;
04024       DBusString uuid;
04025       
04026       ret = dbus_message_new_method_return (message);
04027       if (ret == NULL)
04028         return DBUS_HANDLER_RESULT_NEED_MEMORY;
04029 
04030       sent = FALSE;
04031       _dbus_string_init (&uuid);
04032       if (_dbus_get_local_machine_uuid_encoded (&uuid))
04033         {
04034           const char *v_STRING = _dbus_string_get_const_data (&uuid);
04035           if (dbus_message_append_args (ret,
04036                                         DBUS_TYPE_STRING, &v_STRING,
04037                                         DBUS_TYPE_INVALID))
04038             {
04039               sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
04040             }
04041         }
04042       _dbus_string_free (&uuid);
04043       
04044       dbus_message_unref (ret);
04045 
04046       if (!sent)
04047         return DBUS_HANDLER_RESULT_NEED_MEMORY;
04048       
04049       return DBUS_HANDLER_RESULT_HANDLED;
04050     }
04051   else if (dbus_message_has_interface (message, DBUS_INTERFACE_PEER))
04052     {
04053       /* We need to bounce anything else with this interface, otherwise apps
04054        * could start extending the interface and when we added extensions
04055        * here to DBusConnection we'd break those apps.
04056        */
04057       
04058       DBusMessage *ret;
04059       dbus_bool_t sent;
04060       
04061       ret = dbus_message_new_error (message,
04062                                     DBUS_ERROR_UNKNOWN_METHOD,
04063                                     "Unknown method invoked on org.freedesktop.DBus.Peer interface");
04064       if (ret == NULL)
04065         return DBUS_HANDLER_RESULT_NEED_MEMORY;
04066       
04067       sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
04068       
04069       dbus_message_unref (ret);
04070       
04071       if (!sent)
04072         return DBUS_HANDLER_RESULT_NEED_MEMORY;
04073       
04074       return DBUS_HANDLER_RESULT_HANDLED;
04075     }
04076   else
04077     {
04078       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
04079     }
04080 }
04081 
04088 static DBusHandlerResult
04089 _dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
04090                                                            DBusMessage    *message)
04091 {
04092   /* We just run one filter for now but have the option to run more
04093      if the spec calls for it in the future */
04094 
04095   return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
04096 }
04097 
04140 DBusDispatchStatus
04141 dbus_connection_dispatch (DBusConnection *connection)
04142 {
04143   DBusMessage *message;
04144   DBusList *link, *filter_list_copy, *message_link;
04145   DBusHandlerResult result;
04146   DBusPendingCall *pending;
04147   dbus_int32_t reply_serial;
04148   DBusDispatchStatus status;
04149 
04150   _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
04151 
04152   _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
04153   
04154   CONNECTION_LOCK (connection);
04155   status = _dbus_connection_get_dispatch_status_unlocked (connection);
04156   if (status != DBUS_DISPATCH_DATA_REMAINS)
04157     {
04158       /* unlocks and calls out to user code */
04159       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
04160       return status;
04161     }
04162   
04163   /* We need to ref the connection since the callback could potentially
04164    * drop the last ref to it
04165    */
04166   _dbus_connection_ref_unlocked (connection);
04167 
04168   _dbus_connection_acquire_dispatch (connection);
04169   HAVE_LOCK_CHECK (connection);
04170 
04171   message_link = _dbus_connection_pop_message_link_unlocked (connection);
04172   if (message_link == NULL)
04173     {
04174       /* another thread dispatched our stuff */
04175 
04176       _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
04177       
04178       _dbus_connection_release_dispatch (connection);
04179 
04180       status = _dbus_connection_get_dispatch_status_unlocked (connection);
04181 
04182       _dbus_connection_update_dispatch_status_and_unlock (connection, status);
04183       
04184       dbus_connection_unref (connection);
04185       
04186       return status;
04187     }
04188 
04189   message = message_link->data;
04190 
04191   _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
04192                  message,
04193                  dbus_message_get_type (message),
04194                  dbus_message_get_interface (message) ?
04195                  dbus_message_get_interface (message) :
04196                  "no interface",
04197                  dbus_message_get_member (message) ?
04198                  dbus_message_get_member (message) :
04199                  "no member",
04200                  dbus_message_get_signature (message));
04201 
04202   result = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
04203   
04204   /* Pending call handling must be first, because if you do
04205    * dbus_connection_send_with_reply_and_block() or
04206    * dbus_pending_call_block() then no handlers/filters will be run on
04207    * the reply. We want consistent semantics in the case where we
04208    * dbus_connection_dispatch() the reply.
04209    */
04210   
04211   reply_serial = dbus_message_get_reply_serial (message);
04212   pending = _dbus_hash_table_lookup_int (connection->pending_replies,
04213                                          reply_serial);
04214   if (pending)
04215     {
04216       _dbus_verbose ("Dispatching a pending reply\n");
04217       complete_pending_call_and_unlock (connection, pending, message);
04218       pending = NULL; /* it's probably unref'd */
04219       
04220       CONNECTION_LOCK (connection);
04221       _dbus_verbose ("pending call completed in dispatch\n");
04222       result = DBUS_HANDLER_RESULT_HANDLED;
04223       goto out;
04224     }
04225 
04226   result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
04227   if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
04228     goto out;
04229  
04230   if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
04231     {
04232       _dbus_connection_release_dispatch (connection);
04233       HAVE_LOCK_CHECK (connection);
04234       
04235       _dbus_connection_failed_pop (connection, message_link);
04236 
04237       /* unlocks and calls user code */
04238       _dbus_connection_update_dispatch_status_and_unlock (connection,
04239                                                           DBUS_DISPATCH_NEED_MEMORY);
04240 
04241       if (pending)
04242         dbus_pending_call_unref (pending);
04243       dbus_connection_unref (connection);
04244       
04245       return DBUS_DISPATCH_NEED_MEMORY;
04246     }
04247   
04248   _dbus_list_foreach (&filter_list_copy,
04249                       (DBusForeachFunction)_dbus_message_filter_ref,
04250                       NULL);
04251 
04252   /* We're still protected from dispatch() reentrancy here
04253    * since we acquired the dispatcher
04254    */
04255   CONNECTION_UNLOCK (connection);
04256   
04257   link = _dbus_list_get_first_link (&filter_list_copy);
04258   while (link != NULL)
04259     {
04260       DBusMessageFilter *filter = link->data;
04261       DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
04262 
04263       if (filter->function == NULL)
04264         {
04265           _dbus_verbose ("  filter was removed in a callback function\n");
04266           link = next;
04267           continue;
04268         }
04269 
04270       _dbus_verbose ("  running filter on message %p\n", message);
04271       result = (* filter->function) (connection, message, filter->user_data);
04272 
04273       if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
04274         break;
04275 
04276       link = next;
04277     }
04278 
04279   _dbus_list_foreach (&filter_list_copy,
04280                       (DBusForeachFunction)_dbus_message_filter_unref,
04281                       NULL);
04282   _dbus_list_clear (&filter_list_copy);
04283   
04284   CONNECTION_LOCK (connection);
04285 
04286   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
04287     {
04288       _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
04289       goto out;
04290     }
04291   else if (result == DBUS_HANDLER_RESULT_HANDLED)
04292     {
04293       _dbus_verbose ("filter handled message in dispatch\n");
04294       goto out;
04295     }
04296 
04297   /* We're still protected from dispatch() reentrancy here
04298    * since we acquired the dispatcher
04299    */
04300   _dbus_verbose ("  running object path dispatch on message %p (%d %s %s '%s')\n",
04301                  message,
04302                  dbus_message_get_type (message),
04303                  dbus_message_get_interface (message) ?
04304                  dbus_message_get_interface (message) :
04305                  "no interface",
04306                  dbus_message_get_member (message) ?
04307                  dbus_message_get_member (message) :
04308                  "no member",
04309                  dbus_message_get_signature (message));
04310 
04311   HAVE_LOCK_CHECK (connection);
04312   result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
04313                                                   message);
04314   
04315   CONNECTION_LOCK (connection);
04316 
04317   if (result != DBUS_HANDLER_RESULT_NOT_YET_HANDLED)
04318     {
04319       _dbus_verbose ("object tree handled message in dispatch\n");
04320       goto out;
04321     }
04322 
04323   if (dbus_message_get_type (message) == DBUS_MESSAGE_TYPE_METHOD_CALL)
04324     {
04325       DBusMessage *reply;
04326       DBusString str;
04327       DBusPreallocatedSend *preallocated;
04328 
04329       _dbus_verbose ("  sending error %s\n",
04330                      DBUS_ERROR_UNKNOWN_METHOD);
04331       
04332       if (!_dbus_string_init (&str))
04333         {
04334           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
04335           _dbus_verbose ("no memory for error string in dispatch\n");
04336           goto out;
04337         }
04338               
04339       if (!_dbus_string_append_printf (&str,
04340                                        "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
04341                                        dbus_message_get_member (message),
04342                                        dbus_message_get_signature (message),
04343                                        dbus_message_get_interface (message)))
04344         {
04345           _dbus_string_free (&str);
04346           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
04347           _dbus_verbose ("no memory for error string in dispatch\n");
04348           goto out;
04349         }
04350       
04351       reply = dbus_message_new_error (message,
04352                                       DBUS_ERROR_UNKNOWN_METHOD,
04353                                       _dbus_string_get_const_data (&str));
04354       _dbus_string_free (&str);
04355 
04356       if (reply == NULL)
04357         {
04358           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
04359           _dbus_verbose ("no memory for error reply in dispatch\n");
04360           goto out;
04361         }
04362       
04363       preallocated = _dbus_connection_preallocate_send_unlocked (connection);
04364 
04365       if (preallocated == NULL)
04366         {
04367           dbus_message_unref (reply);
04368           result = DBUS_HANDLER_RESULT_NEED_MEMORY;
04369           _dbus_verbose ("no memory for error send in dispatch\n");
04370           goto out;
04371         }
04372 
04373       _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
04374                                                              reply, NULL);
04375 
04376       dbus_message_unref (reply);
04377       
04378       result = DBUS_HANDLER_RESULT_HANDLED;
04379     }
04380   
04381   _dbus_verbose ("  done dispatching %p (%d %s %s '%s') on connection %p\n", message,
04382                  dbus_message_get_type (message),
04383                  dbus_message_get_interface (message) ?
04384                  dbus_message_get_interface (message) :
04385                  "no interface",
04386                  dbus_message_get_member (message) ?
04387                  dbus_message_get_member (message) :
04388                  "no member",
04389                  dbus_message_get_signature (message),
04390                  connection);
04391   
04392  out:
04393   if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
04394     {
04395       _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
04396       
04397       /* Put message back, and we'll start over.
04398        * Yes this means handlers must be idempotent if they
04399        * don't return HANDLED; c'est la vie.
04400        */
04401       _dbus_connection_putback_message_link_unlocked (connection,
04402                                                       message_link);
04403     }
04404   else
04405     {
04406       _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
04407       
04408       _dbus_list_free_link (message_link);
04409       dbus_message_unref (message); /* don't want the message to count in max message limits
04410                                      * in computing dispatch status below
04411                                      */
04412     }
04413   
04414   _dbus_connection_release_dispatch (connection);
04415   HAVE_LOCK_CHECK (connection);
04416 
04417   _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
04418   status = _dbus_connection_get_dispatch_status_unlocked (connection);
04419 
04420   /* unlocks and calls user code */
04421   _dbus_connection_update_dispatch_status_and_unlock (connection, status);
04422   
04423   dbus_connection_unref (connection);
04424   
04425   return status;
04426 }
04427 
04486 dbus_bool_t
04487 dbus_connection_set_watch_functions (DBusConnection              *connection,
04488                                      DBusAddWatchFunction         add_function,
04489                                      DBusRemoveWatchFunction      remove_function,
04490                                      DBusWatchToggledFunction     toggled_function,
04491                                      void                        *data,
04492                                      DBusFreeFunction             free_data_function)
04493 {
04494   dbus_bool_t retval;
04495   DBusWatchList *watches;
04496 
04497   _dbus_return_val_if_fail (connection != NULL, FALSE);
04498   
04499   CONNECTION_LOCK (connection);
04500 
04501 #ifndef DBUS_DISABLE_CHECKS
04502   if (connection->watches == NULL)
04503     {
04504       _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
04505                                _DBUS_FUNCTION_NAME);
04506       return FALSE;
04507     }
04508 #endif
04509   
04510   /* ref connection for slightly better reentrancy */
04511   _dbus_connection_ref_unlocked (connection);
04512 
04513   /* This can call back into user code, and we need to drop the
04514    * connection lock when it does. This is kind of a lame
04515    * way to do it.
04516    */
04517   watches = connection->watches;
04518   connection->watches = NULL;
04519   CONNECTION_UNLOCK (connection);
04520 
04521   retval = _dbus_watch_list_set_functions (watches,
04522                                            add_function, remove_function,
04523                                            toggled_function,
04524                                            data, free_data_function);
04525   CONNECTION_LOCK (connection);
04526   connection->watches = watches;
04527   
04528   CONNECTION_UNLOCK (connection);
04529   /* drop our paranoid refcount */
04530   dbus_connection_unref (connection);
04531   
04532   return retval;
04533 }
04534 
04568 dbus_bool_t
04569 dbus_connection_set_timeout_functions   (DBusConnection            *connection,
04570                                          DBusAddTimeoutFunction     add_function,
04571                                          DBusRemoveTimeoutFunction  remove_function,
04572                                          DBusTimeoutToggledFunction toggled_function,
04573                                          void                      *data,
04574                                          DBusFreeFunction           free_data_function)
04575 {
04576   dbus_bool_t retval;
04577   DBusTimeoutList *timeouts;
04578 
04579   _dbus_return_val_if_fail (connection != NULL, FALSE);
04580   
04581   CONNECTION_LOCK (connection);
04582 
04583 #ifndef DBUS_DISABLE_CHECKS
04584   if (connection->timeouts == NULL)
04585     {
04586       _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
04587                                _DBUS_FUNCTION_NAME);
04588       return FALSE;
04589     }
04590 #endif
04591   
04592   /* ref connection for slightly better reentrancy */
04593   _dbus_connection_ref_unlocked (connection);
04594 
04595   timeouts = connection->timeouts;
04596   connection->timeouts = NULL;
04597   CONNECTION_UNLOCK (connection);
04598   
04599   retval = _dbus_timeout_list_set_functions (timeouts,
04600                                              add_function, remove_function,
04601                                              toggled_function,
04602                                              data, free_data_function);
04603   CONNECTION_LOCK (connection);
04604   connection->timeouts = timeouts;
04605   
04606   CONNECTION_UNLOCK (connection);
04607   /* drop our paranoid refcount */
04608   dbus_connection_unref (connection);
04609 
04610   return retval;
04611 }
04612 
04627 void
04628 dbus_connection_set_wakeup_main_function (DBusConnection            *connection,
04629                                           DBusWakeupMainFunction     wakeup_main_function,
04630                                           void                      *data,
04631                                           DBusFreeFunction           free_data_function)
04632 {
04633   void *old_data;
04634   DBusFreeFunction old_free_data;
04635 
04636   _dbus_return_if_fail (connection != NULL);
04637   
04638   CONNECTION_LOCK (connection);
04639   old_data = connection->wakeup_main_data;
04640   old_free_data = connection->free_wakeup_main_data;
04641 
04642   connection->wakeup_main_function = wakeup_main_function;
04643   connection->wakeup_main_data = data;
04644   connection->free_wakeup_main_data = free_data_function;
04645   
04646   CONNECTION_UNLOCK (connection);
04647 
04648   /* Callback outside the lock */
04649   if (old_free_data)
04650     (*old_free_data) (old_data);
04651 }
04652 
04673 void
04674 dbus_connection_set_dispatch_status_function (DBusConnection             *connection,
04675                                               DBusDispatchStatusFunction  function,
04676                                               void                       *data,
04677                                               DBusFreeFunction            free_data_function)
04678 {
04679   void *old_data;
04680   DBusFreeFunction old_free_data;
04681 
04682   _dbus_return_if_fail (connection != NULL);
04683   
04684   CONNECTION_LOCK (connection);
04685   old_data = connection->dispatch_status_data;
04686   old_free_data = connection->free_dispatch_status_data;
04687 
04688   connection->dispatch_status_function = function;
04689   connection->dispatch_status_data = data;
04690   connection->free_dispatch_status_data = free_data_function;
04691   
04692   CONNECTION_UNLOCK (connection);
04693 
04694   /* Callback outside the lock */
04695   if (old_free_data)
04696     (*old_free_data) (old_data);
04697 }
04698 
04718 dbus_bool_t
04719 dbus_connection_get_unix_fd (DBusConnection *connection,
04720                              int            *fd)
04721 {
04722   _dbus_return_val_if_fail (connection != NULL, FALSE);
04723   _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
04724 
04725 #ifdef DBUS_WIN
04726   /* FIXME do this on a lower level */
04727   return FALSE;
04728 #endif
04729   
04730   return dbus_connection_get_socket(connection, fd);
04731 }
04732 
04748 dbus_bool_t
04749 dbus_connection_get_socket(DBusConnection              *connection,
04750                            int                         *fd)
04751 {
04752   dbus_bool_t retval;
04753 
04754   _dbus_return_val_if_fail (connection != NULL, FALSE);
04755   _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
04756   
04757   CONNECTION_LOCK (connection);
04758   
04759   retval = _dbus_transport_get_socket_fd (connection->transport,
04760                                           fd);
04761 
04762   CONNECTION_UNLOCK (connection);
04763 
04764   return retval;
04765 }
04766 
04767 
04788 dbus_bool_t
04789 dbus_connection_get_unix_user (DBusConnection *connection,
04790                                unsigned long  *uid)
04791 {
04792   dbus_bool_t result;
04793 
04794   _dbus_return_val_if_fail (connection != NULL, FALSE);
04795   _dbus_return_val_if_fail (uid != NULL, FALSE);
04796 
04797 #ifdef DBUS_WIN
04798   /* FIXME this should be done at a lower level, but it's kind of hard,
04799    * just want to be sure we don't ship with this API returning
04800    * some weird internal fake uid for 1.0
04801    */
04802   return FALSE;
04803 #endif
04804   
04805   CONNECTION_LOCK (connection);
04806 
04807   if (!_dbus_transport_get_is_authenticated (connection->transport))
04808     result = FALSE;
04809   else
04810     result = _dbus_transport_get_unix_user (connection->transport,
04811                                             uid);
04812   CONNECTION_UNLOCK (connection);
04813 
04814   return result;
04815 }
04816 
04827 dbus_bool_t
04828 dbus_connection_get_unix_process_id (DBusConnection *connection,
04829                                      unsigned long  *pid)
04830 {
04831   dbus_bool_t result;
04832 
04833   _dbus_return_val_if_fail (connection != NULL, FALSE);
04834   _dbus_return_val_if_fail (pid != NULL, FALSE);
04835 
04836 #ifdef DBUS_WIN
04837   /* FIXME this should be done at a lower level, but it's kind of hard,
04838    * just want to be sure we don't ship with this API returning
04839    * some weird internal fake uid for 1.0
04840    */
04841   return FALSE;
04842 #endif
04843   
04844   CONNECTION_LOCK (connection);
04845 
04846   if (!_dbus_transport_get_is_authenticated (connection->transport))
04847     result = FALSE;
04848   else
04849     result = _dbus_transport_get_unix_process_id (connection->transport,
04850                                                   pid);
04851   CONNECTION_UNLOCK (connection);
04852 
04853   return result;
04854 }
04855 
04879 void
04880 dbus_connection_set_unix_user_function (DBusConnection             *connection,
04881                                         DBusAllowUnixUserFunction   function,
04882                                         void                       *data,
04883                                         DBusFreeFunction            free_data_function)
04884 {
04885   void *old_data = NULL;
04886   DBusFreeFunction old_free_function = NULL;
04887 
04888   _dbus_return_if_fail (connection != NULL);
04889   
04890   CONNECTION_LOCK (connection);
04891   _dbus_transport_set_unix_user_function (connection->transport,
04892                                           function, data, free_data_function,
04893                                           &old_data, &old_free_function);
04894   CONNECTION_UNLOCK (connection);
04895 
04896   if (old_free_function != NULL)
04897     (* old_free_function) (old_data);    
04898 }
04899 
04918 void
04919 dbus_connection_set_route_peer_messages (DBusConnection             *connection,
04920                                          dbus_bool_t                 value)
04921 {
04922   _dbus_return_if_fail (connection != NULL);
04923   
04924   CONNECTION_LOCK (connection);
04925   connection->route_peer_messages = TRUE;
04926   CONNECTION_UNLOCK (connection);
04927 }
04928 
04950 dbus_bool_t
04951 dbus_connection_add_filter (DBusConnection            *connection,
04952                             DBusHandleMessageFunction  function,
04953                             void                      *user_data,
04954                             DBusFreeFunction           free_data_function)
04955 {
04956   DBusMessageFilter *filter;
04957   
04958   _dbus_return_val_if_fail (connection != NULL, FALSE);
04959   _dbus_return_val_if_fail (function != NULL, FALSE);
04960 
04961   filter = dbus_new0 (DBusMessageFilter, 1);
04962   if (filter == NULL)
04963     return FALSE;
04964 
04965   filter->refcount.value = 1;
04966   
04967   CONNECTION_LOCK (connection);
04968 
04969   if (!_dbus_list_append (&connection->filter_list,
04970                           filter))
04971     {
04972       _dbus_message_filter_unref (filter);
04973       CONNECTION_UNLOCK (connection);
04974       return FALSE;
04975     }
04976 
04977   /* Fill in filter after all memory allocated,
04978    * so we don't run the free_user_data_function
04979    * if the add_filter() fails
04980    */
04981   
04982   filter->function = function;
04983   filter->user_data = user_data;
04984   filter->free_user_data_function = free_data_function;
04985         
04986   CONNECTION_UNLOCK (connection);
04987   return TRUE;
04988 }
04989 
05002 void
05003 dbus_connection_remove_filter (DBusConnection            *connection,
05004                                DBusHandleMessageFunction  function,
05005                                void                      *user_data)
05006 {
05007   DBusList *link;
05008   DBusMessageFilter *filter;
05009   
05010   _dbus_return_if_fail (connection != NULL);
05011   _dbus_return_if_fail (function != NULL);
05012   
05013   CONNECTION_LOCK (connection);
05014 
05015   filter = NULL;
05016   
05017   link = _dbus_list_get_last_link (&connection->filter_list);
05018   while (link != NULL)
05019     {
05020       filter = link->data;
05021 
05022       if (filter->function == function &&
05023           filter->user_data == user_data)
05024         {
05025           _dbus_list_remove_link (&connection->filter_list, link);
05026           filter->function = NULL;
05027           
05028           break;
05029         }
05030         
05031       link = _dbus_list_get_prev_link (&connection->filter_list, link);
05032       filter = NULL;
05033     }
05034   
05035   CONNECTION_UNLOCK (connection);
05036 
05037   if (filter == NULL)
05038     {
05039       _dbus_warn ("Attempt to remove filter function %p user data %p, but no such filter has been added\n",
05040                   function, user_data);
05041       return;
05042     }
05043 
05044   /* Call application code */
05045   if (filter->free_user_data_function)
05046     (* filter->free_user_data_function) (filter->user_data);
05047 
05048   filter->free_user_data_function = NULL;
05049   filter->user_data = NULL;
05050   
05051   _dbus_message_filter_unref (filter);
05052 }
05053 
05065 dbus_bool_t
05066 dbus_connection_register_object_path (DBusConnection              *connection,
05067                                       const char                  *path,
05068                                       const DBusObjectPathVTable  *vtable,
05069                                       void                        *user_data)
05070 {
05071   char **decomposed_path;
05072   dbus_bool_t retval;
05073   
05074   _dbus_return_val_if_fail (connection != NULL, FALSE);
05075   _dbus_return_val_if_fail (path != NULL, FALSE);
05076   _dbus_return_val_if_fail (path[0] == '/', FALSE);
05077   _dbus_return_val_if_fail (vtable != NULL, FALSE);
05078 
05079   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
05080     return FALSE;
05081 
05082   CONNECTION_LOCK (connection);
05083 
05084   retval = _dbus_object_tree_register (connection->objects,
05085                                        FALSE,
05086                                        (const char **) decomposed_path, vtable,
05087                                        user_data);
05088 
05089   CONNECTION_UNLOCK (connection);
05090 
05091   dbus_free_string_array (decomposed_path);
05092 
05093   return retval;
05094 }
05095 
05108 dbus_bool_t
05109 dbus_connection_register_fallback (DBusConnection              *connection,
05110                                    const char                  *path,
05111                                    const DBusObjectPathVTable  *vtable,
05112                                    void                        *user_data)
05113 {
05114   char **decomposed_path;
05115   dbus_bool_t retval;
05116   
05117   _dbus_return_val_if_fail (connection != NULL, FALSE);
05118   _dbus_return_val_if_fail (path != NULL, FALSE);
05119   _dbus_return_val_if_fail (path[0] == '/', FALSE);
05120   _dbus_return_val_if_fail (vtable != NULL, FALSE);
05121 
05122   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
05123     return FALSE;
05124 
05125   CONNECTION_LOCK (connection);
05126 
05127   retval = _dbus_object_tree_register (connection->objects,
05128                                        TRUE,
05129                                        (const char **) decomposed_path, vtable,
05130                                        user_data);
05131 
05132   CONNECTION_UNLOCK (connection);
05133 
05134   dbus_free_string_array (decomposed_path);
05135 
05136   return retval;
05137 }
05138 
05148 dbus_bool_t
05149 dbus_connection_unregister_object_path (DBusConnection              *connection,
05150                                         const char                  *path)
05151 {
05152   char **decomposed_path;
05153 
05154   _dbus_return_val_if_fail (connection != NULL, FALSE);
05155   _dbus_return_val_if_fail (path != NULL, FALSE);
05156   _dbus_return_val_if_fail (path[0] == '/', FALSE);
05157 
05158   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
05159       return FALSE;
05160 
05161   CONNECTION_LOCK (connection);
05162 
05163   _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
05164 
05165   dbus_free_string_array (decomposed_path);
05166 
05167   return TRUE;
05168 }
05169 
05180 dbus_bool_t
05181 dbus_connection_get_object_path_data (DBusConnection *connection,
05182                                       const char     *path,
05183                                       void          **data_p)
05184 {
05185   char **decomposed_path;
05186 
05187   _dbus_return_val_if_fail (connection != NULL, FALSE);
05188   _dbus_return_val_if_fail (path != NULL, FALSE);
05189   _dbus_return_val_if_fail (data_p != NULL, FALSE);
05190 
05191   *data_p = NULL;
05192   
05193   if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
05194     return FALSE;
05195   
05196   CONNECTION_LOCK (connection);
05197 
05198   *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
05199 
05200   CONNECTION_UNLOCK (connection);
05201 
05202   dbus_free_string_array (decomposed_path);
05203 
05204   return TRUE;
05205 }
05206 
05217 dbus_bool_t
05218 dbus_connection_list_registered (DBusConnection              *connection,
05219                                  const char                  *parent_path,
05220                                  char                      ***child_entries)
05221 {
05222   char **decomposed_path;
05223   dbus_bool_t retval;
05224   _dbus_return_val_if_fail (connection != NULL, FALSE);
05225   _dbus_return_val_if_fail (parent_path != NULL, FALSE);
05226   _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
05227   _dbus_return_val_if_fail (child_entries != NULL, FALSE);
05228 
05229   if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
05230     return FALSE;
05231 
05232   CONNECTION_LOCK (connection);
05233 
05234   retval = _dbus_object_tree_list_registered_and_unlock (connection->objects,
05235                                                          (const char **) decomposed_path,
05236                                                          child_entries);
05237   dbus_free_string_array (decomposed_path);
05238 
05239   return retval;
05240 }
05241 
05242 static DBusDataSlotAllocator slot_allocator;
05243 _DBUS_DEFINE_GLOBAL_LOCK (connection_slots);
05244 
05259 dbus_bool_t
05260 dbus_connection_allocate_data_slot (dbus_int32_t *slot_p)
05261 {
05262   return _dbus_data_slot_allocator_alloc (&slot_allocator,
05263                                           &_DBUS_LOCK_NAME (connection_slots),
05264                                           slot_p);
05265 }
05266 
05278 void
05279 dbus_connection_free_data_slot (dbus_int32_t *slot_p)
05280 {
05281   _dbus_return_if_fail (*slot_p >= 0);
05282   
05283   _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
05284 }
05285 
05299 dbus_bool_t
05300 dbus_connection_set_data (DBusConnection   *connection,
05301                           dbus_int32_t      slot,
05302                           void             *data,
05303                           DBusFreeFunction  free_data_func)
05304 {
05305   DBusFreeFunction old_free_func;
05306   void *old_data;
05307   dbus_bool_t retval;
05308 
05309   _dbus_return_val_if_fail (connection != NULL, FALSE);
05310   _dbus_return_val_if_fail (slot >= 0, FALSE);
05311   
05312   CONNECTION_LOCK (connection);
05313 
05314   retval = _dbus_data_slot_list_set (&slot_allocator,
05315                                      &connection->slot_list,
05316                                      slot, data, free_data_func,
05317                                      &old_free_func, &old_data);
05318   
05319   CONNECTION_UNLOCK (connection);
05320 
05321   if (retval)
05322     {
05323       /* Do the actual free outside the connection lock */
05324       if (old_free_func)
05325         (* old_free_func) (old_data);
05326     }
05327 
05328   return retval;
05329 }
05330 
05339 void*
05340 dbus_connection_get_data (DBusConnection   *connection,
05341                           dbus_int32_t      slot)
05342 {
05343   void *res;
05344 
05345   _dbus_return_val_if_fail (connection != NULL, NULL);
05346   
05347   CONNECTION_LOCK (connection);
05348 
05349   res = _dbus_data_slot_list_get (&slot_allocator,
05350                                   &connection->slot_list,
05351                                   slot);
05352   
05353   CONNECTION_UNLOCK (connection);
05354 
05355   return res;
05356 }
05357 
05364 void
05365 dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe)
05366 {  
05367   _dbus_modify_sigpipe = will_modify_sigpipe != FALSE;
05368 }
05369 
05378 void
05379 dbus_connection_set_max_message_size (DBusConnection *connection,
05380                                       long            size)
05381 {
05382   _dbus_return_if_fail (connection != NULL);
05383   
05384   CONNECTION_LOCK (connection);
05385   _dbus_transport_set_max_message_size (connection->transport,
05386                                         size);
05387   CONNECTION_UNLOCK (connection);
05388 }
05389 
05396 long
05397 dbus_connection_get_max_message_size (DBusConnection *connection)
05398 {
05399   long res;
05400 
05401   _dbus_return_val_if_fail (connection != NULL, 0);
05402   
05403   CONNECTION_LOCK (connection);
05404   res = _dbus_transport_get_max_message_size (connection->transport);
05405   CONNECTION_UNLOCK (connection);
05406   return res;
05407 }
05408 
05434 void
05435 dbus_connection_set_max_received_size (DBusConnection *connection,
05436                                        long            size)
05437 {
05438   _dbus_return_if_fail (connection != NULL);
05439   
05440   CONNECTION_LOCK (connection);
05441   _dbus_transport_set_max_received_size (connection->transport,
05442                                          size);
05443   CONNECTION_UNLOCK (connection);
05444 }
05445 
05452 long
05453 dbus_connection_get_max_received_size (DBusConnection *connection)
05454 {
05455   long res;
05456 
05457   _dbus_return_val_if_fail (connection != NULL, 0);
05458   
05459   CONNECTION_LOCK (connection);
05460   res = _dbus_transport_get_max_received_size (connection->transport);
05461   CONNECTION_UNLOCK (connection);
05462   return res;
05463 }
05464 
05475 long
05476 dbus_connection_get_outgoing_size (DBusConnection *connection)
05477 {
05478   long res;
05479 
05480   _dbus_return_val_if_fail (connection != NULL, 0);
05481   
05482   CONNECTION_LOCK (connection);
05483   res = _dbus_counter_get_value (connection->outgoing_counter);
05484   CONNECTION_UNLOCK (connection);
05485   return res;
05486 }
05487 

Generated on Fri Sep 21 18:12:11 2007 for D-Bus by  doxygen 1.5.1