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"
45
47
#ifdef DBUS_DISABLE_CHECKS
46
48
#define TOOK_LOCK_CHECK(connection)
62
64
#define TRACE_LOCKS 1
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); \
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); \
78
#define SLOTS_LOCK(connection) do { \
79
_dbus_mutex_lock ((connection)->slot_mutex); \
82
#define SLOTS_UNLOCK(connection) do { \
83
_dbus_mutex_unlock ((connection)->slot_mutex); \
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 */
236
#ifdef HAVE_DECL_MSG_NOSIGNAL
237
static dbus_bool_t _dbus_modify_sigpipe = FALSE;
226
239
static dbus_bool_t _dbus_modify_sigpipe = TRUE;
229
243
* Implementation details of DBusConnection. All fields are private.
258
272
DBusList *filter_list; /**< List of filters. */
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 */
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);
324
341
static DBusMessageFilter *
325
342
_dbus_message_filter_ref (DBusMessageFilter *filter)
480
497
_dbus_connection_wakeup_mainloop (connection);
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",
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) :
606
623
connection->n_outgoing -= 1;
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",
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) :
621
638
connection, connection->n_outgoing);
623
640
/* Save this link in the link cache also */
624
_dbus_message_remove_size_counter (message, connection->outgoing_counter,
641
_dbus_message_remove_counter (message, connection->outgoing_counter,
626
643
_dbus_list_prepend_link (&connection->link_cache, link);
628
645
dbus_message_unref (message);
647
664
DBusWatchToggleFunction toggle_function,
648
665
dbus_bool_t enabled)
650
DBusWatchList *watches;
651
667
dbus_bool_t retval;
653
669
HAVE_LOCK_CHECK (connection);
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.
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.
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
686
* See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
659
watches = connection->watches;
689
if (connection->watches)
662
connection->watches = NULL;
663
_dbus_connection_ref_unlocked (connection);
664
CONNECTION_UNLOCK (connection);
666
691
if (add_function)
667
retval = (* add_function) (watches, watch);
692
retval = (* add_function) (connection->watches, watch);
668
693
else if (remove_function)
671
(* remove_function) (watches, watch);
696
(* remove_function) (connection->watches, watch);
676
(* toggle_function) (watches, watch, enabled);
701
(* toggle_function) (connection->watches, watch, enabled);
679
CONNECTION_LOCK (connection);
680
connection->watches = watches;
681
_dbus_connection_unref_unlocked (connection);
768
788
DBusTimeoutToggleFunction toggle_function,
769
789
dbus_bool_t enabled)
771
DBusTimeoutList *timeouts;
772
791
dbus_bool_t retval;
774
793
HAVE_LOCK_CHECK (connection);
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.
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.
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
810
* See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
780
timeouts = connection->timeouts;
813
if (connection->timeouts)
783
connection->timeouts = NULL;
784
_dbus_connection_ref_unlocked (connection);
785
CONNECTION_UNLOCK (connection);
787
815
if (add_function)
788
retval = (* add_function) (timeouts, timeout);
816
retval = (* add_function) (connection->timeouts, timeout);
789
817
else if (remove_function)
792
(* remove_function) (timeouts, timeout);
820
(* remove_function) (connection->timeouts, timeout);
797
(* toggle_function) (timeouts, timeout, enabled);
825
(* toggle_function) (connection->timeouts, timeout, enabled);
800
CONNECTION_LOCK (connection);
801
connection->timeouts = timeouts;
802
_dbus_connection_unref_unlocked (connection);
1027
1050
/* We will only touch io_path_acquired which is protected by our mutex */
1028
1051
CONNECTION_UNLOCK (connection);
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);
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);
1036
1059
we_acquired = FALSE;
1040
1063
if (timeout_milliseconds != -1)
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);
1045
1068
if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1046
1069
connection->io_path_mutex,
1061
1084
while (connection->io_path_acquired)
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);
1073
1096
connection->io_path_acquired = TRUE;
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);
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);
1082
1105
CONNECTION_LOCK (connection);
1101
1124
HAVE_LOCK_CHECK (connection);
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);
1106
1129
_dbus_assert (connection->io_path_acquired);
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);
1111
1134
connection->io_path_acquired = FALSE;
1112
1135
_dbus_condvar_wake_one (connection->io_path_cond);
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);
1137
1160
* you specify DBUS_ITERATION_BLOCK; in that case the function
1138
1161
* returns immediately.
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
1140
1169
* Called with connection lock held.
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.
1147
1177
_dbus_connection_do_iteration_unlocked (DBusConnection *connection,
1178
DBusPendingCall *pending,
1148
1179
unsigned int flags,
1149
1180
int timeout_milliseconds)
1151
_dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
1182
_dbus_verbose ("start\n");
1153
1184
HAVE_LOCK_CHECK (connection);
1161
1192
HAVE_LOCK_CHECK (connection);
1163
_dbus_transport_do_iteration (connection->transport,
1164
flags, timeout_milliseconds);
1194
if ( (pending != NULL) && _dbus_pending_call_get_completed_unlocked(pending))
1196
_dbus_verbose ("pending call completed while acquiring I/O path");
1198
else if ( (pending != NULL) &&
1199
_dbus_connection_peek_for_reply_unlocked (connection,
1200
_dbus_pending_call_get_reply_serial_unlocked (pending)))
1202
_dbus_verbose ("pending call completed while acquiring I/O path (reply found in queue)");
1206
_dbus_transport_do_iteration (connection->transport,
1207
flags, timeout_milliseconds);
1165
1210
_dbus_connection_release_io_path (connection);
1168
1213
HAVE_LOCK_CHECK (connection);
1170
_dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1215
_dbus_verbose ("end\n");
1239
1284
if (connection->io_path_cond == NULL)
1287
_dbus_mutex_new_at_location (&connection->slot_mutex);
1288
if (connection->slot_mutex == NULL)
1242
1291
disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1243
1292
DBUS_INTERFACE_LOCAL,
1244
1293
"Disconnected");
1452
1502
HAVE_LOCK_CHECK (connection);
1454
_dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
1504
_dbus_verbose ("middle\n");
1456
1506
status = _dbus_connection_get_dispatch_status_unlocked (connection);
1458
1508
/* this calls out to user code */
1459
1509
_dbus_connection_update_dispatch_status_and_unlock (connection, status);
1461
_dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
1511
_dbus_verbose ("end\n");
1936
1986
_dbus_list_prepend_link (&connection->outgoing_messages,
1937
1987
preallocated->queue_link);
1939
_dbus_message_add_size_counter_link (message,
1940
preallocated->counter_link);
1989
_dbus_message_add_counter_link (message,
1990
preallocated->counter_link);
1942
1992
dbus_free (preallocated);
1943
1993
preallocated = NULL;
1949
1999
sig = dbus_message_get_signature (message);
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",
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) :
2012
2063
message, client_serial);
2014
_dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
2065
_dbus_verbose ("middle\n");
2015
2066
status = _dbus_connection_get_dispatch_status_unlocked (connection);
2017
2068
/* this calls out to user code */
2157
2208
return message;
2212
* Peek the incoming queue to see if we got reply for a specific serial
2215
_dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
2216
dbus_uint32_t client_serial)
2219
HAVE_LOCK_CHECK (connection);
2221
link = _dbus_list_get_first_link (&connection->incoming_messages);
2223
while (link != NULL)
2225
DBusMessage *reply = link->data;
2227
if (dbus_message_get_reply_serial (reply) == client_serial)
2229
_dbus_verbose ("%s reply to %d found in queue\n", _DBUS_FUNCTION_NAME, client_serial);
2232
link = _dbus_list_get_next_link (&connection->incoming_messages, link);
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)
2249
_dbus_verbose ("%s checked for reply\n", _DBUS_FUNCTION_NAME);
2326
_dbus_verbose ("checked for reply\n");
2251
2328
_dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
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,
2336
2414
DBUS_ITERATION_DO_READING |
2337
2415
DBUS_ITERATION_BLOCK,
2338
2416
timeout_milliseconds);
2340
2418
recheck_status:
2342
_dbus_verbose ("%s top of recheck\n", _DBUS_FUNCTION_NAME);
2420
_dbus_verbose ("top of recheck\n");
2344
2422
HAVE_LOCK_CHECK (connection);
2353
2431
if (_dbus_pending_call_get_completed_unlocked (pending))
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);
2400
2478
/* block again, we don't have the reply buffered yet. */
2401
2479
_dbus_connection_do_iteration_unlocked (connection,
2402
2481
DBUS_ITERATION_DO_READING |
2403
2482
DBUS_ITERATION_BLOCK,
2404
2483
timeout_milliseconds - elapsed_milliseconds);
2427
2506
/* block again, we don't have the reply buffered yet. */
2428
2507
_dbus_connection_do_iteration_unlocked (connection,
2429
2509
DBUS_ITERATION_DO_READING |
2430
2510
DBUS_ITERATION_BLOCK,
2431
2511
timeout_milliseconds - elapsed_milliseconds);
2559
2639
/* The connection lock is better than the global
2560
2640
* lock in the atomic increment fallback
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)
2563
#ifdef DBUS_HAVE_ATOMIC_INT
2564
2649
_dbus_atomic_inc (&connection->refcount);
2566
2651
CONNECTION_LOCK (connection);
2580
2665
DBusMessage *message = element;
2581
2666
DBusConnection *connection = data;
2583
_dbus_message_remove_size_counter (message,
2584
connection->outgoing_counter,
2668
_dbus_message_remove_counter (message,
2669
connection->outgoing_counter,
2586
2671
dbus_message_unref (message);
2707
2794
/* The connection lock is better than the global
2708
2795
* lock in the atomic increment fallback
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)
2711
#ifdef DBUS_HAVE_ATOMIC_INT
2712
2804
last_unref = (_dbus_atomic_dec (&connection->refcount) == 1);
2714
2806
CONNECTION_LOCK (connection);
2977
3069
_dbus_return_val_if_fail (connection != NULL, NULL);
2979
3071
CONNECTION_LOCK (connection);
2980
3072
id = _dbus_strdup (_dbus_transport_get_server_id (connection->transport));
2981
3073
CONNECTION_UNLOCK (connection);
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
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.
3091
* @param connection the connection
3092
* @param type the type to check
3093
* @returns TRUE if the type may be send via the connection
3096
dbus_connection_can_send_type(DBusConnection *connection,
3099
_dbus_return_val_if_fail (connection != NULL, FALSE);
3101
if (!_dbus_type_is_valid(type))
3104
if (type != DBUS_TYPE_UNIX_FD)
3107
#ifdef HAVE_UNIX_FD_PASSING
3111
CONNECTION_LOCK(connection);
3112
b = _dbus_transport_can_pass_unix_fd(connection->transport);
3113
CONNECTION_UNLOCK(connection);
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));
3087
3223
CONNECTION_LOCK (connection);
3225
#ifdef HAVE_UNIX_FD_PASSING
3227
if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3228
message->n_unix_fds > 0)
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);
3088
3239
_dbus_connection_send_preallocated_and_unlock (connection,
3090
3241
message, client_serial);
3149
3300
CONNECTION_LOCK (connection);
3302
#ifdef HAVE_UNIX_FD_PASSING
3304
if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3305
message->n_unix_fds > 0)
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);
3151
3316
return _dbus_connection_send_and_unlock (connection,
3168
3333
_dbus_pending_call_get_timeout_unlocked (pending));
3169
3334
_dbus_pending_call_set_timeout_added_unlocked (pending, FALSE);
3171
_dbus_verbose ("%s middle\n", _DBUS_FUNCTION_NAME);
3336
_dbus_verbose ("middle\n");
3172
3337
status = _dbus_connection_get_dispatch_status_unlocked (connection);
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.
3205
* @warning if the connection is disconnected, the #DBusPendingCall
3206
* will be set to #NULL, so be careful with this.
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.
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
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.
3232
3401
CONNECTION_LOCK (connection);
3403
#ifdef HAVE_UNIX_FD_PASSING
3405
if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3406
message->n_unix_fds > 0)
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
3412
CONNECTION_UNLOCK (connection);
3234
3418
if (!_dbus_connection_get_is_connected_unlocked (connection))
3236
3420
CONNECTION_UNLOCK (connection);
3340
3524
DBusMessage *reply;
3341
3525
DBusPendingCall *pending;
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);
3532
#ifdef HAVE_UNIX_FD_PASSING
3534
CONNECTION_LOCK (connection);
3535
if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3536
message->n_unix_fds > 0)
3538
CONNECTION_UNLOCK (connection);
3539
dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
3542
CONNECTION_UNLOCK (connection);
3348
3546
if (!dbus_connection_send_with_reply (connection, message,
3349
3547
&pending, timeout_milliseconds))
3400
3598
while (connection->n_outgoing > 0 &&
3401
3599
_dbus_connection_get_is_connected_unlocked (connection))
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,
3406
3605
DBUS_ITERATION_DO_READING |
3407
3606
DBUS_ITERATION_DO_WRITING |
3408
3607
DBUS_ITERATION_BLOCK,
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);
3416
3615
HAVE_LOCK_CHECK (connection);
3473
3672
if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
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);
3479
3678
else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
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);
3487
3686
CONNECTION_LOCK (connection);
3488
3687
if (_dbus_connection_get_is_connected_unlocked (connection))
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,
3492
3692
DBUS_ITERATION_DO_READING |
3493
3693
DBUS_ITERATION_DO_WRITING |
3494
3694
DBUS_ITERATION_BLOCK,
3639
3839
_dbus_return_val_if_fail (connection != NULL, NULL);
3641
_dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
3841
_dbus_verbose ("start\n");
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;
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",
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) :
3826
4026
connection->n_incoming += 1;
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;
3866
_dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4066
_dbus_verbose ("start\n");
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);
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);
3909
4109
while (connection->dispatch_acquired)
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);
3918
4118
connection->dispatch_acquired = TRUE;
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);
3923
4123
CONNECTION_LOCK (connection);
3937
4137
HAVE_LOCK_CHECK (connection);
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);
3942
4142
_dbus_assert (connection->dispatch_acquired);
3944
4144
connection->dispatch_acquired = FALSE;
3945
4145
_dbus_condvar_wake_one (connection->dispatch_cond);
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);
3998
4198
if (connection->disconnect_message_link != NULL)
4000
_dbus_verbose ("Sending disconnect message from %s\n",
4001
_DBUS_FUNCTION_NAME);
4200
_dbus_verbose ("Sending disconnect message\n");
4003
4202
/* If we have pending calls, queue their timeouts - we want the Disconnected
4004
4203
* to be the last message, after these timeouts.
4153
4352
_dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4155
_dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
4354
_dbus_verbose ("start\n");
4157
4356
CONNECTION_LOCK (connection);
4330
4529
_dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4332
_dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
4531
_dbus_verbose ("\n");
4334
4533
CONNECTION_LOCK (connection);
4335
4534
status = _dbus_connection_get_dispatch_status_unlocked (connection);
4369
4568
message = message_link->data;
4371
_dbus_verbose (" dispatching message %p (%d %s %s '%s')\n",
4570
_dbus_verbose (" dispatching message %p (%s %s %s '%s')\n",
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",
4466
4665
if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4468
_dbus_verbose ("No memory in %s\n", _DBUS_FUNCTION_NAME);
4667
_dbus_verbose ("No memory\n");
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
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",
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;
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",
4573
4772
if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4575
_dbus_verbose ("out of memory in %s\n", _DBUS_FUNCTION_NAME);
4774
_dbus_verbose ("out of memory\n");
4577
4776
/* Put message back, and we'll start over.
4578
4777
* Yes this means handlers must be idempotent if they
4586
_dbus_verbose (" ... done dispatching in %s\n", _DBUS_FUNCTION_NAME);
4785
_dbus_verbose (" ... done dispatching\n");
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);
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);
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.
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.
4659
4860
* @param connection the connection.
4660
4861
* @param add_function function to begin monitoring a new descriptor.
4673
4874
DBusFreeFunction free_data_function)
4675
4876
dbus_bool_t retval;
4676
DBusWatchList *watches;
4678
4878
_dbus_return_val_if_fail (connection != NULL, FALSE);
4680
4880
CONNECTION_LOCK (connection);
4682
#ifndef DBUS_DISABLE_CHECKS
4683
if (connection->watches == NULL)
4685
_dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4686
_DBUS_FUNCTION_NAME);
4691
/* ref connection for slightly better reentrancy */
4692
_dbus_connection_ref_unlocked (connection);
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
4698
watches = connection->watches;
4699
connection->watches = NULL;
4700
CONNECTION_UNLOCK (connection);
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;
4709
4887
CONNECTION_UNLOCK (connection);
4710
/* drop our paranoid refcount */
4711
dbus_connection_unref (connection);
4738
4914
* given remove_function. The timer interval may change whenever the
4739
4915
* timeout is added, removed, or toggled.
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.
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)
4757
4939
dbus_bool_t retval;
4758
DBusTimeoutList *timeouts;
4760
4941
_dbus_return_val_if_fail (connection != NULL, FALSE);
4762
4943
CONNECTION_LOCK (connection);
4764
#ifndef DBUS_DISABLE_CHECKS
4765
if (connection->timeouts == NULL)
4767
_dbus_warn_check_failed ("Re-entrant call to %s is not allowed\n",
4768
_DBUS_FUNCTION_NAME);
4773
/* ref connection for slightly better reentrancy */
4774
_dbus_connection_ref_unlocked (connection);
4776
timeouts = connection->timeouts;
4777
connection->timeouts = NULL;
4778
CONNECTION_UNLOCK (connection);
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;
4787
4950
CONNECTION_UNLOCK (connection);
4788
/* drop our paranoid refcount */
4789
dbus_connection_unref (connection);
5749
5907
* the connection is finalized. The slot number
5750
5908
* must have been allocated with dbus_connection_allocate_data_slot().
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().
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);
5771
CONNECTION_LOCK (connection);
5938
SLOTS_LOCK (connection);
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);
5778
CONNECTION_UNLOCK (connection);
5945
SLOTS_UNLOCK (connection);
5791
5958
* Retrieves data previously set with dbus_connection_set_data().
5792
5959
* The slot must still be allocated (must not have been freed).
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().
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
5804
5980
_dbus_return_val_if_fail (connection != NULL, NULL);
5806
CONNECTION_LOCK (connection);
5982
SLOTS_LOCK (connection);
5808
5984
res = _dbus_data_slot_list_get (&slot_allocator,
5809
5985
&connection->slot_list,
5812
CONNECTION_UNLOCK (connection);
5988
SLOTS_UNLOCK (connection);
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.
6049
* @param connection a #DBusConnection
6050
* @param size maximum message unix fds the connection can receive
6053
dbus_connection_set_max_message_unix_fds (DBusConnection *connection,
6056
_dbus_return_if_fail (connection != NULL);
6058
CONNECTION_LOCK (connection);
6059
_dbus_transport_set_max_message_unix_fds (connection->transport,
6061
CONNECTION_UNLOCK (connection);
6065
* Gets the value set by dbus_connection_set_max_message_unix_fds().
6067
* @param connection the connection
6068
* @returns the max numer of unix fds of a single message
6071
dbus_connection_get_max_message_unix_fds (DBusConnection *connection)
6075
_dbus_return_val_if_fail (connection != NULL, 0);
6077
CONNECTION_LOCK (connection);
6078
res = _dbus_transport_get_max_message_unix_fds (connection->transport);
6079
CONNECTION_UNLOCK (connection);
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
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.
6145
* The semantics are analogous to those of dbus_connection_set_max_received_size().
6147
* @param connection the connection
6148
* @param size the maximum size in bytes of all outstanding messages
6151
dbus_connection_set_max_received_unix_fds (DBusConnection *connection,
6154
_dbus_return_if_fail (connection != NULL);
6156
CONNECTION_LOCK (connection);
6157
_dbus_transport_set_max_received_unix_fds (connection->transport,
6159
CONNECTION_UNLOCK (connection);
6163
* Gets the value set by dbus_connection_set_max_received_unix_fds().
6165
* @param connection the connection
6166
* @returns the max unix fds of all live messages
6169
dbus_connection_get_max_received_unix_fds (DBusConnection *connection)
6173
_dbus_return_val_if_fail (connection != NULL, 0);
6175
CONNECTION_LOCK (connection);
6176
res = _dbus_transport_get_max_received_unix_fds (connection->transport);
6177
CONNECTION_UNLOCK (connection);
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
5939
6196
_dbus_return_val_if_fail (connection != NULL, 0);
5941
CONNECTION_LOCK (connection);
5942
res = _dbus_counter_get_value (connection->outgoing_counter);
6198
CONNECTION_LOCK (connection);
6199
res = _dbus_counter_get_size_value (connection->outgoing_counter);
6200
CONNECTION_UNLOCK (connection);
6205
* Gets the approximate number of uni fds of all messages in the
6206
* outgoing message queue.
6208
* @param connection the connection
6209
* @returns the number of unix fds that have been queued up but not sent
6212
dbus_connection_get_outgoing_unix_fds (DBusConnection *connection)
6216
_dbus_return_val_if_fail (connection != NULL, 0);
6218
CONNECTION_LOCK (connection);
6219
res = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
5943
6220
CONNECTION_UNLOCK (connection);