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;
833 DBusList *keys, *key_link;
834 DBusList *values, *value_link;
836 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
838 activation = bus_connection_get_activation (connection);
840 dbus_message_iter_init (message, &iter);
842 /* The message signature has already been checked for us,
843 * so let's just assert it's right.
845 msg_type = dbus_message_iter_get_arg_type (&iter);
847 _dbus_assert (msg_type == DBUS_TYPE_ARRAY);
849 dbus_message_iter_recurse (&iter, &dict_iter);
853 /* Then loop through the sent dictionary, add the location of
854 * the environment keys and values to lists. The result will
855 * be in reverse order, so we don't have to constantly search
856 * for the end of the list in a loop.
860 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
862 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
864 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
870 dbus_message_iter_get_basic (&dict_entry_iter, &key);
871 dbus_message_iter_next (&dict_entry_iter);
873 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
875 if (value_type != DBUS_TYPE_STRING)
878 dbus_message_iter_get_basic (&dict_entry_iter, &value);
880 if (!_dbus_list_append (&keys, key))
886 if (!_dbus_list_append (&values, value))
892 dbus_message_iter_next (&dict_entry_iter);
895 if (key_type != DBUS_TYPE_INVALID)
898 dbus_message_iter_next (&dict_iter);
901 if (array_type != DBUS_TYPE_INVALID)
904 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
908 while (key_link != NULL)
913 key = key_link->data;
914 value = value_link->data;
916 if (!bus_activation_set_environment_variable (activation,
919 _DBUS_ASSERT_ERROR_IS_SET (error);
920 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
923 key_link = _dbus_list_get_next_link (&keys, key_link);
924 value_link = _dbus_list_get_next_link (&values, value_link);
927 /* FIXME: We can fail early having set only some of the environment variables,
928 * (because of OOM failure). It's sort of hard to fix and it doesn't really
929 * matter, so we're punting for now.
931 if (key_link != NULL)
934 if (!send_ack_reply (connection, transaction,
941 _dbus_list_clear (&keys);
942 _dbus_list_clear (&values);
947 bus_driver_handle_add_match (DBusConnection *connection,
948 BusTransaction *transaction,
949 DBusMessage *message,
955 BusMatchmaker *matchmaker;
957 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
962 if (bus_connection_get_n_match_rules (connection) >=
963 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
965 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
966 "Connection \"%s\" is not allowed to add more match rules "
967 "(increase limits in configuration file if required)",
968 bus_connection_is_active (connection) ?
969 bus_connection_get_name (connection) :
974 if (!dbus_message_get_args (message, error,
975 DBUS_TYPE_STRING, &text,
978 _dbus_verbose ("No memory to get arguments to AddMatch\n");
982 _dbus_string_init_const (&str, text);
984 rule = bus_match_rule_parse (connection, &str, error);
988 matchmaker = bus_connection_get_matchmaker (connection);
990 if (!bus_matchmaker_add_rule (matchmaker, rule))
996 if (!send_ack_reply (connection, transaction,
999 bus_matchmaker_remove_rule (matchmaker, rule);
1003 bus_match_rule_unref (rule);
1008 _DBUS_ASSERT_ERROR_IS_SET (error);
1010 bus_match_rule_unref (rule);
1015 bus_driver_handle_remove_match (DBusConnection *connection,
1016 BusTransaction *transaction,
1017 DBusMessage *message,
1023 BusMatchmaker *matchmaker;
1025 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1030 if (!dbus_message_get_args (message, error,
1031 DBUS_TYPE_STRING, &text,
1034 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1038 _dbus_string_init_const (&str, text);
1040 rule = bus_match_rule_parse (connection, &str, error);
1044 /* Send the ack before we remove the rule, since the ack is undone
1045 * on transaction cancel, but rule removal isn't.
1047 if (!send_ack_reply (connection, transaction,
1051 matchmaker = bus_connection_get_matchmaker (connection);
1053 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1056 bus_match_rule_unref (rule);
1061 _DBUS_ASSERT_ERROR_IS_SET (error);
1063 bus_match_rule_unref (rule);
1068 bus_driver_handle_get_service_owner (DBusConnection *connection,
1069 BusTransaction *transaction,
1070 DBusMessage *message,
1074 const char *base_name;
1076 BusRegistry *registry;
1077 BusService *service;
1080 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1082 registry = bus_connection_get_registry (connection);
1087 if (! dbus_message_get_args (message, error,
1088 DBUS_TYPE_STRING, &text,
1092 _dbus_string_init_const (&str, text);
1093 service = bus_registry_lookup (registry, &str);
1094 if (service == NULL &&
1095 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1097 /* ORG_FREEDESKTOP_DBUS owns itself */
1098 base_name = DBUS_SERVICE_DBUS;
1100 else if (service == NULL)
1102 dbus_set_error (error,
1103 DBUS_ERROR_NAME_HAS_NO_OWNER,
1104 "Could not get owner of name '%s': no such name", text);
1109 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1110 if (base_name == NULL)
1112 /* FIXME - how is this error possible? */
1113 dbus_set_error (error,
1115 "Could not determine unique name for '%s'", text);
1118 _dbus_assert (*base_name == ':');
1121 _dbus_assert (base_name != NULL);
1123 reply = dbus_message_new_method_return (message);
1127 if (! dbus_message_append_args (reply,
1128 DBUS_TYPE_STRING, &base_name,
1132 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1135 dbus_message_unref (reply);
1140 BUS_SET_OOM (error);
1143 _DBUS_ASSERT_ERROR_IS_SET (error);
1145 dbus_message_unref (reply);
1150 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1151 BusTransaction *transaction,
1152 DBusMessage *message,
1156 DBusList *base_names;
1159 BusRegistry *registry;
1160 BusService *service;
1162 DBusMessageIter iter, array_iter;
1163 char *dbus_service_name = DBUS_SERVICE_DBUS;
1165 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1167 registry = bus_connection_get_registry (connection);
1173 if (! dbus_message_get_args (message, error,
1174 DBUS_TYPE_STRING, &text,
1178 _dbus_string_init_const (&str, text);
1179 service = bus_registry_lookup (registry, &str);
1180 if (service == NULL &&
1181 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1183 /* ORG_FREEDESKTOP_DBUS owns itself */
1184 if (! _dbus_list_append (&base_names, dbus_service_name))
1187 else if (service == NULL)
1189 dbus_set_error (error,
1190 DBUS_ERROR_NAME_HAS_NO_OWNER,
1191 "Could not get owners of name '%s': no such name", text);
1196 if (!bus_service_list_queued_owners (service,
1202 _dbus_assert (base_names != NULL);
1204 reply = dbus_message_new_method_return (message);
1208 dbus_message_iter_init_append (reply, &iter);
1209 if (!dbus_message_iter_open_container (&iter,
1211 DBUS_TYPE_STRING_AS_STRING,
1215 link = _dbus_list_get_first_link (&base_names);
1216 while (link != NULL)
1220 _dbus_assert (link->data != NULL);
1221 uname = (char *)link->data;
1223 if (!dbus_message_iter_append_basic (&array_iter,
1228 link = _dbus_list_get_next_link (&base_names, link);
1231 if (! dbus_message_iter_close_container (&iter, &array_iter))
1235 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1238 dbus_message_unref (reply);
1243 BUS_SET_OOM (error);
1246 _DBUS_ASSERT_ERROR_IS_SET (error);
1248 dbus_message_unref (reply);
1251 _dbus_list_clear (&base_names);
1257 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1258 BusTransaction *transaction,
1259 DBusMessage *message,
1262 const char *service;
1264 BusRegistry *registry;
1266 DBusConnection *conn;
1269 dbus_uint32_t uid32;
1271 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1273 registry = bus_connection_get_registry (connection);
1278 if (! dbus_message_get_args (message, error,
1279 DBUS_TYPE_STRING, &service,
1283 _dbus_verbose ("asked for UID of connection %s\n", service);
1285 _dbus_string_init_const (&str, service);
1286 serv = bus_registry_lookup (registry, &str);
1289 dbus_set_error (error,
1290 DBUS_ERROR_NAME_HAS_NO_OWNER,
1291 "Could not get UID of name '%s': no such name", service);
1295 conn = bus_service_get_primary_owners_connection (serv);
1297 reply = dbus_message_new_method_return (message);
1301 if (!dbus_connection_get_unix_user (conn, &uid))
1303 dbus_set_error (error,
1305 "Could not determine UID for '%s'", service);
1310 if (! dbus_message_append_args (reply,
1311 DBUS_TYPE_UINT32, &uid32,
1315 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1318 dbus_message_unref (reply);
1323 BUS_SET_OOM (error);
1326 _DBUS_ASSERT_ERROR_IS_SET (error);
1328 dbus_message_unref (reply);
1333 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1334 BusTransaction *transaction,
1335 DBusMessage *message,
1338 const char *service;
1340 BusRegistry *registry;
1342 DBusConnection *conn;
1345 dbus_uint32_t pid32;
1347 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1349 registry = bus_connection_get_registry (connection);
1354 if (! dbus_message_get_args (message, error,
1355 DBUS_TYPE_STRING, &service,
1359 _dbus_verbose ("asked for PID of connection %s\n", service);
1361 _dbus_string_init_const (&str, service);
1362 serv = bus_registry_lookup (registry, &str);
1365 dbus_set_error (error,
1366 DBUS_ERROR_NAME_HAS_NO_OWNER,
1367 "Could not get PID of name '%s': no such name", service);
1371 conn = bus_service_get_primary_owners_connection (serv);
1373 reply = dbus_message_new_method_return (message);
1377 if (!dbus_connection_get_unix_process_id (conn, &pid))
1379 dbus_set_error (error,
1380 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1381 "Could not determine PID for '%s'", service);
1386 if (! dbus_message_append_args (reply,
1387 DBUS_TYPE_UINT32, &pid32,
1391 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1394 dbus_message_unref (reply);
1399 BUS_SET_OOM (error);
1402 _DBUS_ASSERT_ERROR_IS_SET (error);
1404 dbus_message_unref (reply);
1409 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1410 BusTransaction *transaction,
1411 DBusMessage *message,
1414 const char *service;
1416 BusRegistry *registry;
1418 DBusConnection *conn;
1421 dbus_uint32_t data_size;
1423 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1425 registry = bus_connection_get_registry (connection);
1430 if (! dbus_message_get_args (message, error,
1431 DBUS_TYPE_STRING, &service,
1435 _dbus_verbose ("asked for audit session data for connection %s\n", service);
1437 _dbus_string_init_const (&str, service);
1438 serv = bus_registry_lookup (registry, &str);
1441 dbus_set_error (error,
1442 DBUS_ERROR_NAME_HAS_NO_OWNER,
1443 "Could not get audit session data for name '%s': no such name", service);
1447 conn = bus_service_get_primary_owners_connection (serv);
1449 reply = dbus_message_new_method_return (message);
1453 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1455 dbus_set_error (error,
1456 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1457 "Could not determine audit session data for '%s'", service);
1461 if (! dbus_message_append_args (reply,
1462 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1466 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1469 dbus_message_unref (reply);
1474 BUS_SET_OOM (error);
1477 _DBUS_ASSERT_ERROR_IS_SET (error);
1479 dbus_message_unref (reply);
1484 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1485 BusTransaction *transaction,
1486 DBusMessage *message,
1489 const char *service;
1491 BusRegistry *registry;
1493 DBusConnection *conn;
1495 BusSELinuxID *context;
1497 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1499 registry = bus_connection_get_registry (connection);
1504 if (! dbus_message_get_args (message, error,
1505 DBUS_TYPE_STRING, &service,
1509 _dbus_verbose ("asked for security context of connection %s\n", service);
1511 _dbus_string_init_const (&str, service);
1512 serv = bus_registry_lookup (registry, &str);
1515 dbus_set_error (error,
1516 DBUS_ERROR_NAME_HAS_NO_OWNER,
1517 "Could not get security context of name '%s': no such name", service);
1521 conn = bus_service_get_primary_owners_connection (serv);
1523 reply = dbus_message_new_method_return (message);
1527 context = bus_connection_get_selinux_id (conn);
1530 dbus_set_error (error,
1531 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1532 "Could not determine security context for '%s'", service);
1536 if (! bus_selinux_append_context (reply, context, error))
1539 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1542 dbus_message_unref (reply);
1547 BUS_SET_OOM (error);
1550 _DBUS_ASSERT_ERROR_IS_SET (error);
1552 dbus_message_unref (reply);
1557 bus_driver_handle_reload_config (DBusConnection *connection,
1558 BusTransaction *transaction,
1559 DBusMessage *message,
1562 BusContext *context;
1565 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1569 context = bus_connection_get_context (connection);
1570 if (!bus_context_reload_config (context, error))
1573 reply = dbus_message_new_method_return (message);
1577 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1580 dbus_message_unref (reply);
1584 BUS_SET_OOM (error);
1587 _DBUS_ASSERT_ERROR_IS_SET (error);
1589 dbus_message_unref (reply);
1594 bus_driver_handle_get_id (DBusConnection *connection,
1595 BusTransaction *transaction,
1596 DBusMessage *message,
1599 BusContext *context;
1602 const char *v_STRING;
1604 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1606 if (!_dbus_string_init (&uuid))
1608 BUS_SET_OOM (error);
1614 context = bus_connection_get_context (connection);
1615 if (!bus_context_get_id (context, &uuid))
1618 reply = dbus_message_new_method_return (message);
1622 v_STRING = _dbus_string_get_const_data (&uuid);
1623 if (!dbus_message_append_args (reply,
1624 DBUS_TYPE_STRING, &v_STRING,
1628 _dbus_assert (dbus_message_has_signature (reply, "s"));
1630 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1633 _dbus_string_free (&uuid);
1634 dbus_message_unref (reply);
1638 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1640 BUS_SET_OOM (error);
1643 dbus_message_unref (reply);
1644 _dbus_string_free (&uuid);
1651 const char *in_args;
1652 const char *out_args;
1653 dbus_bool_t (* handler) (DBusConnection *connection,
1654 BusTransaction *transaction,
1655 DBusMessage *message,
1659 /* For speed it might be useful to sort this in order of
1660 * frequency of use (but doesn't matter with only a few items
1663 static const MessageHandler dbus_message_handlers[] = {
1666 DBUS_TYPE_STRING_AS_STRING,
1667 bus_driver_handle_hello },
1669 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1670 DBUS_TYPE_UINT32_AS_STRING,
1671 bus_driver_handle_acquire_service },
1673 DBUS_TYPE_STRING_AS_STRING,
1674 DBUS_TYPE_UINT32_AS_STRING,
1675 bus_driver_handle_release_service },
1676 { "StartServiceByName",
1677 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1678 DBUS_TYPE_UINT32_AS_STRING,
1679 bus_driver_handle_activate_service },
1680 { "UpdateActivationEnvironment",
1681 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,
1683 bus_driver_handle_update_activation_environment },
1685 DBUS_TYPE_STRING_AS_STRING,
1686 DBUS_TYPE_BOOLEAN_AS_STRING,
1687 bus_driver_handle_service_exists },
1690 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1691 bus_driver_handle_list_services },
1692 { "ListActivatableNames",
1694 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1695 bus_driver_handle_list_activatable_services },
1697 DBUS_TYPE_STRING_AS_STRING,
1699 bus_driver_handle_add_match },
1701 DBUS_TYPE_STRING_AS_STRING,
1703 bus_driver_handle_remove_match },
1705 DBUS_TYPE_STRING_AS_STRING,
1706 DBUS_TYPE_STRING_AS_STRING,
1707 bus_driver_handle_get_service_owner },
1708 { "ListQueuedOwners",
1709 DBUS_TYPE_STRING_AS_STRING,
1710 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1711 bus_driver_handle_list_queued_owners },
1712 { "GetConnectionUnixUser",
1713 DBUS_TYPE_STRING_AS_STRING,
1714 DBUS_TYPE_UINT32_AS_STRING,
1715 bus_driver_handle_get_connection_unix_user },
1716 { "GetConnectionUnixProcessID",
1717 DBUS_TYPE_STRING_AS_STRING,
1718 DBUS_TYPE_UINT32_AS_STRING,
1719 bus_driver_handle_get_connection_unix_process_id },
1720 { "GetAdtAuditSessionData",
1721 DBUS_TYPE_STRING_AS_STRING,
1722 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1723 bus_driver_handle_get_adt_audit_session_data },
1724 { "GetConnectionSELinuxSecurityContext",
1725 DBUS_TYPE_STRING_AS_STRING,
1726 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1727 bus_driver_handle_get_connection_selinux_security_context },
1731 bus_driver_handle_reload_config },
1734 DBUS_TYPE_STRING_AS_STRING,
1735 bus_driver_handle_get_id },
1736 { NULL, NULL, NULL, NULL }
1739 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1740 BusTransaction *, DBusMessage *, DBusError *);
1742 static const MessageHandler introspectable_message_handlers[] = {
1743 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1744 { NULL, NULL, NULL, NULL }
1747 #ifdef DBUS_ENABLE_STATS
1748 static const MessageHandler stats_message_handlers[] = {
1749 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1750 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1751 { NULL, NULL, NULL, NULL }
1757 const MessageHandler *message_handlers;
1758 const char *extra_introspection;
1761 /* These should ideally be sorted by frequency of use, although it
1762 * probably doesn't matter with this few items */
1763 static InterfaceHandler interface_handlers[] = {
1764 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1765 " <signal name=\"NameOwnerChanged\">\n"
1766 " <arg type=\"s\"/>\n"
1767 " <arg type=\"s\"/>\n"
1768 " <arg type=\"s\"/>\n"
1770 " <signal name=\"NameLost\">\n"
1771 " <arg type=\"s\"/>\n"
1773 " <signal name=\"NameAcquired\">\n"
1774 " <arg type=\"s\"/>\n"
1776 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1777 #ifdef DBUS_ENABLE_STATS
1778 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1780 { NULL, NULL, NULL }
1784 write_args_for_direction (DBusString *xml,
1785 const char *signature,
1788 DBusTypeReader typereader;
1792 _dbus_string_init_const (&sigstr, signature);
1793 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1795 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1797 const DBusString *subsig;
1800 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1801 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1804 if (!_dbus_string_append_len (xml,
1805 _dbus_string_get_const_data (subsig) + start,
1808 if (!_dbus_string_append (xml, "\"/>\n"))
1811 _dbus_type_reader_next (&typereader);
1819 bus_driver_generate_introspect_string (DBusString *xml)
1821 const InterfaceHandler *ih;
1822 const MessageHandler *mh;
1824 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1826 if (!_dbus_string_append (xml, "<node>\n"))
1829 for (ih = interface_handlers; ih->name != NULL; ih++)
1831 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1835 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1837 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1841 if (!write_args_for_direction (xml, mh->in_args, TRUE))
1844 if (!write_args_for_direction (xml, mh->out_args, FALSE))
1847 if (!_dbus_string_append (xml, " </method>\n"))
1851 if (ih->extra_introspection != NULL &&
1852 !_dbus_string_append (xml, ih->extra_introspection))
1855 if (!_dbus_string_append (xml, " </interface>\n"))
1859 if (!_dbus_string_append (xml, "</node>\n"))
1866 bus_driver_handle_introspect (DBusConnection *connection,
1867 BusTransaction *transaction,
1868 DBusMessage *message,
1873 const char *v_STRING;
1875 _dbus_verbose ("Introspect() on bus driver\n");
1877 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1881 if (! dbus_message_get_args (message, error,
1884 _DBUS_ASSERT_ERROR_IS_SET (error);
1888 if (!_dbus_string_init (&xml))
1890 BUS_SET_OOM (error);
1894 if (!bus_driver_generate_introspect_string (&xml))
1897 v_STRING = _dbus_string_get_const_data (&xml);
1899 reply = dbus_message_new_method_return (message);
1903 if (! dbus_message_append_args (reply,
1904 DBUS_TYPE_STRING, &v_STRING,
1908 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1911 dbus_message_unref (reply);
1912 _dbus_string_free (&xml);
1917 BUS_SET_OOM (error);
1920 dbus_message_unref (reply);
1922 _dbus_string_free (&xml);
1928 bus_driver_handle_message (DBusConnection *connection,
1929 BusTransaction *transaction,
1930 DBusMessage *message,
1933 const char *name, *sender, *interface;
1934 const InterfaceHandler *ih;
1935 const MessageHandler *mh;
1936 dbus_bool_t found_interface = FALSE;
1938 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1940 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
1942 BusContext *context;
1944 context = bus_connection_get_context (connection);
1945 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
1948 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1950 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1951 return TRUE; /* we just ignore this */
1954 /* may be NULL, which means "any interface will do" */
1955 interface = dbus_message_get_interface (message);
1957 _dbus_assert (dbus_message_get_member (message) != NULL);
1959 name = dbus_message_get_member (message);
1960 sender = dbus_message_get_sender (message);
1962 _dbus_verbose ("Driver got a method call: %s\n",
1963 dbus_message_get_member (message));
1965 /* security checks should have kept this from getting here */
1966 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1968 for (ih = interface_handlers; ih->name != NULL; ih++)
1970 if (interface != NULL && strcmp (interface, ih->name) != 0)
1973 found_interface = TRUE;
1975 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1977 if (strcmp (mh->name, name) != 0)
1980 _dbus_verbose ("Found driver handler for %s\n", name);
1982 if (!dbus_message_has_signature (message, mh->in_args))
1984 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1985 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1986 name, dbus_message_get_signature (message),
1989 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1990 "Call to %s has wrong args (%s, expected %s)\n",
1991 name, dbus_message_get_signature (message),
1993 _DBUS_ASSERT_ERROR_IS_SET (error);
1997 if ((* mh->handler) (connection, transaction, message, error))
1999 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2000 _dbus_verbose ("Driver handler succeeded\n");
2005 _DBUS_ASSERT_ERROR_IS_SET (error);
2006 _dbus_verbose ("Driver handler returned failure\n");
2013 _dbus_verbose ("No driver handler for message \"%s\"\n",
2016 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2017 "%s does not understand message %s",
2018 DBUS_SERVICE_DBUS, name);
2024 bus_driver_remove_connection (DBusConnection *connection)
2026 /* FIXME 1.0 Does nothing for now, should unregister the connection
2027 * with the bus driver.