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.
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
26 #include "activation.h"
27 #include "connection.h"
36 #include <dbus/dbus-asv-util.h>
37 #include <dbus/dbus-string.h>
38 #include <dbus/dbus-internals.h>
39 #include <dbus/dbus-message.h>
40 #include <dbus/dbus-marshal-recursive.h>
48 static DBusConnection *
49 bus_driver_get_conn_helper (DBusConnection *connection,
51 const char *what_we_want,
56 BusRegistry *registry;
61 if (!dbus_message_get_args (message, error,
62 DBUS_TYPE_STRING, &name,
66 _dbus_assert (name != NULL);
67 _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
69 registry = bus_connection_get_registry (connection);
70 _dbus_string_init_const (&str, name);
71 serv = bus_registry_lookup (registry, &str);
75 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
76 "Could not get %s of name '%s': no such name",
81 conn = bus_service_get_primary_owners_connection (serv);
82 _dbus_assert (conn != NULL);
90 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
91 DBusMessage *hello_message,
92 BusTransaction *transaction,
96 bus_driver_send_service_owner_changed (const char *service_name,
97 const char *old_owner,
98 const char *new_owner,
99 BusTransaction *transaction,
102 DBusMessage *message;
104 const char *null_service;
106 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
109 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
112 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
114 old_owner ? old_owner : null_service,
115 new_owner ? new_owner : null_service);
117 message = dbus_message_new_signal (DBUS_PATH_DBUS,
127 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
130 if (!dbus_message_append_args (message,
131 DBUS_TYPE_STRING, &service_name,
132 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
133 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
137 _dbus_assert (dbus_message_has_signature (message, "sss"));
139 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
140 dbus_message_unref (message);
145 dbus_message_unref (message);
151 bus_driver_send_service_lost (DBusConnection *connection,
152 const char *service_name,
153 BusTransaction *transaction,
156 DBusMessage *message;
158 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
161 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
163 message = dbus_message_new_signal (DBUS_PATH_DBUS,
173 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
174 !dbus_message_append_args (message,
175 DBUS_TYPE_STRING, &service_name,
178 dbus_message_unref (message);
183 if (!bus_transaction_send_from_driver (transaction, connection, message))
185 dbus_message_unref (message);
191 dbus_message_unref (message);
197 bus_driver_send_service_acquired (DBusConnection *connection,
198 const char *service_name,
199 BusTransaction *transaction,
202 DBusMessage *message;
204 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
207 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
209 message = dbus_message_new_signal (DBUS_PATH_DBUS,
219 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
220 !dbus_message_append_args (message,
221 DBUS_TYPE_STRING, &service_name,
224 dbus_message_unref (message);
229 if (!bus_transaction_send_from_driver (transaction, connection, message))
231 dbus_message_unref (message);
237 dbus_message_unref (message);
243 create_unique_client_name (BusRegistry *registry,
246 /* We never want to use the same unique client name twice, because
247 * we want to guarantee that if you send a message to a given unique
248 * name, you always get the same application. So we use two numbers
249 * for INT_MAX * INT_MAX combinations, should be pretty safe against
252 /* FIXME these should be in BusRegistry rather than static vars */
253 static int next_major_number = 0;
254 static int next_minor_number = 0;
257 len = _dbus_string_get_length (str);
261 /* start out with 1-0, go to 1-1, 1-2, 1-3,
262 * up to 1-MAXINT, then 2-0, 2-1, etc.
264 if (next_minor_number <= 0)
266 next_major_number += 1;
267 next_minor_number = 0;
268 if (next_major_number <= 0)
269 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
272 _dbus_assert (next_major_number > 0);
273 _dbus_assert (next_minor_number >= 0);
275 /* appname:MAJOR-MINOR */
277 if (!_dbus_string_append (str, ":"))
280 if (!_dbus_string_append_int (str, next_major_number))
283 if (!_dbus_string_append (str, "."))
286 if (!_dbus_string_append_int (str, next_minor_number))
289 next_minor_number += 1;
291 /* Check if a client with the name exists */
292 if (bus_registry_lookup (registry, str) == NULL)
295 /* drop the number again, try the next one. */
296 _dbus_string_set_length (str, len);
303 bus_driver_handle_hello (DBusConnection *connection,
304 BusTransaction *transaction,
305 DBusMessage *message,
308 DBusString unique_name;
311 BusRegistry *registry;
312 BusConnections *connections;
314 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
316 if (bus_connection_is_active (connection))
318 /* We already handled an Hello message for this connection. */
319 dbus_set_error (error, DBUS_ERROR_FAILED,
320 "Already handled an Hello message");
324 /* Note that when these limits are exceeded we don't disconnect the
325 * connection; we just sort of leave it hanging there until it times
326 * out or disconnects itself or is dropped due to the max number of
327 * incomplete connections. It's even OK if the connection wants to
328 * retry the hello message, we support that.
330 connections = bus_connection_get_connections (connection);
331 if (!bus_connections_check_limits (connections, connection,
334 _DBUS_ASSERT_ERROR_IS_SET (error);
338 if (!_dbus_string_init (&unique_name))
346 registry = bus_connection_get_registry (connection);
348 if (!create_unique_client_name (registry, &unique_name))
354 if (!bus_connection_complete (connection, &unique_name, error))
356 _DBUS_ASSERT_ERROR_IS_SET (error);
360 if (!dbus_message_set_sender (message,
361 bus_connection_get_name (connection)))
367 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
370 /* Create the service */
371 service = bus_registry_ensure (registry,
372 &unique_name, connection, 0, transaction, error);
376 _dbus_assert (bus_connection_is_active (connection));
380 _dbus_string_free (&unique_name);
385 bus_driver_send_welcome_message (DBusConnection *connection,
386 DBusMessage *hello_message,
387 BusTransaction *transaction,
390 DBusMessage *welcome;
393 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
395 name = bus_connection_get_name (connection);
396 _dbus_assert (name != NULL);
398 welcome = dbus_message_new_method_return (hello_message);
405 if (!dbus_message_append_args (welcome,
406 DBUS_TYPE_STRING, &name,
409 dbus_message_unref (welcome);
414 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
416 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
418 dbus_message_unref (welcome);
424 dbus_message_unref (welcome);
430 bus_driver_handle_list_services (DBusConnection *connection,
431 BusTransaction *transaction,
432 DBusMessage *message,
438 BusRegistry *registry;
440 DBusMessageIter iter;
443 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
445 registry = bus_connection_get_registry (connection);
447 reply = dbus_message_new_method_return (message);
455 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
457 if(!kdbus_list_services (connection, &services, &len))
459 dbus_message_unref (reply);
465 if (!bus_registry_list_services (registry, &services, &len))
467 dbus_message_unref (reply);
472 dbus_message_iter_init_append (reply, &iter);
474 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
475 DBUS_TYPE_STRING_AS_STRING,
478 dbus_free_string_array (services);
479 dbus_message_unref (reply);
484 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
486 /* Include the bus driver in the list */
487 const char *v_STRING = DBUS_SERVICE_DBUS;
488 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
491 dbus_free_string_array (services);
492 dbus_message_unref (reply);
501 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
504 dbus_free_string_array (services);
505 dbus_message_unref (reply);
512 dbus_free_string_array (services);
514 if (!dbus_message_iter_close_container (&iter, &sub))
516 dbus_message_unref (reply);
521 if (!bus_transaction_send_from_driver (transaction, connection, reply))
523 dbus_message_unref (reply);
529 dbus_message_unref (reply);
535 bus_driver_handle_list_activatable_services (DBusConnection *connection,
536 BusTransaction *transaction,
537 DBusMessage *message,
543 BusActivation *activation;
545 DBusMessageIter iter;
548 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
550 activation = bus_connection_get_activation (connection);
552 reply = dbus_message_new_method_return (message);
559 if (!bus_activation_list_services (activation, &services, &len))
561 dbus_message_unref (reply);
566 dbus_message_iter_init_append (reply, &iter);
568 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
569 DBUS_TYPE_STRING_AS_STRING,
572 dbus_free_string_array (services);
573 dbus_message_unref (reply);
579 /* Include the bus driver in the list */
580 const char *v_STRING = DBUS_SERVICE_DBUS;
581 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
584 dbus_free_string_array (services);
585 dbus_message_unref (reply);
594 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
597 dbus_free_string_array (services);
598 dbus_message_unref (reply);
605 dbus_free_string_array (services);
607 if (!dbus_message_iter_close_container (&iter, &sub))
609 dbus_message_unref (reply);
614 if (!bus_transaction_send_from_driver (transaction, connection, reply))
616 dbus_message_unref (reply);
622 dbus_message_unref (reply);
628 bus_driver_handle_acquire_service (DBusConnection *connection,
629 BusTransaction *transaction,
630 DBusMessage *message,
634 DBusString service_name;
636 dbus_uint32_t service_reply;
639 BusRegistry *registry;
641 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
643 registry = bus_connection_get_registry (connection);
645 if (!dbus_message_get_args (message, error,
646 DBUS_TYPE_STRING, &name,
647 DBUS_TYPE_UINT32, &flags,
651 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
656 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
658 if (!bus_registry_acquire_kdbus_service (registry, connection,
660 &service_reply, transaction,
666 _dbus_string_init_const (&service_name, name);
668 if (!bus_registry_acquire_service (registry, connection,
669 &service_name, flags,
670 &service_reply, transaction,
676 reply = dbus_message_new_method_return (message);
683 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
689 _dbus_verbose ("Reply sender: %s, destination: %s\n", dbus_message_get_sender(reply), dbus_message_get_destination(reply)); //todo kdbus incl
691 if (!bus_transaction_send_from_driver (transaction, connection, reply))
701 dbus_message_unref (reply);
706 bus_driver_handle_release_service (DBusConnection *connection,
707 BusTransaction *transaction,
708 DBusMessage *message,
712 DBusString service_name;
714 dbus_uint32_t service_reply;
716 BusRegistry *registry;
718 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
720 registry = bus_connection_get_registry (connection);
722 if (!dbus_message_get_args (message, error,
723 DBUS_TYPE_STRING, &name,
727 _dbus_verbose ("Trying to release name %s\n", name);
732 _dbus_string_init_const (&service_name, name);
734 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
736 registry = (BusRegistry*) message;
737 /* looks like hack? Yes.
738 * But how to pass message to bus_registry_release_service in other way?
739 * In kdbus mode we don't need registry, though.
743 if (!bus_registry_release_service (registry, connection,
744 &service_name, &service_reply,
748 reply = dbus_message_new_method_return (message);
755 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
761 if (!bus_transaction_send_from_driver (transaction, connection, reply))
771 dbus_message_unref (reply);
776 bus_driver_handle_service_exists (DBusConnection *connection,
777 BusTransaction *transaction,
778 DBusMessage *message,
782 DBusString service_name;
784 dbus_bool_t service_exists;
787 BusRegistry *registry;
789 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
791 registry = bus_connection_get_registry (connection);
793 if (!dbus_message_get_args (message, error,
794 DBUS_TYPE_STRING, &name,
800 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
802 service_exists = TRUE;
806 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
809 struct nameInfo info;
811 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
812 if((inter_ret == 0) || (inter_ret == -ENOENT))
813 service_exists = (inter_ret == 0) ? TRUE : FALSE;
816 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
817 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
818 service_exists = FALSE;
823 _dbus_string_init_const (&service_name, name);
824 service = bus_registry_lookup (registry, &service_name);
825 service_exists = service != NULL;
829 reply = dbus_message_new_method_return (message);
836 if (!dbus_message_append_args (reply,
837 DBUS_TYPE_BOOLEAN, &service_exists,
844 if (!bus_transaction_send_from_driver (transaction, connection, reply))
854 dbus_message_unref (reply);
860 bus_driver_handle_activate_service (DBusConnection *connection,
861 BusTransaction *transaction,
862 DBusMessage *message,
868 BusActivation *activation;
870 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
872 activation = bus_connection_get_activation (connection);
874 if (!dbus_message_get_args (message, error,
875 DBUS_TYPE_STRING, &name,
876 DBUS_TYPE_UINT32, &flags,
879 _DBUS_ASSERT_ERROR_IS_SET (error);
880 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
886 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
887 message, name, error))
889 _DBUS_ASSERT_ERROR_IS_SET (error);
890 _dbus_verbose ("bus_activation_activate_service() failed\n");
901 send_ack_reply (DBusConnection *connection,
902 BusTransaction *transaction,
903 DBusMessage *message,
908 if (dbus_message_get_no_reply (message))
911 reply = dbus_message_new_method_return (message);
918 if (!bus_transaction_send_from_driver (transaction, connection, reply))
921 dbus_message_unref (reply);
925 dbus_message_unref (reply);
931 bus_driver_handle_update_activation_environment (DBusConnection *connection,
932 BusTransaction *transaction,
933 DBusMessage *message,
937 BusActivation *activation;
938 DBusMessageIter iter;
939 DBusMessageIter dict_iter;
940 DBusMessageIter dict_entry_iter;
943 DBusList *keys, *key_link;
944 DBusList *values, *value_link;
946 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
948 activation = bus_connection_get_activation (connection);
950 dbus_message_iter_init (message, &iter);
952 /* The message signature has already been checked for us,
953 * so let's just assert it's right.
955 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
957 dbus_message_iter_recurse (&iter, &dict_iter);
961 /* Then loop through the sent dictionary, add the location of
962 * the environment keys and values to lists. The result will
963 * be in reverse order, so we don't have to constantly search
964 * for the end of the list in a loop.
968 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
970 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
972 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
978 dbus_message_iter_get_basic (&dict_entry_iter, &key);
979 dbus_message_iter_next (&dict_entry_iter);
981 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
983 if (value_type != DBUS_TYPE_STRING)
986 dbus_message_iter_get_basic (&dict_entry_iter, &value);
988 if (!_dbus_list_append (&keys, key))
994 if (!_dbus_list_append (&values, value))
1000 dbus_message_iter_next (&dict_entry_iter);
1003 if (key_type != DBUS_TYPE_INVALID)
1006 dbus_message_iter_next (&dict_iter);
1009 if (array_type != DBUS_TYPE_INVALID)
1012 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1015 value_link = values;
1016 while (key_link != NULL)
1021 key = key_link->data;
1022 value = value_link->data;
1024 if (!bus_activation_set_environment_variable (activation,
1027 _DBUS_ASSERT_ERROR_IS_SET (error);
1028 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1031 key_link = _dbus_list_get_next_link (&keys, key_link);
1032 value_link = _dbus_list_get_next_link (&values, value_link);
1035 /* FIXME: We can fail early having set only some of the environment variables,
1036 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1037 * matter, so we're punting for now.
1039 if (key_link != NULL)
1042 if (!send_ack_reply (connection, transaction,
1049 _dbus_list_clear (&keys);
1050 _dbus_list_clear (&values);
1055 bus_driver_handle_add_match (DBusConnection *connection,
1056 BusTransaction *transaction,
1057 DBusMessage *message,
1063 BusMatchmaker *matchmaker;
1065 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1070 if (bus_connection_get_n_match_rules (connection) >=
1071 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1073 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1074 "Connection \"%s\" is not allowed to add more match rules "
1075 "(increase limits in configuration file if required)",
1076 bus_connection_is_active (connection) ?
1077 bus_connection_get_name (connection) :
1082 if (!dbus_message_get_args (message, error,
1083 DBUS_TYPE_STRING, &text,
1086 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1090 _dbus_string_init_const (&str, text);
1092 rule = bus_match_rule_parse (connection, &str, error);
1096 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1099 if (!kdbus_add_match_rule (connection, message, text, error))
1102 if (!send_ack_reply (connection, transaction,
1108 matchmaker = bus_connection_get_matchmaker (connection);
1110 if (!bus_matchmaker_add_rule (matchmaker, rule))
1112 BUS_SET_OOM (error);
1116 if (!send_ack_reply (connection, transaction,
1119 bus_matchmaker_remove_rule (matchmaker, rule);
1124 bus_match_rule_unref (rule);
1129 _DBUS_ASSERT_ERROR_IS_SET (error);
1131 bus_match_rule_unref (rule);
1136 bus_driver_handle_remove_match (DBusConnection *connection,
1137 BusTransaction *transaction,
1138 DBusMessage *message,
1144 BusMatchmaker *matchmaker;
1146 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1151 if (!dbus_message_get_args (message, error,
1152 DBUS_TYPE_STRING, &text,
1155 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1159 _dbus_string_init_const (&str, text);
1161 rule = bus_match_rule_parse (connection, &str, error);
1165 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1167 if(!kdbus_remove_match(connection, message, error))
1171 /* Send the ack before we remove the rule, since the ack is undone
1172 * on transaction cancel, but rule removal isn't.
1174 if (!send_ack_reply (connection, transaction,
1178 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1180 matchmaker = bus_connection_get_matchmaker (connection);
1182 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1186 bus_match_rule_unref (rule);
1191 _DBUS_ASSERT_ERROR_IS_SET (error);
1193 bus_match_rule_unref (rule);
1198 bus_driver_handle_get_service_owner (DBusConnection *connection,
1199 BusTransaction *transaction,
1200 DBusMessage *message,
1204 const char *base_name;
1206 BusRegistry *registry;
1207 BusService *service;
1209 char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1211 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1213 registry = bus_connection_get_registry (connection);
1218 if (! dbus_message_get_args (message, error,
1219 DBUS_TYPE_STRING, &text,
1223 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1226 struct nameInfo info;
1228 ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
1229 if(ret == 0) //unique id of the name
1231 sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
1232 _dbus_verbose("Unique name discovered:%s\n", unique_name);
1233 base_name = unique_name;
1235 else if(ret == -ENOENT) //name has no owner
1237 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
1238 "Could not get owner of name '%s': no such name", text);
1243 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
1244 dbus_set_error (error, DBUS_ERROR_FAILED,
1245 "Could not determine unique name for '%s'", text);
1251 _dbus_string_init_const (&str, text);
1252 service = bus_registry_lookup (registry, &str);
1253 if (service == NULL &&
1254 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1256 /* ORG_FREEDESKTOP_DBUS owns itself */
1257 base_name = DBUS_SERVICE_DBUS;
1259 else if (service == NULL)
1261 dbus_set_error (error,
1262 DBUS_ERROR_NAME_HAS_NO_OWNER,
1263 "Could not get owner of name '%s': no such name", text);
1268 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1269 if (base_name == NULL)
1271 /* FIXME - how is this error possible? */
1272 dbus_set_error (error,
1274 "Could not determine unique name for '%s'", text);
1277 _dbus_assert (*base_name == ':');
1280 _dbus_assert (base_name != NULL);
1282 reply = dbus_message_new_method_return (message);
1286 if (! dbus_message_append_args (reply,
1287 DBUS_TYPE_STRING, &base_name,
1291 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1294 dbus_message_unref (reply);
1299 BUS_SET_OOM (error);
1302 _DBUS_ASSERT_ERROR_IS_SET (error);
1304 dbus_message_unref (reply);
1309 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1310 BusTransaction *transaction,
1311 DBusMessage *message,
1315 DBusList *base_names;
1318 BusRegistry *registry;
1319 BusService *service;
1321 DBusMessageIter iter, array_iter;
1322 char *dbus_service_name = DBUS_SERVICE_DBUS;
1324 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1326 registry = bus_connection_get_registry (connection);
1332 if (! dbus_message_get_args (message, error,
1333 DBUS_TYPE_STRING, &text,
1337 _dbus_string_init_const (&str, text);
1338 service = bus_registry_lookup (registry, &str);
1339 if (service == NULL &&
1340 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1342 /* ORG_FREEDESKTOP_DBUS owns itself */
1343 if (! _dbus_list_append (&base_names, dbus_service_name))
1346 else if (service == NULL)
1348 dbus_set_error (error,
1349 DBUS_ERROR_NAME_HAS_NO_OWNER,
1350 "Could not get owners of name '%s': no such name", text);
1355 if (!bus_service_list_queued_owners (service,
1361 _dbus_assert (base_names != NULL);
1363 reply = dbus_message_new_method_return (message);
1367 dbus_message_iter_init_append (reply, &iter);
1368 if (!dbus_message_iter_open_container (&iter,
1370 DBUS_TYPE_STRING_AS_STRING,
1374 link = _dbus_list_get_first_link (&base_names);
1375 while (link != NULL)
1379 _dbus_assert (link->data != NULL);
1380 uname = (char *)link->data;
1382 if (!dbus_message_iter_append_basic (&array_iter,
1387 link = _dbus_list_get_next_link (&base_names, link);
1390 if (! dbus_message_iter_close_container (&iter, &array_iter))
1394 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1397 dbus_message_unref (reply);
1402 BUS_SET_OOM (error);
1405 _DBUS_ASSERT_ERROR_IS_SET (error);
1407 dbus_message_unref (reply);
1410 _dbus_list_clear (&base_names);
1416 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1417 BusTransaction *transaction,
1418 DBusMessage *message,
1421 DBusConnection *conn;
1424 dbus_uint32_t uid32;
1425 const char *service;
1427 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1429 reply = dbus_message_new_method_return (message);
1433 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1435 if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
1440 conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1446 if (!dbus_connection_get_unix_user (conn, &uid))
1448 dbus_set_error (error,
1450 "Could not determine UID for '%s'", service);
1456 if (! dbus_message_append_args (reply,
1457 DBUS_TYPE_UINT32, &uid32,
1461 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1464 dbus_message_unref (reply);
1469 BUS_SET_OOM (error);
1472 _DBUS_ASSERT_ERROR_IS_SET (error);
1474 dbus_message_unref (reply);
1479 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1480 BusTransaction *transaction,
1481 DBusMessage *message,
1484 DBusConnection *conn;
1487 dbus_uint32_t pid32;
1488 const char *service;
1490 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1492 reply = dbus_message_new_method_return (message);
1496 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1498 if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
1503 conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1511 if (!dbus_connection_get_unix_process_id (conn, &pid))
1513 dbus_set_error (error,
1514 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1515 "Could not determine PID for '%s'", service);
1521 if (! dbus_message_append_args (reply,
1522 DBUS_TYPE_UINT32, &pid32,
1526 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1529 dbus_message_unref (reply);
1534 BUS_SET_OOM (error);
1537 _DBUS_ASSERT_ERROR_IS_SET (error);
1539 dbus_message_unref (reply);
1544 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1545 BusTransaction *transaction,
1546 DBusMessage *message,
1549 DBusConnection *conn;
1552 dbus_uint32_t data_size;
1553 const char *service;
1555 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1559 conn = bus_driver_get_conn_helper (connection, message,
1560 "audit session data", &service, error);
1565 reply = dbus_message_new_method_return (message);
1569 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1571 dbus_set_error (error,
1572 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1573 "Could not determine audit session data for '%s'", service);
1577 if (! dbus_message_append_args (reply,
1578 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1582 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1585 dbus_message_unref (reply);
1590 BUS_SET_OOM (error);
1593 _DBUS_ASSERT_ERROR_IS_SET (error);
1595 dbus_message_unref (reply);
1600 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1601 BusTransaction *transaction,
1602 DBusMessage *message,
1605 DBusConnection *conn;
1607 BusSELinuxID *context;
1608 const char *service;
1610 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1612 reply = dbus_message_new_method_return (message);
1616 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1618 if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
1623 conn = bus_driver_get_conn_helper (connection, message, "security context",
1629 context = bus_connection_get_selinux_id (conn);
1632 dbus_set_error (error,
1633 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1634 "Could not determine security context for '%s'", service);
1638 if (! bus_selinux_append_context (reply, context, error))
1642 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1645 dbus_message_unref (reply);
1650 BUS_SET_OOM (error);
1653 _DBUS_ASSERT_ERROR_IS_SET (error);
1655 dbus_message_unref (reply);
1660 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1661 BusTransaction *transaction,
1662 DBusMessage *message,
1665 DBusConnection *conn;
1667 DBusMessageIter reply_iter;
1668 DBusMessageIter array_iter;
1669 unsigned long ulong_val;
1670 const char *service;
1672 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1674 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1678 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1680 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1686 /* we can't represent > 32-bit pids; if your system needs them, please
1687 * add ProcessID64 to the spec or something */
1688 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1689 ulong_val <= _DBUS_UINT32_MAX)
1691 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1695 /* we can't represent > 32-bit uids; if your system needs them, please
1696 * add UnixUserID64 to the spec or something */
1697 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1698 ulong_val <= _DBUS_UINT32_MAX)
1700 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1706 if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
1708 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1714 if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
1716 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1723 if (!_dbus_asv_close (&reply_iter, &array_iter))
1726 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1728 /* this time we don't want to close the iterator again, so just
1729 * get rid of the message */
1730 dbus_message_unref (reply);
1738 BUS_SET_OOM (error);
1741 _DBUS_ASSERT_ERROR_IS_SET (error);
1745 _dbus_asv_abandon (&reply_iter, &array_iter);
1746 dbus_message_unref (reply);
1753 bus_driver_handle_reload_config (DBusConnection *connection,
1754 BusTransaction *transaction,
1755 DBusMessage *message,
1758 BusContext *context;
1761 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1765 context = bus_connection_get_context (connection);
1766 if (!bus_context_reload_config (context, error))
1769 reply = dbus_message_new_method_return (message);
1773 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1776 dbus_message_unref (reply);
1780 BUS_SET_OOM (error);
1783 _DBUS_ASSERT_ERROR_IS_SET (error);
1785 dbus_message_unref (reply);
1790 bus_driver_handle_get_id (DBusConnection *connection,
1791 BusTransaction *transaction,
1792 DBusMessage *message,
1795 BusContext *context;
1798 const char *v_STRING;
1800 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1802 if (!_dbus_string_init (&uuid))
1804 BUS_SET_OOM (error);
1810 context = bus_connection_get_context (connection);
1811 if (!bus_context_get_id (context, &uuid))
1814 reply = dbus_message_new_method_return (message);
1818 v_STRING = _dbus_string_get_const_data (&uuid);
1819 if (!dbus_message_append_args (reply,
1820 DBUS_TYPE_STRING, &v_STRING,
1824 _dbus_assert (dbus_message_has_signature (reply, "s"));
1826 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1829 _dbus_string_free (&uuid);
1830 dbus_message_unref (reply);
1834 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1836 BUS_SET_OOM (error);
1839 dbus_message_unref (reply);
1840 _dbus_string_free (&uuid);
1847 const char *in_args;
1848 const char *out_args;
1849 dbus_bool_t (* handler) (DBusConnection *connection,
1850 BusTransaction *transaction,
1851 DBusMessage *message,
1855 /* For speed it might be useful to sort this in order of
1856 * frequency of use (but doesn't matter with only a few items
1859 static const MessageHandler dbus_message_handlers[] = {
1862 DBUS_TYPE_STRING_AS_STRING,
1863 bus_driver_handle_hello },
1865 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1866 DBUS_TYPE_UINT32_AS_STRING,
1867 bus_driver_handle_acquire_service },
1869 DBUS_TYPE_STRING_AS_STRING,
1870 DBUS_TYPE_UINT32_AS_STRING,
1871 bus_driver_handle_release_service },
1872 { "StartServiceByName",
1873 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1874 DBUS_TYPE_UINT32_AS_STRING,
1875 bus_driver_handle_activate_service },
1876 { "UpdateActivationEnvironment",
1877 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,
1879 bus_driver_handle_update_activation_environment },
1881 DBUS_TYPE_STRING_AS_STRING,
1882 DBUS_TYPE_BOOLEAN_AS_STRING,
1883 bus_driver_handle_service_exists },
1886 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1887 bus_driver_handle_list_services },
1888 { "ListActivatableNames",
1890 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1891 bus_driver_handle_list_activatable_services },
1893 DBUS_TYPE_STRING_AS_STRING,
1895 bus_driver_handle_add_match },
1897 DBUS_TYPE_STRING_AS_STRING,
1899 bus_driver_handle_remove_match },
1901 DBUS_TYPE_STRING_AS_STRING,
1902 DBUS_TYPE_STRING_AS_STRING,
1903 bus_driver_handle_get_service_owner },
1904 { "ListQueuedOwners",
1905 DBUS_TYPE_STRING_AS_STRING,
1906 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1907 bus_driver_handle_list_queued_owners },
1908 { "GetConnectionUnixUser",
1909 DBUS_TYPE_STRING_AS_STRING,
1910 DBUS_TYPE_UINT32_AS_STRING,
1911 bus_driver_handle_get_connection_unix_user },
1912 { "GetConnectionUnixProcessID",
1913 DBUS_TYPE_STRING_AS_STRING,
1914 DBUS_TYPE_UINT32_AS_STRING,
1915 bus_driver_handle_get_connection_unix_process_id },
1916 { "GetAdtAuditSessionData",
1917 DBUS_TYPE_STRING_AS_STRING,
1918 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1919 bus_driver_handle_get_adt_audit_session_data },
1920 { "GetConnectionSELinuxSecurityContext",
1921 DBUS_TYPE_STRING_AS_STRING,
1922 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1923 bus_driver_handle_get_connection_selinux_security_context },
1927 bus_driver_handle_reload_config },
1930 DBUS_TYPE_STRING_AS_STRING,
1931 bus_driver_handle_get_id },
1932 { "GetConnectionCredentials", "s", "a{sv}",
1933 bus_driver_handle_get_connection_credentials },
1934 { NULL, NULL, NULL, NULL }
1937 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1938 BusTransaction *, DBusMessage *, DBusError *);
1940 static const MessageHandler introspectable_message_handlers[] = {
1941 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1942 { NULL, NULL, NULL, NULL }
1945 #ifdef DBUS_ENABLE_STATS
1946 static const MessageHandler stats_message_handlers[] = {
1947 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1948 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1949 { NULL, NULL, NULL, NULL }
1955 const MessageHandler *message_handlers;
1956 const char *extra_introspection;
1959 /* These should ideally be sorted by frequency of use, although it
1960 * probably doesn't matter with this few items */
1961 static InterfaceHandler interface_handlers[] = {
1962 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1963 " <signal name=\"NameOwnerChanged\">\n"
1964 " <arg type=\"s\"/>\n"
1965 " <arg type=\"s\"/>\n"
1966 " <arg type=\"s\"/>\n"
1968 " <signal name=\"NameLost\">\n"
1969 " <arg type=\"s\"/>\n"
1971 " <signal name=\"NameAcquired\">\n"
1972 " <arg type=\"s\"/>\n"
1974 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1975 #ifdef DBUS_ENABLE_STATS
1976 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1978 { NULL, NULL, NULL }
1982 write_args_for_direction (DBusString *xml,
1983 const char *signature,
1986 DBusTypeReader typereader;
1990 _dbus_string_init_const (&sigstr, signature);
1991 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1993 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1995 const DBusString *subsig;
1998 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1999 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
2002 if (!_dbus_string_append_len (xml,
2003 _dbus_string_get_const_data (subsig) + start,
2006 if (!_dbus_string_append (xml, "\"/>\n"))
2009 _dbus_type_reader_next (&typereader);
2017 bus_driver_generate_introspect_string (DBusString *xml)
2019 const InterfaceHandler *ih;
2020 const MessageHandler *mh;
2022 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2024 if (!_dbus_string_append (xml, "<node>\n"))
2027 for (ih = interface_handlers; ih->name != NULL; ih++)
2029 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2033 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2035 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2039 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2042 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2045 if (!_dbus_string_append (xml, " </method>\n"))
2049 if (ih->extra_introspection != NULL &&
2050 !_dbus_string_append (xml, ih->extra_introspection))
2053 if (!_dbus_string_append (xml, " </interface>\n"))
2057 if (!_dbus_string_append (xml, "</node>\n"))
2064 bus_driver_handle_introspect (DBusConnection *connection,
2065 BusTransaction *transaction,
2066 DBusMessage *message,
2071 const char *v_STRING;
2073 _dbus_verbose ("Introspect() on bus driver\n");
2075 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2079 if (! dbus_message_get_args (message, error,
2082 _DBUS_ASSERT_ERROR_IS_SET (error);
2086 if (!_dbus_string_init (&xml))
2088 BUS_SET_OOM (error);
2092 if (!bus_driver_generate_introspect_string (&xml))
2095 v_STRING = _dbus_string_get_const_data (&xml);
2097 reply = dbus_message_new_method_return (message);
2101 if (! dbus_message_append_args (reply,
2102 DBUS_TYPE_STRING, &v_STRING,
2106 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2109 dbus_message_unref (reply);
2110 _dbus_string_free (&xml);
2115 BUS_SET_OOM (error);
2118 dbus_message_unref (reply);
2120 _dbus_string_free (&xml);
2126 bus_driver_handle_message (DBusConnection *connection,
2127 BusTransaction *transaction,
2128 DBusMessage *message,
2131 const char *name, *interface;
2132 const InterfaceHandler *ih;
2133 const MessageHandler *mh;
2134 dbus_bool_t found_interface = FALSE;
2136 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2138 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2140 BusContext *context;
2142 context = bus_connection_get_context (connection);
2143 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2146 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2148 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2149 return TRUE; /* we just ignore this */
2152 /* may be NULL, which means "any interface will do" */
2153 interface = dbus_message_get_interface (message);
2155 _dbus_assert (dbus_message_get_member (message) != NULL);
2157 name = dbus_message_get_member (message);
2159 _dbus_verbose ("Driver got a method call: %s\n", name);
2161 /* security checks should have kept this from getting here */
2162 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2163 strcmp (name, "Hello") == 0);
2165 for (ih = interface_handlers; ih->name != NULL; ih++)
2167 if (interface != NULL && strcmp (interface, ih->name) != 0)
2170 found_interface = TRUE;
2172 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2174 if (strcmp (mh->name, name) != 0)
2177 _dbus_verbose ("Found driver handler for %s\n", name);
2179 if (!dbus_message_has_signature (message, mh->in_args))
2181 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2182 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2183 name, dbus_message_get_signature (message),
2186 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2187 "Call to %s has wrong args (%s, expected %s)\n",
2188 name, dbus_message_get_signature (message),
2190 _DBUS_ASSERT_ERROR_IS_SET (error);
2194 if ((* mh->handler) (connection, transaction, message, error))
2196 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2197 _dbus_verbose ("Driver handler succeeded\n");
2202 _DBUS_ASSERT_ERROR_IS_SET (error);
2203 _dbus_verbose ("Driver handler returned failure\n");
2209 _dbus_verbose ("No driver handler for message \"%s\"\n",
2212 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2213 "%s does not understand message %s",
2214 DBUS_SERVICE_DBUS, name);
2220 bus_driver_remove_connection (DBusConnection *connection)
2222 /* FIXME 1.0 Does nothing for now, should unregister the connection
2223 * with the bus driver.