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;
1539 const char *service;
1541 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1545 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1551 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1555 /* we can't represent > 32-bit pids; if your system needs them, please
1556 * add ProcessID64 to the spec or something */
1557 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1558 ulong_val <= _DBUS_UINT32_MAX)
1560 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1564 /* we can't represent > 32-bit uids; if your system needs them, please
1565 * add UnixUserID64 to the spec or something */
1566 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1567 ulong_val <= _DBUS_UINT32_MAX)
1569 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1573 if (dbus_connection_get_windows_user (conn, &windows_sid))
1575 if (!_dbus_asv_add_string (&array_iter, "WindowsSID", windows_sid))
1577 dbus_free(windows_sid);
1580 dbus_free(windows_sid);
1583 if (!_dbus_asv_close (&reply_iter, &array_iter))
1586 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1588 /* this time we don't want to close the iterator again, so just
1589 * get rid of the message */
1590 dbus_message_unref (reply);
1598 BUS_SET_OOM (error);
1601 _DBUS_ASSERT_ERROR_IS_SET (error);
1605 _dbus_asv_abandon (&reply_iter, &array_iter);
1606 dbus_message_unref (reply);
1613 bus_driver_handle_reload_config (DBusConnection *connection,
1614 BusTransaction *transaction,
1615 DBusMessage *message,
1618 BusContext *context;
1621 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1625 context = bus_connection_get_context (connection);
1626 if (!bus_context_reload_config (context, error))
1629 reply = dbus_message_new_method_return (message);
1633 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1636 dbus_message_unref (reply);
1640 BUS_SET_OOM (error);
1643 _DBUS_ASSERT_ERROR_IS_SET (error);
1645 dbus_message_unref (reply);
1650 bus_driver_handle_get_id (DBusConnection *connection,
1651 BusTransaction *transaction,
1652 DBusMessage *message,
1655 BusContext *context;
1658 const char *v_STRING;
1660 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1662 if (!_dbus_string_init (&uuid))
1664 BUS_SET_OOM (error);
1670 context = bus_connection_get_context (connection);
1671 if (!bus_context_get_id (context, &uuid))
1674 reply = dbus_message_new_method_return (message);
1678 v_STRING = _dbus_string_get_const_data (&uuid);
1679 if (!dbus_message_append_args (reply,
1680 DBUS_TYPE_STRING, &v_STRING,
1684 _dbus_assert (dbus_message_has_signature (reply, "s"));
1686 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1689 _dbus_string_free (&uuid);
1690 dbus_message_unref (reply);
1694 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1696 BUS_SET_OOM (error);
1699 dbus_message_unref (reply);
1700 _dbus_string_free (&uuid);
1707 const char *in_args;
1708 const char *out_args;
1709 dbus_bool_t (* handler) (DBusConnection *connection,
1710 BusTransaction *transaction,
1711 DBusMessage *message,
1715 /* For speed it might be useful to sort this in order of
1716 * frequency of use (but doesn't matter with only a few items
1719 static const MessageHandler dbus_message_handlers[] = {
1722 DBUS_TYPE_STRING_AS_STRING,
1723 bus_driver_handle_hello },
1725 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1726 DBUS_TYPE_UINT32_AS_STRING,
1727 bus_driver_handle_acquire_service },
1729 DBUS_TYPE_STRING_AS_STRING,
1730 DBUS_TYPE_UINT32_AS_STRING,
1731 bus_driver_handle_release_service },
1732 { "StartServiceByName",
1733 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1734 DBUS_TYPE_UINT32_AS_STRING,
1735 bus_driver_handle_activate_service },
1736 { "UpdateActivationEnvironment",
1737 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,
1739 bus_driver_handle_update_activation_environment },
1741 DBUS_TYPE_STRING_AS_STRING,
1742 DBUS_TYPE_BOOLEAN_AS_STRING,
1743 bus_driver_handle_service_exists },
1746 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1747 bus_driver_handle_list_services },
1748 { "ListActivatableNames",
1750 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1751 bus_driver_handle_list_activatable_services },
1753 DBUS_TYPE_STRING_AS_STRING,
1755 bus_driver_handle_add_match },
1757 DBUS_TYPE_STRING_AS_STRING,
1759 bus_driver_handle_remove_match },
1761 DBUS_TYPE_STRING_AS_STRING,
1762 DBUS_TYPE_STRING_AS_STRING,
1763 bus_driver_handle_get_service_owner },
1764 { "ListQueuedOwners",
1765 DBUS_TYPE_STRING_AS_STRING,
1766 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1767 bus_driver_handle_list_queued_owners },
1768 { "GetConnectionUnixUser",
1769 DBUS_TYPE_STRING_AS_STRING,
1770 DBUS_TYPE_UINT32_AS_STRING,
1771 bus_driver_handle_get_connection_unix_user },
1772 { "GetConnectionUnixProcessID",
1773 DBUS_TYPE_STRING_AS_STRING,
1774 DBUS_TYPE_UINT32_AS_STRING,
1775 bus_driver_handle_get_connection_unix_process_id },
1776 { "GetAdtAuditSessionData",
1777 DBUS_TYPE_STRING_AS_STRING,
1778 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1779 bus_driver_handle_get_adt_audit_session_data },
1780 { "GetConnectionSELinuxSecurityContext",
1781 DBUS_TYPE_STRING_AS_STRING,
1782 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1783 bus_driver_handle_get_connection_selinux_security_context },
1787 bus_driver_handle_reload_config },
1790 DBUS_TYPE_STRING_AS_STRING,
1791 bus_driver_handle_get_id },
1792 { "GetConnectionCredentials", "s", "a{sv}",
1793 bus_driver_handle_get_connection_credentials },
1794 { NULL, NULL, NULL, NULL }
1797 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1798 BusTransaction *, DBusMessage *, DBusError *);
1800 static const MessageHandler introspectable_message_handlers[] = {
1801 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1802 { NULL, NULL, NULL, NULL }
1805 #ifdef DBUS_ENABLE_STATS
1806 static const MessageHandler stats_message_handlers[] = {
1807 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1808 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1809 { NULL, NULL, NULL, NULL }
1815 const MessageHandler *message_handlers;
1816 const char *extra_introspection;
1819 /* These should ideally be sorted by frequency of use, although it
1820 * probably doesn't matter with this few items */
1821 static InterfaceHandler interface_handlers[] = {
1822 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1823 " <signal name=\"NameOwnerChanged\">\n"
1824 " <arg type=\"s\"/>\n"
1825 " <arg type=\"s\"/>\n"
1826 " <arg type=\"s\"/>\n"
1828 " <signal name=\"NameLost\">\n"
1829 " <arg type=\"s\"/>\n"
1831 " <signal name=\"NameAcquired\">\n"
1832 " <arg type=\"s\"/>\n"
1834 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1835 #ifdef DBUS_ENABLE_STATS
1836 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1838 { NULL, NULL, NULL }
1842 write_args_for_direction (DBusString *xml,
1843 const char *signature,
1846 DBusTypeReader typereader;
1850 _dbus_string_init_const (&sigstr, signature);
1851 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1853 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1855 const DBusString *subsig;
1858 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1859 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1862 if (!_dbus_string_append_len (xml,
1863 _dbus_string_get_const_data (subsig) + start,
1866 if (!_dbus_string_append (xml, "\"/>\n"))
1869 _dbus_type_reader_next (&typereader);
1877 bus_driver_generate_introspect_string (DBusString *xml)
1879 const InterfaceHandler *ih;
1880 const MessageHandler *mh;
1882 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1884 if (!_dbus_string_append (xml, "<node>\n"))
1887 for (ih = interface_handlers; ih->name != NULL; ih++)
1889 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1893 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1895 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1899 if (!write_args_for_direction (xml, mh->in_args, TRUE))
1902 if (!write_args_for_direction (xml, mh->out_args, FALSE))
1905 if (!_dbus_string_append (xml, " </method>\n"))
1909 if (ih->extra_introspection != NULL &&
1910 !_dbus_string_append (xml, ih->extra_introspection))
1913 if (!_dbus_string_append (xml, " </interface>\n"))
1917 if (!_dbus_string_append (xml, "</node>\n"))
1924 bus_driver_handle_introspect (DBusConnection *connection,
1925 BusTransaction *transaction,
1926 DBusMessage *message,
1931 const char *v_STRING;
1933 _dbus_verbose ("Introspect() on bus driver\n");
1935 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1939 if (! dbus_message_get_args (message, error,
1942 _DBUS_ASSERT_ERROR_IS_SET (error);
1946 if (!_dbus_string_init (&xml))
1948 BUS_SET_OOM (error);
1952 if (!bus_driver_generate_introspect_string (&xml))
1955 v_STRING = _dbus_string_get_const_data (&xml);
1957 reply = dbus_message_new_method_return (message);
1961 if (! dbus_message_append_args (reply,
1962 DBUS_TYPE_STRING, &v_STRING,
1966 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1969 dbus_message_unref (reply);
1970 _dbus_string_free (&xml);
1975 BUS_SET_OOM (error);
1978 dbus_message_unref (reply);
1980 _dbus_string_free (&xml);
1986 bus_driver_handle_message (DBusConnection *connection,
1987 BusTransaction *transaction,
1988 DBusMessage *message,
1991 const char *name, *interface;
1992 const InterfaceHandler *ih;
1993 const MessageHandler *mh;
1994 dbus_bool_t found_interface = FALSE;
1996 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1998 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
2000 BusContext *context;
2002 context = bus_connection_get_context (connection);
2003 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
2006 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
2008 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
2009 return TRUE; /* we just ignore this */
2012 /* may be NULL, which means "any interface will do" */
2013 interface = dbus_message_get_interface (message);
2015 _dbus_assert (dbus_message_get_member (message) != NULL);
2017 name = dbus_message_get_member (message);
2019 _dbus_verbose ("Driver got a method call: %s\n", name);
2021 /* security checks should have kept this from getting here */
2022 #ifndef DBUS_DISABLE_ASSERT
2024 const char *sender = dbus_message_get_sender (message);
2026 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
2030 for (ih = interface_handlers; ih->name != NULL; ih++)
2032 if (interface != NULL && strcmp (interface, ih->name) != 0)
2035 found_interface = TRUE;
2037 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2039 if (strcmp (mh->name, name) != 0)
2042 _dbus_verbose ("Found driver handler for %s\n", name);
2044 if (!dbus_message_has_signature (message, mh->in_args))
2046 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2047 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2048 name, dbus_message_get_signature (message),
2051 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2052 "Call to %s has wrong args (%s, expected %s)\n",
2053 name, dbus_message_get_signature (message),
2055 _DBUS_ASSERT_ERROR_IS_SET (error);
2059 if ((* mh->handler) (connection, transaction, message, error))
2061 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2062 _dbus_verbose ("Driver handler succeeded\n");
2067 _DBUS_ASSERT_ERROR_IS_SET (error);
2068 _dbus_verbose ("Driver handler returned failure\n");
2074 _dbus_verbose ("No driver handler for message \"%s\"\n",
2077 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2078 "%s does not understand message %s",
2079 DBUS_SERVICE_DBUS, name);
2085 bus_driver_remove_connection (DBusConnection *connection)
2087 /* FIXME 1.0 Does nothing for now, should unregister the connection
2088 * with the bus driver.