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>
43 static DBusConnection *
44 bus_driver_get_conn_helper (DBusConnection *connection,
46 const char *what_we_want,
51 BusRegistry *registry;
56 if (!dbus_message_get_args (message, error,
57 DBUS_TYPE_STRING, &name,
61 _dbus_assert (name != NULL);
62 _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
64 registry = bus_connection_get_registry (connection);
65 _dbus_string_init_const (&str, name);
66 serv = bus_registry_lookup (registry, &str);
70 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
71 "Could not get %s of name '%s': no such name",
76 conn = bus_service_get_primary_owners_connection (serv);
77 _dbus_assert (conn != NULL);
85 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
86 DBusMessage *hello_message,
87 BusTransaction *transaction,
91 bus_driver_send_service_owner_changed (const char *service_name,
92 const char *old_owner,
93 const char *new_owner,
94 BusTransaction *transaction,
99 const char *null_service;
101 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
104 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
106 old_owner ? old_owner : null_service,
107 new_owner ? new_owner : null_service);
109 message = dbus_message_new_signal (DBUS_PATH_DBUS,
119 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
122 if (!dbus_message_append_args (message,
123 DBUS_TYPE_STRING, &service_name,
124 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
125 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
129 _dbus_assert (dbus_message_has_signature (message, "sss"));
131 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
132 dbus_message_unref (message);
137 dbus_message_unref (message);
143 bus_driver_send_service_lost (DBusConnection *connection,
144 const char *service_name,
145 BusTransaction *transaction,
148 DBusMessage *message;
150 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
152 message = dbus_message_new_signal (DBUS_PATH_DBUS,
162 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
163 !dbus_message_append_args (message,
164 DBUS_TYPE_STRING, &service_name,
167 dbus_message_unref (message);
172 if (!bus_transaction_send_from_driver (transaction, connection, message))
174 dbus_message_unref (message);
180 dbus_message_unref (message);
186 bus_driver_send_service_acquired (DBusConnection *connection,
187 const char *service_name,
188 BusTransaction *transaction,
191 DBusMessage *message;
193 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
195 message = dbus_message_new_signal (DBUS_PATH_DBUS,
205 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
206 !dbus_message_append_args (message,
207 DBUS_TYPE_STRING, &service_name,
210 dbus_message_unref (message);
215 if (!bus_transaction_send_from_driver (transaction, connection, message))
217 dbus_message_unref (message);
223 dbus_message_unref (message);
229 create_unique_client_name (BusRegistry *registry,
232 /* We never want to use the same unique client name twice, because
233 * we want to guarantee that if you send a message to a given unique
234 * name, you always get the same application. So we use two numbers
235 * for INT_MAX * INT_MAX combinations, should be pretty safe against
238 /* FIXME these should be in BusRegistry rather than static vars */
239 static int next_major_number = 0;
240 static int next_minor_number = 0;
243 len = _dbus_string_get_length (str);
247 /* start out with 1-0, go to 1-1, 1-2, 1-3,
248 * up to 1-MAXINT, then 2-0, 2-1, etc.
250 if (next_minor_number <= 0)
252 next_major_number += 1;
253 next_minor_number = 0;
254 if (next_major_number <= 0)
255 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
258 _dbus_assert (next_major_number > 0);
259 _dbus_assert (next_minor_number >= 0);
261 /* appname:MAJOR-MINOR */
263 if (!_dbus_string_append (str, ":"))
266 if (!_dbus_string_append_int (str, next_major_number))
269 if (!_dbus_string_append (str, "."))
272 if (!_dbus_string_append_int (str, next_minor_number))
275 next_minor_number += 1;
277 /* Check if a client with the name exists */
278 if (bus_registry_lookup (registry, str) == NULL)
281 /* drop the number again, try the next one. */
282 _dbus_string_set_length (str, len);
289 bus_driver_handle_hello (DBusConnection *connection,
290 BusTransaction *transaction,
291 DBusMessage *message,
294 DBusString unique_name;
297 BusRegistry *registry;
298 BusConnections *connections;
300 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 if (bus_connection_is_active (connection))
304 /* We already handled an Hello message for this connection. */
305 dbus_set_error (error, DBUS_ERROR_FAILED,
306 "Already handled an Hello message");
310 /* Note that when these limits are exceeded we don't disconnect the
311 * connection; we just sort of leave it hanging there until it times
312 * out or disconnects itself or is dropped due to the max number of
313 * incomplete connections. It's even OK if the connection wants to
314 * retry the hello message, we support that.
316 connections = bus_connection_get_connections (connection);
317 if (!bus_connections_check_limits (connections, connection,
320 _DBUS_ASSERT_ERROR_IS_SET (error);
324 if (!_dbus_string_init (&unique_name))
332 registry = bus_connection_get_registry (connection);
334 if (!create_unique_client_name (registry, &unique_name))
340 if (!bus_connection_complete (connection, &unique_name, error))
342 _DBUS_ASSERT_ERROR_IS_SET (error);
346 if (!dbus_message_set_sender (message,
347 bus_connection_get_name (connection)))
353 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
356 /* Create the service */
357 service = bus_registry_ensure (registry,
358 &unique_name, connection, 0, transaction, error);
362 _dbus_assert (bus_connection_is_active (connection));
366 _dbus_string_free (&unique_name);
371 bus_driver_send_welcome_message (DBusConnection *connection,
372 DBusMessage *hello_message,
373 BusTransaction *transaction,
376 DBusMessage *welcome;
379 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
381 name = bus_connection_get_name (connection);
382 _dbus_assert (name != NULL);
384 welcome = dbus_message_new_method_return (hello_message);
391 if (!dbus_message_append_args (welcome,
392 DBUS_TYPE_STRING, &name,
395 dbus_message_unref (welcome);
400 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
402 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
404 dbus_message_unref (welcome);
410 dbus_message_unref (welcome);
416 bus_driver_handle_list_services (DBusConnection *connection,
417 BusTransaction *transaction,
418 DBusMessage *message,
424 BusRegistry *registry;
426 DBusMessageIter iter;
429 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
431 registry = bus_connection_get_registry (connection);
433 reply = dbus_message_new_method_return (message);
440 if (!bus_registry_list_services (registry, &services, &len))
442 dbus_message_unref (reply);
447 dbus_message_iter_init_append (reply, &iter);
449 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
450 DBUS_TYPE_STRING_AS_STRING,
453 dbus_free_string_array (services);
454 dbus_message_unref (reply);
460 /* Include the bus driver in the list */
461 const char *v_STRING = DBUS_SERVICE_DBUS;
462 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
465 dbus_free_string_array (services);
466 dbus_message_unref (reply);
475 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
478 dbus_free_string_array (services);
479 dbus_message_unref (reply);
486 dbus_free_string_array (services);
488 if (!dbus_message_iter_close_container (&iter, &sub))
490 dbus_message_unref (reply);
495 if (!bus_transaction_send_from_driver (transaction, connection, reply))
497 dbus_message_unref (reply);
503 dbus_message_unref (reply);
509 bus_driver_handle_list_activatable_services (DBusConnection *connection,
510 BusTransaction *transaction,
511 DBusMessage *message,
517 BusActivation *activation;
519 DBusMessageIter iter;
522 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
524 activation = bus_connection_get_activation (connection);
526 reply = dbus_message_new_method_return (message);
533 if (!bus_activation_list_services (activation, &services, &len))
535 dbus_message_unref (reply);
540 dbus_message_iter_init_append (reply, &iter);
542 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
543 DBUS_TYPE_STRING_AS_STRING,
546 dbus_free_string_array (services);
547 dbus_message_unref (reply);
553 /* Include the bus driver in the list */
554 const char *v_STRING = DBUS_SERVICE_DBUS;
555 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
558 dbus_free_string_array (services);
559 dbus_message_unref (reply);
568 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
571 dbus_free_string_array (services);
572 dbus_message_unref (reply);
579 dbus_free_string_array (services);
581 if (!dbus_message_iter_close_container (&iter, &sub))
583 dbus_message_unref (reply);
588 if (!bus_transaction_send_from_driver (transaction, connection, reply))
590 dbus_message_unref (reply);
596 dbus_message_unref (reply);
602 bus_driver_handle_acquire_service (DBusConnection *connection,
603 BusTransaction *transaction,
604 DBusMessage *message,
608 DBusString service_name;
610 dbus_uint32_t service_reply;
613 BusRegistry *registry;
615 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
617 registry = bus_connection_get_registry (connection);
619 if (!dbus_message_get_args (message, error,
620 DBUS_TYPE_STRING, &name,
621 DBUS_TYPE_UINT32, &flags,
625 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
630 _dbus_string_init_const (&service_name, name);
632 if (!bus_registry_acquire_service (registry, connection,
633 &service_name, flags,
634 &service_reply, transaction,
638 reply = dbus_message_new_method_return (message);
645 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
651 if (!bus_transaction_send_from_driver (transaction, connection, reply))
661 dbus_message_unref (reply);
666 bus_driver_handle_release_service (DBusConnection *connection,
667 BusTransaction *transaction,
668 DBusMessage *message,
672 DBusString service_name;
674 dbus_uint32_t service_reply;
676 BusRegistry *registry;
678 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
680 registry = bus_connection_get_registry (connection);
682 if (!dbus_message_get_args (message, error,
683 DBUS_TYPE_STRING, &name,
687 _dbus_verbose ("Trying to release name %s\n", name);
692 _dbus_string_init_const (&service_name, name);
694 if (!bus_registry_release_service (registry, connection,
695 &service_name, &service_reply,
699 reply = dbus_message_new_method_return (message);
706 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
712 if (!bus_transaction_send_from_driver (transaction, connection, reply))
722 dbus_message_unref (reply);
727 bus_driver_handle_service_exists (DBusConnection *connection,
728 BusTransaction *transaction,
729 DBusMessage *message,
733 DBusString service_name;
735 dbus_bool_t service_exists;
738 BusRegistry *registry;
740 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
742 registry = bus_connection_get_registry (connection);
744 if (!dbus_message_get_args (message, error,
745 DBUS_TYPE_STRING, &name,
751 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
753 service_exists = TRUE;
757 _dbus_string_init_const (&service_name, name);
758 service = bus_registry_lookup (registry, &service_name);
759 service_exists = service != NULL;
762 reply = dbus_message_new_method_return (message);
769 if (!dbus_message_append_args (reply,
770 DBUS_TYPE_BOOLEAN, &service_exists,
777 if (!bus_transaction_send_from_driver (transaction, connection, reply))
787 dbus_message_unref (reply);
793 bus_driver_handle_activate_service (DBusConnection *connection,
794 BusTransaction *transaction,
795 DBusMessage *message,
801 BusActivation *activation;
803 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
805 activation = bus_connection_get_activation (connection);
807 if (!dbus_message_get_args (message, error,
808 DBUS_TYPE_STRING, &name,
809 DBUS_TYPE_UINT32, &flags,
812 _DBUS_ASSERT_ERROR_IS_SET (error);
813 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
819 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
820 message, name, error))
822 _DBUS_ASSERT_ERROR_IS_SET (error);
823 _dbus_verbose ("bus_activation_activate_service() failed\n");
834 send_ack_reply (DBusConnection *connection,
835 BusTransaction *transaction,
836 DBusMessage *message,
841 if (dbus_message_get_no_reply (message))
844 reply = dbus_message_new_method_return (message);
851 if (!bus_transaction_send_from_driver (transaction, connection, reply))
854 dbus_message_unref (reply);
858 dbus_message_unref (reply);
864 bus_driver_handle_update_activation_environment (DBusConnection *connection,
865 BusTransaction *transaction,
866 DBusMessage *message,
870 BusActivation *activation;
871 DBusMessageIter iter;
872 DBusMessageIter dict_iter;
873 DBusMessageIter dict_entry_iter;
876 DBusList *keys, *key_link;
877 DBusList *values, *value_link;
879 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
881 activation = bus_connection_get_activation (connection);
883 dbus_message_iter_init (message, &iter);
885 /* The message signature has already been checked for us,
886 * so let's just assert it's right.
888 #ifndef DBUS_DISABLE_ASSERT
890 int msg_type = dbus_message_iter_get_arg_type (&iter);
892 _dbus_assert (msg_type == DBUS_TYPE_ARRAY);
896 dbus_message_iter_recurse (&iter, &dict_iter);
900 /* Then loop through the sent dictionary, add the location of
901 * the environment keys and values to lists. The result will
902 * be in reverse order, so we don't have to constantly search
903 * for the end of the list in a loop.
907 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
909 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
911 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
917 dbus_message_iter_get_basic (&dict_entry_iter, &key);
918 dbus_message_iter_next (&dict_entry_iter);
920 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
922 if (value_type != DBUS_TYPE_STRING)
925 dbus_message_iter_get_basic (&dict_entry_iter, &value);
927 if (!_dbus_list_append (&keys, key))
933 if (!_dbus_list_append (&values, value))
939 dbus_message_iter_next (&dict_entry_iter);
942 if (key_type != DBUS_TYPE_INVALID)
945 dbus_message_iter_next (&dict_iter);
948 if (array_type != DBUS_TYPE_INVALID)
951 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
955 while (key_link != NULL)
960 key = key_link->data;
961 value = value_link->data;
963 if (!bus_activation_set_environment_variable (activation,
966 _DBUS_ASSERT_ERROR_IS_SET (error);
967 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
970 key_link = _dbus_list_get_next_link (&keys, key_link);
971 value_link = _dbus_list_get_next_link (&values, value_link);
974 /* FIXME: We can fail early having set only some of the environment variables,
975 * (because of OOM failure). It's sort of hard to fix and it doesn't really
976 * matter, so we're punting for now.
978 if (key_link != NULL)
981 if (!send_ack_reply (connection, transaction,
988 _dbus_list_clear (&keys);
989 _dbus_list_clear (&values);
994 bus_driver_handle_add_match (DBusConnection *connection,
995 BusTransaction *transaction,
996 DBusMessage *message,
1002 BusMatchmaker *matchmaker;
1004 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1009 if (bus_connection_get_n_match_rules (connection) >=
1010 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1012 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1013 "Connection \"%s\" is not allowed to add more match rules "
1014 "(increase limits in configuration file if required)",
1015 bus_connection_is_active (connection) ?
1016 bus_connection_get_name (connection) :
1021 if (!dbus_message_get_args (message, error,
1022 DBUS_TYPE_STRING, &text,
1025 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1029 _dbus_string_init_const (&str, text);
1031 rule = bus_match_rule_parse (connection, &str, error);
1035 matchmaker = bus_connection_get_matchmaker (connection);
1037 if (!bus_matchmaker_add_rule (matchmaker, rule))
1039 BUS_SET_OOM (error);
1043 if (!send_ack_reply (connection, transaction,
1046 bus_matchmaker_remove_rule (matchmaker, rule);
1050 bus_match_rule_unref (rule);
1055 _DBUS_ASSERT_ERROR_IS_SET (error);
1057 bus_match_rule_unref (rule);
1062 bus_driver_handle_remove_match (DBusConnection *connection,
1063 BusTransaction *transaction,
1064 DBusMessage *message,
1070 BusMatchmaker *matchmaker;
1072 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1077 if (!dbus_message_get_args (message, error,
1078 DBUS_TYPE_STRING, &text,
1081 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1085 _dbus_string_init_const (&str, text);
1087 rule = bus_match_rule_parse (connection, &str, error);
1091 /* Send the ack before we remove the rule, since the ack is undone
1092 * on transaction cancel, but rule removal isn't.
1094 if (!send_ack_reply (connection, transaction,
1098 matchmaker = bus_connection_get_matchmaker (connection);
1100 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1103 bus_match_rule_unref (rule);
1108 _DBUS_ASSERT_ERROR_IS_SET (error);
1110 bus_match_rule_unref (rule);
1115 bus_driver_handle_get_service_owner (DBusConnection *connection,
1116 BusTransaction *transaction,
1117 DBusMessage *message,
1121 const char *base_name;
1123 BusRegistry *registry;
1124 BusService *service;
1127 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1129 registry = bus_connection_get_registry (connection);
1134 if (! dbus_message_get_args (message, error,
1135 DBUS_TYPE_STRING, &text,
1139 _dbus_string_init_const (&str, text);
1140 service = bus_registry_lookup (registry, &str);
1141 if (service == NULL &&
1142 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1144 /* ORG_FREEDESKTOP_DBUS owns itself */
1145 base_name = DBUS_SERVICE_DBUS;
1147 else if (service == NULL)
1149 dbus_set_error (error,
1150 DBUS_ERROR_NAME_HAS_NO_OWNER,
1151 "Could not get owner of name '%s': no such name", text);
1156 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1157 if (base_name == NULL)
1159 /* FIXME - how is this error possible? */
1160 dbus_set_error (error,
1162 "Could not determine unique name for '%s'", text);
1165 _dbus_assert (*base_name == ':');
1168 _dbus_assert (base_name != NULL);
1170 reply = dbus_message_new_method_return (message);
1174 if (! dbus_message_append_args (reply,
1175 DBUS_TYPE_STRING, &base_name,
1179 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1182 dbus_message_unref (reply);
1187 BUS_SET_OOM (error);
1190 _DBUS_ASSERT_ERROR_IS_SET (error);
1192 dbus_message_unref (reply);
1197 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1198 BusTransaction *transaction,
1199 DBusMessage *message,
1203 DBusList *base_names;
1206 BusRegistry *registry;
1207 BusService *service;
1209 DBusMessageIter iter, array_iter;
1210 char *dbus_service_name = DBUS_SERVICE_DBUS;
1212 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1214 registry = bus_connection_get_registry (connection);
1220 if (! dbus_message_get_args (message, error,
1221 DBUS_TYPE_STRING, &text,
1225 _dbus_string_init_const (&str, text);
1226 service = bus_registry_lookup (registry, &str);
1227 if (service == NULL &&
1228 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1230 /* ORG_FREEDESKTOP_DBUS owns itself */
1231 if (! _dbus_list_append (&base_names, dbus_service_name))
1234 else if (service == NULL)
1236 dbus_set_error (error,
1237 DBUS_ERROR_NAME_HAS_NO_OWNER,
1238 "Could not get owners of name '%s': no such name", text);
1243 if (!bus_service_list_queued_owners (service,
1249 _dbus_assert (base_names != NULL);
1251 reply = dbus_message_new_method_return (message);
1255 dbus_message_iter_init_append (reply, &iter);
1256 if (!dbus_message_iter_open_container (&iter,
1258 DBUS_TYPE_STRING_AS_STRING,
1262 link = _dbus_list_get_first_link (&base_names);
1263 while (link != NULL)
1267 _dbus_assert (link->data != NULL);
1268 uname = (char *)link->data;
1270 if (!dbus_message_iter_append_basic (&array_iter,
1275 link = _dbus_list_get_next_link (&base_names, link);
1278 if (! dbus_message_iter_close_container (&iter, &array_iter))
1282 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1285 dbus_message_unref (reply);
1290 BUS_SET_OOM (error);
1293 _DBUS_ASSERT_ERROR_IS_SET (error);
1295 dbus_message_unref (reply);
1298 _dbus_list_clear (&base_names);
1304 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1305 BusTransaction *transaction,
1306 DBusMessage *message,
1309 DBusConnection *conn;
1312 dbus_uint32_t uid32;
1313 const char *service;
1315 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1319 conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1325 reply = dbus_message_new_method_return (message);
1329 if (!dbus_connection_get_unix_user (conn, &uid))
1331 dbus_set_error (error,
1333 "Could not determine UID for '%s'", service);
1338 if (! dbus_message_append_args (reply,
1339 DBUS_TYPE_UINT32, &uid32,
1343 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1346 dbus_message_unref (reply);
1351 BUS_SET_OOM (error);
1354 _DBUS_ASSERT_ERROR_IS_SET (error);
1356 dbus_message_unref (reply);
1361 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1362 BusTransaction *transaction,
1363 DBusMessage *message,
1366 DBusConnection *conn;
1369 dbus_uint32_t pid32;
1370 const char *service;
1372 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1376 conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
1382 reply = dbus_message_new_method_return (message);
1386 if (!dbus_connection_get_unix_process_id (conn, &pid))
1388 dbus_set_error (error,
1389 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1390 "Could not determine PID for '%s'", service);
1395 if (! dbus_message_append_args (reply,
1396 DBUS_TYPE_UINT32, &pid32,
1400 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1403 dbus_message_unref (reply);
1408 BUS_SET_OOM (error);
1411 _DBUS_ASSERT_ERROR_IS_SET (error);
1413 dbus_message_unref (reply);
1418 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1419 BusTransaction *transaction,
1420 DBusMessage *message,
1423 DBusConnection *conn;
1426 dbus_uint32_t data_size;
1427 const char *service;
1429 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1433 conn = bus_driver_get_conn_helper (connection, message,
1434 "audit session data", &service, error);
1439 reply = dbus_message_new_method_return (message);
1443 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1445 dbus_set_error (error,
1446 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1447 "Could not determine audit session data for '%s'", service);
1451 if (! dbus_message_append_args (reply,
1452 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
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_selinux_security_context (DBusConnection *connection,
1475 BusTransaction *transaction,
1476 DBusMessage *message,
1479 DBusConnection *conn;
1481 BusSELinuxID *context;
1482 const char *service;
1484 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1488 conn = bus_driver_get_conn_helper (connection, message, "security context",
1494 reply = dbus_message_new_method_return (message);
1498 context = bus_connection_get_selinux_id (conn);
1501 dbus_set_error (error,
1502 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1503 "Could not determine security context for '%s'", service);
1507 if (! bus_selinux_append_context (reply, context, error))
1510 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1513 dbus_message_unref (reply);
1518 BUS_SET_OOM (error);
1521 _DBUS_ASSERT_ERROR_IS_SET (error);
1523 dbus_message_unref (reply);
1528 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1529 BusTransaction *transaction,
1530 DBusMessage *message,
1533 DBusConnection *conn;
1535 DBusMessageIter reply_iter;
1536 DBusMessageIter array_iter;
1537 unsigned long ulong_val;
1538 const char *service;
1540 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1544 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1550 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1554 /* we can't represent > 32-bit pids; if your system needs them, please
1555 * add ProcessID64 to the spec or something */
1556 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1557 ulong_val <= _DBUS_UINT32_MAX)
1559 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1563 /* we can't represent > 32-bit uids; if your system needs them, please
1564 * add UnixUserID64 to the spec or something */
1565 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1566 ulong_val <= _DBUS_UINT32_MAX)
1568 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1572 if (!_dbus_asv_close (&reply_iter, &array_iter))
1575 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1577 /* this time we don't want to close the iterator again, so just
1578 * get rid of the message */
1579 dbus_message_unref (reply);
1587 BUS_SET_OOM (error);
1590 _DBUS_ASSERT_ERROR_IS_SET (error);
1594 _dbus_asv_abandon (&reply_iter, &array_iter);
1595 dbus_message_unref (reply);
1602 bus_driver_handle_reload_config (DBusConnection *connection,
1603 BusTransaction *transaction,
1604 DBusMessage *message,
1607 BusContext *context;
1610 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1614 context = bus_connection_get_context (connection);
1615 if (!bus_context_reload_config (context, error))
1618 reply = dbus_message_new_method_return (message);
1622 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1625 dbus_message_unref (reply);
1629 BUS_SET_OOM (error);
1632 _DBUS_ASSERT_ERROR_IS_SET (error);
1634 dbus_message_unref (reply);
1639 bus_driver_handle_get_id (DBusConnection *connection,
1640 BusTransaction *transaction,
1641 DBusMessage *message,
1644 BusContext *context;
1647 const char *v_STRING;
1649 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1651 if (!_dbus_string_init (&uuid))
1653 BUS_SET_OOM (error);
1659 context = bus_connection_get_context (connection);
1660 if (!bus_context_get_id (context, &uuid))
1663 reply = dbus_message_new_method_return (message);
1667 v_STRING = _dbus_string_get_const_data (&uuid);
1668 if (!dbus_message_append_args (reply,
1669 DBUS_TYPE_STRING, &v_STRING,
1673 _dbus_assert (dbus_message_has_signature (reply, "s"));
1675 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1678 _dbus_string_free (&uuid);
1679 dbus_message_unref (reply);
1683 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1685 BUS_SET_OOM (error);
1688 dbus_message_unref (reply);
1689 _dbus_string_free (&uuid);
1696 const char *in_args;
1697 const char *out_args;
1698 dbus_bool_t (* handler) (DBusConnection *connection,
1699 BusTransaction *transaction,
1700 DBusMessage *message,
1704 /* For speed it might be useful to sort this in order of
1705 * frequency of use (but doesn't matter with only a few items
1708 static const MessageHandler dbus_message_handlers[] = {
1711 DBUS_TYPE_STRING_AS_STRING,
1712 bus_driver_handle_hello },
1714 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1715 DBUS_TYPE_UINT32_AS_STRING,
1716 bus_driver_handle_acquire_service },
1718 DBUS_TYPE_STRING_AS_STRING,
1719 DBUS_TYPE_UINT32_AS_STRING,
1720 bus_driver_handle_release_service },
1721 { "StartServiceByName",
1722 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1723 DBUS_TYPE_UINT32_AS_STRING,
1724 bus_driver_handle_activate_service },
1725 { "UpdateActivationEnvironment",
1726 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,
1728 bus_driver_handle_update_activation_environment },
1730 DBUS_TYPE_STRING_AS_STRING,
1731 DBUS_TYPE_BOOLEAN_AS_STRING,
1732 bus_driver_handle_service_exists },
1735 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1736 bus_driver_handle_list_services },
1737 { "ListActivatableNames",
1739 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1740 bus_driver_handle_list_activatable_services },
1742 DBUS_TYPE_STRING_AS_STRING,
1744 bus_driver_handle_add_match },
1746 DBUS_TYPE_STRING_AS_STRING,
1748 bus_driver_handle_remove_match },
1750 DBUS_TYPE_STRING_AS_STRING,
1751 DBUS_TYPE_STRING_AS_STRING,
1752 bus_driver_handle_get_service_owner },
1753 { "ListQueuedOwners",
1754 DBUS_TYPE_STRING_AS_STRING,
1755 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1756 bus_driver_handle_list_queued_owners },
1757 { "GetConnectionUnixUser",
1758 DBUS_TYPE_STRING_AS_STRING,
1759 DBUS_TYPE_UINT32_AS_STRING,
1760 bus_driver_handle_get_connection_unix_user },
1761 { "GetConnectionUnixProcessID",
1762 DBUS_TYPE_STRING_AS_STRING,
1763 DBUS_TYPE_UINT32_AS_STRING,
1764 bus_driver_handle_get_connection_unix_process_id },
1765 { "GetAdtAuditSessionData",
1766 DBUS_TYPE_STRING_AS_STRING,
1767 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1768 bus_driver_handle_get_adt_audit_session_data },
1769 { "GetConnectionSELinuxSecurityContext",
1770 DBUS_TYPE_STRING_AS_STRING,
1771 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1772 bus_driver_handle_get_connection_selinux_security_context },
1776 bus_driver_handle_reload_config },
1779 DBUS_TYPE_STRING_AS_STRING,
1780 bus_driver_handle_get_id },
1781 { "GetConnectionCredentials", "s", "a{sv}",
1782 bus_driver_handle_get_connection_credentials },
1783 { NULL, NULL, NULL, NULL }
1786 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1787 BusTransaction *, DBusMessage *, DBusError *);
1789 static const MessageHandler introspectable_message_handlers[] = {
1790 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1791 { NULL, NULL, NULL, NULL }
1794 #ifdef DBUS_ENABLE_STATS
1795 static const MessageHandler stats_message_handlers[] = {
1796 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1797 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1798 { NULL, NULL, NULL, NULL }
1804 const MessageHandler *message_handlers;
1805 const char *extra_introspection;
1808 /* These should ideally be sorted by frequency of use, although it
1809 * probably doesn't matter with this few items */
1810 static InterfaceHandler interface_handlers[] = {
1811 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1812 " <signal name=\"NameOwnerChanged\">\n"
1813 " <arg type=\"s\"/>\n"
1814 " <arg type=\"s\"/>\n"
1815 " <arg type=\"s\"/>\n"
1817 " <signal name=\"NameLost\">\n"
1818 " <arg type=\"s\"/>\n"
1820 " <signal name=\"NameAcquired\">\n"
1821 " <arg type=\"s\"/>\n"
1823 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1824 #ifdef DBUS_ENABLE_STATS
1825 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1827 { NULL, NULL, NULL }
1831 write_args_for_direction (DBusString *xml,
1832 const char *signature,
1835 DBusTypeReader typereader;
1839 _dbus_string_init_const (&sigstr, signature);
1840 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1842 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1844 const DBusString *subsig;
1847 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1848 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1851 if (!_dbus_string_append_len (xml,
1852 _dbus_string_get_const_data (subsig) + start,
1855 if (!_dbus_string_append (xml, "\"/>\n"))
1858 _dbus_type_reader_next (&typereader);
1866 bus_driver_generate_introspect_string (DBusString *xml)
1868 const InterfaceHandler *ih;
1869 const MessageHandler *mh;
1871 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1873 if (!_dbus_string_append (xml, "<node>\n"))
1876 for (ih = interface_handlers; ih->name != NULL; ih++)
1878 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1882 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1884 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1888 if (!write_args_for_direction (xml, mh->in_args, TRUE))
1891 if (!write_args_for_direction (xml, mh->out_args, FALSE))
1894 if (!_dbus_string_append (xml, " </method>\n"))
1898 if (ih->extra_introspection != NULL &&
1899 !_dbus_string_append (xml, ih->extra_introspection))
1902 if (!_dbus_string_append (xml, " </interface>\n"))
1906 if (!_dbus_string_append (xml, "</node>\n"))
1913 bus_driver_handle_introspect (DBusConnection *connection,
1914 BusTransaction *transaction,
1915 DBusMessage *message,
1920 const char *v_STRING;
1922 _dbus_verbose ("Introspect() on bus driver\n");
1924 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1928 if (! dbus_message_get_args (message, error,
1931 _DBUS_ASSERT_ERROR_IS_SET (error);
1935 if (!_dbus_string_init (&xml))
1937 BUS_SET_OOM (error);
1941 if (!bus_driver_generate_introspect_string (&xml))
1944 v_STRING = _dbus_string_get_const_data (&xml);
1946 reply = dbus_message_new_method_return (message);
1950 if (! dbus_message_append_args (reply,
1951 DBUS_TYPE_STRING, &v_STRING,
1955 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1958 dbus_message_unref (reply);
1959 _dbus_string_free (&xml);
1964 BUS_SET_OOM (error);
1967 dbus_message_unref (reply);
1969 _dbus_string_free (&xml);
1975 bus_driver_handle_message (DBusConnection *connection,
1976 BusTransaction *transaction,
1977 DBusMessage *message,
1980 const char *name, *interface;
1981 const InterfaceHandler *ih;
1982 const MessageHandler *mh;
1983 dbus_bool_t found_interface = FALSE;
1985 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1987 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
1989 BusContext *context;
1991 context = bus_connection_get_context (connection);
1992 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
1995 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1997 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1998 return TRUE; /* we just ignore this */
2001 /* may be NULL, which means "any interface will do" */
2002 interface = dbus_message_get_interface (message);
2004 _dbus_assert (dbus_message_get_member (message) != NULL);
2006 name = dbus_message_get_member (message);
2008 _dbus_verbose ("Driver got a method call: %s\n", name);
2010 /* security checks should have kept this from getting here */
2011 #ifndef DBUS_DISABLE_ASSERT
2013 const char *sender = dbus_message_get_sender (message);
2015 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
2019 for (ih = interface_handlers; ih->name != NULL; ih++)
2021 if (interface != NULL && strcmp (interface, ih->name) != 0)
2024 found_interface = TRUE;
2026 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2028 if (strcmp (mh->name, name) != 0)
2031 _dbus_verbose ("Found driver handler for %s\n", name);
2033 if (!dbus_message_has_signature (message, mh->in_args))
2035 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2036 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2037 name, dbus_message_get_signature (message),
2040 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2041 "Call to %s has wrong args (%s, expected %s)\n",
2042 name, dbus_message_get_signature (message),
2044 _DBUS_ASSERT_ERROR_IS_SET (error);
2048 if ((* mh->handler) (connection, transaction, message, error))
2050 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2051 _dbus_verbose ("Driver handler succeeded\n");
2056 _DBUS_ASSERT_ERROR_IS_SET (error);
2057 _dbus_verbose ("Driver handler returned failure\n");
2063 _dbus_verbose ("No driver handler for message \"%s\"\n",
2066 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2067 "%s does not understand message %s",
2068 DBUS_SERVICE_DBUS, name);
2074 bus_driver_remove_connection (DBusConnection *connection)
2076 /* FIXME 1.0 Does nothing for now, should unregister the connection
2077 * with the bus driver.