~ubuntu-branches/ubuntu/maverick/dbus/maverick-proposed

« back to all changes in this revision

Viewing changes to dbus/dbus-connection.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-27 13:06:32 UTC
  • mfrom: (1.1.23 upstream)
  • Revision ID: james.westby@ubuntu.com-20100927130632-bqs145trvchd2lmf
Tags: 1.4.0-0ubuntu1
* New upstream release
 - Fixes https://bugs.freedesktop.org/show_bug.cgi?id=17754 Race condition in protected_change_timeout
 - Requested by various upstream KDE developers http://lists.kde.org/?t=128514970000004&r=1&w=2

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
#include "dbus-list.h"
34
34
#include "dbus-hash.h"
35
35
#include "dbus-message-internal.h"
 
36
#include "dbus-message-private.h"
36
37
#include "dbus-threads.h"
37
38
#include "dbus-protocol.h"
38
39
#include "dbus-dataslot.h"
41
42
#include "dbus-object-tree.h"
42
43
#include "dbus-threads-internal.h"
43
44
#include "dbus-bus.h"
 
45
#include "dbus-marshal-basic.h"
44
46
 
45
47
#ifdef DBUS_DISABLE_CHECKS
46
48
#define TOOK_LOCK_CHECK(connection)
62
64
#define TRACE_LOCKS 1
63
65
 
64
66
#define CONNECTION_LOCK(connection)   do {                                      \
65
 
    if (TRACE_LOCKS) { _dbus_verbose ("  LOCK: %s\n", _DBUS_FUNCTION_NAME); }   \
 
67
    if (TRACE_LOCKS) { _dbus_verbose ("LOCK\n"); }   \
66
68
    _dbus_mutex_lock ((connection)->mutex);                                      \
67
69
    TOOK_LOCK_CHECK (connection);                                               \
68
70
  } while (0)
69
71
 
70
72
#define CONNECTION_UNLOCK(connection) do {                                              \
71
 
    if (TRACE_LOCKS) { _dbus_verbose ("  UNLOCK: %s\n", _DBUS_FUNCTION_NAME);  }        \
 
73
    if (TRACE_LOCKS) { _dbus_verbose ("UNLOCK\n");  }        \
72
74
    RELEASING_LOCK_CHECK (connection);                                                  \
73
75
    _dbus_mutex_unlock ((connection)->mutex);                                            \
74
76
  } while (0)
75
77
 
 
78
#define SLOTS_LOCK(connection) do {                     \
 
79
    _dbus_mutex_lock ((connection)->slot_mutex);        \
 
80
  } while (0)
 
81
 
 
82
#define SLOTS_UNLOCK(connection) do {                   \
 
83
    _dbus_mutex_unlock ((connection)->slot_mutex);      \
 
84
  } while (0)
 
85
 
76
86
#define DISPATCH_STATUS_NAME(s)                                            \
77
87
                     ((s) == DBUS_DISPATCH_COMPLETE ? "complete" :         \
78
88
                      (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
223
233
  DBusList *counter_link;     /**< Preallocated link in the resource counter */
224
234
};
225
235
 
 
236
#ifdef HAVE_DECL_MSG_NOSIGNAL
 
237
static dbus_bool_t _dbus_modify_sigpipe = FALSE;
 
238
#else
226
239
static dbus_bool_t _dbus_modify_sigpipe = TRUE;
 
240
#endif
227
241
 
228
242
/**
229
243
 * Implementation details of DBusConnection. All fields are private.
257
271
  
258
272
  DBusList *filter_list;        /**< List of filters. */
259
273
 
 
274
  DBusMutex *slot_mutex;        /**< Lock on slot_list so overall connection lock need not be taken */
260
275
  DBusDataSlotList slot_list;   /**< Data stored by allocated integer ID */
261
276
 
262
277
  DBusHashTable *pending_replies;  /**< Hash of message serials to #DBusPendingCall. */  
320
335
static DBusDispatchStatus _dbus_connection_flush_unlocked                    (DBusConnection     *connection);
321
336
static void               _dbus_connection_close_possibly_shared_and_unlock  (DBusConnection     *connection);
322
337
static dbus_bool_t        _dbus_connection_get_is_connected_unlocked         (DBusConnection     *connection);
 
338
static dbus_bool_t        _dbus_connection_peek_for_reply_unlocked           (DBusConnection     *connection,
 
339
                                                                              dbus_uint32_t       client_serial);
323
340
 
324
341
static DBusMessageFilter *
325
342
_dbus_message_filter_ref (DBusMessageFilter *filter)
479
496
 
480
497
  _dbus_connection_wakeup_mainloop (connection);
481
498
  
482
 
  _dbus_verbose ("Message %p (%d %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
 
499
  _dbus_verbose ("Message %p (%s %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
483
500
                 message,
484
 
                 dbus_message_get_type (message),
 
501
                 dbus_message_type_to_string (dbus_message_get_type (message)),
485
502
                 dbus_message_get_path (message) ?
486
503
                 dbus_message_get_path (message) :
487
504
                 "no path",
605
622
  
606
623
  connection->n_outgoing -= 1;
607
624
 
608
 
  _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
 
625
  _dbus_verbose ("Message %p (%s %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
609
626
                 message,
610
 
                 dbus_message_get_type (message),
 
627
                 dbus_message_type_to_string (dbus_message_get_type (message)),
611
628
                 dbus_message_get_path (message) ?
612
629
                 dbus_message_get_path (message) :
613
630
                 "no path",
621
638
                 connection, connection->n_outgoing);
622
639
 
623
640
  /* Save this link in the link cache also */
624
 
  _dbus_message_remove_size_counter (message, connection->outgoing_counter,
625
 
                                     &link);
 
641
  _dbus_message_remove_counter (message, connection->outgoing_counter,
 
642
                                &link);
626
643
  _dbus_list_prepend_link (&connection->link_cache, link);
627
644
  
628
645
  dbus_message_unref (message);
647
664
                        DBusWatchToggleFunction toggle_function,
648
665
                        dbus_bool_t             enabled)
649
666
{
650
 
  DBusWatchList *watches;
651
667
  dbus_bool_t retval;
652
 
  
 
668
 
653
669
  HAVE_LOCK_CHECK (connection);
654
670
 
655
 
  /* This isn't really safe or reasonable; a better pattern is the "do everything, then
656
 
   * drop lock and call out" one; but it has to be propagated up through all callers
 
671
  /* The original purpose of protected_change_watch() was to hold a
 
672
   * ref on the connection while dropping the connection lock, then
 
673
   * calling out to the app.  This was a broken hack that did not
 
674
   * work, since the connection was in a hosed state (no WatchList
 
675
   * field) while calling out.
 
676
   *
 
677
   * So for now we'll just keep the lock while calling out. This means
 
678
   * apps are not allowed to call DBusConnection methods inside a
 
679
   * watch function or they will deadlock.
 
680
   *
 
681
   * The "real fix" is to use the _and_unlock() pattern found
 
682
   * elsewhere in the code, to defer calling out to the app until
 
683
   * we're about to drop locks and return flow of control to the app
 
684
   * anyway.
 
685
   *
 
686
   * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
657
687
   */
658
 
  
659
 
  watches = connection->watches;
660
 
  if (watches)
 
688
 
 
689
  if (connection->watches)
661
690
    {
662
 
      connection->watches = NULL;
663
 
      _dbus_connection_ref_unlocked (connection);
664
 
      CONNECTION_UNLOCK (connection);
665
 
 
666
691
      if (add_function)
667
 
        retval = (* add_function) (watches, watch);
 
692
        retval = (* add_function) (connection->watches, watch);
668
693
      else if (remove_function)
669
694
        {
670
695
          retval = TRUE;
671
 
          (* remove_function) (watches, watch);
 
696
          (* remove_function) (connection->watches, watch);
672
697
        }
673
698
      else
674
699
        {
675
700
          retval = TRUE;
676
 
          (* toggle_function) (watches, watch, enabled);
 
701
          (* toggle_function) (connection->watches, watch, enabled);
677
702
        }
678
 
      
679
 
      CONNECTION_LOCK (connection);
680
 
      connection->watches = watches;
681
 
      _dbus_connection_unref_unlocked (connection);
682
 
 
683
703
      return retval;
684
704
    }
685
705
  else
768
788
                          DBusTimeoutToggleFunction toggle_function,
769
789
                          dbus_bool_t               enabled)
770
790
{
771
 
  DBusTimeoutList *timeouts;
772
791
  dbus_bool_t retval;
773
 
  
 
792
 
774
793
  HAVE_LOCK_CHECK (connection);
775
794
 
776
 
  /* This isn't really safe or reasonable; a better pattern is the "do everything, then
777
 
   * drop lock and call out" one; but it has to be propagated up through all callers
 
795
  /* The original purpose of protected_change_timeout() was to hold a
 
796
   * ref on the connection while dropping the connection lock, then
 
797
   * calling out to the app.  This was a broken hack that did not
 
798
   * work, since the connection was in a hosed state (no TimeoutList
 
799
   * field) while calling out.
 
800
   *
 
801
   * So for now we'll just keep the lock while calling out. This means
 
802
   * apps are not allowed to call DBusConnection methods inside a
 
803
   * timeout function or they will deadlock.
 
804
   *
 
805
   * The "real fix" is to use the _and_unlock() pattern found
 
806
   * elsewhere in the code, to defer calling out to the app until
 
807
   * we're about to drop locks and return flow of control to the app
 
808
   * anyway.
 
809
   *
 
810
   * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
778
811
   */
779
 
  
780
 
  timeouts = connection->timeouts;
781
 
  if (timeouts)
 
812
 
 
813
  if (connection->timeouts)
782
814
    {
783
 
      connection->timeouts = NULL;
784
 
      _dbus_connection_ref_unlocked (connection);
785
 
      CONNECTION_UNLOCK (connection);
786
 
 
787
815
      if (add_function)
788
 
        retval = (* add_function) (timeouts, timeout);
 
816
        retval = (* add_function) (connection->timeouts, timeout);
789
817
      else if (remove_function)
790
818
        {
791
819
          retval = TRUE;
792
 
          (* remove_function) (timeouts, timeout);
 
820
          (* remove_function) (connection->timeouts, timeout);
793
821
        }
794
822
      else
795
823
        {
796
824
          retval = TRUE;
797
 
          (* toggle_function) (timeouts, timeout, enabled);
 
825
          (* toggle_function) (connection->timeouts, timeout, enabled);
798
826
        }
799
 
      
800
 
      CONNECTION_LOCK (connection);
801
 
      connection->timeouts = timeouts;
802
 
      _dbus_connection_unref_unlocked (connection);
803
 
 
804
827
      return retval;
805
828
    }
806
829
  else
1027
1050
  /* We will only touch io_path_acquired which is protected by our mutex */
1028
1051
  CONNECTION_UNLOCK (connection);
1029
1052
  
1030
 
  _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
 
1053
  _dbus_verbose ("locking io_path_mutex\n");
1031
1054
  _dbus_mutex_lock (connection->io_path_mutex);
1032
1055
 
1033
 
  _dbus_verbose ("%s start connection->io_path_acquired = %d timeout = %d\n",
1034
 
                 _DBUS_FUNCTION_NAME, connection->io_path_acquired, timeout_milliseconds);
 
1056
  _dbus_verbose ("start connection->io_path_acquired = %d timeout = %d\n",
 
1057
                 connection->io_path_acquired, timeout_milliseconds);
1035
1058
 
1036
1059
  we_acquired = FALSE;
1037
1060
  
1039
1062
    {
1040
1063
      if (timeout_milliseconds != -1)
1041
1064
        {
1042
 
          _dbus_verbose ("%s waiting %d for IO path to be acquirable\n",
1043
 
                         _DBUS_FUNCTION_NAME, timeout_milliseconds);
 
1065
          _dbus_verbose ("waiting %d for IO path to be acquirable\n",
 
1066
                         timeout_milliseconds);
1044
1067
 
1045
1068
          if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1046
1069
                                           connection->io_path_mutex,
1060
1083
        {
1061
1084
          while (connection->io_path_acquired)
1062
1085
            {
1063
 
              _dbus_verbose ("%s waiting for IO path to be acquirable\n", _DBUS_FUNCTION_NAME);
 
1086
              _dbus_verbose ("waiting for IO path to be acquirable\n");
1064
1087
              _dbus_condvar_wait (connection->io_path_cond, 
1065
1088
                                  connection->io_path_mutex);
1066
1089
            }
1073
1096
      connection->io_path_acquired = TRUE;
1074
1097
    }
1075
1098
  
1076
 
  _dbus_verbose ("%s end connection->io_path_acquired = %d we_acquired = %d\n",
1077
 
                 _DBUS_FUNCTION_NAME, connection->io_path_acquired, we_acquired);
 
1099
  _dbus_verbose ("end connection->io_path_acquired = %d we_acquired = %d\n",
 
1100
                 connection->io_path_acquired, we_acquired);
1078
1101
 
1079
 
  _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
 
1102
  _dbus_verbose ("unlocking io_path_mutex\n");
1080
1103
  _dbus_mutex_unlock (connection->io_path_mutex);
1081
1104
 
1082
1105
  CONNECTION_LOCK (connection);
1100
1123
{
1101
1124
  HAVE_LOCK_CHECK (connection);
1102
1125
  
1103
 
  _dbus_verbose ("%s locking io_path_mutex\n", _DBUS_FUNCTION_NAME);
 
1126
  _dbus_verbose ("locking io_path_mutex\n");
1104
1127
  _dbus_mutex_lock (connection->io_path_mutex);
1105
1128
  
1106
1129
  _dbus_assert (connection->io_path_acquired);
1107
1130
 
1108
 
  _dbus_verbose ("%s start connection->io_path_acquired = %d\n",
1109
 
                 _DBUS_FUNCTION_NAME, connection->io_path_acquired);
 
1131
  _dbus_verbose ("start connection->io_path_acquired = %d\n",
 
1132
                 connection->io_path_acquired);
1110
1133
  
1111
1134
  connection->io_path_acquired = FALSE;
1112
1135
  _dbus_condvar_wake_one (connection->io_path_cond);
1113
1136
 
1114
 
  _dbus_verbose ("%s unlocking io_path_mutex\n", _DBUS_FUNCTION_NAME);
 
1137
  _dbus_verbose ("unlocking io_path_mutex\n");
1115
1138
  _dbus_mutex_unlock (connection->io_path_mutex);
1116
1139
}
1117
1140
 
1137
1160
 * you specify DBUS_ITERATION_BLOCK; in that case the function
1138
1161
 * returns immediately.
1139
1162
 *
 
1163
 * If pending is not NULL then a check is made if the pending call
 
1164
 * is completed after the io path has been required. If the call
 
1165
 * has been completed nothing is done. This must be done since
 
1166
 * the _dbus_connection_acquire_io_path releases the connection
 
1167
 * lock for a while.
 
1168
 *
1140
1169
 * Called with connection lock held.
1141
1170
 * 
1142
1171
 * @param connection the connection.
 
1172
 * @param pending the pending call that should be checked or NULL
1143
1173
 * @param flags iteration flags.
1144
1174
 * @param timeout_milliseconds maximum blocking time, or -1 for no limit.
1145
1175
 */
1146
1176
void
1147
1177
_dbus_connection_do_iteration_unlocked (DBusConnection *connection,
 
1178
                                        DBusPendingCall *pending,
1148
1179
                                        unsigned int    flags,
1149
1180
                                        int             timeout_milliseconds)
1150
1181
{
1151
 
  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
 
1182
  _dbus_verbose ("start\n");
1152
1183
  
1153
1184
  HAVE_LOCK_CHECK (connection);
1154
1185
  
1160
1191
    {
1161
1192
      HAVE_LOCK_CHECK (connection);
1162
1193
      
1163
 
      _dbus_transport_do_iteration (connection->transport,
1164
 
                                    flags, timeout_milliseconds);
 
1194
      if ( (pending != NULL) && _dbus_pending_call_get_completed_unlocked(pending))
 
1195
        {
 
1196
          _dbus_verbose ("pending call completed while acquiring I/O path");
 
1197
        }
 
1198
      else if ( (pending != NULL) &&
 
1199
                _dbus_connection_peek_for_reply_unlocked (connection,
 
1200
                                                          _dbus_pending_call_get_reply_serial_unlocked (pending)))
 
1201
        {
 
1202
          _dbus_verbose ("pending call completed while acquiring I/O path (reply found in queue)");
 
1203
        }
 
1204
      else
 
1205
        {
 
1206
          _dbus_transport_do_iteration (connection->transport,
 
1207
                                        flags, timeout_milliseconds);
 
1208
        }
 
1209
 
1165
1210
      _dbus_connection_release_io_path (connection);
1166
1211
    }
1167
1212
 
1168
1213
  HAVE_LOCK_CHECK (connection);
1169
1214
 
1170
 
  _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
 
1215
  _dbus_verbose ("end\n");
1171
1216
}
1172
1217
 
1173
1218
/**
1239
1284
  if (connection->io_path_cond == NULL)
1240
1285
    goto error;
1241
1286
 
 
1287
  _dbus_mutex_new_at_location (&connection->slot_mutex);
 
1288
  if (connection->slot_mutex == NULL)
 
1289
    goto error;
 
1290
 
1242
1291
  disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1243
1292
                                                DBUS_INTERFACE_LOCAL,
1244
1293
                                                "Disconnected");
1315
1364
      _dbus_mutex_free_at_location (&connection->mutex);
1316
1365
      _dbus_mutex_free_at_location (&connection->io_path_mutex);
1317
1366
      _dbus_mutex_free_at_location (&connection->dispatch_mutex);
 
1367
      _dbus_mutex_free_at_location (&connection->slot_mutex);
1318
1368
      dbus_free (connection);
1319
1369
    }
1320
1370
  if (pending_replies)
1432
1482
 
1433
1483
  connection = data;
1434
1484
 
1435
 
  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
 
1485
  _dbus_verbose ("start\n");
1436
1486
  
1437
1487
  CONNECTION_LOCK (connection);
1438
1488
 
1451
1501
 
1452
1502
  HAVE_LOCK_CHECK (connection);
1453
1503
 
1454
 
  _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
 
1504
  _dbus_verbose ("middle\n");
1455
1505
  
1456
1506
  status = _dbus_connection_get_dispatch_status_unlocked (connection);
1457
1507
 
1458
1508
  /* this calls out to user code */
1459
1509
  _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1460
1510
 
1461
 
  _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
 
1511
  _dbus_verbose ("end\n");
1462
1512
  
1463
1513
  return retval;
1464
1514
}
1936
1986
  _dbus_list_prepend_link (&connection->outgoing_messages,
1937
1987
                           preallocated->queue_link);
1938
1988
 
1939
 
  _dbus_message_add_size_counter_link (message,
1940
 
                                       preallocated->counter_link);
 
1989
  _dbus_message_add_counter_link (message,
 
1990
                                  preallocated->counter_link);
1941
1991
 
1942
1992
  dbus_free (preallocated);
1943
1993
  preallocated = NULL;
1948
1998
 
1949
1999
  sig = dbus_message_get_signature (message);
1950
2000
  
1951
 
  _dbus_verbose ("Message %p (%d %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
 
2001
  _dbus_verbose ("Message %p (%s %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
1952
2002
                 message,
1953
 
                 dbus_message_get_type (message),
 
2003
                 dbus_message_type_to_string (dbus_message_get_type (message)),
1954
2004
                 dbus_message_get_path (message) ?
1955
2005
                 dbus_message_get_path (message) :
1956
2006
                 "no path",
1989
2039
   * out immediately, and otherwise get them queued up
1990
2040
   */
1991
2041
  _dbus_connection_do_iteration_unlocked (connection,
 
2042
                                          NULL,
1992
2043
                                          DBUS_ITERATION_DO_WRITING,
1993
2044
                                          -1);
1994
2045
 
2011
2062
                                                         preallocated,
2012
2063
                                                         message, client_serial);
2013
2064
 
2014
 
  _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
 
2065
  _dbus_verbose ("middle\n");
2015
2066
  status = _dbus_connection_get_dispatch_status_unlocked (connection);
2016
2067
 
2017
2068
  /* this calls out to user code */
2157
2208
  return message;
2158
2209
}
2159
2210
 
 
2211
/*
 
2212
 * Peek the incoming queue to see if we got reply for a specific serial
 
2213
 */
 
2214
static dbus_bool_t
 
2215
_dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
 
2216
                                          dbus_uint32_t   client_serial)
 
2217
{
 
2218
  DBusList *link;
 
2219
  HAVE_LOCK_CHECK (connection);
 
2220
 
 
2221
  link = _dbus_list_get_first_link (&connection->incoming_messages);
 
2222
 
 
2223
  while (link != NULL)
 
2224
    {
 
2225
      DBusMessage *reply = link->data;
 
2226
 
 
2227
      if (dbus_message_get_reply_serial (reply) == client_serial)
 
2228
        {
 
2229
          _dbus_verbose ("%s reply to %d found in queue\n", _DBUS_FUNCTION_NAME, client_serial);
 
2230
          return TRUE;
 
2231
        }
 
2232
      link = _dbus_list_get_next_link (&connection->incoming_messages, link);
 
2233
    }
 
2234
 
 
2235
  return FALSE;
 
2236
}
2160
2237
 
2161
2238
/* This is slightly strange since we can pop a message here without
2162
2239
 * the dispatch lock.
2246
2323
                                    _dbus_pending_call_get_reply_serial_unlocked (pending));
2247
2324
  if (reply != NULL)
2248
2325
    {
2249
 
      _dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
 
2326
      _dbus_verbose ("checked for reply\n");
2250
2327
 
2251
2328
      _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2252
2329
 
2333
2410
  /* Now we wait... */
2334
2411
  /* always block at least once as we know we don't have the reply yet */
2335
2412
  _dbus_connection_do_iteration_unlocked (connection,
 
2413
                                          pending,
2336
2414
                                          DBUS_ITERATION_DO_READING |
2337
2415
                                          DBUS_ITERATION_BLOCK,
2338
2416
                                          timeout_milliseconds);
2339
2417
 
2340
2418
 recheck_status:
2341
2419
 
2342
 
  _dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
 
2420
  _dbus_verbose ("top of recheck\n");
2343
2421
  
2344
2422
  HAVE_LOCK_CHECK (connection);
2345
2423
  
2352
2430
   */
2353
2431
  if (_dbus_pending_call_get_completed_unlocked (pending))
2354
2432
    {
2355
 
      _dbus_verbose ("Pending call completed by dispatch in %s\n", _DBUS_FUNCTION_NAME);
 
2433
      _dbus_verbose ("Pending call completed by dispatch\n");
2356
2434
      _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2357
2435
      dbus_pending_call_unref (pending);
2358
2436
      return;
2399
2477
        {          
2400
2478
          /* block again, we don't have the reply buffered yet. */
2401
2479
          _dbus_connection_do_iteration_unlocked (connection,
 
2480
                                                  pending,
2402
2481
                                                  DBUS_ITERATION_DO_READING |
2403
2482
                                                  DBUS_ITERATION_BLOCK,
2404
2483
                                                  timeout_milliseconds - elapsed_milliseconds);
2426
2505
        {          
2427
2506
          /* block again, we don't have the reply buffered yet. */
2428
2507
          _dbus_connection_do_iteration_unlocked (connection,
 
2508
                                                  NULL,
2429
2509
                                                  DBUS_ITERATION_DO_READING |
2430
2510
                                                  DBUS_ITERATION_BLOCK,
2431
2511
                                                  timeout_milliseconds - elapsed_milliseconds);
2558
2638
  
2559
2639
  /* The connection lock is better than the global
2560
2640
   * lock in the atomic increment fallback
 
2641
   *
 
2642
   * (FIXME but for now we always use the atomic version,
 
2643
   * to avoid taking the connection lock, due to
 
2644
   * the mess with set_timeout_functions()/set_watch_functions()
 
2645
   * calling out to the app without dropping locks)
2561
2646
   */
2562
2647
  
2563
 
#ifdef DBUS_HAVE_ATOMIC_INT
 
2648
#if 1
2564
2649
  _dbus_atomic_inc (&connection->refcount);
2565
2650
#else
2566
2651
  CONNECTION_LOCK (connection);
2580
2665
  DBusMessage *message = element;
2581
2666
  DBusConnection *connection = data;
2582
2667
 
2583
 
  _dbus_message_remove_size_counter (message,
2584
 
                                     connection->outgoing_counter,
2585
 
                                     NULL);
 
2668
  _dbus_message_remove_counter (message,
 
2669
                                connection->outgoing_counter,
 
2670
                                NULL);
2586
2671
  dbus_message_unref (message);
2587
2672
}
2588
2673
 
2672
2757
  _dbus_mutex_free_at_location (&connection->io_path_mutex);
2673
2758
  _dbus_mutex_free_at_location (&connection->dispatch_mutex);
2674
2759
 
 
2760
  _dbus_mutex_free_at_location (&connection->slot_mutex);
 
2761
 
2675
2762
  _dbus_mutex_free_at_location (&connection->mutex);
2676
2763
  
2677
2764
  dbus_free (connection);
2706
2793
  
2707
2794
  /* The connection lock is better than the global
2708
2795
   * lock in the atomic increment fallback
 
2796
   *
 
2797
   * (FIXME but for now we always use the atomic version,
 
2798
   * to avoid taking the connection lock, due to
 
2799
   * the mess with set_timeout_functions()/set_watch_functions()
 
2800
   * calling out to the app without dropping locks)
2709
2801
   */
2710
2802
  
2711
 
#ifdef DBUS_HAVE_ATOMIC_INT
 
2803
#if 1
2712
2804
  last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2713
2805
#else
2714
2806
  CONNECTION_LOCK (connection);
2975
3067
  char *id;
2976
3068
 
2977
3069
  _dbus_return_val_if_fail (connection != NULL, NULL);
2978
 
  
 
3070
 
2979
3071
  CONNECTION_LOCK (connection);
2980
3072
  id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2981
3073
  CONNECTION_UNLOCK (connection);
2982
 
  
 
3074
 
2983
3075
  return id;
2984
3076
}
2985
3077
 
2986
3078
/**
 
3079
 * Tests whether a certain type can be send via the connection. This
 
3080
 * will always return TRUE for all types, with the exception of
 
3081
 * DBUS_TYPE_UNIX_FD. The function will return TRUE for
 
3082
 * DBUS_TYPE_UNIX_FD only on systems that know Unix file descriptors
 
3083
 * and can send them via the chosen transport and when the remote side
 
3084
 * supports this.
 
3085
 *
 
3086
 * This function can be used to do runtime checking for types that
 
3087
 * might be unknown to the specific D-Bus client implementation
 
3088
 * version, i.e. it will return FALSE for all types this
 
3089
 * implementation does not know.
 
3090
 *
 
3091
 * @param connection the connection
 
3092
 * @param type the type to check
 
3093
 * @returns TRUE if the type may be send via the connection
 
3094
 */
 
3095
dbus_bool_t
 
3096
dbus_connection_can_send_type(DBusConnection *connection,
 
3097
                                  int type)
 
3098
{
 
3099
  _dbus_return_val_if_fail (connection != NULL, FALSE);
 
3100
 
 
3101
  if (!_dbus_type_is_valid(type))
 
3102
    return FALSE;
 
3103
 
 
3104
  if (type != DBUS_TYPE_UNIX_FD)
 
3105
    return TRUE;
 
3106
 
 
3107
#ifdef HAVE_UNIX_FD_PASSING
 
3108
  {
 
3109
    dbus_bool_t b;
 
3110
 
 
3111
    CONNECTION_LOCK(connection);
 
3112
    b = _dbus_transport_can_pass_unix_fd(connection->transport);
 
3113
    CONNECTION_UNLOCK(connection);
 
3114
 
 
3115
    return b;
 
3116
  }
 
3117
#endif
 
3118
 
 
3119
  return FALSE;
 
3120
}
 
3121
 
 
3122
/**
2987
3123
 * Set whether _exit() should be called when the connection receives a
2988
3124
 * disconnect signal. The call to _exit() comes after any handlers for
2989
3125
 * the disconnect signal run; handlers can cancel the exit by calling
3083
3219
  _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
3084
3220
                        (dbus_message_get_interface (message) != NULL &&
3085
3221
                         dbus_message_get_member (message) != NULL));
3086
 
  
 
3222
 
3087
3223
  CONNECTION_LOCK (connection);
 
3224
 
 
3225
#ifdef HAVE_UNIX_FD_PASSING
 
3226
 
 
3227
  if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
 
3228
      message->n_unix_fds > 0)
 
3229
    {
 
3230
      /* Refuse to send fds on a connection that cannot handle
 
3231
         them. Unfortunately we cannot return a proper error here, so
 
3232
         the best we can is just return. */
 
3233
      CONNECTION_UNLOCK (connection);
 
3234
      return;
 
3235
    }
 
3236
 
 
3237
#endif
 
3238
 
3088
3239
  _dbus_connection_send_preallocated_and_unlock (connection,
3089
3240
                                                 preallocated,
3090
3241
                                                 message, client_serial);
3148
3299
 
3149
3300
  CONNECTION_LOCK (connection);
3150
3301
 
 
3302
#ifdef HAVE_UNIX_FD_PASSING
 
3303
 
 
3304
  if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
 
3305
      message->n_unix_fds > 0)
 
3306
    {
 
3307
      /* Refuse to send fds on a connection that cannot handle
 
3308
         them. Unfortunately we cannot return a proper error here, so
 
3309
         the best we can is just return. */
 
3310
      CONNECTION_UNLOCK (connection);
 
3311
      return FALSE;
 
3312
    }
 
3313
 
 
3314
#endif
 
3315
 
3151
3316
  return _dbus_connection_send_and_unlock (connection,
3152
3317
                                           message,
3153
3318
                                           serial);
3168
3333
                                            _dbus_pending_call_get_timeout_unlocked (pending));
3169
3334
  _dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3170
3335
 
3171
 
  _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
 
3336
  _dbus_verbose ("middle\n");
3172
3337
  status = _dbus_connection_get_dispatch_status_unlocked (connection);
3173
3338
 
3174
3339
  /* Unlocks, and calls out to user code */
3202
3367
 * you want a very short or very long timeout.  If INT_MAX is passed for
3203
3368
 * the timeout, no timeout will be set and the call will block forever.
3204
3369
 *
3205
 
 * @warning if the connection is disconnected, the #DBusPendingCall
3206
 
 * will be set to #NULL, so be careful with this.
3207
 
 * 
 
3370
 * @warning if the connection is disconnected or you try to send Unix
 
3371
 * file descriptors on a connection that does not support them, the
 
3372
 * #DBusPendingCall will be set to #NULL, so be careful with this.
 
3373
 *
3208
3374
 * @param connection the connection
3209
3375
 * @param message the message to send
3210
 
 * @param pending_return return location for a #DBusPendingCall object, or #NULL if connection is disconnected
 
3376
 * @param pending_return return location for a #DBusPendingCall
 
3377
 * object, or #NULL if connection is disconnected or when you try to
 
3378
 * send Unix file descriptors on a connection that does not support
 
3379
 * them.
3211
3380
 * @param timeout_milliseconds timeout in milliseconds, -1 for default or INT_MAX for no timeout
3212
3381
 * @returns #FALSE if no memory, #TRUE otherwise.
3213
3382
 *
3231
3400
 
3232
3401
  CONNECTION_LOCK (connection);
3233
3402
 
 
3403
#ifdef HAVE_UNIX_FD_PASSING
 
3404
 
 
3405
  if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
 
3406
      message->n_unix_fds > 0)
 
3407
    {
 
3408
      /* Refuse to send fds on a connection that cannot handle
 
3409
         them. Unfortunately we cannot return a proper error here, so
 
3410
         the best we can do is return TRUE but leave *pending_return
 
3411
         as NULL. */
 
3412
      CONNECTION_UNLOCK (connection);
 
3413
      return TRUE;
 
3414
    }
 
3415
 
 
3416
#endif
 
3417
 
3234
3418
   if (!_dbus_connection_get_is_connected_unlocked (connection))
3235
3419
    {
3236
3420
      CONNECTION_UNLOCK (connection);
3339
3523
{
3340
3524
  DBusMessage *reply;
3341
3525
  DBusPendingCall *pending;
3342
 
  
 
3526
 
3343
3527
  _dbus_return_val_if_fail (connection != NULL, NULL);
3344
3528
  _dbus_return_val_if_fail (message != NULL, NULL);
3345
3529
  _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3346
3530
  _dbus_return_val_if_error_is_set (error, NULL);
3347
 
  
 
3531
 
 
3532
#ifdef HAVE_UNIX_FD_PASSING
 
3533
 
 
3534
  CONNECTION_LOCK (connection);
 
3535
  if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
 
3536
      message->n_unix_fds > 0)
 
3537
    {
 
3538
      CONNECTION_UNLOCK (connection);
 
3539
      dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
 
3540
      return NULL;
 
3541
    }
 
3542
  CONNECTION_UNLOCK (connection);
 
3543
 
 
3544
#endif
 
3545
 
3348
3546
  if (!dbus_connection_send_with_reply (connection, message,
3349
3547
                                        &pending, timeout_milliseconds))
3350
3548
    {
3400
3598
  while (connection->n_outgoing > 0 &&
3401
3599
         _dbus_connection_get_is_connected_unlocked (connection))
3402
3600
    {
3403
 
      _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
 
3601
      _dbus_verbose ("doing iteration in\n");
3404
3602
      HAVE_LOCK_CHECK (connection);
3405
3603
      _dbus_connection_do_iteration_unlocked (connection,
 
3604
                                              NULL,
3406
3605
                                              DBUS_ITERATION_DO_READING |
3407
3606
                                              DBUS_ITERATION_DO_WRITING |
3408
3607
                                              DBUS_ITERATION_BLOCK,
3410
3609
    }
3411
3610
 
3412
3611
  HAVE_LOCK_CHECK (connection);
3413
 
  _dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
 
3612
  _dbus_verbose ("middle\n");
3414
3613
  status = _dbus_connection_get_dispatch_status_unlocked (connection);
3415
3614
 
3416
3615
  HAVE_LOCK_CHECK (connection);
3442
3641
  /* Unlocks and calls out to user code */
3443
3642
  _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3444
3643
 
3445
 
  _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
 
3644
  _dbus_verbose ("end\n");
3446
3645
}
3447
3646
 
3448
3647
/**
3472
3671
 
3473
3672
  if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3474
3673
    {
3475
 
      _dbus_verbose ("doing dispatch in %s\n", _DBUS_FUNCTION_NAME);
 
3674
      _dbus_verbose ("doing dispatch\n");
3476
3675
      dbus_connection_dispatch (connection);
3477
3676
      CONNECTION_LOCK (connection);
3478
3677
    }
3479
3678
  else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3480
3679
    {
3481
 
      _dbus_verbose ("pausing for memory in %s\n", _DBUS_FUNCTION_NAME);
 
3680
      _dbus_verbose ("pausing for memory\n");
3482
3681
      _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3483
3682
      CONNECTION_LOCK (connection);
3484
3683
    }
3487
3686
      CONNECTION_LOCK (connection);
3488
3687
      if (_dbus_connection_get_is_connected_unlocked (connection))
3489
3688
        {
3490
 
          _dbus_verbose ("doing iteration in %s\n", _DBUS_FUNCTION_NAME);
 
3689
          _dbus_verbose ("doing iteration\n");
3491
3690
          _dbus_connection_do_iteration_unlocked (connection,
 
3691
                                                  NULL,
3492
3692
                                                  DBUS_ITERATION_DO_READING |
3493
3693
                                                  DBUS_ITERATION_DO_WRITING |
3494
3694
                                                  DBUS_ITERATION_BLOCK,
3638
3838
 
3639
3839
  _dbus_return_val_if_fail (connection != NULL, NULL);
3640
3840
 
3641
 
  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
 
3841
  _dbus_verbose ("start\n");
3642
3842
  
3643
3843
  /* this is called for the side effect that it queues
3644
3844
   * up any messages from the transport
3760
3960
      link = _dbus_list_pop_first_link (&connection->incoming_messages);
3761
3961
      connection->n_incoming -= 1;
3762
3962
 
3763
 
      _dbus_verbose ("Message %p (%d %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
 
3963
      _dbus_verbose ("Message %p (%s %s %s %s '%s') removed from incoming queue %p, %d incoming\n",
3764
3964
                     link->data,
3765
 
                     dbus_message_get_type (link->data),
 
3965
                     dbus_message_type_to_string (dbus_message_get_type (link->data)),
3766
3966
                     dbus_message_get_path (link->data) ?
3767
3967
                     dbus_message_get_path (link->data) :
3768
3968
                     "no path",
3825
4025
                           message_link);
3826
4026
  connection->n_incoming += 1;
3827
4027
 
3828
 
  _dbus_verbose ("Message %p (%d %s %s '%s') put back into queue %p, %d incoming\n",
 
4028
  _dbus_verbose ("Message %p (%s %s %s '%s') put back into queue %p, %d incoming\n",
3829
4029
                 message_link->data,
3830
 
                 dbus_message_get_type (message_link->data),
 
4030
                 dbus_message_type_to_string (dbus_message_get_type (message_link->data)),
3831
4031
                 dbus_message_get_interface (message_link->data) ?
3832
4032
                 dbus_message_get_interface (message_link->data) :
3833
4033
                 "no interface",
3863
4063
  DBusMessage *message;
3864
4064
  DBusDispatchStatus status;
3865
4065
 
3866
 
  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
 
4066
  _dbus_verbose ("start\n");
3867
4067
  
3868
4068
  /* this is called for the side effect that it queues
3869
4069
   * up any messages from the transport
3903
4103
  _dbus_connection_ref_unlocked (connection);
3904
4104
  CONNECTION_UNLOCK (connection);
3905
4105
  
3906
 
  _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
 
4106
  _dbus_verbose ("locking dispatch_mutex\n");
3907
4107
  _dbus_mutex_lock (connection->dispatch_mutex);
3908
4108
 
3909
4109
  while (connection->dispatch_acquired)
3910
4110
    {
3911
 
      _dbus_verbose ("%s waiting for dispatch to be acquirable\n", _DBUS_FUNCTION_NAME);
 
4111
      _dbus_verbose ("waiting for dispatch to be acquirable\n");
3912
4112
      _dbus_condvar_wait (connection->dispatch_cond, 
3913
4113
                          connection->dispatch_mutex);
3914
4114
    }
3917
4117
 
3918
4118
  connection->dispatch_acquired = TRUE;
3919
4119
 
3920
 
  _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
 
4120
  _dbus_verbose ("unlocking dispatch_mutex\n");
3921
4121
  _dbus_mutex_unlock (connection->dispatch_mutex);
3922
4122
  
3923
4123
  CONNECTION_LOCK (connection);
3936
4136
{
3937
4137
  HAVE_LOCK_CHECK (connection);
3938
4138
  
3939
 
  _dbus_verbose ("%s locking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
 
4139
  _dbus_verbose ("locking dispatch_mutex\n");
3940
4140
  _dbus_mutex_lock (connection->dispatch_mutex);
3941
4141
  
3942
4142
  _dbus_assert (connection->dispatch_acquired);
3944
4144
  connection->dispatch_acquired = FALSE;
3945
4145
  _dbus_condvar_wake_one (connection->dispatch_cond);
3946
4146
 
3947
 
  _dbus_verbose ("%s unlocking dispatch_mutex\n", _DBUS_FUNCTION_NAME);
 
4147
  _dbus_verbose ("unlocking dispatch_mutex\n");
3948
4148
  _dbus_mutex_unlock (connection->dispatch_mutex);
3949
4149
}
3950
4150
 
3997
4197
  
3998
4198
  if (connection->disconnect_message_link != NULL)
3999
4199
    {
4000
 
      _dbus_verbose ("Sending disconnect message from %s\n",
4001
 
                     _DBUS_FUNCTION_NAME);
 
4200
      _dbus_verbose ("Sending disconnect message\n");
4002
4201
      
4003
4202
      /* If we have pending calls, queue their timeouts - we want the Disconnected
4004
4203
       * to be the last message, after these timeouts.
4152
4351
 
4153
4352
  _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4154
4353
 
4155
 
  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
 
4354
  _dbus_verbose ("start\n");
4156
4355
  
4157
4356
  CONNECTION_LOCK (connection);
4158
4357
 
4329
4528
 
4330
4529
  _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4331
4530
 
4332
 
  _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
 
4531
  _dbus_verbose ("\n");
4333
4532
  
4334
4533
  CONNECTION_LOCK (connection);
4335
4534
  status = _dbus_connection_get_dispatch_status_unlocked (connection);
4368
4567
 
4369
4568
  message = message_link->data;
4370
4569
 
4371
 
  _dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
 
4570
  _dbus_verbose (" dispatching message %p (%s %s %s '%s')\n",
4372
4571
                 message,
4373
 
                 dbus_message_get_type (message),
 
4572
                 dbus_message_type_to_string (dbus_message_get_type (message)),
4374
4573
                 dbus_message_get_interface (message) ?
4375
4574
                 dbus_message_get_interface (message) :
4376
4575
                 "no interface",
4465
4664
 
4466
4665
  if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4467
4666
    {
4468
 
      _dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
 
4667
      _dbus_verbose ("No memory\n");
4469
4668
      goto out;
4470
4669
    }
4471
4670
  else if (result == DBUS_HANDLER_RESULT_HANDLED)
4477
4676
  /* We're still protected from dispatch() reentrancy here
4478
4677
   * since we acquired the dispatcher
4479
4678
   */
4480
 
  _dbus_verbose ("  running object path dispatch on message %p (%d %s %s '%s')\n",
 
4679
  _dbus_verbose ("  running object path dispatch on message %p (%s %s %s '%s')\n",
4481
4680
                 message,
4482
 
                 dbus_message_get_type (message),
 
4681
                 dbus_message_type_to_string (dbus_message_get_type (message)),
4483
4682
                 dbus_message_get_interface (message) ?
4484
4683
                 dbus_message_get_interface (message) :
4485
4684
                 "no interface",
4558
4757
      result = DBUS_HANDLER_RESULT_HANDLED;
4559
4758
    }
4560
4759
  
4561
 
  _dbus_verbose ("  done dispatching %p (%d %s %s '%s') on connection %p\n", message,
4562
 
                 dbus_message_get_type (message),
 
4760
  _dbus_verbose ("  done dispatching %p (%s %s %s '%s') on connection %p\n", message,
 
4761
                 dbus_message_type_to_string (dbus_message_get_type (message)),
4563
4762
                 dbus_message_get_interface (message) ?
4564
4763
                 dbus_message_get_interface (message) :
4565
4764
                 "no interface",
4572
4771
 out:
4573
4772
  if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4574
4773
    {
4575
 
      _dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
 
4774
      _dbus_verbose ("out of memory\n");
4576
4775
      
4577
4776
      /* Put message back, and we'll start over.
4578
4777
       * Yes this means handlers must be idempotent if they
4583
4782
    }
4584
4783
  else
4585
4784
    {
4586
 
      _dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
 
4785
      _dbus_verbose (" ... done dispatching\n");
4587
4786
      
4588
4787
      _dbus_list_free_link (message_link);
4589
4788
      dbus_message_unref (message); /* don't want the message to count in max message limits
4594
4793
  _dbus_connection_release_dispatch (connection);
4595
4794
  HAVE_LOCK_CHECK (connection);
4596
4795
 
4597
 
  _dbus_verbose ("%s before final status update\n", _DBUS_FUNCTION_NAME);
 
4796
  _dbus_verbose ("before final status update\n");
4598
4797
  status = _dbus_connection_get_dispatch_status_unlocked (connection);
4599
4798
 
4600
4799
  /* unlocks and calls user code */
4652
4851
 * should be that dbus_connection_set_watch_functions() has no effect,
4653
4852
 * but the add_function and remove_function may have been called.
4654
4853
 *
4655
 
 * @todo We need to drop the lock when we call the
4656
 
 * add/remove/toggled functions which can be a side effect
4657
 
 * of setting the watch functions.
 
4854
 * @note The thread lock on DBusConnection is held while
 
4855
 * watch functions are invoked, so inside these functions you
 
4856
 * may not invoke any methods on DBusConnection or it will deadlock.
 
4857
 * See the comments in the code or http://lists.freedesktop.org/archives/dbus/2007-July/tread.html#8144
 
4858
 * if you encounter this issue and want to attempt writing a patch.
4658
4859
 * 
4659
4860
 * @param connection the connection.
4660
4861
 * @param add_function function to begin monitoring a new descriptor.
4673
4874
                                     DBusFreeFunction             free_data_function)
4674
4875
{
4675
4876
  dbus_bool_t retval;
4676
 
  DBusWatchList *watches;
4677
4877
 
4678
4878
  _dbus_return_val_if_fail (connection != NULL, FALSE);
4679
4879
  
4680
4880
  CONNECTION_LOCK (connection);
4681
4881
 
4682
 
#ifndef DBUS_DISABLE_CHECKS
4683
 
  if (connection->watches == NULL)
4684
 
    {
4685
 
      _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4686
 
                               _DBUS_FUNCTION_NAME);
4687
 
      return FALSE;
4688
 
    }
4689
 
#endif
4690
 
  
4691
 
  /* ref connection for slightly better reentrancy */
4692
 
  _dbus_connection_ref_unlocked (connection);
4693
 
 
4694
 
  /* This can call back into user code, and we need to drop the
4695
 
   * connection lock when it does. This is kind of a lame
4696
 
   * way to do it.
4697
 
   */
4698
 
  watches = connection->watches;
4699
 
  connection->watches = NULL;
4700
 
  CONNECTION_UNLOCK (connection);
4701
 
 
4702
 
  retval = _dbus_watch_list_set_functions (watches,
 
4882
  retval = _dbus_watch_list_set_functions (connection->watches,
4703
4883
                                           add_function, remove_function,
4704
4884
                                           toggled_function,
4705
4885
                                           data, free_data_function);
4706
 
  CONNECTION_LOCK (connection);
4707
 
  connection->watches = watches;
4708
 
  
 
4886
 
4709
4887
  CONNECTION_UNLOCK (connection);
4710
 
  /* drop our paranoid refcount */
4711
 
  dbus_connection_unref (connection);
4712
 
  
 
4888
 
4713
4889
  return retval;
4714
4890
}
4715
4891
 
4738
4914
 * given remove_function.  The timer interval may change whenever the
4739
4915
 * timeout is added, removed, or toggled.
4740
4916
 *
 
4917
 * @note The thread lock on DBusConnection is held while
 
4918
 * timeout functions are invoked, so inside these functions you
 
4919
 * may not invoke any methods on DBusConnection or it will deadlock.
 
4920
 * See the comments in the code or http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
 
4921
 * if you encounter this issue and want to attempt writing a patch.
 
4922
 *
4741
4923
 * @param connection the connection.
4742
4924
 * @param add_function function to add a timeout.
4743
4925
 * @param remove_function function to remove a timeout.
4755
4937
                                         DBusFreeFunction           free_data_function)
4756
4938
{
4757
4939
  dbus_bool_t retval;
4758
 
  DBusTimeoutList *timeouts;
4759
4940
 
4760
4941
  _dbus_return_val_if_fail (connection != NULL, FALSE);
4761
4942
  
4762
4943
  CONNECTION_LOCK (connection);
4763
4944
 
4764
 
#ifndef DBUS_DISABLE_CHECKS
4765
 
  if (connection->timeouts == NULL)
4766
 
    {
4767
 
      _dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4768
 
                               _DBUS_FUNCTION_NAME);
4769
 
      return FALSE;
4770
 
    }
4771
 
#endif
4772
 
  
4773
 
  /* ref connection for slightly better reentrancy */
4774
 
  _dbus_connection_ref_unlocked (connection);
4775
 
 
4776
 
  timeouts = connection->timeouts;
4777
 
  connection->timeouts = NULL;
4778
 
  CONNECTION_UNLOCK (connection);
4779
 
  
4780
 
  retval = _dbus_timeout_list_set_functions (timeouts,
 
4945
  retval = _dbus_timeout_list_set_functions (connection->timeouts,
4781
4946
                                             add_function, remove_function,
4782
4947
                                             toggled_function,
4783
4948
                                             data, free_data_function);
4784
 
  CONNECTION_LOCK (connection);
4785
 
  connection->timeouts = timeouts;
4786
 
  
 
4949
 
4787
4950
  CONNECTION_UNLOCK (connection);
4788
 
  /* drop our paranoid refcount */
4789
 
  dbus_connection_unref (connection);
4790
4951
 
4791
4952
  return retval;
4792
4953
}
5020
5181
  else
5021
5182
    result = _dbus_transport_get_unix_process_id (connection->transport,
5022
5183
                                                  pid);
5023
 
#ifdef DBUS_WIN
5024
 
  _dbus_assert (!result);
5025
 
#endif
5026
 
  
 
5184
 
5027
5185
  CONNECTION_UNLOCK (connection);
5028
5186
 
5029
5187
  return result;
5749
5907
 * the connection is finalized. The slot number
5750
5908
 * must have been allocated with dbus_connection_allocate_data_slot().
5751
5909
 *
 
5910
 * @note This function does not take the
 
5911
 * main thread lock on DBusConnection, which allows it to be
 
5912
 * used from inside watch and timeout functions. (See the
 
5913
 * note in docs for dbus_connection_set_watch_functions().)
 
5914
 * A side effect of this is that you need to know there's
 
5915
 * a reference held on the connection while invoking
 
5916
 * dbus_connection_set_data(), or the connection could be
 
5917
 * finalized during dbus_connection_set_data().
 
5918
 *
5752
5919
 * @param connection the connection
5753
5920
 * @param slot the slot number
5754
5921
 * @param data the data to store
5768
5935
  _dbus_return_val_if_fail (connection != NULL, FALSE);
5769
5936
  _dbus_return_val_if_fail (slot >= 0, FALSE);
5770
5937
  
5771
 
  CONNECTION_LOCK (connection);
 
5938
  SLOTS_LOCK (connection);
5772
5939
 
5773
5940
  retval = _dbus_data_slot_list_set (&slot_allocator,
5774
5941
                                     &connection->slot_list,
5775
5942
                                     slot, data, free_data_func,
5776
5943
                                     &old_free_func, &old_data);
5777
5944
  
5778
 
  CONNECTION_UNLOCK (connection);
 
5945
  SLOTS_UNLOCK (connection);
5779
5946
 
5780
5947
  if (retval)
5781
5948
    {
5791
5958
 * Retrieves data previously set with dbus_connection_set_data().
5792
5959
 * The slot must still be allocated (must not have been freed).
5793
5960
 *
 
5961
 * @note This function does not take the
 
5962
 * main thread lock on DBusConnection, which allows it to be
 
5963
 * used from inside watch and timeout functions. (See the
 
5964
 * note in docs for dbus_connection_set_watch_functions().)
 
5965
 * A side effect of this is that you need to know there's
 
5966
 * a reference held on the connection while invoking
 
5967
 * dbus_connection_get_data(), or the connection could be
 
5968
 * finalized during dbus_connection_get_data().
 
5969
 *
5794
5970
 * @param connection the connection
5795
5971
 * @param slot the slot to get data from
5796
5972
 * @returns the data, or #NULL if not found
5803
5979
 
5804
5980
  _dbus_return_val_if_fail (connection != NULL, NULL);
5805
5981
  
5806
 
  CONNECTION_LOCK (connection);
 
5982
  SLOTS_LOCK (connection);
5807
5983
 
5808
5984
  res = _dbus_data_slot_list_get (&slot_allocator,
5809
5985
                                  &connection->slot_list,
5810
5986
                                  slot);
5811
5987
  
5812
 
  CONNECTION_UNLOCK (connection);
 
5988
  SLOTS_UNLOCK (connection);
5813
5989
 
5814
5990
  return res;
5815
5991
}
5866
6042
}
5867
6043
 
5868
6044
/**
 
6045
 * Specifies the maximum number of unix fds a message on this
 
6046
 * connection is allowed to receive. Messages with more unix fds will
 
6047
 * result in disconnecting the connection.
 
6048
 *
 
6049
 * @param connection a #DBusConnection
 
6050
 * @param size maximum message unix fds the connection can receive
 
6051
 */
 
6052
void
 
6053
dbus_connection_set_max_message_unix_fds (DBusConnection *connection,
 
6054
                                          long            n)
 
6055
{
 
6056
  _dbus_return_if_fail (connection != NULL);
 
6057
 
 
6058
  CONNECTION_LOCK (connection);
 
6059
  _dbus_transport_set_max_message_unix_fds (connection->transport,
 
6060
                                            n);
 
6061
  CONNECTION_UNLOCK (connection);
 
6062
}
 
6063
 
 
6064
/**
 
6065
 * Gets the value set by dbus_connection_set_max_message_unix_fds().
 
6066
 *
 
6067
 * @param connection the connection
 
6068
 * @returns the max numer of unix fds of a single message
 
6069
 */
 
6070
long
 
6071
dbus_connection_get_max_message_unix_fds (DBusConnection *connection)
 
6072
{
 
6073
  long res;
 
6074
 
 
6075
  _dbus_return_val_if_fail (connection != NULL, 0);
 
6076
 
 
6077
  CONNECTION_LOCK (connection);
 
6078
  res = _dbus_transport_get_max_message_unix_fds (connection->transport);
 
6079
  CONNECTION_UNLOCK (connection);
 
6080
  return res;
 
6081
}
 
6082
 
 
6083
/**
5869
6084
 * Sets the maximum total number of bytes that can be used for all messages
5870
6085
 * received on this connection. Messages count toward the maximum until
5871
6086
 * they are finalized. When the maximum is reached, the connection will
5922
6137
}
5923
6138
 
5924
6139
/**
 
6140
 * Sets the maximum total number of unix fds that can be used for all messages
 
6141
 * received on this connection. Messages count toward the maximum until
 
6142
 * they are finalized. When the maximum is reached, the connection will
 
6143
 * not read more data until some messages are finalized.
 
6144
 *
 
6145
 * The semantics are analogous to those of dbus_connection_set_max_received_size().
 
6146
 *
 
6147
 * @param connection the connection
 
6148
 * @param size the maximum size in bytes of all outstanding messages
 
6149
 */
 
6150
void
 
6151
dbus_connection_set_max_received_unix_fds (DBusConnection *connection,
 
6152
                                           long            n)
 
6153
{
 
6154
  _dbus_return_if_fail (connection != NULL);
 
6155
 
 
6156
  CONNECTION_LOCK (connection);
 
6157
  _dbus_transport_set_max_received_unix_fds (connection->transport,
 
6158
                                             n);
 
6159
  CONNECTION_UNLOCK (connection);
 
6160
}
 
6161
 
 
6162
/**
 
6163
 * Gets the value set by dbus_connection_set_max_received_unix_fds().
 
6164
 *
 
6165
 * @param connection the connection
 
6166
 * @returns the max unix fds of all live messages
 
6167
 */
 
6168
long
 
6169
dbus_connection_get_max_received_unix_fds (DBusConnection *connection)
 
6170
{
 
6171
  long res;
 
6172
 
 
6173
  _dbus_return_val_if_fail (connection != NULL, 0);
 
6174
 
 
6175
  CONNECTION_LOCK (connection);
 
6176
  res = _dbus_transport_get_max_received_unix_fds (connection->transport);
 
6177
  CONNECTION_UNLOCK (connection);
 
6178
  return res;
 
6179
}
 
6180
 
 
6181
/**
5925
6182
 * Gets the approximate size in bytes of all messages in the outgoing
5926
6183
 * message queue. The size is approximate in that you shouldn't use
5927
6184
 * it to decide how many bytes to read off the network or anything
5937
6194
  long res;
5938
6195
 
5939
6196
  _dbus_return_val_if_fail (connection != NULL, 0);
5940
 
  
5941
 
  CONNECTION_LOCK (connection);
5942
 
  res = _dbus_counter_get_value (connection->outgoing_counter);
 
6197
 
 
6198
  CONNECTION_LOCK (connection);
 
6199
  res = _dbus_counter_get_size_value (connection->outgoing_counter);
 
6200
  CONNECTION_UNLOCK (connection);
 
6201
  return res;
 
6202
}
 
6203
 
 
6204
/**
 
6205
 * Gets the approximate number of uni fds of all messages in the
 
6206
 * outgoing message queue.
 
6207
 *
 
6208
 * @param connection the connection
 
6209
 * @returns the number of unix fds that have been queued up but not sent
 
6210
 */
 
6211
long
 
6212
dbus_connection_get_outgoing_unix_fds (DBusConnection *connection)
 
6213
{
 
6214
  long res;
 
6215
 
 
6216
  _dbus_return_val_if_fail (connection != NULL, 0);
 
6217
 
 
6218
  CONNECTION_LOCK (connection);
 
6219
  res = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
5943
6220
  CONNECTION_UNLOCK (connection);
5944
6221
  return res;
5945
6222
}