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"
35 #include <dbus/dbus-string.h>
36 #include <dbus/dbus-internals.h>
37 #include <dbus/dbus-message.h>
38 #include <dbus/dbus-marshal-recursive.h>
41 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
42 DBusMessage *hello_message,
43 BusTransaction *transaction,
47 bus_driver_send_service_owner_changed (const char *service_name,
48 const char *old_owner,
49 const char *new_owner,
50 BusTransaction *transaction,
55 const char *null_service;
57 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
60 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
62 old_owner ? old_owner : null_service,
63 new_owner ? new_owner : null_service);
65 message = dbus_message_new_signal (DBUS_PATH_DBUS,
75 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
78 if (!dbus_message_append_args (message,
79 DBUS_TYPE_STRING, &service_name,
80 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
81 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
85 _dbus_assert (dbus_message_has_signature (message, "sss"));
87 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
88 dbus_message_unref (message);
93 dbus_message_unref (message);
99 bus_driver_send_service_lost (DBusConnection *connection,
100 const char *service_name,
101 BusTransaction *transaction,
104 DBusMessage *message;
106 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
108 message = dbus_message_new_signal (DBUS_PATH_DBUS,
118 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
119 !dbus_message_append_args (message,
120 DBUS_TYPE_STRING, &service_name,
123 dbus_message_unref (message);
128 if (!bus_transaction_send_from_driver (transaction, connection, message))
130 dbus_message_unref (message);
136 dbus_message_unref (message);
142 bus_driver_send_service_acquired (DBusConnection *connection,
143 const char *service_name,
144 BusTransaction *transaction,
147 DBusMessage *message;
149 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
151 message = dbus_message_new_signal (DBUS_PATH_DBUS,
161 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
162 !dbus_message_append_args (message,
163 DBUS_TYPE_STRING, &service_name,
166 dbus_message_unref (message);
171 if (!bus_transaction_send_from_driver (transaction, connection, message))
173 dbus_message_unref (message);
179 dbus_message_unref (message);
185 create_unique_client_name (BusRegistry *registry,
188 /* We never want to use the same unique client name twice, because
189 * we want to guarantee that if you send a message to a given unique
190 * name, you always get the same application. So we use two numbers
191 * for INT_MAX * INT_MAX combinations, should be pretty safe against
194 /* FIXME these should be in BusRegistry rather than static vars */
195 static int next_major_number = 0;
196 static int next_minor_number = 0;
199 len = _dbus_string_get_length (str);
203 /* start out with 1-0, go to 1-1, 1-2, 1-3,
204 * up to 1-MAXINT, then 2-0, 2-1, etc.
206 if (next_minor_number <= 0)
208 next_major_number += 1;
209 next_minor_number = 0;
210 if (next_major_number <= 0)
211 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
214 _dbus_assert (next_major_number > 0);
215 _dbus_assert (next_minor_number >= 0);
217 /* appname:MAJOR-MINOR */
219 if (!_dbus_string_append (str, ":"))
222 if (!_dbus_string_append_int (str, next_major_number))
225 if (!_dbus_string_append (str, "."))
228 if (!_dbus_string_append_int (str, next_minor_number))
231 next_minor_number += 1;
233 /* Check if a client with the name exists */
234 if (bus_registry_lookup (registry, str) == NULL)
237 /* drop the number again, try the next one. */
238 _dbus_string_set_length (str, len);
245 bus_driver_handle_hello (DBusConnection *connection,
246 BusTransaction *transaction,
247 DBusMessage *message,
250 DBusString unique_name;
253 BusRegistry *registry;
254 BusConnections *connections;
256 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
258 if (bus_connection_is_active (connection))
260 /* We already handled an Hello message for this connection. */
261 dbus_set_error (error, DBUS_ERROR_FAILED,
262 "Already handled an Hello message");
266 /* Note that when these limits are exceeded we don't disconnect the
267 * connection; we just sort of leave it hanging there until it times
268 * out or disconnects itself or is dropped due to the max number of
269 * incomplete connections. It's even OK if the connection wants to
270 * retry the hello message, we support that.
272 connections = bus_connection_get_connections (connection);
273 if (!bus_connections_check_limits (connections, connection,
276 _DBUS_ASSERT_ERROR_IS_SET (error);
280 if (!_dbus_string_init (&unique_name))
288 registry = bus_connection_get_registry (connection);
290 if (!create_unique_client_name (registry, &unique_name))
296 if (!bus_connection_complete (connection, &unique_name, error))
298 _DBUS_ASSERT_ERROR_IS_SET (error);
302 if (!dbus_message_set_sender (message,
303 bus_connection_get_name (connection)))
309 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
312 /* Create the service */
313 service = bus_registry_ensure (registry,
314 &unique_name, connection, 0, transaction, error);
318 _dbus_assert (bus_connection_is_active (connection));
322 _dbus_string_free (&unique_name);
327 bus_driver_send_welcome_message (DBusConnection *connection,
328 DBusMessage *hello_message,
329 BusTransaction *transaction,
332 DBusMessage *welcome;
335 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
337 name = bus_connection_get_name (connection);
338 _dbus_assert (name != NULL);
340 welcome = dbus_message_new_method_return (hello_message);
347 if (!dbus_message_append_args (welcome,
348 DBUS_TYPE_STRING, &name,
351 dbus_message_unref (welcome);
356 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
358 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
360 dbus_message_unref (welcome);
366 dbus_message_unref (welcome);
372 bus_driver_handle_list_services (DBusConnection *connection,
373 BusTransaction *transaction,
374 DBusMessage *message,
380 BusRegistry *registry;
382 DBusMessageIter iter;
385 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
387 registry = bus_connection_get_registry (connection);
389 reply = dbus_message_new_method_return (message);
396 if (!bus_registry_list_services (registry, &services, &len))
398 dbus_message_unref (reply);
403 dbus_message_iter_init_append (reply, &iter);
405 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
406 DBUS_TYPE_STRING_AS_STRING,
409 dbus_free_string_array (services);
410 dbus_message_unref (reply);
416 /* Include the bus driver in the list */
417 const char *v_STRING = DBUS_SERVICE_DBUS;
418 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
421 dbus_free_string_array (services);
422 dbus_message_unref (reply);
431 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
434 dbus_free_string_array (services);
435 dbus_message_unref (reply);
442 dbus_free_string_array (services);
444 if (!dbus_message_iter_close_container (&iter, &sub))
446 dbus_message_unref (reply);
451 if (!bus_transaction_send_from_driver (transaction, connection, reply))
453 dbus_message_unref (reply);
459 dbus_message_unref (reply);
465 bus_driver_handle_list_activatable_services (DBusConnection *connection,
466 BusTransaction *transaction,
467 DBusMessage *message,
473 BusActivation *activation;
475 DBusMessageIter iter;
478 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
480 activation = bus_connection_get_activation (connection);
482 reply = dbus_message_new_method_return (message);
489 if (!bus_activation_list_services (activation, &services, &len))
491 dbus_message_unref (reply);
496 dbus_message_iter_init_append (reply, &iter);
498 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
499 DBUS_TYPE_STRING_AS_STRING,
502 dbus_free_string_array (services);
503 dbus_message_unref (reply);
509 /* Include the bus driver in the list */
510 const char *v_STRING = DBUS_SERVICE_DBUS;
511 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
514 dbus_free_string_array (services);
515 dbus_message_unref (reply);
524 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
527 dbus_free_string_array (services);
528 dbus_message_unref (reply);
535 dbus_free_string_array (services);
537 if (!dbus_message_iter_close_container (&iter, &sub))
539 dbus_message_unref (reply);
544 if (!bus_transaction_send_from_driver (transaction, connection, reply))
546 dbus_message_unref (reply);
552 dbus_message_unref (reply);
558 bus_driver_handle_acquire_service (DBusConnection *connection,
559 BusTransaction *transaction,
560 DBusMessage *message,
564 DBusString service_name;
566 dbus_uint32_t service_reply;
569 BusRegistry *registry;
571 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
573 registry = bus_connection_get_registry (connection);
575 if (!dbus_message_get_args (message, error,
576 DBUS_TYPE_STRING, &name,
577 DBUS_TYPE_UINT32, &flags,
581 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
586 _dbus_string_init_const (&service_name, name);
588 if (!bus_registry_acquire_service (registry, connection,
589 &service_name, flags,
590 &service_reply, transaction,
594 reply = dbus_message_new_method_return (message);
601 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
607 if (!bus_transaction_send_from_driver (transaction, connection, reply))
617 dbus_message_unref (reply);
622 bus_driver_handle_release_service (DBusConnection *connection,
623 BusTransaction *transaction,
624 DBusMessage *message,
628 DBusString service_name;
630 dbus_uint32_t service_reply;
632 BusRegistry *registry;
634 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
636 registry = bus_connection_get_registry (connection);
638 if (!dbus_message_get_args (message, error,
639 DBUS_TYPE_STRING, &name,
643 _dbus_verbose ("Trying to release name %s\n", name);
648 _dbus_string_init_const (&service_name, name);
650 if (!bus_registry_release_service (registry, connection,
651 &service_name, &service_reply,
655 reply = dbus_message_new_method_return (message);
662 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
668 if (!bus_transaction_send_from_driver (transaction, connection, reply))
678 dbus_message_unref (reply);
683 bus_driver_handle_service_exists (DBusConnection *connection,
684 BusTransaction *transaction,
685 DBusMessage *message,
689 DBusString service_name;
691 dbus_bool_t service_exists;
694 BusRegistry *registry;
696 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
698 registry = bus_connection_get_registry (connection);
700 if (!dbus_message_get_args (message, error,
701 DBUS_TYPE_STRING, &name,
707 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
709 service_exists = TRUE;
713 _dbus_string_init_const (&service_name, name);
714 service = bus_registry_lookup (registry, &service_name);
715 service_exists = service != NULL;
718 reply = dbus_message_new_method_return (message);
725 if (!dbus_message_append_args (reply,
726 DBUS_TYPE_BOOLEAN, &service_exists,
733 if (!bus_transaction_send_from_driver (transaction, connection, reply))
743 dbus_message_unref (reply);
749 bus_driver_handle_activate_service (DBusConnection *connection,
750 BusTransaction *transaction,
751 DBusMessage *message,
757 BusActivation *activation;
759 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
761 activation = bus_connection_get_activation (connection);
763 if (!dbus_message_get_args (message, error,
764 DBUS_TYPE_STRING, &name,
765 DBUS_TYPE_UINT32, &flags,
768 _DBUS_ASSERT_ERROR_IS_SET (error);
769 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
775 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
776 message, name, error))
778 _DBUS_ASSERT_ERROR_IS_SET (error);
779 _dbus_verbose ("bus_activation_activate_service() failed\n");
790 send_ack_reply (DBusConnection *connection,
791 BusTransaction *transaction,
792 DBusMessage *message,
797 if (dbus_message_get_no_reply (message))
800 reply = dbus_message_new_method_return (message);
807 if (!bus_transaction_send_from_driver (transaction, connection, reply))
810 dbus_message_unref (reply);
814 dbus_message_unref (reply);
820 bus_driver_handle_update_activation_environment (DBusConnection *connection,
821 BusTransaction *transaction,
822 DBusMessage *message,
826 BusActivation *activation;
827 DBusMessageIter iter;
828 DBusMessageIter dict_iter;
829 DBusMessageIter dict_entry_iter;
832 DBusList *keys, *key_link;
833 DBusList *values, *value_link;
835 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
837 activation = bus_connection_get_activation (connection);
839 dbus_message_iter_init (message, &iter);
841 /* The message signature has already been checked for us,
842 * so let's just assert it's right.
844 #ifndef DBUS_DISABLE_ASSERT
846 int msg_type = dbus_message_iter_get_arg_type (&iter);
848 _dbus_assert (msg_type == DBUS_TYPE_ARRAY);
852 dbus_message_iter_recurse (&iter, &dict_iter);
856 /* Then loop through the sent dictionary, add the location of
857 * the environment keys and values to lists. The result will
858 * be in reverse order, so we don't have to constantly search
859 * for the end of the list in a loop.
863 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
865 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
867 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
873 dbus_message_iter_get_basic (&dict_entry_iter, &key);
874 dbus_message_iter_next (&dict_entry_iter);
876 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
878 if (value_type != DBUS_TYPE_STRING)
881 dbus_message_iter_get_basic (&dict_entry_iter, &value);
883 if (!_dbus_list_append (&keys, key))
889 if (!_dbus_list_append (&values, value))
895 dbus_message_iter_next (&dict_entry_iter);
898 if (key_type != DBUS_TYPE_INVALID)
901 dbus_message_iter_next (&dict_iter);
904 if (array_type != DBUS_TYPE_INVALID)
907 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
911 while (key_link != NULL)
916 key = key_link->data;
917 value = value_link->data;
919 if (!bus_activation_set_environment_variable (activation,
922 _DBUS_ASSERT_ERROR_IS_SET (error);
923 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
926 key_link = _dbus_list_get_next_link (&keys, key_link);
927 value_link = _dbus_list_get_next_link (&values, value_link);
930 /* FIXME: We can fail early having set only some of the environment variables,
931 * (because of OOM failure). It's sort of hard to fix and it doesn't really
932 * matter, so we're punting for now.
934 if (key_link != NULL)
937 if (!send_ack_reply (connection, transaction,
944 _dbus_list_clear (&keys);
945 _dbus_list_clear (&values);
950 bus_driver_handle_add_match (DBusConnection *connection,
951 BusTransaction *transaction,
952 DBusMessage *message,
958 BusMatchmaker *matchmaker;
960 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
965 if (bus_connection_get_n_match_rules (connection) >=
966 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
968 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
969 "Connection \"%s\" is not allowed to add more match rules "
970 "(increase limits in configuration file if required)",
971 bus_connection_is_active (connection) ?
972 bus_connection_get_name (connection) :
977 if (!dbus_message_get_args (message, error,
978 DBUS_TYPE_STRING, &text,
981 _dbus_verbose ("No memory to get arguments to AddMatch\n");
985 _dbus_string_init_const (&str, text);
987 rule = bus_match_rule_parse (connection, &str, error);
991 matchmaker = bus_connection_get_matchmaker (connection);
993 if (!bus_matchmaker_add_rule (matchmaker, rule))
999 if (!send_ack_reply (connection, transaction,
1002 bus_matchmaker_remove_rule (matchmaker, rule);
1006 bus_match_rule_unref (rule);
1011 _DBUS_ASSERT_ERROR_IS_SET (error);
1013 bus_match_rule_unref (rule);
1018 bus_driver_handle_remove_match (DBusConnection *connection,
1019 BusTransaction *transaction,
1020 DBusMessage *message,
1026 BusMatchmaker *matchmaker;
1028 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1033 if (!dbus_message_get_args (message, error,
1034 DBUS_TYPE_STRING, &text,
1037 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1041 _dbus_string_init_const (&str, text);
1043 rule = bus_match_rule_parse (connection, &str, error);
1047 /* Send the ack before we remove the rule, since the ack is undone
1048 * on transaction cancel, but rule removal isn't.
1050 if (!send_ack_reply (connection, transaction,
1054 matchmaker = bus_connection_get_matchmaker (connection);
1056 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1059 bus_match_rule_unref (rule);
1064 _DBUS_ASSERT_ERROR_IS_SET (error);
1066 bus_match_rule_unref (rule);
1071 bus_driver_handle_get_service_owner (DBusConnection *connection,
1072 BusTransaction *transaction,
1073 DBusMessage *message,
1077 const char *base_name;
1079 BusRegistry *registry;
1080 BusService *service;
1083 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1085 registry = bus_connection_get_registry (connection);
1090 if (! dbus_message_get_args (message, error,
1091 DBUS_TYPE_STRING, &text,
1095 _dbus_string_init_const (&str, text);
1096 service = bus_registry_lookup (registry, &str);
1097 if (service == NULL &&
1098 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1100 /* ORG_FREEDESKTOP_DBUS owns itself */
1101 base_name = DBUS_SERVICE_DBUS;
1103 else if (service == NULL)
1105 dbus_set_error (error,
1106 DBUS_ERROR_NAME_HAS_NO_OWNER,
1107 "Could not get owner of name '%s': no such name", text);
1112 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1113 if (base_name == NULL)
1115 /* FIXME - how is this error possible? */
1116 dbus_set_error (error,
1118 "Could not determine unique name for '%s'", text);
1121 _dbus_assert (*base_name == ':');
1124 _dbus_assert (base_name != NULL);
1126 reply = dbus_message_new_method_return (message);
1130 if (! dbus_message_append_args (reply,
1131 DBUS_TYPE_STRING, &base_name,
1135 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1138 dbus_message_unref (reply);
1143 BUS_SET_OOM (error);
1146 _DBUS_ASSERT_ERROR_IS_SET (error);
1148 dbus_message_unref (reply);
1153 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1154 BusTransaction *transaction,
1155 DBusMessage *message,
1159 DBusList *base_names;
1162 BusRegistry *registry;
1163 BusService *service;
1165 DBusMessageIter iter, array_iter;
1166 char *dbus_service_name = DBUS_SERVICE_DBUS;
1168 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1170 registry = bus_connection_get_registry (connection);
1176 if (! dbus_message_get_args (message, error,
1177 DBUS_TYPE_STRING, &text,
1181 _dbus_string_init_const (&str, text);
1182 service = bus_registry_lookup (registry, &str);
1183 if (service == NULL &&
1184 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1186 /* ORG_FREEDESKTOP_DBUS owns itself */
1187 if (! _dbus_list_append (&base_names, dbus_service_name))
1190 else if (service == NULL)
1192 dbus_set_error (error,
1193 DBUS_ERROR_NAME_HAS_NO_OWNER,
1194 "Could not get owners of name '%s': no such name", text);
1199 if (!bus_service_list_queued_owners (service,
1205 _dbus_assert (base_names != NULL);
1207 reply = dbus_message_new_method_return (message);
1211 dbus_message_iter_init_append (reply, &iter);
1212 if (!dbus_message_iter_open_container (&iter,
1214 DBUS_TYPE_STRING_AS_STRING,
1218 link = _dbus_list_get_first_link (&base_names);
1219 while (link != NULL)
1223 _dbus_assert (link->data != NULL);
1224 uname = (char *)link->data;
1226 if (!dbus_message_iter_append_basic (&array_iter,
1231 link = _dbus_list_get_next_link (&base_names, link);
1234 if (! dbus_message_iter_close_container (&iter, &array_iter))
1238 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1241 dbus_message_unref (reply);
1246 BUS_SET_OOM (error);
1249 _DBUS_ASSERT_ERROR_IS_SET (error);
1251 dbus_message_unref (reply);
1254 _dbus_list_clear (&base_names);
1260 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1261 BusTransaction *transaction,
1262 DBusMessage *message,
1265 const char *service;
1267 BusRegistry *registry;
1269 DBusConnection *conn;
1272 dbus_uint32_t uid32;
1274 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1276 registry = bus_connection_get_registry (connection);
1281 if (! dbus_message_get_args (message, error,
1282 DBUS_TYPE_STRING, &service,
1286 _dbus_verbose ("asked for UID of connection %s\n", service);
1288 _dbus_string_init_const (&str, service);
1289 serv = bus_registry_lookup (registry, &str);
1292 dbus_set_error (error,
1293 DBUS_ERROR_NAME_HAS_NO_OWNER,
1294 "Could not get UID of name '%s': no such name", service);
1298 conn = bus_service_get_primary_owners_connection (serv);
1300 reply = dbus_message_new_method_return (message);
1304 if (!dbus_connection_get_unix_user (conn, &uid))
1306 dbus_set_error (error,
1308 "Could not determine UID for '%s'", service);
1313 if (! dbus_message_append_args (reply,
1314 DBUS_TYPE_UINT32, &uid32,
1318 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1321 dbus_message_unref (reply);
1326 BUS_SET_OOM (error);
1329 _DBUS_ASSERT_ERROR_IS_SET (error);
1331 dbus_message_unref (reply);
1336 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1337 BusTransaction *transaction,
1338 DBusMessage *message,
1341 const char *service;
1343 BusRegistry *registry;
1345 DBusConnection *conn;
1348 dbus_uint32_t pid32;
1350 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1352 registry = bus_connection_get_registry (connection);
1357 if (! dbus_message_get_args (message, error,
1358 DBUS_TYPE_STRING, &service,
1362 _dbus_verbose ("asked for PID of connection %s\n", service);
1364 _dbus_string_init_const (&str, service);
1365 serv = bus_registry_lookup (registry, &str);
1368 dbus_set_error (error,
1369 DBUS_ERROR_NAME_HAS_NO_OWNER,
1370 "Could not get PID of name '%s': no such name", service);
1374 conn = bus_service_get_primary_owners_connection (serv);
1376 reply = dbus_message_new_method_return (message);
1380 if (!dbus_connection_get_unix_process_id (conn, &pid))
1382 dbus_set_error (error,
1383 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1384 "Could not determine PID for '%s'", service);
1389 if (! dbus_message_append_args (reply,
1390 DBUS_TYPE_UINT32, &pid32,
1394 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1397 dbus_message_unref (reply);
1402 BUS_SET_OOM (error);
1405 _DBUS_ASSERT_ERROR_IS_SET (error);
1407 dbus_message_unref (reply);
1412 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1413 BusTransaction *transaction,
1414 DBusMessage *message,
1417 const char *service;
1419 BusRegistry *registry;
1421 DBusConnection *conn;
1424 dbus_uint32_t data_size;
1426 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1428 registry = bus_connection_get_registry (connection);
1433 if (! dbus_message_get_args (message, error,
1434 DBUS_TYPE_STRING, &service,
1438 _dbus_verbose ("asked for audit session data for connection %s\n", service);
1440 _dbus_string_init_const (&str, service);
1441 serv = bus_registry_lookup (registry, &str);
1444 dbus_set_error (error,
1445 DBUS_ERROR_NAME_HAS_NO_OWNER,
1446 "Could not get audit session data for name '%s': no such name", service);
1450 conn = bus_service_get_primary_owners_connection (serv);
1452 reply = dbus_message_new_method_return (message);
1456 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1458 dbus_set_error (error,
1459 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1460 "Could not determine audit session data for '%s'", service);
1464 if (! dbus_message_append_args (reply,
1465 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1469 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1472 dbus_message_unref (reply);
1477 BUS_SET_OOM (error);
1480 _DBUS_ASSERT_ERROR_IS_SET (error);
1482 dbus_message_unref (reply);
1487 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1488 BusTransaction *transaction,
1489 DBusMessage *message,
1492 const char *service;
1494 BusRegistry *registry;
1496 DBusConnection *conn;
1498 BusSELinuxID *context;
1500 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1502 registry = bus_connection_get_registry (connection);
1507 if (! dbus_message_get_args (message, error,
1508 DBUS_TYPE_STRING, &service,
1512 _dbus_verbose ("asked for security context of connection %s\n", service);
1514 _dbus_string_init_const (&str, service);
1515 serv = bus_registry_lookup (registry, &str);
1518 dbus_set_error (error,
1519 DBUS_ERROR_NAME_HAS_NO_OWNER,
1520 "Could not get security context of name '%s': no such name", service);
1524 conn = bus_service_get_primary_owners_connection (serv);
1526 reply = dbus_message_new_method_return (message);
1530 context = bus_connection_get_selinux_id (conn);
1533 dbus_set_error (error,
1534 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1535 "Could not determine security context for '%s'", service);
1539 if (! bus_selinux_append_context (reply, context, error))
1542 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1545 dbus_message_unref (reply);
1550 BUS_SET_OOM (error);
1553 _DBUS_ASSERT_ERROR_IS_SET (error);
1555 dbus_message_unref (reply);
1560 bus_driver_handle_reload_config (DBusConnection *connection,
1561 BusTransaction *transaction,
1562 DBusMessage *message,
1565 BusContext *context;
1568 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1572 context = bus_connection_get_context (connection);
1573 if (!bus_context_reload_config (context, error))
1576 reply = dbus_message_new_method_return (message);
1580 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1583 dbus_message_unref (reply);
1587 BUS_SET_OOM (error);
1590 _DBUS_ASSERT_ERROR_IS_SET (error);
1592 dbus_message_unref (reply);
1597 bus_driver_handle_get_id (DBusConnection *connection,
1598 BusTransaction *transaction,
1599 DBusMessage *message,
1602 BusContext *context;
1605 const char *v_STRING;
1607 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1609 if (!_dbus_string_init (&uuid))
1611 BUS_SET_OOM (error);
1617 context = bus_connection_get_context (connection);
1618 if (!bus_context_get_id (context, &uuid))
1621 reply = dbus_message_new_method_return (message);
1625 v_STRING = _dbus_string_get_const_data (&uuid);
1626 if (!dbus_message_append_args (reply,
1627 DBUS_TYPE_STRING, &v_STRING,
1631 _dbus_assert (dbus_message_has_signature (reply, "s"));
1633 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1636 _dbus_string_free (&uuid);
1637 dbus_message_unref (reply);
1641 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1643 BUS_SET_OOM (error);
1646 dbus_message_unref (reply);
1647 _dbus_string_free (&uuid);
1654 const char *in_args;
1655 const char *out_args;
1656 dbus_bool_t (* handler) (DBusConnection *connection,
1657 BusTransaction *transaction,
1658 DBusMessage *message,
1662 /* For speed it might be useful to sort this in order of
1663 * frequency of use (but doesn't matter with only a few items
1666 static const MessageHandler dbus_message_handlers[] = {
1669 DBUS_TYPE_STRING_AS_STRING,
1670 bus_driver_handle_hello },
1672 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1673 DBUS_TYPE_UINT32_AS_STRING,
1674 bus_driver_handle_acquire_service },
1676 DBUS_TYPE_STRING_AS_STRING,
1677 DBUS_TYPE_UINT32_AS_STRING,
1678 bus_driver_handle_release_service },
1679 { "StartServiceByName",
1680 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1681 DBUS_TYPE_UINT32_AS_STRING,
1682 bus_driver_handle_activate_service },
1683 { "UpdateActivationEnvironment",
1684 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,
1686 bus_driver_handle_update_activation_environment },
1688 DBUS_TYPE_STRING_AS_STRING,
1689 DBUS_TYPE_BOOLEAN_AS_STRING,
1690 bus_driver_handle_service_exists },
1693 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1694 bus_driver_handle_list_services },
1695 { "ListActivatableNames",
1697 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1698 bus_driver_handle_list_activatable_services },
1700 DBUS_TYPE_STRING_AS_STRING,
1702 bus_driver_handle_add_match },
1704 DBUS_TYPE_STRING_AS_STRING,
1706 bus_driver_handle_remove_match },
1708 DBUS_TYPE_STRING_AS_STRING,
1709 DBUS_TYPE_STRING_AS_STRING,
1710 bus_driver_handle_get_service_owner },
1711 { "ListQueuedOwners",
1712 DBUS_TYPE_STRING_AS_STRING,
1713 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1714 bus_driver_handle_list_queued_owners },
1715 { "GetConnectionUnixUser",
1716 DBUS_TYPE_STRING_AS_STRING,
1717 DBUS_TYPE_UINT32_AS_STRING,
1718 bus_driver_handle_get_connection_unix_user },
1719 { "GetConnectionUnixProcessID",
1720 DBUS_TYPE_STRING_AS_STRING,
1721 DBUS_TYPE_UINT32_AS_STRING,
1722 bus_driver_handle_get_connection_unix_process_id },
1723 { "GetAdtAuditSessionData",
1724 DBUS_TYPE_STRING_AS_STRING,
1725 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1726 bus_driver_handle_get_adt_audit_session_data },
1727 { "GetConnectionSELinuxSecurityContext",
1728 DBUS_TYPE_STRING_AS_STRING,
1729 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1730 bus_driver_handle_get_connection_selinux_security_context },
1734 bus_driver_handle_reload_config },
1737 DBUS_TYPE_STRING_AS_STRING,
1738 bus_driver_handle_get_id },
1739 { NULL, NULL, NULL, NULL }
1742 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1743 BusTransaction *, DBusMessage *, DBusError *);
1745 static const MessageHandler introspectable_message_handlers[] = {
1746 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1747 { NULL, NULL, NULL, NULL }
1750 #ifdef DBUS_ENABLE_STATS
1751 static const MessageHandler stats_message_handlers[] = {
1752 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1753 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1754 { NULL, NULL, NULL, NULL }
1760 const MessageHandler *message_handlers;
1761 const char *extra_introspection;
1764 /* These should ideally be sorted by frequency of use, although it
1765 * probably doesn't matter with this few items */
1766 static InterfaceHandler interface_handlers[] = {
1767 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1768 " <signal name=\"NameOwnerChanged\">\n"
1769 " <arg type=\"s\"/>\n"
1770 " <arg type=\"s\"/>\n"
1771 " <arg type=\"s\"/>\n"
1773 " <signal name=\"NameLost\">\n"
1774 " <arg type=\"s\"/>\n"
1776 " <signal name=\"NameAcquired\">\n"
1777 " <arg type=\"s\"/>\n"
1779 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1780 #ifdef DBUS_ENABLE_STATS
1781 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1783 { NULL, NULL, NULL }
1787 write_args_for_direction (DBusString *xml,
1788 const char *signature,
1791 DBusTypeReader typereader;
1795 _dbus_string_init_const (&sigstr, signature);
1796 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1798 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1800 const DBusString *subsig;
1803 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1804 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1807 if (!_dbus_string_append_len (xml,
1808 _dbus_string_get_const_data (subsig) + start,
1811 if (!_dbus_string_append (xml, "\"/>\n"))
1814 _dbus_type_reader_next (&typereader);
1822 bus_driver_generate_introspect_string (DBusString *xml)
1824 const InterfaceHandler *ih;
1825 const MessageHandler *mh;
1827 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1829 if (!_dbus_string_append (xml, "<node>\n"))
1832 for (ih = interface_handlers; ih->name != NULL; ih++)
1834 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1838 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1840 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1844 if (!write_args_for_direction (xml, mh->in_args, TRUE))
1847 if (!write_args_for_direction (xml, mh->out_args, FALSE))
1850 if (!_dbus_string_append (xml, " </method>\n"))
1854 if (ih->extra_introspection != NULL &&
1855 !_dbus_string_append (xml, ih->extra_introspection))
1858 if (!_dbus_string_append (xml, " </interface>\n"))
1862 if (!_dbus_string_append (xml, "</node>\n"))
1869 bus_driver_handle_introspect (DBusConnection *connection,
1870 BusTransaction *transaction,
1871 DBusMessage *message,
1876 const char *v_STRING;
1878 _dbus_verbose ("Introspect() on bus driver\n");
1880 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1884 if (! dbus_message_get_args (message, error,
1887 _DBUS_ASSERT_ERROR_IS_SET (error);
1891 if (!_dbus_string_init (&xml))
1893 BUS_SET_OOM (error);
1897 if (!bus_driver_generate_introspect_string (&xml))
1900 v_STRING = _dbus_string_get_const_data (&xml);
1902 reply = dbus_message_new_method_return (message);
1906 if (! dbus_message_append_args (reply,
1907 DBUS_TYPE_STRING, &v_STRING,
1911 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1914 dbus_message_unref (reply);
1915 _dbus_string_free (&xml);
1920 BUS_SET_OOM (error);
1923 dbus_message_unref (reply);
1925 _dbus_string_free (&xml);
1931 bus_driver_handle_message (DBusConnection *connection,
1932 BusTransaction *transaction,
1933 DBusMessage *message,
1936 const char *name, *interface;
1937 const InterfaceHandler *ih;
1938 const MessageHandler *mh;
1939 dbus_bool_t found_interface = FALSE;
1941 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1943 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
1945 BusContext *context;
1947 context = bus_connection_get_context (connection);
1948 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
1951 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1953 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1954 return TRUE; /* we just ignore this */
1957 /* may be NULL, which means "any interface will do" */
1958 interface = dbus_message_get_interface (message);
1960 _dbus_assert (dbus_message_get_member (message) != NULL);
1962 name = dbus_message_get_member (message);
1964 _dbus_verbose ("Driver got a method call: %s\n", name);
1966 /* security checks should have kept this from getting here */
1967 #ifndef DBUS_DISABLE_ASSERT
1969 const char *sender = dbus_message_get_sender (message);
1971 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1975 for (ih = interface_handlers; ih->name != NULL; ih++)
1977 if (interface != NULL && strcmp (interface, ih->name) != 0)
1980 found_interface = TRUE;
1982 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1984 if (strcmp (mh->name, name) != 0)
1987 _dbus_verbose ("Found driver handler for %s\n", name);
1989 if (!dbus_message_has_signature (message, mh->in_args))
1991 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1992 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1993 name, dbus_message_get_signature (message),
1996 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1997 "Call to %s has wrong args (%s, expected %s)\n",
1998 name, dbus_message_get_signature (message),
2000 _DBUS_ASSERT_ERROR_IS_SET (error);
2004 if ((* mh->handler) (connection, transaction, message, error))
2006 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2007 _dbus_verbose ("Driver handler succeeded\n");
2012 _DBUS_ASSERT_ERROR_IS_SET (error);
2013 _dbus_verbose ("Driver handler returned failure\n");
2019 _dbus_verbose ("No driver handler for message \"%s\"\n",
2022 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2023 "%s does not understand message %s",
2024 DBUS_SERVICE_DBUS, name);
2030 bus_driver_remove_connection (DBusConnection *connection)
2032 /* FIXME 1.0 Does nothing for now, should unregister the connection
2033 * with the bus driver.