D-Bus 1.15.2
dbus-server.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-server.c DBusServer object
3 *
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
5 *
6 * Licensed under the Academic Free License version 2.1
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24#include <config.h>
25#include "dbus-server.h"
26#include "dbus-server-socket.h"
27#include "dbus-string.h"
28#ifdef DBUS_ENABLE_EMBEDDED_TESTS
29#include "dbus-server-debug-pipe.h"
30#endif
31#include "dbus-address.h"
32#include "dbus-protocol.h"
33
55#ifndef _dbus_server_trace_ref
56void
57_dbus_server_trace_ref (DBusServer *server,
58 int old_refcount,
59 int new_refcount,
60 const char *why)
61{
62 static int enabled = -1;
63
64 _dbus_trace_ref ("DBusServer", server, old_refcount, new_refcount, why,
65 "DBUS_SERVER_TRACE", &enabled);
66}
67#endif
68
69/* this is a little fragile since it assumes the address doesn't
70 * already have a guid, but it shouldn't
71 */
72static char*
73copy_address_with_guid_appended (const DBusString *address,
74 const DBusString *guid_hex)
75{
76 DBusString with_guid;
77 char *retval;
78
79 if (!_dbus_string_init (&with_guid))
80 return NULL;
81
82 if (!_dbus_string_copy (address, 0, &with_guid,
83 _dbus_string_get_length (&with_guid)) ||
84 !_dbus_string_append (&with_guid, ",guid=") ||
85 !_dbus_string_copy (guid_hex, 0,
86 &with_guid, _dbus_string_get_length (&with_guid)))
87 {
88 _dbus_string_free (&with_guid);
89 return NULL;
90 }
91
92 retval = NULL;
93 _dbus_string_steal_data (&with_guid, &retval);
94
95 _dbus_string_free (&with_guid);
96
97 return retval; /* may be NULL if steal_data failed */
98}
99
112 const DBusServerVTable *vtable,
113 const DBusString *address,
114 DBusError *error)
115{
116 server->vtable = vtable;
117
118#ifdef DBUS_DISABLE_ASSERT
119 _dbus_atomic_inc (&server->refcount);
120#else
121 {
122 dbus_int32_t old_refcount = _dbus_atomic_inc (&server->refcount);
123
124 _dbus_assert (old_refcount == 0);
125 }
126#endif
127
128 server->address = NULL;
129 server->watches = NULL;
130 server->timeouts = NULL;
131 server->published_address = FALSE;
132
133 if (!_dbus_string_init (&server->guid_hex))
134 {
135 _DBUS_SET_OOM (error);
136 return FALSE;
137 }
138
139 if (!_dbus_generate_uuid (&server->guid, error))
140 goto failed;
141
142 if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
143 goto oom;
144
145 server->address = copy_address_with_guid_appended (address,
146 &server->guid_hex);
147 if (server->address == NULL)
148 goto oom;
149
151 if (server->mutex == NULL)
152 goto oom;
153
154 server->watches = _dbus_watch_list_new ();
155 if (server->watches == NULL)
156 goto oom;
157
158 server->timeouts = _dbus_timeout_list_new ();
159 if (server->timeouts == NULL)
160 goto oom;
161
163
164 _dbus_verbose ("Initialized server on address %s\n", server->address);
165
166 return TRUE;
167
168 oom:
169 _DBUS_SET_OOM (error);
170 failed:
172 server->mutex = NULL;
173 if (server->watches)
174 {
176 server->watches = NULL;
177 }
178 if (server->timeouts)
179 {
181 server->timeouts = NULL;
182 }
183 if (server->address)
184 {
185 dbus_free (server->address);
186 server->address = NULL;
187 }
188 _dbus_string_free (&server->guid_hex);
189
190 return FALSE;
191}
192
199void
201{
202 /* We don't have the lock, but nobody should be accessing
203 * concurrently since they don't have a ref
204 */
205#ifndef DBUS_DISABLE_CHECKS
207#endif
208 _dbus_assert (server->disconnected);
209
210 /* calls out to application code... */
212
214
217
219
220 dbus_free (server->address);
221
223
224 _dbus_string_free (&server->guid_hex);
225}
226
227
230 DBusWatch *watch);
233 DBusWatch *watch);
236 DBusWatch *watch,
237 dbus_bool_t enabled);
238
239static dbus_bool_t
240protected_change_watch (DBusServer *server,
241 DBusWatch *watch,
242 DBusWatchAddFunction add_function,
243 DBusWatchRemoveFunction remove_function,
244 DBusWatchToggleFunction toggle_function,
245 dbus_bool_t enabled)
246{
247 DBusWatchList *watches;
248 dbus_bool_t retval;
249
250 HAVE_LOCK_CHECK (server);
251
252 /* This isn't really safe or reasonable; a better pattern is the "do
253 * everything, then drop lock and call out" one; but it has to be
254 * propagated up through all callers
255 */
256
257 watches = server->watches;
258 if (watches)
259 {
260 server->watches = NULL;
262 SERVER_UNLOCK (server);
263
264 if (add_function)
265 retval = (* add_function) (watches, watch);
266 else if (remove_function)
267 {
268 retval = TRUE;
269 (* remove_function) (watches, watch);
270 }
271 else
272 {
273 retval = TRUE;
274 (* toggle_function) (watches, watch, enabled);
275 }
276
277 SERVER_LOCK (server);
278 server->watches = watches;
280
281 return retval;
282 }
283 else
284 return FALSE;
285}
286
296 DBusWatch *watch)
297{
298 HAVE_LOCK_CHECK (server);
299 return protected_change_watch (server, watch,
301 NULL, NULL, FALSE);
302}
303
310void
312 DBusWatch *watch)
313{
314 HAVE_LOCK_CHECK (server);
315 protected_change_watch (server, watch,
316 NULL,
318 NULL, FALSE);
319}
320
328void
330 dbus_bool_t enabled)
331{
333}
334
337 DBusTimeout *timeout);
340 DBusTimeout *timeout);
343 DBusTimeout *timeout,
344 dbus_bool_t enabled);
345
346
347static dbus_bool_t
348protected_change_timeout (DBusServer *server,
349 DBusTimeout *timeout,
350 DBusTimeoutAddFunction add_function,
351 DBusTimeoutRemoveFunction remove_function,
352 DBusTimeoutToggleFunction toggle_function,
353 dbus_bool_t enabled)
354{
355 DBusTimeoutList *timeouts;
356 dbus_bool_t retval;
357
358 HAVE_LOCK_CHECK (server);
359
360 /* This isn't really safe or reasonable; a better pattern is the "do everything, then
361 * drop lock and call out" one; but it has to be propagated up through all callers
362 */
363
364 timeouts = server->timeouts;
365 if (timeouts)
366 {
367 server->timeouts = NULL;
369 SERVER_UNLOCK (server);
370
371 if (add_function)
372 retval = (* add_function) (timeouts, timeout);
373 else if (remove_function)
374 {
375 retval = TRUE;
376 (* remove_function) (timeouts, timeout);
377 }
378 else
379 {
380 retval = TRUE;
381 (* toggle_function) (timeouts, timeout, enabled);
382 }
383
384 SERVER_LOCK (server);
385 server->timeouts = timeouts;
387
388 return retval;
389 }
390 else
391 return FALSE;
392}
393
405 DBusTimeout *timeout)
406{
407 return protected_change_timeout (server, timeout,
409 NULL, NULL, FALSE);
410}
411
418void
420 DBusTimeout *timeout)
421{
422 protected_change_timeout (server, timeout,
423 NULL,
425 NULL, FALSE);
426}
427
437void
439 DBusTimeout *timeout,
440 dbus_bool_t enabled)
441{
442 protected_change_timeout (server, timeout,
443 NULL, NULL,
445 enabled);
446}
447
448
454void
456{
457 dbus_int32_t old_refcount;
458
459 _dbus_assert (server != NULL);
460 HAVE_LOCK_CHECK (server);
461
462 old_refcount = _dbus_atomic_inc (&server->refcount);
463 _dbus_assert (old_refcount > 0);
464 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
465 "ref_unlocked");
466}
467
473void
475{
476 dbus_int32_t old_refcount;
477
478 /* Keep this in sync with dbus_server_unref */
479
480 _dbus_assert (server != NULL);
481
482 HAVE_LOCK_CHECK (server);
483
484 old_refcount = _dbus_atomic_dec (&server->refcount);
485 _dbus_assert (old_refcount > 0);
486
487 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
488 "unref_unlocked");
489
490 if (old_refcount == 1)
491 {
492 _dbus_assert (server->disconnected);
493
494 SERVER_UNLOCK (server);
495
496 _dbus_assert (server->vtable->finalize != NULL);
497
498 (* server->vtable->finalize) (server);
499 }
500}
501
523static const struct {
524 DBusServerListenResult (* func) (DBusAddressEntry *entry,
525 DBusServer **server_p,
526 DBusError *error);
527} listen_funcs[] = {
531#ifdef DBUS_ENABLE_EMBEDDED_TESTS
532 , { _dbus_server_listen_debug_pipe }
533#endif
534};
535
557dbus_server_listen (const char *address,
558 DBusError *error)
559{
560 DBusServer *server;
561 DBusAddressEntry **entries;
562 int len, i;
563 DBusError first_connect_error = DBUS_ERROR_INIT;
564 dbus_bool_t handled_once;
565
566 _dbus_return_val_if_fail (address != NULL, NULL);
567 _dbus_return_val_if_error_is_set (error, NULL);
568
569 if (!dbus_parse_address (address, &entries, &len, error))
570 return NULL;
571
572 server = NULL;
573 handled_once = FALSE;
574
575 for (i = 0; i < len; i++)
576 {
577 int j;
578
579 for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
580 {
581 DBusServerListenResult result;
582 DBusError tmp_error = DBUS_ERROR_INIT;
583
584 result = (* listen_funcs[j].func) (entries[i],
585 &server,
586 &tmp_error);
587
588 if (result == DBUS_SERVER_LISTEN_OK)
589 {
590 _dbus_assert (server != NULL);
591 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
592 handled_once = TRUE;
593 goto out;
594 }
595 else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
596 {
597 _dbus_assert (server == NULL);
598 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
599 dbus_set_error (error,
601 "Address '%s' already used",
602 dbus_address_entry_get_method (entries[0]));
603 handled_once = TRUE;
604 goto out;
605 }
606 else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
607 {
608 _dbus_assert (server == NULL);
609 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
610 dbus_move_error (&tmp_error, error);
611 handled_once = TRUE;
612 goto out;
613 }
614 else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
615 {
616 _dbus_assert (server == NULL);
617 _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
618
619 /* keep trying addresses */
620 }
621 else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
622 {
623 _dbus_assert (server == NULL);
624 _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
625 if (!dbus_error_is_set (&first_connect_error))
626 dbus_move_error (&tmp_error, &first_connect_error);
627 else
628 dbus_error_free (&tmp_error);
629
630 handled_once = TRUE;
631
632 /* keep trying addresses */
633 }
634 else
635 {
636 _dbus_assert_not_reached ("Unknown result in dbus_server_listen");
637 }
638 }
639
640 _dbus_assert (server == NULL);
641 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
642 }
643
644 out:
645
646 if (!handled_once)
647 {
648 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
649 if (len > 0)
650 dbus_set_error (error,
652 "Unknown address type '%s'",
653 dbus_address_entry_get_method (entries[0]));
654 else
655 dbus_set_error (error,
657 "Empty address '%s'",
658 address);
659 }
660
662
663 if (server == NULL)
664 {
665 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
666 dbus_error_is_set (error));
667
668 if (error && dbus_error_is_set (error))
669 {
670 /* already set the error */
671 }
672 else
673 {
674 /* didn't set the error but either error should be
675 * NULL or first_connect_error should be set.
676 */
677 _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
678 dbus_move_error (&first_connect_error, error);
679 }
680
681 _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
682 _DBUS_ASSERT_ERROR_IS_SET (error);
683
684 return NULL;
685 }
686 else
687 {
688 dbus_error_free (&first_connect_error);
689 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
690 return server;
691 }
692}
693
702{
703 dbus_int32_t old_refcount;
704
705 _dbus_return_val_if_fail (server != NULL, NULL);
706
707 old_refcount = _dbus_atomic_inc (&server->refcount);
708
709#ifndef DBUS_DISABLE_CHECKS
710 if (_DBUS_UNLIKELY (old_refcount <= 0))
711 {
712 _dbus_atomic_dec (&server->refcount);
713 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME, "old_refcount > 0",
714 __FILE__, __LINE__);
715 return NULL;
716 }
717#endif
718
719 _dbus_server_trace_ref (server, old_refcount, old_refcount + 1, "ref");
720
721 return server;
722}
723
732void
734{
735 dbus_int32_t old_refcount;
736
737 /* keep this in sync with unref_unlocked */
738
739 _dbus_return_if_fail (server != NULL);
740
741 old_refcount = _dbus_atomic_dec (&server->refcount);
742
743#ifndef DBUS_DISABLE_CHECKS
744 if (_DBUS_UNLIKELY (old_refcount <= 0))
745 {
746 /* undo side-effect first
747 * please do not try to simplify the code here by using
748 * _dbus_atomic_get(), why we don't use it is
749 * because it issues another atomic operation even though
750 * DBUS_DISABLE_CHECKS defined.
751 * Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68303
752 */
753 _dbus_atomic_inc (&server->refcount);
754 _dbus_warn_return_if_fail (_DBUS_FUNCTION_NAME, "old_refcount > 0",
755 __FILE__, __LINE__);
756 return;
757 }
758#endif
759
760 _dbus_server_trace_ref (server, old_refcount, old_refcount - 1, "unref");
761
762 if (old_refcount == 1)
763 {
764 /* lock not held! */
765 _dbus_assert (server->disconnected);
766
767 _dbus_assert (server->vtable->finalize != NULL);
768
769 (* server->vtable->finalize) (server);
770 }
771}
772
773void
774_dbus_server_disconnect_unlocked (DBusServer *server)
775{
776 _dbus_assert (server->vtable->disconnect != NULL);
777
778 if (!server->disconnected)
779 {
780 /* this has to be first so recursive calls to disconnect don't happen */
781 server->disconnected = TRUE;
782
783 (* server->vtable->disconnect) (server);
784 }
785}
786
795void
797{
798 _dbus_return_if_fail (server != NULL);
799
800 dbus_server_ref (server);
801 SERVER_LOCK (server);
802
803 _dbus_server_disconnect_unlocked (server);
804
805 SERVER_UNLOCK (server);
806 dbus_server_unref (server);
807}
808
816{
817 dbus_bool_t retval;
818
819 _dbus_return_val_if_fail (server != NULL, FALSE);
820
821 SERVER_LOCK (server);
822 retval = !server->disconnected;
823 SERVER_UNLOCK (server);
824
825 return retval;
826}
827
835char*
837{
838 char *retval;
839
840 _dbus_return_val_if_fail (server != NULL, NULL);
841
842 SERVER_LOCK (server);
843 retval = _dbus_strdup (server->address);
844 SERVER_UNLOCK (server);
845
846 return retval;
847}
848
871char*
873{
874 char *retval;
875
876 _dbus_return_val_if_fail (server != NULL, NULL);
877
878 SERVER_LOCK (server);
879 retval = NULL;
880 _dbus_string_copy_data (&server->guid_hex, &retval);
881 SERVER_UNLOCK (server);
882
883 return retval;
884}
885
906void
909 void *data,
910 DBusFreeFunction free_data_function)
911{
912 DBusFreeFunction old_free_function;
913 void *old_data;
914
915 _dbus_return_if_fail (server != NULL);
916
917 SERVER_LOCK (server);
918 old_free_function = server->new_connection_free_data_function;
919 old_data = server->new_connection_data;
920
921 server->new_connection_function = function;
922 server->new_connection_data = data;
923 server->new_connection_free_data_function = free_data_function;
924 SERVER_UNLOCK (server);
925
926 if (old_free_function != NULL)
927 (* old_free_function) (old_data);
928}
929
948 DBusAddWatchFunction add_function,
949 DBusRemoveWatchFunction remove_function,
950 DBusWatchToggledFunction toggled_function,
951 void *data,
952 DBusFreeFunction free_data_function)
953{
954 dbus_bool_t result;
955 DBusWatchList *watches;
956
957 _dbus_return_val_if_fail (server != NULL, FALSE);
958
959 SERVER_LOCK (server);
960 watches = server->watches;
961 server->watches = NULL;
962 if (watches)
963 {
964 SERVER_UNLOCK (server);
965 result = _dbus_watch_list_set_functions (watches,
966 add_function,
967 remove_function,
968 toggled_function,
969 data,
970 free_data_function);
971 SERVER_LOCK (server);
972 }
973 else
974 {
975 _dbus_warn_check_failed ("Re-entrant call to %s", _DBUS_FUNCTION_NAME);
976 result = FALSE;
977 }
978 server->watches = watches;
979 SERVER_UNLOCK (server);
980
981 return result;
982}
983
1001 DBusAddTimeoutFunction add_function,
1002 DBusRemoveTimeoutFunction remove_function,
1003 DBusTimeoutToggledFunction toggled_function,
1004 void *data,
1005 DBusFreeFunction free_data_function)
1006{
1007 dbus_bool_t result;
1008 DBusTimeoutList *timeouts;
1009
1010 _dbus_return_val_if_fail (server != NULL, FALSE);
1011
1012 SERVER_LOCK (server);
1013 timeouts = server->timeouts;
1014 server->timeouts = NULL;
1015 if (timeouts)
1016 {
1017 SERVER_UNLOCK (server);
1018 result = _dbus_timeout_list_set_functions (timeouts,
1019 add_function,
1020 remove_function,
1021 toggled_function,
1022 data,
1023 free_data_function);
1024 SERVER_LOCK (server);
1025 }
1026 else
1027 {
1028 _dbus_warn_check_failed ("Re-entrant call to %s", _DBUS_FUNCTION_NAME);
1029 result = FALSE;
1030 }
1031 server->timeouts = timeouts;
1032 SERVER_UNLOCK (server);
1033
1034 return result;
1035}
1036
1052 const char **mechanisms)
1053{
1054 char **copy;
1055
1056 _dbus_return_val_if_fail (server != NULL, FALSE);
1057
1058 SERVER_LOCK (server);
1059
1060 if (mechanisms != NULL)
1061 {
1062 copy = _dbus_dup_string_array (mechanisms);
1063 if (copy == NULL)
1064 {
1065 SERVER_UNLOCK (server);
1066 return FALSE;
1067 }
1068 }
1069 else
1070 copy = NULL;
1071
1073 server->auth_mechanisms = copy;
1074
1075 SERVER_UNLOCK (server);
1076
1077 return TRUE;
1078}
1079
1080static DBusDataSlotAllocator slot_allocator =
1081 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (server_slots));
1082
1099{
1100 return _dbus_data_slot_allocator_alloc (&slot_allocator,
1101 slot_p);
1102}
1103
1115void
1117{
1118 _dbus_return_if_fail (*slot_p >= 0);
1119
1120 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1121}
1122
1138 int slot,
1139 void *data,
1140 DBusFreeFunction free_data_func)
1141{
1142 DBusFreeFunction old_free_func;
1143 void *old_data;
1144 dbus_bool_t retval;
1145
1146 _dbus_return_val_if_fail (server != NULL, FALSE);
1147
1148 SERVER_LOCK (server);
1149
1150 retval = _dbus_data_slot_list_set (&slot_allocator,
1151 &server->slot_list,
1152 slot, data, free_data_func,
1153 &old_free_func, &old_data);
1154
1155
1156 SERVER_UNLOCK (server);
1157
1158 if (retval)
1159 {
1160 /* Do the actual free outside the server lock */
1161 if (old_free_func)
1162 (* old_free_func) (old_data);
1163 }
1164
1165 return retval;
1166}
1167
1176void*
1178 int slot)
1179{
1180 void *res;
1181
1182 _dbus_return_val_if_fail (server != NULL, NULL);
1183
1184 SERVER_LOCK (server);
1185
1186 res = _dbus_data_slot_list_get (&slot_allocator,
1187 &server->slot_list,
1188 slot);
1189
1190 SERVER_UNLOCK (server);
1191
1192 return res;
1193}
1194
void dbus_address_entries_free(DBusAddressEntry **entries)
Frees a NULL-terminated array of address entries.
Definition: dbus-address.c:192
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:366
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:230
void(* DBusWatchToggledFunction)(DBusWatch *watch, void *data)
Called when dbus_watch_get_enabled() may return a different value than it did before.
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch to be monitored by the main loop.
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
void(* DBusRemoveWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus no longer needs a watch to be monitored by the main loop.
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
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:70
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:279
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:354
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:211
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
#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.
dbus_bool_t _dbus_generate_uuid(DBusGUID *uuid, DBusError *error)
Generates a new UUID.
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.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:63
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:740
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
dbus_bool_t _dbus_server_add_watch(DBusServer *server, DBusWatch *watch)
Adds a watch for this server, chaining out to application-provided watch handlers.
Definition: dbus-server.c:295
void(* DBusWatchRemoveFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
Definition: dbus-server.c:232
void _dbus_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
Definition: dbus-server.c:311
void _dbus_server_remove_timeout(DBusServer *server, DBusTimeout *timeout)
Removes a timeout previously added with _dbus_server_add_timeout().
Definition: dbus-server.c:419
void _dbus_server_unref_unlocked(DBusServer *server)
Like dbus_server_unref() but does not acquire the lock (must already be held)
Definition: dbus-server.c:474
void _dbus_server_toggle_timeout(DBusServer *server, DBusTimeout *timeout, dbus_bool_t enabled)
Toggles a timeout and notifies app via server's DBusTimeoutToggledFunction if available.
Definition: dbus-server.c:438
void(* DBusTimeoutToggleFunction)(DBusTimeoutList *list, DBusTimeout *timeout, dbus_bool_t enabled)
Function to be called in protected_change_timeout() with refcount held.
Definition: dbus-server.c:342
void(* DBusTimeoutRemoveFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
Definition: dbus-server.c:339
dbus_bool_t _dbus_server_add_timeout(DBusServer *server, DBusTimeout *timeout)
Adds a timeout for this server, chaining out to application-provided timeout handlers.
Definition: dbus-server.c:404
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address, DBusError *error)
Initializes the members of the DBusServer base class.
Definition: dbus-server.c:111
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
Definition: dbus-server.c:200
void _dbus_server_ref_unlocked(DBusServer *server)
Like dbus_server_ref() but does not acquire the lock (must already be held)
Definition: dbus-server.c:455
dbus_bool_t(* DBusTimeoutAddFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
Definition: dbus-server.c:336
void(* DBusWatchToggleFunction)(DBusWatchList *list, DBusWatch *watch, dbus_bool_t enabled)
Function to be called in protected_change_watch() with refcount held.
Definition: dbus-server.c:235
void _dbus_server_toggle_all_watches(DBusServer *server, dbus_bool_t enabled)
Toggles all watch and notifies app via server's DBusWatchToggledFunction if available.
Definition: dbus-server.c:329
dbus_bool_t(* DBusWatchAddFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
Definition: dbus-server.c:229
DBusServerListenResult _dbus_server_listen_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for various socket-related addresses (well, currently only tcp a...
DBusServerListenResult _dbus_server_listen_unix_socket(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry for UNIX socket addresses.
DBusServerListenResult _dbus_server_listen_platform_specific(DBusAddressEntry *entry, DBusServer **server_p, DBusError *error)
Tries to interpret the address entry in a platform-specific way, creating a platform-specific server ...
dbus_bool_t dbus_server_allocate_data_slot(dbus_int32_t *slot_p)
Allocates an integer ID to be used for storing application-specific data on any DBusServer.
Definition: dbus-server.c:1098
void dbus_server_disconnect(DBusServer *server)
Releases the server's address and stops listening for new clients.
Definition: dbus-server.c:796
dbus_bool_t dbus_server_set_auth_mechanisms(DBusServer *server, const char **mechanisms)
Sets the authentication mechanisms that this server offers to clients, as a NULL-terminated array of ...
Definition: dbus-server.c:1051
char * dbus_server_get_id(DBusServer *server)
Returns the unique ID of the server, as a newly-allocated string which must be freed by the caller.
Definition: dbus-server.c:872
DBusServer * dbus_server_listen(const char *address, DBusError *error)
Listens for new connections on the given address.
Definition: dbus-server.c:557
char * dbus_server_get_address(DBusServer *server)
Returns the address of the server, as a newly-allocated string which must be freed by the caller.
Definition: dbus-server.c:836
dbus_bool_t dbus_server_get_is_connected(DBusServer *server)
Returns TRUE if the server is still listening for new connections.
Definition: dbus-server.c:815
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
Definition: dbus-server.c:733
void dbus_server_set_new_connection_function(DBusServer *server, DBusNewConnectionFunction function, void *data, DBusFreeFunction free_data_function)
Sets a function to be used for handling new connections.
Definition: dbus-server.c:907
dbus_bool_t dbus_server_set_watch_functions(DBusServer *server, DBusAddWatchFunction add_function, DBusRemoveWatchFunction remove_function, DBusWatchToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the watch functions for the server.
Definition: dbus-server.c:947
dbus_bool_t dbus_server_set_data(DBusServer *server, int slot, void *data, DBusFreeFunction free_data_func)
Stores a pointer on a DBusServer, along with an optional function to be used for freeing the data whe...
Definition: dbus-server.c:1137
DBusServer * dbus_server_ref(DBusServer *server)
Increments the reference count of a DBusServer.
Definition: dbus-server.c:701
void * dbus_server_get_data(DBusServer *server, int slot)
Retrieves data previously set with dbus_server_set_data().
Definition: dbus-server.c:1177
void dbus_server_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for server data slots.
Definition: dbus-server.c:1116
dbus_bool_t dbus_server_set_timeout_functions(DBusServer *server, DBusAddTimeoutFunction add_function, DBusRemoveTimeoutFunction remove_function, DBusTimeoutToggledFunction toggled_function, void *data, DBusFreeFunction free_data_function)
Sets the timeout functions for the server.
Definition: dbus-server.c:1000
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
Definition: dbus-server.h:48
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:978
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:180
dbus_bool_t _dbus_string_copy(const DBusString *source, int start, DBusString *dest, int insert_at)
Like _dbus_string_move(), but does not delete the section of the source string that's copied to the d...
Definition: dbus-string.c:1343
dbus_bool_t _dbus_string_steal_data(DBusString *str, char **data_return)
Like _dbus_string_get_data(), but removes the gotten data from the original string.
Definition: dbus-string.c:684
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:276
dbus_bool_t _dbus_string_copy_data(const DBusString *str, char **data_return)
Copies the data from the string into a char*.
Definition: dbus-string.c:715
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:782
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
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:54
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:95
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:312
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Definition: dbus-timeout.c:215
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:364
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
Definition: dbus-timeout.c:198
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:241
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:342
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
int dbus_int32_t
A 32-bit signed 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:381
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:232
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:249
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:295
void _dbus_watch_list_toggle_all_watches(DBusWatchList *watch_list, dbus_bool_t enabled)
Sets all watches to the given enabled state, invoking the application's DBusWatchToggledFunction if a...
Definition: dbus-watch.c:472
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:414
Internals of DBusAddressEntry.
Definition: dbus-address.c:47
An allocator that tracks a set of slot IDs.
Definition: dbus-dataslot.h:56
Object representing an exception.
Definition: dbus-errors.h:49
Virtual table to be implemented by all server "subclasses".
void(* disconnect)(DBusServer *server)
Disconnect this server.
void(* finalize)(DBusServer *server)
The finalize method must free the server.
Internals of DBusServer object.
dbus_bool_t published_address
flag which indicates that server has published its bus address.
DBusDataSlotList slot_list
Data stored by allocated integer ID.
char * address
Address this server is listening on.
DBusFreeFunction new_connection_free_data_function
Callback to invoke to free new_connection_data when server is finalized or data is replaced.
DBusAtomic refcount
Reference count.
DBusWatchList * watches
Our watches.
DBusGUID guid
Globally unique ID of server.
DBusString guid_hex
Hex-encoded version of GUID.
unsigned int disconnected
TRUE if we are disconnected.
DBusRMutex * mutex
Lock on the server object.
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
const DBusServerVTable * vtable
Virtual methods for this instance.
void * new_connection_data
Data for new connection callback.
unsigned int have_server_lock
Does someone have the server mutex locked.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
DBusTimeoutList * timeouts
Our timeouts.
DBusTimeoutList implementation details.
Definition: dbus-timeout.c:181
Internals of DBusTimeout.
Definition: dbus-timeout.c:41
DBusWatchList implementation details.
Definition: dbus-watch.c:215
Implementation of DBusWatch.
Definition: dbus-watch.c:41