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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "activation.h"
26 #include "connection.h"
33 #include <dbus/dbus-string.h>
34 #include <dbus/dbus-internals.h>
35 #include <dbus/dbus-marshal-recursive.h>
38 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
39 DBusMessage *hello_message,
40 BusTransaction *transaction,
44 bus_driver_send_service_owner_changed (const char *service_name,
45 const char *old_owner,
46 const char *new_owner,
47 BusTransaction *transaction,
52 const char *null_service;
54 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
57 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
59 old_owner ? old_owner : null_service,
60 new_owner ? new_owner : null_service);
62 message = dbus_message_new_signal (DBUS_PATH_DBUS,
72 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
75 if (!dbus_message_append_args (message,
76 DBUS_TYPE_STRING, &service_name,
77 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
78 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
82 _dbus_assert (dbus_message_has_signature (message, "sss"));
84 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
85 dbus_message_unref (message);
90 dbus_message_unref (message);
96 bus_driver_send_service_lost (DBusConnection *connection,
97 const char *service_name,
98 BusTransaction *transaction,
101 DBusMessage *message;
103 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
105 message = dbus_message_new_signal (DBUS_PATH_DBUS,
115 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
116 !dbus_message_append_args (message,
117 DBUS_TYPE_STRING, &service_name,
120 dbus_message_unref (message);
125 if (!bus_transaction_send_from_driver (transaction, connection, message))
127 dbus_message_unref (message);
133 dbus_message_unref (message);
139 bus_driver_send_service_acquired (DBusConnection *connection,
140 const char *service_name,
141 BusTransaction *transaction,
144 DBusMessage *message;
146 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
148 message = dbus_message_new_signal (DBUS_PATH_DBUS,
158 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
159 !dbus_message_append_args (message,
160 DBUS_TYPE_STRING, &service_name,
163 dbus_message_unref (message);
168 if (!bus_transaction_send_from_driver (transaction, connection, message))
170 dbus_message_unref (message);
176 dbus_message_unref (message);
182 create_unique_client_name (BusRegistry *registry,
185 /* We never want to use the same unique client name twice, because
186 * we want to guarantee that if you send a message to a given unique
187 * name, you always get the same application. So we use two numbers
188 * for INT_MAX * INT_MAX combinations, should be pretty safe against
191 /* FIXME these should be in BusRegistry rather than static vars */
192 static int next_major_number = 0;
193 static int next_minor_number = 0;
196 len = _dbus_string_get_length (str);
200 /* start out with 1-0, go to 1-1, 1-2, 1-3,
201 * up to 1-MAXINT, then 2-0, 2-1, etc.
203 if (next_minor_number <= 0)
205 next_major_number += 1;
206 next_minor_number = 0;
207 if (next_major_number <= 0)
208 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
211 _dbus_assert (next_major_number > 0);
212 _dbus_assert (next_minor_number >= 0);
214 /* appname:MAJOR-MINOR */
216 if (!_dbus_string_append (str, ":"))
219 if (!_dbus_string_append_int (str, next_major_number))
222 if (!_dbus_string_append (str, "."))
225 if (!_dbus_string_append_int (str, next_minor_number))
228 next_minor_number += 1;
230 /* Check if a client with the name exists */
231 if (bus_registry_lookup (registry, str) == NULL)
234 /* drop the number again, try the next one. */
235 _dbus_string_set_length (str, len);
242 bus_driver_handle_hello (DBusConnection *connection,
243 BusTransaction *transaction,
244 DBusMessage *message,
247 DBusString unique_name;
250 BusRegistry *registry;
251 BusConnections *connections;
253 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
255 if (bus_connection_is_active (connection))
257 /* We already handled an Hello message for this connection. */
258 dbus_set_error (error, DBUS_ERROR_FAILED,
259 "Already handled an Hello message");
263 /* Note that when these limits are exceeded we don't disconnect the
264 * connection; we just sort of leave it hanging there until it times
265 * out or disconnects itself or is dropped due to the max number of
266 * incomplete connections. It's even OK if the connection wants to
267 * retry the hello message, we support that.
269 connections = bus_connection_get_connections (connection);
270 if (!bus_connections_check_limits (connections, connection,
273 _DBUS_ASSERT_ERROR_IS_SET (error);
277 if (!_dbus_string_init (&unique_name))
285 registry = bus_connection_get_registry (connection);
287 if (!create_unique_client_name (registry, &unique_name))
293 if (!bus_connection_complete (connection, &unique_name, error))
295 _DBUS_ASSERT_ERROR_IS_SET (error);
299 if (!dbus_message_set_sender (message,
300 bus_connection_get_name (connection)))
306 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
309 /* Create the service */
310 service = bus_registry_ensure (registry,
311 &unique_name, connection, 0, transaction, error);
315 _dbus_assert (bus_connection_is_active (connection));
319 _dbus_string_free (&unique_name);
324 bus_driver_send_welcome_message (DBusConnection *connection,
325 DBusMessage *hello_message,
326 BusTransaction *transaction,
329 DBusMessage *welcome;
332 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
334 name = bus_connection_get_name (connection);
335 _dbus_assert (name != NULL);
337 welcome = dbus_message_new_method_return (hello_message);
344 if (!dbus_message_append_args (welcome,
345 DBUS_TYPE_STRING, &name,
348 dbus_message_unref (welcome);
353 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
355 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
357 dbus_message_unref (welcome);
363 dbus_message_unref (welcome);
369 bus_driver_handle_list_services (DBusConnection *connection,
370 BusTransaction *transaction,
371 DBusMessage *message,
377 BusRegistry *registry;
379 DBusMessageIter iter;
382 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
384 registry = bus_connection_get_registry (connection);
386 reply = dbus_message_new_method_return (message);
393 if (!bus_registry_list_services (registry, &services, &len))
395 dbus_message_unref (reply);
400 dbus_message_iter_init_append (reply, &iter);
402 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
403 DBUS_TYPE_STRING_AS_STRING,
406 dbus_free_string_array (services);
407 dbus_message_unref (reply);
413 /* Include the bus driver in the list */
414 const char *v_STRING = DBUS_SERVICE_DBUS;
415 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
418 dbus_free_string_array (services);
419 dbus_message_unref (reply);
428 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
431 dbus_free_string_array (services);
432 dbus_message_unref (reply);
439 dbus_free_string_array (services);
441 if (!dbus_message_iter_close_container (&iter, &sub))
443 dbus_message_unref (reply);
448 if (!bus_transaction_send_from_driver (transaction, connection, reply))
450 dbus_message_unref (reply);
456 dbus_message_unref (reply);
462 bus_driver_handle_list_activatable_services (DBusConnection *connection,
463 BusTransaction *transaction,
464 DBusMessage *message,
470 BusActivation *activation;
472 DBusMessageIter iter;
475 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
477 activation = bus_connection_get_activation (connection);
479 reply = dbus_message_new_method_return (message);
486 if (!bus_activation_list_services (activation, &services, &len))
488 dbus_message_unref (reply);
493 dbus_message_iter_init_append (reply, &iter);
495 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
496 DBUS_TYPE_STRING_AS_STRING,
499 dbus_free_string_array (services);
500 dbus_message_unref (reply);
506 /* Include the bus driver in the list */
507 const char *v_STRING = DBUS_SERVICE_DBUS;
508 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
511 dbus_free_string_array (services);
512 dbus_message_unref (reply);
521 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
524 dbus_free_string_array (services);
525 dbus_message_unref (reply);
532 dbus_free_string_array (services);
534 if (!dbus_message_iter_close_container (&iter, &sub))
536 dbus_message_unref (reply);
541 if (!bus_transaction_send_from_driver (transaction, connection, reply))
543 dbus_message_unref (reply);
549 dbus_message_unref (reply);
555 bus_driver_handle_acquire_service (DBusConnection *connection,
556 BusTransaction *transaction,
557 DBusMessage *message,
561 DBusString service_name;
563 dbus_uint32_t service_reply;
566 BusRegistry *registry;
568 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
570 registry = bus_connection_get_registry (connection);
572 if (!dbus_message_get_args (message, error,
573 DBUS_TYPE_STRING, &name,
574 DBUS_TYPE_UINT32, &flags,
578 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
583 _dbus_string_init_const (&service_name, name);
585 if (!bus_registry_acquire_service (registry, connection,
586 &service_name, flags,
587 &service_reply, transaction,
591 reply = dbus_message_new_method_return (message);
598 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
604 if (!bus_transaction_send_from_driver (transaction, connection, reply))
614 dbus_message_unref (reply);
619 bus_driver_handle_release_service (DBusConnection *connection,
620 BusTransaction *transaction,
621 DBusMessage *message,
625 DBusString service_name;
627 dbus_uint32_t service_reply;
629 BusRegistry *registry;
631 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
633 registry = bus_connection_get_registry (connection);
635 if (!dbus_message_get_args (message, error,
636 DBUS_TYPE_STRING, &name,
640 _dbus_verbose ("Trying to release name %s\n", name);
645 _dbus_string_init_const (&service_name, name);
647 if (!bus_registry_release_service (registry, connection,
648 &service_name, &service_reply,
652 reply = dbus_message_new_method_return (message);
659 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
665 if (!bus_transaction_send_from_driver (transaction, connection, reply))
675 dbus_message_unref (reply);
680 bus_driver_handle_service_exists (DBusConnection *connection,
681 BusTransaction *transaction,
682 DBusMessage *message,
686 DBusString service_name;
688 dbus_bool_t service_exists;
691 BusRegistry *registry;
693 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
695 registry = bus_connection_get_registry (connection);
697 if (!dbus_message_get_args (message, error,
698 DBUS_TYPE_STRING, &name,
704 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
706 service_exists = TRUE;
710 _dbus_string_init_const (&service_name, name);
711 service = bus_registry_lookup (registry, &service_name);
712 service_exists = service != NULL;
715 reply = dbus_message_new_method_return (message);
722 if (!dbus_message_append_args (reply,
723 DBUS_TYPE_BOOLEAN, &service_exists,
730 if (!bus_transaction_send_from_driver (transaction, connection, reply))
740 dbus_message_unref (reply);
746 bus_driver_handle_activate_service (DBusConnection *connection,
747 BusTransaction *transaction,
748 DBusMessage *message,
754 BusActivation *activation;
756 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
758 activation = bus_connection_get_activation (connection);
760 if (!dbus_message_get_args (message, error,
761 DBUS_TYPE_STRING, &name,
762 DBUS_TYPE_UINT32, &flags,
765 _DBUS_ASSERT_ERROR_IS_SET (error);
766 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
772 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
773 message, name, error))
775 _DBUS_ASSERT_ERROR_IS_SET (error);
776 _dbus_verbose ("bus_activation_activate_service() failed\n");
787 send_ack_reply (DBusConnection *connection,
788 BusTransaction *transaction,
789 DBusMessage *message,
794 reply = dbus_message_new_method_return (message);
801 if (!bus_transaction_send_from_driver (transaction, connection, reply))
804 dbus_message_unref (reply);
808 dbus_message_unref (reply);
814 bus_driver_handle_update_activation_environment (DBusConnection *connection,
815 BusTransaction *transaction,
816 DBusMessage *message,
820 BusActivation *activation;
821 DBusMessageIter iter;
822 DBusMessageIter dict_iter;
823 DBusMessageIter dict_entry_iter;
827 DBusList *keys, *key_link;
828 DBusList *values, *value_link;
830 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
832 activation = bus_connection_get_activation (connection);
834 dbus_message_iter_init (message, &iter);
836 /* The message signature has already been checked for us,
837 * so let's just assert it's right.
839 msg_type = dbus_message_iter_get_arg_type (&iter);
841 _dbus_assert (msg_type == DBUS_TYPE_ARRAY);
843 dbus_message_iter_recurse (&iter, &dict_iter);
847 /* Then loop through the sent dictionary, add the location of
848 * the environment keys and values to lists. The result will
849 * be in reverse order, so we don't have to constantly search
850 * for the end of the list in a loop.
854 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
856 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
858 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
864 dbus_message_iter_get_basic (&dict_entry_iter, &key);
865 dbus_message_iter_next (&dict_entry_iter);
867 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
869 if (value_type != DBUS_TYPE_STRING)
872 dbus_message_iter_get_basic (&dict_entry_iter, &value);
874 if (!_dbus_list_append (&keys, key))
880 if (!_dbus_list_append (&values, value))
886 dbus_message_iter_next (&dict_entry_iter);
889 if (key_type != DBUS_TYPE_INVALID)
892 dbus_message_iter_next (&dict_iter);
895 if (array_type != DBUS_TYPE_INVALID)
898 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
902 while (key_link != NULL)
907 key = key_link->data;
908 value = value_link->data;
910 if (!bus_activation_set_environment_variable (activation,
913 _DBUS_ASSERT_ERROR_IS_SET (error);
914 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
917 key_link = _dbus_list_get_next_link (&keys, key_link);
918 value_link = _dbus_list_get_next_link (&values, value_link);
921 /* FIXME: We can fail early having set only some of the environment variables,
922 * (because of OOM failure). It's sort of hard to fix and it doesn't really
923 * matter, so we're punting for now.
925 if (key_link != NULL)
928 if (!send_ack_reply (connection, transaction,
935 _dbus_list_clear (&keys);
936 _dbus_list_clear (&values);
941 bus_driver_handle_add_match (DBusConnection *connection,
942 BusTransaction *transaction,
943 DBusMessage *message,
949 BusMatchmaker *matchmaker;
951 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
956 if (bus_connection_get_n_match_rules (connection) >=
957 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
959 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
960 "Connection \"%s\" is not allowed to add more match rules "
961 "(increase limits in configuration file if required)",
962 bus_connection_is_active (connection) ?
963 bus_connection_get_name (connection) :
968 if (!dbus_message_get_args (message, error,
969 DBUS_TYPE_STRING, &text,
972 _dbus_verbose ("No memory to get arguments to AddMatch\n");
976 _dbus_string_init_const (&str, text);
978 rule = bus_match_rule_parse (connection, &str, error);
982 matchmaker = bus_connection_get_matchmaker (connection);
984 if (!bus_matchmaker_add_rule (matchmaker, rule))
990 if (!send_ack_reply (connection, transaction,
993 bus_matchmaker_remove_rule (matchmaker, rule);
997 bus_match_rule_unref (rule);
1002 _DBUS_ASSERT_ERROR_IS_SET (error);
1004 bus_match_rule_unref (rule);
1009 bus_driver_handle_remove_match (DBusConnection *connection,
1010 BusTransaction *transaction,
1011 DBusMessage *message,
1017 BusMatchmaker *matchmaker;
1019 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1024 if (!dbus_message_get_args (message, error,
1025 DBUS_TYPE_STRING, &text,
1028 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1032 _dbus_string_init_const (&str, text);
1034 rule = bus_match_rule_parse (connection, &str, error);
1038 /* Send the ack before we remove the rule, since the ack is undone
1039 * on transaction cancel, but rule removal isn't.
1041 if (!send_ack_reply (connection, transaction,
1045 matchmaker = bus_connection_get_matchmaker (connection);
1047 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1050 bus_match_rule_unref (rule);
1055 _DBUS_ASSERT_ERROR_IS_SET (error);
1057 bus_match_rule_unref (rule);
1062 bus_driver_handle_get_service_owner (DBusConnection *connection,
1063 BusTransaction *transaction,
1064 DBusMessage *message,
1068 const char *base_name;
1070 BusRegistry *registry;
1071 BusService *service;
1074 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1076 registry = bus_connection_get_registry (connection);
1081 if (! dbus_message_get_args (message, error,
1082 DBUS_TYPE_STRING, &text,
1086 _dbus_string_init_const (&str, text);
1087 service = bus_registry_lookup (registry, &str);
1088 if (service == NULL &&
1089 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1091 /* ORG_FREEDESKTOP_DBUS owns itself */
1092 base_name = DBUS_SERVICE_DBUS;
1094 else if (service == NULL)
1096 dbus_set_error (error,
1097 DBUS_ERROR_NAME_HAS_NO_OWNER,
1098 "Could not get owner of name '%s': no such name", text);
1103 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1104 if (base_name == NULL)
1106 /* FIXME - how is this error possible? */
1107 dbus_set_error (error,
1109 "Could not determine unique name for '%s'", text);
1112 _dbus_assert (*base_name == ':');
1115 _dbus_assert (base_name != NULL);
1117 reply = dbus_message_new_method_return (message);
1121 if (! dbus_message_append_args (reply,
1122 DBUS_TYPE_STRING, &base_name,
1126 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1129 dbus_message_unref (reply);
1134 BUS_SET_OOM (error);
1137 _DBUS_ASSERT_ERROR_IS_SET (error);
1139 dbus_message_unref (reply);
1144 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1145 BusTransaction *transaction,
1146 DBusMessage *message,
1150 DBusList *base_names;
1153 BusRegistry *registry;
1154 BusService *service;
1156 DBusMessageIter iter, array_iter;
1157 char *dbus_service_name = DBUS_SERVICE_DBUS;
1159 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1161 registry = bus_connection_get_registry (connection);
1167 if (! dbus_message_get_args (message, error,
1168 DBUS_TYPE_STRING, &text,
1172 _dbus_string_init_const (&str, text);
1173 service = bus_registry_lookup (registry, &str);
1174 if (service == NULL &&
1175 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1177 /* ORG_FREEDESKTOP_DBUS owns itself */
1178 if (! _dbus_list_append (&base_names, dbus_service_name))
1181 else if (service == NULL)
1183 dbus_set_error (error,
1184 DBUS_ERROR_NAME_HAS_NO_OWNER,
1185 "Could not get owners of name '%s': no such name", text);
1190 if (!bus_service_list_queued_owners (service,
1196 _dbus_assert (base_names != NULL);
1198 reply = dbus_message_new_method_return (message);
1202 dbus_message_iter_init_append (reply, &iter);
1203 if (!dbus_message_iter_open_container (&iter,
1205 DBUS_TYPE_STRING_AS_STRING,
1209 link = _dbus_list_get_first_link (&base_names);
1210 while (link != NULL)
1214 _dbus_assert (link->data != NULL);
1215 uname = (char *)link->data;
1217 if (!dbus_message_iter_append_basic (&array_iter,
1222 link = _dbus_list_get_next_link (&base_names, link);
1225 if (! dbus_message_iter_close_container (&iter, &array_iter))
1229 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1232 dbus_message_unref (reply);
1237 BUS_SET_OOM (error);
1240 _DBUS_ASSERT_ERROR_IS_SET (error);
1242 dbus_message_unref (reply);
1245 _dbus_list_clear (&base_names);
1251 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1252 BusTransaction *transaction,
1253 DBusMessage *message,
1256 const char *service;
1258 BusRegistry *registry;
1260 DBusConnection *conn;
1263 dbus_uint32_t uid32;
1265 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1267 registry = bus_connection_get_registry (connection);
1272 if (! dbus_message_get_args (message, error,
1273 DBUS_TYPE_STRING, &service,
1277 _dbus_verbose ("asked for UID of connection %s\n", service);
1279 _dbus_string_init_const (&str, service);
1280 serv = bus_registry_lookup (registry, &str);
1283 dbus_set_error (error,
1284 DBUS_ERROR_NAME_HAS_NO_OWNER,
1285 "Could not get UID of name '%s': no such name", service);
1289 conn = bus_service_get_primary_owners_connection (serv);
1291 reply = dbus_message_new_method_return (message);
1295 if (!dbus_connection_get_unix_user (conn, &uid))
1297 dbus_set_error (error,
1299 "Could not determine UID for '%s'", service);
1304 if (! dbus_message_append_args (reply,
1305 DBUS_TYPE_UINT32, &uid32,
1309 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1312 dbus_message_unref (reply);
1317 BUS_SET_OOM (error);
1320 _DBUS_ASSERT_ERROR_IS_SET (error);
1322 dbus_message_unref (reply);
1327 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1328 BusTransaction *transaction,
1329 DBusMessage *message,
1332 const char *service;
1334 BusRegistry *registry;
1336 DBusConnection *conn;
1339 dbus_uint32_t pid32;
1341 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1343 registry = bus_connection_get_registry (connection);
1348 if (! dbus_message_get_args (message, error,
1349 DBUS_TYPE_STRING, &service,
1353 _dbus_verbose ("asked for PID of connection %s\n", service);
1355 _dbus_string_init_const (&str, service);
1356 serv = bus_registry_lookup (registry, &str);
1359 dbus_set_error (error,
1360 DBUS_ERROR_NAME_HAS_NO_OWNER,
1361 "Could not get PID of name '%s': no such name", service);
1365 conn = bus_service_get_primary_owners_connection (serv);
1367 reply = dbus_message_new_method_return (message);
1371 if (!dbus_connection_get_unix_process_id (conn, &pid))
1373 dbus_set_error (error,
1374 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1375 "Could not determine PID for '%s'", service);
1380 if (! dbus_message_append_args (reply,
1381 DBUS_TYPE_UINT32, &pid32,
1385 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1388 dbus_message_unref (reply);
1393 BUS_SET_OOM (error);
1396 _DBUS_ASSERT_ERROR_IS_SET (error);
1398 dbus_message_unref (reply);
1403 bus_driver_handle_get_adt_audit_session_data (DBusConnection *connection,
1404 BusTransaction *transaction,
1405 DBusMessage *message,
1408 const char *service;
1410 BusRegistry *registry;
1412 DBusConnection *conn;
1415 dbus_uint32_t data_size;
1417 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1419 registry = bus_connection_get_registry (connection);
1424 if (! dbus_message_get_args (message, error,
1425 DBUS_TYPE_STRING, &service,
1429 _dbus_verbose ("asked for audit session data for connection %s\n", service);
1431 _dbus_string_init_const (&str, service);
1432 serv = bus_registry_lookup (registry, &str);
1435 dbus_set_error (error,
1436 DBUS_ERROR_NAME_HAS_NO_OWNER,
1437 "Could not get audit session data for name '%s': no such name", service);
1441 conn = bus_service_get_primary_owners_connection (serv);
1443 reply = dbus_message_new_method_return (message);
1447 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1449 dbus_set_error (error,
1450 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1451 "Could not determine audit session data for '%s'", service);
1455 if (! dbus_message_append_args (reply,
1456 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1460 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1463 dbus_message_unref (reply);
1468 BUS_SET_OOM (error);
1471 _DBUS_ASSERT_ERROR_IS_SET (error);
1473 dbus_message_unref (reply);
1478 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1479 BusTransaction *transaction,
1480 DBusMessage *message,
1483 const char *service;
1485 BusRegistry *registry;
1487 DBusConnection *conn;
1489 BusSELinuxID *context;
1491 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1493 registry = bus_connection_get_registry (connection);
1498 if (! dbus_message_get_args (message, error,
1499 DBUS_TYPE_STRING, &service,
1503 _dbus_verbose ("asked for security context of connection %s\n", service);
1505 _dbus_string_init_const (&str, service);
1506 serv = bus_registry_lookup (registry, &str);
1509 dbus_set_error (error,
1510 DBUS_ERROR_NAME_HAS_NO_OWNER,
1511 "Could not get security context of name '%s': no such name", service);
1515 conn = bus_service_get_primary_owners_connection (serv);
1517 reply = dbus_message_new_method_return (message);
1521 context = bus_connection_get_selinux_id (conn);
1524 dbus_set_error (error,
1525 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1526 "Could not determine security context for '%s'", service);
1530 if (! bus_selinux_append_context (reply, context, error))
1533 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1536 dbus_message_unref (reply);
1541 BUS_SET_OOM (error);
1544 _DBUS_ASSERT_ERROR_IS_SET (error);
1546 dbus_message_unref (reply);
1551 bus_driver_handle_reload_config (DBusConnection *connection,
1552 BusTransaction *transaction,
1553 DBusMessage *message,
1556 BusContext *context;
1559 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1563 context = bus_connection_get_context (connection);
1564 if (!bus_context_reload_config (context, error))
1567 reply = dbus_message_new_method_return (message);
1571 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1574 dbus_message_unref (reply);
1578 BUS_SET_OOM (error);
1581 _DBUS_ASSERT_ERROR_IS_SET (error);
1583 dbus_message_unref (reply);
1588 bus_driver_handle_get_id (DBusConnection *connection,
1589 BusTransaction *transaction,
1590 DBusMessage *message,
1593 BusContext *context;
1596 const char *v_STRING;
1598 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1600 if (!_dbus_string_init (&uuid))
1602 BUS_SET_OOM (error);
1608 context = bus_connection_get_context (connection);
1609 if (!bus_context_get_id (context, &uuid))
1612 reply = dbus_message_new_method_return (message);
1616 v_STRING = _dbus_string_get_const_data (&uuid);
1617 if (!dbus_message_append_args (reply,
1618 DBUS_TYPE_STRING, &v_STRING,
1622 _dbus_assert (dbus_message_has_signature (reply, "s"));
1624 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1627 _dbus_string_free (&uuid);
1628 dbus_message_unref (reply);
1632 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1634 BUS_SET_OOM (error);
1637 dbus_message_unref (reply);
1638 _dbus_string_free (&uuid);
1642 /* For speed it might be useful to sort this in order of
1643 * frequency of use (but doesn't matter with only a few items
1649 const char *in_args;
1650 const char *out_args;
1651 dbus_bool_t (* handler) (DBusConnection *connection,
1652 BusTransaction *transaction,
1653 DBusMessage *message,
1655 } message_handlers[] = {
1658 DBUS_TYPE_STRING_AS_STRING,
1659 bus_driver_handle_hello },
1661 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1662 DBUS_TYPE_UINT32_AS_STRING,
1663 bus_driver_handle_acquire_service },
1665 DBUS_TYPE_STRING_AS_STRING,
1666 DBUS_TYPE_UINT32_AS_STRING,
1667 bus_driver_handle_release_service },
1668 { "StartServiceByName",
1669 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1670 DBUS_TYPE_UINT32_AS_STRING,
1671 bus_driver_handle_activate_service },
1672 { "UpdateActivationEnvironment",
1673 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,
1675 bus_driver_handle_update_activation_environment },
1677 DBUS_TYPE_STRING_AS_STRING,
1678 DBUS_TYPE_BOOLEAN_AS_STRING,
1679 bus_driver_handle_service_exists },
1682 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1683 bus_driver_handle_list_services },
1684 { "ListActivatableNames",
1686 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1687 bus_driver_handle_list_activatable_services },
1689 DBUS_TYPE_STRING_AS_STRING,
1691 bus_driver_handle_add_match },
1693 DBUS_TYPE_STRING_AS_STRING,
1695 bus_driver_handle_remove_match },
1697 DBUS_TYPE_STRING_AS_STRING,
1698 DBUS_TYPE_STRING_AS_STRING,
1699 bus_driver_handle_get_service_owner },
1700 { "ListQueuedOwners",
1701 DBUS_TYPE_STRING_AS_STRING,
1702 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1703 bus_driver_handle_list_queued_owners },
1704 { "GetConnectionUnixUser",
1705 DBUS_TYPE_STRING_AS_STRING,
1706 DBUS_TYPE_UINT32_AS_STRING,
1707 bus_driver_handle_get_connection_unix_user },
1708 { "GetConnectionUnixProcessID",
1709 DBUS_TYPE_STRING_AS_STRING,
1710 DBUS_TYPE_UINT32_AS_STRING,
1711 bus_driver_handle_get_connection_unix_process_id },
1712 { "GetAdtAuditSessionData",
1713 DBUS_TYPE_STRING_AS_STRING,
1714 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1715 bus_driver_handle_get_adt_audit_session_data },
1716 { "GetConnectionSELinuxSecurityContext",
1717 DBUS_TYPE_STRING_AS_STRING,
1718 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1719 bus_driver_handle_get_connection_selinux_security_context },
1723 bus_driver_handle_reload_config },
1726 DBUS_TYPE_STRING_AS_STRING,
1727 bus_driver_handle_get_id }
1731 write_args_for_direction (DBusString *xml,
1732 const char *signature,
1735 DBusTypeReader typereader;
1739 _dbus_string_init_const (&sigstr, signature);
1740 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1742 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1744 const DBusString *subsig;
1747 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1748 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1751 if (!_dbus_string_append_len (xml,
1752 _dbus_string_get_const_data (subsig) + start,
1755 if (!_dbus_string_append (xml, "\"/>\n"))
1758 _dbus_type_reader_next (&typereader);
1766 bus_driver_generate_introspect_string (DBusString *xml)
1770 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1772 if (!_dbus_string_append (xml, "<node>\n"))
1774 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE))
1776 if (!_dbus_string_append (xml, " <method name=\"Introspect\">\n"))
1778 if (!_dbus_string_append_printf (xml, " <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING))
1780 if (!_dbus_string_append (xml, " </method>\n"))
1782 if (!_dbus_string_append (xml, " </interface>\n"))
1785 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1786 DBUS_INTERFACE_DBUS))
1790 while (i < _DBUS_N_ELEMENTS (message_handlers))
1793 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1794 message_handlers[i].name))
1797 if (!write_args_for_direction (xml, message_handlers[i].in_args, TRUE))
1800 if (!write_args_for_direction (xml, message_handlers[i].out_args, FALSE))
1803 if (!_dbus_string_append (xml, " </method>\n"))
1809 if (!_dbus_string_append_printf (xml, " <signal name=\"NameOwnerChanged\">\n"))
1812 if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1815 if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1818 if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1821 if (!_dbus_string_append_printf (xml, " </signal>\n"))
1826 if (!_dbus_string_append_printf (xml, " <signal name=\"NameLost\">\n"))
1829 if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1832 if (!_dbus_string_append_printf (xml, " </signal>\n"))
1837 if (!_dbus_string_append_printf (xml, " <signal name=\"NameAcquired\">\n"))
1840 if (!_dbus_string_append_printf (xml, " <arg type=\"s\"/>\n"))
1843 if (!_dbus_string_append_printf (xml, " </signal>\n"))
1846 if (!_dbus_string_append (xml, " </interface>\n"))
1849 if (!_dbus_string_append (xml, "</node>\n"))
1856 bus_driver_handle_introspect (DBusConnection *connection,
1857 BusTransaction *transaction,
1858 DBusMessage *message,
1863 const char *v_STRING;
1865 _dbus_verbose ("Introspect() on bus driver\n");
1867 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1871 if (! dbus_message_get_args (message, error,
1874 _DBUS_ASSERT_ERROR_IS_SET (error);
1878 if (!_dbus_string_init (&xml))
1880 BUS_SET_OOM (error);
1884 if (!bus_driver_generate_introspect_string (&xml))
1887 v_STRING = _dbus_string_get_const_data (&xml);
1889 reply = dbus_message_new_method_return (message);
1893 if (! dbus_message_append_args (reply,
1894 DBUS_TYPE_STRING, &v_STRING,
1898 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1901 dbus_message_unref (reply);
1902 _dbus_string_free (&xml);
1907 BUS_SET_OOM (error);
1910 dbus_message_unref (reply);
1912 _dbus_string_free (&xml);
1918 bus_driver_handle_message (DBusConnection *connection,
1919 BusTransaction *transaction,
1920 DBusMessage *message,
1923 const char *name, *sender, *interface;
1926 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1928 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1930 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1931 return TRUE; /* we just ignore this */
1934 if (dbus_message_is_method_call (message,
1935 DBUS_INTERFACE_INTROSPECTABLE,
1937 return bus_driver_handle_introspect (connection, transaction, message, error);
1939 interface = dbus_message_get_interface (message);
1940 if (interface == NULL)
1941 interface = DBUS_INTERFACE_DBUS;
1943 _dbus_assert (dbus_message_get_member (message) != NULL);
1945 name = dbus_message_get_member (message);
1946 sender = dbus_message_get_sender (message);
1948 if (strcmp (interface,
1949 DBUS_INTERFACE_DBUS) != 0)
1951 _dbus_verbose ("Driver got message to unknown interface \"%s\"\n",
1956 _dbus_verbose ("Driver got a method call: %s\n",
1957 dbus_message_get_member (message));
1959 /* security checks should have kept this from getting here */
1960 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1963 while (i < _DBUS_N_ELEMENTS (message_handlers))
1965 if (strcmp (message_handlers[i].name, name) == 0)
1967 _dbus_verbose ("Found driver handler for %s\n", name);
1969 if (!dbus_message_has_signature (message, message_handlers[i].in_args))
1971 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1972 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1973 name, dbus_message_get_signature (message),
1974 message_handlers[i].in_args);
1976 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1977 "Call to %s has wrong args (%s, expected %s)\n",
1978 name, dbus_message_get_signature (message),
1979 message_handlers[i].in_args);
1980 _DBUS_ASSERT_ERROR_IS_SET (error);
1984 if ((* message_handlers[i].handler) (connection, transaction, message, error))
1986 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1987 _dbus_verbose ("Driver handler succeeded\n");
1992 _DBUS_ASSERT_ERROR_IS_SET (error);
1993 _dbus_verbose ("Driver handler returned failure\n");
2002 _dbus_verbose ("No driver handler for message \"%s\"\n",
2005 dbus_set_error (error, DBUS_ERROR_UNKNOWN_METHOD,
2006 "%s does not understand message %s",
2007 DBUS_SERVICE_DBUS, name);
2013 bus_driver_remove_connection (DBusConnection *connection)
2015 /* FIXME 1.0 Does nothing for now, should unregister the connection
2016 * with the bus driver.