1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* driver.c Bus client (driver)
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2003, 2004, 2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 #include "activation.h"
27 #include "connection.h"
36 #include <dbus/dbus-asv-util.h>
37 #include <dbus/dbus-string.h>
38 #include <dbus/dbus-internals.h>
39 #include <dbus/dbus-message.h>
40 #include <dbus/dbus-marshal-recursive.h>
43 static DBusConnection *
44 bus_driver_get_conn_helper (DBusConnection *connection,
46 const char *what_we_want,
51 BusRegistry *registry;
56 if (!dbus_message_get_args (message, error,
57 DBUS_TYPE_STRING, &name,
61 _dbus_assert (name != NULL);
62 _dbus_verbose ("asked for %s of connection %s\n", what_we_want, name);
64 registry = bus_connection_get_registry (connection);
65 _dbus_string_init_const (&str, name);
66 serv = bus_registry_lookup (registry, &str);
70 dbus_set_error (error, DBUS_ERROR_NAME_HAS_NO_OWNER,
71 "Could not get %s of name '%s': no such name",
76 conn = bus_service_get_primary_owners_connection (serv);
77 _dbus_assert (conn != NULL);
85 static dbus_bool_t bus_driver_send_welcome_message (DBusConnection *connection,
86 DBusMessage *hello_message,
87 BusTransaction *transaction,
91 bus_driver_send_service_owner_changed (const char *service_name,
92 const char *old_owner,
93 const char *new_owner,
94 BusTransaction *transaction,
99 const char *null_service;
101 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
104 _dbus_verbose ("sending name owner changed: %s [%s -> %s]\n",
106 old_owner ? old_owner : null_service,
107 new_owner ? new_owner : null_service);
109 message = dbus_message_new_signal (DBUS_PATH_DBUS,
119 if (!dbus_message_set_sender (message, DBUS_SERVICE_DBUS))
122 if (!dbus_message_append_args (message,
123 DBUS_TYPE_STRING, &service_name,
124 DBUS_TYPE_STRING, old_owner ? &old_owner : &null_service,
125 DBUS_TYPE_STRING, new_owner ? &new_owner : &null_service,
129 _dbus_assert (dbus_message_has_signature (message, "sss"));
131 retval = bus_dispatch_matches (transaction, NULL, NULL, message, error);
132 dbus_message_unref (message);
137 dbus_message_unref (message);
143 bus_driver_send_service_lost (DBusConnection *connection,
144 const char *service_name,
145 BusTransaction *transaction,
148 DBusMessage *message;
150 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
152 message = dbus_message_new_signal (DBUS_PATH_DBUS,
162 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
163 !dbus_message_append_args (message,
164 DBUS_TYPE_STRING, &service_name,
167 dbus_message_unref (message);
172 if (!bus_transaction_send_from_driver (transaction, connection, message))
174 dbus_message_unref (message);
180 dbus_message_unref (message);
186 bus_driver_send_service_acquired (DBusConnection *connection,
187 const char *service_name,
188 BusTransaction *transaction,
191 DBusMessage *message;
193 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
195 message = dbus_message_new_signal (DBUS_PATH_DBUS,
205 if (!dbus_message_set_destination (message, bus_connection_get_name (connection)) ||
206 !dbus_message_append_args (message,
207 DBUS_TYPE_STRING, &service_name,
210 dbus_message_unref (message);
215 if (!bus_transaction_send_from_driver (transaction, connection, message))
217 dbus_message_unref (message);
223 dbus_message_unref (message);
229 create_unique_client_name (BusRegistry *registry,
232 /* We never want to use the same unique client name twice, because
233 * we want to guarantee that if you send a message to a given unique
234 * name, you always get the same application. So we use two numbers
235 * for INT_MAX * INT_MAX combinations, should be pretty safe against
238 /* FIXME these should be in BusRegistry rather than static vars */
239 static int next_major_number = 0;
240 static int next_minor_number = 0;
243 len = _dbus_string_get_length (str);
247 /* start out with 1-0, go to 1-1, 1-2, 1-3,
248 * up to 1-MAXINT, then 2-0, 2-1, etc.
250 if (next_minor_number <= 0)
252 next_major_number += 1;
253 next_minor_number = 0;
254 if (next_major_number <= 0)
255 _dbus_assert_not_reached ("INT_MAX * INT_MAX clients were added");
258 _dbus_assert (next_major_number > 0);
259 _dbus_assert (next_minor_number >= 0);
261 /* appname:MAJOR-MINOR */
263 if (!_dbus_string_append (str, ":"))
266 if (!_dbus_string_append_int (str, next_major_number))
269 if (!_dbus_string_append (str, "."))
272 if (!_dbus_string_append_int (str, next_minor_number))
275 next_minor_number += 1;
277 /* Check if a client with the name exists */
278 if (bus_registry_lookup (registry, str) == NULL)
281 /* drop the number again, try the next one. */
282 _dbus_string_set_length (str, len);
289 bus_driver_handle_hello (DBusConnection *connection,
290 BusTransaction *transaction,
291 DBusMessage *message,
294 DBusString unique_name;
297 BusRegistry *registry;
298 BusConnections *connections;
300 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
302 if (bus_connection_is_active (connection))
304 /* We already handled an Hello message for this connection. */
305 dbus_set_error (error, DBUS_ERROR_FAILED,
306 "Already handled an Hello message");
310 /* Note that when these limits are exceeded we don't disconnect the
311 * connection; we just sort of leave it hanging there until it times
312 * out or disconnects itself or is dropped due to the max number of
313 * incomplete connections. It's even OK if the connection wants to
314 * retry the hello message, we support that.
316 connections = bus_connection_get_connections (connection);
317 if (!bus_connections_check_limits (connections, connection,
320 _DBUS_ASSERT_ERROR_IS_SET (error);
324 if (!_dbus_string_init (&unique_name))
332 registry = bus_connection_get_registry (connection);
334 if (!create_unique_client_name (registry, &unique_name))
340 if (!bus_connection_complete (connection, &unique_name, error))
342 _DBUS_ASSERT_ERROR_IS_SET (error);
346 if (!dbus_message_set_sender (message,
347 bus_connection_get_name (connection)))
353 if (!bus_driver_send_welcome_message (connection, message, transaction, error))
356 /* Create the service */
357 service = bus_registry_ensure (registry,
358 &unique_name, connection, 0, transaction, error);
362 _dbus_assert (bus_connection_is_active (connection));
366 _dbus_string_free (&unique_name);
371 bus_driver_send_welcome_message (DBusConnection *connection,
372 DBusMessage *hello_message,
373 BusTransaction *transaction,
376 DBusMessage *welcome;
379 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
381 name = bus_connection_get_name (connection);
382 _dbus_assert (name != NULL);
384 welcome = dbus_message_new_method_return (hello_message);
391 if (!dbus_message_append_args (welcome,
392 DBUS_TYPE_STRING, &name,
395 dbus_message_unref (welcome);
400 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
402 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
404 dbus_message_unref (welcome);
410 dbus_message_unref (welcome);
416 bus_driver_handle_list_services (DBusConnection *connection,
417 BusTransaction *transaction,
418 DBusMessage *message,
424 BusRegistry *registry;
426 DBusMessageIter iter;
429 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
431 registry = bus_connection_get_registry (connection);
433 reply = dbus_message_new_method_return (message);
440 if (!bus_registry_list_services (registry, &services, &len))
442 dbus_message_unref (reply);
447 dbus_message_iter_init_append (reply, &iter);
449 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
450 DBUS_TYPE_STRING_AS_STRING,
453 dbus_free_string_array (services);
454 dbus_message_unref (reply);
460 /* Include the bus driver in the list */
461 const char *v_STRING = DBUS_SERVICE_DBUS;
462 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
465 dbus_free_string_array (services);
466 dbus_message_unref (reply);
475 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
478 dbus_free_string_array (services);
479 dbus_message_unref (reply);
486 dbus_free_string_array (services);
488 if (!dbus_message_iter_close_container (&iter, &sub))
490 dbus_message_unref (reply);
495 if (!bus_transaction_send_from_driver (transaction, connection, reply))
497 dbus_message_unref (reply);
503 dbus_message_unref (reply);
509 bus_driver_handle_list_activatable_services (DBusConnection *connection,
510 BusTransaction *transaction,
511 DBusMessage *message,
517 BusActivation *activation;
519 DBusMessageIter iter;
522 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
524 activation = bus_connection_get_activation (connection);
526 reply = dbus_message_new_method_return (message);
533 if (!bus_activation_list_services (activation, &services, &len))
535 dbus_message_unref (reply);
540 dbus_message_iter_init_append (reply, &iter);
542 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
543 DBUS_TYPE_STRING_AS_STRING,
546 dbus_free_string_array (services);
547 dbus_message_unref (reply);
553 /* Include the bus driver in the list */
554 const char *v_STRING = DBUS_SERVICE_DBUS;
555 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
558 dbus_free_string_array (services);
559 dbus_message_unref (reply);
568 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
571 dbus_free_string_array (services);
572 dbus_message_unref (reply);
579 dbus_free_string_array (services);
581 if (!dbus_message_iter_close_container (&iter, &sub))
583 dbus_message_unref (reply);
588 if (!bus_transaction_send_from_driver (transaction, connection, reply))
590 dbus_message_unref (reply);
596 dbus_message_unref (reply);
602 bus_driver_handle_acquire_service (DBusConnection *connection,
603 BusTransaction *transaction,
604 DBusMessage *message,
608 DBusString service_name;
610 dbus_uint32_t service_reply;
613 BusRegistry *registry;
615 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
617 registry = bus_connection_get_registry (connection);
619 if (!dbus_message_get_args (message, error,
620 DBUS_TYPE_STRING, &name,
621 DBUS_TYPE_UINT32, &flags,
625 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
630 _dbus_string_init_const (&service_name, name);
632 if (!bus_registry_acquire_service (registry, connection,
633 &service_name, flags,
634 &service_reply, transaction,
638 reply = dbus_message_new_method_return (message);
645 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
651 if (!bus_transaction_send_from_driver (transaction, connection, reply))
661 dbus_message_unref (reply);
666 bus_driver_handle_release_service (DBusConnection *connection,
667 BusTransaction *transaction,
668 DBusMessage *message,
672 DBusString service_name;
674 dbus_uint32_t service_reply;
676 BusRegistry *registry;
678 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
680 registry = bus_connection_get_registry (connection);
682 if (!dbus_message_get_args (message, error,
683 DBUS_TYPE_STRING, &name,
687 _dbus_verbose ("Trying to release name %s\n", name);
692 _dbus_string_init_const (&service_name, name);
694 if (!bus_registry_release_service (registry, connection,
695 &service_name, &service_reply,
699 reply = dbus_message_new_method_return (message);
706 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
712 if (!bus_transaction_send_from_driver (transaction, connection, reply))
722 dbus_message_unref (reply);
727 bus_driver_handle_service_exists (DBusConnection *connection,
728 BusTransaction *transaction,
729 DBusMessage *message,
733 DBusString service_name;
735 dbus_bool_t service_exists;
738 BusRegistry *registry;
740 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
742 registry = bus_connection_get_registry (connection);
744 if (!dbus_message_get_args (message, error,
745 DBUS_TYPE_STRING, &name,
751 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
753 service_exists = TRUE;
757 _dbus_string_init_const (&service_name, name);
758 service = bus_registry_lookup (registry, &service_name);
759 service_exists = service != NULL;
762 reply = dbus_message_new_method_return (message);
769 if (!dbus_message_append_args (reply,
770 DBUS_TYPE_BOOLEAN, &service_exists,
777 if (!bus_transaction_send_from_driver (transaction, connection, reply))
787 dbus_message_unref (reply);
793 bus_driver_handle_activate_service (DBusConnection *connection,
794 BusTransaction *transaction,
795 DBusMessage *message,
801 BusActivation *activation;
803 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
805 activation = bus_connection_get_activation (connection);
807 if (!dbus_message_get_args (message, error,
808 DBUS_TYPE_STRING, &name,
809 DBUS_TYPE_UINT32, &flags,
812 _DBUS_ASSERT_ERROR_IS_SET (error);
813 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
819 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
820 message, name, error))
822 _DBUS_ASSERT_ERROR_IS_SET (error);
823 _dbus_verbose ("bus_activation_activate_service() failed\n");
834 send_ack_reply (DBusConnection *connection,
835 BusTransaction *transaction,
836 DBusMessage *message,
841 if (dbus_message_get_no_reply (message))
844 reply = dbus_message_new_method_return (message);
851 if (!bus_transaction_send_from_driver (transaction, connection, reply))
854 dbus_message_unref (reply);
858 dbus_message_unref (reply);
864 bus_driver_handle_update_activation_environment (DBusConnection *connection,
865 BusTransaction *transaction,
866 DBusMessage *message,
870 BusActivation *activation;
871 DBusMessageIter iter;
872 DBusMessageIter dict_iter;
873 DBusMessageIter dict_entry_iter;
876 DBusList *keys, *key_link;
877 DBusList *values, *value_link;
879 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
881 activation = bus_connection_get_activation (connection);
883 dbus_message_iter_init (message, &iter);
885 /* The message signature has already been checked for us,
886 * so let's just assert it's right.
888 _dbus_assert (dbus_message_iter_get_arg_type (&iter) == DBUS_TYPE_ARRAY);
890 dbus_message_iter_recurse (&iter, &dict_iter);
894 /* Then loop through the sent dictionary, add the location of
895 * the environment keys and values to lists. The result will
896 * be in reverse order, so we don't have to constantly search
897 * for the end of the list in a loop.
901 while ((array_type = dbus_message_iter_get_arg_type (&dict_iter)) == DBUS_TYPE_DICT_ENTRY)
903 dbus_message_iter_recurse (&dict_iter, &dict_entry_iter);
905 while ((key_type = dbus_message_iter_get_arg_type (&dict_entry_iter)) == DBUS_TYPE_STRING)
911 dbus_message_iter_get_basic (&dict_entry_iter, &key);
912 dbus_message_iter_next (&dict_entry_iter);
914 value_type = dbus_message_iter_get_arg_type (&dict_entry_iter);
916 if (value_type != DBUS_TYPE_STRING)
919 dbus_message_iter_get_basic (&dict_entry_iter, &value);
921 if (!_dbus_list_append (&keys, key))
927 if (!_dbus_list_append (&values, value))
933 dbus_message_iter_next (&dict_entry_iter);
936 if (key_type != DBUS_TYPE_INVALID)
939 dbus_message_iter_next (&dict_iter);
942 if (array_type != DBUS_TYPE_INVALID)
945 _dbus_assert (_dbus_list_get_length (&keys) == _dbus_list_get_length (&values));
949 while (key_link != NULL)
954 key = key_link->data;
955 value = value_link->data;
957 if (!bus_activation_set_environment_variable (activation,
960 _DBUS_ASSERT_ERROR_IS_SET (error);
961 _dbus_verbose ("bus_activation_set_environment_variable() failed\n");
964 key_link = _dbus_list_get_next_link (&keys, key_link);
965 value_link = _dbus_list_get_next_link (&values, value_link);
968 /* FIXME: We can fail early having set only some of the environment variables,
969 * (because of OOM failure). It's sort of hard to fix and it doesn't really
970 * matter, so we're punting for now.
972 if (key_link != NULL)
975 if (!send_ack_reply (connection, transaction,
982 _dbus_list_clear (&keys);
983 _dbus_list_clear (&values);
988 bus_driver_handle_add_match (DBusConnection *connection,
989 BusTransaction *transaction,
990 DBusMessage *message,
996 BusMatchmaker *matchmaker;
998 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1003 if (bus_connection_get_n_match_rules (connection) >=
1004 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
1006 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
1007 "Connection \"%s\" is not allowed to add more match rules "
1008 "(increase limits in configuration file if required)",
1009 bus_connection_is_active (connection) ?
1010 bus_connection_get_name (connection) :
1015 if (!dbus_message_get_args (message, error,
1016 DBUS_TYPE_STRING, &text,
1019 _dbus_verbose ("No memory to get arguments to AddMatch\n");
1023 _dbus_string_init_const (&str, text);
1025 rule = bus_match_rule_parse (connection, &str, error);
1029 matchmaker = bus_connection_get_matchmaker (connection);
1031 if (!bus_matchmaker_add_rule (matchmaker, rule))
1033 BUS_SET_OOM (error);
1037 if (!send_ack_reply (connection, transaction,
1040 bus_matchmaker_remove_rule (matchmaker, rule);
1044 bus_match_rule_unref (rule);
1049 _DBUS_ASSERT_ERROR_IS_SET (error);
1051 bus_match_rule_unref (rule);
1056 bus_driver_handle_remove_match (DBusConnection *connection,
1057 BusTransaction *transaction,
1058 DBusMessage *message,
1064 BusMatchmaker *matchmaker;
1066 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1071 if (!dbus_message_get_args (message, error,
1072 DBUS_TYPE_STRING, &text,
1075 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
1079 _dbus_string_init_const (&str, text);
1081 rule = bus_match_rule_parse (connection, &str, error);
1085 /* Send the ack before we remove the rule, since the ack is undone
1086 * on transaction cancel, but rule removal isn't.
1088 if (!send_ack_reply (connection, transaction,
1092 matchmaker = bus_connection_get_matchmaker (connection);
1094 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
1097 bus_match_rule_unref (rule);
1102 _DBUS_ASSERT_ERROR_IS_SET (error);
1104 bus_match_rule_unref (rule);
1109 bus_driver_handle_get_service_owner (DBusConnection *connection,
1110 BusTransaction *transaction,
1111 DBusMessage *message,
1115 const char *base_name;
1117 BusRegistry *registry;
1118 BusService *service;
1121 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1123 registry = bus_connection_get_registry (connection);
1128 if (! dbus_message_get_args (message, error,
1129 DBUS_TYPE_STRING, &text,
1133 _dbus_string_init_const (&str, text);
1134 service = bus_registry_lookup (registry, &str);
1135 if (service == NULL &&
1136 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1138 /* ORG_FREEDESKTOP_DBUS owns itself */
1139 base_name = DBUS_SERVICE_DBUS;
1141 else if (service == NULL)
1143 dbus_set_error (error,
1144 DBUS_ERROR_NAME_HAS_NO_OWNER,
1145 "Could not get owner of name '%s': no such name", text);
1150 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
1151 if (base_name == NULL)
1153 /* FIXME - how is this error possible? */
1154 dbus_set_error (error,
1156 "Could not determine unique name for '%s'", text);
1159 _dbus_assert (*base_name == ':');
1162 _dbus_assert (base_name != NULL);
1164 reply = dbus_message_new_method_return (message);
1168 if (! dbus_message_append_args (reply,
1169 DBUS_TYPE_STRING, &base_name,
1173 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1176 dbus_message_unref (reply);
1181 BUS_SET_OOM (error);
1184 _DBUS_ASSERT_ERROR_IS_SET (error);
1186 dbus_message_unref (reply);
1191 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1192 BusTransaction *transaction,
1193 DBusMessage *message,
1197 DBusList *base_names;
1200 BusRegistry *registry;
1201 BusService *service;
1203 DBusMessageIter iter, array_iter;
1204 char *dbus_service_name = DBUS_SERVICE_DBUS;
1206 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1208 registry = bus_connection_get_registry (connection);
1214 if (! dbus_message_get_args (message, error,
1215 DBUS_TYPE_STRING, &text,
1219 _dbus_string_init_const (&str, text);
1220 service = bus_registry_lookup (registry, &str);
1221 if (service == NULL &&
1222 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1224 /* ORG_FREEDESKTOP_DBUS owns itself */
1225 if (! _dbus_list_append (&base_names, dbus_service_name))
1228 else if (service == NULL)
1230 dbus_set_error (error,
1231 DBUS_ERROR_NAME_HAS_NO_OWNER,
1232 "Could not get owners of name '%s': no such name", text);
1237 if (!bus_service_list_queued_owners (service,
1243 _dbus_assert (base_names != NULL);
1245 reply = dbus_message_new_method_return (message);
1249 dbus_message_iter_init_append (reply, &iter);
1250 if (!dbus_message_iter_open_container (&iter,
1252 DBUS_TYPE_STRING_AS_STRING,
1256 link = _dbus_list_get_first_link (&base_names);
1257 while (link != NULL)
1261 _dbus_assert (link->data != NULL);
1262 uname = (char *)link->data;
1264 if (!dbus_message_iter_append_basic (&array_iter,
1269 link = _dbus_list_get_next_link (&base_names, link);
1272 if (! dbus_message_iter_close_container (&iter, &array_iter))
1276 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1279 dbus_message_unref (reply);
1284 BUS_SET_OOM (error);
1287 _DBUS_ASSERT_ERROR_IS_SET (error);
1289 dbus_message_unref (reply);
1292 _dbus_list_clear (&base_names);
1298 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1299 BusTransaction *transaction,
1300 DBusMessage *message,
1303 DBusConnection *conn;
1306 dbus_uint32_t uid32;
1307 const char *service;
1309 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1313 conn = bus_driver_get_conn_helper (connection, message, "UID", &service,
1319 reply = dbus_message_new_method_return (message);
1323 if (!dbus_connection_get_unix_user (conn, &uid))
1325 dbus_set_error (error,
1327 "Could not determine UID for '%s'", service);
1332 if (! dbus_message_append_args (reply,
1333 DBUS_TYPE_UINT32, &uid32,
1337 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1340 dbus_message_unref (reply);
1345 BUS_SET_OOM (error);
1348 _DBUS_ASSERT_ERROR_IS_SET (error);
1350 dbus_message_unref (reply);
1355 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1356 BusTransaction *transaction,
1357 DBusMessage *message,
1360 DBusConnection *conn;
1363 dbus_uint32_t pid32;
1364 const char *service;
1366 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1370 conn = bus_driver_get_conn_helper (connection, message, "PID", &service,
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 DBusConnection *conn;
1420 dbus_uint32_t data_size;
1421 const char *service;
1423 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1427 conn = bus_driver_get_conn_helper (connection, message,
1428 "audit session data", &service, error);
1433 reply = dbus_message_new_method_return (message);
1437 if (!dbus_connection_get_adt_audit_session_data (conn, &data, &data_size) || data == NULL)
1439 dbus_set_error (error,
1440 DBUS_ERROR_ADT_AUDIT_DATA_UNKNOWN,
1441 "Could not determine audit session data for '%s'", service);
1445 if (! dbus_message_append_args (reply,
1446 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE, &data, data_size,
1450 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1453 dbus_message_unref (reply);
1458 BUS_SET_OOM (error);
1461 _DBUS_ASSERT_ERROR_IS_SET (error);
1463 dbus_message_unref (reply);
1468 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1469 BusTransaction *transaction,
1470 DBusMessage *message,
1473 DBusConnection *conn;
1475 BusSELinuxID *context;
1476 const char *service;
1478 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1482 conn = bus_driver_get_conn_helper (connection, message, "security context",
1488 reply = dbus_message_new_method_return (message);
1492 context = bus_connection_get_selinux_id (conn);
1495 dbus_set_error (error,
1496 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1497 "Could not determine security context for '%s'", service);
1501 if (! bus_selinux_append_context (reply, context, error))
1504 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1507 dbus_message_unref (reply);
1512 BUS_SET_OOM (error);
1515 _DBUS_ASSERT_ERROR_IS_SET (error);
1517 dbus_message_unref (reply);
1522 bus_driver_handle_get_connection_credentials (DBusConnection *connection,
1523 BusTransaction *transaction,
1524 DBusMessage *message,
1527 DBusConnection *conn;
1529 DBusMessageIter reply_iter;
1530 DBusMessageIter array_iter;
1531 unsigned long ulong_val;
1532 const char *service;
1534 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1538 conn = bus_driver_get_conn_helper (connection, message, "credentials",
1544 reply = _dbus_asv_new_method_return (message, &reply_iter, &array_iter);
1548 /* we can't represent > 32-bit pids; if your system needs them, please
1549 * add ProcessID64 to the spec or something */
1550 if (dbus_connection_get_unix_process_id (conn, &ulong_val) &&
1551 ulong_val <= _DBUS_UINT32_MAX)
1553 if (!_dbus_asv_add_uint32 (&array_iter, "ProcessID", ulong_val))
1557 /* we can't represent > 32-bit uids; if your system needs them, please
1558 * add UnixUserID64 to the spec or something */
1559 if (dbus_connection_get_unix_user (conn, &ulong_val) &&
1560 ulong_val <= _DBUS_UINT32_MAX)
1562 if (!_dbus_asv_add_uint32 (&array_iter, "UnixUserID", ulong_val))
1566 if (!_dbus_asv_close (&reply_iter, &array_iter))
1569 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1571 /* this time we don't want to close the iterator again, so just
1572 * get rid of the message */
1573 dbus_message_unref (reply);
1581 BUS_SET_OOM (error);
1584 _DBUS_ASSERT_ERROR_IS_SET (error);
1588 _dbus_asv_abandon (&reply_iter, &array_iter);
1589 dbus_message_unref (reply);
1596 bus_driver_handle_reload_config (DBusConnection *connection,
1597 BusTransaction *transaction,
1598 DBusMessage *message,
1601 BusContext *context;
1604 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1608 context = bus_connection_get_context (connection);
1609 if (!bus_context_reload_config (context, error))
1612 reply = dbus_message_new_method_return (message);
1616 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1619 dbus_message_unref (reply);
1623 BUS_SET_OOM (error);
1626 _DBUS_ASSERT_ERROR_IS_SET (error);
1628 dbus_message_unref (reply);
1633 bus_driver_handle_get_id (DBusConnection *connection,
1634 BusTransaction *transaction,
1635 DBusMessage *message,
1638 BusContext *context;
1641 const char *v_STRING;
1643 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1645 if (!_dbus_string_init (&uuid))
1647 BUS_SET_OOM (error);
1653 context = bus_connection_get_context (connection);
1654 if (!bus_context_get_id (context, &uuid))
1657 reply = dbus_message_new_method_return (message);
1661 v_STRING = _dbus_string_get_const_data (&uuid);
1662 if (!dbus_message_append_args (reply,
1663 DBUS_TYPE_STRING, &v_STRING,
1667 _dbus_assert (dbus_message_has_signature (reply, "s"));
1669 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1672 _dbus_string_free (&uuid);
1673 dbus_message_unref (reply);
1677 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1679 BUS_SET_OOM (error);
1682 dbus_message_unref (reply);
1683 _dbus_string_free (&uuid);
1690 const char *in_args;
1691 const char *out_args;
1692 dbus_bool_t (* handler) (DBusConnection *connection,
1693 BusTransaction *transaction,
1694 DBusMessage *message,
1698 /* For speed it might be useful to sort this in order of
1699 * frequency of use (but doesn't matter with only a few items
1702 static const MessageHandler dbus_message_handlers[] = {
1705 DBUS_TYPE_STRING_AS_STRING,
1706 bus_driver_handle_hello },
1708 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1709 DBUS_TYPE_UINT32_AS_STRING,
1710 bus_driver_handle_acquire_service },
1712 DBUS_TYPE_STRING_AS_STRING,
1713 DBUS_TYPE_UINT32_AS_STRING,
1714 bus_driver_handle_release_service },
1715 { "StartServiceByName",
1716 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1717 DBUS_TYPE_UINT32_AS_STRING,
1718 bus_driver_handle_activate_service },
1719 { "UpdateActivationEnvironment",
1720 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,
1722 bus_driver_handle_update_activation_environment },
1724 DBUS_TYPE_STRING_AS_STRING,
1725 DBUS_TYPE_BOOLEAN_AS_STRING,
1726 bus_driver_handle_service_exists },
1729 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1730 bus_driver_handle_list_services },
1731 { "ListActivatableNames",
1733 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1734 bus_driver_handle_list_activatable_services },
1736 DBUS_TYPE_STRING_AS_STRING,
1738 bus_driver_handle_add_match },
1740 DBUS_TYPE_STRING_AS_STRING,
1742 bus_driver_handle_remove_match },
1744 DBUS_TYPE_STRING_AS_STRING,
1745 DBUS_TYPE_STRING_AS_STRING,
1746 bus_driver_handle_get_service_owner },
1747 { "ListQueuedOwners",
1748 DBUS_TYPE_STRING_AS_STRING,
1749 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1750 bus_driver_handle_list_queued_owners },
1751 { "GetConnectionUnixUser",
1752 DBUS_TYPE_STRING_AS_STRING,
1753 DBUS_TYPE_UINT32_AS_STRING,
1754 bus_driver_handle_get_connection_unix_user },
1755 { "GetConnectionUnixProcessID",
1756 DBUS_TYPE_STRING_AS_STRING,
1757 DBUS_TYPE_UINT32_AS_STRING,
1758 bus_driver_handle_get_connection_unix_process_id },
1759 { "GetAdtAuditSessionData",
1760 DBUS_TYPE_STRING_AS_STRING,
1761 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1762 bus_driver_handle_get_adt_audit_session_data },
1763 { "GetConnectionSELinuxSecurityContext",
1764 DBUS_TYPE_STRING_AS_STRING,
1765 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1766 bus_driver_handle_get_connection_selinux_security_context },
1770 bus_driver_handle_reload_config },
1773 DBUS_TYPE_STRING_AS_STRING,
1774 bus_driver_handle_get_id },
1775 { "GetConnectionCredentials", "s", "a{sv}",
1776 bus_driver_handle_get_connection_credentials },
1777 { NULL, NULL, NULL, NULL }
1780 static dbus_bool_t bus_driver_handle_introspect (DBusConnection *,
1781 BusTransaction *, DBusMessage *, DBusError *);
1783 static const MessageHandler introspectable_message_handlers[] = {
1784 { "Introspect", "", DBUS_TYPE_STRING_AS_STRING, bus_driver_handle_introspect },
1785 { NULL, NULL, NULL, NULL }
1788 #ifdef DBUS_ENABLE_STATS
1789 static const MessageHandler stats_message_handlers[] = {
1790 { "GetStats", "", "a{sv}", bus_stats_handle_get_stats },
1791 { "GetConnectionStats", "s", "a{sv}", bus_stats_handle_get_connection_stats },
1792 { NULL, NULL, NULL, NULL }
1798 const MessageHandler *message_handlers;
1799 const char *extra_introspection;
1802 /* These should ideally be sorted by frequency of use, although it
1803 * probably doesn't matter with this few items */
1804 static InterfaceHandler interface_handlers[] = {
1805 { DBUS_INTERFACE_DBUS, dbus_message_handlers,
1806 " <signal name=\"NameOwnerChanged\">\n"
1807 " <arg type=\"s\"/>\n"
1808 " <arg type=\"s\"/>\n"
1809 " <arg type=\"s\"/>\n"
1811 " <signal name=\"NameLost\">\n"
1812 " <arg type=\"s\"/>\n"
1814 " <signal name=\"NameAcquired\">\n"
1815 " <arg type=\"s\"/>\n"
1817 { DBUS_INTERFACE_INTROSPECTABLE, introspectable_message_handlers, NULL },
1818 #ifdef DBUS_ENABLE_STATS
1819 { BUS_INTERFACE_STATS, stats_message_handlers, NULL },
1821 { NULL, NULL, NULL }
1825 write_args_for_direction (DBusString *xml,
1826 const char *signature,
1829 DBusTypeReader typereader;
1833 _dbus_string_init_const (&sigstr, signature);
1834 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1836 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1838 const DBusString *subsig;
1841 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1842 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1845 if (!_dbus_string_append_len (xml,
1846 _dbus_string_get_const_data (subsig) + start,
1849 if (!_dbus_string_append (xml, "\"/>\n"))
1852 _dbus_type_reader_next (&typereader);
1860 bus_driver_generate_introspect_string (DBusString *xml)
1862 const InterfaceHandler *ih;
1863 const MessageHandler *mh;
1865 if (!_dbus_string_append (xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1867 if (!_dbus_string_append (xml, "<node>\n"))
1870 for (ih = interface_handlers; ih->name != NULL; ih++)
1872 if (!_dbus_string_append_printf (xml, " <interface name=\"%s\">\n",
1876 for (mh = ih->message_handlers; mh->name != NULL; mh++)
1878 if (!_dbus_string_append_printf (xml, " <method name=\"%s\">\n",
1882 if (!write_args_for_direction (xml, mh->in_args, TRUE))
1885 if (!write_args_for_direction (xml, mh->out_args, FALSE))
1888 if (!_dbus_string_append (xml, " </method>\n"))
1892 if (ih->extra_introspection != NULL &&
1893 !_dbus_string_append (xml, ih->extra_introspection))
1896 if (!_dbus_string_append (xml, " </interface>\n"))
1900 if (!_dbus_string_append (xml, "</node>\n"))
1907 bus_driver_handle_introspect (DBusConnection *connection,
1908 BusTransaction *transaction,
1909 DBusMessage *message,
1914 const char *v_STRING;
1916 _dbus_verbose ("Introspect() on bus driver\n");
1918 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1922 if (! dbus_message_get_args (message, error,
1925 _DBUS_ASSERT_ERROR_IS_SET (error);
1929 if (!_dbus_string_init (&xml))
1931 BUS_SET_OOM (error);
1935 if (!bus_driver_generate_introspect_string (&xml))
1938 v_STRING = _dbus_string_get_const_data (&xml);
1940 reply = dbus_message_new_method_return (message);
1944 if (! dbus_message_append_args (reply,
1945 DBUS_TYPE_STRING, &v_STRING,
1949 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1952 dbus_message_unref (reply);
1953 _dbus_string_free (&xml);
1958 BUS_SET_OOM (error);
1961 dbus_message_unref (reply);
1963 _dbus_string_free (&xml);
1969 bus_driver_handle_message (DBusConnection *connection,
1970 BusTransaction *transaction,
1971 DBusMessage *message,
1974 const char *name, *interface;
1975 const InterfaceHandler *ih;
1976 const MessageHandler *mh;
1977 dbus_bool_t found_interface = FALSE;
1979 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1981 if (dbus_message_is_signal (message, "org.freedesktop.systemd1.Activator", "ActivationFailure"))
1983 BusContext *context;
1985 context = bus_connection_get_context (connection);
1986 return dbus_activation_systemd_failure(bus_context_get_activation(context), message);
1989 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1991 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1992 return TRUE; /* we just ignore this */
1995 /* may be NULL, which means "any interface will do" */
1996 interface = dbus_message_get_interface (message);
1998 _dbus_assert (dbus_message_get_member (message) != NULL);
2000 name = dbus_message_get_member (message);
2002 _dbus_verbose ("Driver got a method call: %s\n", name);
2004 /* security checks should have kept this from getting here */
2005 _dbus_assert (dbus_message_get_sender (message) != NULL ||
2006 strcmp (name, "Hello") == 0);
2008 for (ih = interface_handlers; ih->name != NULL; ih++)
2010 if (interface != NULL && strcmp (interface, ih->name) != 0)
2013 found_interface = TRUE;
2015 for (mh = ih->message_handlers; mh->name != NULL; mh++)
2017 if (strcmp (mh->name, name) != 0)
2020 _dbus_verbose ("Found driver handler for %s\n", name);
2022 if (!dbus_message_has_signature (message, mh->in_args))
2024 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2025 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
2026 name, dbus_message_get_signature (message),
2029 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
2030 "Call to %s has wrong args (%s, expected %s)\n",
2031 name, dbus_message_get_signature (message),
2033 _DBUS_ASSERT_ERROR_IS_SET (error);
2037 if ((* mh->handler) (connection, transaction, message, error))
2039 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
2040 _dbus_verbose ("Driver handler succeeded\n");
2045 _DBUS_ASSERT_ERROR_IS_SET (error);
2046 _dbus_verbose ("Driver handler returned failure\n");
2052 _dbus_verbose ("No driver handler for message \"%s\"\n",
2055 dbus_set_error (error, found_interface ? DBUS_ERROR_UNKNOWN_METHOD : DBUS_ERROR_UNKNOWN_INTERFACE,
2056 "%s does not understand message %s",
2057 DBUS_SERVICE_DBUS, name);
2063 bus_driver_remove_connection (DBusConnection *connection)
2065 /* FIXME 1.0 Does nothing for now, should unregister the connection
2066 * with the bus driver.