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)))
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)))
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)))
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)))
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))) //not needed for kdbus, we got it from kdbus_list_services
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)))
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 if (!bus_transaction_send_from_driver (transaction, connection, reply))
699 dbus_message_unref (reply);
704 bus_driver_handle_release_service (DBusConnection *connection,
705 BusTransaction *transaction,
706 DBusMessage *message,
710 DBusString service_name;
712 dbus_uint32_t service_reply;
714 BusRegistry *registry;
716 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
718 if (!dbus_message_get_args (message, error,
719 DBUS_TYPE_STRING, &name,
723 _dbus_verbose ("Trying to release name %s\n", name);
728 _dbus_string_init_const (&service_name, name);
729 registry = bus_connection_get_registry (connection);
731 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
733 if (!bus_registry_release_service_kdbus (dbus_message_get_sender(message), connection,
734 &service_name, &service_reply,
738 else if (!bus_registry_release_service (registry, connection,
739 &service_name, &service_reply,
743 reply = dbus_message_new_method_return (message);
750 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
756 if (!bus_transaction_send_from_driver (transaction, connection, reply))
766 dbus_message_unref (reply);
771 bus_driver_handle_service_exists (DBusConnection *connection,
772 BusTransaction *transaction,
773 DBusMessage *message,
777 DBusString service_name;
779 dbus_bool_t service_exists;
782 BusRegistry *registry;
784 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
786 registry = bus_connection_get_registry (connection);
788 if (!dbus_message_get_args (message, error,
789 DBUS_TYPE_STRING, &name,
795 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
797 service_exists = TRUE;
801 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
804 struct nameInfo info;
806 inter_ret = kdbus_NameQuery(name, dbus_connection_get_transport(connection), &info);
807 if((inter_ret == 0) || (inter_ret == -ENOENT))
808 service_exists = (inter_ret == 0) ? TRUE : FALSE;
811 _dbus_verbose("kdbus error checking if name exists: err %d (%m)\n", errno);
812 dbus_set_error (error, DBUS_ERROR_FAILED, "Could not determine whether name '%s' exists", name);
813 service_exists = FALSE;
818 _dbus_string_init_const (&service_name, name);
819 service = bus_registry_lookup (registry, &service_name);
820 service_exists = service != NULL;
824 reply = dbus_message_new_method_return (message);
831 if (!dbus_message_append_args (reply,
832 DBUS_TYPE_BOOLEAN, &service_exists,
839 if (!bus_transaction_send_from_driver (transaction, connection, reply))
849 dbus_message_unref (reply);
855 bus_driver_handle_activate_service (DBusConnection *connection,
856 BusTransaction *transaction,
857 DBusMessage *message,
863 BusActivation *activation;
865 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
867 activation = bus_connection_get_activation (connection);
869 if (!dbus_message_get_args (message, error,
870 DBUS_TYPE_STRING, &name,
871 DBUS_TYPE_UINT32, &flags,
874 _DBUS_ASSERT_ERROR_IS_SET (error);
875 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
881 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
882 message, name, error))
884 _DBUS_ASSERT_ERROR_IS_SET (error);
885 _dbus_verbose ("bus_activation_activate_service() failed\n");
896 send_ack_reply (DBusConnection *connection,
897 BusTransaction *transaction,
898 DBusMessage *message,
903 if (dbus_message_get_no_reply (message))
906 reply = dbus_message_new_method_return (message);
913 if (!bus_transaction_send_from_driver (transaction, connection, reply))
916 dbus_message_unref (reply);
920 dbus_message_unref (reply);
926 bus_driver_handle_update_activation_environment (DBusConnection *connection,
927 BusTransaction *transaction,
928 DBusMessage *message,
932 BusActivation *activation;
933 DBusMessageIter iter;
934 DBusMessageIter dict_iter;
935 DBusMessageIter dict_entry_iter;
938 DBusList *keys, *key_link;
939 DBusList *values, *value_link;
941 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
943 activation = bus_connection_get_activation (connection);
945 dbus_message_iter_init (message, &iter);
947 /* The message signature has already been checked for us,
948 * so let's just assert it's right.
950 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
952 dbus_message_iter_recurse (&iter, &dict_iter);
956 /* Then loop through the sent dictionary, add the location of
957 * the environment keys and values to lists. The result will
958 * be in reverse order, so we don't have to constantly search
959 * for the end of the list in a loop.
963 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
965 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
967 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
973 dbus_message_iter_get_basic (&dict_entry_iter, &key);
974 dbus_message_iter_next (&dict_entry_iter);
976 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
978 if (value_type != DBUS_TYPE_STRING)
981 dbus_message_iter_get_basic (&dict_entry_iter, &value);
983 if (!_dbus_list_append (&keys, key))
989 if (!_dbus_list_append (&values, value))
995 dbus_message_iter_next (&dict_entry_iter);
998 if (key_type != DBUS_TYPE_INVALID)
1001 dbus_message_iter_next (&dict_iter);
1004 if (array_type != DBUS_TYPE_INVALID)
1007 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
1010 value_link = values;
1011 while (key_link != NULL)
1016 key = key_link->data;
1017 value = value_link->data;
1019 if (!bus_activation_set_environment_variable (activation,
1022 _DBUS_ASSERT_ERROR_IS_SET (error);
1023 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
1026 key_link = _dbus_list_get_next_link (&keys, key_link);
1027 value_link = _dbus_list_get_next_link (&values, value_link);
1030 /* FIXME: We can fail early having set only some of the environment variables,
1031 * (because of OOM failure). It's sort of hard to fix and it doesn't really
1032 * matter, so we're punting for now.
1034 if (key_link != NULL)
1037 if (!send_ack_reply (connection, transaction,
1044 _dbus_list_clear (&keys);
1045 _dbus_list_clear (&values);
1050 bus_driver_handle_add_match (DBusConnection *connection,
1051 BusTransaction *transaction,
1052 DBusMessage *message,
1058 BusMatchmaker *matchmaker;
1060 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1065 if (bus_connection_get_n_match_rules (connection) >=
1066 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1068 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1069 "Connection \"%s\" is not allowed to add more match rules "
1070 "(increase limits in configuration file if required)",
1071 bus_connection_is_active (connection) ?
1072 bus_connection_get_name (connection) :
1077 if (!dbus_message_get_args (message, error,
1078 DBUS_TYPE_STRING, &text,
1081 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1085 _dbus_string_init_const (&str, text);
1087 rule = bus_match_rule_parse (connection, &str, error);
1091 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1094 if (!kdbus_add_match_rule (connection, message, text, error))
1097 if (!send_ack_reply (connection, transaction,
1103 matchmaker = bus_connection_get_matchmaker (connection);
1105 if (!bus_matchmaker_add_rule (matchmaker, rule))
1107 BUS_SET_OOM (error);
1111 if (!send_ack_reply (connection, transaction,
1114 bus_matchmaker_remove_rule (matchmaker, rule);
1119 bus_match_rule_unref (rule);
1124 _DBUS_ASSERT_ERROR_IS_SET (error);
1126 bus_match_rule_unref (rule);
1131 bus_driver_handle_remove_match (DBusConnection *connection,
1132 BusTransaction *transaction,
1133 DBusMessage *message,
1139 BusMatchmaker *matchmaker;
1141 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1146 if (!dbus_message_get_args (message, error,
1147 DBUS_TYPE_STRING, &text,
1150 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1154 _dbus_string_init_const (&str, text);
1156 rule = bus_match_rule_parse (connection, &str, error);
1160 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1162 if(!kdbus_remove_match(connection, message, error))
1166 /* Send the ack before we remove the rule, since the ack is undone
1167 * on transaction cancel, but rule removal isn't.
1169 if (!send_ack_reply (connection, transaction,
1173 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1175 matchmaker = bus_connection_get_matchmaker (connection);
1177 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1181 bus_match_rule_unref (rule);
1186 _DBUS_ASSERT_ERROR_IS_SET (error);
1188 bus_match_rule_unref (rule);
1193 bus_driver_handle_get_service_owner (DBusConnection *connection,
1194 BusTransaction *transaction,
1195 DBusMessage *message,
1199 const char *base_name;
1201 BusRegistry *registry;
1202 BusService *service;
1204 char unique_name[(unsigned int)(snprintf((char*)base_name, 0, "%llu", ULLONG_MAX) + sizeof(":1."))];
1206 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1208 registry = bus_connection_get_registry (connection);
1213 if (! dbus_message_get_args (message, error,
1214 DBUS_TYPE_STRING, &text,
1218 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1221 struct nameInfo info;
1223 ret = kdbus_NameQuery(text, dbus_connection_get_transport(connection), &info);
1224 if(ret == 0) //unique id of the name
1226 sprintf(unique_name, ":1.%llu", (unsigned long long int)info.uniqueId);
1227 _dbus_verbose("Unique name discovered:%s\n", unique_name);
1228 base_name = unique_name;
1230 else if(ret == -ENOENT) //name has no owner
1232 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
1233 "Could not get owner of name '%s': no such name", text);
1238 _dbus_verbose("kdbus error sending name query: err %d (%m)\n", errno);
1239 dbus_set_error (error, DBUS_ERROR_FAILED,
1240 "Could not determine unique name for '%s'", text);
1246 _dbus_string_init_const (&str, text);
1247 service = bus_registry_lookup (registry, &str);
1248 if (service == NULL &&
1249 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1251 /* ORG_FREEDESKTOP_DBUS owns itself */
1252 base_name = DBUS_SERVICE_DBUS;
1254 else if (service == NULL)
1256 dbus_set_error (error,
1257 DBUS_ERROR_NAME_HAS_NO_OWNER,
1258 "Could not get owner of name '%s': no such name", text);
1263 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1264 if (base_name == NULL)
1266 /* FIXME - how is this error possible? */
1267 dbus_set_error (error,
1269 "Could not determine unique name for '%s'", text);
1272 _dbus_assert (*base_name == ':');
1275 _dbus_assert (base_name != NULL);
1277 reply = dbus_message_new_method_return (message);
1281 if (! dbus_message_append_args (reply,
1282 DBUS_TYPE_STRING, &base_name,
1286 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1289 dbus_message_unref (reply);
1294 BUS_SET_OOM (error);
1297 _DBUS_ASSERT_ERROR_IS_SET (error);
1299 dbus_message_unref (reply);
1304 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1305 BusTransaction *transaction,
1306 DBusMessage *message,
1310 DBusList *base_names;
1313 BusRegistry *registry;
1314 BusService *service;
1316 DBusMessageIter iter, array_iter;
1317 char *dbus_service_name = DBUS_SERVICE_DBUS;
1319 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1321 registry = bus_connection_get_registry (connection);
1327 if (! dbus_message_get_args (message, error,
1328 DBUS_TYPE_STRING, &text,
1332 _dbus_string_init_const (&str, text);
1333 service = bus_registry_lookup (registry, &str);
1334 if (service == NULL &&
1335 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1337 /* ORG_FREEDESKTOP_DBUS owns itself */
1338 if (! _dbus_list_append (&base_names, dbus_service_name))
1341 else if (service == NULL)
1343 dbus_set_error (error,
1344 DBUS_ERROR_NAME_HAS_NO_OWNER,
1345 "Could not get owners of name '%s': no such name", text);
1350 if (!bus_service_list_queued_owners (service,
1356 _dbus_assert (base_names != NULL);
1358 reply = dbus_message_new_method_return (message);
1362 dbus_message_iter_init_append (reply, &iter);
1363 if (!dbus_message_iter_open_container (&iter,
1365 DBUS_TYPE_STRING_AS_STRING,
1369 link = _dbus_list_get_first_link (&base_names);
1370 while (link != NULL)
1374 _dbus_assert (link->data != NULL);
1375 uname = (char *)link->data;
1377 if (!dbus_message_iter_append_basic (&array_iter,
1382 link = _dbus_list_get_next_link (&base_names, link);
1385 if (! dbus_message_iter_close_container (&iter, &array_iter))
1389 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1392 dbus_message_unref (reply);
1397 BUS_SET_OOM (error);
1400 _DBUS_ASSERT_ERROR_IS_SET (error);
1402 dbus_message_unref (reply);
1405 _dbus_list_clear (&base_names);
1411 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1412 BusTransaction *transaction,
1413 DBusMessage *message,
1416 DBusConnection *conn;
1419 dbus_uint32_t uid32;
1420 const char *service;
1422 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1424 reply = dbus_message_new_method_return (message);
1428 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1430 if(!kdbus_get_connection_unix_user(connection, message, &uid, error))
1435 conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1441 if (!dbus_connection_get_unix_user (conn, &uid))
1443 dbus_set_error (error,
1445 "Could not determine UID for '%s'", service);
1451 if (! dbus_message_append_args (reply,
1452 DBUS_TYPE_UINT32, &uid32,
1456 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1459 dbus_message_unref (reply);
1464 BUS_SET_OOM (error);
1467 _DBUS_ASSERT_ERROR_IS_SET (error);
1469 dbus_message_unref (reply);
1474 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1475 BusTransaction *transaction,
1476 DBusMessage *message,
1479 DBusConnection *conn;
1482 dbus_uint32_t pid32;
1483 const char *service;
1485 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1487 reply = dbus_message_new_method_return (message);
1491 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1493 if(!kdbus_get_connection_unix_process_id(connection, message, &pid, error))
1498 conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1506 if (!dbus_connection_get_unix_process_id (conn, &pid))
1508 dbus_set_error (error,
1509 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1510 "Could not determine PID for '%s'", service);
1516 if (! dbus_message_append_args (reply,
1517 DBUS_TYPE_UINT32, &pid32,
1521 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1524 dbus_message_unref (reply);
1529 BUS_SET_OOM (error);
1532 _DBUS_ASSERT_ERROR_IS_SET (error);
1534 dbus_message_unref (reply);
1539 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1540 BusTransaction *transaction,
1541 DBusMessage *message,
1544 DBusConnection *conn;
1547 dbus_uint32_t data_size;
1548 const char *service;
1550 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1554 conn = bus_driver_get_conn_helper (connection, message,
1555 "audit session data", &service, error);
1560 reply = dbus_message_new_method_return (message);
1564 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1566 dbus_set_error (error,
1567 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1568 "Could not determine audit session data for '%s'", service);
1572 if (! dbus_message_append_args (reply,
1573 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1577 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1580 dbus_message_unref (reply);
1585 BUS_SET_OOM (error);
1588 _DBUS_ASSERT_ERROR_IS_SET (error);
1590 dbus_message_unref (reply);
1595 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1596 BusTransaction *transaction,
1597 DBusMessage *message,
1600 DBusConnection *conn;
1602 BusSELinuxID *context;
1603 const char *service;
1605 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1607 reply = dbus_message_new_method_return (message);
1611 if(bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1613 if(!kdbus_get_connection_unix_selinux_security_context(connection, message, reply, error))
1618 conn = bus_driver_get_conn_helper (connection, message, "security context",
1624 context = bus_connection_get_selinux_id (conn);
1627 dbus_set_error (error,
1628 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1629 "Could not determine security context for '%s'", service);
1633 if (! bus_selinux_append_context (reply, context, error))
1637 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1640 dbus_message_unref (reply);
1645 BUS_SET_OOM (error);
1648 _DBUS_ASSERT_ERROR_IS_SET (error);
1650 dbus_message_unref (reply);
1655 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1656 BusTransaction *transaction,
1657 DBusMessage *message,
1660 DBusConnection *conn;
1662 DBusMessageIter reply_iter;
1663 DBusMessageIter array_iter;
1664 unsigned long ulong_val;
1665 const char *service;
1667 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1669 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1673 if(!bus_context_is_kdbus(bus_transaction_get_context (transaction)))
1675 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1681 /* we can't represent > 32-bit pids; if your system needs them, please
1682 * add ProcessID64 to the spec or something */
1683 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1684 ulong_val <= _DBUS_UINT32_MAX)
1686 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1690 /* we can't represent > 32-bit uids; if your system needs them, please
1691 * add UnixUserID64 to the spec or something */
1692 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1693 ulong_val <= _DBUS_UINT32_MAX)
1695 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1701 if(kdbus_get_connection_unix_process_id(connection, message, &ulong_val, error))
1703 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1709 if(kdbus_get_connection_unix_user(connection, message, &ulong_val, error))
1711 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1718 if (!_dbus_asv_close (&reply_iter, &array_iter))
1721 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1723 /* this time we don't want to close the iterator again, so just
1724 * get rid of the message */
1725 dbus_message_unref (reply);
1733 BUS_SET_OOM (error);
1736 _DBUS_ASSERT_ERROR_IS_SET (error);
1740 _dbus_asv_abandon (&reply_iter, &array_iter);
1741 dbus_message_unref (reply);
1748 bus_driver_handle_reload_config (DBusConnection *connection,
1749 BusTransaction *transaction,
1750 DBusMessage *message,
1753 BusContext *context;
1756 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1760 context = bus_connection_get_context (connection);
1761 if (!bus_context_reload_config (context, error))
1764 reply = dbus_message_new_method_return (message);
1768 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1771 dbus_message_unref (reply);
1775 BUS_SET_OOM (error);
1778 _DBUS_ASSERT_ERROR_IS_SET (error);
1780 dbus_message_unref (reply);
1785 bus_driver_handle_get_id (DBusConnection *connection,
1786 BusTransaction *transaction,
1787 DBusMessage *message,
1790 BusContext *context;
1793 const char *v_STRING;
1795 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1797 if (!_dbus_string_init (&uuid))
1799 BUS_SET_OOM (error);
1805 context = bus_connection_get_context (connection);
1806 if (!bus_context_get_id (context, &uuid))
1809 reply = dbus_message_new_method_return (message);
1813 v_STRING = _dbus_string_get_const_data (&uuid);
1814 if (!dbus_message_append_args (reply,
1815 DBUS_TYPE_STRING, &v_STRING,
1819 _dbus_assert (dbus_message_has_signature (reply, "s"));
1821 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1824 _dbus_string_free (&uuid);
1825 dbus_message_unref (reply);
1829 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1831 BUS_SET_OOM (error);
1834 dbus_message_unref (reply);
1835 _dbus_string_free (&uuid);
1842 const char *in_args;
1843 const char *out_args;
1844 dbus_bool_t (* handler) (DBusConnection *connection,
1845 BusTransaction *transaction,
1846 DBusMessage *message,
1850 /* For speed it might be useful to sort this in order of
1851 * frequency of use (but doesn't matter with only a few items
1854 static const MessageHandler dbus_message_handlers[] = {
1857 DBUS_TYPE_STRING_AS_STRING,
1858 bus_driver_handle_hello },
1860 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1861 DBUS_TYPE_UINT32_AS_STRING,
1862 bus_driver_handle_acquire_service },
1864 DBUS_TYPE_STRING_AS_STRING,
1865 DBUS_TYPE_UINT32_AS_STRING,
1866 bus_driver_handle_release_service },
1867 { "StartServiceByName",
1868 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1869 DBUS_TYPE_UINT32_AS_STRING,
1870 bus_driver_handle_activate_service },
1871 { "UpdateActivationEnvironment",
1872 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,
1874 bus_driver_handle_update_activation_environment },
1876 DBUS_TYPE_STRING_AS_STRING,
1877 DBUS_TYPE_BOOLEAN_AS_STRING,
1878 bus_driver_handle_service_exists },
1881 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1882 bus_driver_handle_list_services },
1883 { "ListActivatableNames",
1885 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1886 bus_driver_handle_list_activatable_services },
1888 DBUS_TYPE_STRING_AS_STRING,
1890 bus_driver_handle_add_match },
1892 DBUS_TYPE_STRING_AS_STRING,
1894 bus_driver_handle_remove_match },
1896 DBUS_TYPE_STRING_AS_STRING,
1897 DBUS_TYPE_STRING_AS_STRING,
1898 bus_driver_handle_get_service_owner },
1899 { "ListQueuedOwners",
1900 DBUS_TYPE_STRING_AS_STRING,
1901 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1902 bus_driver_handle_list_queued_owners },
1903 { "GetConnectionUnixUser",
1904 DBUS_TYPE_STRING_AS_STRING,
1905 DBUS_TYPE_UINT32_AS_STRING,
1906 bus_driver_handle_get_connection_unix_user },
1907 { "GetConnectionUnixProcessID",
1908 DBUS_TYPE_STRING_AS_STRING,
1909 DBUS_TYPE_UINT32_AS_STRING,
1910 bus_driver_handle_get_connection_unix_process_id },
1911 { "GetAdtAuditSessionData",
1912 DBUS_TYPE_STRING_AS_STRING,
1913 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1914 bus_driver_handle_get_adt_audit_session_data },
1915 { "GetConnectionSELinuxSecurityContext",
1916 DBUS_TYPE_STRING_AS_STRING,
1917 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1918 bus_driver_handle_get_connection_selinux_security_context },
1922 bus_driver_handle_reload_config },
1925 DBUS_TYPE_STRING_AS_STRING,
1926 bus_driver_handle_get_id },
1927 { "GetConnectionCredentials", "s", "a{sv}",
1928 bus_driver_handle_get_connection_credentials },
1929 { NULL, NULL, NULL, NULL }
1932 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1933 BusTransaction *, DBusMessage *, DBusError *);
1935 static const MessageHandler introspectable_message_handlers[] = {
1936 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1937 { NULL, NULL, NULL, NULL }
1940 #ifdef DBUS_ENABLE_STATS
1941 static const MessageHandler stats_message_handlers[] = {
1942 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1943 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1944 { NULL, NULL, NULL, NULL }
1950 const MessageHandler *message_handlers;
1951 const char *extra_introspection;
1954 /* These should ideally be sorted by frequency of use, although it
1955 * probably doesn't matter with this few items */
1956 static InterfaceHandler interface_handlers[] = {
1957 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1958 " <signal name=\"NameOwnerChanged\">\n"
1959 " <arg type=\"s\"/>\n"
1960 " <arg type=\"s\"/>\n"
1961 " <arg type=\"s\"/>\n"
1963 " <signal name=\"NameLost\">\n"
1964 " <arg type=\"s\"/>\n"
1966 " <signal name=\"NameAcquired\">\n"
1967 " <arg type=\"s\"/>\n"
1969 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1970 #ifdef DBUS_ENABLE_STATS
1971 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1973 { NULL, NULL, NULL }
1977 write_args_for_direction (DBusString *xml,
1978 const char *signature,
1981 DBusTypeReader typereader;
1985 _dbus_string_init_const (&sigstr, signature);
1986 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1988 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1990 const DBusString *subsig;
1993 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1994 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1997 if (!_dbus_string_append_len (xml,
1998 _dbus_string_get_const_data (subsig) + start,
2001 if (!_dbus_string_append (xml, "\"/>\n"))
2004 _dbus_type_reader_next (&typereader);
2012 bus_driver_generate_introspect_string (DBusString *xml)
2014 const InterfaceHandler *ih;
2015 const MessageHandler *mh;
2017 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
2019 if (!_dbus_string_append (xml, "<node>\n"))
2022 for (ih = interface_handlers; ih->name != NULL; ih++)
2024 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
2028 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2030 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
2034 if (!write_args_for_direction (xml, mh->in_args, TRUE))
2037 if (!write_args_for_direction (xml, mh->out_args, FALSE))
2040 if (!_dbus_string_append (xml, " </method>\n"))
2044 if (ih->extra_introspection != NULL &&
2045 !_dbus_string_append (xml, ih->extra_introspection))
2048 if (!_dbus_string_append (xml, " </interface>\n"))
2052 if (!_dbus_string_append (xml, "</node>\n"))
2059 bus_driver_handle_introspect (DBusConnection *connection,
2060 BusTransaction *transaction,
2061 DBusMessage *message,
2066 const char *v_STRING;
2068 _dbus_verbose ("Introspect() on bus driver\n");
2070 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2074 if (! dbus_message_get_args (message, error,
2077 _DBUS_ASSERT_ERROR_IS_SET (error);
2081 if (!_dbus_string_init (&xml))
2083 BUS_SET_OOM (error);
2087 if (!bus_driver_generate_introspect_string (&xml))
2090 v_STRING = _dbus_string_get_const_data (&xml);
2092 reply = dbus_message_new_method_return (message);
2096 if (! dbus_message_append_args (reply,
2097 DBUS_TYPE_STRING, &v_STRING,
2101 if (! bus_transaction_send_from_driver (transaction, connection, reply))
2104 dbus_message_unref (reply);
2105 _dbus_string_free (&xml);
2110 BUS_SET_OOM (error);
2113 dbus_message_unref (reply);
2115 _dbus_string_free (&xml);
2121 bus_driver_handle_message (DBusConnection *connection,
2122 BusTransaction *transaction,
2123 DBusMessage *message,
2126 const char *name, *interface;
2127 const InterfaceHandler *ih;
2128 const MessageHandler *mh;
2129 dbus_bool_t found_interface = FALSE;
2131 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2133 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2135 BusContext *context;
2137 context = bus_connection_get_context (connection);
2138 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2141 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2143 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2144 return TRUE; /* we just ignore this */
2147 /* may be NULL, which means "any interface will do" */
2148 interface = dbus_message_get_interface (message);
2150 _dbus_assert (dbus_message_get_member (message) != NULL);
2152 name = dbus_message_get_member (message);
2154 _dbus_verbose ("Driver got a method call: %s\n", name);
2156 /* security checks should have kept this from getting here */
2157 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2158 strcmp (name, "Hello") == 0);
2160 for (ih = interface_handlers; ih->name != NULL; ih++)
2162 if (interface != NULL && strcmp (interface, ih->name) != 0)
2165 found_interface = TRUE;
2167 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2169 if (strcmp (mh->name, name) != 0)
2172 _dbus_verbose ("Found driver handler for %s\n", name);
2174 if (!dbus_message_has_signature (message, mh->in_args))
2176 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2177 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2178 name, dbus_message_get_signature (message),
2181 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2182 "Call to %s has wrong args (%s, expected %s)\n",
2183 name, dbus_message_get_signature (message),
2185 _DBUS_ASSERT_ERROR_IS_SET (error);
2189 if ((* mh->handler) (connection, transaction, message, error))
2191 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2192 _dbus_verbose ("Driver handler succeeded\n");
2197 _DBUS_ASSERT_ERROR_IS_SET (error);
2198 _dbus_verbose ("Driver handler returned failure\n");
2204 _dbus_verbose ("No driver handler for message \"%s\"\n",
2207 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2208 "%s does not understand message %s",
2209 DBUS_SERVICE_DBUS, name);
2215 bus_driver_remove_connection (DBusConnection *connection)
2217 /* FIXME 1.0 Does nothing for now, should unregister the connection
2218 * with the bus driver.