1 /* -*- mode: C; c-file-style: "gnu" -*- */
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_add_match (DBusConnection *connection,
815 BusTransaction *transaction,
816 DBusMessage *message,
822 BusMatchmaker *matchmaker;
824 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
829 if (bus_connection_get_n_match_rules (connection) >=
830 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
832 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
833 "Connection \"%s\" is not allowed to add more match rules "
834 "(increase limits in configuration file if required)",
835 bus_connection_is_active (connection) ?
836 bus_connection_get_name (connection) :
841 if (!dbus_message_get_args (message, error,
842 DBUS_TYPE_STRING, &text,
845 _dbus_verbose ("No memory to get arguments to AddMatch\n");
849 _dbus_string_init_const (&str, text);
851 rule = bus_match_rule_parse (connection, &str, error);
855 matchmaker = bus_connection_get_matchmaker (connection);
857 if (!bus_matchmaker_add_rule (matchmaker, rule))
863 if (!send_ack_reply (connection, transaction,
866 bus_matchmaker_remove_rule (matchmaker, rule);
870 bus_match_rule_unref (rule);
875 _DBUS_ASSERT_ERROR_IS_SET (error);
877 bus_match_rule_unref (rule);
882 bus_driver_handle_remove_match (DBusConnection *connection,
883 BusTransaction *transaction,
884 DBusMessage *message,
890 BusMatchmaker *matchmaker;
892 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
897 if (!dbus_message_get_args (message, error,
898 DBUS_TYPE_STRING, &text,
901 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
905 _dbus_string_init_const (&str, text);
907 rule = bus_match_rule_parse (connection, &str, error);
911 /* Send the ack before we remove the rule, since the ack is undone
912 * on transaction cancel, but rule removal isn't.
914 if (!send_ack_reply (connection, transaction,
918 matchmaker = bus_connection_get_matchmaker (connection);
920 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
923 bus_match_rule_unref (rule);
928 _DBUS_ASSERT_ERROR_IS_SET (error);
930 bus_match_rule_unref (rule);
935 bus_driver_handle_get_service_owner (DBusConnection *connection,
936 BusTransaction *transaction,
937 DBusMessage *message,
941 const char *base_name;
943 BusRegistry *registry;
947 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
949 registry = bus_connection_get_registry (connection);
954 if (! dbus_message_get_args (message, error,
955 DBUS_TYPE_STRING, &text,
959 _dbus_string_init_const (&str, text);
960 service = bus_registry_lookup (registry, &str);
961 if (service == NULL &&
962 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
964 /* ORG_FREEDESKTOP_DBUS owns itself */
965 base_name = DBUS_SERVICE_DBUS;
967 else if (service == NULL)
969 dbus_set_error (error,
970 DBUS_ERROR_NAME_HAS_NO_OWNER,
971 "Could not get owner of name '%s': no such name", text);
976 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
977 if (base_name == NULL)
979 /* FIXME - how is this error possible? */
980 dbus_set_error (error,
982 "Could not determine unique name for '%s'", text);
985 _dbus_assert (*base_name == ':');
988 _dbus_assert (base_name != NULL);
990 reply = dbus_message_new_method_return (message);
994 if (! dbus_message_append_args (reply,
995 DBUS_TYPE_STRING, &base_name,
999 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1002 dbus_message_unref (reply);
1007 BUS_SET_OOM (error);
1010 _DBUS_ASSERT_ERROR_IS_SET (error);
1012 dbus_message_unref (reply);
1017 bus_driver_handle_list_queued_owners (DBusConnection *connection,
1018 BusTransaction *transaction,
1019 DBusMessage *message,
1023 DBusList *base_names;
1026 BusRegistry *registry;
1027 BusService *service;
1029 DBusMessageIter iter, array_iter;
1030 char *dbus_service_name = DBUS_SERVICE_DBUS;
1032 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1034 registry = bus_connection_get_registry (connection);
1040 if (! dbus_message_get_args (message, error,
1041 DBUS_TYPE_STRING, &text,
1045 _dbus_string_init_const (&str, text);
1046 service = bus_registry_lookup (registry, &str);
1047 if (service == NULL &&
1048 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
1050 /* ORG_FREEDESKTOP_DBUS owns itself */
1051 if (! _dbus_list_append (&base_names, dbus_service_name))
1054 else if (service == NULL)
1056 dbus_set_error (error,
1057 DBUS_ERROR_NAME_HAS_NO_OWNER,
1058 "Could not get owners of name '%s': no such name", text);
1063 if (!bus_service_list_queued_owners (service,
1069 _dbus_assert (base_names != NULL);
1071 reply = dbus_message_new_method_return (message);
1075 dbus_message_iter_init_append (reply, &iter);
1076 if (!dbus_message_iter_open_container (&iter,
1078 DBUS_TYPE_STRING_AS_STRING,
1082 link = _dbus_list_get_first_link (&base_names);
1083 while (link != NULL)
1087 _dbus_assert (link->data != NULL);
1088 uname = (char *)link->data;
1090 if (!dbus_message_iter_append_basic (&array_iter,
1095 link = _dbus_list_get_next_link (&base_names, link);
1098 if (! dbus_message_iter_close_container (&iter, &array_iter))
1102 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1105 dbus_message_unref (reply);
1110 BUS_SET_OOM (error);
1113 _DBUS_ASSERT_ERROR_IS_SET (error);
1115 dbus_message_unref (reply);
1118 _dbus_list_clear (&base_names);
1124 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1125 BusTransaction *transaction,
1126 DBusMessage *message,
1129 const char *service;
1131 BusRegistry *registry;
1133 DBusConnection *conn;
1136 dbus_uint32_t uid32;
1138 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1140 registry = bus_connection_get_registry (connection);
1145 if (! dbus_message_get_args (message, error,
1146 DBUS_TYPE_STRING, &service,
1150 _dbus_verbose ("asked for UID of connection %s\n", service);
1152 _dbus_string_init_const (&str, service);
1153 serv = bus_registry_lookup (registry, &str);
1156 dbus_set_error (error,
1157 DBUS_ERROR_NAME_HAS_NO_OWNER,
1158 "Could not get UID of name '%s': no such name", service);
1162 conn = bus_service_get_primary_owners_connection (serv);
1164 reply = dbus_message_new_method_return (message);
1168 if (!dbus_connection_get_unix_user (conn, &uid))
1170 dbus_set_error (error,
1172 "Could not determine UID for '%s'", service);
1177 if (! dbus_message_append_args (reply,
1178 DBUS_TYPE_UINT32, &uid32,
1182 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1185 dbus_message_unref (reply);
1190 BUS_SET_OOM (error);
1193 _DBUS_ASSERT_ERROR_IS_SET (error);
1195 dbus_message_unref (reply);
1200 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1201 BusTransaction *transaction,
1202 DBusMessage *message,
1205 const char *service;
1207 BusRegistry *registry;
1209 DBusConnection *conn;
1212 dbus_uint32_t pid32;
1214 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1216 registry = bus_connection_get_registry (connection);
1221 if (! dbus_message_get_args (message, error,
1222 DBUS_TYPE_STRING, &service,
1226 _dbus_verbose ("asked for PID of connection %s\n", service);
1228 _dbus_string_init_const (&str, service);
1229 serv = bus_registry_lookup (registry, &str);
1232 dbus_set_error (error,
1233 DBUS_ERROR_NAME_HAS_NO_OWNER,
1234 "Could not get PID of name '%s': no such name", service);
1238 conn = bus_service_get_primary_owners_connection (serv);
1240 reply = dbus_message_new_method_return (message);
1244 if (!dbus_connection_get_unix_process_id (conn, &pid))
1246 dbus_set_error (error,
1247 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1248 "Could not determine PID for '%s'", service);
1253 if (! dbus_message_append_args (reply,
1254 DBUS_TYPE_UINT32, &pid32,
1258 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1261 dbus_message_unref (reply);
1266 BUS_SET_OOM (error);
1269 _DBUS_ASSERT_ERROR_IS_SET (error);
1271 dbus_message_unref (reply);
1276 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1277 BusTransaction *transaction,
1278 DBusMessage *message,
1281 const char *service;
1283 BusRegistry *registry;
1285 DBusConnection *conn;
1287 BusSELinuxID *context;
1289 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1291 registry = bus_connection_get_registry (connection);
1296 if (! dbus_message_get_args (message, error,
1297 DBUS_TYPE_STRING, &service,
1301 _dbus_verbose ("asked for security context of connection %s\n", service);
1303 _dbus_string_init_const (&str, service);
1304 serv = bus_registry_lookup (registry, &str);
1307 dbus_set_error (error,
1308 DBUS_ERROR_NAME_HAS_NO_OWNER,
1309 "Could not get security context of name '%s': no such name", service);
1313 conn = bus_service_get_primary_owners_connection (serv);
1315 reply = dbus_message_new_method_return (message);
1319 context = bus_connection_get_selinux_id (conn);
1322 dbus_set_error (error,
1323 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1324 "Could not determine security context for '%s'", service);
1328 if (! bus_selinux_append_context (reply, context, error))
1331 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1334 dbus_message_unref (reply);
1339 BUS_SET_OOM (error);
1342 _DBUS_ASSERT_ERROR_IS_SET (error);
1344 dbus_message_unref (reply);
1349 bus_driver_handle_reload_config (DBusConnection *connection,
1350 BusTransaction *transaction,
1351 DBusMessage *message,
1354 BusContext *context;
1357 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1361 context = bus_connection_get_context (connection);
1362 if (!bus_context_reload_config (context, error))
1365 reply = dbus_message_new_method_return (message);
1369 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1372 dbus_message_unref (reply);
1376 BUS_SET_OOM (error);
1379 _DBUS_ASSERT_ERROR_IS_SET (error);
1381 dbus_message_unref (reply);
1385 /* For speed it might be useful to sort this in order of
1386 * frequency of use (but doesn't matter with only a few items
1392 const char *in_args;
1393 const char *out_args;
1394 dbus_bool_t (* handler) (DBusConnection *connection,
1395 BusTransaction *transaction,
1396 DBusMessage *message,
1398 } message_handlers[] = {
1400 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1401 DBUS_TYPE_UINT32_AS_STRING,
1402 bus_driver_handle_acquire_service },
1404 DBUS_TYPE_STRING_AS_STRING,
1405 DBUS_TYPE_UINT32_AS_STRING,
1406 bus_driver_handle_release_service },
1407 { "StartServiceByName",
1408 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1409 DBUS_TYPE_UINT32_AS_STRING,
1410 bus_driver_handle_activate_service },
1413 DBUS_TYPE_STRING_AS_STRING,
1414 bus_driver_handle_hello },
1416 DBUS_TYPE_STRING_AS_STRING,
1417 DBUS_TYPE_BOOLEAN_AS_STRING,
1418 bus_driver_handle_service_exists },
1421 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1422 bus_driver_handle_list_services },
1423 { "ListActivatableNames",
1425 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1426 bus_driver_handle_list_activatable_services },
1428 DBUS_TYPE_STRING_AS_STRING,
1430 bus_driver_handle_add_match },
1432 DBUS_TYPE_STRING_AS_STRING,
1434 bus_driver_handle_remove_match },
1436 DBUS_TYPE_STRING_AS_STRING,
1437 DBUS_TYPE_STRING_AS_STRING,
1438 bus_driver_handle_get_service_owner },
1439 { "ListQueuedOwners",
1440 DBUS_TYPE_STRING_AS_STRING,
1441 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1442 bus_driver_handle_list_queued_owners },
1443 { "GetConnectionUnixUser",
1444 DBUS_TYPE_STRING_AS_STRING,
1445 DBUS_TYPE_UINT32_AS_STRING,
1446 bus_driver_handle_get_connection_unix_user },
1447 { "GetConnectionUnixProcessID",
1448 DBUS_TYPE_STRING_AS_STRING,
1449 DBUS_TYPE_UINT32_AS_STRING,
1450 bus_driver_handle_get_connection_unix_process_id },
1451 { "GetConnectionSELinuxSecurityContext",
1452 DBUS_TYPE_STRING_AS_STRING,
1453 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1454 bus_driver_handle_get_connection_selinux_security_context },
1458 bus_driver_handle_reload_config }
1462 write_args_for_direction (DBusString *xml,
1463 const char *signature,
1466 DBusTypeReader typereader;
1470 _dbus_string_init_const (&sigstr, signature);
1471 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1473 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1475 const DBusString *subsig;
1478 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1479 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1482 if (!_dbus_string_append_len (xml,
1483 _dbus_string_get_const_data (subsig) + start,
1486 if (!_dbus_string_append (xml, "\"/>\n"))
1489 _dbus_type_reader_next (&typereader);
1497 bus_driver_handle_introspect (DBusConnection *connection,
1498 BusTransaction *transaction,
1499 DBusMessage *message,
1504 const char *v_STRING;
1507 _dbus_verbose ("Introspect() on bus driver\n");
1509 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1513 if (! dbus_message_get_args (message, error,
1516 _DBUS_ASSERT_ERROR_IS_SET (error);
1520 if (!_dbus_string_init (&xml))
1522 BUS_SET_OOM (error);
1526 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1528 if (!_dbus_string_append (&xml, "<node>\n"))
1530 if (!_dbus_string_append_printf (&xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE))
1532 if (!_dbus_string_append (&xml, " <method name=\"Introspect\">\n"))
1534 if (!_dbus_string_append_printf (&xml, " <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING))
1536 if (!_dbus_string_append (&xml, " </method>\n"))
1538 if (!_dbus_string_append (&xml, " </interface>\n"))
1541 if (!_dbus_string_append_printf (&xml, " <interface name=\"%s\">\n",
1542 DBUS_INTERFACE_DBUS))
1546 while (i < _DBUS_N_ELEMENTS (message_handlers))
1549 if (!_dbus_string_append_printf (&xml, " <method name=\"%s\">\n",
1550 message_handlers[i].name))
1553 if (!write_args_for_direction (&xml, message_handlers[i].in_args, TRUE))
1556 if (!write_args_for_direction (&xml, message_handlers[i].out_args, FALSE))
1559 if (!_dbus_string_append (&xml, " </method>\n"))
1565 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameOwnerChanged\">\n"))
1568 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1571 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1574 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1577 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1582 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameLost\">\n"))
1585 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1588 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1593 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameAcquired\">\n"))
1596 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1599 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1604 if (!_dbus_string_append (&xml, " </interface>\n"))
1607 if (!_dbus_string_append (&xml, "</node>\n"))
1610 reply = dbus_message_new_method_return (message);
1614 v_STRING = _dbus_string_get_const_data (&xml);
1615 if (! dbus_message_append_args (reply,
1616 DBUS_TYPE_STRING, &v_STRING,
1620 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1623 dbus_message_unref (reply);
1624 _dbus_string_free (&xml);
1629 BUS_SET_OOM (error);
1632 dbus_message_unref (reply);
1634 _dbus_string_free (&xml);
1640 bus_driver_handle_message (DBusConnection *connection,
1641 BusTransaction *transaction,
1642 DBusMessage *message,
1645 const char *name, *sender, *interface;
1648 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1650 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1652 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1653 return TRUE; /* we just ignore this */
1656 if (dbus_message_is_method_call (message,
1657 DBUS_INTERFACE_INTROSPECTABLE,
1659 return bus_driver_handle_introspect (connection, transaction, message, error);
1661 interface = dbus_message_get_interface (message);
1662 if (interface == NULL)
1663 interface = DBUS_INTERFACE_DBUS;
1665 _dbus_assert (dbus_message_get_member (message) != NULL);
1667 name = dbus_message_get_member (message);
1668 sender = dbus_message_get_sender (message);
1670 if (strcmp (interface,
1671 DBUS_INTERFACE_DBUS) != 0)
1673 _dbus_verbose ("Driver got message to unknown interface \"%s\"\n",
1678 _dbus_verbose ("Driver got a method call: %s\n",
1679 dbus_message_get_member (message));
1681 /* security checks should have kept this from getting here */
1682 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1685 while (i < _DBUS_N_ELEMENTS (message_handlers))
1687 if (strcmp (message_handlers[i].name, name) == 0)
1689 _dbus_verbose ("Found driver handler for %s\n", name);
1691 if (!dbus_message_has_signature (message, message_handlers[i].in_args))
1693 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1694 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1695 name, dbus_message_get_signature (message),
1696 message_handlers[i].in_args);
1698 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1699 "Call to %s has wrong args (%s, expected %s)\n",
1700 name, dbus_message_get_signature (message),
1701 message_handlers[i].in_args);
1702 _DBUS_ASSERT_ERROR_IS_SET (error);
1706 if ((* message_handlers[i].handler) (connection, transaction, message, error))
1708 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1709 _dbus_verbose ("Driver handler succeeded\n");
1714 _DBUS_ASSERT_ERROR_IS_SET (error);
1715 _dbus_verbose ("Driver handler returned failure\n");
1724 _dbus_verbose ("No driver handler for message \"%s\"\n",
1727 dbus_set_error (error, DBUS_ERROR_UNKNOWN_METHOD,
1728 "%s does not understand message %s",
1729 DBUS_SERVICE_DBUS, name);
1735 bus_driver_remove_connection (DBusConnection *connection)
1737 /* FIXME 1.0 Does nothing for now, should unregister the connection
1738 * with the bus driver.