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)))
736 registry = (BusRegistry*) dbus_message_get_sender(message);
737 /* todo looks like hack? Yes.
738 * But how to pass sender of message to bus_registry_release_service in other way?
742 if (!bus_registry_release_service (registry, connection,
743 &service_name, &service_reply,
747 reply = dbus_message_new_method_return (message);
754 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
760 if (!bus_transaction_send_from_driver (transaction, connection, reply))
770 dbus_message_unref (reply);
775 bus_driver_handle_service_exists (DBusConnection *connection,
776 BusTransaction *transaction,
777 DBusMessage *message,
781 DBusString service_name;
783 dbus_bool_t service_exists;
786 BusRegistry *registry;
788 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
790 registry = bus_connection_get_registry (connection);
792 if (!dbus_message_get_args (message, error,
793 DBUS_TYPE_STRING, &name,
799 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
801 service_exists = TRUE;
805 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
808 struct nameInfo info;
810 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
811 if((inter_ret == 0) || (inter_ret == -ENOENT))
812 service_exists = (inter_ret == 0) ? TRUE : FALSE;
815 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
816 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
817 service_exists = FALSE;
822 _dbus_string_init_const (&service_name, name);
823 service = bus_registry_lookup (registry, &service_name);
824 service_exists = service != NULL;
828 reply = dbus_message_new_method_return (message);
835 if (!dbus_message_append_args (reply,
836 DBUS_TYPE_BOOLEAN, &service_exists,
843 if (!bus_transaction_send_from_driver (transaction, connection, reply))
853 dbus_message_unref (reply);
859 bus_driver_handle_activate_service (DBusConnection *connection,
860 BusTransaction *transaction,
861 DBusMessage *message,
867 BusActivation *activation;
869 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
871 activation = bus_connection_get_activation (connection);
873 if (!dbus_message_get_args (message, error,
874 DBUS_TYPE_STRING, &name,
875 DBUS_TYPE_UINT32, &flags,
878 _DBUS_ASSERT_ERROR_IS_SET (error);
879 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
885 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
886 message, name, error))
888 _DBUS_ASSERT_ERROR_IS_SET (error);
889 _dbus_verbose ("bus_activation_activate_service() failed\n");
900 send_ack_reply (DBusConnection *connection,
901 BusTransaction *transaction,
902 DBusMessage *message,
907 if (dbus_message_get_no_reply (message))
910 reply = dbus_message_new_method_return (message);
917 if (!bus_transaction_send_from_driver (transaction, connection, reply))
920 dbus_message_unref (reply);
924 dbus_message_unref (reply);
930 bus_driver_handle_update_activation_environment (DBusConnection *connection,
931 BusTransaction *transaction,
932 DBusMessage *message,
936 BusActivation *activation;
937 DBusMessageIter iter;
938 DBusMessageIter dict_iter;
939 DBusMessageIter dict_entry_iter;
942 DBusList *keys, *key_link;
943 DBusList *values, *value_link;
945 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
947 activation = bus_connection_get_activation (connection);
949 dbus_message_iter_init (message, &iter);
951 /* The message signature has already been checked for us,
952 * so let's just assert it's right.
954 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
956 dbus_message_iter_recurse (&iter, &dict_iter);
960 /* Then loop through the sent dictionary, add the location of
961 * the environment keys and values to lists. The result will
962 * be in reverse order, so we don't have to constantly search
963 * for the end of the list in a loop.
967 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
969 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
971 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
977 dbus_message_iter_get_basic (&dict_entry_iter, &key);
978 dbus_message_iter_next (&dict_entry_iter);
980 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
982 if (value_type != DBUS_TYPE_STRING)
985 dbus_message_iter_get_basic (&dict_entry_iter, &value);
987 if (!_dbus_list_append (&keys, key))
993 if (!_dbus_list_append (&values, value))
999 dbus_message_iter_next (&dict_entry_iter);
1002 if (key_type != DBUS_TYPE_INVALID)
1005 dbus_message_iter_next (&dict_iter);
1008 if (array_type != DBUS_TYPE_INVALID)
1011 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1014 value_link = values;
1015 while (key_link != NULL)
1020 key = key_link->data;
1021 value = value_link->data;
1023 if (!bus_activation_set_environment_variable (activation,
1026 _DBUS_ASSERT_ERROR_IS_SET (error);
1027 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1030 key_link = _dbus_list_get_next_link (&keys, key_link);
1031 value_link = _dbus_list_get_next_link (&values, value_link);
1034 /* FIXME: We can fail early having set only some of the environment variables,
1035 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1036 * matter, so we're punting for now.
1038 if (key_link != NULL)
1041 if (!send_ack_reply (connection, transaction,
1048 _dbus_list_clear (&keys);
1049 _dbus_list_clear (&values);
1054 bus_driver_handle_add_match (DBusConnection *connection,
1055 BusTransaction *transaction,
1056 DBusMessage *message,
1062 BusMatchmaker *matchmaker;
1064 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1069 if (bus_connection_get_n_match_rules (connection) >=
1070 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1072 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1073 "Connection \"%s\" is not allowed to add more match rules "
1074 "(increase limits in configuration file if required)",
1075 bus_connection_is_active (connection) ?
1076 bus_connection_get_name (connection) :
1081 if (!dbus_message_get_args (message, error,
1082 DBUS_TYPE_STRING, &text,
1085 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1089 _dbus_string_init_const (&str, text);
1091 rule = bus_match_rule_parse (connection, &str, error);
1095 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1098 if (!kdbus_add_match_rule (connection, message, text, error))
1101 if (!send_ack_reply (connection, transaction,
1107 matchmaker = bus_connection_get_matchmaker (connection);
1109 if (!bus_matchmaker_add_rule (matchmaker, rule))
1111 BUS_SET_OOM (error);
1115 if (!send_ack_reply (connection, transaction,
1118 bus_matchmaker_remove_rule (matchmaker, rule);
1123 bus_match_rule_unref (rule);
1128 _DBUS_ASSERT_ERROR_IS_SET (error);
1130 bus_match_rule_unref (rule);
1135 bus_driver_handle_remove_match (DBusConnection *connection,
1136 BusTransaction *transaction,
1137 DBusMessage *message,
1143 BusMatchmaker *matchmaker;
1145 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1150 if (!dbus_message_get_args (message, error,
1151 DBUS_TYPE_STRING, &text,
1154 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1158 _dbus_string_init_const (&str, text);
1160 rule = bus_match_rule_parse (connection, &str, error);
1164 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1166 if(!kdbus_remove_match(connection, message, error))
1170 /* Send the ack before we remove the rule, since the ack is undone
1171 * on transaction cancel, but rule removal isn't.
1173 if (!send_ack_reply (connection, transaction,
1177 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1179 matchmaker = bus_connection_get_matchmaker (connection);
1181 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1185 bus_match_rule_unref (rule);
1190 _DBUS_ASSERT_ERROR_IS_SET (error);
1192 bus_match_rule_unref (rule);
1197 bus_driver_handle_get_service_owner (DBusConnection *connection,
1198 BusTransaction *transaction,
1199 DBusMessage *message,
1203 const char *base_name;
1205 BusRegistry *registry;
1206 BusService *service;
1208 char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1210 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1212 registry = bus_connection_get_registry (connection);
1217 if (! dbus_message_get_args (message, error,
1218 DBUS_TYPE_STRING, &text,
1222 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1225 struct nameInfo info;
1227 ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
1228 if(ret == 0) //unique id of the name
1230 sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
1231 _dbus_verbose("Unique name discovered:%s\n", unique_name);
1232 base_name = unique_name;
1234 else if(ret == -ENOENT) //name has no owner
1236 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
1237 "Could not get owner of name '%s': no such name", text);
1242 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
1243 dbus_set_error (error, DBUS_ERROR_FAILED,
1244 "Could not determine unique name for '%s'", text);
1250 _dbus_string_init_const (&str, text);
1251 service = bus_registry_lookup (registry, &str);
1252 if (service == NULL &&
1253 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1255 /* ORG_FREEDESKTOP_DBUS owns itself */
1256 base_name = DBUS_SERVICE_DBUS;
1258 else if (service == NULL)
1260 dbus_set_error (error,
1261 DBUS_ERROR_NAME_HAS_NO_OWNER,
1262 "Could not get owner of name '%s': no such name", text);
1267 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1268 if (base_name == NULL)
1270 /* FIXME - how is this error possible? */
1271 dbus_set_error (error,
1273 "Could not determine unique name for '%s'", text);
1276 _dbus_assert (*base_name == ':');
1279 _dbus_assert (base_name != NULL);
1281 reply = dbus_message_new_method_return (message);
1285 if (! dbus_message_append_args (reply,
1286 DBUS_TYPE_STRING, &base_name,
1290 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1293 dbus_message_unref (reply);
1298 BUS_SET_OOM (error);
1301 _DBUS_ASSERT_ERROR_IS_SET (error);
1303 dbus_message_unref (reply);
1308 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1309 BusTransaction *transaction,
1310 DBusMessage *message,
1314 DBusList *base_names;
1317 BusRegistry *registry;
1318 BusService *service;
1320 DBusMessageIter iter, array_iter;
1321 char *dbus_service_name = DBUS_SERVICE_DBUS;
1323 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1325 registry = bus_connection_get_registry (connection);
1331 if (! dbus_message_get_args (message, error,
1332 DBUS_TYPE_STRING, &text,
1336 _dbus_string_init_const (&str, text);
1337 service = bus_registry_lookup (registry, &str);
1338 if (service == NULL &&
1339 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1341 /* ORG_FREEDESKTOP_DBUS owns itself */
1342 if (! _dbus_list_append (&base_names, dbus_service_name))
1345 else if (service == NULL)
1347 dbus_set_error (error,
1348 DBUS_ERROR_NAME_HAS_NO_OWNER,
1349 "Could not get owners of name '%s': no such name", text);
1354 if (!bus_service_list_queued_owners (service,
1360 _dbus_assert (base_names != NULL);
1362 reply = dbus_message_new_method_return (message);
1366 dbus_message_iter_init_append (reply, &iter);
1367 if (!dbus_message_iter_open_container (&iter,
1369 DBUS_TYPE_STRING_AS_STRING,
1373 link = _dbus_list_get_first_link (&base_names);
1374 while (link != NULL)
1378 _dbus_assert (link->data != NULL);
1379 uname = (char *)link->data;
1381 if (!dbus_message_iter_append_basic (&array_iter,
1386 link = _dbus_list_get_next_link (&base_names, link);
1389 if (! dbus_message_iter_close_container (&iter, &array_iter))
1393 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1396 dbus_message_unref (reply);
1401 BUS_SET_OOM (error);
1404 _DBUS_ASSERT_ERROR_IS_SET (error);
1406 dbus_message_unref (reply);
1409 _dbus_list_clear (&base_names);
1415 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1416 BusTransaction *transaction,
1417 DBusMessage *message,
1420 DBusConnection *conn;
1423 dbus_uint32_t uid32;
1424 const char *service;
1426 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1428 reply = dbus_message_new_method_return (message);
1432 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1434 if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
1439 conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1445 if (!dbus_connection_get_unix_user (conn, &uid))
1447 dbus_set_error (error,
1449 "Could not determine UID for '%s'", service);
1455 if (! dbus_message_append_args (reply,
1456 DBUS_TYPE_UINT32, &uid32,
1460 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1463 dbus_message_unref (reply);
1468 BUS_SET_OOM (error);
1471 _DBUS_ASSERT_ERROR_IS_SET (error);
1473 dbus_message_unref (reply);
1478 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1479 BusTransaction *transaction,
1480 DBusMessage *message,
1483 DBusConnection *conn;
1486 dbus_uint32_t pid32;
1487 const char *service;
1489 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1491 reply = dbus_message_new_method_return (message);
1495 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1497 if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
1502 conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1510 if (!dbus_connection_get_unix_process_id (conn, &pid))
1512 dbus_set_error (error,
1513 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1514 "Could not determine PID for '%s'", service);
1520 if (! dbus_message_append_args (reply,
1521 DBUS_TYPE_UINT32, &pid32,
1525 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1528 dbus_message_unref (reply);
1533 BUS_SET_OOM (error);
1536 _DBUS_ASSERT_ERROR_IS_SET (error);
1538 dbus_message_unref (reply);
1543 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1544 BusTransaction *transaction,
1545 DBusMessage *message,
1548 DBusConnection *conn;
1551 dbus_uint32_t data_size;
1552 const char *service;
1554 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1558 conn = bus_driver_get_conn_helper (connection, message,
1559 "audit session data", &service, error);
1564 reply = dbus_message_new_method_return (message);
1568 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1570 dbus_set_error (error,
1571 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1572 "Could not determine audit session data for '%s'", service);
1576 if (! dbus_message_append_args (reply,
1577 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1581 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1584 dbus_message_unref (reply);
1589 BUS_SET_OOM (error);
1592 _DBUS_ASSERT_ERROR_IS_SET (error);
1594 dbus_message_unref (reply);
1599 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1600 BusTransaction *transaction,
1601 DBusMessage *message,
1604 DBusConnection *conn;
1606 BusSELinuxID *context;
1607 const char *service;
1609 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1611 reply = dbus_message_new_method_return (message);
1615 if(bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1617 if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
1622 conn = bus_driver_get_conn_helper (connection, message, "security context",
1628 context = bus_connection_get_selinux_id (conn);
1631 dbus_set_error (error,
1632 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1633 "Could not determine security context for '%s'", service);
1637 if (! bus_selinux_append_context (reply, context, error))
1641 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1644 dbus_message_unref (reply);
1649 BUS_SET_OOM (error);
1652 _DBUS_ASSERT_ERROR_IS_SET (error);
1654 dbus_message_unref (reply);
1659 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1660 BusTransaction *transaction,
1661 DBusMessage *message,
1664 DBusConnection *conn;
1666 DBusMessageIter reply_iter;
1667 DBusMessageIter array_iter;
1668 unsigned long ulong_val;
1669 const char *service;
1671 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1673 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1677 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction))) //todo kdbus incl
1679 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1685 /* we can't represent > 32-bit pids; if your system needs them, please
1686 * add ProcessID64 to the spec or something */
1687 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1688 ulong_val <= _DBUS_UINT32_MAX)
1690 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1694 /* we can't represent > 32-bit uids; if your system needs them, please
1695 * add UnixUserID64 to the spec or something */
1696 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1697 ulong_val <= _DBUS_UINT32_MAX)
1699 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1705 if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
1707 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1713 if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
1715 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1722 if (!_dbus_asv_close (&reply_iter, &array_iter))
1725 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1727 /* this time we don't want to close the iterator again, so just
1728 * get rid of the message */
1729 dbus_message_unref (reply);
1737 BUS_SET_OOM (error);
1740 _DBUS_ASSERT_ERROR_IS_SET (error);
1744 _dbus_asv_abandon (&reply_iter, &array_iter);
1745 dbus_message_unref (reply);
1752 bus_driver_handle_reload_config (DBusConnection *connection,
1753 BusTransaction *transaction,
1754 DBusMessage *message,
1757 BusContext *context;
1760 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1764 context = bus_connection_get_context (connection);
1765 if (!bus_context_reload_config (context, error))
1768 reply = dbus_message_new_method_return (message);
1772 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1775 dbus_message_unref (reply);
1779 BUS_SET_OOM (error);
1782 _DBUS_ASSERT_ERROR_IS_SET (error);
1784 dbus_message_unref (reply);
1789 bus_driver_handle_get_id (DBusConnection *connection,
1790 BusTransaction *transaction,
1791 DBusMessage *message,
1794 BusContext *context;
1797 const char *v_STRING;
1799 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1801 if (!_dbus_string_init (&uuid))
1803 BUS_SET_OOM (error);
1809 context = bus_connection_get_context (connection);
1810 if (!bus_context_get_id (context, &uuid))
1813 reply = dbus_message_new_method_return (message);
1817 v_STRING = _dbus_string_get_const_data (&uuid);
1818 if (!dbus_message_append_args (reply,
1819 DBUS_TYPE_STRING, &v_STRING,
1823 _dbus_assert (dbus_message_has_signature (reply, "s"));
1825 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1828 _dbus_string_free (&uuid);
1829 dbus_message_unref (reply);
1833 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1835 BUS_SET_OOM (error);
1838 dbus_message_unref (reply);
1839 _dbus_string_free (&uuid);
1846 const char *in_args;
1847 const char *out_args;
1848 dbus_bool_t (* handler) (DBusConnection *connection,
1849 BusTransaction *transaction,
1850 DBusMessage *message,
1854 /* For speed it might be useful to sort this in order of
1855 * frequency of use (but doesn't matter with only a few items
1858 static const MessageHandler dbus_message_handlers[] = {
1861 DBUS_TYPE_STRING_AS_STRING,
1862 bus_driver_handle_hello },
1864 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1865 DBUS_TYPE_UINT32_AS_STRING,
1866 bus_driver_handle_acquire_service },
1868 DBUS_TYPE_STRING_AS_STRING,
1869 DBUS_TYPE_UINT32_AS_STRING,
1870 bus_driver_handle_release_service },
1871 { "StartServiceByName",
1872 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1873 DBUS_TYPE_UINT32_AS_STRING,
1874 bus_driver_handle_activate_service },
1875 { "UpdateActivationEnvironment",
1876 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,
1878 bus_driver_handle_update_activation_environment },
1880 DBUS_TYPE_STRING_AS_STRING,
1881 DBUS_TYPE_BOOLEAN_AS_STRING,
1882 bus_driver_handle_service_exists },
1885 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1886 bus_driver_handle_list_services },
1887 { "ListActivatableNames",
1889 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1890 bus_driver_handle_list_activatable_services },
1892 DBUS_TYPE_STRING_AS_STRING,
1894 bus_driver_handle_add_match },
1896 DBUS_TYPE_STRING_AS_STRING,
1898 bus_driver_handle_remove_match },
1900 DBUS_TYPE_STRING_AS_STRING,
1901 DBUS_TYPE_STRING_AS_STRING,
1902 bus_driver_handle_get_service_owner },
1903 { "ListQueuedOwners",
1904 DBUS_TYPE_STRING_AS_STRING,
1905 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1906 bus_driver_handle_list_queued_owners },
1907 { "GetConnectionUnixUser",
1908 DBUS_TYPE_STRING_AS_STRING,
1909 DBUS_TYPE_UINT32_AS_STRING,
1910 bus_driver_handle_get_connection_unix_user },
1911 { "GetConnectionUnixProcessID",
1912 DBUS_TYPE_STRING_AS_STRING,
1913 DBUS_TYPE_UINT32_AS_STRING,
1914 bus_driver_handle_get_connection_unix_process_id },
1915 { "GetAdtAuditSessionData",
1916 DBUS_TYPE_STRING_AS_STRING,
1917 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1918 bus_driver_handle_get_adt_audit_session_data },
1919 { "GetConnectionSELinuxSecurityContext",
1920 DBUS_TYPE_STRING_AS_STRING,
1921 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1922 bus_driver_handle_get_connection_selinux_security_context },
1926 bus_driver_handle_reload_config },
1929 DBUS_TYPE_STRING_AS_STRING,
1930 bus_driver_handle_get_id },
1931 { "GetConnectionCredentials", "s", "a{sv}",
1932 bus_driver_handle_get_connection_credentials },
1933 { NULL, NULL, NULL, NULL }
1936 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1937 BusTransaction *, DBusMessage *, DBusError *);
1939 static const MessageHandler introspectable_message_handlers[] = {
1940 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1941 { NULL, NULL, NULL, NULL }
1944 #ifdef DBUS_ENABLE_STATS
1945 static const MessageHandler stats_message_handlers[] = {
1946 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1947 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1948 { NULL, NULL, NULL, NULL }
1954 const MessageHandler *message_handlers;
1955 const char *extra_introspection;
1958 /* These should ideally be sorted by frequency of use, although it
1959 * probably doesn't matter with this few items */
1960 static InterfaceHandler interface_handlers[] = {
1961 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1962 " <signal name=\"NameOwnerChanged\">\n"
1963 " <arg type=\"s\"/>\n"
1964 " <arg type=\"s\"/>\n"
1965 " <arg type=\"s\"/>\n"
1967 " <signal name=\"NameLost\">\n"
1968 " <arg type=\"s\"/>\n"
1970 " <signal name=\"NameAcquired\">\n"
1971 " <arg type=\"s\"/>\n"
1973 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1974 #ifdef DBUS_ENABLE_STATS
1975 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1977 { NULL, NULL, NULL }
1981 write_args_for_direction (DBusString *xml,
1982 const char *signature,
1985 DBusTypeReader typereader;
1989 _dbus_string_init_const (&sigstr, signature);
1990 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1992 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1994 const DBusString *subsig;
1997 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1998 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
2001 if (!_dbus_string_append_len (xml,
2002 _dbus_string_get_const_data (subsig) + start,
2005 if (!_dbus_string_append (xml, "\"/>\n"))
2008 _dbus_type_reader_next (&typereader);
2016 bus_driver_generate_introspect_string (DBusString *xml)
2018 const InterfaceHandler *ih;
2019 const MessageHandler *mh;
2021 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2023 if (!_dbus_string_append (xml, "<node>\n"))
2026 for (ih = interface_handlers; ih->name != NULL; ih++)
2028 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2032 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2034 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2038 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2041 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2044 if (!_dbus_string_append (xml, " </method>\n"))
2048 if (ih->extra_introspection != NULL &&
2049 !_dbus_string_append (xml, ih->extra_introspection))
2052 if (!_dbus_string_append (xml, " </interface>\n"))
2056 if (!_dbus_string_append (xml, "</node>\n"))
2063 bus_driver_handle_introspect (DBusConnection *connection,
2064 BusTransaction *transaction,
2065 DBusMessage *message,
2070 const char *v_STRING;
2072 _dbus_verbose ("Introspect() on bus driver\n");
2074 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2078 if (! dbus_message_get_args (message, error,
2081 _DBUS_ASSERT_ERROR_IS_SET (error);
2085 if (!_dbus_string_init (&xml))
2087 BUS_SET_OOM (error);
2091 if (!bus_driver_generate_introspect_string (&xml))
2094 v_STRING = _dbus_string_get_const_data (&xml);
2096 reply = dbus_message_new_method_return (message);
2100 if (! dbus_message_append_args (reply,
2101 DBUS_TYPE_STRING, &v_STRING,
2105 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2108 dbus_message_unref (reply);
2109 _dbus_string_free (&xml);
2114 BUS_SET_OOM (error);
2117 dbus_message_unref (reply);
2119 _dbus_string_free (&xml);
2125 bus_driver_handle_message (DBusConnection *connection,
2126 BusTransaction *transaction,
2127 DBusMessage *message,
2130 const char *name, *interface;
2131 const InterfaceHandler *ih;
2132 const MessageHandler *mh;
2133 dbus_bool_t found_interface = FALSE;
2135 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2137 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2139 BusContext *context;
2141 context = bus_connection_get_context (connection);
2142 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2145 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2147 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2148 return TRUE; /* we just ignore this */
2151 /* may be NULL, which means "any interface will do" */
2152 interface = dbus_message_get_interface (message);
2154 _dbus_assert (dbus_message_get_member (message) != NULL);
2156 name = dbus_message_get_member (message);
2158 _dbus_verbose ("Driver got a method call: %s\n", name);
2160 /* security checks should have kept this from getting here */
2161 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2162 strcmp (name, "Hello") == 0);
2164 for (ih = interface_handlers; ih->name != NULL; ih++)
2166 if (interface != NULL && strcmp (interface, ih->name) != 0)
2169 found_interface = TRUE;
2171 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2173 if (strcmp (mh->name, name) != 0)
2176 _dbus_verbose ("Found driver handler for %s\n", name);
2178 if (!dbus_message_has_signature (message, mh->in_args))
2180 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2181 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2182 name, dbus_message_get_signature (message),
2185 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2186 "Call to %s has wrong args (%s, expected %s)\n",
2187 name, dbus_message_get_signature (message),
2189 _DBUS_ASSERT_ERROR_IS_SET (error);
2193 if ((* mh->handler) (connection, transaction, message, error))
2195 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2196 _dbus_verbose ("Driver handler succeeded\n");
2201 _DBUS_ASSERT_ERROR_IS_SET (error);
2202 _dbus_verbose ("Driver handler returned failure\n");
2208 _dbus_verbose ("No driver handler for message \"%s\"\n",
2211 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2212 "%s does not understand message %s",
2213 DBUS_SERVICE_DBUS, name);
2219 bus_driver_remove_connection (DBusConnection *connection)
2221 /* FIXME 1.0 Does nothing for now, should unregister the connection
2222 * with the bus driver.