D-Bus  1.6.12
dbus-bus.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-bus.c Convenience functions for communicating with the bus.
3  *
4  * Copyright (C) 2003 CodeFactory AB
5  * Copyright (C) 2003 Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #include <config.h>
26 #include "dbus-bus.h"
27 #include "dbus-protocol.h"
28 #include "dbus-internals.h"
29 #include "dbus-message.h"
30 #include "dbus-marshal-validate.h"
31 #include "dbus-threads-internal.h"
32 #include "dbus-connection-internal.h"
33 #include "dbus-string.h"
34 
76 typedef struct
77 {
79  char *unique_name;
81  unsigned int is_well_known : 1;
82 } BusData;
83 
86 static dbus_int32_t bus_data_slot = -1;
87 
89 #define N_BUS_TYPES 3
90 
91 static DBusConnection *bus_connections[N_BUS_TYPES];
92 static char *bus_connection_addresses[N_BUS_TYPES] = { NULL, NULL, NULL };
93 
94 static DBusBusType activation_bus_type = DBUS_BUS_STARTER;
95 
96 static dbus_bool_t initialized = FALSE;
97 
102 
109 _DBUS_DEFINE_GLOBAL_LOCK (bus_datas);
110 
111 static void
112 addresses_shutdown_func (void *data)
113 {
114  int i;
115 
116  i = 0;
117  while (i < N_BUS_TYPES)
118  {
119  if (bus_connections[i] != NULL)
120  _dbus_warn_check_failed ("dbus_shutdown() called but connections were still live. This probably means the application did not drop all its references to bus connections.\n");
121 
122  dbus_free (bus_connection_addresses[i]);
123  bus_connection_addresses[i] = NULL;
124  ++i;
125  }
126 
127  activation_bus_type = DBUS_BUS_STARTER;
128 
129  initialized = FALSE;
130 }
131 
132 static dbus_bool_t
133 get_from_env (char **connection_p,
134  const char *env_var)
135 {
136  const char *s;
137 
138  _dbus_assert (*connection_p == NULL);
139 
140  s = _dbus_getenv (env_var);
141  if (s == NULL || *s == '\0')
142  return TRUE; /* successfully didn't use the env var */
143  else
144  {
145  *connection_p = _dbus_strdup (s);
146  return *connection_p != NULL;
147  }
148 }
149 
150 static dbus_bool_t
151 init_session_address (void)
152 {
153  dbus_bool_t retval;
154 
155  retval = FALSE;
156 
157  /* First, look in the environment. This is the normal case on
158  * freedesktop.org/Unix systems. */
159  get_from_env (&bus_connection_addresses[DBUS_BUS_SESSION],
160  "DBUS_SESSION_BUS_ADDRESS");
161  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
162  {
163  dbus_bool_t supported;
164  DBusString addr;
165  DBusError error = DBUS_ERROR_INIT;
166 
167  if (!_dbus_string_init (&addr))
168  return FALSE;
169 
170  supported = FALSE;
171  /* So it's not in the environment - let's try a platform-specific method.
172  * On MacOS, this involves asking launchd. On Windows (not specified yet)
173  * we might do a COM lookup.
174  * Ignore errors - if we failed, fall back to autolaunch. */
175  retval = _dbus_lookup_session_address (&supported, &addr, &error);
176  if (supported && retval)
177  {
178  retval =_dbus_string_steal_data (&addr, &bus_connection_addresses[DBUS_BUS_SESSION]);
179  }
180  else if (supported && !retval)
181  {
182  if (dbus_error_is_set(&error))
183  _dbus_warn ("Dynamic session lookup supported but failed: %s\n", error.message);
184  else
185  _dbus_warn ("Dynamic session lookup supported but failed silently\n");
186  }
187  _dbus_string_free (&addr);
188  }
189  else
190  retval = TRUE;
191 
192  if (!retval)
193  return FALSE;
194 
195  /* The DBUS_SESSION_BUS_DEFAULT_ADDRESS should have really been named
196  * DBUS_SESSION_BUS_FALLBACK_ADDRESS.
197  */
198  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
199  bus_connection_addresses[DBUS_BUS_SESSION] =
200  _dbus_strdup (DBUS_SESSION_BUS_DEFAULT_ADDRESS);
201  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
202  return FALSE;
203 
204  return TRUE;
205 }
206 
207 static dbus_bool_t
208 init_connections_unlocked (void)
209 {
210  if (!initialized)
211  {
212  const char *s;
213  int i;
214 
215  i = 0;
216  while (i < N_BUS_TYPES)
217  {
218  bus_connections[i] = NULL;
219  ++i;
220  }
221 
222  /* Don't init these twice, we may run this code twice if
223  * init_connections_unlocked() fails midway through.
224  * In practice, each block below should contain only one
225  * "return FALSE" or running through twice may not
226  * work right.
227  */
228 
229  if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
230  {
231  _dbus_verbose ("Filling in system bus address...\n");
232 
233  if (!get_from_env (&bus_connection_addresses[DBUS_BUS_SYSTEM],
234  "DBUS_SYSTEM_BUS_ADDRESS"))
235  return FALSE;
236  }
237 
238 
239  if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
240  {
241  /* Use default system bus address if none set in environment */
242  bus_connection_addresses[DBUS_BUS_SYSTEM] =
243  _dbus_strdup (DBUS_SYSTEM_BUS_DEFAULT_ADDRESS);
244 
245  if (bus_connection_addresses[DBUS_BUS_SYSTEM] == NULL)
246  return FALSE;
247 
248  _dbus_verbose (" used default system bus \"%s\"\n",
249  bus_connection_addresses[DBUS_BUS_SYSTEM]);
250  }
251  else
252  _dbus_verbose (" used env var system bus \"%s\"\n",
253  bus_connection_addresses[DBUS_BUS_SYSTEM]);
254 
255  if (bus_connection_addresses[DBUS_BUS_SESSION] == NULL)
256  {
257  _dbus_verbose ("Filling in session bus address...\n");
258 
259  if (!init_session_address ())
260  return FALSE;
261 
262  _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_SESSION] ?
263  bus_connection_addresses[DBUS_BUS_SESSION] : "none set");
264  }
265 
266  if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
267  {
268  _dbus_verbose ("Filling in activation bus address...\n");
269 
270  if (!get_from_env (&bus_connection_addresses[DBUS_BUS_STARTER],
271  "DBUS_STARTER_ADDRESS"))
272  return FALSE;
273 
274  _dbus_verbose (" \"%s\"\n", bus_connection_addresses[DBUS_BUS_STARTER] ?
275  bus_connection_addresses[DBUS_BUS_STARTER] : "none set");
276  }
277 
278 
279  if (bus_connection_addresses[DBUS_BUS_STARTER] != NULL)
280  {
281  s = _dbus_getenv ("DBUS_STARTER_BUS_TYPE");
282 
283  if (s != NULL)
284  {
285  _dbus_verbose ("Bus activation type was set to \"%s\"\n", s);
286 
287  if (strcmp (s, "system") == 0)
288  activation_bus_type = DBUS_BUS_SYSTEM;
289  else if (strcmp (s, "session") == 0)
290  activation_bus_type = DBUS_BUS_SESSION;
291  }
292  }
293  else
294  {
295  /* Default to the session bus instead if available */
296  if (bus_connection_addresses[DBUS_BUS_SESSION] != NULL)
297  {
298  bus_connection_addresses[DBUS_BUS_STARTER] =
299  _dbus_strdup (bus_connection_addresses[DBUS_BUS_SESSION]);
300  if (bus_connection_addresses[DBUS_BUS_STARTER] == NULL)
301  return FALSE;
302  }
303  }
304 
305  /* If we return FALSE we have to be sure that restarting
306  * the above code will work right
307  */
308 
309  if (!_dbus_setenv ("DBUS_ACTIVATION_ADDRESS", NULL))
310  return FALSE;
311 
312  if (!_dbus_setenv ("DBUS_ACTIVATION_BUS_TYPE", NULL))
313  return FALSE;
314 
315  if (!_dbus_register_shutdown_func (addresses_shutdown_func,
316  NULL))
317  return FALSE;
318 
319  initialized = TRUE;
320  }
321 
322  return initialized;
323 }
324 
325 static void
326 bus_data_free (void *data)
327 {
328  BusData *bd = data;
329 
330  if (bd->is_well_known)
331  {
332  int i;
333  _DBUS_LOCK (bus);
334  /* We may be stored in more than one slot */
335  /* This should now be impossible - these slots are supposed to
336  * be cleared on disconnect, so should not need to be cleared on
337  * finalize
338  */
339  i = 0;
340  while (i < N_BUS_TYPES)
341  {
342  if (bus_connections[i] == bd->connection)
343  bus_connections[i] = NULL;
344 
345  ++i;
346  }
347  _DBUS_UNLOCK (bus);
348  }
349 
350  dbus_free (bd->unique_name);
351  dbus_free (bd);
352 
353  dbus_connection_free_data_slot (&bus_data_slot);
354 }
355 
356 static BusData*
357 ensure_bus_data (DBusConnection *connection)
358 {
359  BusData *bd;
360 
361  if (!dbus_connection_allocate_data_slot (&bus_data_slot))
362  return NULL;
363 
364  bd = dbus_connection_get_data (connection, bus_data_slot);
365  if (bd == NULL)
366  {
367  bd = dbus_new0 (BusData, 1);
368  if (bd == NULL)
369  {
370  dbus_connection_free_data_slot (&bus_data_slot);
371  return NULL;
372  }
373 
374  bd->connection = connection;
375 
376  if (!dbus_connection_set_data (connection, bus_data_slot, bd,
377  bus_data_free))
378  {
379  dbus_free (bd);
380  dbus_connection_free_data_slot (&bus_data_slot);
381  return NULL;
382  }
383 
384  /* Data slot refcount now held by the BusData */
385  }
386  else
387  {
388  dbus_connection_free_data_slot (&bus_data_slot);
389  }
390 
391  return bd;
392 }
393 
400 void
402 {
403  int i;
404 
405  _DBUS_LOCK (bus);
406 
407  /* We are expecting to have the connection saved in only one of these
408  * slots, but someone could in a pathological case set system and session
409  * bus to the same bus or something. Or set one of them to the starter
410  * bus without setting the starter bus type in the env variable.
411  * So we don't break the loop as soon as we find a match.
412  */
413  for (i = 0; i < N_BUS_TYPES; ++i)
414  {
415  if (bus_connections[i] == connection)
416  {
417  bus_connections[i] = NULL;
418  }
419  }
420 
421  _DBUS_UNLOCK (bus);
422 }
423 
424 static DBusConnection *
425 internal_bus_get (DBusBusType type,
426  dbus_bool_t private,
427  DBusError *error)
428 {
429  const char *address;
430  DBusConnection *connection;
431  BusData *bd;
432  DBusBusType address_type;
433 
434  _dbus_return_val_if_fail (type >= 0 && type < N_BUS_TYPES, NULL);
435  _dbus_return_val_if_error_is_set (error, NULL);
436 
437  connection = NULL;
438 
439  _DBUS_LOCK (bus);
440 
441  if (!init_connections_unlocked ())
442  {
443  _DBUS_SET_OOM (error);
444  goto out;
445  }
446 
447  /* We want to use the activation address even if the
448  * activating bus is the session or system bus,
449  * per the spec.
450  */
451  address_type = type;
452 
453  /* Use the real type of the activation bus for getting its
454  * connection, but only if the real type's address is available. (If
455  * the activating bus isn't a well-known bus then
456  * activation_bus_type == DBUS_BUS_STARTER)
457  */
458  if (type == DBUS_BUS_STARTER &&
459  bus_connection_addresses[activation_bus_type] != NULL)
460  type = activation_bus_type;
461 
462  if (!private && bus_connections[type] != NULL)
463  {
464  connection = bus_connections[type];
465  dbus_connection_ref (connection);
466  goto out;
467  }
468 
469  address = bus_connection_addresses[address_type];
470  if (address == NULL)
471  {
473  "Unable to determine the address of the message bus (try 'man dbus-launch' and 'man dbus-daemon' for help)");
474  goto out;
475  }
476 
477  if (private)
478  connection = dbus_connection_open_private (address, error);
479  else
480  connection = dbus_connection_open (address, error);
481 
482  if (!connection)
483  {
484  goto out;
485  }
486 
487  if (!dbus_bus_register (connection, error))
488  {
490  dbus_connection_unref (connection);
491  connection = NULL;
492  goto out;
493  }
494 
495  if (!private)
496  {
497  /* store a weak ref to the connection (dbus-connection.c is
498  * supposed to have a strong ref that it drops on disconnect,
499  * since this is a shared connection)
500  */
501  bus_connections[type] = connection;
502  }
503 
504  /* By default we're bound to the lifecycle of
505  * the message bus.
506  */
508  TRUE);
509 
510  _DBUS_LOCK (bus_datas);
511  bd = ensure_bus_data (connection);
512  _dbus_assert (bd != NULL); /* it should have been created on
513  register, so OOM not possible */
514  bd->is_well_known = TRUE;
515  _DBUS_UNLOCK (bus_datas);
516 
517 out:
518  /* Return a reference to the caller, or NULL with error set. */
519  if (connection == NULL)
520  _DBUS_ASSERT_ERROR_IS_SET (error);
521 
522  _DBUS_UNLOCK (bus);
523  return connection;
524 }
525 
526  /* end of implementation details docs */
528 
561  DBusError *error)
562 {
563  return internal_bus_get (type, FALSE, error);
564 }
565 
593  DBusError *error)
594 {
595  return internal_bus_get (type, TRUE, error);
596 }
597 
649  DBusError *error)
650 {
651  DBusMessage *message, *reply;
652  char *name;
653  BusData *bd;
654  dbus_bool_t retval;
655 
656  _dbus_return_val_if_fail (connection != NULL, FALSE);
657  _dbus_return_val_if_error_is_set (error, FALSE);
658 
659  retval = FALSE;
660  message = NULL;
661  reply = NULL;
662 
663  _DBUS_LOCK (bus_datas);
664 
665  bd = ensure_bus_data (connection);
666  if (bd == NULL)
667  {
668  _DBUS_SET_OOM (error);
669  goto out;
670  }
671 
672  if (bd->unique_name != NULL)
673  {
674  _dbus_verbose ("Ignoring attempt to register the same DBusConnection %s with the message bus a second time.\n",
675  bd->unique_name);
676  /* Success! */
677  retval = TRUE;
678  goto out;
679  }
680 
684  "Hello");
685 
686  if (!message)
687  {
688  _DBUS_SET_OOM (error);
689  goto out;
690  }
691 
692  reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
693 
694  if (reply == NULL)
695  goto out;
696  else if (dbus_set_error_from_message (error, reply))
697  goto out;
698  else if (!dbus_message_get_args (reply, error,
699  DBUS_TYPE_STRING, &name,
701  goto out;
702 
703  bd->unique_name = _dbus_strdup (name);
704  if (bd->unique_name == NULL)
705  {
706  _DBUS_SET_OOM (error);
707  goto out;
708  }
709 
710  retval = TRUE;
711 
712  out:
713  _DBUS_UNLOCK (bus_datas);
714 
715  if (message)
716  dbus_message_unref (message);
717 
718  if (reply)
719  dbus_message_unref (reply);
720 
721  if (!retval)
722  _DBUS_ASSERT_ERROR_IS_SET (error);
723 
724  return retval;
725 }
726 
727 
764  const char *unique_name)
765 {
766  BusData *bd;
767  dbus_bool_t success = FALSE;
768 
769  _dbus_return_val_if_fail (connection != NULL, FALSE);
770  _dbus_return_val_if_fail (unique_name != NULL, FALSE);
771 
772  _DBUS_LOCK (bus_datas);
773 
774  bd = ensure_bus_data (connection);
775  if (bd == NULL)
776  goto out;
777 
778  _dbus_assert (bd->unique_name == NULL);
779 
780  bd->unique_name = _dbus_strdup (unique_name);
781  success = bd->unique_name != NULL;
782 
783 out:
784  _DBUS_UNLOCK (bus_datas);
785 
786  return success;
787 }
788 
807 const char*
809 {
810  BusData *bd;
811  const char *unique_name = NULL;
812 
813  _dbus_return_val_if_fail (connection != NULL, NULL);
814 
815  _DBUS_LOCK (bus_datas);
816 
817  bd = ensure_bus_data (connection);
818  if (bd == NULL)
819  goto out;
820 
821  unique_name = bd->unique_name;
822 
823 out:
824  _DBUS_UNLOCK (bus_datas);
825 
826  return unique_name;
827 }
828 
852 unsigned long
854  const char *name,
855  DBusError *error)
856 {
857  DBusMessage *message, *reply;
858  dbus_uint32_t uid;
859 
860  _dbus_return_val_if_fail (connection != NULL, DBUS_UID_UNSET);
861  _dbus_return_val_if_fail (name != NULL, DBUS_UID_UNSET);
862  _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), DBUS_UID_UNSET);
863  _dbus_return_val_if_error_is_set (error, DBUS_UID_UNSET);
864 
868  "GetConnectionUnixUser");
869 
870  if (message == NULL)
871  {
872  _DBUS_SET_OOM (error);
873  return DBUS_UID_UNSET;
874  }
875 
876  if (!dbus_message_append_args (message,
877  DBUS_TYPE_STRING, &name,
879  {
880  dbus_message_unref (message);
881  _DBUS_SET_OOM (error);
882  return DBUS_UID_UNSET;
883  }
884 
885  reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
886  error);
887 
888  dbus_message_unref (message);
889 
890  if (reply == NULL)
891  {
892  _DBUS_ASSERT_ERROR_IS_SET (error);
893  return DBUS_UID_UNSET;
894  }
895 
896  if (dbus_set_error_from_message (error, reply))
897  {
898  _DBUS_ASSERT_ERROR_IS_SET (error);
899  dbus_message_unref (reply);
900  return DBUS_UID_UNSET;
901  }
902 
903  if (!dbus_message_get_args (reply, error,
904  DBUS_TYPE_UINT32, &uid,
906  {
907  _DBUS_ASSERT_ERROR_IS_SET (error);
908  dbus_message_unref (reply);
909  return DBUS_UID_UNSET;
910  }
911 
912  dbus_message_unref (reply);
913 
914  return (unsigned long) uid;
915 }
916 
935 char*
937  DBusError *error)
938 {
939  DBusMessage *message, *reply;
940  char *id;
941  const char *v_STRING;
942 
943  _dbus_return_val_if_fail (connection != NULL, NULL);
944  _dbus_return_val_if_error_is_set (error, NULL);
945 
949  "GetId");
950 
951  if (message == NULL)
952  {
953  _DBUS_SET_OOM (error);
954  return NULL;
955  }
956 
957  reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
958  error);
959 
960  dbus_message_unref (message);
961 
962  if (reply == NULL)
963  {
964  _DBUS_ASSERT_ERROR_IS_SET (error);
965  return NULL;
966  }
967 
968  if (dbus_set_error_from_message (error, reply))
969  {
970  _DBUS_ASSERT_ERROR_IS_SET (error);
971  dbus_message_unref (reply);
972  return NULL;
973  }
974 
975  v_STRING = NULL;
976  if (!dbus_message_get_args (reply, error,
977  DBUS_TYPE_STRING, &v_STRING,
979  {
980  _DBUS_ASSERT_ERROR_IS_SET (error);
981  dbus_message_unref (reply);
982  return NULL;
983  }
984 
985  id = _dbus_strdup (v_STRING); /* may be NULL */
986 
987  dbus_message_unref (reply);
988 
989  if (id == NULL)
990  _DBUS_SET_OOM (error);
991 
992  /* FIXME it might be nice to cache the ID locally */
993 
994  return id;
995 }
996 
1099 int
1101  const char *name,
1102  unsigned int flags,
1103  DBusError *error)
1104 {
1105  DBusMessage *message, *reply;
1106  dbus_uint32_t result;
1107 
1108  _dbus_return_val_if_fail (connection != NULL, 0);
1109  _dbus_return_val_if_fail (name != NULL, 0);
1110  _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1111  _dbus_return_val_if_error_is_set (error, 0);
1112 
1116  "RequestName");
1117 
1118  if (message == NULL)
1119  {
1120  _DBUS_SET_OOM (error);
1121  return -1;
1122  }
1123 
1124  if (!dbus_message_append_args (message,
1125  DBUS_TYPE_STRING, &name,
1126  DBUS_TYPE_UINT32, &flags,
1128  {
1129  dbus_message_unref (message);
1130  _DBUS_SET_OOM (error);
1131  return -1;
1132  }
1133 
1134  reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1135  error);
1136 
1137  dbus_message_unref (message);
1138 
1139  if (reply == NULL)
1140  {
1141  _DBUS_ASSERT_ERROR_IS_SET (error);
1142  return -1;
1143  }
1144 
1145  if (dbus_set_error_from_message (error, reply))
1146  {
1147  _DBUS_ASSERT_ERROR_IS_SET (error);
1148  dbus_message_unref (reply);
1149  return -1;
1150  }
1151 
1152  if (!dbus_message_get_args (reply, error,
1153  DBUS_TYPE_UINT32, &result,
1155  {
1156  _DBUS_ASSERT_ERROR_IS_SET (error);
1157  dbus_message_unref (reply);
1158  return -1;
1159  }
1160 
1161  dbus_message_unref (reply);
1162 
1163  return result;
1164 }
1165 
1166 
1185 int
1187  const char *name,
1188  DBusError *error)
1189 {
1190  DBusMessage *message, *reply;
1191  dbus_uint32_t result;
1192 
1193  _dbus_return_val_if_fail (connection != NULL, 0);
1194  _dbus_return_val_if_fail (name != NULL, 0);
1195  _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), 0);
1196  _dbus_return_val_if_error_is_set (error, 0);
1197 
1201  "ReleaseName");
1202 
1203  if (message == NULL)
1204  {
1205  _DBUS_SET_OOM (error);
1206  return -1;
1207  }
1208 
1209  if (!dbus_message_append_args (message,
1210  DBUS_TYPE_STRING, &name,
1212  {
1213  dbus_message_unref (message);
1214  _DBUS_SET_OOM (error);
1215  return -1;
1216  }
1217 
1218  reply = dbus_connection_send_with_reply_and_block (connection, message, -1,
1219  error);
1220 
1221  dbus_message_unref (message);
1222 
1223  if (reply == NULL)
1224  {
1225  _DBUS_ASSERT_ERROR_IS_SET (error);
1226  return -1;
1227  }
1228 
1229  if (dbus_set_error_from_message (error, reply))
1230  {
1231  _DBUS_ASSERT_ERROR_IS_SET (error);
1232  dbus_message_unref (reply);
1233  return -1;
1234  }
1235 
1236  if (!dbus_message_get_args (reply, error,
1237  DBUS_TYPE_UINT32, &result,
1239  {
1240  _DBUS_ASSERT_ERROR_IS_SET (error);
1241  dbus_message_unref (reply);
1242  return -1;
1243  }
1244 
1245  dbus_message_unref (reply);
1246 
1247  return result;
1248 }
1249 
1269  const char *name,
1270  DBusError *error)
1271 {
1272  DBusMessage *message, *reply;
1273  dbus_bool_t exists;
1274 
1275  _dbus_return_val_if_fail (connection != NULL, FALSE);
1276  _dbus_return_val_if_fail (name != NULL, FALSE);
1277  _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1278  _dbus_return_val_if_error_is_set (error, FALSE);
1279 
1283  "NameHasOwner");
1284  if (message == NULL)
1285  {
1286  _DBUS_SET_OOM (error);
1287  return FALSE;
1288  }
1289 
1290  if (!dbus_message_append_args (message,
1291  DBUS_TYPE_STRING, &name,
1293  {
1294  dbus_message_unref (message);
1295  _DBUS_SET_OOM (error);
1296  return FALSE;
1297  }
1298 
1299  reply = dbus_connection_send_with_reply_and_block (connection, message, -1, error);
1300  dbus_message_unref (message);
1301 
1302  if (reply == NULL)
1303  {
1304  _DBUS_ASSERT_ERROR_IS_SET (error);
1305  return FALSE;
1306  }
1307 
1308  if (!dbus_message_get_args (reply, error,
1309  DBUS_TYPE_BOOLEAN, &exists,
1311  {
1312  _DBUS_ASSERT_ERROR_IS_SET (error);
1313  dbus_message_unref (reply);
1314  return FALSE;
1315  }
1316 
1317  dbus_message_unref (reply);
1318  return exists;
1319 }
1320 
1345  const char *name,
1346  dbus_uint32_t flags,
1347  dbus_uint32_t *result,
1348  DBusError *error)
1349 {
1350  DBusMessage *msg;
1351  DBusMessage *reply;
1352 
1353  _dbus_return_val_if_fail (connection != NULL, FALSE);
1354  _dbus_return_val_if_fail (_dbus_check_is_valid_bus_name (name), FALSE);
1355 
1359  "StartServiceByName");
1360 
1361  if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &name,
1363  {
1364  dbus_message_unref (msg);
1365  _DBUS_SET_OOM (error);
1366  return FALSE;
1367  }
1368 
1369  reply = dbus_connection_send_with_reply_and_block (connection, msg,
1370  -1, error);
1371  dbus_message_unref (msg);
1372 
1373  if (reply == NULL)
1374  {
1375  _DBUS_ASSERT_ERROR_IS_SET (error);
1376  return FALSE;
1377  }
1378 
1379  if (dbus_set_error_from_message (error, reply))
1380  {
1381  _DBUS_ASSERT_ERROR_IS_SET (error);
1382  dbus_message_unref (reply);
1383  return FALSE;
1384  }
1385 
1386  if (result != NULL &&
1387  !dbus_message_get_args (reply, error, DBUS_TYPE_UINT32,
1388  result, DBUS_TYPE_INVALID))
1389  {
1390  _DBUS_ASSERT_ERROR_IS_SET (error);
1391  dbus_message_unref (reply);
1392  return FALSE;
1393  }
1394 
1395  dbus_message_unref (reply);
1396  return TRUE;
1397 }
1398 
1399 static void
1400 send_no_return_values (DBusConnection *connection,
1401  DBusMessage *msg,
1402  DBusError *error)
1403 {
1404  if (error)
1405  {
1406  /* Block to check success codepath */
1407  DBusMessage *reply;
1408 
1409  reply = dbus_connection_send_with_reply_and_block (connection, msg,
1410  -1, error);
1411 
1412  if (reply == NULL)
1413  _DBUS_ASSERT_ERROR_IS_SET (error);
1414  else
1415  dbus_message_unref (reply);
1416  }
1417  else
1418  {
1419  /* Silently-fail nonblocking codepath */
1421  dbus_connection_send (connection, msg, NULL);
1422  }
1423 }
1424 
1513 void
1515  const char *rule,
1516  DBusError *error)
1517 {
1518  DBusMessage *msg;
1519 
1520  _dbus_return_if_fail (rule != NULL);
1521 
1525  "AddMatch");
1526 
1527  if (msg == NULL)
1528  {
1529  _DBUS_SET_OOM (error);
1530  return;
1531  }
1532 
1533  if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1535  {
1536  dbus_message_unref (msg);
1537  _DBUS_SET_OOM (error);
1538  return;
1539  }
1540 
1541  send_no_return_values (connection, msg, error);
1542 
1543  dbus_message_unref (msg);
1544 }
1545 
1563 void
1565  const char *rule,
1566  DBusError *error)
1567 {
1568  DBusMessage *msg;
1569 
1570  _dbus_return_if_fail (rule != NULL);
1571 
1575  "RemoveMatch");
1576 
1577  if (!dbus_message_append_args (msg, DBUS_TYPE_STRING, &rule,
1579  {
1580  dbus_message_unref (msg);
1581  _DBUS_SET_OOM (error);
1582  return;
1583  }
1584 
1585  send_no_return_values (connection, msg, error);
1586 
1587  dbus_message_unref (msg);
1588 }
1589 
unsigned int dbus_uint32_t
A 32-bit unsigned integer on all platforms.
const char * message
public error message field
Definition: dbus-errors.h:51
#define NULL
A null pointer, defined appropriately for C or C++.
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, the other end of the connection may (but is not required to) optimize by not sending method return or error replies.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
int dbus_bus_release_name(DBusConnection *connection, const char *name, DBusError *error)
Asks the bus to unassign the given name from this connection by invoking the ReleaseName method on th...
Definition: dbus-bus.c:1186
DBusConnection * dbus_connection_ref(DBusConnection *connection)
Increments the reference count of a DBusConnection.
#define DBUS_TYPE_STRING
Type code marking a UTF-8 encoded, nul-terminated Unicode string.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define DBUS_INTERFACE_DBUS
The interface exported by the object with DBUS_SERVICE_DBUS and DBUS_PATH_DBUS.
Definition: dbus-shared.h:88
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
void _dbus_warn_check_failed(const char *format,...)
Prints a &quot;critical&quot; warning to stderr when an assertion fails; differs from _dbus_warn primarily in t...
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...
#define N_BUS_TYPES
Number of bus types.
Definition: dbus-bus.c:89
Implementation details of DBusConnection.
The login session bus.
Definition: dbus-shared.h:58
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
Block of message-bus-related data we attach to each DBusConnection used with these convenience functi...
Definition: dbus-bus.c:76
#define DBUS_UID_UNSET
an invalid UID used to represent an uninitialized dbus_uid_t field
Definition: dbus-sysdeps.h:107
void dbus_connection_free_data_slot(dbus_int32_t *slot_p)
Deallocates a global ID for connection data slots.
const char * _dbus_getenv(const char *varname)
Wrapper for getenv().
Definition: dbus-sysdeps.c:183
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.
DBusConnection * dbus_connection_open_private(const char *address, DBusError *error)
Opens a new, dedicated connection to a remote address.
Internals of DBusMessage.
int dbus_bus_request_name(DBusConnection *connection, const char *name, unsigned int flags, DBusError *error)
Asks the bus to assign the given name to this connection by invoking the RequestName method on the bu...
Definition: dbus-bus.c:1100
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
DBusConnection * connection
Connection we&#39;re associated with.
Definition: dbus-bus.c:78
The systemwide bus.
Definition: dbus-shared.h:59
dbus_bool_t _dbus_setenv(const char *varname, const char *value)
Wrapper for setenv().
Definition: dbus-sysdeps.c:112
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
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.
#define DBUS_PATH_DBUS
The object path used to talk to the bus itself.
Definition: dbus-shared.h:80
dbus_bool_t dbus_connection_send(DBusConnection *connection, DBusMessage *message, dbus_uint32_t *serial)
Adds a message to the outgoing message queue.
DBusConnection * dbus_bus_get(DBusBusType type, DBusError *error)
Connects to a bus daemon and registers the client with it.
Definition: dbus-bus.c:560
void * dbus_connection_get_data(DBusConnection *connection, dbus_int32_t slot)
Retrieves data previously set with dbus_connection_set_data().
dbus_bool_t dbus_bus_set_unique_name(DBusConnection *connection, const char *unique_name)
Sets the unique name of the connection, as assigned by the message bus.
Definition: dbus-bus.c:763
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
char * unique_name
Unique name of this connection.
Definition: dbus-bus.c:79
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_bus_remove_match(DBusConnection *connection, const char *rule, DBusError *error)
Removes a previously-added match rule &quot;by value&quot; (the most recently-added identical rule gets removed...
Definition: dbus-bus.c:1564
DBusConnection * dbus_connection_open(const char *address, DBusError *error)
Gets a connection to a remote address.
dbus_bool_t dbus_message_get_args(DBusMessage *message, DBusError *error, int first_arg_type,...)
Gets arguments from a message given a variable argument list.
Object representing an exception.
Definition: dbus-errors.h:48
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:401
dbus_bool_t dbus_message_append_args(DBusMessage *message, int first_arg_type,...)
Appends fields to a message given a variable argument list.
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
#define DBUS_TYPE_UINT32
Type code marking a 32-bit unsigned integer.
Definition: dbus-protocol.h:86
#define _DBUS_UNLOCK(name)
Unlocks a global lock.
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define TRUE
Expands to &quot;1&quot;.
dbus_bool_t dbus_bus_register(DBusConnection *connection, DBusError *error)
Registers a connection with the bus.
Definition: dbus-bus.c:648
#define DBUS_ERROR_FAILED
A generic error; &quot;something went wrong&quot; - see the error message for more.
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_bus_start_service_by_name(DBusConnection *connection, const char *name, dbus_uint32_t flags, dbus_uint32_t *result, DBusError *error)
Starts a service that will request ownership of the given name.
Definition: dbus-bus.c:1344
#define _DBUS_DEFINE_GLOBAL_LOCK(name)
Defines a global lock variable with the given name.
#define DBUS_TYPE_INVALID
Type code that is never equal to a legitimate type code.
Definition: dbus-protocol.h:60
unsigned long dbus_bus_get_unix_user(DBusConnection *connection, const char *name, DBusError *error)
Asks the bus to return the UID the named connection authenticated as, if any.
Definition: dbus-bus.c:853
#define DBUS_TYPE_BOOLEAN
Type code marking a boolean.
Definition: dbus-protocol.h:70
unsigned int is_well_known
Is one of the well-known connections in our global array.
Definition: dbus-bus.c:81
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...
The bus that started us, if any.
Definition: dbus-shared.h:60
dbus_bool_t _dbus_lookup_session_address(dbus_bool_t *supported, DBusString *address, DBusError *error)
Determines the address of the session bus by querying a platform-specific method. ...
void dbus_bus_add_match(DBusConnection *connection, const char *rule, DBusError *error)
Adds a match rule to match messages going through the message bus.
Definition: dbus-bus.c:1514
#define FALSE
Expands to &quot;0&quot;.
const char * dbus_bus_get_unique_name(DBusConnection *connection)
Gets the unique name of the connection as assigned by the message bus.
Definition: dbus-bus.c:808
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:809
#define _DBUS_LOCK(name)
Locks a global lock.
char * dbus_bus_get_id(DBusConnection *connection, DBusError *error)
Asks the bus to return its globally unique ID, as described in the D-Bus specification.
Definition: dbus-bus.c:936
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:619
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero...
#define DBUS_SERVICE_DBUS
The bus name used to talk to the bus itself.
Definition: dbus-shared.h:76
int dbus_int32_t
A 32-bit signed integer on all platforms.
char * _dbus_strdup(const char *str)
Duplicates a string.
DBusBusType
Well-known bus types.
Definition: dbus-shared.h:56
DBusConnection * dbus_bus_get_private(DBusBusType type, DBusError *error)
Connects to a bus daemon and registers the client with it as with dbus_bus_register().
Definition: dbus-bus.c:592
void dbus_message_unref(DBusMessage *message)
Decrements the reference count of a DBusMessage, freeing the message if the count reaches 0...
dbus_bool_t dbus_bus_name_has_owner(DBusConnection *connection, const char *name, DBusError *error)
Asks the bus whether a certain name has an owner.
Definition: dbus-bus.c:1268
DBusMessage * dbus_message_new_method_call(const char *destination, const char *path, const char *interface, const char *method)
Constructs a new message to invoke a method on a remote object.
dbus_bool_t dbus_error_is_set(const DBusError *error)
Checks whether an error occurred (the error is set).
Definition: dbus-errors.c:329