D-Bus 1.15.4
dbus-connection.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-connection.c DBusConnection object
3 *
4 * Copyright (C) 2002-2006 Red Hat Inc.
5 *
6 * SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
7 *
8 * Licensed under the Academic Free License version 2.1
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 *
24 */
25
26#include <config.h>
27#include "dbus-shared.h"
28#include "dbus-connection.h"
29#include "dbus-list.h"
30#include "dbus-timeout.h"
31#include "dbus-transport.h"
32#include "dbus-watch.h"
33#include "dbus-connection-internal.h"
34#include "dbus-pending-call-internal.h"
35#include "dbus-list.h"
36#include "dbus-hash.h"
37#include "dbus-message-internal.h"
38#include "dbus-message-private.h"
39#include "dbus-threads.h"
40#include "dbus-protocol.h"
41#include "dbus-dataslot.h"
42#include "dbus-string.h"
43#include "dbus-signature.h"
44#include "dbus-pending-call.h"
45#include "dbus-object-tree.h"
46#include "dbus-threads-internal.h"
47#include "dbus-bus.h"
48#include "dbus-marshal-basic.h"
49
50#ifdef DBUS_DISABLE_CHECKS
51#define TOOK_LOCK_CHECK(connection)
52#define RELEASING_LOCK_CHECK(connection)
53#define HAVE_LOCK_CHECK(connection)
54#else
55#define TOOK_LOCK_CHECK(connection) do { \
56 _dbus_assert (!(connection)->have_connection_lock); \
57 (connection)->have_connection_lock = TRUE; \
58 } while (0)
59#define RELEASING_LOCK_CHECK(connection) do { \
60 _dbus_assert ((connection)->have_connection_lock); \
61 (connection)->have_connection_lock = FALSE; \
62 } while (0)
63#define HAVE_LOCK_CHECK(connection) _dbus_assert ((connection)->have_connection_lock)
64/* A "DO_NOT_HAVE_LOCK_CHECK" is impossible since we need the lock to check the flag */
65#endif
66
67#define TRACE_LOCKS 1
68
69#define CONNECTION_LOCK(connection) do { \
70 if (TRACE_LOCKS) { _dbus_verbose ("LOCK\n"); } \
71 _dbus_rmutex_lock ((connection)->mutex); \
72 TOOK_LOCK_CHECK (connection); \
73 } while (0)
74
75#define CONNECTION_UNLOCK(connection) _dbus_connection_unlock (connection)
76
77#define SLOTS_LOCK(connection) do { \
78 _dbus_rmutex_lock ((connection)->slot_mutex); \
79 } while (0)
80
81#define SLOTS_UNLOCK(connection) do { \
82 _dbus_rmutex_unlock ((connection)->slot_mutex); \
83 } while (0)
84
85#define DISPATCH_STATUS_NAME(s) \
86 ((s) == DBUS_DISPATCH_COMPLETE ? "complete" : \
87 (s) == DBUS_DISPATCH_DATA_REMAINS ? "data remains" : \
88 (s) == DBUS_DISPATCH_NEED_MEMORY ? "need memory" : \
89 "???")
90
208static void
209_dbus_connection_trace_ref (DBusConnection *connection,
210 int old_refcount,
211 int new_refcount,
212 const char *why)
213{
214#ifdef DBUS_ENABLE_VERBOSE_MODE
215 static int enabled = -1;
216
217 _dbus_trace_ref ("DBusConnection", connection, old_refcount, new_refcount,
218 why, "DBUS_CONNECTION_TRACE", &enabled);
219#endif
220}
221
226
231{
234 void *user_data;
236};
237
238
243{
247};
248
249#if HAVE_DECL_MSG_NOSIGNAL
250static DBusAtomic _dbus_modify_sigpipe = { FALSE };
251#else
252static DBusAtomic _dbus_modify_sigpipe = { TRUE };
253#endif
254
259{
310 /* These two MUST be bools and not bitfields, because they are protected by a separate lock
311 * from connection->mutex and all bitfields in a word have to be read/written together.
312 * So you can't have a different lock for different bitfields in the same word.
313 */
317 unsigned int shareable : 1;
319 unsigned int exit_on_disconnect : 1;
321 unsigned int builtin_filters_enabled : 1;
323 unsigned int route_peer_messages : 1;
333#ifndef DBUS_DISABLE_CHECKS
334 unsigned int have_connection_lock : 1;
335#endif
336
337#if defined(DBUS_ENABLE_CHECKS) || defined(DBUS_ENABLE_ASSERT)
338 int generation;
339#endif
340};
341
342static DBusDispatchStatus _dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection);
343static void _dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
344 DBusDispatchStatus new_status);
345static void _dbus_connection_last_unref (DBusConnection *connection);
346static void _dbus_connection_acquire_dispatch (DBusConnection *connection);
347static void _dbus_connection_release_dispatch (DBusConnection *connection);
348static DBusDispatchStatus _dbus_connection_flush_unlocked (DBusConnection *connection);
349static void _dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection);
350static dbus_bool_t _dbus_connection_get_is_connected_unlocked (DBusConnection *connection);
351static dbus_bool_t _dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
352 dbus_uint32_t client_serial);
353
354static DBusMessageFilter *
355_dbus_message_filter_ref (DBusMessageFilter *filter)
356{
357#ifdef DBUS_DISABLE_ASSERT
358 _dbus_atomic_inc (&filter->refcount);
359#else
360 dbus_int32_t old_value;
361
362 old_value = _dbus_atomic_inc (&filter->refcount);
363 _dbus_assert (old_value > 0);
364#endif
365
366 return filter;
367}
368
369static void
370_dbus_message_filter_unref (DBusMessageFilter *filter)
371{
372 dbus_int32_t old_value;
373
374 old_value = _dbus_atomic_dec (&filter->refcount);
375 _dbus_assert (old_value > 0);
376
377 if (old_value == 1)
378 {
379 if (filter->free_user_data_function)
380 (* filter->free_user_data_function) (filter->user_data);
381
382 dbus_free (filter);
383 }
384}
385
391void
393{
394 CONNECTION_LOCK (connection);
395}
396
402void
404{
405 DBusList *expired_messages;
406 DBusList *iter;
407
408 if (TRACE_LOCKS)
409 {
410 _dbus_verbose ("UNLOCK\n");
411 }
412
413 /* If we had messages that expired (fell off the incoming or outgoing
414 * queues) while we were locked, actually release them now */
415 expired_messages = connection->expired_messages;
416 connection->expired_messages = NULL;
417
418 RELEASING_LOCK_CHECK (connection);
419 _dbus_rmutex_unlock (connection->mutex);
420
421 for (iter = _dbus_list_pop_first_link (&expired_messages);
422 iter != NULL;
423 iter = _dbus_list_pop_first_link (&expired_messages))
424 {
425 DBusMessage *message = iter->data;
426
427 dbus_message_unref (message);
429 }
430}
431
439static void
440_dbus_connection_wakeup_mainloop (DBusConnection *connection)
441{
442 if (connection->wakeup_main_function)
443 (*connection->wakeup_main_function) (connection->wakeup_main_data);
444}
445
446#ifdef DBUS_ENABLE_EMBEDDED_TESTS
459void
460_dbus_connection_test_get_locks (DBusConnection *connection,
461 DBusMutex **mutex_loc,
462 DBusMutex **dispatch_mutex_loc,
463 DBusMutex **io_path_mutex_loc,
464 DBusCondVar **dispatch_cond_loc,
465 DBusCondVar **io_path_cond_loc)
466{
467 *mutex_loc = (DBusMutex *) connection->mutex;
468 *dispatch_mutex_loc = (DBusMutex *) connection->dispatch_mutex;
469 *io_path_mutex_loc = (DBusMutex *) connection->io_path_mutex;
470 *dispatch_cond_loc = connection->dispatch_cond;
471 *io_path_cond_loc = connection->io_path_cond;
472}
473#endif
474
483void
485 DBusList *link)
486{
487 DBusPendingCall *pending;
488 dbus_uint32_t reply_serial;
489 DBusMessage *message;
490
492
494 link);
495 message = link->data;
496
497 /* If this is a reply we're waiting on, remove timeout for it */
498 reply_serial = dbus_message_get_reply_serial (message);
499 if (reply_serial != 0)
500 {
501 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
502 reply_serial);
503 if (pending != NULL)
504 {
508
510 }
511 }
512
513
514
515 connection->n_incoming += 1;
516
517 _dbus_connection_wakeup_mainloop (connection);
518
519 _dbus_verbose ("Message %p (%s %s %s %s '%s' reply to %u) added to incoming queue %p, %d incoming\n",
520 message,
522 dbus_message_get_path (message) ?
523 dbus_message_get_path (message) :
524 "no path",
527 "no interface",
528 dbus_message_get_member (message) ?
529 dbus_message_get_member (message) :
530 "no member",
533 connection,
534 connection->n_incoming);
535
536 _dbus_message_trace_ref (message, -1, -1,
537 "_dbus_conection_queue_received_message_link");
538}
539
548void
550 DBusList *link)
551{
552 HAVE_LOCK_CHECK (connection);
553
554 _dbus_list_append_link (&connection->incoming_messages, link);
555
556 connection->n_incoming += 1;
557
558 _dbus_connection_wakeup_mainloop (connection);
559
560 _dbus_message_trace_ref (link->data, -1, -1,
561 "_dbus_connection_queue_synthesized_message_link");
562
563 _dbus_verbose ("Synthesized message %p added to incoming queue %p, %d incoming\n",
564 link->data, connection, connection->n_incoming);
565}
566
567
577{
578 HAVE_LOCK_CHECK (connection);
579 return connection->outgoing_messages != NULL;
580}
581
593{
594 dbus_bool_t v;
595
596 _dbus_return_val_if_fail (connection != NULL, FALSE);
597
598 CONNECTION_LOCK (connection);
600 CONNECTION_UNLOCK (connection);
601
602 return v;
603}
604
614{
615 HAVE_LOCK_CHECK (connection);
616
617 return _dbus_list_get_last (&connection->outgoing_messages);
618}
619
628void
630 DBusMessage *message)
631{
632 DBusList *link;
633
634 HAVE_LOCK_CHECK (connection);
635
636 /* This can be called before we even complete authentication, since
637 * it's called on disconnect to clean up the outgoing queue.
638 * It's also called as we successfully send each message.
639 */
640
641 link = _dbus_list_get_last_link (&connection->outgoing_messages);
642 _dbus_assert (link != NULL);
643 _dbus_assert (link->data == message);
644
646 link);
647 _dbus_list_prepend_link (&connection->expired_messages, link);
648
649 connection->n_outgoing -= 1;
650
651 _dbus_verbose ("Message %p (%s %s %s %s '%s') removed from outgoing queue %p, %d left to send\n",
652 message,
654 dbus_message_get_path (message) ?
655 dbus_message_get_path (message) :
656 "no path",
659 "no interface",
660 dbus_message_get_member (message) ?
661 dbus_message_get_member (message) :
662 "no member",
664 connection, connection->n_outgoing);
665
666 /* It's OK that in principle we call the notify function, because for the
667 * outgoing limit, there isn't one */
668 _dbus_message_remove_counter (message, connection->outgoing_counter);
669
670 /* The message will actually be unreffed when we unlock */
671}
672
675 DBusWatch *watch);
678 DBusWatch *watch);
681 DBusWatch *watch,
682 dbus_bool_t enabled);
683
684static dbus_bool_t
685protected_change_watch (DBusConnection *connection,
686 DBusWatch *watch,
687 DBusWatchAddFunction add_function,
688 DBusWatchRemoveFunction remove_function,
689 DBusWatchToggleFunction toggle_function,
690 dbus_bool_t enabled)
691{
692 dbus_bool_t retval;
693
694 HAVE_LOCK_CHECK (connection);
695
696 /* The original purpose of protected_change_watch() was to hold a
697 * ref on the connection while dropping the connection lock, then
698 * calling out to the app. This was a broken hack that did not
699 * work, since the connection was in a hosed state (no WatchList
700 * field) while calling out.
701 *
702 * So for now we'll just keep the lock while calling out. This means
703 * apps are not allowed to call DBusConnection methods inside a
704 * watch function or they will deadlock.
705 *
706 * The "real fix" is to use the _and_unlock() pattern found
707 * elsewhere in the code, to defer calling out to the app until
708 * we're about to drop locks and return flow of control to the app
709 * anyway.
710 *
711 * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
712 */
713
714 if (connection->watches)
715 {
716 if (add_function)
717 retval = (* add_function) (connection->watches, watch);
718 else if (remove_function)
719 {
720 retval = TRUE;
721 (* remove_function) (connection->watches, watch);
722 }
723 else
724 {
725 retval = TRUE;
726 (* toggle_function) (connection->watches, watch, enabled);
727 }
728 return retval;
729 }
730 else
731 return FALSE;
732}
733
734
748 DBusWatch *watch)
749{
750 return protected_change_watch (connection, watch,
752 NULL, NULL, FALSE);
753}
754
764void
766 DBusWatch *watch)
767{
768 protected_change_watch (connection, watch,
769 NULL,
771 NULL, FALSE);
772}
773
784void
786 DBusWatch *watch,
787 dbus_bool_t enabled)
788{
789 _dbus_assert (watch != NULL);
790
791 protected_change_watch (connection, watch,
792 NULL, NULL,
794 enabled);
795}
796
799 DBusTimeout *timeout);
802 DBusTimeout *timeout);
805 DBusTimeout *timeout,
806 dbus_bool_t enabled);
807
808static dbus_bool_t
809protected_change_timeout (DBusConnection *connection,
810 DBusTimeout *timeout,
811 DBusTimeoutAddFunction add_function,
812 DBusTimeoutRemoveFunction remove_function,
813 DBusTimeoutToggleFunction toggle_function,
814 dbus_bool_t enabled)
815{
816 dbus_bool_t retval;
817
818 HAVE_LOCK_CHECK (connection);
819
820 /* The original purpose of protected_change_timeout() was to hold a
821 * ref on the connection while dropping the connection lock, then
822 * calling out to the app. This was a broken hack that did not
823 * work, since the connection was in a hosed state (no TimeoutList
824 * field) while calling out.
825 *
826 * So for now we'll just keep the lock while calling out. This means
827 * apps are not allowed to call DBusConnection methods inside a
828 * timeout function or they will deadlock.
829 *
830 * The "real fix" is to use the _and_unlock() pattern found
831 * elsewhere in the code, to defer calling out to the app until
832 * we're about to drop locks and return flow of control to the app
833 * anyway.
834 *
835 * See http://lists.freedesktop.org/archives/dbus/2007-July/thread.html#8144
836 */
837
838 if (connection->timeouts)
839 {
840 if (add_function)
841 retval = (* add_function) (connection->timeouts, timeout);
842 else if (remove_function)
843 {
844 retval = TRUE;
845 (* remove_function) (connection->timeouts, timeout);
846 }
847 else
848 {
849 retval = TRUE;
850 (* toggle_function) (connection->timeouts, timeout, enabled);
851 }
852 return retval;
853 }
854 else
855 return FALSE;
856}
857
872 DBusTimeout *timeout)
873{
874 return protected_change_timeout (connection, timeout,
876 NULL, NULL, FALSE);
877}
878
888void
890 DBusTimeout *timeout)
891{
892 protected_change_timeout (connection, timeout,
893 NULL,
895 NULL, FALSE);
896}
897
908void
910 DBusTimeout *timeout,
911 dbus_bool_t enabled)
912{
913 protected_change_timeout (connection, timeout,
914 NULL, NULL,
916 enabled);
917}
918
919static dbus_bool_t
920_dbus_connection_attach_pending_call_unlocked (DBusConnection *connection,
921 DBusPendingCall *pending)
922{
923 dbus_uint32_t reply_serial;
924 DBusTimeout *timeout;
925
926 HAVE_LOCK_CHECK (connection);
927
928 reply_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
929
930 _dbus_assert (reply_serial != 0);
931
932 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
933
934 if (timeout)
935 {
936 if (!_dbus_connection_add_timeout_unlocked (connection, timeout))
937 return FALSE;
938
940 reply_serial,
941 pending))
942 {
943 _dbus_connection_remove_timeout_unlocked (connection, timeout);
944
946 HAVE_LOCK_CHECK (connection);
947 return FALSE;
948 }
949
951 }
952 else
953 {
955 reply_serial,
956 pending))
957 {
958 HAVE_LOCK_CHECK (connection);
959 return FALSE;
960 }
961 }
962
964
965 HAVE_LOCK_CHECK (connection);
966
967 return TRUE;
968}
969
970static void
971free_pending_call_on_hash_removal (void *data)
972{
973 DBusPendingCall *pending;
974 DBusConnection *connection;
975
976 if (data == NULL)
977 return;
978
979 pending = data;
980
981 connection = _dbus_pending_call_get_connection_unlocked (pending);
982
983 HAVE_LOCK_CHECK (connection);
984
986 {
989
991 }
992
993 /* FIXME 1.0? this is sort of dangerous and undesirable to drop the lock
994 * here, but the pending call finalizer could in principle call out to
995 * application code so we pretty much have to... some larger code reorg
996 * might be needed.
997 */
1000 CONNECTION_LOCK (connection);
1002}
1003
1004static void
1005_dbus_connection_detach_pending_call_unlocked (DBusConnection *connection,
1006 DBusPendingCall *pending)
1007{
1008 /* This ends up unlocking to call the pending call finalizer, which is unexpected to
1009 * say the least.
1010 */
1013}
1014
1015static void
1016_dbus_connection_detach_pending_call_and_unlock (DBusConnection *connection,
1017 DBusPendingCall *pending)
1018{
1019 /* The idea here is to avoid finalizing the pending call
1020 * with the lock held, since there's a destroy notifier
1021 * in pending call that goes out to application code.
1022 *
1023 * There's an extra unlock inside the hash table
1024 * "free pending call" function FIXME...
1025 */
1029
1033
1035
1037}
1038
1047void
1049 DBusPendingCall *pending)
1050{
1051 CONNECTION_LOCK (connection);
1052 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
1053}
1054
1064static dbus_bool_t
1065_dbus_connection_acquire_io_path (DBusConnection *connection,
1066 int timeout_milliseconds)
1067{
1068 dbus_bool_t we_acquired;
1069
1070 HAVE_LOCK_CHECK (connection);
1071
1072 /* We don't want the connection to vanish */
1073 _dbus_connection_ref_unlocked (connection);
1074
1075 /* We will only touch io_path_acquired which is protected by our mutex */
1076 CONNECTION_UNLOCK (connection);
1077
1078 _dbus_verbose ("locking io_path_mutex\n");
1079 _dbus_cmutex_lock (connection->io_path_mutex);
1080
1081 _dbus_verbose ("start connection->io_path_acquired = %d timeout = %d\n",
1082 connection->io_path_acquired, timeout_milliseconds);
1083
1084 we_acquired = FALSE;
1085
1086 if (connection->io_path_acquired)
1087 {
1088 if (timeout_milliseconds != -1)
1089 {
1090 _dbus_verbose ("waiting %d for IO path to be acquirable\n",
1091 timeout_milliseconds);
1092
1093 if (!_dbus_condvar_wait_timeout (connection->io_path_cond,
1094 connection->io_path_mutex,
1095 timeout_milliseconds))
1096 {
1097 /* We timed out before anyone signaled. */
1098 /* (writing the loop to handle the !timedout case by
1099 * waiting longer if needed is a pain since dbus
1100 * wraps pthread_cond_timedwait to take a relative
1101 * time instead of absolute, something kind of stupid
1102 * on our part. for now it doesn't matter, we will just
1103 * end up back here eventually.)
1104 */
1105 }
1106 }
1107 else
1108 {
1109 while (connection->io_path_acquired)
1110 {
1111 _dbus_verbose ("waiting for IO path to be acquirable\n");
1112 _dbus_condvar_wait (connection->io_path_cond,
1113 connection->io_path_mutex);
1114 }
1115 }
1116 }
1117
1118 if (!connection->io_path_acquired)
1119 {
1120 we_acquired = TRUE;
1121 connection->io_path_acquired = TRUE;
1122 }
1123
1124 _dbus_verbose ("end connection->io_path_acquired = %d we_acquired = %d\n",
1125 connection->io_path_acquired, we_acquired);
1126
1127 _dbus_verbose ("unlocking io_path_mutex\n");
1128 _dbus_cmutex_unlock (connection->io_path_mutex);
1129
1130 CONNECTION_LOCK (connection);
1131
1132 HAVE_LOCK_CHECK (connection);
1133
1135
1136 return we_acquired;
1137}
1138
1146static void
1147_dbus_connection_release_io_path (DBusConnection *connection)
1148{
1149 HAVE_LOCK_CHECK (connection);
1150
1151 _dbus_verbose ("locking io_path_mutex\n");
1152 _dbus_cmutex_lock (connection->io_path_mutex);
1153
1154 _dbus_assert (connection->io_path_acquired);
1155
1156 _dbus_verbose ("start connection->io_path_acquired = %d\n",
1157 connection->io_path_acquired);
1158
1159 connection->io_path_acquired = FALSE;
1161
1162 _dbus_verbose ("unlocking io_path_mutex\n");
1163 _dbus_cmutex_unlock (connection->io_path_mutex);
1164}
1165
1201void
1203 DBusPendingCall *pending,
1204 unsigned int flags,
1205 int timeout_milliseconds)
1206{
1207 _dbus_verbose ("start\n");
1208
1209 HAVE_LOCK_CHECK (connection);
1210
1211 if (connection->n_outgoing == 0)
1212 flags &= ~DBUS_ITERATION_DO_WRITING;
1213
1214 if (_dbus_connection_acquire_io_path (connection,
1215 (flags & DBUS_ITERATION_BLOCK) ? timeout_milliseconds : 0))
1216 {
1217 HAVE_LOCK_CHECK (connection);
1218
1219 if ( (pending != NULL) && _dbus_pending_call_get_completed_unlocked(pending))
1220 {
1221 _dbus_verbose ("pending call completed while acquiring I/O path");
1222 }
1223 else if ( (pending != NULL) &&
1224 _dbus_connection_peek_for_reply_unlocked (connection,
1226 {
1227 _dbus_verbose ("pending call completed while acquiring I/O path (reply found in queue)");
1228 }
1229 else
1230 {
1232 flags, timeout_milliseconds);
1233 }
1234
1235 _dbus_connection_release_io_path (connection);
1236 }
1237
1238 HAVE_LOCK_CHECK (connection);
1239
1240 _dbus_verbose ("end\n");
1241}
1242
1254{
1255 DBusConnection *connection;
1256 DBusWatchList *watch_list;
1257 DBusTimeoutList *timeout_list;
1258 DBusHashTable *pending_replies;
1259 DBusList *disconnect_link;
1260 DBusMessage *disconnect_message;
1261 DBusCounter *outgoing_counter;
1262 DBusObjectTree *objects;
1263
1264 watch_list = NULL;
1265 connection = NULL;
1266 pending_replies = NULL;
1267 timeout_list = NULL;
1268 disconnect_link = NULL;
1269 disconnect_message = NULL;
1270 outgoing_counter = NULL;
1271 objects = NULL;
1272
1273 watch_list = _dbus_watch_list_new ();
1274 if (watch_list == NULL)
1275 goto error;
1276
1277 timeout_list = _dbus_timeout_list_new ();
1278 if (timeout_list == NULL)
1279 goto error;
1280
1281 pending_replies =
1283 NULL,
1284 (DBusFreeFunction)free_pending_call_on_hash_removal);
1285 if (pending_replies == NULL)
1286 goto error;
1287
1288 connection = dbus_new0 (DBusConnection, 1);
1289 if (connection == NULL)
1290 goto error;
1291
1292 _dbus_rmutex_new_at_location (&connection->mutex);
1293 if (connection->mutex == NULL)
1294 goto error;
1295
1297 if (connection->io_path_mutex == NULL)
1298 goto error;
1299
1301 if (connection->dispatch_mutex == NULL)
1302 goto error;
1303
1305 if (connection->dispatch_cond == NULL)
1306 goto error;
1307
1309 if (connection->io_path_cond == NULL)
1310 goto error;
1311
1313 if (connection->slot_mutex == NULL)
1314 goto error;
1315
1316 disconnect_message = dbus_message_new_signal (DBUS_PATH_LOCAL,
1318 "Disconnected");
1319
1320 if (disconnect_message == NULL)
1321 goto error;
1322
1323 disconnect_link = _dbus_list_alloc_link (disconnect_message);
1324 if (disconnect_link == NULL)
1325 goto error;
1326
1327 outgoing_counter = _dbus_counter_new ();
1328 if (outgoing_counter == NULL)
1329 goto error;
1330
1331 objects = _dbus_object_tree_new (connection);
1332 if (objects == NULL)
1333 goto error;
1334
1335 if (_dbus_atomic_get (&_dbus_modify_sigpipe) != 0)
1337
1338 /* initialized to 0: use atomic op to avoid mixing atomic and non-atomic */
1339 _dbus_atomic_inc (&connection->refcount);
1340 connection->transport = transport;
1341 connection->watches = watch_list;
1342 connection->timeouts = timeout_list;
1343 connection->pending_replies = pending_replies;
1344 connection->outgoing_counter = outgoing_counter;
1345 connection->filter_list = NULL;
1346 connection->last_dispatch_status = DBUS_DISPATCH_COMPLETE; /* so we're notified first time there's data */
1347 connection->objects = objects;
1348 connection->exit_on_disconnect = FALSE;
1349 connection->shareable = FALSE;
1350 connection->builtin_filters_enabled = TRUE;
1351 connection->route_peer_messages = FALSE;
1352 connection->disconnected_message_arrived = FALSE;
1354
1355#if defined(DBUS_ENABLE_CHECKS) || defined(DBUS_ENABLE_ASSERT)
1356 connection->generation = _dbus_current_generation;
1357#endif
1358
1359 _dbus_data_slot_list_init (&connection->slot_list);
1360
1361 connection->client_serial = 1;
1362
1363 connection->disconnect_message_link = disconnect_link;
1364
1365 CONNECTION_LOCK (connection);
1366
1367 if (!_dbus_transport_set_connection (transport, connection))
1368 {
1369 CONNECTION_UNLOCK (connection);
1370
1371 goto error;
1372 }
1373
1374 _dbus_transport_ref (transport);
1375
1376 CONNECTION_UNLOCK (connection);
1377
1378 _dbus_connection_trace_ref (connection, 0, 1, "new_for_transport");
1379 return connection;
1380
1381 error:
1382 if (disconnect_message != NULL)
1383 dbus_message_unref (disconnect_message);
1384
1385 if (disconnect_link != NULL)
1386 _dbus_list_free_link (disconnect_link);
1387
1388 if (connection != NULL)
1389 {
1392 _dbus_rmutex_free_at_location (&connection->mutex);
1396 dbus_free (connection);
1397 }
1398 if (pending_replies)
1399 _dbus_hash_table_unref (pending_replies);
1400
1401 if (watch_list)
1402 _dbus_watch_list_free (watch_list);
1403
1404 if (timeout_list)
1405 _dbus_timeout_list_free (timeout_list);
1406
1407 if (outgoing_counter)
1408 _dbus_counter_unref (outgoing_counter);
1409
1410 if (objects)
1411 _dbus_object_tree_unref (objects);
1412
1413 return NULL;
1414}
1415
1425{
1426 dbus_int32_t old_refcount;
1427
1428 _dbus_assert (connection != NULL);
1429 _dbus_assert (connection->generation == _dbus_current_generation);
1430
1431 HAVE_LOCK_CHECK (connection);
1432
1433 old_refcount = _dbus_atomic_inc (&connection->refcount);
1434 _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
1435 "ref_unlocked");
1436
1437 return connection;
1438}
1439
1446void
1448{
1449 dbus_int32_t old_refcount;
1450
1451 HAVE_LOCK_CHECK (connection);
1452
1453 _dbus_assert (connection != NULL);
1454
1455 old_refcount = _dbus_atomic_dec (&connection->refcount);
1456
1457 _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
1458 "unref_unlocked");
1459
1460 if (old_refcount == 1)
1461 _dbus_connection_last_unref (connection);
1462}
1463
1464static dbus_uint32_t
1465_dbus_connection_get_next_client_serial (DBusConnection *connection)
1466{
1467 dbus_uint32_t serial;
1468
1469 serial = connection->client_serial++;
1470
1471 if (connection->client_serial == 0)
1472 connection->client_serial = 1;
1473
1474 return serial;
1475}
1476
1492 unsigned int condition,
1493 void *data)
1494{
1495 DBusConnection *connection;
1496 dbus_bool_t retval;
1497 DBusDispatchStatus status;
1498
1499 connection = data;
1500
1501 _dbus_verbose ("start\n");
1502
1503 CONNECTION_LOCK (connection);
1504
1505 if (!_dbus_connection_acquire_io_path (connection, 1))
1506 {
1507 /* another thread is handling the message */
1508 CONNECTION_UNLOCK (connection);
1509 return TRUE;
1510 }
1511
1512 HAVE_LOCK_CHECK (connection);
1513 retval = _dbus_transport_handle_watch (connection->transport,
1514 watch, condition);
1515
1516 _dbus_connection_release_io_path (connection);
1517
1518 HAVE_LOCK_CHECK (connection);
1519
1520 _dbus_verbose ("middle\n");
1521
1522 status = _dbus_connection_get_dispatch_status_unlocked (connection);
1523
1524 /* this calls out to user code */
1525 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
1526
1527 _dbus_verbose ("end\n");
1528
1529 return retval;
1530}
1531
1532/* Protected by _DBUS_LOCK (shared_connections) */
1533static DBusHashTable *shared_connections = NULL;
1534static DBusList *shared_connections_no_guid = NULL;
1535
1536static void
1537close_connection_on_shutdown (DBusConnection *connection)
1538{
1539 DBusMessage *message;
1540
1541 dbus_connection_ref (connection);
1543
1544 /* Churn through to the Disconnected message */
1545 while ((message = dbus_connection_pop_message (connection)))
1546 {
1547 dbus_message_unref (message);
1548 }
1549 dbus_connection_unref (connection);
1550}
1551
1552static void
1553shared_connections_shutdown (void *data)
1554{
1555 int n_entries;
1556
1557 if (!_DBUS_LOCK (shared_connections))
1558 {
1559 /* We'd have initialized locks before adding anything, so there
1560 * can't be anything there. */
1561 return;
1562 }
1563
1564 /* This is a little bit unpleasant... better ideas? */
1565 while ((n_entries = _dbus_hash_table_get_n_entries (shared_connections)) > 0)
1566 {
1567 DBusConnection *connection;
1568 DBusHashIter iter;
1569
1570 _dbus_hash_iter_init (shared_connections, &iter);
1571 _dbus_hash_iter_next (&iter);
1572
1573 connection = _dbus_hash_iter_get_value (&iter);
1574
1575 _DBUS_UNLOCK (shared_connections);
1576 close_connection_on_shutdown (connection);
1577 if (!_DBUS_LOCK (shared_connections))
1578 _dbus_assert_not_reached ("global locks were already initialized");
1579
1580 /* The connection should now be dead and not in our hash ... */
1581 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) < n_entries);
1582 }
1583
1584 _dbus_assert (_dbus_hash_table_get_n_entries (shared_connections) == 0);
1585
1586 _dbus_hash_table_unref (shared_connections);
1587 shared_connections = NULL;
1588
1589 if (shared_connections_no_guid != NULL)
1590 {
1591 DBusConnection *connection;
1592 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1593 while (connection != NULL)
1594 {
1595 _DBUS_UNLOCK (shared_connections);
1596 close_connection_on_shutdown (connection);
1597 if (!_DBUS_LOCK (shared_connections))
1598 _dbus_assert_not_reached ("global locks were already initialized");
1599 connection = _dbus_list_pop_first (&shared_connections_no_guid);
1600 }
1601 }
1602
1603 shared_connections_no_guid = NULL;
1604
1605 _DBUS_UNLOCK (shared_connections);
1606}
1607
1608static dbus_bool_t
1609connection_lookup_shared (DBusAddressEntry *entry,
1610 DBusConnection **result)
1611{
1612 _dbus_verbose ("checking for existing connection\n");
1613
1614 *result = NULL;
1615
1616 if (!_DBUS_LOCK (shared_connections))
1617 {
1618 /* If it was shared, we'd have initialized global locks when we put
1619 * it in shared_connections. */
1620 return FALSE;
1621 }
1622
1623 if (shared_connections == NULL)
1624 {
1625 _dbus_verbose ("creating shared_connections hash table\n");
1626
1627 shared_connections = _dbus_hash_table_new (DBUS_HASH_STRING,
1628 dbus_free,
1629 NULL);
1630 if (shared_connections == NULL)
1631 {
1632 _DBUS_UNLOCK (shared_connections);
1633 return FALSE;
1634 }
1635
1636 if (!_dbus_register_shutdown_func (shared_connections_shutdown, NULL))
1637 {
1638 _dbus_hash_table_unref (shared_connections);
1639 shared_connections = NULL;
1640 _DBUS_UNLOCK (shared_connections);
1641 return FALSE;
1642 }
1643
1644 _dbus_verbose (" successfully created shared_connections\n");
1645
1646 _DBUS_UNLOCK (shared_connections);
1647 return TRUE; /* no point looking up in the hash we just made */
1648 }
1649 else
1650 {
1651 const char *guid;
1652
1653 guid = dbus_address_entry_get_value (entry, "guid");
1654
1655 if (guid != NULL)
1656 {
1657 DBusConnection *connection;
1658
1659 connection = _dbus_hash_table_lookup_string (shared_connections,
1660 guid);
1661
1662 if (connection)
1663 {
1664 /* The DBusConnection can't be finalized without taking
1665 * the shared_connections lock to remove it from the
1666 * hash. So it's safe to ref the connection here.
1667 * However, it may be disconnected if the Disconnected
1668 * message hasn't been processed yet, in which case we
1669 * want to pretend it isn't in the hash and avoid
1670 * returning it.
1671 *
1672 * The idea is to avoid ever returning a disconnected connection
1673 * from dbus_connection_open(). We could just synchronously
1674 * drop our shared ref to the connection on connection disconnect,
1675 * and then assert here that the connection is connected, but
1676 * that causes reentrancy headaches.
1677 */
1678 CONNECTION_LOCK (connection);
1679 if (_dbus_connection_get_is_connected_unlocked (connection))
1680 {
1681 _dbus_connection_ref_unlocked (connection);
1682 *result = connection;
1683 _dbus_verbose ("looked up existing connection to server guid %s\n",
1684 guid);
1685 }
1686 else
1687 {
1688 _dbus_verbose ("looked up existing connection to server guid %s but it was disconnected so ignoring it\n",
1689 guid);
1690 }
1691 CONNECTION_UNLOCK (connection);
1692 }
1693 }
1694
1695 _DBUS_UNLOCK (shared_connections);
1696 return TRUE;
1697 }
1698}
1699
1700static dbus_bool_t
1701connection_record_shared_unlocked (DBusConnection *connection,
1702 const char *guid)
1703{
1704 char *guid_key;
1705 char *guid_in_connection;
1706
1707 HAVE_LOCK_CHECK (connection);
1708 _dbus_assert (connection->server_guid == NULL);
1709 _dbus_assert (connection->shareable);
1710
1711 /* get a hard ref on this connection, even if
1712 * we won't in fact store it in the hash, we still
1713 * need to hold a ref on it until it's disconnected.
1714 */
1715 _dbus_connection_ref_unlocked (connection);
1716
1717 if (guid == NULL)
1718 {
1719 if (!_DBUS_LOCK (shared_connections))
1720 return FALSE;
1721
1722 if (!_dbus_list_prepend (&shared_connections_no_guid, connection))
1723 {
1724 _DBUS_UNLOCK (shared_connections);
1725 return FALSE;
1726 }
1727
1728 _DBUS_UNLOCK (shared_connections);
1729 return TRUE; /* don't store in the hash */
1730 }
1731
1732 /* A separate copy of the key is required in the hash table, because
1733 * we don't have a lock on the connection when we are doing a hash
1734 * lookup.
1735 */
1736
1737 guid_key = _dbus_strdup (guid);
1738 if (guid_key == NULL)
1739 return FALSE;
1740
1741 guid_in_connection = _dbus_strdup (guid);
1742 if (guid_in_connection == NULL)
1743 {
1744 dbus_free (guid_key);
1745 return FALSE;
1746 }
1747
1748 if (!_DBUS_LOCK (shared_connections))
1749 {
1750 dbus_free (guid_in_connection);
1751 dbus_free (guid_key);
1752 return FALSE;
1753 }
1754
1755 _dbus_assert (shared_connections != NULL);
1756
1757 if (!_dbus_hash_table_insert_string (shared_connections,
1758 guid_key, connection))
1759 {
1760 dbus_free (guid_key);
1761 dbus_free (guid_in_connection);
1762 _DBUS_UNLOCK (shared_connections);
1763 return FALSE;
1764 }
1765
1766 connection->server_guid = guid_in_connection;
1767
1768 _dbus_verbose ("stored connection to %s to be shared\n",
1769 connection->server_guid);
1770
1771 _DBUS_UNLOCK (shared_connections);
1772
1773 _dbus_assert (connection->server_guid != NULL);
1774
1775 return TRUE;
1776}
1777
1778static void
1779connection_forget_shared_unlocked (DBusConnection *connection)
1780{
1781 HAVE_LOCK_CHECK (connection);
1782
1783 if (!connection->shareable)
1784 return;
1785
1786 if (!_DBUS_LOCK (shared_connections))
1787 {
1788 /* If it was shared, we'd have initialized global locks when we put
1789 * it in the table; so it can't be there. */
1790 return;
1791 }
1792
1793 if (connection->server_guid != NULL)
1794 {
1795 _dbus_verbose ("dropping connection to %s out of the shared table\n",
1796 connection->server_guid);
1797
1798 if (!_dbus_hash_table_remove_string (shared_connections,
1799 connection->server_guid))
1800 _dbus_assert_not_reached ("connection was not in the shared table");
1801
1802 dbus_free (connection->server_guid);
1803 connection->server_guid = NULL;
1804 }
1805 else
1806 {
1807 _dbus_list_remove (&shared_connections_no_guid, connection);
1808 }
1809
1810 _DBUS_UNLOCK (shared_connections);
1811
1812 /* remove our reference held on all shareable connections */
1814}
1815
1816static DBusConnection*
1817connection_try_from_address_entry (DBusAddressEntry *entry,
1818 DBusError *error)
1819{
1820 DBusTransport *transport;
1821 DBusConnection *connection;
1822
1823 transport = _dbus_transport_open (entry, error);
1824
1825 if (transport == NULL)
1826 {
1827 _DBUS_ASSERT_ERROR_IS_SET (error);
1828 return NULL;
1829 }
1830
1831 connection = _dbus_connection_new_for_transport (transport);
1832
1833 _dbus_transport_unref (transport);
1834
1835 if (connection == NULL)
1836 {
1837 _DBUS_SET_OOM (error);
1838 return NULL;
1839 }
1840
1841#ifndef DBUS_DISABLE_CHECKS
1842 _dbus_assert (!connection->have_connection_lock);
1843#endif
1844 return connection;
1845}
1846
1847/*
1848 * If the shared parameter is true, then any existing connection will
1849 * be used (and if a new connection is created, it will be available
1850 * for use by others). If the shared parameter is false, a new
1851 * connection will always be created, and the new connection will
1852 * never be returned to other callers.
1853 *
1854 * @param address the address
1855 * @param shared whether the connection is shared or private
1856 * @param error error return
1857 * @returns the connection or #NULL on error
1858 */
1859static DBusConnection*
1860_dbus_connection_open_internal (const char *address,
1861 dbus_bool_t shared,
1862 DBusError *error)
1863{
1864 DBusConnection *connection;
1865 DBusAddressEntry **entries;
1866 DBusError tmp_error = DBUS_ERROR_INIT;
1867 DBusError first_error = DBUS_ERROR_INIT;
1868 int len, i;
1869
1870 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1871
1872 _dbus_verbose ("opening %s connection to: %s\n",
1873 shared ? "shared" : "private", address);
1874
1875 if (!dbus_parse_address (address, &entries, &len, error))
1876 return NULL;
1877
1878 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1879
1880 connection = NULL;
1881
1882 for (i = 0; i < len; i++)
1883 {
1884 if (shared)
1885 {
1886 if (!connection_lookup_shared (entries[i], &connection))
1887 _DBUS_SET_OOM (&tmp_error);
1888 }
1889
1890 if (connection == NULL)
1891 {
1892 connection = connection_try_from_address_entry (entries[i],
1893 &tmp_error);
1894
1895 if (connection != NULL && shared)
1896 {
1897 const char *guid;
1898
1899 connection->shareable = TRUE;
1900
1901 /* guid may be NULL */
1902 guid = dbus_address_entry_get_value (entries[i], "guid");
1903
1904 CONNECTION_LOCK (connection);
1905
1906 if (!connection_record_shared_unlocked (connection, guid))
1907 {
1908 _DBUS_SET_OOM (&tmp_error);
1909 _dbus_connection_close_possibly_shared_and_unlock (connection);
1910 dbus_connection_unref (connection);
1911 connection = NULL;
1912 }
1913 else
1914 CONNECTION_UNLOCK (connection);
1915 }
1916 }
1917
1918 if (connection)
1919 break;
1920
1921 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
1922
1923 if (i == 0)
1924 dbus_move_error (&tmp_error, &first_error);
1925 else
1926 dbus_error_free (&tmp_error);
1927 }
1928
1929 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1930 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
1931
1932 if (connection == NULL)
1933 {
1934 _DBUS_ASSERT_ERROR_IS_SET (&first_error);
1935 dbus_move_error (&first_error, error);
1936 }
1937 else
1938 dbus_error_free (&first_error);
1939
1940 dbus_address_entries_free (entries);
1941 return connection;
1942}
1943
1952void
1954{
1955 _dbus_assert (connection != NULL);
1956 _dbus_assert (connection->generation == _dbus_current_generation);
1957
1958 CONNECTION_LOCK (connection);
1959 _dbus_connection_close_possibly_shared_and_unlock (connection);
1960}
1961
1963_dbus_connection_preallocate_send_unlocked (DBusConnection *connection)
1964{
1965 DBusPreallocatedSend *preallocated;
1966
1967 HAVE_LOCK_CHECK (connection);
1968
1969 _dbus_assert (connection != NULL);
1970
1971 preallocated = dbus_new (DBusPreallocatedSend, 1);
1972 if (preallocated == NULL)
1973 return NULL;
1974
1975 preallocated->queue_link = _dbus_list_alloc_link (NULL);
1976 if (preallocated->queue_link == NULL)
1977 goto failed_0;
1978
1979 preallocated->counter_link = _dbus_list_alloc_link (connection->outgoing_counter);
1980 if (preallocated->counter_link == NULL)
1981 goto failed_1;
1982
1983 _dbus_counter_ref (preallocated->counter_link->data);
1984
1985 preallocated->connection = connection;
1986
1987 return preallocated;
1988
1989 failed_1:
1990 _dbus_list_free_link (preallocated->queue_link);
1991 failed_0:
1992 dbus_free (preallocated);
1993
1994 return NULL;
1995}
1996
1997/* Called with lock held, does not update dispatch status */
1998static void
1999_dbus_connection_send_preallocated_unlocked_no_update (DBusConnection *connection,
2000 DBusPreallocatedSend *preallocated,
2001 DBusMessage *message,
2002 dbus_uint32_t *client_serial)
2003{
2004 dbus_uint32_t serial;
2005
2006 preallocated->queue_link->data = message;
2008 preallocated->queue_link);
2009
2010 /* It's OK that we'll never call the notify function, because for the
2011 * outgoing limit, there isn't one */
2013 preallocated->counter_link);
2014
2015 dbus_free (preallocated);
2016 preallocated = NULL;
2017
2018 dbus_message_ref (message);
2019
2020 connection->n_outgoing += 1;
2021
2022 _dbus_verbose ("Message %p (%s %s %s %s '%s') for %s added to outgoing queue %p, %d pending to send\n",
2023 message,
2025 dbus_message_get_path (message) ?
2026 dbus_message_get_path (message) :
2027 "no path",
2028 dbus_message_get_interface (message) ?
2029 dbus_message_get_interface (message) :
2030 "no interface",
2031 dbus_message_get_member (message) ?
2032 dbus_message_get_member (message) :
2033 "no member",
2037 "null",
2038 connection,
2039 connection->n_outgoing);
2040
2041 if (dbus_message_get_serial (message) == 0)
2042 {
2043 serial = _dbus_connection_get_next_client_serial (connection);
2044 dbus_message_set_serial (message, serial);
2045 if (client_serial)
2046 *client_serial = serial;
2047 }
2048 else
2049 {
2050 if (client_serial)
2051 *client_serial = dbus_message_get_serial (message);
2052 }
2053
2054 _dbus_verbose ("Message %p serial is %u\n",
2055 message, dbus_message_get_serial (message));
2056
2057 dbus_message_lock (message);
2058
2059 /* Now we need to run an iteration to hopefully just write the messages
2060 * out immediately, and otherwise get them queued up
2061 */
2063 NULL,
2064 DBUS_ITERATION_DO_WRITING,
2065 -1);
2066
2067 /* If stuff is still queued up, be sure we wake up the main loop */
2068 if (connection->n_outgoing > 0)
2069 _dbus_connection_wakeup_mainloop (connection);
2070}
2071
2072static void
2073_dbus_connection_send_preallocated_and_unlock (DBusConnection *connection,
2074 DBusPreallocatedSend *preallocated,
2075 DBusMessage *message,
2076 dbus_uint32_t *client_serial)
2077{
2078 DBusDispatchStatus status;
2079
2080 HAVE_LOCK_CHECK (connection);
2081
2082 _dbus_connection_send_preallocated_unlocked_no_update (connection,
2083 preallocated,
2084 message, client_serial);
2085
2086 _dbus_verbose ("middle\n");
2087 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2088
2089 /* this calls out to user code */
2090 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2091}
2092
2104 DBusMessage *message,
2105 dbus_uint32_t *client_serial)
2106{
2107 DBusPreallocatedSend *preallocated;
2108
2109 _dbus_assert (connection != NULL);
2110 _dbus_assert (message != NULL);
2111
2112 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
2113 if (preallocated == NULL)
2114 {
2115 CONNECTION_UNLOCK (connection);
2116 return FALSE;
2117 }
2118
2119 _dbus_connection_send_preallocated_and_unlock (connection,
2120 preallocated,
2121 message,
2122 client_serial);
2123 return TRUE;
2124}
2125
2150void
2152{
2153 dbus_int32_t refcount;
2154
2155 CONNECTION_LOCK (connection);
2156
2157 refcount = _dbus_atomic_get (&connection->refcount);
2158 /* The caller should have at least one ref */
2159 _dbus_assert (refcount >= 1);
2160
2161 if (refcount == 1)
2162 _dbus_connection_close_possibly_shared_and_unlock (connection);
2163 else
2164 CONNECTION_UNLOCK (connection);
2165}
2166
2167
2177static void
2178_dbus_memory_pause_based_on_timeout (int timeout_milliseconds)
2179{
2180 if (timeout_milliseconds == -1)
2182 else if (timeout_milliseconds < 100)
2183 ; /* just busy loop */
2184 else if (timeout_milliseconds <= 1000)
2185 _dbus_sleep_milliseconds (timeout_milliseconds / 3);
2186 else
2188}
2189
2190static DBusMessage *
2191generate_local_error_message (dbus_uint32_t serial,
2192 const char *error_name,
2193 const char *error_msg)
2194{
2195 DBusMessage *message;
2197 if (!message)
2198 goto out;
2199
2200 if (!dbus_message_set_error_name (message, error_name))
2201 {
2202 dbus_message_unref (message);
2203 message = NULL;
2204 goto out;
2205 }
2206
2207 dbus_message_set_no_reply (message, TRUE);
2208
2209 if (!dbus_message_set_reply_serial (message,
2210 serial))
2211 {
2212 dbus_message_unref (message);
2213 message = NULL;
2214 goto out;
2215 }
2216
2217 if (error_msg != NULL)
2218 {
2219 DBusMessageIter iter;
2220
2221 dbus_message_iter_init_append (message, &iter);
2224 &error_msg))
2225 {
2226 dbus_message_unref (message);
2227 message = NULL;
2228 goto out;
2229 }
2230 }
2231
2232 out:
2233 return message;
2234}
2235
2236/*
2237 * Peek the incoming queue to see if we got reply for a specific serial
2238 */
2239static dbus_bool_t
2240_dbus_connection_peek_for_reply_unlocked (DBusConnection *connection,
2241 dbus_uint32_t client_serial)
2242{
2243 DBusList *link;
2244 HAVE_LOCK_CHECK (connection);
2245
2246 link = _dbus_list_get_first_link (&connection->incoming_messages);
2247
2248 while (link != NULL)
2249 {
2250 DBusMessage *reply = link->data;
2251
2252 if (dbus_message_get_reply_serial (reply) == client_serial)
2253 {
2254 _dbus_verbose ("%s reply to %d found in queue\n", _DBUS_FUNCTION_NAME, client_serial);
2255 return TRUE;
2256 }
2257 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2258 }
2259
2260 return FALSE;
2261}
2262
2263/* This is slightly strange since we can pop a message here without
2264 * the dispatch lock.
2265 */
2266static DBusMessage*
2267check_for_reply_unlocked (DBusConnection *connection,
2268 dbus_uint32_t client_serial)
2269{
2270 DBusList *link;
2271
2272 HAVE_LOCK_CHECK (connection);
2273
2274 link = _dbus_list_get_first_link (&connection->incoming_messages);
2275
2276 while (link != NULL)
2277 {
2278 DBusMessage *reply = link->data;
2279
2280 if (dbus_message_get_reply_serial (reply) == client_serial)
2281 {
2282 _dbus_list_remove_link (&connection->incoming_messages, link);
2283 connection->n_incoming -= 1;
2284 return reply;
2285 }
2286 link = _dbus_list_get_next_link (&connection->incoming_messages, link);
2287 }
2288
2289 return NULL;
2290}
2291
2292static void
2293connection_timeout_and_complete_all_pending_calls_unlocked (DBusConnection *connection)
2294{
2295 /* We can't iterate over the hash in the normal way since we'll be
2296 * dropping the lock for each item. So we restart the
2297 * iter each time as we drain the hash table.
2298 */
2299
2300 while (_dbus_hash_table_get_n_entries (connection->pending_replies) > 0)
2301 {
2302 DBusPendingCall *pending;
2303 DBusHashIter iter;
2304
2305 _dbus_hash_iter_init (connection->pending_replies, &iter);
2306 _dbus_hash_iter_next (&iter);
2307
2308 pending = _dbus_hash_iter_get_value (&iter);
2310
2312 connection);
2313
2319
2321 CONNECTION_LOCK (connection);
2322 }
2323 HAVE_LOCK_CHECK (connection);
2324}
2325
2326static void
2327complete_pending_call_and_unlock (DBusConnection *connection,
2328 DBusPendingCall *pending,
2329 DBusMessage *message)
2330{
2331 _dbus_pending_call_set_reply_unlocked (pending, message);
2332 _dbus_pending_call_ref_unlocked (pending); /* in case there's no app with a ref held */
2334 _dbus_connection_detach_pending_call_and_unlock (connection, pending);
2335
2336 /* Must be called unlocked since it invokes app callback */
2338 dbus_pending_call_unref (pending);
2339}
2340
2341static dbus_bool_t
2342check_for_reply_and_update_dispatch_unlocked (DBusConnection *connection,
2343 DBusPendingCall *pending)
2344{
2345 DBusMessage *reply;
2346 DBusDispatchStatus status;
2347
2348 reply = check_for_reply_unlocked (connection,
2350 if (reply != NULL)
2351 {
2352 _dbus_verbose ("checked for reply\n");
2353
2354 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): got reply\n");
2355
2356 complete_pending_call_and_unlock (connection, pending, reply);
2357 dbus_message_unref (reply);
2358
2359 CONNECTION_LOCK (connection);
2360 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2361 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2362 dbus_pending_call_unref (pending);
2363
2364 return TRUE;
2365 }
2366
2367 return FALSE;
2368}
2369
2384void
2386{
2387 long start_tv_sec, start_tv_usec;
2388 long tv_sec, tv_usec;
2389 DBusDispatchStatus status;
2390 DBusConnection *connection;
2391 dbus_uint32_t client_serial;
2392 DBusTimeout *timeout;
2393 int timeout_milliseconds, elapsed_milliseconds;
2394
2395 _dbus_assert (pending != NULL);
2396
2397 if (dbus_pending_call_get_completed (pending))
2398 return;
2399
2400 dbus_pending_call_ref (pending); /* necessary because the call could be canceled */
2401
2402 connection = _dbus_pending_call_get_connection_and_lock (pending);
2403
2404 /* Flush message queue - note, can affect dispatch status */
2405 _dbus_connection_flush_unlocked (connection);
2406
2407 client_serial = _dbus_pending_call_get_reply_serial_unlocked (pending);
2408
2409 /* note that timeout_milliseconds is limited to a smallish value
2410 * in _dbus_pending_call_new() so overflows aren't possible
2411 * below
2412 */
2413 timeout = _dbus_pending_call_get_timeout_unlocked (pending);
2414 _dbus_get_monotonic_time (&start_tv_sec, &start_tv_usec);
2415 if (timeout)
2416 {
2417 timeout_milliseconds = dbus_timeout_get_interval (timeout);
2418
2419 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block %d milliseconds for reply serial %u from %ld sec %ld usec\n",
2420 timeout_milliseconds,
2421 client_serial,
2422 start_tv_sec, start_tv_usec);
2423 }
2424 else
2425 {
2426 timeout_milliseconds = -1;
2427
2428 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): will block for reply serial %u\n", client_serial);
2429 }
2430
2431 /* check to see if we already got the data off the socket */
2432 /* from another blocked pending call */
2433 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2434 return;
2435
2436 /* Now we wait... */
2437 /* always block at least once as we know we don't have the reply yet */
2439 pending,
2440 DBUS_ITERATION_DO_READING |
2441 DBUS_ITERATION_BLOCK,
2442 timeout_milliseconds);
2443
2444 recheck_status:
2445
2446 _dbus_verbose ("top of recheck\n");
2447
2448 HAVE_LOCK_CHECK (connection);
2449
2450 /* queue messages and get status */
2451
2452 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2453
2454 /* the get_completed() is in case a dispatch() while we were blocking
2455 * got the reply instead of us.
2456 */
2458 {
2459 _dbus_verbose ("Pending call completed by dispatch\n");
2460 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2461 dbus_pending_call_unref (pending);
2462 return;
2463 }
2464
2465 if (status == DBUS_DISPATCH_DATA_REMAINS)
2466 {
2467 if (check_for_reply_and_update_dispatch_unlocked (connection, pending))
2468 return;
2469 }
2470
2471 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
2472 elapsed_milliseconds = (tv_sec - start_tv_sec) * 1000 +
2473 (tv_usec - start_tv_usec) / 1000;
2474
2475 if (!_dbus_connection_get_is_connected_unlocked (connection))
2476 {
2477 DBusMessage *error_msg;
2478
2479 error_msg = generate_local_error_message (client_serial,
2481 "Connection was disconnected before a reply was received");
2482
2483 /* on OOM error_msg is set to NULL */
2484 complete_pending_call_and_unlock (connection, pending, error_msg);
2485 if (error_msg != NULL)
2486 dbus_message_unref (error_msg);
2487 dbus_pending_call_unref (pending);
2488 return;
2489 }
2490 else if (connection->disconnect_message_link == NULL)
2491 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): disconnected\n");
2492 else if (timeout == NULL)
2493 {
2494 if (status == DBUS_DISPATCH_NEED_MEMORY)
2495 {
2496 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2497 * we may already have a reply in the buffer and just can't process
2498 * it.
2499 */
2500 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2501
2502 _dbus_memory_pause_based_on_timeout (timeout_milliseconds - elapsed_milliseconds);
2503 }
2504 else
2505 {
2506 /* block again, we don't have the reply buffered yet. */
2508 pending,
2509 DBUS_ITERATION_DO_READING |
2510 DBUS_ITERATION_BLOCK,
2511 timeout_milliseconds - elapsed_milliseconds);
2512 }
2513
2514 goto recheck_status;
2515 }
2516 else if (tv_sec < start_tv_sec)
2517 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): clock set backward\n");
2518 else if (elapsed_milliseconds < timeout_milliseconds)
2519 {
2520 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): %d milliseconds remain\n", timeout_milliseconds - elapsed_milliseconds);
2521
2522 if (status == DBUS_DISPATCH_NEED_MEMORY)
2523 {
2524 /* Try sleeping a bit, as we aren't sure we need to block for reading,
2525 * we may already have a reply in the buffer and just can't process
2526 * it.
2527 */
2528 _dbus_verbose ("dbus_connection_send_with_reply_and_block() waiting for more memory\n");
2529
2530 _dbus_memory_pause_based_on_timeout (timeout_milliseconds - elapsed_milliseconds);
2531 }
2532 else
2533 {
2534 /* block again, we don't have the reply buffered yet. */
2536 pending,
2537 DBUS_ITERATION_DO_READING |
2538 DBUS_ITERATION_BLOCK,
2539 timeout_milliseconds - elapsed_milliseconds);
2540 }
2541
2542 goto recheck_status;
2543 }
2544
2545 _dbus_verbose ("dbus_connection_send_with_reply_and_block(): Waited %d milliseconds and got no reply\n",
2546 elapsed_milliseconds);
2547
2549
2550 /* unlock and call user code */
2551 complete_pending_call_and_unlock (connection, pending, NULL);
2552
2553 /* update user code on dispatch status */
2554 CONNECTION_LOCK (connection);
2555 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2556 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2557 dbus_pending_call_unref (pending);
2558}
2559
2565int
2567{
2569}
2570
2578void
2580 DBusPendingFdsChangeFunction callback,
2581 void *data)
2582{
2584 callback, data);
2585}
2586
2624dbus_connection_open (const char *address,
2625 DBusError *error)
2626{
2627 DBusConnection *connection;
2628
2629 _dbus_return_val_if_fail (address != NULL, NULL);
2630 _dbus_return_val_if_error_is_set (error, NULL);
2631
2632 connection = _dbus_connection_open_internal (address,
2633 TRUE,
2634 error);
2635
2636 return connection;
2637}
2638
2667dbus_connection_open_private (const char *address,
2668 DBusError *error)
2669{
2670 DBusConnection *connection;
2671
2672 _dbus_return_val_if_fail (address != NULL, NULL);
2673 _dbus_return_val_if_error_is_set (error, NULL);
2674
2675 connection = _dbus_connection_open_internal (address,
2676 FALSE,
2677 error);
2678
2679 return connection;
2680}
2681
2690{
2691 dbus_int32_t old_refcount;
2692
2693 _dbus_return_val_if_fail (connection != NULL, NULL);
2694 _dbus_return_val_if_fail (connection->generation == _dbus_current_generation, NULL);
2695 old_refcount = _dbus_atomic_inc (&connection->refcount);
2696 _dbus_connection_trace_ref (connection, old_refcount, old_refcount + 1,
2697 "ref");
2698
2699 return connection;
2700}
2701
2702static void
2703free_outgoing_message (void *element,
2704 void *data)
2705{
2706 DBusMessage *message = element;
2707 DBusConnection *connection = data;
2708
2709 _dbus_message_remove_counter (message, connection->outgoing_counter);
2710 dbus_message_unref (message);
2711}
2712
2713/* This is run without the mutex held, but after the last reference
2714 * to the connection has been dropped we should have no thread-related
2715 * problems
2716 */
2717static void
2718_dbus_connection_last_unref (DBusConnection *connection)
2719{
2720 DBusList *link;
2721
2722 _dbus_verbose ("Finalizing connection %p\n", connection);
2723
2724 _dbus_assert (_dbus_atomic_get (&connection->refcount) == 0);
2725
2726 /* You have to disconnect the connection before unref:ing it. Otherwise
2727 * you won't get the disconnected message.
2728 */
2730 _dbus_assert (connection->server_guid == NULL);
2731
2732 /* ---- We're going to call various application callbacks here, hope it doesn't break anything... */
2734
2739
2740 _dbus_watch_list_free (connection->watches);
2741 connection->watches = NULL;
2742
2743 _dbus_timeout_list_free (connection->timeouts);
2744 connection->timeouts = NULL;
2745
2746 _dbus_data_slot_list_free (&connection->slot_list);
2747
2748 link = _dbus_list_get_first_link (&connection->filter_list);
2749 while (link != NULL)
2750 {
2751 DBusMessageFilter *filter = link->data;
2752 DBusList *next = _dbus_list_get_next_link (&connection->filter_list, link);
2753
2754 filter->function = NULL;
2755 _dbus_message_filter_unref (filter); /* calls app callback */
2756 link->data = NULL;
2757
2758 link = next;
2759 }
2760 _dbus_list_clear (&connection->filter_list);
2761
2762 /* ---- Done with stuff that invokes application callbacks */
2763
2764 _dbus_object_tree_unref (connection->objects);
2765
2767 connection->pending_replies = NULL;
2768
2770 free_outgoing_message,
2771 connection);
2772 _dbus_list_clear (&connection->outgoing_messages);
2773
2776
2778
2779 _dbus_transport_unref (connection->transport);
2780
2781 if (connection->disconnect_message_link)
2782 {
2783 DBusMessage *message = connection->disconnect_message_link->data;
2784 dbus_message_unref (message);
2786 }
2787
2790
2793
2795
2796 _dbus_rmutex_free_at_location (&connection->mutex);
2797
2798 dbus_free (connection);
2799}
2800
2820void
2822{
2823 dbus_int32_t old_refcount;
2824
2825 _dbus_return_if_fail (connection != NULL);
2826 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2827
2828 old_refcount = _dbus_atomic_dec (&connection->refcount);
2829
2830 _dbus_connection_trace_ref (connection, old_refcount, old_refcount - 1,
2831 "unref");
2832
2833 if (old_refcount == 1)
2834 {
2835#ifndef DBUS_DISABLE_CHECKS
2837 {
2838 _dbus_warn_check_failed ("The last reference on a connection was dropped without closing the connection. This is a bug in an application. See dbus_connection_unref() documentation for details.\n%s",
2839 connection->shareable ?
2840 "Most likely, the application called unref() too many times and removed a reference belonging to libdbus, since this is a shared connection." :
2841 "Most likely, the application was supposed to call dbus_connection_close(), since this is a private connection.");
2842 return;
2843 }
2844#endif
2845 _dbus_connection_last_unref (connection);
2846 }
2847}
2848
2849/*
2850 * Note that the transport can disconnect itself (other end drops us)
2851 * and in that case this function never runs. So this function must
2852 * not do anything more than disconnect the transport and update the
2853 * dispatch status.
2854 *
2855 * If the transport self-disconnects, then we assume someone will
2856 * dispatch the connection to cause the dispatch status update.
2857 */
2858static void
2859_dbus_connection_close_possibly_shared_and_unlock (DBusConnection *connection)
2860{
2861 DBusDispatchStatus status;
2862
2863 HAVE_LOCK_CHECK (connection);
2864
2865 _dbus_verbose ("Disconnecting %p\n", connection);
2866
2867 /* We need to ref because update_dispatch_status_and_unlock will unref
2868 * the connection if it was shared and libdbus was the only remaining
2869 * refcount holder.
2870 */
2871 _dbus_connection_ref_unlocked (connection);
2872
2874
2875 /* This has the side effect of queuing the disconnect message link
2876 * (unless we don't have enough memory, possibly, so don't assert it).
2877 * After the disconnect message link is queued, dbus_bus_get/dbus_connection_open
2878 * should never again return the newly-disconnected connection.
2879 *
2880 * However, we only unref the shared connection and exit_on_disconnect when
2881 * the disconnect message reaches the head of the message queue,
2882 * NOT when it's first queued.
2883 */
2884 status = _dbus_connection_get_dispatch_status_unlocked (connection);
2885
2886 /* This calls out to user code */
2887 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
2888
2889 /* Could also call out to user code */
2890 dbus_connection_unref (connection);
2891}
2892
2935void
2937{
2938 _dbus_return_if_fail (connection != NULL);
2939 _dbus_return_if_fail (connection->generation == _dbus_current_generation);
2940
2941 CONNECTION_LOCK (connection);
2942
2943#ifndef DBUS_DISABLE_CHECKS
2944 if (connection->shareable)
2945 {
2946 CONNECTION_UNLOCK (connection);
2947
2948 _dbus_warn_check_failed ("Applications must not close shared connections - see dbus_connection_close() docs. This is a bug in the application.");
2949 return;
2950 }
2951#endif
2952
2953 _dbus_connection_close_possibly_shared_and_unlock (connection);
2954}
2955
2956static dbus_bool_t
2957_dbus_connection_get_is_connected_unlocked (DBusConnection *connection)
2958{
2959 HAVE_LOCK_CHECK (connection);
2960 return _dbus_transport_get_is_connected (connection->transport);
2961}
2962
2978{
2979 dbus_bool_t res;
2980
2981 _dbus_return_val_if_fail (connection != NULL, FALSE);
2982
2983 CONNECTION_LOCK (connection);
2984 res = _dbus_connection_get_is_connected_unlocked (connection);
2985 CONNECTION_UNLOCK (connection);
2986
2987 return res;
2988}
2989
3000{
3001 dbus_bool_t res;
3002
3003 _dbus_return_val_if_fail (connection != NULL, FALSE);
3004
3005 CONNECTION_LOCK (connection);
3007 CONNECTION_UNLOCK (connection);
3008
3009 return res;
3010}
3011
3034{
3035 dbus_bool_t res;
3036
3037 _dbus_return_val_if_fail (connection != NULL, FALSE);
3038
3039 CONNECTION_LOCK (connection);
3040 res = _dbus_transport_get_is_anonymous (connection->transport);
3041 CONNECTION_UNLOCK (connection);
3042
3043 return res;
3044}
3045
3077char*
3079{
3080 char *id;
3081
3082 _dbus_return_val_if_fail (connection != NULL, NULL);
3083
3084 CONNECTION_LOCK (connection);
3086 CONNECTION_UNLOCK (connection);
3087
3088 return id;
3089}
3090
3110 int type)
3111{
3112 _dbus_return_val_if_fail (connection != NULL, FALSE);
3113
3114 if (!dbus_type_is_valid (type))
3115 return FALSE;
3116
3117 if (type != DBUS_TYPE_UNIX_FD)
3118 return TRUE;
3119
3120#ifdef HAVE_UNIX_FD_PASSING
3121 {
3122 dbus_bool_t b;
3123
3124 CONNECTION_LOCK(connection);
3126 CONNECTION_UNLOCK(connection);
3127
3128 return b;
3129 }
3130#endif
3131
3132 return FALSE;
3133}
3134
3148void
3150 dbus_bool_t exit_on_disconnect)
3151{
3152 _dbus_return_if_fail (connection != NULL);
3153
3154 CONNECTION_LOCK (connection);
3155 connection->exit_on_disconnect = exit_on_disconnect != FALSE;
3156 CONNECTION_UNLOCK (connection);
3157}
3158
3170{
3171 DBusPreallocatedSend *preallocated;
3172
3173 _dbus_return_val_if_fail (connection != NULL, NULL);
3174
3175 CONNECTION_LOCK (connection);
3176
3177 preallocated =
3178 _dbus_connection_preallocate_send_unlocked (connection);
3179
3180 CONNECTION_UNLOCK (connection);
3181
3182 return preallocated;
3183}
3184
3194void
3196 DBusPreallocatedSend *preallocated)
3197{
3198 _dbus_return_if_fail (connection != NULL);
3199 _dbus_return_if_fail (preallocated != NULL);
3200 _dbus_return_if_fail (connection == preallocated->connection);
3201
3202 _dbus_list_free_link (preallocated->queue_link);
3203 _dbus_counter_unref (preallocated->counter_link->data);
3204 _dbus_list_free_link (preallocated->counter_link);
3205 dbus_free (preallocated);
3206}
3207
3220void
3222 DBusPreallocatedSend *preallocated,
3223 DBusMessage *message,
3224 dbus_uint32_t *client_serial)
3225{
3226 _dbus_return_if_fail (connection != NULL);
3227 _dbus_return_if_fail (preallocated != NULL);
3228 _dbus_return_if_fail (message != NULL);
3229 _dbus_return_if_fail (preallocated->connection == connection);
3230 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL ||
3231 dbus_message_get_member (message) != NULL);
3232 _dbus_return_if_fail (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_SIGNAL ||
3233 (dbus_message_get_interface (message) != NULL &&
3234 dbus_message_get_member (message) != NULL));
3235
3236 CONNECTION_LOCK (connection);
3237
3238#ifdef HAVE_UNIX_FD_PASSING
3239
3240 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3241 message->n_unix_fds > 0)
3242 {
3243 /* Refuse to send fds on a connection that cannot handle
3244 them. Unfortunately we cannot return a proper error here, so
3245 the best we can is just return. */
3246 CONNECTION_UNLOCK (connection);
3247 return;
3248 }
3249
3250#endif
3251
3252 _dbus_connection_send_preallocated_and_unlock (connection,
3253 preallocated,
3254 message, client_serial);
3255}
3256
3257static dbus_bool_t
3258_dbus_connection_send_unlocked_no_update (DBusConnection *connection,
3259 DBusMessage *message,
3260 dbus_uint32_t *client_serial)
3261{
3262 DBusPreallocatedSend *preallocated;
3263
3264 _dbus_assert (connection != NULL);
3265 _dbus_assert (message != NULL);
3266
3267 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
3268 if (preallocated == NULL)
3269 return FALSE;
3270
3271 _dbus_connection_send_preallocated_unlocked_no_update (connection,
3272 preallocated,
3273 message,
3274 client_serial);
3275 return TRUE;
3276}
3277
3307 DBusMessage *message,
3308 dbus_uint32_t *serial)
3309{
3310 _dbus_return_val_if_fail (connection != NULL, FALSE);
3311 _dbus_return_val_if_fail (message != NULL, FALSE);
3312
3313 CONNECTION_LOCK (connection);
3314
3315#ifdef HAVE_UNIX_FD_PASSING
3316
3317 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3318 message->n_unix_fds > 0)
3319 {
3320 /* Refuse to send fds on a connection that cannot handle
3321 them. Unfortunately we cannot return a proper error here, so
3322 the best we can is just return. */
3323 CONNECTION_UNLOCK (connection);
3324 return FALSE;
3325 }
3326
3327#endif
3328
3329 return _dbus_connection_send_and_unlock (connection,
3330 message,
3331 serial);
3332}
3333
3334static dbus_bool_t
3335reply_handler_timeout (void *data)
3336{
3337 DBusConnection *connection;
3338 DBusDispatchStatus status;
3339 DBusPendingCall *pending = data;
3340
3341 connection = _dbus_pending_call_get_connection_and_lock (pending);
3342 _dbus_connection_ref_unlocked (connection);
3343
3345 connection);
3349
3350 _dbus_verbose ("middle\n");
3351 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3352
3353 /* Unlocks, and calls out to user code */
3354 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3355 dbus_connection_unref (connection);
3356
3357 return TRUE;
3358}
3359
3404 DBusMessage *message,
3405 DBusPendingCall **pending_return,
3406 int timeout_milliseconds)
3407{
3408 DBusPendingCall *pending;
3409 dbus_int32_t serial = -1;
3410 DBusDispatchStatus status;
3411
3412 _dbus_return_val_if_fail (connection != NULL, FALSE);
3413 _dbus_return_val_if_fail (message != NULL, FALSE);
3414 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3415
3416 if (pending_return)
3417 *pending_return = NULL;
3418
3419 CONNECTION_LOCK (connection);
3420
3421#ifdef HAVE_UNIX_FD_PASSING
3422
3423 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3424 message->n_unix_fds > 0)
3425 {
3426 /* Refuse to send fds on a connection that cannot handle
3427 them. Unfortunately we cannot return a proper error here, so
3428 the best we can do is return TRUE but leave *pending_return
3429 as NULL. */
3430 CONNECTION_UNLOCK (connection);
3431 return TRUE;
3432 }
3433
3434#endif
3435
3436 if (!_dbus_connection_get_is_connected_unlocked (connection))
3437 {
3438 CONNECTION_UNLOCK (connection);
3439
3440 return TRUE;
3441 }
3442
3443 pending = _dbus_pending_call_new_unlocked (connection,
3444 timeout_milliseconds,
3445 reply_handler_timeout);
3446
3447 if (pending == NULL)
3448 {
3449 CONNECTION_UNLOCK (connection);
3450 return FALSE;
3451 }
3452
3453 /* Assign a serial to the message */
3454 serial = dbus_message_get_serial (message);
3455 if (serial == 0)
3456 {
3457 serial = _dbus_connection_get_next_client_serial (connection);
3458 dbus_message_set_serial (message, serial);
3459 }
3460
3461 if (!_dbus_pending_call_set_timeout_error_unlocked (pending, message, serial))
3462 goto error;
3463
3464 /* Insert the serial in the pending replies hash;
3465 * hash takes a refcount on DBusPendingCall.
3466 * Also, add the timeout.
3467 */
3468 if (!_dbus_connection_attach_pending_call_unlocked (connection,
3469 pending))
3470 goto error;
3471
3472 if (!_dbus_connection_send_unlocked_no_update (connection, message, NULL))
3473 {
3474 _dbus_connection_detach_pending_call_and_unlock (connection,
3475 pending);
3476 goto error_unlocked;
3477 }
3478
3479 if (pending_return)
3480 *pending_return = pending; /* hand off refcount */
3481 else
3482 {
3483 _dbus_connection_detach_pending_call_unlocked (connection, pending);
3484 /* we still have a ref to the pending call in this case, we unref
3485 * after unlocking, below
3486 */
3487 }
3488
3489 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3490
3491 /* this calls out to user code */
3492 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3493
3494 if (pending_return == NULL)
3495 dbus_pending_call_unref (pending);
3496
3497 return TRUE;
3498
3499 error:
3500 CONNECTION_UNLOCK (connection);
3501 error_unlocked:
3502 dbus_pending_call_unref (pending);
3503 return FALSE;
3504}
3505
3540 DBusMessage *message,
3541 int timeout_milliseconds,
3542 DBusError *error)
3543{
3544 DBusMessage *reply;
3545 DBusPendingCall *pending;
3546
3547 _dbus_return_val_if_fail (connection != NULL, NULL);
3548 _dbus_return_val_if_fail (message != NULL, NULL);
3549 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, NULL);
3550 _dbus_return_val_if_error_is_set (error, NULL);
3551
3552#ifdef HAVE_UNIX_FD_PASSING
3553
3554 CONNECTION_LOCK (connection);
3555 if (!_dbus_transport_can_pass_unix_fd(connection->transport) &&
3556 message->n_unix_fds > 0)
3557 {
3558 CONNECTION_UNLOCK (connection);
3559 dbus_set_error(error, DBUS_ERROR_FAILED, "Cannot send file descriptors on this connection.");
3560 return NULL;
3561 }
3562 CONNECTION_UNLOCK (connection);
3563
3564#endif
3565
3566 if (!dbus_connection_send_with_reply (connection, message,
3567 &pending, timeout_milliseconds))
3568 {
3569 _DBUS_SET_OOM (error);
3570 return NULL;
3571 }
3572
3573 if (pending == NULL)
3574 {
3575 dbus_set_error (error, DBUS_ERROR_DISCONNECTED, "Connection is closed");
3576 return NULL;
3577 }
3578
3579 dbus_pending_call_block (pending);
3580
3581 reply = dbus_pending_call_steal_reply (pending);
3582 dbus_pending_call_unref (pending);
3583
3584 /* call_complete_and_unlock() called from pending_call_block() should
3585 * always fill this in.
3586 */
3587 _dbus_assert (reply != NULL);
3588
3589 if (dbus_set_error_from_message (error, reply))
3590 {
3591 dbus_message_unref (reply);
3592 return NULL;
3593 }
3594 else
3595 return reply;
3596}
3597
3606static DBusDispatchStatus
3607_dbus_connection_flush_unlocked (DBusConnection *connection)
3608{
3609 /* We have to specify DBUS_ITERATION_DO_READING here because
3610 * otherwise we could have two apps deadlock if they are both doing
3611 * a flush(), and the kernel buffers fill up. This could change the
3612 * dispatch status.
3613 */
3614 DBusDispatchStatus status;
3615
3616 HAVE_LOCK_CHECK (connection);
3617
3618 while (connection->n_outgoing > 0 &&
3619 _dbus_connection_get_is_connected_unlocked (connection))
3620 {
3621 _dbus_verbose ("doing iteration in\n");
3622 HAVE_LOCK_CHECK (connection);
3624 NULL,
3625 DBUS_ITERATION_DO_READING |
3626 DBUS_ITERATION_DO_WRITING |
3627 DBUS_ITERATION_BLOCK,
3628 -1);
3629 }
3630
3631 HAVE_LOCK_CHECK (connection);
3632 _dbus_verbose ("middle\n");
3633 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3634
3635 HAVE_LOCK_CHECK (connection);
3636 return status;
3637}
3638
3644void
3646{
3647 /* We have to specify DBUS_ITERATION_DO_READING here because
3648 * otherwise we could have two apps deadlock if they are both doing
3649 * a flush(), and the kernel buffers fill up. This could change the
3650 * dispatch status.
3651 */
3652 DBusDispatchStatus status;
3653
3654 _dbus_return_if_fail (connection != NULL);
3655
3656 CONNECTION_LOCK (connection);
3657
3658 status = _dbus_connection_flush_unlocked (connection);
3659
3660 HAVE_LOCK_CHECK (connection);
3661 /* Unlocks and calls out to user code */
3662 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3663
3664 _dbus_verbose ("end\n");
3665}
3666
3677static dbus_bool_t
3678_dbus_connection_read_write_dispatch (DBusConnection *connection,
3679 int timeout_milliseconds,
3680 dbus_bool_t dispatch)
3681{
3682 DBusDispatchStatus dstatus;
3683 dbus_bool_t progress_possible;
3684
3685 /* Need to grab a ref here in case we're a private connection and
3686 * the user drops the last ref in a handler we call; see bug
3687 * https://bugs.freedesktop.org/show_bug.cgi?id=15635
3688 */
3689 dbus_connection_ref (connection);
3690 dstatus = dbus_connection_get_dispatch_status (connection);
3691
3692 if (dispatch && dstatus == DBUS_DISPATCH_DATA_REMAINS)
3693 {
3694 _dbus_verbose ("doing dispatch\n");
3695 dbus_connection_dispatch (connection);
3696 CONNECTION_LOCK (connection);
3697 }
3698 else if (dstatus == DBUS_DISPATCH_NEED_MEMORY)
3699 {
3700 _dbus_verbose ("pausing for memory\n");
3701 _dbus_memory_pause_based_on_timeout (timeout_milliseconds);
3702 CONNECTION_LOCK (connection);
3703 }
3704 else
3705 {
3706 CONNECTION_LOCK (connection);
3707 if (_dbus_connection_get_is_connected_unlocked (connection))
3708 {
3709 _dbus_verbose ("doing iteration\n");
3711 NULL,
3712 DBUS_ITERATION_DO_READING |
3713 DBUS_ITERATION_DO_WRITING |
3714 DBUS_ITERATION_BLOCK,
3715 timeout_milliseconds);
3716 }
3717 }
3718
3719 HAVE_LOCK_CHECK (connection);
3720 /* If we can dispatch, we can make progress until the Disconnected message
3721 * has been processed; if we can only read/write, we can make progress
3722 * as long as the transport is open.
3723 */
3724 if (dispatch)
3725 progress_possible = connection->n_incoming != 0 ||
3726 connection->disconnect_message_link != NULL;
3727 else
3728 progress_possible = _dbus_connection_get_is_connected_unlocked (connection);
3729
3730 CONNECTION_UNLOCK (connection);
3731
3732 dbus_connection_unref (connection);
3733
3734 return progress_possible; /* TRUE if we can make more progress */
3735}
3736
3737
3774 int timeout_milliseconds)
3775{
3776 _dbus_return_val_if_fail (connection != NULL, FALSE);
3777 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3778 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, TRUE);
3779}
3780
3806 int timeout_milliseconds)
3807{
3808 _dbus_return_val_if_fail (connection != NULL, FALSE);
3809 _dbus_return_val_if_fail (timeout_milliseconds >= 0 || timeout_milliseconds == -1, FALSE);
3810 return _dbus_connection_read_write_dispatch(connection, timeout_milliseconds, FALSE);
3811}
3812
3813/* We need to call this anytime we pop the head of the queue, and then
3814 * update_dispatch_status_and_unlock needs to be called afterward
3815 * which will "process" the disconnected message and set
3816 * disconnected_message_processed.
3817 */
3818static void
3819check_disconnected_message_arrived_unlocked (DBusConnection *connection,
3820 DBusMessage *head_of_queue)
3821{
3822 HAVE_LOCK_CHECK (connection);
3823
3824 /* checking that the link is NULL is an optimization to avoid the is_signal call */
3825 if (connection->disconnect_message_link == NULL &&
3826 dbus_message_is_signal (head_of_queue,
3828 "Disconnected"))
3829 {
3830 connection->disconnected_message_arrived = TRUE;
3831 }
3832}
3833
3855{
3856 DBusDispatchStatus status;
3857 DBusMessage *message;
3858
3859 _dbus_return_val_if_fail (connection != NULL, NULL);
3860
3861 _dbus_verbose ("start\n");
3862
3863 /* this is called for the side effect that it queues
3864 * up any messages from the transport
3865 */
3866 status = dbus_connection_get_dispatch_status (connection);
3867 if (status != DBUS_DISPATCH_DATA_REMAINS)
3868 return NULL;
3869
3870 CONNECTION_LOCK (connection);
3871
3872 _dbus_connection_acquire_dispatch (connection);
3873
3874 /* While a message is outstanding, the dispatch lock is held */
3875 _dbus_assert (connection->message_borrowed == NULL);
3876
3877 connection->message_borrowed = _dbus_list_get_first (&connection->incoming_messages);
3878
3879 message = connection->message_borrowed;
3880
3881 check_disconnected_message_arrived_unlocked (connection, message);
3882
3883 /* Note that we KEEP the dispatch lock until the message is returned */
3884 if (message == NULL)
3885 _dbus_connection_release_dispatch (connection);
3886
3887 CONNECTION_UNLOCK (connection);
3888
3889 _dbus_message_trace_ref (message, -1, -1, "dbus_connection_borrow_message");
3890
3891 /* We don't update dispatch status until it's returned or stolen */
3892
3893 return message;
3894}
3895
3904void
3906 DBusMessage *message)
3907{
3908 DBusDispatchStatus status;
3909
3910 _dbus_return_if_fail (connection != NULL);
3911 _dbus_return_if_fail (message != NULL);
3912 _dbus_return_if_fail (message == connection->message_borrowed);
3913 _dbus_return_if_fail (connection->dispatch_acquired);
3914
3915 CONNECTION_LOCK (connection);
3916
3917 _dbus_assert (message == connection->message_borrowed);
3918
3919 connection->message_borrowed = NULL;
3920
3921 _dbus_connection_release_dispatch (connection);
3922
3923 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3924 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3925
3926 _dbus_message_trace_ref (message, -1, -1, "dbus_connection_return_message");
3927}
3928
3938void
3940 DBusMessage *message)
3941{
3942 DBusMessage *pop_message;
3943 DBusDispatchStatus status;
3944
3945 _dbus_return_if_fail (connection != NULL);
3946 _dbus_return_if_fail (message != NULL);
3947 _dbus_return_if_fail (message == connection->message_borrowed);
3948 _dbus_return_if_fail (connection->dispatch_acquired);
3949
3950 CONNECTION_LOCK (connection);
3951
3952 _dbus_assert (message == connection->message_borrowed);
3953
3954 pop_message = _dbus_list_pop_first (&connection->incoming_messages);
3955 _dbus_assert (message == pop_message);
3956 (void) pop_message; /* unused unless asserting */
3957
3958 connection->n_incoming -= 1;
3959
3960 _dbus_verbose ("Incoming message %p stolen from queue, %d incoming\n",
3961 message, connection->n_incoming);
3962
3963 connection->message_borrowed = NULL;
3964
3965 _dbus_connection_release_dispatch (connection);
3966
3967 status = _dbus_connection_get_dispatch_status_unlocked (connection);
3968 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
3969 _dbus_message_trace_ref (message, -1, -1,
3970 "dbus_connection_steal_borrowed_message");
3971}
3972
3973/* See dbus_connection_pop_message, but requires the caller to own
3974 * the lock before calling. May drop the lock while running.
3975 */
3976static DBusList*
3977_dbus_connection_pop_message_link_unlocked (DBusConnection *connection)
3978{
3979 HAVE_LOCK_CHECK (connection);
3980
3981 _dbus_assert (connection->message_borrowed == NULL);
3982
3983 if (connection->n_incoming > 0)
3984 {
3985 DBusList *link;
3986
3987 link = _dbus_list_pop_first_link (&connection->incoming_messages);
3988 connection->n_incoming -= 1;
3989
3990 _dbus_verbose ("Message %p (%s %s %s %s sig:'%s' serial:%u) removed from incoming queue %p, %d incoming\n",
3991 link->data,
3993 dbus_message_get_path (link->data) ?
3994 dbus_message_get_path (link->data) :
3995 "no path",
3998 "no interface",
4001 "no member",
4004 connection, connection->n_incoming);
4005
4006 _dbus_message_trace_ref (link->data, -1, -1,
4007 "_dbus_connection_pop_message_link_unlocked");
4008
4009 check_disconnected_message_arrived_unlocked (connection, link->data);
4010
4011 return link;
4012 }
4013 else
4014 return NULL;
4015}
4016
4017/* See dbus_connection_pop_message, but requires the caller to own
4018 * the lock before calling. May drop the lock while running.
4019 */
4020static DBusMessage*
4021_dbus_connection_pop_message_unlocked (DBusConnection *connection)
4022{
4023 DBusList *link;
4024
4025 HAVE_LOCK_CHECK (connection);
4026
4027 link = _dbus_connection_pop_message_link_unlocked (connection);
4028
4029 if (link != NULL)
4030 {
4031 DBusMessage *message;
4032
4033 message = link->data;
4034
4035 _dbus_list_free_link (link);
4036
4037 return message;
4038 }
4039 else
4040 return NULL;
4041}
4042
4043static void
4044_dbus_connection_putback_message_link_unlocked (DBusConnection *connection,
4045 DBusList *message_link)
4046{
4047 HAVE_LOCK_CHECK (connection);
4048
4049 _dbus_assert (message_link != NULL);
4050 /* You can't borrow a message while a link is outstanding */
4051 _dbus_assert (connection->message_borrowed == NULL);
4052 /* We had to have the dispatch lock across the pop/putback */
4053 _dbus_assert (connection->dispatch_acquired);
4054
4056 message_link);
4057 connection->n_incoming += 1;
4058
4059 _dbus_verbose ("Message %p (%s %s %s '%s') put back into queue %p, %d incoming\n",
4060 message_link->data,
4062 dbus_message_get_interface (message_link->data) ?
4063 dbus_message_get_interface (message_link->data) :
4064 "no interface",
4065 dbus_message_get_member (message_link->data) ?
4066 dbus_message_get_member (message_link->data) :
4067 "no member",
4068 dbus_message_get_signature (message_link->data),
4069 connection, connection->n_incoming);
4070
4071 _dbus_message_trace_ref (message_link->data, -1, -1,
4072 "_dbus_connection_putback_message_link_unlocked");
4073}
4074
4096{
4097 DBusMessage *message;
4098 DBusDispatchStatus status;
4099
4100 _dbus_verbose ("start\n");
4101
4102 /* this is called for the side effect that it queues
4103 * up any messages from the transport
4104 */
4105 status = dbus_connection_get_dispatch_status (connection);
4106 if (status != DBUS_DISPATCH_DATA_REMAINS)
4107 return NULL;
4108
4109 CONNECTION_LOCK (connection);
4110 _dbus_connection_acquire_dispatch (connection);
4111 HAVE_LOCK_CHECK (connection);
4112
4113 message = _dbus_connection_pop_message_unlocked (connection);
4114
4115 _dbus_verbose ("Returning popped message %p\n", message);
4116
4117 _dbus_connection_release_dispatch (connection);
4118
4119 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4120 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4121
4122 return message;
4123}
4124
4132static void
4133_dbus_connection_acquire_dispatch (DBusConnection *connection)
4134{
4135 HAVE_LOCK_CHECK (connection);
4136
4137 _dbus_connection_ref_unlocked (connection);
4138 CONNECTION_UNLOCK (connection);
4139
4140 _dbus_verbose ("locking dispatch_mutex\n");
4141 _dbus_cmutex_lock (connection->dispatch_mutex);
4142
4143 while (connection->dispatch_acquired)
4144 {
4145 _dbus_verbose ("waiting for dispatch to be acquirable\n");
4146 _dbus_condvar_wait (connection->dispatch_cond,
4147 connection->dispatch_mutex);
4148 }
4149
4150 _dbus_assert (!connection->dispatch_acquired);
4151
4152 connection->dispatch_acquired = TRUE;
4153
4154 _dbus_verbose ("unlocking dispatch_mutex\n");
4155 _dbus_cmutex_unlock (connection->dispatch_mutex);
4156
4157 CONNECTION_LOCK (connection);
4159}
4160
4168static void
4169_dbus_connection_release_dispatch (DBusConnection *connection)
4170{
4171 HAVE_LOCK_CHECK (connection);
4172
4173 _dbus_verbose ("locking dispatch_mutex\n");
4174 _dbus_cmutex_lock (connection->dispatch_mutex);
4175
4176 _dbus_assert (connection->dispatch_acquired);
4177
4178 connection->dispatch_acquired = FALSE;
4180
4181 _dbus_verbose ("unlocking dispatch_mutex\n");
4182 _dbus_cmutex_unlock (connection->dispatch_mutex);
4183}
4184
4185static void
4186_dbus_connection_failed_pop (DBusConnection *connection,
4187 DBusList *message_link)
4188{
4190 message_link);
4191 connection->n_incoming += 1;
4192}
4193
4194/* Note this may be called multiple times since we don't track whether we already did it */
4195static void
4196notify_disconnected_unlocked (DBusConnection *connection)
4197{
4198 HAVE_LOCK_CHECK (connection);
4199
4200 /* Set the weakref in dbus-bus.c to NULL, so nobody will get a disconnected
4201 * connection from dbus_bus_get(). We make the same guarantee for
4202 * dbus_connection_open() but in a different way since we don't want to
4203 * unref right here; we instead check for connectedness before returning
4204 * the connection from the hash.
4205 */
4207
4208 /* Dump the outgoing queue, we aren't going to be able to
4209 * send it now, and we'd like accessors like
4210 * dbus_connection_get_outgoing_size() to be accurate.
4211 */
4212 if (connection->n_outgoing > 0)
4213 {
4214 DBusList *link;
4215
4216 _dbus_verbose ("Dropping %d outgoing messages since we're disconnected\n",
4217 connection->n_outgoing);
4218
4219 while ((link = _dbus_list_get_last_link (&connection->outgoing_messages)))
4220 {
4221 _dbus_connection_message_sent_unlocked (connection, link->data);
4222 }
4223 }
4224}
4225
4226/* Note this may be called multiple times since we don't track whether we already did it */
4227static DBusDispatchStatus
4228notify_disconnected_and_dispatch_complete_unlocked (DBusConnection *connection)
4229{
4230 HAVE_LOCK_CHECK (connection);
4231
4232 if (connection->disconnect_message_link != NULL)
4233 {
4234 _dbus_verbose ("Sending disconnect message\n");
4235
4236 /* If we have pending calls, queue their timeouts - we want the Disconnected
4237 * to be the last message, after these timeouts.
4238 */
4239 connection_timeout_and_complete_all_pending_calls_unlocked (connection);
4240
4241 /* We haven't sent the disconnect message already,
4242 * and all real messages have been queued up.
4243 */
4245 connection->disconnect_message_link);
4246 connection->disconnect_message_link = NULL;
4247
4249 }
4250
4252}
4253
4254static DBusDispatchStatus
4255_dbus_connection_get_dispatch_status_unlocked (DBusConnection *connection)
4256{
4257 HAVE_LOCK_CHECK (connection);
4258
4259 if (connection->n_incoming > 0)
4261 else if (!_dbus_transport_queue_messages (connection->transport))
4263 else
4264 {
4265 DBusDispatchStatus status;
4266 dbus_bool_t is_connected;
4267
4268 status = _dbus_transport_get_dispatch_status (connection->transport);
4269 is_connected = _dbus_transport_get_is_connected (connection->transport);
4270
4271 _dbus_verbose ("dispatch status = %s is_connected = %d\n",
4272 DISPATCH_STATUS_NAME (status), is_connected);
4273
4274 if (!is_connected)
4275 {
4276 /* It's possible this would be better done by having an explicit
4277 * notification from _dbus_transport_disconnect() that would
4278 * synchronously do this, instead of waiting for the next dispatch
4279 * status check. However, probably not good to change until it causes
4280 * a problem.
4281 */
4282 notify_disconnected_unlocked (connection);
4283
4284 /* I'm not sure this is needed; the idea is that we want to
4285 * queue the Disconnected only after we've read all the
4286 * messages, but if we're disconnected maybe we are guaranteed
4287 * to have read them all ?
4288 */
4289 if (status == DBUS_DISPATCH_COMPLETE)
4290 status = notify_disconnected_and_dispatch_complete_unlocked (connection);
4291 }
4292
4293 if (status != DBUS_DISPATCH_COMPLETE)
4294 return status;
4295 else if (connection->n_incoming > 0)
4297 else
4299 }
4300}
4301
4302static void
4303_dbus_connection_update_dispatch_status_and_unlock (DBusConnection *connection,
4304 DBusDispatchStatus new_status)
4305{
4306 dbus_bool_t changed;
4308 void *data;
4309
4310 HAVE_LOCK_CHECK (connection);
4311
4312 _dbus_connection_ref_unlocked (connection);
4313
4314 changed = new_status != connection->last_dispatch_status;
4315
4316 connection->last_dispatch_status = new_status;
4317
4318 function = connection->dispatch_status_function;
4319 data = connection->dispatch_status_data;
4320
4321 if (connection->disconnected_message_arrived &&
4322 !connection->disconnected_message_processed)
4323 {
4325
4326 /* this does an unref, but we have a ref
4327 * so we should not run the finalizer here
4328 * inside the lock.
4329 */
4330 connection_forget_shared_unlocked (connection);
4331
4332 if (connection->exit_on_disconnect)
4333 {
4334 CONNECTION_UNLOCK (connection);
4335
4336 _dbus_verbose ("Exiting on Disconnected signal\n");
4337 _dbus_exit (1);
4338 _dbus_assert_not_reached ("Call to exit() returned");
4339 }
4340 }
4341
4342 /* We drop the lock */
4343 CONNECTION_UNLOCK (connection);
4344
4345 if (changed && function)
4346 {
4347 _dbus_verbose ("Notifying of change to dispatch status of %p now %d (%s)\n",
4348 connection, new_status,
4349 DISPATCH_STATUS_NAME (new_status));
4350 (* function) (connection, new_status, data);
4351 }
4352
4353 dbus_connection_unref (connection);
4354}
4355
4383{
4384 DBusDispatchStatus status;
4385
4386 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4387
4388 _dbus_verbose ("start\n");
4389
4390 CONNECTION_LOCK (connection);
4391
4392 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4393
4394 CONNECTION_UNLOCK (connection);
4395
4396 return status;
4397}
4398
4402static DBusHandlerResult
4403_dbus_connection_peer_filter_unlocked_no_update (DBusConnection *connection,
4404 DBusMessage *message)
4405{
4406 dbus_bool_t sent = FALSE;
4407 DBusMessage *ret = NULL;
4408 DBusList *expire_link;
4409
4410 if (connection->route_peer_messages && dbus_message_get_destination (message) != NULL)
4411 {
4412 /* This means we're letting the bus route this message */
4414 }
4415
4417 {
4419 }
4420
4421 /* Preallocate a linked-list link, so that if we need to dispose of a
4422 * message, we can attach it to the expired list */
4423 expire_link = _dbus_list_alloc_link (NULL);
4424
4425 if (!expire_link)
4427
4428 if (dbus_message_is_method_call (message,
4430 "Ping"))
4431 {
4432 ret = dbus_message_new_method_return (message);
4433 if (ret == NULL)
4434 goto out;
4435
4436 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4437 }
4438 else if (dbus_message_is_method_call (message,
4440 "GetMachineId"))
4441 {
4442 DBusString uuid;
4443 DBusError error = DBUS_ERROR_INIT;
4444
4445 if (!_dbus_string_init (&uuid))
4446 goto out;
4447
4448 if (_dbus_get_local_machine_uuid_encoded (&uuid, &error))
4449 {
4450 const char *v_STRING;
4451
4452 ret = dbus_message_new_method_return (message);
4453
4454 if (ret == NULL)
4455 {
4456 _dbus_string_free (&uuid);
4457 goto out;
4458 }
4459
4460 v_STRING = _dbus_string_get_const_data (&uuid);
4461 if (dbus_message_append_args (ret,
4462 DBUS_TYPE_STRING, &v_STRING,
4464 {
4465 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4466 }
4467 }
4468 else if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
4469 {
4470 dbus_error_free (&error);
4471 goto out;
4472 }
4473 else
4474 {
4475 ret = dbus_message_new_error (message, error.name, error.message);
4476 dbus_error_free (&error);
4477
4478 if (ret == NULL)
4479 goto out;
4480
4481 sent = _dbus_connection_send_unlocked_no_update (connection, ret,
4482 NULL);
4483 }
4484
4485 _dbus_string_free (&uuid);
4486 }
4487 else
4488 {
4489 /* We need to bounce anything else with this interface, otherwise apps
4490 * could start extending the interface and when we added extensions
4491 * here to DBusConnection we'd break those apps.
4492 */
4493 ret = dbus_message_new_error (message,
4495 "Unknown method invoked on org.freedesktop.DBus.Peer interface");
4496 if (ret == NULL)
4497 goto out;
4498
4499 sent = _dbus_connection_send_unlocked_no_update (connection, ret, NULL);
4500 }
4501
4502out:
4503 if (ret == NULL)
4504 {
4505 _dbus_list_free_link (expire_link);
4506 }
4507 else
4508 {
4509 /* It'll be safe to unref the reply when we unlock */
4510 expire_link->data = ret;
4511 _dbus_list_prepend_link (&connection->expired_messages, expire_link);
4512 }
4513
4514 if (!sent)
4516
4518}
4519
4526static DBusHandlerResult
4527_dbus_connection_run_builtin_filters_unlocked_no_update (DBusConnection *connection,
4528 DBusMessage *message)
4529{
4530 /* We just run one filter for now but have the option to run more
4531 if the spec calls for it in the future */
4532
4533 return _dbus_connection_peer_filter_unlocked_no_update (connection, message);
4534}
4535
4580{
4581 DBusMessage *message;
4582 DBusList *link, *filter_list_copy, *message_link;
4583 DBusHandlerResult result;
4584 DBusPendingCall *pending;
4585 dbus_int32_t reply_serial;
4586 DBusDispatchStatus status;
4587 dbus_bool_t found_object;
4588
4589 _dbus_return_val_if_fail (connection != NULL, DBUS_DISPATCH_COMPLETE);
4590
4591 _dbus_verbose ("\n");
4592
4593 CONNECTION_LOCK (connection);
4594 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4595 if (status != DBUS_DISPATCH_DATA_REMAINS)
4596 {
4597 /* unlocks and calls out to user code */
4598 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4599 return status;
4600 }
4601
4602 /* We need to ref the connection since the callback could potentially
4603 * drop the last ref to it
4604 */
4605 _dbus_connection_ref_unlocked (connection);
4606
4607 _dbus_connection_acquire_dispatch (connection);
4608 HAVE_LOCK_CHECK (connection);
4609
4610 message_link = _dbus_connection_pop_message_link_unlocked (connection);
4611 if (message_link == NULL)
4612 {
4613 /* another thread dispatched our stuff */
4614
4615 _dbus_verbose ("another thread dispatched message (during acquire_dispatch above)\n");
4616
4617 _dbus_connection_release_dispatch (connection);
4618
4619 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4620
4621 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4622
4623 dbus_connection_unref (connection);
4624
4625 return status;
4626 }
4627
4628 message = message_link->data;
4629
4630 _dbus_verbose (" dispatching message %p (%s %s %s '%s')\n",
4631 message,
4633 dbus_message_get_interface (message) ?
4634 dbus_message_get_interface (message) :
4635 "no interface",
4636 dbus_message_get_member (message) ?
4637 dbus_message_get_member (message) :
4638 "no member",
4639 dbus_message_get_signature (message));
4640
4642
4643 /* Pending call handling must be first, because if you do
4644 * dbus_connection_send_with_reply_and_block() or
4645 * dbus_pending_call_block() then no handlers/filters will be run on
4646 * the reply. We want consistent semantics in the case where we
4647 * dbus_connection_dispatch() the reply.
4648 */
4649
4650 reply_serial = dbus_message_get_reply_serial (message);
4651 pending = _dbus_hash_table_lookup_int (connection->pending_replies,
4652 reply_serial);
4653 if (pending)
4654 {
4655 _dbus_verbose ("Dispatching a pending reply\n");
4656 complete_pending_call_and_unlock (connection, pending, message);
4657 pending = NULL; /* it's probably unref'd */
4658
4659 CONNECTION_LOCK (connection);
4660 _dbus_verbose ("pending call completed in dispatch\n");
4662 goto out;
4663 }
4664
4665 /* If skipping builtin filters, we are probably a monitor. */
4666 if (connection->builtin_filters_enabled)
4667 {
4668 result = _dbus_connection_run_builtin_filters_unlocked_no_update (connection, message);
4670 goto out;
4671 }
4672
4673 if (!_dbus_list_copy (&connection->filter_list, &filter_list_copy))
4674 {
4675 _dbus_connection_release_dispatch (connection);
4676 HAVE_LOCK_CHECK (connection);
4677
4678 _dbus_connection_failed_pop (connection, message_link);
4679
4680 /* unlocks and calls user code */
4681 _dbus_connection_update_dispatch_status_and_unlock (connection,
4683 dbus_connection_unref (connection);
4684
4686 }
4687
4688 for (link = _dbus_list_get_first_link (&filter_list_copy);
4689 link != NULL;
4690 link = _dbus_list_get_next_link (&filter_list_copy, link))
4691 _dbus_message_filter_ref (link->data);
4692
4693 /* We're still protected from dispatch() reentrancy here
4694 * since we acquired the dispatcher
4695 */
4696 CONNECTION_UNLOCK (connection);
4697
4698 link = _dbus_list_get_first_link (&filter_list_copy);
4699 while (link != NULL)
4700 {
4701 DBusMessageFilter *filter = link->data;
4702 DBusList *next = _dbus_list_get_next_link (&filter_list_copy, link);
4703
4704 if (filter->function == NULL)
4705 {
4706 _dbus_verbose (" filter was removed in a callback function\n");
4707 link = next;
4708 continue;
4709 }
4710
4711 _dbus_verbose (" running filter on message %p\n", message);
4712 result = (* filter->function) (connection, message, filter->user_data);
4713
4715 break;
4716
4717 link = next;
4718 }
4719
4720 _dbus_list_clear_full (&filter_list_copy,
4721 (DBusFreeFunction) _dbus_message_filter_unref);
4722
4723 CONNECTION_LOCK (connection);
4724
4725 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4726 {
4727 _dbus_verbose ("No memory\n");
4728 goto out;
4729 }
4730 else if (result == DBUS_HANDLER_RESULT_HANDLED)
4731 {
4732 _dbus_verbose ("filter handled message in dispatch\n");
4733 goto out;
4734 }
4735
4736 /* We're still protected from dispatch() reentrancy here
4737 * since we acquired the dispatcher
4738 */
4739 _dbus_verbose (" running object path dispatch on message %p (%s %s %s '%s')\n",
4740 message,
4742 dbus_message_get_interface (message) ?
4743 dbus_message_get_interface (message) :
4744 "no interface",
4745 dbus_message_get_member (message) ?
4746 dbus_message_get_member (message) :
4747 "no member",
4748 dbus_message_get_signature (message));
4749
4750 HAVE_LOCK_CHECK (connection);
4751 result = _dbus_object_tree_dispatch_and_unlock (connection->objects,
4752 message,
4753 &found_object);
4754
4755 CONNECTION_LOCK (connection);
4756
4758 {
4759 _dbus_verbose ("object tree handled message in dispatch\n");
4760 goto out;
4761 }
4762
4764 {
4765 DBusMessage *reply;
4766 DBusString str;
4767 DBusPreallocatedSend *preallocated;
4768 DBusList *expire_link;
4769
4770 _dbus_verbose (" sending error %s\n",
4772
4773 if (!_dbus_string_init (&str))
4774 {
4776 _dbus_verbose ("no memory for error string in dispatch\n");
4777 goto out;
4778 }
4779
4780 if (!_dbus_string_append_printf (&str,
4781 "Method \"%s\" with signature \"%s\" on interface \"%s\" doesn't exist\n",
4782 dbus_message_get_member (message),
4784 dbus_message_get_interface (message)))
4785 {
4786 _dbus_string_free (&str);
4788 _dbus_verbose ("no memory for error string in dispatch\n");
4789 goto out;
4790 }
4791
4792 reply = dbus_message_new_error (message,
4795 _dbus_string_free (&str);
4796
4797 if (reply == NULL)
4798 {
4800 _dbus_verbose ("no memory for error reply in dispatch\n");
4801 goto out;
4802 }
4803
4804 expire_link = _dbus_list_alloc_link (reply);
4805
4806 if (expire_link == NULL)
4807 {
4808 dbus_message_unref (reply);
4810 _dbus_verbose ("no memory for error send in dispatch\n");
4811 goto out;
4812 }
4813
4814 preallocated = _dbus_connection_preallocate_send_unlocked (connection);
4815
4816 if (preallocated == NULL)
4817 {
4818 _dbus_list_free_link (expire_link);
4819 /* It's OK that this is finalized, because it hasn't been seen by
4820 * anything that could attach user callbacks */
4821 dbus_message_unref (reply);
4823 _dbus_verbose ("no memory for error send in dispatch\n");
4824 goto out;
4825 }
4826
4827 _dbus_connection_send_preallocated_unlocked_no_update (connection, preallocated,
4828 reply, NULL);
4829 /* reply will be freed when we release the lock */
4830 _dbus_list_prepend_link (&connection->expired_messages, expire_link);
4831
4833 }
4834
4835 _dbus_verbose (" done dispatching %p (%s %s %s '%s') on connection %p\n", message,
4837 dbus_message_get_interface (message) ?
4838 dbus_message_get_interface (message) :
4839 "no interface",
4840 dbus_message_get_member (message) ?
4841 dbus_message_get_member (message) :
4842 "no member",
4844 connection);
4845
4846 out:
4847 if (result == DBUS_HANDLER_RESULT_NEED_MEMORY)
4848 {
4849 _dbus_verbose ("out of memory\n");
4850
4851 /* Put message back, and we'll start over.
4852 * Yes this means handlers must be idempotent if they
4853 * don't return HANDLED; c'est la vie.
4854 */
4855 _dbus_connection_putback_message_link_unlocked (connection,
4856 message_link);
4857 /* now we don't want to free them */
4858 message_link = NULL;
4859 message = NULL;
4860 }
4861 else
4862 {
4863 _dbus_verbose (" ... done dispatching\n");
4864 }
4865
4866 _dbus_connection_release_dispatch (connection);
4867 HAVE_LOCK_CHECK (connection);
4868
4869 if (message != NULL)
4870 {
4871 /* We don't want this message to count in maximum message limits when
4872 * computing the dispatch status, below. We have to drop the lock
4873 * temporarily, because finalizing a message can trigger callbacks.
4874 *
4875 * We have a reference to the connection, and we don't use any cached
4876 * pointers to the connection's internals below this point, so it should
4877 * be safe to drop the lock and take it back. */
4878 CONNECTION_UNLOCK (connection);
4879 dbus_message_unref (message);
4880 CONNECTION_LOCK (connection);
4881 }
4882
4883 if (message_link != NULL)
4884 _dbus_list_free_link (message_link);
4885
4886 _dbus_verbose ("before final status update\n");
4887 status = _dbus_connection_get_dispatch_status_unlocked (connection);
4888
4889 /* unlocks and calls user code */
4890 _dbus_connection_update_dispatch_status_and_unlock (connection, status);
4891
4892 dbus_connection_unref (connection);
4893
4894 return status;
4895}
4896
4960 DBusAddWatchFunction add_function,
4961 DBusRemoveWatchFunction remove_function,
4962 DBusWatchToggledFunction toggled_function,
4963 void *data,
4964 DBusFreeFunction free_data_function)
4965{
4966 dbus_bool_t retval;
4967
4968 _dbus_return_val_if_fail (connection != NULL, FALSE);
4969
4970 CONNECTION_LOCK (connection);
4971
4972 retval = _dbus_watch_list_set_functions (connection->watches,
4973 add_function, remove_function,
4974 toggled_function,
4975 data, free_data_function);
4976
4977 CONNECTION_UNLOCK (connection);
4978
4979 return retval;
4980}
4981
5023 DBusAddTimeoutFunction add_function,
5024 DBusRemoveTimeoutFunction remove_function,
5025 DBusTimeoutToggledFunction toggled_function,
5026 void *data,
5027 DBusFreeFunction free_data_function)
5028{
5029 dbus_bool_t retval;
5030
5031 _dbus_return_val_if_fail (connection != NULL, FALSE);
5032
5033 CONNECTION_LOCK (connection);
5034
5035 retval = _dbus_timeout_list_set_functions (connection->timeouts,
5036 add_function, remove_function,
5037 toggled_function,
5038 data, free_data_function);
5039
5040 CONNECTION_UNLOCK (connection);
5041
5042 return retval;
5043}
5044
5059void
5061 DBusWakeupMainFunction wakeup_main_function,
5062 void *data,
5063 DBusFreeFunction free_data_function)
5064{
5065 void *old_data;
5066 DBusFreeFunction old_free_data;
5067
5068 _dbus_return_if_fail (connection != NULL);
5069
5070 CONNECTION_LOCK (connection);
5071 old_data = connection->wakeup_main_data;
5072 old_free_data = connection->free_wakeup_main_data;
5073
5074 connection->wakeup_main_function = wakeup_main_function;
5075 connection->wakeup_main_data = data;
5076 connection->free_wakeup_main_data = free_data_function;
5077
5078 CONNECTION_UNLOCK (connection);
5079
5080 /* Callback outside the lock */
5081 if (old_free_data)
5082 (*old_free_data) (old_data);
5083}
5084
5105void
5108 void *data,
5109 DBusFreeFunction free_data_function)
5110{
5111 void *old_data;
5112 DBusFreeFunction old_free_data;
5113
5114 _dbus_return_if_fail (connection != NULL);
5115
5116 CONNECTION_LOCK (connection);
5117 old_data = connection->dispatch_status_data;
5118 old_free_data = connection->free_dispatch_status_data;
5119
5120 connection->dispatch_status_function = function;
5121 connection->dispatch_status_data = data;
5122 connection->free_dispatch_status_data = free_data_function;
5123
5124 CONNECTION_UNLOCK (connection);
5125
5126 /* Callback outside the lock */
5127 if (old_free_data)
5128 (*old_free_data) (old_data);
5129}
5130
5152 int *fd)
5153{
5154 _dbus_return_val_if_fail (connection != NULL, FALSE);
5155 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5156
5157#ifdef DBUS_WIN
5158 /* FIXME do this on a lower level */
5159 return FALSE;
5160#endif
5161
5162 return dbus_connection_get_socket(connection, fd);
5163}
5164
5182 int *fd)
5183{
5184 dbus_bool_t retval;
5185 DBusSocket s = DBUS_SOCKET_INIT;
5186
5187 _dbus_return_val_if_fail (connection != NULL, FALSE);
5188 _dbus_return_val_if_fail (connection->transport != NULL, FALSE);
5189
5190 CONNECTION_LOCK (connection);
5191
5192 retval = _dbus_transport_get_socket_fd (connection->transport, &s);
5193
5194 if (retval)
5195 {
5196 *fd = _dbus_socket_get_int (s);
5197 }
5198
5199 CONNECTION_UNLOCK (connection);
5200
5201 return retval;
5202}
5203
5204
5229 unsigned long *uid)
5230{
5231 dbus_bool_t result;
5232
5233 _dbus_return_val_if_fail (connection != NULL, FALSE);
5234 _dbus_return_val_if_fail (uid != NULL, FALSE);
5235
5236 CONNECTION_LOCK (connection);
5237
5239 result = FALSE;
5240 else
5241 result = _dbus_transport_get_unix_user (connection->transport,
5242 uid);
5243
5244#ifdef DBUS_WIN
5245 _dbus_assert (!result);
5246#endif
5247
5248 CONNECTION_UNLOCK (connection);
5249
5250 return result;
5251}
5252
5265 unsigned long *pid)
5266{
5267 dbus_bool_t result;
5268
5269 _dbus_return_val_if_fail (connection != NULL, FALSE);
5270 _dbus_return_val_if_fail (pid != NULL, FALSE);
5271
5272 CONNECTION_LOCK (connection);
5273
5275 result = FALSE;
5276 else
5277 result = _dbus_transport_get_unix_process_id (connection->transport,
5278 pid);
5279
5280 CONNECTION_UNLOCK (connection);
5281
5282 return result;
5283}
5284
5298 void **data,
5299 dbus_int32_t *data_size)
5300{
5301 dbus_bool_t result;
5302
5303 _dbus_return_val_if_fail (connection != NULL, FALSE);
5304 _dbus_return_val_if_fail (data != NULL, FALSE);
5305 _dbus_return_val_if_fail (data_size != NULL, FALSE);
5306
5307 CONNECTION_LOCK (connection);
5308
5310 result = FALSE;
5311 else
5313 data,
5314 data_size);
5315 CONNECTION_UNLOCK (connection);
5316
5317 return result;
5318}
5319
5342void
5345 void *data,
5346 DBusFreeFunction free_data_function)
5347{
5348 void *old_data = NULL;
5349 DBusFreeFunction old_free_function = NULL;
5350
5351 _dbus_return_if_fail (connection != NULL);
5352
5353 CONNECTION_LOCK (connection);
5355 function, data, free_data_function,
5356 &old_data, &old_free_function);
5357 CONNECTION_UNLOCK (connection);
5358
5359 if (old_free_function != NULL)
5360 (* old_free_function) (old_data);
5361}
5362
5363/* Same calling convention as dbus_connection_get_windows_user */
5365_dbus_connection_get_linux_security_label (DBusConnection *connection,
5366 char **label_p)
5367{
5368 dbus_bool_t result;
5369
5370 _dbus_assert (connection != NULL);
5371 _dbus_assert (label_p != NULL);
5372
5373 CONNECTION_LOCK (connection);
5374
5376 result = FALSE;
5377 else
5378 result = _dbus_transport_get_linux_security_label (connection->transport,
5379 label_p);
5380#ifndef __linux__
5381 _dbus_assert (!result);
5382#endif
5383
5384 CONNECTION_UNLOCK (connection);
5385
5386 return result;
5387}
5388
5390_dbus_connection_get_credentials (DBusConnection *connection)
5391{
5392 DBusCredentials *result;
5393
5394 _dbus_assert (connection != NULL);
5395
5396 CONNECTION_LOCK (connection);
5397
5399 result = NULL;
5400 else
5401 result = _dbus_transport_get_credentials (connection->transport);
5402
5403 CONNECTION_UNLOCK (connection);
5404
5405 return result;
5406}
5407
5441 char **windows_sid_p)
5442{
5443 dbus_bool_t result;
5444
5445 _dbus_return_val_if_fail (connection != NULL, FALSE);
5446 _dbus_return_val_if_fail (windows_sid_p != NULL, FALSE);
5447
5448 CONNECTION_LOCK (connection);
5449
5451 result = FALSE;
5452 else
5453 result = _dbus_transport_get_windows_user (connection->transport,
5454 windows_sid_p);
5455
5456#ifdef DBUS_UNIX
5457 _dbus_assert (!result);
5458#endif
5459
5460 CONNECTION_UNLOCK (connection);
5461
5462 return result;
5463}
5464
5486void
5489 void *data,
5490 DBusFreeFunction free_data_function)
5491{
5492 void *old_data = NULL;
5493 DBusFreeFunction old_free_function = NULL;
5494
5495 _dbus_return_if_fail (connection != NULL);
5496
5497 CONNECTION_LOCK (connection);
5499 function, data, free_data_function,
5500 &old_data, &old_free_function);
5501 CONNECTION_UNLOCK (connection);
5502
5503 if (old_free_function != NULL)
5504 (* old_free_function) (old_data);
5505}
5506
5533void
5535 dbus_bool_t value)
5536{
5537 _dbus_return_if_fail (connection != NULL);
5538
5539 CONNECTION_LOCK (connection);
5541 CONNECTION_UNLOCK (connection);
5542}
5543
5565void
5567 dbus_bool_t value)
5568{
5569 _dbus_return_if_fail (connection != NULL);
5570
5571 CONNECTION_LOCK (connection);
5572 connection->builtin_filters_enabled = value;
5573 CONNECTION_UNLOCK (connection);
5574}
5575
5593void
5595 dbus_bool_t value)
5596{
5597 _dbus_return_if_fail (connection != NULL);
5598
5599 CONNECTION_LOCK (connection);
5600 connection->route_peer_messages = value;
5601 CONNECTION_UNLOCK (connection);
5602}
5603
5628 void *user_data,
5629 DBusFreeFunction free_data_function)
5630{
5631 DBusMessageFilter *filter;
5632
5633 _dbus_return_val_if_fail (connection != NULL, FALSE);
5634 _dbus_return_val_if_fail (function != NULL, FALSE);
5635
5636 filter = dbus_new0 (DBusMessageFilter, 1);
5637 if (filter == NULL)
5638 return FALSE;
5639
5640 _dbus_atomic_inc (&filter->refcount);
5641
5642 CONNECTION_LOCK (connection);
5643
5644 if (!_dbus_list_append (&connection->filter_list,
5645 filter))
5646 {
5647 _dbus_message_filter_unref (filter);
5648 CONNECTION_UNLOCK (connection);
5649 return FALSE;
5650 }
5651
5652 /* Fill in filter after all memory allocated,
5653 * so we don't run the free_user_data_function
5654 * if the add_filter() fails
5655 */
5656
5657 filter->function = function;
5658 filter->user_data = user_data;
5659 filter->free_user_data_function = free_data_function;
5660
5661 CONNECTION_UNLOCK (connection);
5662 return TRUE;
5663}
5664
5677void
5680 void *user_data)
5681{
5682 DBusList *link;
5683 DBusMessageFilter *filter;
5684
5685 _dbus_return_if_fail (connection != NULL);
5686 _dbus_return_if_fail (function != NULL);
5687
5688 CONNECTION_LOCK (connection);
5689
5690 filter = NULL;
5691
5692 link = _dbus_list_get_last_link (&connection->filter_list);
5693 while (link != NULL)
5694 {
5695 filter = link->data;
5696
5697 if (filter->function == function &&
5698 filter->user_data == user_data)
5699 {
5700 _dbus_list_remove_link (&connection->filter_list, link);
5701 filter->function = NULL;
5702
5703 break;
5704 }
5705
5706 link = _dbus_list_get_prev_link (&connection->filter_list, link);
5707 filter = NULL;
5708 }
5709
5710 CONNECTION_UNLOCK (connection);
5711
5712#ifndef DBUS_DISABLE_CHECKS
5713 if (filter == NULL)
5714 {
5715 _dbus_warn_check_failed ("Attempt to remove filter function %p user data %p, but no such filter has been added",
5716 function, user_data);
5717 return;
5718 }
5719#endif
5720
5721 /* Call application code */
5722 if (filter->free_user_data_function)
5723 (* filter->free_user_data_function) (filter->user_data);
5724
5725 filter->free_user_data_function = NULL;
5726 filter->user_data = NULL;
5727
5728 _dbus_message_filter_unref (filter);
5729}
5730
5746static dbus_bool_t
5747_dbus_connection_register_object_path (DBusConnection *connection,
5748 dbus_bool_t fallback,
5749 const char *path,
5750 const DBusObjectPathVTable *vtable,
5751 void *user_data,
5752 DBusError *error)
5753{
5754 char **decomposed_path;
5755 dbus_bool_t retval;
5756
5757 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5758 return FALSE;
5759
5760 CONNECTION_LOCK (connection);
5761
5762 retval = _dbus_object_tree_register (connection->objects,
5763 fallback,
5764 (const char **) decomposed_path, vtable,
5765 user_data, error);
5766
5767 CONNECTION_UNLOCK (connection);
5768
5769 dbus_free_string_array (decomposed_path);
5770
5771 return retval;
5772}
5773
5788 const char *path,
5789 const DBusObjectPathVTable *vtable,
5790 void *user_data,
5791 DBusError *error)
5792{
5793 _dbus_return_val_if_fail (connection != NULL, FALSE);
5794 _dbus_return_val_if_fail (path != NULL, FALSE);
5795 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5796 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5797
5798 return _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, error);
5799}
5800
5818 const char *path,
5819 const DBusObjectPathVTable *vtable,
5820 void *user_data)
5821{
5822 dbus_bool_t retval;
5823 DBusError error = DBUS_ERROR_INIT;
5824
5825 _dbus_return_val_if_fail (connection != NULL, FALSE);
5826 _dbus_return_val_if_fail (path != NULL, FALSE);
5827 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5828 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5829
5830 retval = _dbus_connection_register_object_path (connection, FALSE, path, vtable, user_data, &error);
5831
5833 {
5834 _dbus_warn ("%s", error.message);
5835 dbus_error_free (&error);
5836 return FALSE;
5837 }
5838
5839 return retval;
5840}
5841
5858 const char *path,
5859 const DBusObjectPathVTable *vtable,
5860 void *user_data,
5861 DBusError *error)
5862{
5863 _dbus_return_val_if_fail (connection != NULL, FALSE);
5864 _dbus_return_val_if_fail (path != NULL, FALSE);
5865 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5866 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5867
5868 return _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, error);
5869}
5870
5890 const char *path,
5891 const DBusObjectPathVTable *vtable,
5892 void *user_data)
5893{
5894 dbus_bool_t retval;
5895 DBusError error = DBUS_ERROR_INIT;
5896
5897 _dbus_return_val_if_fail (connection != NULL, FALSE);
5898 _dbus_return_val_if_fail (path != NULL, FALSE);
5899 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5900 _dbus_return_val_if_fail (vtable != NULL, FALSE);
5901
5902 retval = _dbus_connection_register_object_path (connection, TRUE, path, vtable, user_data, &error);
5903
5905 {
5906 _dbus_warn ("%s", error.message);
5907 dbus_error_free (&error);
5908 return FALSE;
5909 }
5910
5911 return retval;
5912}
5913
5925 const char *path)
5926{
5927 char **decomposed_path;
5928
5929 _dbus_return_val_if_fail (connection != NULL, FALSE);
5930 _dbus_return_val_if_fail (path != NULL, FALSE);
5931 _dbus_return_val_if_fail (path[0] == '/', FALSE);
5932
5933 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5934 return FALSE;
5935
5936 CONNECTION_LOCK (connection);
5937
5938 _dbus_object_tree_unregister_and_unlock (connection->objects, (const char **) decomposed_path);
5939
5940 dbus_free_string_array (decomposed_path);
5941
5942 return TRUE;
5943}
5944
5957 const char *path,
5958 void **data_p)
5959{
5960 char **decomposed_path;
5961
5962 _dbus_return_val_if_fail (connection != NULL, FALSE);
5963 _dbus_return_val_if_fail (path != NULL, FALSE);
5964 _dbus_return_val_if_fail (data_p != NULL, FALSE);
5965
5966 *data_p = NULL;
5967
5968 if (!_dbus_decompose_path (path, strlen (path), &decomposed_path, NULL))
5969 return FALSE;
5970
5971 CONNECTION_LOCK (connection);
5972
5973 *data_p = _dbus_object_tree_get_user_data_unlocked (connection->objects, (const char**) decomposed_path);
5974
5975 CONNECTION_UNLOCK (connection);
5976
5977 dbus_free_string_array (decomposed_path);
5978
5979 return TRUE;
5980}
5981
5994 const char *parent_path,
5995 char ***child_entries)
5996{
5997 char **decomposed_path;
5998 dbus_bool_t retval;
5999 _dbus_return_val_if_fail (connection != NULL, FALSE);
6000 _dbus_return_val_if_fail (parent_path != NULL, FALSE);
6001 _dbus_return_val_if_fail (parent_path[0] == '/', FALSE);
6002 _dbus_return_val_if_fail (child_entries != NULL, FALSE);
6003
6004 if (!_dbus_decompose_path (parent_path, strlen (parent_path), &decomposed_path, NULL))
6005 return FALSE;
6006
6007 CONNECTION_LOCK (connection);
6008
6010 (const char **) decomposed_path,
6011 child_entries);
6012 dbus_free_string_array (decomposed_path);
6013
6014 return retval;
6015}
6016
6017static DBusDataSlotAllocator slot_allocator =
6018 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (connection_slots));
6019
6036{
6037 return _dbus_data_slot_allocator_alloc (&slot_allocator,
6038 slot_p);
6039}
6040
6052void
6054{
6055 _dbus_return_if_fail (*slot_p >= 0);
6056
6057 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
6058}
6059
6084 dbus_int32_t slot,
6085 void *data,
6086 DBusFreeFunction free_data_func)
6087{
6088 DBusFreeFunction old_free_func;
6089 void *old_data;
6090 dbus_bool_t retval;
6091
6092 _dbus_return_val_if_fail (connection != NULL, FALSE);
6093 _dbus_return_val_if_fail (slot >= 0, FALSE);
6094
6095 SLOTS_LOCK (connection);
6096
6097 retval = _dbus_data_slot_list_set (&slot_allocator,
6098 &connection->slot_list,
6099 slot, data, free_data_func,
6100 &old_free_func, &old_data);
6101
6102 SLOTS_UNLOCK (connection);
6103
6104 if (retval)
6105 {
6106 /* Do the actual free outside the connection lock */
6107 if (old_free_func)
6108 (* old_free_func) (old_data);
6109 }
6110
6111 return retval;
6112}
6113
6131void*
6133 dbus_int32_t slot)
6134{
6135 void *res;
6136
6137 _dbus_return_val_if_fail (connection != NULL, NULL);
6138 _dbus_return_val_if_fail (slot >= 0, NULL);
6139
6140 SLOTS_LOCK (connection);
6141
6142 res = _dbus_data_slot_list_get (&slot_allocator,
6143 &connection->slot_list,
6144 slot);
6145
6146 SLOTS_UNLOCK (connection);
6147
6148 return res;
6149}
6150
6157void
6159{
6160 if (will_modify_sigpipe)
6161 _dbus_atomic_set_nonzero (&_dbus_modify_sigpipe);
6162 else
6163 _dbus_atomic_set_zero (&_dbus_modify_sigpipe);
6164}
6165
6174void
6176 long size)
6177{
6178 _dbus_return_if_fail (connection != NULL);
6179
6180 CONNECTION_LOCK (connection);
6182 size);
6183 CONNECTION_UNLOCK (connection);
6184}
6185
6192long
6194{
6195 long res;
6196
6197 _dbus_return_val_if_fail (connection != NULL, 0);
6198
6199 CONNECTION_LOCK (connection);
6201 CONNECTION_UNLOCK (connection);
6202 return res;
6203}
6204
6213void
6215 long n)
6216{
6217 _dbus_return_if_fail (connection != NULL);
6218
6219 CONNECTION_LOCK (connection);
6221 n);
6222 CONNECTION_UNLOCK (connection);
6223}
6224
6231long
6233{
6234 long res;
6235
6236 _dbus_return_val_if_fail (connection != NULL, 0);
6237
6238 CONNECTION_LOCK (connection);
6240 CONNECTION_UNLOCK (connection);
6241 return res;
6242}
6243
6269void
6271 long size)
6272{
6273 _dbus_return_if_fail (connection != NULL);
6274
6275 CONNECTION_LOCK (connection);
6277 size);
6278 CONNECTION_UNLOCK (connection);
6279}
6280
6287long
6289{
6290 long res;
6291
6292 _dbus_return_val_if_fail (connection != NULL, 0);
6293
6294 CONNECTION_LOCK (connection);
6296 CONNECTION_UNLOCK (connection);
6297 return res;
6298}
6299
6311void
6313 long n)
6314{
6315 _dbus_return_if_fail (connection != NULL);
6316
6317 CONNECTION_LOCK (connection);
6319 n);
6320 CONNECTION_UNLOCK (connection);
6321}
6322
6329long
6331{
6332 long res;
6333
6334 _dbus_return_val_if_fail (connection != NULL, 0);
6335
6336 CONNECTION_LOCK (connection);
6338 CONNECTION_UNLOCK (connection);
6339 return res;
6340}
6341
6352long
6354{
6355 long res;
6356
6357 _dbus_return_val_if_fail (connection != NULL, 0);
6358
6359 CONNECTION_LOCK (connection);
6361 CONNECTION_UNLOCK (connection);
6362 return res;
6363}
6364
6365#ifdef DBUS_ENABLE_STATS
6366void
6367_dbus_connection_get_stats (DBusConnection *connection,
6368 dbus_uint32_t *in_messages,
6369 dbus_uint32_t *in_bytes,
6370 dbus_uint32_t *in_fds,
6371 dbus_uint32_t *in_peak_bytes,
6372 dbus_uint32_t *in_peak_fds,
6373 dbus_uint32_t *out_messages,
6374 dbus_uint32_t *out_bytes,
6375 dbus_uint32_t *out_fds,
6376 dbus_uint32_t *out_peak_bytes,
6377 dbus_uint32_t *out_peak_fds)
6378{
6379 CONNECTION_LOCK (connection);
6380
6381 if (in_messages != NULL)
6382 *in_messages = connection->n_incoming;
6383
6384 _dbus_transport_get_stats (connection->transport,
6385 in_bytes, in_fds, in_peak_bytes, in_peak_fds);
6386
6387 if (out_messages != NULL)
6388 *out_messages = connection->n_outgoing;
6389
6390 if (out_bytes != NULL)
6391 *out_bytes = _dbus_counter_get_size_value (connection->outgoing_counter);
6392
6393 if (out_fds != NULL)
6394 *out_fds = _dbus_counter_get_unix_fd_value (connection->outgoing_counter);
6395
6396 if (out_peak_bytes != NULL)
6397 *out_peak_bytes = _dbus_counter_get_peak_size_value (connection->outgoing_counter);
6398
6399 if (out_peak_fds != NULL)
6400 *out_peak_fds = _dbus_counter_get_peak_unix_fd_value (connection->outgoing_counter);
6401
6402 CONNECTION_UNLOCK (connection);
6403}
6404#endif /* DBUS_ENABLE_STATS */
6405
6413long
6415{
6416 long res;
6417
6418 _dbus_return_val_if_fail (connection != NULL, 0);
6419
6420 CONNECTION_LOCK (connection);
6422 CONNECTION_UNLOCK (connection);
6423 return res;
6424}
6425
6426#ifdef DBUS_ENABLE_EMBEDDED_TESTS
6433const char*
6434_dbus_connection_get_address (DBusConnection *connection)
6435{
6436 return _dbus_transport_get_address (connection->transport);
6437}
6438#endif
6439
void dbus_address_entries_free(DBusAddressEntry **entries)
Frees a NULL-terminated array of address entries.
Definition: dbus-address.c:194
dbus_bool_t dbus_parse_address(const char *address, DBusAddressEntry ***entry_result, int *array_len, DBusError *error)
Parses an address string of the form:
Definition: dbus-address.c:368
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition: dbus-address.c:249
void _dbus_bus_notify_shared_connection_disconnected_unlocked(DBusConnection *connection)
Internal function that checks to see if this is a shared connection owned by the bus and if it is unr...
Definition: dbus-bus.c:390
dbus_bool_t _dbus_connection_handle_watch(DBusWatch *watch, unsigned int condition, void *data)
A callback for use with dbus_watch_new() to create a DBusWatch.
void _dbus_connection_toggle_timeout_unlocked(DBusConnection *connection, DBusTimeout *timeout, dbus_bool_t enabled)
Toggles a timeout and notifies app via connection's DBusTimeoutToggledFunction if available.
dbus_bool_t dbus_connection_has_messages_to_send(DBusConnection *connection)
Checks whether there are messages in the outgoing message queue.
void _dbus_connection_do_iteration_unlocked(DBusConnection *connection, DBusPendingCall *pending, unsigned int flags, int timeout_milliseconds)
Queues incoming messages and sends outgoing messages for this connection, optionally blocking in the ...
dbus_bool_t _dbus_connection_send_and_unlock(DBusConnection *connection, DBusMessage *message, dbus_uint32_t *client_serial)
Like dbus_connection_send(), but assumes the connection is already locked on function entry,...
DBusConnection * _dbus_connection_new_for_transport(DBusTransport *transport)
Creates a new connection for the given transport.
dbus_bool_t _dbus_connection_has_messages_to_send_unlocked(DBusConnection *connection)
Checks whether there are messages in the outgoing message queue.
void(* DBusWatchRemoveFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
void _dbus_connection_unlock(DBusConnection *connection)
Releases the connection lock.
void _dbus_connection_lock(DBusConnection *connection)
Acquires the connection lock.
void _dbus_connection_remove_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Removes a watch using the connection's DBusRemoveWatchFunction if available.
dbus_bool_t _dbus_connection_add_timeout_unlocked(DBusConnection *connection, DBusTimeout *timeout)
Adds a timeout using the connection's DBusAddTimeoutFunction if available.
void _dbus_connection_toggle_watch_unlocked(DBusConnection *connection, DBusWatch *watch, dbus_bool_t enabled)
Toggles a watch and notifies app via connection's DBusWatchToggledFunction if available.
void(* DBusTimeoutToggleFunction)(DBusTimeoutList *list, DBusTimeout *timeout, dbus_bool_t enabled)
Function to be called in protected_change_timeout() with refcount held.
void(* DBusTimeoutRemoveFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
dbus_bool_t _dbus_connection_add_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Adds a watch using the connection's DBusAddWatchFunction if available.
void _dbus_connection_remove_pending_call(DBusConnection *connection, DBusPendingCall *pending)
Removes a pending call from the connection, such that the pending reply will be ignored.
void _dbus_connection_close_if_only_one_ref(DBusConnection *connection)
Used internally to handle the semantics of dbus_server_set_new_connection_function().
void _dbus_connection_unref_unlocked(DBusConnection *connection)
Decrements the reference count of a DBusConnection.
DBusMessage * _dbus_connection_get_message_to_send(DBusConnection *connection)
Gets the next outgoing message.
dbus_bool_t(* DBusTimeoutAddFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
void _dbus_connection_message_sent_unlocked(DBusConnection *connection, DBusMessage *message)
Notifies the connection that a message has been sent, so the message can be removed from the outgoing...
void _dbus_connection_remove_timeout_unlocked(DBusConnection *connection, DBusTimeout *timeout)
Removes a timeout using the connection's DBusRemoveTimeoutFunction if available.
void(* DBusWatchToggleFunction)(DBusWatchList *list, DBusWatch *watch, dbus_bool_t enabled)
Function to be called in protected_change_watch() with refcount held.
void _dbus_connection_queue_received_message_link(DBusConnection *connection, DBusList *link)
Adds a message-containing list link to the incoming message queue, taking ownership of the link and t...
int _dbus_connection_get_pending_fds_count(DBusConnection *connection)
Return how many file descriptors are pending in the loader.
void _dbus_connection_set_pending_fds_function(DBusConnection *connection, DBusPendingFdsChangeFunction callback, void *data)
Register a function to be called whenever the number of pending file descriptors in the loader change...
dbus_bool_t(* DBusWatchAddFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
void _dbus_connection_block_pending_call(DBusPendingCall *pending)
Blocks until a pending call times out or gets a reply.
void _dbus_connection_queue_synthesized_message_link(DBusConnection *connection, DBusList *link)
Adds a link + message to the incoming message queue.
void _dbus_connection_close_possibly_shared(DBusConnection *connection)
Closes a shared OR private connection, while dbus_connection_close() can only be used on private conn...
DBusConnection * _dbus_connection_ref_unlocked(DBusConnection *connection)
Increments the reference count of a DBusConnection.
void dbus_connection_return_message(DBusConnection *connection, DBusMessage *message)
Used to return a message after peeking at it using dbus_connection_borrow_message().
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
dbus_bool_t dbus_connection_get_object_path_data(DBusConnection *connection, const char *path, void **data_p)
Gets the user data passed to dbus_connection_register_object_path() or dbus_connection_register_fallb...
void dbus_connection_set_max_message_size(DBusConnection *connection, long size)
Specifies the maximum size message this connection is allowed to receive.
void dbus_connection_flush(DBusConnection *connection)
Blocks until the outgoing message queue is empty.
long dbus_connection_get_max_message_unix_fds(DBusConnection *connection)
Gets the value set by dbus_connection_set_max_message_unix_fds().
void dbus_connection_set_exit_on_disconnect(DBusConnection *connection, dbus_bool_t exit_on_disconnect)
Set whether _exit() should be called when the connection receives a disconnect signal.
dbus_bool_t dbus_connection_get_socket(DBusConnection *connection, int *fd)
Gets the underlying Windows or UNIX socket file descriptor of the connection, if any.
DBusMessage * dbus_connection_pop_message(DBusConnection *connection)
Returns the first-received message from the incoming message queue, removing it from the queue.
long dbus_connection_get_max_received_unix_fds(DBusConnection *connection)
Gets the value set by dbus_connection_set_max_received_unix_fds().
dbus_bool_t dbus_connection_register_object_path(DBusConnection *connection, const char *path, const DBusObjectPathVTable *vtable, void *user_data)
Registers a handler for a given path in the object hierarchy.
void dbus_connection_close(DBusConnection *connection)
Closes a private connection, so no further data can be sent or received.
void dbus_connection_set_max_message_unix_fds(DBusConnection *connection, long n)
Specifies the maximum number of unix fds a message on this connection is allowed to receive.
void dbus_connection_set_wakeup_main_function(DBusConnection *connection, DBusWakeupMainFunction wakeup_main_function, void *data, DBusFreeFunction free_data_function)
Sets the mainloop wakeup function for the connection.
dbus_bool_t dbus_connection_get_windows_user(DBusConnection *connection, char **windows_sid_p)
Gets the Windows user SID of the connection if known.
dbus_bool_t dbus_connection_get_is_authenticated(DBusConnection *connection)
Gets whether the connection was authenticated.
void dbus_connection_send_preallocated(DBusConnection *connection, DBusPreallocatedSend *preallocated, DBusMessage *message, dbus_uint32_t *client_serial)
Sends a message using preallocated resources.
dbus_bool_t dbus_connection_read_write(DBusConnection *connection, int timeout_milliseconds)
This function is intended for use with applications that don't want to write a main loop and deal wit...
long dbus_connection_get_max_received_size(DBusConnection *connection)
Gets the value set by dbus_connection_set_max_received_size().
dbus_bool_t dbus_connection_get_unix_fd(DBusConnection *connection, int *fd)
Get the UNIX file descriptor of the connection, if any.
void(* DBusDispatchStatusFunction)(DBusConnection *connection, DBusDispatchStatus new_status, void *data)
Called when the return value of dbus_connection_get_dispatch_status() may have changed.
dbus_bool_t dbus_connection_can_send_type(DBusConnection *connection, int type)
Tests whether a certain type can be send via the connection.
dbus_bool_t dbus_connection_list_registered(DBusConnection *connection, const char *parent_path, char ***child_entries)
Lists the registered fallback handlers and object path handlers at the given parent_path.
void * dbus_connection_get_data(DBusConnection *connection, dbus_int32_t slot)
Retrieves data previously set with dbus_connection_set_data().
DBusConnection * dbus_connection_open_private(const char *address, DBusError *error)
Opens a new, dedicated connection to a remote address.
long dbus_connection_get_outgoing_size(DBusConnection *connection)
Gets the approximate size in bytes of all messages in the outgoing message queue.
void dbus_connection_set_dispatch_status_function(DBusConnection *connection, DBusDispatchStatusFunction function, void *data, DBusFreeFunction free_data_function)
Set a function to be invoked when the dispatch status changes.
dbus_bool_t dbus_connection_read_write_dispatch(DBusConnection *connection, int timeout_milliseconds)
This function is intended for use with applications that don't want to write a main loop and deal wit...
DBusHandlerResult(* DBusHandleMessageFunction)(DBusConnection *connection, DBusMessage *message, void *user_data)
Called when a message needs to be handled.
void dbus_connection_remove_filter(DBusConnection *connection, DBusHandleMessageFunction function, void *user_data)
Removes a previously-added message filter.
dbus_bool_t dbus_connection_get_is_connected(DBusConnection *connection)
Gets whether the connection is currently open.
DBusPreallocatedSend * dbus_connection_preallocate_send(DBusConnection *connection)
Preallocates resources needed to send a message, allowing the message to be sent without the possibil...
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
void dbus_connection_set_max_received_size(DBusConnection *connection, long size)
Sets the maximum total number of bytes that can be used for all messages received on this connection.
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
DBusDispatchStatus dbus_connection_dispatch(DBusConnection *connection)
Processes any incoming data.
dbus_bool_t dbus_connection_unregister_object_path(DBusConnection *connection, const char *path)
Unregisters the handler registered with exactly the given path.
void dbus_connection_set_unix_user_function(DBusConnection *connection, DBusAllowUnixUserFunction function, void *data, DBusFreeFunction free_data_function)
Sets a predicate function used to determine whether a given user ID is allowed to connect.
dbus_bool_t dbus_connection_try_register_object_path(DBusConnection *connection, const char *path, const DBusObjectPathVTable *vtable, void *user_data, DBusError *error)
Registers a handler for a given path in the object hierarchy.
void dbus_connection_steal_borrowed_message(DBusConnection *connection, DBusMessage *message)
Used to keep a message after peeking at it using dbus_connection_borrow_message().
dbus_bool_t dbus_connection_allocate_data_slot(dbus_int32_t *slot_p)
Allocates an integer ID to be used for storing application-specific data on any DBusConnection.
void dbus_connection_set_change_sigpipe(dbus_bool_t will_modify_sigpipe)
This function sets a global flag for whether dbus_connection_new() will set SIGPIPE behavior to SIG_I...
long dbus_connection_get_max_message_size(DBusConnection *connection)
Gets the value set by dbus_connection_set_max_message_size().
void dbus_connection_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for connection data slots.
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
void dbus_connection_set_max_received_unix_fds(DBusConnection *connection, long n)
Sets the maximum total number of unix fds that can be used for all messages received on this connecti...
dbus_bool_t dbus_connection_set_data(DBusConnection *connection, dbus_int32_t slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusConnection, along with an optional function to be used for freeing the data...
DBusDispatchStatus dbus_connection_get_dispatch_status(DBusConnection *connection)
Gets the current state of the incoming message queue.
DBusMessage * dbus_connection_send_with_reply_and_block(DBusConnection *connection, DBusMessage *message, int timeout_milliseconds, DBusError *error)
Sends a message and blocks a certain time period while waiting for a reply.
dbus_bool_t dbus_connection_try_register_fallback(DBusConnection *connection, const char *path, const DBusObjectPathVTable *vtable, void *user_data, DBusError *error)
Registers a fallback handler for a given subsection of the object hierarchy.
dbus_bool_t dbus_connection_get_adt_audit_session_data(DBusConnection *connection, void **data, dbus_int32_t *data_size)
Gets the ADT audit data of the connection if any.
DBusMessage * dbus_connection_borrow_message(DBusConnection *connection)
Returns the first-received message from the incoming message queue, leaving it in the queue.
dbus_bool_t dbus_connection_send_with_reply(DBusConnection *connection, DBusMessage *message, DBusPendingCall **pending_return, int timeout_milliseconds)
Queues a message to send, as with dbus_connection_send(), but also returns a DBusPendingCall used to ...
void dbus_connection_set_windows_user_function(DBusConnection *connection, DBusAllowWindowsUserFunction function, void *data, DBusFreeFunction free_data_function)
Sets a predicate function used to determine whether a given user ID is allowed to connect.
dbus_bool_t dbus_connection_get_is_anonymous(DBusConnection *connection)
Gets whether the connection is not authenticated as a specific user.
dbus_bool_t dbus_connection_set_timeout_functions(DBusConnection *connection, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions for the connection.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
dbus_bool_t(* DBusAllowUnixUserFunction)(DBusConnection *connection, unsigned long uid, void *data)
Called during authentication to check whether the given UNIX user ID is allowed to connect,...
DBusDispatchStatus
Indicates the status of incoming data on a DBusConnection.
dbus_bool_t dbus_connection_register_fallback(DBusConnection *connection, const char *path, const DBusObjectPathVTable *vtable, void *user_data)
Registers a fallback handler for a given subsection of the object hierarchy.
DBusConnection * dbus_connection_open(const char *address, DBusError *error)
Gets a connection to a remote address.
void dbus_connection_free_preallocated_send(DBusConnection *connection, DBusPreallocatedSend *preallocated)
Frees preallocated message-sending resources from dbus_connection_preallocate_send().
long dbus_connection_get_outgoing_unix_fds(DBusConnection *connection)
Gets the approximate number of uni fds of all messages in the outgoing message queue.
void(* DBusWakeupMainFunction)(void *data)
Called when the main loop's thread should be notified that there's now work to do.
void dbus_connection_set_allow_anonymous(DBusConnection *connection, dbus_bool_t value)
This function must be called on the server side of a connection when the connection is first seen in ...
dbus_bool_t dbus_connection_add_filter(DBusConnection *connection, DBusHandleMessageFunction function, void *user_data, DBusFreeFunction free_data_function)
Adds a message filter.
dbus_bool_t dbus_connection_send(DBusConnection *connection, DBusMessage *message, dbus_uint32_t *serial)
Adds a message to the outgoing message queue.
DBusConnection * dbus_connection_ref(DBusConnection *connection)
Increments the reference count of a DBusConnection.
void dbus_connection_set_builtin_filters_enabled(DBusConnection *connection, dbus_bool_t value)
Enables the builtin filtering of messages.
char * dbus_connection_get_server_id(DBusConnection *connection)
Gets the ID of the server address we are authenticated to, if this connection is on the client side.
dbus_bool_t(* DBusAllowWindowsUserFunction)(DBusConnection *connection, const char *user_sid, void *data)
Called during authentication to check whether the given Windows user ID is allowed to connect,...
dbus_bool_t dbus_connection_set_watch_functions(DBusConnection *connection, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions for the connection.
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
dbus_bool_t dbus_connection_get_unix_process_id(DBusConnection *connection, unsigned long *pid)
Gets the process ID of the connection if any.
dbus_bool_t dbus_connection_get_unix_user(DBusConnection *connection, unsigned long *uid)
Gets the UNIX user ID of the connection if known.
void dbus_connection_set_route_peer_messages(DBusConnection *connection, dbus_bool_t value)
Normally DBusConnection automatically handles all messages to the org.freedesktop....
@ DBUS_DISPATCH_NEED_MEMORY
More memory is needed to continue.
@ DBUS_DISPATCH_COMPLETE
All currently available data has been processed.
@ DBUS_DISPATCH_DATA_REMAINS
There is more data to potentially convert to messages.
void _dbus_data_slot_allocator_free(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Deallocates an ID previously allocated with _dbus_data_slot_allocator_alloc().
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
void _dbus_data_slot_list_free(DBusDataSlotList *list)
Frees the data slot list and all data slots contained in it, calling application-provided free functi...
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
dbus_bool_t _dbus_data_slot_list_set(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot, void *data, DBusFreeFunction free_data_func, DBusFreeFunction *old_free_func, void **old_data)
Stores a pointer in the data slot list, along with an optional function to be used for freeing the da...
dbus_bool_t _dbus_data_slot_allocator_alloc(DBusDataSlotAllocator *allocator, dbus_int32_t *slot_id_p)
Allocates an integer ID to be used for storing data in a DBusDataSlotList.
Definition: dbus-dataslot.c:72
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:64
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:281
dbus_bool_t dbus_error_has_name(const DBusError *error, const char *name)
Checks whether the error is set and has the given name.
Definition: dbus-errors.c:304
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:356
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:213
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
Definition: dbus-hash.c:1461
void * _dbus_hash_iter_get_value(DBusHashIter *iter)
Gets the value of the current entry.
Definition: dbus-hash.c:620
dbus_bool_t _dbus_hash_table_insert_int(DBusHashTable *table, int key, void *value)
Creates a hash entry with the given key and value.
Definition: dbus-hash.c:1312
dbus_bool_t _dbus_hash_table_insert_string(DBusHashTable *table, char *key, void *value)
Creates a hash entry with the given key and value.
Definition: dbus-hash.c:1278
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero.
Definition: dbus-hash.c:368
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
Definition: dbus-hash.c:550
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
Definition: dbus-hash.c:524
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
Definition: dbus-hash.c:292
dbus_bool_t _dbus_hash_table_remove_int(DBusHashTable *table, int key)
Removes the hash entry for the given key.
Definition: dbus-hash.c:1215
dbus_bool_t _dbus_hash_table_remove_string(DBusHashTable *table, const char *key)
Removes the hash entry for the given key.
Definition: dbus-hash.c:1187
void * _dbus_hash_table_lookup_string(DBusHashTable *table, const char *key)
Looks up the value for a given string in a hash table of type DBUS_HASH_STRING.
Definition: dbus-hash.c:1113
void _dbus_hash_iter_remove_entry(DBusHashIter *iter)
Removes the current entry from the hash table.
Definition: dbus-hash.c:599
void * _dbus_hash_table_lookup_int(DBusHashTable *table, int key)
Looks up the value for a given integer in a hash table of type DBUS_HASH_INT.
Definition: dbus-hash.c:1138
@ DBUS_HASH_INT
Hash keys are integers.
Definition: dbus-hash.h:70
@ DBUS_HASH_STRING
Hash keys are strings.
Definition: dbus-hash.h:69
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
#define _DBUS_LOCK(name)
Locks a global lock, initializing it first if necessary.
void _dbus_warn_check_failed(const char *format,...)
Prints a "critical" warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
char * _dbus_strdup(const char *str)
Duplicates a string.
dbus_bool_t _dbus_get_local_machine_uuid_encoded(DBusString *uuid_str, DBusError *error)
Gets the hex-encoded UUID of the machine this function is executed on.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition: dbus-list.c:597
dbus_bool_t _dbus_list_copy(DBusList **list, DBusList **dest)
Copies a list.
Definition: dbus-list.c:727
DBusList * _dbus_list_pop_first_link(DBusList **list)
Removes the first link in the list and returns it.
Definition: dbus-list.c:658
void * _dbus_list_get_last(DBusList **list)
Gets the last data in the list.
Definition: dbus-list.c:626
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
Definition: dbus-list.c:418
void _dbus_list_append_link(DBusList **list, DBusList *link)
Appends a link to the list.
Definition: dbus-list.c:318
void _dbus_list_clear_full(DBusList **list, DBusFreeFunction function)
Free every link and every element in the list.
Definition: dbus-list.c:570
void _dbus_list_remove_link(DBusList **list, DBusList *link)
Removes a link from the list.
Definition: dbus-list.c:530
void * _dbus_list_get_first(DBusList **list)
Gets the first data in the list.
Definition: dbus-list.c:642
DBusList * _dbus_list_get_last_link(DBusList **list)
Gets the last link in the list.
Definition: dbus-list.c:610
void _dbus_list_unlink(DBusList **list, DBusList *link)
Removes the given link from the list, but doesn't free it.
Definition: dbus-list.c:502
#define _dbus_list_get_prev_link(list, link)
Gets the previous link in the list, or NULL if there are no more links.
Definition: dbus-list.h:122
void _dbus_list_free_link(DBusList *link)
Frees a linked list node allocated with _dbus_list_alloc_link.
Definition: dbus-list.c:257
void * _dbus_list_pop_first(DBusList **list)
Removes the first value in the list and returns it.
Definition: dbus-list.c:679
void _dbus_list_foreach(DBusList **list, DBusForeachFunction function, void *data)
Calls the given function for each element in the list.
Definition: dbus-list.c:789
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
Definition: dbus-list.c:545
void _dbus_list_prepend_link(DBusList **list, DBusList *link)
Prepends a link to the list.
Definition: dbus-list.c:336
dbus_bool_t _dbus_list_prepend(DBusList **list, void *data)
Prepends a value to the list.
Definition: dbus-list.c:295
DBusList * _dbus_list_alloc_link(void *data)
Allocates a linked list node.
Definition: dbus-list.c:245
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:273
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition: dbus-list.h:121
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
int _dbus_current_generation
_dbus_current_generation is used to track each time that dbus_shutdown() is called,...
Definition: dbus-memory.c:774
DBUS_PRIVATE_EXPORT dbus_bool_t _dbus_register_shutdown_func(DBusShutdownFunction function, void *data)
Register a cleanup function to be called exactly once the next time dbus_shutdown() is called.
Definition: dbus-memory.c:803
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:65
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:694
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:59
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:60
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:742
void dbus_message_set_serial(DBusMessage *message, dbus_uint32_t serial)
Sets the serial number of a message.
Definition: dbus-message.c:301
void dbus_message_lock(DBusMessage *message)
Locks a message.
Definition: dbus-message.c:431
const char * dbus_message_type_to_string(int type)
Utility function to convert a D-Bus message type into a machine-readable string (not translated).
void _dbus_message_remove_counter(DBusMessage *message, DBusCounter *counter)
Removes a counter tracking the size/unix fds of this message, and decrements the counter by the size/...
Definition: dbus-message.c:399
void _dbus_message_add_counter_link(DBusMessage *message, DBusList *link)
Adds a counter to be incremented immediately with the size/unix fds of this message,...
Definition: dbus-message.c:327
dbus_bool_t dbus_message_has_interface(DBusMessage *message, const char *iface)
Checks if the message has an interface.
void dbus_message_set_no_reply(DBusMessage *message, dbus_bool_t no_reply)
Sets a flag indicating that the message does not want a reply; if this flag is set,...
dbus_bool_t dbus_message_iter_append_basic(DBusMessageIter *iter, int type, const void *value)
Appends a basic-typed value to the message.
const char * dbus_message_get_path(DBusMessage *message)
Gets the object path this message is being sent to (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitt...
const char * dbus_message_get_interface(DBusMessage *message)
Gets the interface this message is being sent to (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted...
DBusMessage * dbus_message_new_error(DBusMessage *reply_to, const char *error_name, const char *error_message)
Creates a new message that is an error reply to another message.
dbus_uint32_t dbus_message_get_serial(DBusMessage *message)
Returns the serial of a message or 0 if none has been specified.
int dbus_message_get_type(DBusMessage *message)
Gets the type of a message.
dbus_bool_t dbus_message_append_args(DBusMessage *message, int first_arg_type,...)
Appends fields to a message given a variable argument list.
DBusMessage * dbus_message_new_signal(const char *path, const char *iface, const char *name)
Constructs a new message representing a signal emission.
DBusMessage * dbus_message_ref(DBusMessage *message)
Increments the reference count of a DBusMessage.
dbus_bool_t dbus_message_set_error_name(DBusMessage *message, const char *error_name)
Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
dbus_uint32_t dbus_message_get_reply_serial(DBusMessage *message)
Returns the serial that the message is a reply to or 0 if none.
DBusMessage * dbus_message_new_method_return(DBusMessage *method_call)
Constructs a message that is a reply to a method call.
const char * dbus_message_get_destination(DBusMessage *message)
Gets the destination of a message or NULL if there is none set.
void dbus_message_unref(DBusMessage *message)
Decrements the reference count of a DBusMessage, freeing the message if the count reaches 0.
DBusMessage * dbus_message_new(int message_type)
Constructs a new message of the given message type.
dbus_bool_t dbus_set_error_from_message(DBusError *error, DBusMessage *message)
Sets a DBusError based on the contents of the given message.
dbus_bool_t dbus_message_is_method_call(DBusMessage *message, const char *iface, const char *method)
Checks whether the message is a method call with the given interface and member fields.
dbus_bool_t dbus_message_set_reply_serial(DBusMessage *message, dbus_uint32_t reply_serial)
Sets the reply serial of a message (the serial of the message this is a reply to).
dbus_bool_t dbus_message_is_signal(DBusMessage *message, const char *iface, const char *signal_name)
Checks whether the message is a signal with the given interface and member fields.
const char * dbus_message_get_signature(DBusMessage *message)
Gets the type signature of the message, i.e.
const char * dbus_message_get_member(DBusMessage *message)
Gets the interface member being invoked (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted (DBUS_MESSAGE_TYPE...
void dbus_message_iter_init_append(DBusMessage *message, DBusMessageIter *iter)
Initializes a DBusMessageIter for appending arguments to the end of a message.
void _dbus_object_tree_free_all_unlocked(DBusObjectTree *tree)
Free all the handlers in the tree.
void _dbus_object_tree_unregister_and_unlock(DBusObjectTree *tree, const char **path)
Unregisters an object subtree that was registered with the same path.
void _dbus_object_tree_unref(DBusObjectTree *tree)
Decrement the reference count.
dbus_bool_t _dbus_object_tree_list_registered_and_unlock(DBusObjectTree *tree, const char **parent_path, char ***child_entries)
Lists the registered fallback handlers and object path handlers at the given parent_path.
dbus_bool_t _dbus_decompose_path(const char *data, int len, char ***path, int *path_len)
Decompose an object path.
dbus_bool_t _dbus_object_tree_register(DBusObjectTree *tree, dbus_bool_t fallback, const char **path, const DBusObjectPathVTable *vtable, void *user_data, DBusError *error)
Registers a new subtree in the global object tree.
void * _dbus_object_tree_get_user_data_unlocked(DBusObjectTree *tree, const char **path)
Looks up the data passed to _dbus_object_tree_register() for a handler at the given path.
DBusHandlerResult _dbus_object_tree_dispatch_and_unlock(DBusObjectTree *tree, DBusMessage *message, dbus_bool_t *found_object)
Tries to dispatch a message by directing it to handler for the object path listed in the message head...
DBusObjectTree * _dbus_object_tree_new(DBusConnection *connection)
Creates a new object tree, representing a mapping from paths to handler vtables.
void _dbus_pending_call_finish_completion(DBusPendingCall *pending)
Call the notifier function for the pending call.
DBusConnection * _dbus_pending_call_get_connection_and_lock(DBusPendingCall *pending)
Gets the connection associated with this pending call.
DBUS_PRIVATE_EXPORT DBusPendingCall * _dbus_pending_call_ref_unlocked(DBusPendingCall *pending)
Increments the reference count on a pending call, while the lock on its connection is already held.
void _dbus_pending_call_queue_timeout_error_unlocked(DBusPendingCall *pending, DBusConnection *connection)
If the pending call hasn't been timed out, add its timeout error reply to the connection's incoming m...
DBUS_PRIVATE_EXPORT void _dbus_pending_call_unref_and_unlock(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0.
dbus_bool_t _dbus_pending_call_get_completed_unlocked(DBusPendingCall *pending)
Checks whether the pending call has received a reply yet, or not.
void _dbus_pending_call_set_reply_unlocked(DBusPendingCall *pending, DBusMessage *message)
Sets the reply of a pending call with the given message, or if the message is NULL,...
DBUS_PRIVATE_EXPORT DBusPendingCall * _dbus_pending_call_new_unlocked(DBusConnection *connection, int timeout_milliseconds, DBusTimeoutHandler timeout_handler)
Creates a new pending reply object.
dbus_bool_t _dbus_pending_call_set_timeout_error_unlocked(DBusPendingCall *pending, DBusMessage *message, dbus_uint32_t serial)
Sets the reply message associated with the pending call to a timeout error.
#define CONNECTION_LOCK(connection)
Internals of DBusPendingCall.
#define CONNECTION_UNLOCK(connection)
shorter and more visible way to write _dbus_connection_unlock()
void _dbus_pending_call_start_completion_unlocked(DBusPendingCall *pending)
Sets the pending call to completed.
dbus_bool_t _dbus_pending_call_is_timeout_added_unlocked(DBusPendingCall *pending)
Checks to see if a timeout has been added.
dbus_uint32_t _dbus_pending_call_get_reply_serial_unlocked(DBusPendingCall *pending)
Gets the reply's serial number.
DBusConnection * _dbus_pending_call_get_connection_unlocked(DBusPendingCall *pending)
Gets the connection associated with this pending call.
DBusTimeout * _dbus_pending_call_get_timeout_unlocked(DBusPendingCall *pending)
Retrives the timeout.
void _dbus_pending_call_set_timeout_added_unlocked(DBusPendingCall *pending, dbus_bool_t is_added)
Sets wether the timeout has been added.
DBusPendingCall * dbus_pending_call_ref(DBusPendingCall *pending)
Increments the reference count on a pending call.
DBusMessage * dbus_pending_call_steal_reply(DBusPendingCall *pending)
Gets the reply, or returns NULL if none has been received yet.
void dbus_pending_call_block(DBusPendingCall *pending)
Block until the pending call is completed.
dbus_bool_t dbus_pending_call_get_completed(DBusPendingCall *pending)
Checks whether the pending call has received a reply yet, or not.
void dbus_pending_call_unref(DBusPendingCall *pending)
Decrements the reference count on a pending call, freeing it if the count reaches 0.
#define DBUS_MESSAGE_TYPE_METHOD_CALL
Message type of a method call message, see dbus_message_get_type()
#define DBUS_MESSAGE_TYPE_ERROR
Message type of an error reply message, see dbus_message_get_type()
#define DBUS_ERROR_UNKNOWN_METHOD
Method name you invoked isn't known by the object you invoked it on.
#define DBUS_TYPE_UNIX_FD
Type code marking a unix file descriptor.
#define DBUS_ERROR_OBJECT_PATH_IN_USE
There's already an object with the requested object path.
#define DBUS_MESSAGE_TYPE_SIGNAL
Message type of a signal message, see dbus_message_get_type()
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
#define DBUS_ERROR_UNKNOWN_OBJECT
Object you invoked a method on isn't known.
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition: dbus-protocol.h:62
#define DBUS_ERROR_FAILED
A generic error; "something went wrong" - see the error message for more.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define DBUS_ERROR_DISCONNECTED
The connection is disconnected and you're trying to use it.
DBusCounter * _dbus_counter_new(void)
Creates a new DBusCounter.
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
void _dbus_counter_unref(DBusCounter *counter)
Decrements refcount of the counter and possibly finalizes the counter.
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.
DBusCounter * _dbus_counter_ref(DBusCounter *counter)
Increments refcount of the counter.
DBusHandlerResult
Results that a message handler can return.
Definition: dbus-shared.h:69
#define DBUS_PATH_LOCAL
The object path used in local/in-process-generated messages.
Definition: dbus-shared.h:84
#define DBUS_INTERFACE_LOCAL
This is a special interface whose methods can only be invoked by the local implementation (messages f...
Definition: dbus-shared.h:107
#define DBUS_INTERFACE_PEER
The interface supported by most dbus peers.
Definition: dbus-shared.h:101
@ DBUS_HANDLER_RESULT_NEED_MEMORY
Need more memory in order to return DBUS_HANDLER_RESULT_HANDLED or DBUS_HANDLER_RESULT_NOT_YET_HANDLE...
Definition: dbus-shared.h:72
@ DBUS_HANDLER_RESULT_HANDLED
Message has had its effect - no need to run more handlers.
Definition: dbus-shared.h:70
@ DBUS_HANDLER_RESULT_NOT_YET_HANDLED
Message has not had any effect - see if other handlers want it.
Definition: dbus-shared.h:71
dbus_bool_t dbus_type_is_valid(int typecode)
Return TRUE if the argument is a valid typecode.
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:182
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init(), and fills it with the same contents as #_DBUS_STRING_I...
Definition: dbus-string.c:278
dbus_bool_t _dbus_string_append_printf(DBusString *str, const char *format,...)
Appends a printf-style formatted string to the DBusString.
Definition: dbus-string.c:1147
const char * _dbus_string_get_const_data(const DBusString *str)
Gets the raw character buffer from a const string.
Definition: dbus-string.c:513
void _dbus_exit(int code)
Exit the process, returning the given value.
void _dbus_atomic_set_nonzero(DBusAtomic *atomic)
Atomically set the value of an integer to something nonzero.
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
dbus_int32_t _dbus_atomic_get(DBusAtomic *atomic)
Atomically get the value of an integer.
void _dbus_disable_sigpipe(void)
signal (SIGPIPE, SIG_IGN);
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
void _dbus_atomic_set_zero(DBusAtomic *atomic)
Atomically set the value of an integer to 0.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
void _dbus_get_monotonic_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void _dbus_rmutex_new_at_location(DBusRMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
Definition: dbus-threads.c:56
void _dbus_cmutex_free_at_location(DBusCMutex **location_p)
Frees a DBusCMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:110
void _dbus_condvar_free_at_location(DBusCondVar **location_p)
Frees a condition variable; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:227
DBUS_PRIVATE_EXPORT void _dbus_rmutex_unlock(DBusRMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:153
void _dbus_condvar_wait(DBusCondVar *cond, DBusCMutex *mutex)
Atomically unlocks the mutex and waits for the conditions variable to be signalled.
Definition: dbus-threads.c:243
void _dbus_condvar_new_at_location(DBusCondVar **location_p)
This does the same thing as _dbus_condvar_new.
Definition: dbus-threads.c:202
void _dbus_cmutex_new_at_location(DBusCMutex **location_p)
Creates a new mutex or creates a no-op mutex if threads are not initialized.
Definition: dbus-threads.c:80
void _dbus_condvar_wake_one(DBusCondVar *cond)
If there are threads waiting on the condition variable, wake up exactly one.
Definition: dbus-threads.c:281
dbus_bool_t _dbus_condvar_wait_timeout(DBusCondVar *cond, DBusCMutex *mutex, int timeout_milliseconds)
Atomically unlocks the mutex and waits for the conditions variable to be signalled,...
Definition: dbus-threads.c:264
void _dbus_cmutex_lock(DBusCMutex *mutex)
Locks a mutex.
Definition: dbus-threads.c:139
void _dbus_cmutex_unlock(DBusCMutex *mutex)
Unlocks a mutex.
Definition: dbus-threads.c:167
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:97
struct DBusMutex DBusMutex
An opaque mutex type provided by the DBusThreadFunctions implementation installed by dbus_threads_ini...
Definition: dbus-threads.h:43
dbus_bool_t _dbus_timeout_list_add_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Adds a new timeout to the timeout list, invoking the application DBusAddTimeoutFunction if appropriat...
Definition: dbus-timeout.c:314
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Definition: dbus-timeout.c:217
void _dbus_timeout_list_toggle_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout, dbus_bool_t enabled)
Sets a timeout to the given enabled state, invoking the application's DBusTimeoutToggledFunction if a...
Definition: dbus-timeout.c:366
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
Definition: dbus-timeout.c:200
dbus_bool_t _dbus_timeout_list_set_functions(DBusTimeoutList *timeout_list, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions.
Definition: dbus-timeout.c:243
void _dbus_timeout_list_remove_timeout(DBusTimeoutList *timeout_list, DBusTimeout *timeout)
Removes a timeout from the timeout list, invoking the application's DBusRemoveTimeoutFunction if appr...
Definition: dbus-timeout.c:344
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
Definition: dbus-timeout.c:444
void _dbus_transport_set_max_message_size(DBusTransport *transport, long size)
See dbus_connection_set_max_message_size().
void _dbus_transport_set_max_received_size(DBusTransport *transport, long size)
See dbus_connection_set_max_received_size().
DBusDispatchStatus _dbus_transport_get_dispatch_status(DBusTransport *transport)
Reports our current dispatch status (whether there's buffered data to be queued as messages,...
int _dbus_transport_get_pending_fds_count(DBusTransport *transport)
Return how many file descriptors are pending in the loader.
dbus_bool_t _dbus_transport_get_adt_audit_session_data(DBusTransport *transport, void **data, int *data_size)
See dbus_connection_get_adt_audit_session_data().
dbus_bool_t _dbus_transport_get_windows_user(DBusTransport *transport, char **windows_sid_p)
See dbus_connection_get_windows_user().
dbus_bool_t _dbus_transport_queue_messages(DBusTransport *transport)
Processes data we've read while handling a watch, potentially converting some of it to messages and q...
dbus_bool_t _dbus_transport_get_socket_fd(DBusTransport *transport, DBusSocket *fd_p)
Get the socket file descriptor, if any.
const char * _dbus_transport_get_address(DBusTransport *transport)
Gets the address of a transport.
dbus_bool_t _dbus_transport_handle_watch(DBusTransport *transport, DBusWatch *watch, unsigned int condition)
Handles a watch by reading data, writing data, or disconnecting the transport, as appropriate for the...
DBusTransport * _dbus_transport_ref(DBusTransport *transport)
Increments the reference count for the transport.
dbus_bool_t _dbus_transport_peek_is_authenticated(DBusTransport *transport)
Returns TRUE if we have been authenticated.
void _dbus_transport_set_allow_anonymous(DBusTransport *transport, dbus_bool_t value)
See dbus_connection_set_allow_anonymous()
void _dbus_transport_disconnect(DBusTransport *transport)
Closes our end of the connection to a remote application.
long _dbus_transport_get_max_received_size(DBusTransport *transport)
See dbus_connection_get_max_received_size().
DBusCredentials * _dbus_transport_get_credentials(DBusTransport *transport)
If the transport has already been authenticated, return its credentials.
dbus_bool_t _dbus_transport_set_connection(DBusTransport *transport, DBusConnection *connection)
Sets the connection using this transport.
void _dbus_transport_set_unix_user_function(DBusTransport *transport, DBusAllowUnixUserFunction function, void *data, DBusFreeFunction free_data_function, void **old_data, DBusFreeFunction *old_free_data_function)
See dbus_connection_set_unix_user_function().
long _dbus_transport_get_max_message_unix_fds(DBusTransport *transport)
See dbus_connection_get_max_message_unix_fds().
void _dbus_transport_set_max_received_unix_fds(DBusTransport *transport, long n)
See dbus_connection_set_max_received_unix_fds().
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
dbus_bool_t _dbus_transport_can_pass_unix_fd(DBusTransport *transport)
Returns TRUE if the transport supports sending unix fds.
dbus_bool_t _dbus_transport_try_to_authenticate(DBusTransport *transport)
Returns TRUE if we have been authenticated.
void _dbus_transport_do_iteration(DBusTransport *transport, unsigned int flags, int timeout_milliseconds)
Performs a single poll()/select() on the transport's file descriptors and then reads/writes data as a...
long _dbus_transport_get_max_received_unix_fds(DBusTransport *transport)
See dbus_connection_set_max_received_unix_fds().
dbus_bool_t _dbus_transport_get_is_connected(DBusTransport *transport)
Returns TRUE if the transport has not been disconnected.
void _dbus_transport_set_max_message_unix_fds(DBusTransport *transport, long n)
See dbus_connection_set_max_message_unix_fds().
void _dbus_transport_set_pending_fds_function(DBusTransport *transport, void(*callback)(void *), void *data)
Register a function to be called whenever the number of pending file descriptors in the loader change...
void _dbus_transport_set_windows_user_function(DBusTransport *transport, DBusAllowWindowsUserFunction function, void *data, DBusFreeFunction free_data_function, void **old_data, DBusFreeFunction *old_free_data_function)
See dbus_connection_set_windows_user_function().
long _dbus_transport_get_max_message_size(DBusTransport *transport)
See dbus_connection_get_max_message_size().
dbus_bool_t _dbus_transport_get_unix_process_id(DBusTransport *transport, unsigned long *pid)
See dbus_connection_get_unix_process_id().
DBusTransport * _dbus_transport_open(DBusAddressEntry *entry, DBusError *error)
Try to open a new transport for the given address entry.
const char * _dbus_transport_get_server_id(DBusTransport *transport)
Gets the id of the server we are connected to (see dbus_server_get_id()).
dbus_bool_t _dbus_transport_get_is_anonymous(DBusTransport *transport)
See dbus_connection_get_is_anonymous().
dbus_bool_t _dbus_transport_get_unix_user(DBusTransport *transport, unsigned long *uid)
See dbus_connection_get_unix_user().
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:37
int dbus_int32_t
A 32-bit signed integer on all platforms.
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
dbus_bool_t _dbus_watch_list_add_watch(DBusWatchList *watch_list, DBusWatch *watch)
Adds a new watch to the watch list, invoking the application DBusAddWatchFunction if appropriate.
Definition: dbus-watch.c:383
void _dbus_watch_list_toggle_watch(DBusWatchList *watch_list, DBusWatch *watch, dbus_bool_t enabled)
Sets a watch to the given enabled state, invoking the application's DBusWatchToggledFunction if appro...
Definition: dbus-watch.c:443
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:234
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:251
dbus_bool_t _dbus_watch_list_set_functions(DBusWatchList *watch_list, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions.
Definition: dbus-watch.c:297
void _dbus_watch_list_remove_watch(DBusWatchList *watch_list, DBusWatch *watch)
Removes a watch from the watch list, invoking the application's DBusRemoveWatchFunction if appropriat...
Definition: dbus-watch.c:416
Internals of DBusAddressEntry.
Definition: dbus-address.c:49
An atomic integer safe to increment or decrement from multiple threads.
Definition: dbus-sysdeps.h:323
Definition: d