1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* driver.c Bus client (driver)
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
6 * Copyright (C) 2013 Samsung Electronics
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "activation.h"
28 #include "connection.h"
37 #include <dbus/dbus-asv-util.h>
38 #include <dbus/dbus-string.h>
39 #include <dbus/dbus-internals.h>
40 #include <dbus/dbus-message.h>
41 #include <dbus/dbus-marshal-recursive.h>
49 static DBusConnection *
50 bus_driver_get_conn_helper (DBusConnection *connection,
52 const char *what_we_want,
57 BusRegistry *registry;
62 if (!dbus_message_get_args (message, error,
63 DBUS_TYPE_STRING, &name,
67 _dbus_assert (name != NULL);
68 _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
70 registry = bus_connection_get_registry (connection);
71 _dbus_string_init_const (&str, name);
72 serv = bus_registry_lookup (registry, &str);
76 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
77 "Could not get %s of name '%s': no such name",
82 conn = bus_service_get_primary_owners_connection (serv);
83 _dbus_assert (conn != NULL);
91 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
92 DBusMessage *hello_message,
93 BusTransaction *transaction,
97 bus_driver_send_service_owner_changed (const char *service_name,
98 const char *old_owner,
99 const char *new_owner,
100 BusTransaction *transaction,
103 DBusMessage *message;
105 const char *null_service;
107 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
110 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
113 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
115 old_owner ? old_owner : null_service,
116 new_owner ? new_owner : null_service);
118 message = dbus_message_new_signal (DBUS_PATH_DBUS,
128 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
131 if (!dbus_message_append_args (message,
132 DBUS_TYPE_STRING, &service_name,
133 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
134 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
138 _dbus_assert (dbus_message_has_signature (message, "sss"));
140 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
141 dbus_message_unref (message);
146 dbus_message_unref (message);
152 bus_driver_send_service_lost (DBusConnection *connection,
153 const char *service_name,
154 BusTransaction *transaction,
157 DBusMessage *message;
159 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
162 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
164 message = dbus_message_new_signal (DBUS_PATH_DBUS,
174 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
175 !dbus_message_append_args (message,
176 DBUS_TYPE_STRING, &service_name,
179 dbus_message_unref (message);
184 if (!bus_transaction_send_from_driver (transaction, connection, message))
186 dbus_message_unref (message);
192 dbus_message_unref (message);
198 bus_driver_send_service_acquired (DBusConnection *connection,
199 const char *service_name,
200 BusTransaction *transaction,
203 DBusMessage *message;
205 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
208 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
210 message = dbus_message_new_signal (DBUS_PATH_DBUS,
220 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
221 !dbus_message_append_args (message,
222 DBUS_TYPE_STRING, &service_name,
225 dbus_message_unref (message);
230 if (!bus_transaction_send_from_driver (transaction, connection, message))
232 dbus_message_unref (message);
238 dbus_message_unref (message);
244 create_unique_client_name (BusRegistry *registry,
247 /* We never want to use the same unique client name twice, because
248 * we want to guarantee that if you send a message to a given unique
249 * name, you always get the same application. So we use two numbers
250 * for INT_MAX * INT_MAX combinations, should be pretty safe against
253 /* FIXME these should be in BusRegistry rather than static vars */
254 static int next_major_number = 0;
255 static int next_minor_number = 0;
258 len = _dbus_string_get_length (str);
262 /* start out with 1-0, go to 1-1, 1-2, 1-3,
263 * up to 1-MAXINT, then 2-0, 2-1, etc.
265 if (next_minor_number <= 0)
267 next_major_number += 1;
268 next_minor_number = 0;
269 if (next_major_number <= 0)
270 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
273 _dbus_assert (next_major_number > 0);
274 _dbus_assert (next_minor_number >= 0);
276 /* appname:MAJOR-MINOR */
278 if (!_dbus_string_append (str, ":"))
281 if (!_dbus_string_append_int (str, next_major_number))
284 if (!_dbus_string_append (str, "."))
287 if (!_dbus_string_append_int (str, next_minor_number))
290 next_minor_number += 1;
292 /* Check if a client with the name exists */
293 if (bus_registry_lookup (registry, str) == NULL)
296 /* drop the number again, try the next one. */
297 _dbus_string_set_length (str, len);
304 bus_driver_handle_hello (DBusConnection *connection,
305 BusTransaction *transaction,
306 DBusMessage *message,
309 DBusString unique_name;
312 BusRegistry *registry;
313 BusConnections *connections;
315 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
317 if (bus_connection_is_active (connection))
319 /* We already handled an Hello message for this connection. */
320 dbus_set_error (error, DBUS_ERROR_FAILED,
321 "Already handled an Hello message");
325 /* Note that when these limits are exceeded we don't disconnect the
326 * connection; we just sort of leave it hanging there until it times
327 * out or disconnects itself or is dropped due to the max number of
328 * incomplete connections. It's even OK if the connection wants to
329 * retry the hello message, we support that.
331 connections = bus_connection_get_connections (connection);
332 if (!bus_connections_check_limits (connections, connection,
335 _DBUS_ASSERT_ERROR_IS_SET (error);
339 if (!_dbus_string_init (&unique_name))
347 registry = bus_connection_get_registry (connection);
349 if (!create_unique_client_name (registry, &unique_name))
355 if (!bus_connection_complete (connection, &unique_name, error))
357 _DBUS_ASSERT_ERROR_IS_SET (error);
361 if (!dbus_message_set_sender (message,
362 bus_connection_get_name (connection)))
368 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
371 /* Create the service */
372 service = bus_registry_ensure (registry,
373 &unique_name, connection, 0, transaction, error);
377 _dbus_assert (bus_connection_is_active (connection));
381 _dbus_string_free (&unique_name);
386 bus_driver_send_welcome_message (DBusConnection *connection,
387 DBusMessage *hello_message,
388 BusTransaction *transaction,
391 DBusMessage *welcome;
394 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
396 name = bus_connection_get_name (connection);
397 _dbus_assert (name != NULL);
399 welcome = dbus_message_new_method_return (hello_message);
406 if (!dbus_message_append_args (welcome,
407 DBUS_TYPE_STRING, &name,
410 dbus_message_unref (welcome);
415 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
417 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
419 dbus_message_unref (welcome);
425 dbus_message_unref (welcome);
431 bus_driver_handle_list_services (DBusConnection *connection,
432 BusTransaction *transaction,
433 DBusMessage *message,
439 BusRegistry *registry;
441 DBusMessageIter iter;
444 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
446 registry = bus_connection_get_registry (connection);
448 reply = dbus_message_new_method_return (message);
456 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
458 if(!kdbus_list_services (connection, &services, &len))
460 dbus_message_unref (reply);
466 if (!bus_registry_list_services (registry, &services, &len))
468 dbus_message_unref (reply);
473 dbus_message_iter_init_append (reply, &iter);
475 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
476 DBUS_TYPE_STRING_AS_STRING,
479 dbus_free_string_array (services);
480 dbus_message_unref (reply);
485 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //not needed for kdbus, we got it from kdbus_list_services
487 /* Include the bus driver in the list */
488 const char *v_STRING = DBUS_SERVICE_DBUS;
489 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
492 dbus_free_string_array (services);
493 dbus_message_unref (reply);
502 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
505 dbus_free_string_array (services);
506 dbus_message_unref (reply);
513 dbus_free_string_array (services);
515 if (!dbus_message_iter_close_container (&iter, &sub))
517 dbus_message_unref (reply);
522 if (!bus_transaction_send_from_driver (transaction, connection, reply))
524 dbus_message_unref (reply);
530 dbus_message_unref (reply);
536 bus_driver_handle_list_activatable_services (DBusConnection *connection,
537 BusTransaction *transaction,
538 DBusMessage *message,
544 BusActivation *activation;
546 DBusMessageIter iter;
549 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
551 activation = bus_connection_get_activation (connection);
553 reply = dbus_message_new_method_return (message);
560 if (!bus_activation_list_services (activation, &services, &len))
562 dbus_message_unref (reply);
567 dbus_message_iter_init_append (reply, &iter);
569 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
570 DBUS_TYPE_STRING_AS_STRING,
573 dbus_free_string_array (services);
574 dbus_message_unref (reply);
580 /* Include the bus driver in the list */
581 const char *v_STRING = DBUS_SERVICE_DBUS;
582 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
585 dbus_free_string_array (services);
586 dbus_message_unref (reply);
595 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
598 dbus_free_string_array (services);
599 dbus_message_unref (reply);
606 dbus_free_string_array (services);
608 if (!dbus_message_iter_close_container (&iter, &sub))
610 dbus_message_unref (reply);
615 if (!bus_transaction_send_from_driver (transaction, connection, reply))
617 dbus_message_unref (reply);
623 dbus_message_unref (reply);
629 bus_driver_handle_acquire_service (DBusConnection *connection,
630 BusTransaction *transaction,
631 DBusMessage *message,
635 DBusString service_name;
637 dbus_uint32_t service_reply;
640 BusRegistry *registry;
642 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
644 registry = bus_connection_get_registry (connection);
646 if (!dbus_message_get_args (message, error,
647 DBUS_TYPE_STRING, &name,
648 DBUS_TYPE_UINT32, &flags,
652 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
657 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
659 if (!bus_registry_acquire_kdbus_service (registry, connection,
661 &service_reply, transaction,
667 _dbus_string_init_const (&service_name, name);
669 if (!bus_registry_acquire_service (registry, connection,
670 &service_name, flags,
671 &service_reply, transaction,
677 reply = dbus_message_new_method_return (message);
684 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
690 if (!bus_transaction_send_from_driver (transaction, connection, reply))
700 dbus_message_unref (reply);
705 bus_driver_handle_release_service (DBusConnection *connection,
706 BusTransaction *transaction,
707 DBusMessage *message,
711 DBusString service_name;
713 dbus_uint32_t service_reply;
715 BusRegistry *registry;
717 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
719 if (!dbus_message_get_args (message, error,
720 DBUS_TYPE_STRING, &name,
724 _dbus_verbose ("Trying to release name %s\n", name);
729 _dbus_string_init_const (&service_name, name);
730 registry = bus_connection_get_registry (connection);
732 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
734 if (!bus_registry_release_service_kdbus (dbus_message_get_sender(message), connection,
735 &service_name, &service_reply,
739 else if (!bus_registry_release_service (registry, connection,
740 &service_name, &service_reply,
744 reply = dbus_message_new_method_return (message);
751 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
757 if (!bus_transaction_send_from_driver (transaction, connection, reply))
767 dbus_message_unref (reply);
772 bus_driver_handle_service_exists (DBusConnection *connection,
773 BusTransaction *transaction,
774 DBusMessage *message,
778 DBusString service_name;
780 dbus_bool_t service_exists;
783 BusRegistry *registry;
785 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
787 registry = bus_connection_get_registry (connection);
789 if (!dbus_message_get_args (message, error,
790 DBUS_TYPE_STRING, &name,
796 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
798 service_exists = TRUE;
802 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
805 struct nameInfo info;
807 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
808 if((inter_ret == 0) || (inter_ret == -ENOENT))
809 service_exists = (inter_ret == 0) ? TRUE : FALSE;
812 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
813 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
814 service_exists = FALSE;
819 _dbus_string_init_const (&service_name, name);
820 service = bus_registry_lookup (registry, &service_name);
821 service_exists = service != NULL;
825 reply = dbus_message_new_method_return (message);
832 if (!dbus_message_append_args (reply,
833 DBUS_TYPE_BOOLEAN, &service_exists,
840 if (!bus_transaction_send_from_driver (transaction, connection, reply))
850 dbus_message_unref (reply);
856 bus_driver_handle_activate_service (DBusConnection *connection,
857 BusTransaction *transaction,
858 DBusMessage *message,
864 BusActivation *activation;
866 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
868 activation = bus_connection_get_activation (connection);
870 if (!dbus_message_get_args (message, error,
871 DBUS_TYPE_STRING, &name,
872 DBUS_TYPE_UINT32, &flags,
875 _DBUS_ASSERT_ERROR_IS_SET (error);
876 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
882 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
883 message, name, error))
885 _DBUS_ASSERT_ERROR_IS_SET (error);
886 _dbus_verbose ("bus_activation_activate_service() failed\n");
897 send_ack_reply (DBusConnection *connection,
898 BusTransaction *transaction,
899 DBusMessage *message,
904 if (dbus_message_get_no_reply (message))
907 reply = dbus_message_new_method_return (message);
914 if (!bus_transaction_send_from_driver (transaction, connection, reply))
917 dbus_message_unref (reply);
921 dbus_message_unref (reply);
927 bus_driver_handle_update_activation_environment (DBusConnection *connection,
928 BusTransaction *transaction,
929 DBusMessage *message,
933 BusActivation *activation;
934 DBusMessageIter iter;
935 DBusMessageIter dict_iter;
936 DBusMessageIter dict_entry_iter;
939 DBusList *keys, *key_link;
940 DBusList *values, *value_link;
942 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
944 activation = bus_connection_get_activation (connection);
946 dbus_message_iter_init (message, &iter);
948 /* The message signature has already been checked for us,
949 * so let's just assert it's right.
951 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
953 dbus_message_iter_recurse (&iter, &dict_iter);
957 /* Then loop through the sent dictionary, add the location of
958 * the environment keys and values to lists. The result will
959 * be in reverse order, so we don't have to constantly search
960 * for the end of the list in a loop.
964 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
966 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
968 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
974 dbus_message_iter_get_basic (&dict_entry_iter, &key);
975 dbus_message_iter_next (&dict_entry_iter);
977 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
979 if (value_type != DBUS_TYPE_STRING)
982 dbus_message_iter_get_basic (&dict_entry_iter, &value);
984 if (!_dbus_list_append (&keys, key))
990 if (!_dbus_list_append (&values, value))
996 dbus_message_iter_next (&dict_entry_iter);
999 if (key_type != DBUS_TYPE_INVALID)
1002 dbus_message_iter_next (&dict_iter);
1005 if (array_type != DBUS_TYPE_INVALID)
1008 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1011 value_link = values;
1012 while (key_link != NULL)
1017 key = key_link->data;
1018 value = value_link->data;
1020 if (!bus_activation_set_environment_variable (activation,
1023 _DBUS_ASSERT_ERROR_IS_SET (error);
1024 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1027 key_link = _dbus_list_get_next_link (&keys, key_link);
1028 value_link = _dbus_list_get_next_link (&values, value_link);
1031 /* FIXME: We can fail early having set only some of the environment variables,
1032 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1033 * matter, so we're punting for now.
1035 if (key_link != NULL)
1038 if (!send_ack_reply (connection, transaction,
1045 _dbus_list_clear (&keys);
1046 _dbus_list_clear (&values);
1051 bus_driver_handle_add_match (DBusConnection *connection,
1052 BusTransaction *transaction,
1053 DBusMessage *message,
1059 BusMatchmaker *matchmaker;
1061 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1066 if (bus_connection_get_n_match_rules (connection) >=
1067 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1069 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1070 "Connection \"%s\" is not allowed to add more match rules "
1071 "(increase limits in configuration file if required)",
1072 bus_connection_is_active (connection) ?
1073 bus_connection_get_name (connection) :
1078 if (!dbus_message_get_args (message, error,
1079 DBUS_TYPE_STRING, &text,
1082 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1086 _dbus_string_init_const (&str, text);
1088 rule = bus_match_rule_parse (connection, &str, error);
1092 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1095 if (!kdbus_add_match_rule (connection, message, text, error))
1098 if (!send_ack_reply (connection, transaction,
1104 matchmaker = bus_connection_get_matchmaker (connection);
1106 if (!bus_matchmaker_add_rule (matchmaker, rule))
1108 BUS_SET_OOM (error);
1112 if (!send_ack_reply (connection, transaction,
1115 bus_matchmaker_remove_rule (matchmaker, rule);
1120 bus_match_rule_unref (rule);
1125 _DBUS_ASSERT_ERROR_IS_SET (error);
1127 bus_match_rule_unref (rule);
1132 bus_driver_handle_remove_match (DBusConnection *connection,
1133 BusTransaction *transaction,
1134 DBusMessage *message,
1140 BusMatchmaker *matchmaker;
1142 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1147 if (!dbus_message_get_args (message, error,
1148 DBUS_TYPE_STRING, &text,
1151 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1155 _dbus_string_init_const (&str, text);
1157 rule = bus_match_rule_parse (connection, &str, error);
1161 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1163 if(!kdbus_remove_match(connection, message, error))
1167 /* Send the ack before we remove the rule, since the ack is undone
1168 * on transaction cancel, but rule removal isn't.
1170 if (!send_ack_reply (connection, transaction,
1174 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1176 matchmaker = bus_connection_get_matchmaker (connection);
1178 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1182 bus_match_rule_unref (rule);
1187 _DBUS_ASSERT_ERROR_IS_SET (error);
1189 bus_match_rule_unref (rule);
1194 bus_driver_handle_get_service_owner (DBusConnection *connection,
1195 BusTransaction *transaction,
1196 DBusMessage *message,
1200 const char *base_name;
1202 BusRegistry *registry;
1203 BusService *service;
1205 char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1207 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1209 registry = bus_connection_get_registry (connection);
1214 if (! dbus_message_get_args (message, error,
1215 DBUS_TYPE_STRING, &text,
1219 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1222 struct nameInfo info;
1224 ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
1225 if(ret == 0) //unique id of the name
1227 sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
1228 _dbus_verbose("Unique name discovered:%s\n", unique_name);
1229 base_name = unique_name;
1231 else if(ret == -ENOENT) //name has no owner
1233 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
1234 "Could not get owner of name '%s': no such name", text);
1239 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
1240 dbus_set_error (error, DBUS_ERROR_FAILED,
1241 "Could not determine unique name for '%s'", text);
1247 _dbus_string_init_const (&str, text);
1248 service = bus_registry_lookup (registry, &str);
1249 if (service == NULL &&
1250 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1252 /* ORG_FREEDESKTOP_DBUS owns itself */
1253 base_name = DBUS_SERVICE_DBUS;
1255 else if (service == NULL)
1257 dbus_set_error (error,
1258 DBUS_ERROR_NAME_HAS_NO_OWNER,
1259 "Could not get owner of name '%s': no such name", text);
1264 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1265 if (base_name == NULL)
1267 /* FIXME - how is this error possible? */
1268 dbus_set_error (error,
1270 "Could not determine unique name for '%s'", text);
1273 _dbus_assert (*base_name == ':');
1276 _dbus_assert (base_name != NULL);
1278 reply = dbus_message_new_method_return (message);
1282 if (! dbus_message_append_args (reply,
1283 DBUS_TYPE_STRING, &base_name,
1287 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1290 dbus_message_unref (reply);
1295 BUS_SET_OOM (error);
1298 _DBUS_ASSERT_ERROR_IS_SET (error);
1300 dbus_message_unref (reply);
1305 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1306 BusTransaction *transaction,
1307 DBusMessage *message,
1311 DBusList *base_names;
1314 BusRegistry *registry;
1315 BusService *service;
1317 DBusMessageIter iter, array_iter;
1318 char *dbus_service_name = DBUS_SERVICE_DBUS;
1320 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1322 registry = bus_connection_get_registry (connection);
1328 if (! dbus_message_get_args (message, error,
1329 DBUS_TYPE_STRING, &text,
1333 _dbus_string_init_const (&str, text);
1334 service = bus_registry_lookup (registry, &str);
1335 if (service == NULL &&
1336 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1338 /* ORG_FREEDESKTOP_DBUS owns itself */
1339 if (! _dbus_list_append (&base_names, dbus_service_name))
1342 else if (service == NULL)
1344 dbus_set_error (error,
1345 DBUS_ERROR_NAME_HAS_NO_OWNER,
1346 "Could not get owners of name '%s': no such name", text);
1351 if (!bus_service_list_queued_owners (service,
1357 _dbus_assert (base_names != NULL);
1359 reply = dbus_message_new_method_return (message);
1363 dbus_message_iter_init_append (reply, &iter);
1364 if (!dbus_message_iter_open_container (&iter,
1366 DBUS_TYPE_STRING_AS_STRING,
1370 link = _dbus_list_get_first_link (&base_names);
1371 while (link != NULL)
1375 _dbus_assert (link->data != NULL);
1376 uname = (char *)link->data;
1378 if (!dbus_message_iter_append_basic (&array_iter,
1383 link = _dbus_list_get_next_link (&base_names, link);
1386 if (! dbus_message_iter_close_container (&iter, &array_iter))
1390 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1393 dbus_message_unref (reply);
1398 BUS_SET_OOM (error);
1401 _DBUS_ASSERT_ERROR_IS_SET (error);
1403 dbus_message_unref (reply);
1406 _dbus_list_clear (&base_names);
1412 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1413 BusTransaction *transaction,
1414 DBusMessage *message,
1417 DBusConnection *conn;
1420 dbus_uint32_t uid32;
1421 const char *service;
1423 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1425 reply = dbus_message_new_method_return (message);
1429 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1431 if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
1436 conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1442 if (!dbus_connection_get_unix_user (conn, &uid))
1444 dbus_set_error (error,
1446 "Could not determine UID for '%s'", service);
1452 if (! dbus_message_append_args (reply,
1453 DBUS_TYPE_UINT32, &uid32,
1457 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1460 dbus_message_unref (reply);
1465 BUS_SET_OOM (error);
1468 _DBUS_ASSERT_ERROR_IS_SET (error);
1470 dbus_message_unref (reply);
1475 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1476 BusTransaction *transaction,
1477 DBusMessage *message,
1480 DBusConnection *conn;
1483 dbus_uint32_t pid32;
1484 const char *service;
1486 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1488 reply = dbus_message_new_method_return (message);
1492 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1494 if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
1499 conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1507 if (!dbus_connection_get_unix_process_id (conn, &pid))
1509 dbus_set_error (error,
1510 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1511 "Could not determine PID for '%s'", service);
1517 if (! dbus_message_append_args (reply,
1518 DBUS_TYPE_UINT32, &pid32,
1522 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1525 dbus_message_unref (reply);
1530 BUS_SET_OOM (error);
1533 _DBUS_ASSERT_ERROR_IS_SET (error);
1535 dbus_message_unref (reply);
1540 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1541 BusTransaction *transaction,
1542 DBusMessage *message,
1545 DBusConnection *conn;
1548 dbus_uint32_t data_size;
1549 const char *service;
1551 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1555 conn = bus_driver_get_conn_helper (connection, message,
1556 "audit session data", &service, error);
1561 reply = dbus_message_new_method_return (message);
1565 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1567 dbus_set_error (error,
1568 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1569 "Could not determine audit session data for '%s'", service);
1573 if (! dbus_message_append_args (reply,
1574 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1578 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1581 dbus_message_unref (reply);
1586 BUS_SET_OOM (error);
1589 _DBUS_ASSERT_ERROR_IS_SET (error);
1591 dbus_message_unref (reply);
1596 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1597 BusTransaction *transaction,
1598 DBusMessage *message,
1601 DBusConnection *conn;
1603 BusSELinuxID *context;
1604 const char *service;
1606 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1608 reply = dbus_message_new_method_return (message);
1612 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1614 if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
1619 conn = bus_driver_get_conn_helper (connection, message, "security context",
1625 context = bus_connection_get_selinux_id (conn);
1628 dbus_set_error (error,
1629 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1630 "Could not determine security context for '%s'", service);
1634 if (! bus_selinux_append_context (reply, context, error))
1638 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1641 dbus_message_unref (reply);
1646 BUS_SET_OOM (error);
1649 _DBUS_ASSERT_ERROR_IS_SET (error);
1651 dbus_message_unref (reply);
1656 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1657 BusTransaction *transaction,
1658 DBusMessage *message,
1661 DBusConnection *conn;
1663 DBusMessageIter reply_iter;
1664 DBusMessageIter array_iter;
1665 unsigned long ulong_val;
1666 const char *service;
1668 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1670 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1674 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1676 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1682 /* we can't represent > 32-bit pids; if your system needs them, please
1683 * add ProcessID64 to the spec or something */
1684 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1685 ulong_val <= _DBUS_UINT32_MAX)
1687 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1691 /* we can't represent > 32-bit uids; if your system needs them, please
1692 * add UnixUserID64 to the spec or something */
1693 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1694 ulong_val <= _DBUS_UINT32_MAX)
1696 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1702 if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
1704 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1710 if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
1712 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1719 if (!_dbus_asv_close (&reply_iter, &array_iter))
1722 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1724 /* this time we don't want to close the iterator again, so just
1725 * get rid of the message */
1726 dbus_message_unref (reply);
1734 BUS_SET_OOM (error);
1737 _DBUS_ASSERT_ERROR_IS_SET (error);
1741 _dbus_asv_abandon (&reply_iter, &array_iter);
1742 dbus_message_unref (reply);
1749 bus_driver_handle_reload_config (DBusConnection *connection,
1750 BusTransaction *transaction,
1751 DBusMessage *message,
1754 BusContext *context;
1757 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1761 context = bus_connection_get_context (connection);
1762 if (!bus_context_reload_config (context, error))
1765 reply = dbus_message_new_method_return (message);
1769 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1772 dbus_message_unref (reply);
1776 BUS_SET_OOM (error);
1779 _DBUS_ASSERT_ERROR_IS_SET (error);
1781 dbus_message_unref (reply);
1786 bus_driver_handle_get_id (DBusConnection *connection,
1787 BusTransaction *transaction,
1788 DBusMessage *message,
1791 BusContext *context;
1794 const char *v_STRING;
1796 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1798 if (!_dbus_string_init (&uuid))
1800 BUS_SET_OOM (error);
1806 context = bus_connection_get_context (connection);
1807 if (!bus_context_get_id (context, &uuid))
1810 reply = dbus_message_new_method_return (message);
1814 v_STRING = _dbus_string_get_const_data (&uuid);
1815 if (!dbus_message_append_args (reply,
1816 DBUS_TYPE_STRING, &v_STRING,
1820 _dbus_assert (dbus_message_has_signature (reply, "s"));
1822 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1825 _dbus_string_free (&uuid);
1826 dbus_message_unref (reply);
1830 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1832 BUS_SET_OOM (error);
1835 dbus_message_unref (reply);
1836 _dbus_string_free (&uuid);
1843 const char *in_args;
1844 const char *out_args;
1845 dbus_bool_t (* handler) (DBusConnection *connection,
1846 BusTransaction *transaction,
1847 DBusMessage *message,
1851 /* For speed it might be useful to sort this in order of
1852 * frequency of use (but doesn't matter with only a few items
1855 static const MessageHandler dbus_message_handlers[] = {
1858 DBUS_TYPE_STRING_AS_STRING,
1859 bus_driver_handle_hello },
1861 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1862 DBUS_TYPE_UINT32_AS_STRING,
1863 bus_driver_handle_acquire_service },
1865 DBUS_TYPE_STRING_AS_STRING,
1866 DBUS_TYPE_UINT32_AS_STRING,
1867 bus_driver_handle_release_service },
1868 { "StartServiceByName",
1869 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1870 DBUS_TYPE_UINT32_AS_STRING,
1871 bus_driver_handle_activate_service },
1872 { "UpdateActivationEnvironment",
1873 DBUS_TYPE_ARRAY_AS_STRING DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_STRING_AS_STRING DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
1875 bus_driver_handle_update_activation_environment },
1877 DBUS_TYPE_STRING_AS_STRING,
1878 DBUS_TYPE_BOOLEAN_AS_STRING,
1879 bus_driver_handle_service_exists },
1882 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1883 bus_driver_handle_list_services },
1884 { "ListActivatableNames",
1886 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1887 bus_driver_handle_list_activatable_services },
1889 DBUS_TYPE_STRING_AS_STRING,
1891 bus_driver_handle_add_match },
1893 DBUS_TYPE_STRING_AS_STRING,
1895 bus_driver_handle_remove_match },
1897 DBUS_TYPE_STRING_AS_STRING,
1898 DBUS_TYPE_STRING_AS_STRING,
1899 bus_driver_handle_get_service_owner },
1900 { "ListQueuedOwners",
1901 DBUS_TYPE_STRING_AS_STRING,
1902 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1903 bus_driver_handle_list_queued_owners },
1904 { "GetConnectionUnixUser",
1905 DBUS_TYPE_STRING_AS_STRING,
1906 DBUS_TYPE_UINT32_AS_STRING,
1907 bus_driver_handle_get_connection_unix_user },
1908 { "GetConnectionUnixProcessID",
1909 DBUS_TYPE_STRING_AS_STRING,
1910 DBUS_TYPE_UINT32_AS_STRING,
1911 bus_driver_handle_get_connection_unix_process_id },
1912 { "GetAdtAuditSessionData",
1913 DBUS_TYPE_STRING_AS_STRING,
1914 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1915 bus_driver_handle_get_adt_audit_session_data },
1916 { "GetConnectionSELinuxSecurityContext",
1917 DBUS_TYPE_STRING_AS_STRING,
1918 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1919 bus_driver_handle_get_connection_selinux_security_context },
1923 bus_driver_handle_reload_config },
1926 DBUS_TYPE_STRING_AS_STRING,
1927 bus_driver_handle_get_id },
1928 { "GetConnectionCredentials", "s", "a{sv}",
1929 bus_driver_handle_get_connection_credentials },
1930 { NULL, NULL, NULL, NULL }
1933 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1934 BusTransaction *, DBusMessage *, DBusError *);
1936 static const MessageHandler introspectable_message_handlers[] = {
1937 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1938 { NULL, NULL, NULL, NULL }
1941 #ifdef DBUS_ENABLE_STATS
1942 static const MessageHandler stats_message_handlers[] = {
1943 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1944 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1945 { NULL, NULL, NULL, NULL }
1951 const MessageHandler *message_handlers;
1952 const char *extra_introspection;
1955 /* These should ideally be sorted by frequency of use, although it
1956 * probably doesn't matter with this few items */
1957 static InterfaceHandler interface_handlers[] = {
1958 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1959 " <signal name=\"NameOwnerChanged\">\n"
1960 " <arg type=\"s\"/>\n"
1961 " <arg type=\"s\"/>\n"
1962 " <arg type=\"s\"/>\n"
1964 " <signal name=\"NameLost\">\n"
1965 " <arg type=\"s\"/>\n"
1967 " <signal name=\"NameAcquired\">\n"
1968 " <arg type=\"s\"/>\n"
1970 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1971 #ifdef DBUS_ENABLE_STATS
1972 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1974 { NULL, NULL, NULL }
1978 write_args_for_direction (DBusString *xml,
1979 const char *signature,
1982 DBusTypeReader typereader;
1986 _dbus_string_init_const (&sigstr, signature);
1987 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1989 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1991 const DBusString *subsig;
1994 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1995 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1998 if (!_dbus_string_append_len (xml,
1999 _dbus_string_get_const_data (subsig) + start,
2002 if (!_dbus_string_append (xml, "\"/>\n"))
2005 _dbus_type_reader_next (&typereader);
2013 bus_driver_generate_introspect_string (DBusString *xml)
2015 const InterfaceHandler *ih;
2016 const MessageHandler *mh;
2018 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2020 if (!_dbus_string_append (xml, "<node>\n"))
2023 for (ih = interface_handlers; ih->name != NULL; ih++)
2025 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2029 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2031 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2035 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2038 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2041 if (!_dbus_string_append (xml, " </method>\n"))
2045 if (ih->extra_introspection != NULL &&
2046 !_dbus_string_append (xml, ih->extra_introspection))
2049 if (!_dbus_string_append (xml, " </interface>\n"))
2053 if (!_dbus_string_append (xml, "</node>\n"))
2060 bus_driver_handle_introspect (DBusConnection *connection,
2061 BusTransaction *transaction,
2062 DBusMessage *message,
2067 const char *v_STRING;
2069 _dbus_verbose ("Introspect() on bus driver\n");
2071 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2075 if (! dbus_message_get_args (message, error,
2078 _DBUS_ASSERT_ERROR_IS_SET (error);
2082 if (!_dbus_string_init (&xml))
2084 BUS_SET_OOM (error);
2088 if (!bus_driver_generate_introspect_string (&xml))
2091 v_STRING = _dbus_string_get_const_data (&xml);
2093 reply = dbus_message_new_method_return (message);
2097 if (! dbus_message_append_args (reply,
2098 DBUS_TYPE_STRING, &v_STRING,
2102 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2105 dbus_message_unref (reply);
2106 _dbus_string_free (&xml);
2111 BUS_SET_OOM (error);
2114 dbus_message_unref (reply);
2116 _dbus_string_free (&xml);
2122 bus_driver_handle_message (DBusConnection *connection,
2123 BusTransaction *transaction,
2124 DBusMessage *message,
2127 const char *name, *interface;
2128 const InterfaceHandler *ih;
2129 const MessageHandler *mh;
2130 dbus_bool_t found_interface = FALSE;
2132 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2134 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2136 BusContext *context;
2138 context = bus_connection_get_context (connection);
2139 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2142 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2144 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2145 return TRUE; /* we just ignore this */
2148 /* may be NULL, which means "any interface will do" */
2149 interface = dbus_message_get_interface (message);
2151 _dbus_assert (dbus_message_get_member (message) != NULL);
2153 name = dbus_message_get_member (message);
2155 _dbus_verbose ("Driver got a method call: %s\n", name);
2157 /* security checks should have kept this from getting here */
2158 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2159 strcmp (name, "Hello") == 0);
2161 for (ih = interface_handlers; ih->name != NULL; ih++)
2163 if (interface != NULL && strcmp (interface, ih->name) != 0)
2166 found_interface = TRUE;
2168 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2170 if (strcmp (mh->name, name) != 0)
2173 _dbus_verbose ("Found driver handler for %s\n", name);
2175 if (!dbus_message_has_signature (message, mh->in_args))
2177 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2178 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2179 name, dbus_message_get_signature (message),
2182 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2183 "Call to %s has wrong args (%s, expected %s)\n",
2184 name, dbus_message_get_signature (message),
2186 _DBUS_ASSERT_ERROR_IS_SET (error);
2190 if ((* mh->handler) (connection, transaction, message, error))
2192 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2193 _dbus_verbose ("Driver handler succeeded\n");
2198 _DBUS_ASSERT_ERROR_IS_SET (error);
2199 _dbus_verbose ("Driver handler returned failure\n");
2205 _dbus_verbose ("No driver handler for message \"%s\"\n",
2208 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2209 "%s does not understand message %s",
2210 DBUS_SERVICE_DBUS, name);
2216 bus_driver_remove_connection (DBusConnection *connection)
2218 /* FIXME 1.0 Does nothing for now, should unregister the connection
2219 * with the bus driver.