D-Bus  1.8.16
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-unix.h"
27 #include "dbus-server-socket.h"
28 #include "dbus-string.h"
29 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
30 #include "dbus-server-debug-pipe.h"
31 #endif
32 #include "dbus-address.h"
33 #include "dbus-protocol.h"
34 
56 #ifndef _dbus_server_trace_ref
57 void
58 _dbus_server_trace_ref (DBusServer *server,
59  int old_refcount,
60  int new_refcount,
61  const char *why)
62 {
63  static int enabled = -1;
64 
65  _dbus_trace_ref ("DBusServer", server, old_refcount, new_refcount, why,
66  "DBUS_SERVER_TRACE", &enabled);
67 }
68 #endif
69 
70 /* this is a little fragile since it assumes the address doesn't
71  * already have a guid, but it shouldn't
72  */
73 static char*
74 copy_address_with_guid_appended (const DBusString *address,
75  const DBusString *guid_hex)
76 {
77  DBusString with_guid;
78  char *retval;
79 
80  if (!_dbus_string_init (&with_guid))
81  return NULL;
82 
83  if (!_dbus_string_copy (address, 0, &with_guid,
84  _dbus_string_get_length (&with_guid)) ||
85  !_dbus_string_append (&with_guid, ",guid=") ||
86  !_dbus_string_copy (guid_hex, 0,
87  &with_guid, _dbus_string_get_length (&with_guid)))
88  {
89  _dbus_string_free (&with_guid);
90  return NULL;
91  }
92 
93  retval = NULL;
94  _dbus_string_steal_data (&with_guid, &retval);
95 
96  _dbus_string_free (&with_guid);
97 
98  return retval; /* may be NULL if steal_data failed */
99 }
100 
112  const DBusServerVTable *vtable,
113  const DBusString *address)
114 {
115  server->vtable = vtable;
116 
117 #ifdef DBUS_DISABLE_ASSERT
118  _dbus_atomic_inc (&server->refcount);
119 #else
120  {
121  dbus_int32_t old_refcount = _dbus_atomic_inc (&server->refcount);
122 
123  _dbus_assert (old_refcount == 0);
124  }
125 #endif
126 
127  server->address = NULL;
128  server->watches = NULL;
129  server->timeouts = NULL;
130  server->published_address = FALSE;
131 
132  if (!_dbus_string_init (&server->guid_hex))
133  return FALSE;
134 
135  _dbus_generate_uuid (&server->guid);
136 
137  if (!_dbus_uuid_encode (&server->guid, &server->guid_hex))
138  goto failed;
139 
140  server->address = copy_address_with_guid_appended (address,
141  &server->guid_hex);
142  if (server->address == NULL)
143  goto failed;
144 
146  if (server->mutex == NULL)
147  goto failed;
148 
149  server->watches = _dbus_watch_list_new ();
150  if (server->watches == NULL)
151  goto failed;
152 
153  server->timeouts = _dbus_timeout_list_new ();
154  if (server->timeouts == NULL)
155  goto failed;
156 
158 
159  _dbus_verbose ("Initialized server on address %s\n", server->address);
160 
161  return TRUE;
162 
163  failed:
165  server->mutex = NULL;
166  if (server->watches)
167  {
168  _dbus_watch_list_free (server->watches);
169  server->watches = NULL;
170  }
171  if (server->timeouts)
172  {
174  server->timeouts = NULL;
175  }
176  if (server->address)
177  {
178  dbus_free (server->address);
179  server->address = NULL;
180  }
181  _dbus_string_free (&server->guid_hex);
182 
183  return FALSE;
184 }
185 
192 void
194 {
195  /* We don't have the lock, but nobody should be accessing
196  * concurrently since they don't have a ref
197  */
198 #ifndef DBUS_DISABLE_CHECKS
199  _dbus_assert (!server->have_server_lock);
200 #endif
201  _dbus_assert (server->disconnected);
202 
203  /* calls out to application code... */
205 
207 
208  _dbus_watch_list_free (server->watches);
210 
212 
213  dbus_free (server->address);
214 
216 
217  _dbus_string_free (&server->guid_hex);
218 }
219 
220 
223  DBusWatch *watch);
225 typedef void (* DBusWatchRemoveFunction) (DBusWatchList *list,
226  DBusWatch *watch);
228 typedef void (* DBusWatchToggleFunction) (DBusWatchList *list,
229  DBusWatch *watch,
230  dbus_bool_t enabled);
231 
232 static dbus_bool_t
233 protected_change_watch (DBusServer *server,
234  DBusWatch *watch,
235  DBusWatchAddFunction add_function,
236  DBusWatchRemoveFunction remove_function,
237  DBusWatchToggleFunction toggle_function,
238  dbus_bool_t enabled)
239 {
240  DBusWatchList *watches;
241  dbus_bool_t retval;
242 
243  HAVE_LOCK_CHECK (server);
244 
245  /* This isn't really safe or reasonable; a better pattern is the "do
246  * everything, then drop lock and call out" one; but it has to be
247  * propagated up through all callers
248  */
249 
250  watches = server->watches;
251  if (watches)
252  {
253  server->watches = NULL;
254  _dbus_server_ref_unlocked (server);
255  SERVER_UNLOCK (server);
256 
257  if (add_function)
258  retval = (* add_function) (watches, watch);
259  else if (remove_function)
260  {
261  retval = TRUE;
262  (* remove_function) (watches, watch);
263  }
264  else
265  {
266  retval = TRUE;
267  (* toggle_function) (watches, watch, enabled);
268  }
269 
270  SERVER_LOCK (server);
271  server->watches = watches;
273 
274  return retval;
275  }
276  else
277  return FALSE;
278 }
279 
289  DBusWatch *watch)
290 {
291  HAVE_LOCK_CHECK (server);
292  return protected_change_watch (server, watch,
294  NULL, NULL, FALSE);
295 }
296 
303 void
305  DBusWatch *watch)
306 {
307  HAVE_LOCK_CHECK (server);
308  protected_change_watch (server, watch,
309  NULL,
311  NULL, FALSE);
312 }
313 
321 void
323  dbus_bool_t enabled)
324 {
325  _dbus_watch_list_toggle_all_watches (server->watches, enabled);
326 }
327 
330  DBusTimeout *timeout);
333  DBusTimeout *timeout);
336  DBusTimeout *timeout,
337  dbus_bool_t enabled);
338 
339 
340 static dbus_bool_t
341 protected_change_timeout (DBusServer *server,
342  DBusTimeout *timeout,
343  DBusTimeoutAddFunction add_function,
344  DBusTimeoutRemoveFunction remove_function,
345  DBusTimeoutToggleFunction toggle_function,
346  dbus_bool_t enabled)
347 {
348  DBusTimeoutList *timeouts;
349  dbus_bool_t retval;
350 
351  HAVE_LOCK_CHECK (server);
352 
353  /* This isn't really safe or reasonable; a better pattern is the "do everything, then
354  * drop lock and call out" one; but it has to be propagated up through all callers
355  */
356 
357  timeouts = server->timeouts;
358  if (timeouts)
359  {
360  server->timeouts = NULL;
361  _dbus_server_ref_unlocked (server);
362  SERVER_UNLOCK (server);
363 
364  if (add_function)
365  retval = (* add_function) (timeouts, timeout);
366  else if (remove_function)
367  {
368  retval = TRUE;
369  (* remove_function) (timeouts, timeout);
370  }
371  else
372  {
373  retval = TRUE;
374  (* toggle_function) (timeouts, timeout, enabled);
375  }
376 
377  SERVER_LOCK (server);
378  server->timeouts = timeouts;
380 
381  return retval;
382  }
383  else
384  return FALSE;
385 }
386 
398  DBusTimeout *timeout)
399 {
400  return protected_change_timeout (server, timeout,
402  NULL, NULL, FALSE);
403 }
404 
411 void
413  DBusTimeout *timeout)
414 {
415  protected_change_timeout (server, timeout,
416  NULL,
418  NULL, FALSE);
419 }
420 
430 void
432  DBusTimeout *timeout,
433  dbus_bool_t enabled)
434 {
435  protected_change_timeout (server, timeout,
436  NULL, NULL,
438  enabled);
439 }
440 
441 
447 void
449 {
450  dbus_int32_t old_refcount;
451 
452  _dbus_assert (server != NULL);
453  HAVE_LOCK_CHECK (server);
454 
455  old_refcount = _dbus_atomic_inc (&server->refcount);
456  _dbus_assert (old_refcount > 0);
457  _dbus_server_trace_ref (server, old_refcount, old_refcount + 1,
458  "ref_unlocked");
459 }
460 
466 void
468 {
469  dbus_int32_t old_refcount;
470 
471  /* Keep this in sync with dbus_server_unref */
472 
473  _dbus_assert (server != NULL);
474 
475  HAVE_LOCK_CHECK (server);
476 
477  old_refcount = _dbus_atomic_dec (&server->refcount);
478  _dbus_assert (old_refcount > 0);
479 
480  _dbus_server_trace_ref (server, old_refcount, old_refcount - 1,
481  "unref_unlocked");
482 
483  if (old_refcount == 1)
484  {
485  _dbus_assert (server->disconnected);
486 
487  SERVER_UNLOCK (server);
488 
489  _dbus_assert (server->vtable->finalize != NULL);
490 
491  (* server->vtable->finalize) (server);
492  }
493 }
494 
516 static const struct {
517  DBusServerListenResult (* func) (DBusAddressEntry *entry,
518  DBusServer **server_p,
519  DBusError *error);
520 } listen_funcs[] = {
523 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
524  , { _dbus_server_listen_debug_pipe }
525 #endif
526 };
527 
548 DBusServer*
549 dbus_server_listen (const char *address,
550  DBusError *error)
551 {
552  DBusServer *server;
553  DBusAddressEntry **entries;
554  int len, i;
555  DBusError first_connect_error = DBUS_ERROR_INIT;
556  dbus_bool_t handled_once;
557 
558  _dbus_return_val_if_fail (address != NULL, NULL);
559  _dbus_return_val_if_error_is_set (error, NULL);
560 
561  if (!dbus_parse_address (address, &entries, &len, error))
562  return NULL;
563 
564  server = NULL;
565  handled_once = FALSE;
566 
567  for (i = 0; i < len; i++)
568  {
569  int j;
570 
571  for (j = 0; j < (int) _DBUS_N_ELEMENTS (listen_funcs); ++j)
572  {
573  DBusServerListenResult result;
574  DBusError tmp_error = DBUS_ERROR_INIT;
575 
576  result = (* listen_funcs[j].func) (entries[i],
577  &server,
578  &tmp_error);
579 
580  if (result == DBUS_SERVER_LISTEN_OK)
581  {
582  _dbus_assert (server != NULL);
583  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
584  handled_once = TRUE;
585  goto out;
586  }
587  else if (result == DBUS_SERVER_LISTEN_ADDRESS_ALREADY_USED)
588  {
589  _dbus_assert (server == NULL);
590  dbus_set_error (error,
592  "Address '%s' already used",
593  dbus_address_entry_get_method (entries[0]));
594  handled_once = TRUE;
595  goto out;
596  }
597  else if (result == DBUS_SERVER_LISTEN_BAD_ADDRESS)
598  {
599  _dbus_assert (server == NULL);
600  _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
601  dbus_move_error (&tmp_error, error);
602  handled_once = TRUE;
603  goto out;
604  }
605  else if (result == DBUS_SERVER_LISTEN_NOT_HANDLED)
606  {
607  _dbus_assert (server == NULL);
608  _DBUS_ASSERT_ERROR_IS_CLEAR (&tmp_error);
609 
610  /* keep trying addresses */
611  }
612  else if (result == DBUS_SERVER_LISTEN_DID_NOT_CONNECT)
613  {
614  _dbus_assert (server == NULL);
615  _DBUS_ASSERT_ERROR_IS_SET (&tmp_error);
616  if (!dbus_error_is_set (&first_connect_error))
617  dbus_move_error (&tmp_error, &first_connect_error);
618  else
619  dbus_error_free (&tmp_error);
620 
621  handled_once = TRUE;
622 
623  /* keep trying addresses */
624  }
625  }
626 
627  _dbus_assert (server == NULL);
628  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
629  }
630 
631  out:
632 
633  if (!handled_once)
634  {
635  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
636  if (len > 0)
637  dbus_set_error (error,
639  "Unknown address type '%s'",
640  dbus_address_entry_get_method (entries[0]));
641  else
642  dbus_set_error (error,
644  "Empty address '%s'",
645  address);
646  }
647 
648  dbus_address_entries_free (entries);
649 
650  if (server == NULL)
651  {
652  _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error) ||
653  dbus_error_is_set (error));
654 
655  if (error && dbus_error_is_set (error))
656  {
657  /* already set the error */
658  }
659  else
660  {
661  /* didn't set the error but either error should be
662  * NULL or first_connect_error should be set.
663  */
664  _dbus_assert (error == NULL || dbus_error_is_set (&first_connect_error));
665  dbus_move_error (&first_connect_error, error);
666  }
667 
668  _DBUS_ASSERT_ERROR_IS_CLEAR (&first_connect_error); /* be sure we freed it */
669  _DBUS_ASSERT_ERROR_IS_SET (error);
670 
671  return NULL;
672  }
673  else
674  {
675  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
676  return server;
677  }
678 }
679 
686 DBusServer *
688 {
689  dbus_int32_t old_refcount;
690 
691  _dbus_return_val_if_fail (server != NULL, NULL);
692 
693  old_refcount = _dbus_atomic_inc (&server->refcount);
694 
695 #ifndef DBUS_DISABLE_CHECKS
696  if (_DBUS_UNLIKELY (old_refcount <= 0))
697  {
698  _dbus_atomic_dec (&server->refcount);
700  _DBUS_FUNCTION_NAME, "old_refcount > 0",
701  __FILE__, __LINE__);
702  return NULL;
703  }
704 #endif
705 
706  _dbus_server_trace_ref (server, old_refcount, old_refcount + 1, "ref");
707 
708  return server;
709 }
710 
719 void
721 {
722  dbus_int32_t old_refcount;
723 
724  /* keep this in sync with unref_unlocked */
725 
726  _dbus_return_if_fail (server != NULL);
727 
728  old_refcount = _dbus_atomic_dec (&server->refcount);
729 
730 #ifndef DBUS_DISABLE_CHECKS
731  if (_DBUS_UNLIKELY (old_refcount <= 0))
732  {
733  /* undo side-effect first
734  * please do not try to simplify the code here by using
735  * _dbus_atomic_get(), why we don't use it is
736  * because it issues another atomic operation even though
737  * DBUS_DISABLE_CHECKS defined.
738  * Bug: https://bugs.freedesktop.org/show_bug.cgi?id=68303
739  */
740  _dbus_atomic_inc (&server->refcount);
742  _DBUS_FUNCTION_NAME, "old_refcount > 0",
743  __FILE__, __LINE__);
744  return;
745  }
746 #endif
747 
748  _dbus_server_trace_ref (server, old_refcount, old_refcount - 1, "unref");
749 
750  if (old_refcount == 1)
751  {
752  /* lock not held! */
753  _dbus_assert (server->disconnected);
754 
755  _dbus_assert (server->vtable->finalize != NULL);
756 
757  (* server->vtable->finalize) (server);
758  }
759 }
760 
769 void
771 {
772  _dbus_return_if_fail (server != NULL);
773 
774  dbus_server_ref (server);
775  SERVER_LOCK (server);
776 
777  _dbus_assert (server->vtable->disconnect != NULL);
778 
779  if (!server->disconnected)
780  {
781  /* this has to be first so recursive calls to disconnect don't happen */
782  server->disconnected = TRUE;
783 
784  (* server->vtable->disconnect) (server);
785  }
786 
787  SERVER_UNLOCK (server);
788  dbus_server_unref (server);
789 }
790 
798 {
799  dbus_bool_t retval;
800 
801  _dbus_return_val_if_fail (server != NULL, FALSE);
802 
803  SERVER_LOCK (server);
804  retval = !server->disconnected;
805  SERVER_UNLOCK (server);
806 
807  return retval;
808 }
809 
817 char*
819 {
820  char *retval;
821 
822  _dbus_return_val_if_fail (server != NULL, NULL);
823 
824  SERVER_LOCK (server);
825  retval = _dbus_strdup (server->address);
826  SERVER_UNLOCK (server);
827 
828  return retval;
829 }
830 
853 char*
855 {
856  char *retval;
857 
858  _dbus_return_val_if_fail (server != NULL, NULL);
859 
860  SERVER_LOCK (server);
861  retval = NULL;
862  _dbus_string_copy_data (&server->guid_hex, &retval);
863  SERVER_UNLOCK (server);
864 
865  return retval;
866 }
867 
888 void
890  DBusNewConnectionFunction function,
891  void *data,
892  DBusFreeFunction free_data_function)
893 {
894  DBusFreeFunction old_free_function;
895  void *old_data;
896 
897  _dbus_return_if_fail (server != NULL);
898 
899  SERVER_LOCK (server);
900  old_free_function = server->new_connection_free_data_function;
901  old_data = server->new_connection_data;
902 
903  server->new_connection_function = function;
904  server->new_connection_data = data;
905  server->new_connection_free_data_function = free_data_function;
906  SERVER_UNLOCK (server);
907 
908  if (old_free_function != NULL)
909  (* old_free_function) (old_data);
910 }
911 
930  DBusAddWatchFunction add_function,
931  DBusRemoveWatchFunction remove_function,
932  DBusWatchToggledFunction toggled_function,
933  void *data,
934  DBusFreeFunction free_data_function)
935 {
936  dbus_bool_t result;
937  DBusWatchList *watches;
938 
939  _dbus_return_val_if_fail (server != NULL, FALSE);
940 
941  SERVER_LOCK (server);
942  watches = server->watches;
943  server->watches = NULL;
944  if (watches)
945  {
946  SERVER_UNLOCK (server);
947  result = _dbus_watch_list_set_functions (watches,
948  add_function,
949  remove_function,
950  toggled_function,
951  data,
952  free_data_function);
953  SERVER_LOCK (server);
954  }
955  else
956  {
957  _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
958  result = FALSE;
959  }
960  server->watches = watches;
961  SERVER_UNLOCK (server);
962 
963  return result;
964 }
965 
983  DBusAddTimeoutFunction add_function,
984  DBusRemoveTimeoutFunction remove_function,
985  DBusTimeoutToggledFunction toggled_function,
986  void *data,
987  DBusFreeFunction free_data_function)
988 {
989  dbus_bool_t result;
990  DBusTimeoutList *timeouts;
991 
992  _dbus_return_val_if_fail (server != NULL, FALSE);
993 
994  SERVER_LOCK (server);
995  timeouts = server->timeouts;
996  server->timeouts = NULL;
997  if (timeouts)
998  {
999  SERVER_UNLOCK (server);
1000  result = _dbus_timeout_list_set_functions (timeouts,
1001  add_function,
1002  remove_function,
1003  toggled_function,
1004  data,
1005  free_data_function);
1006  SERVER_LOCK (server);
1007  }
1008  else
1009  {
1010  _dbus_warn_check_failed ("Re-entrant call to %s\n", _DBUS_FUNCTION_NAME);
1011  result = FALSE;
1012  }
1013  server->timeouts = timeouts;
1014  SERVER_UNLOCK (server);
1015 
1016  return result;
1017 }
1018 
1034  const char **mechanisms)
1035 {
1036  char **copy;
1037 
1038  _dbus_return_val_if_fail (server != NULL, FALSE);
1039 
1040  SERVER_LOCK (server);
1041 
1042  if (mechanisms != NULL)
1043  {
1044  copy = _dbus_dup_string_array (mechanisms);
1045  if (copy == NULL)
1046  return FALSE;
1047  }
1048  else
1049  copy = NULL;
1050 
1052  server->auth_mechanisms = copy;
1053 
1054  SERVER_UNLOCK (server);
1055 
1056  return TRUE;
1057 }
1058 
1059 static DBusDataSlotAllocator slot_allocator =
1060  _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (server_slots));
1061 
1078 {
1079  return _dbus_data_slot_allocator_alloc (&slot_allocator,
1080  slot_p);
1081 }
1082 
1094 void
1096 {
1097  _dbus_return_if_fail (*slot_p >= 0);
1098 
1099  _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
1100 }
1101 
1117  int slot,
1118  void *data,
1119  DBusFreeFunction free_data_func)
1120 {
1121  DBusFreeFunction old_free_func;
1122  void *old_data;
1123  dbus_bool_t retval;
1124 
1125  _dbus_return_val_if_fail (server != NULL, FALSE);
1126 
1127  SERVER_LOCK (server);
1128 
1129  retval = _dbus_data_slot_list_set (&slot_allocator,
1130  &server->slot_list,
1131  slot, data, free_data_func,
1132  &old_free_func, &old_data);
1133 
1134 
1135  SERVER_UNLOCK (server);
1136 
1137  if (retval)
1138  {
1139  /* Do the actual free outside the server lock */
1140  if (old_free_func)
1141  (* old_free_func) (old_data);
1142  }
1143 
1144  return retval;
1145 }
1146 
1155 void*
1157  int slot)
1158 {
1159  void *res;
1160 
1161  _dbus_return_val_if_fail (server != NULL, NULL);
1162 
1163  SERVER_LOCK (server);
1164 
1165  res = _dbus_data_slot_list_get (&slot_allocator,
1166  &server->slot_list,
1167  slot);
1168 
1169  SERVER_UNLOCK (server);
1170 
1171  return res;
1172 }
1173 
1176 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
1177 #include "dbus-test.h"
1178 #include <string.h>
1179 
1181 _dbus_server_test (void)
1182 {
1183  const char *valid_addresses[] = {
1184  "tcp:port=1234",
1185  "tcp:host=localhost,port=1234",
1186  "tcp:host=localhost,port=1234;tcp:port=5678",
1187 #ifdef DBUS_UNIX
1188  "unix:path=./boogie",
1189  "tcp:port=1234;unix:path=./boogie",
1190 #endif
1191  };
1192 
1193  DBusServer *server;
1194  int i;
1195 
1196  for (i = 0; i < _DBUS_N_ELEMENTS (valid_addresses); i++)
1197  {
1198  DBusError error = DBUS_ERROR_INIT;
1199  char *address;
1200  char *id;
1201 
1202  server = dbus_server_listen (valid_addresses[i], &error);
1203  if (server == NULL)
1204  {
1205  _dbus_warn ("server listen error: %s: %s\n", error.name, error.message);
1206  dbus_error_free (&error);
1207  _dbus_assert_not_reached ("Failed to listen for valid address.");
1208  }
1209 
1210  id = dbus_server_get_id (server);
1211  _dbus_assert (id != NULL);
1212  address = dbus_server_get_address (server);
1213  _dbus_assert (address != NULL);
1214 
1215  if (strstr (address, id) == NULL)
1216  {
1217  _dbus_warn ("server id '%s' is not in the server address '%s'\n",
1218  id, address);
1219  _dbus_assert_not_reached ("bad server id or address");
1220  }
1221 
1222  dbus_free (id);
1223  dbus_free (address);
1224 
1225  dbus_server_disconnect (server);
1226  dbus_server_unref (server);
1227  }
1228 
1229  return TRUE;
1230 }
1231 
1232 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */
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:918
Internals of DBusTimeout.
Definition: dbus-timeout.c:40
void dbus_server_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for server data slots.
Definition: dbus-server.c:1095
dbus_bool_t _dbus_uuid_encode(const DBusGUID *uuid, DBusString *encoded)
Hex-encode a UUID.
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:288
const char * message
public error message field
Definition: dbus-errors.h:51
Implementation of DBusWatch.
Definition: dbus-watch.c:40
#define NULL
A null pointer, defined appropriately for C or C++.
void(* DBusFreeFunction)(void *memory)
The type of a function which frees a block of memory.
Definition: dbus-memory.h:64
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:239
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
void(* disconnect)(DBusServer *server)
Disconnect this server.
DBusWatchList * _dbus_watch_list_new(void)
Creates a new watch list.
Definition: dbus-watch.c:232
unsigned int disconnected
TRUE if we are disconnected.
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_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:1116
void _dbus_timeout_list_free(DBusTimeoutList *timeout_list)
Frees a DBusTimeoutList.
Definition: dbus-timeout.c:211
Internals of DBusServer object.
const char * _dbus_return_if_fail_warning_format
String used in _dbus_return_if_fail macro.
DBusTimeoutList * _dbus_timeout_list_new(void)
Creates a new timeout list.
Definition: dbus-timeout.c:194
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:227
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:376
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...
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
void dbus_address_entries_free(DBusAddressEntry **entries)
Frees a NULL-terminated array of address entries.
Definition: dbus-address.c:189
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:53
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:335
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
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:448
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:1265
void(* DBusNewConnectionFunction)(DBusServer *server, DBusConnection *new_connection, void *data)
Called when a new connection to the server is available.
Definition: dbus-server.h:47
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 ...
DBusNewConnectionFunction new_connection_function
Callback to invoke when a new connection is created.
char ** auth_mechanisms
Array of allowed authentication mechanisms.
void(* DBusRemoveTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus no longer needs a timeout to be monitored by the main loop.
const DBusServerVTable * vtable
Virtual methods for this instance.
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:228
dbus_int32_t _dbus_atomic_dec(DBusAtomic *atomic)
Atomically decrement an integer.
void(* finalize)(DBusServer *server)
The finalize method must free the server.
DBusRMutex * mutex
Lock on the server object.
DBusServer * dbus_server_listen(const char *address, DBusError *error)
Listens for new connections on the given address.
Definition: dbus-server.c:549
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:310
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:431
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:655
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
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:467
DBusAtomic refcount
Reference count.
dbus_bool_t(* DBusAddTimeoutFunction)(DBusTimeout *timeout, void *data)
Called when libdbus needs a new timeout to be monitored by the main loop.
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
dbus_bool_t(* DBusTimeoutAddFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
Definition: dbus-server.c:329
DBusTimeoutList * timeouts
Our timeouts.
DBusServer * dbus_server_ref(DBusServer *server)
Increments the reference count of a DBusServer.
Definition: dbus-server.c:687
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...
Internals of DBusAddressEntry.
Definition: dbus-address.c:43
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:465
void _dbus_server_remove_watch(DBusServer *server, DBusWatch *watch)
Removes a watch previously added with _dbus_server_remove_watch().
Definition: dbus-server.c:304
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:1077
Object representing an exception.
Definition: dbus-errors.h:48
dbus_bool_t _dbus_server_init_base(DBusServer *server, const DBusServerVTable *vtable, const DBusString *address)
Initializes the members of the DBusServer base class.
Definition: dbus-server.c:111
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:889
#define DBUS_ERROR_BAD_ADDRESS
A D-Bus bus address was malformed.
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_server_get_data(DBusServer *server, int slot)
Retrieves data previously set with dbus_server_set_data().
Definition: dbus-server.c:1156
void dbus_server_disconnect(DBusServer *server)
Releases the server's address and stops listening for new clients.
Definition: dbus-server.c:770
dbus_int32_t _dbus_atomic_inc(DBusAtomic *atomic)
Atomically increments an integer.
void _dbus_server_remove_timeout(DBusServer *server, DBusTimeout *timeout)
Removes a timeout previously added with _dbus_server_add_timeout().
Definition: dbus-server.c:412
void _dbus_server_finalize_base(DBusServer *server)
Finalizes the members of the DBusServer base class.
Definition: dbus-server.c:193
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
Virtual table to be implemented by all server "subclasses".
DBusWatchList * watches
Our watches.
void dbus_server_unref(DBusServer *server)
Decrements the reference count of a DBusServer.
Definition: dbus-server.c:720
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:362
#define DBUS_ERROR_ADDRESS_IN_USE
Can't bind a socket since its address is in use (i.e.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
char ** _dbus_dup_string_array(const char **array)
Duplicates a string array.
DBusDataSlotList slot_list
Data stored by allocated integer ID.
void * _dbus_data_slot_list_get(DBusDataSlotAllocator *allocator, DBusDataSlotList *list, int slot)
Retrieves data previously set with _dbus_data_slot_list_set_data().
void(* DBusTimeoutToggledFunction)(DBusTimeout *timeout, void *data)
Called when dbus_timeout_get_enabled() may return a different value than it did before.
#define TRUE
Expands to "1".
void _dbus_data_slot_list_init(DBusDataSlotList *list)
Initializes a slot list.
#define _dbus_assert_not_reached(explanation)
Aborts with an error message if called.
char * address
Address this server is listening on.
dbus_bool_t published_address
flag which indicates that server has published its bus address.
void dbus_move_error(DBusError *src, DBusError *dest)
Moves an error src into dest, freeing src and overwriting dest.
Definition: dbus-errors.c:279
const char * name
public error name field
Definition: dbus-errors.h:50
dbus_bool_t(* DBusAddWatchFunction)(DBusWatch *watch, void *data)
Called when libdbus needs a new watch 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.
DBusFreeFunction new_connection_free_data_function
Callback to invoke to free new_connection_data when server is finalized or data is replaced...
DBusWatchList implementation details.
Definition: dbus-watch.c:214
void * new_connection_data
Data for new connection callback.
An allocator that tracks a set of slot IDs.
Definition: dbus-dataslot.h:55
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:1033
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:797
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...
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:340
void(* DBusTimeoutRemoveFunction)(DBusTimeoutList *list, DBusTimeout *timeout)
Function to be called in protected_change_timeout() with refcount held.
Definition: dbus-server.c:332
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:296
void dbus_free_string_array(char **str_array)
Frees a NULL-terminated array of strings.
Definition: dbus-memory.c:749
void _dbus_rmutex_free_at_location(DBusRMutex **location_p)
Frees a DBusRMutex; does nothing if passed a NULL pointer.
Definition: dbus-threads.c:94
unsigned int have_server_lock
Does someone have the server mutex locked.
DBusString guid_hex
Hex-encoded version of GUID.
dbus_bool_t(* DBusWatchAddFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
Definition: dbus-server.c:222
#define FALSE
Expands to "0".
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:322
void _dbus_generate_uuid(DBusGUID *uuid)
Generates a new UUID.
void(* DBusWatchRemoveFunction)(DBusWatchList *list, DBusWatch *watch)
Function to be called in protected_change_watch() with refcount held.
Definition: dbus-server.c:225
dbus_bool_t dbus_parse_address(const char *address, DBusAddressEntry ***entry, int *array_len, DBusError *error)
Parses an address string of the form:
Definition: dbus-address.c:363
#define _DBUS_LOCK_NAME(name)
Expands to name of a global lock variable.
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:818
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:624
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:929
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:982
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:397
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:69
void _dbus_watch_list_free(DBusWatchList *watch_list)
Frees a DBusWatchList.
Definition: dbus-watch.c:249
int dbus_int32_t
A 32-bit signed integer on all platforms.
char * _dbus_strdup(const char *str)
Duplicates a string.
DBusGUID guid
Globally unique ID of server.
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().
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329
DBusTimeoutList implementation details.
Definition: dbus-timeout.c:176
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:854
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:409
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...