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, transaction, error);
315 bus_service_set_prohibit_replacement (service, TRUE);
317 _dbus_assert (bus_connection_is_active (connection));
321 _dbus_string_free (&unique_name);
326 bus_driver_send_welcome_message (DBusConnection *connection,
327 DBusMessage *hello_message,
328 BusTransaction *transaction,
331 DBusMessage *welcome;
334 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
336 name = bus_connection_get_name (connection);
337 _dbus_assert (name != NULL);
339 welcome = dbus_message_new_method_return (hello_message);
346 if (!dbus_message_append_args (welcome,
347 DBUS_TYPE_STRING, &name,
350 dbus_message_unref (welcome);
355 _dbus_assert (dbus_message_has_signature (welcome, DBUS_TYPE_STRING_AS_STRING));
357 if (!bus_transaction_send_from_driver (transaction, connection, welcome))
359 dbus_message_unref (welcome);
365 dbus_message_unref (welcome);
371 bus_driver_handle_list_services (DBusConnection *connection,
372 BusTransaction *transaction,
373 DBusMessage *message,
379 BusRegistry *registry;
381 DBusMessageIter iter;
384 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
386 registry = bus_connection_get_registry (connection);
388 reply = dbus_message_new_method_return (message);
395 if (!bus_registry_list_services (registry, &services, &len))
397 dbus_message_unref (reply);
402 dbus_message_iter_init_append (reply, &iter);
404 if (!dbus_message_iter_open_container (&iter, DBUS_TYPE_ARRAY,
405 DBUS_TYPE_STRING_AS_STRING,
408 dbus_free_string_array (services);
409 dbus_message_unref (reply);
415 /* Include the bus driver in the list */
416 const char *v_STRING = DBUS_SERVICE_DBUS;
417 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
420 dbus_free_string_array (services);
421 dbus_message_unref (reply);
430 if (!dbus_message_iter_append_basic (&sub, DBUS_TYPE_STRING,
433 dbus_free_string_array (services);
434 dbus_message_unref (reply);
441 if (!dbus_message_iter_close_container (&iter, &sub))
443 dbus_free_string_array (services);
444 dbus_message_unref (reply);
449 dbus_free_string_array (services);
451 if (!bus_transaction_send_from_driver (transaction, connection, reply))
453 dbus_message_unref (reply);
459 dbus_message_unref (reply);
465 bus_driver_handle_acquire_service (DBusConnection *connection,
466 BusTransaction *transaction,
467 DBusMessage *message,
471 DBusString service_name;
473 dbus_uint32_t service_reply;
476 BusRegistry *registry;
478 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
480 registry = bus_connection_get_registry (connection);
482 if (!dbus_message_get_args (message, error,
483 DBUS_TYPE_STRING, &name,
484 DBUS_TYPE_UINT32, &flags,
488 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
493 _dbus_string_init_const (&service_name, name);
495 if (!bus_registry_acquire_service (registry, connection,
496 &service_name, flags,
497 &service_reply, transaction,
501 reply = dbus_message_new_method_return (message);
508 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
514 if (!bus_transaction_send_from_driver (transaction, connection, reply))
524 dbus_message_unref (reply);
529 bus_driver_handle_release_service (DBusConnection *connection,
530 BusTransaction *transaction,
531 DBusMessage *message,
535 DBusString service_name;
537 dbus_uint32_t service_reply;
539 BusRegistry *registry;
541 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
543 registry = bus_connection_get_registry (connection);
545 if (!dbus_message_get_args (message, error,
546 DBUS_TYPE_STRING, &name,
550 _dbus_verbose ("Trying to release name %s\n", name);
555 _dbus_string_init_const (&service_name, name);
557 if (!bus_registry_release_service (registry, connection,
558 &service_name, &service_reply,
562 reply = dbus_message_new_method_return (message);
569 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
575 if (!bus_transaction_send_from_driver (transaction, connection, reply))
585 dbus_message_unref (reply);
590 bus_driver_handle_service_exists (DBusConnection *connection,
591 BusTransaction *transaction,
592 DBusMessage *message,
596 DBusString service_name;
598 dbus_bool_t service_exists;
601 BusRegistry *registry;
603 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
605 registry = bus_connection_get_registry (connection);
607 if (!dbus_message_get_args (message, error,
608 DBUS_TYPE_STRING, &name,
614 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
616 service_exists = TRUE;
620 _dbus_string_init_const (&service_name, name);
621 service = bus_registry_lookup (registry, &service_name);
622 service_exists = service != NULL;
625 reply = dbus_message_new_method_return (message);
632 if (!dbus_message_append_args (reply,
633 DBUS_TYPE_BOOLEAN, &service_exists,
640 if (!bus_transaction_send_from_driver (transaction, connection, reply))
650 dbus_message_unref (reply);
656 bus_driver_handle_activate_service (DBusConnection *connection,
657 BusTransaction *transaction,
658 DBusMessage *message,
664 BusActivation *activation;
666 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
668 activation = bus_connection_get_activation (connection);
670 if (!dbus_message_get_args (message, error,
671 DBUS_TYPE_STRING, &name,
672 DBUS_TYPE_UINT32, &flags,
675 _DBUS_ASSERT_ERROR_IS_SET (error);
676 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
682 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
683 message, name, error))
685 _DBUS_ASSERT_ERROR_IS_SET (error);
686 _dbus_verbose ("bus_activation_activate_service() failed\n");
697 send_ack_reply (DBusConnection *connection,
698 BusTransaction *transaction,
699 DBusMessage *message,
704 reply = dbus_message_new_method_return (message);
711 if (!bus_transaction_send_from_driver (transaction, connection, reply))
714 dbus_message_unref (reply);
718 dbus_message_unref (reply);
724 bus_driver_handle_add_match (DBusConnection *connection,
725 BusTransaction *transaction,
726 DBusMessage *message,
732 BusMatchmaker *matchmaker;
734 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
739 if (bus_connection_get_n_match_rules (connection) >=
740 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
742 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
743 "Connection \"%s\" is not allowed to add more match rules "
744 "(increase limits in configuration file if required)",
745 bus_connection_is_active (connection) ?
746 bus_connection_get_name (connection) :
751 if (!dbus_message_get_args (message, error,
752 DBUS_TYPE_STRING, &text,
755 _dbus_verbose ("No memory to get arguments to AddMatch\n");
759 _dbus_string_init_const (&str, text);
761 rule = bus_match_rule_parse (connection, &str, error);
765 matchmaker = bus_connection_get_matchmaker (connection);
767 if (!bus_matchmaker_add_rule (matchmaker, rule))
773 if (!send_ack_reply (connection, transaction,
776 bus_matchmaker_remove_rule (matchmaker, rule);
780 bus_match_rule_unref (rule);
785 _DBUS_ASSERT_ERROR_IS_SET (error);
787 bus_match_rule_unref (rule);
792 bus_driver_handle_remove_match (DBusConnection *connection,
793 BusTransaction *transaction,
794 DBusMessage *message,
800 BusMatchmaker *matchmaker;
802 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
807 if (!dbus_message_get_args (message, error,
808 DBUS_TYPE_STRING, &text,
811 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
815 _dbus_string_init_const (&str, text);
817 rule = bus_match_rule_parse (connection, &str, error);
821 /* Send the ack before we remove the rule, since the ack is undone
822 * on transaction cancel, but rule removal isn't.
824 if (!send_ack_reply (connection, transaction,
828 matchmaker = bus_connection_get_matchmaker (connection);
830 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
833 bus_match_rule_unref (rule);
838 _DBUS_ASSERT_ERROR_IS_SET (error);
840 bus_match_rule_unref (rule);
845 bus_driver_handle_get_service_owner (DBusConnection *connection,
846 BusTransaction *transaction,
847 DBusMessage *message,
851 const char *base_name;
853 BusRegistry *registry;
857 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
859 registry = bus_connection_get_registry (connection);
864 if (! dbus_message_get_args (message, error,
865 DBUS_TYPE_STRING, &text,
869 _dbus_string_init_const (&str, text);
870 service = bus_registry_lookup (registry, &str);
871 if (service == NULL &&
872 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
874 /* ORG_FREEDESKTOP_DBUS owns itself */
875 base_name = DBUS_SERVICE_DBUS;
877 else if (service == NULL)
879 dbus_set_error (error,
880 DBUS_ERROR_NAME_HAS_NO_OWNER,
881 "Could not get owner of name '%s': no such name", text);
886 base_name = bus_connection_get_name (bus_service_get_primary_owner (service));
887 if (base_name == NULL)
889 /* FIXME - how is this error possible? */
890 dbus_set_error (error,
892 "Could not determine unique name for '%s'", text);
895 _dbus_assert (*base_name == ':');
898 _dbus_assert (base_name != NULL);
900 reply = dbus_message_new_method_return (message);
904 if (! dbus_message_append_args (reply,
905 DBUS_TYPE_STRING, &base_name,
909 if (! bus_transaction_send_from_driver (transaction, connection, reply))
912 dbus_message_unref (reply);
920 _DBUS_ASSERT_ERROR_IS_SET (error);
922 dbus_message_unref (reply);
927 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
928 BusTransaction *transaction,
929 DBusMessage *message,
934 BusRegistry *registry;
936 DBusConnection *conn;
941 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
943 registry = bus_connection_get_registry (connection);
948 if (! dbus_message_get_args (message, error,
949 DBUS_TYPE_STRING, &service,
953 _dbus_verbose ("asked for UID of connection %s\n", service);
955 _dbus_string_init_const (&str, service);
956 serv = bus_registry_lookup (registry, &str);
959 dbus_set_error (error,
960 DBUS_ERROR_NAME_HAS_NO_OWNER,
961 "Could not get UID of name '%s': no such name", service);
965 conn = bus_service_get_primary_owner (serv);
967 reply = dbus_message_new_method_return (message);
971 if (!dbus_connection_get_unix_user (conn, &uid))
973 dbus_set_error (error,
975 "Could not determine UID for '%s'", service);
980 if (! dbus_message_append_args (reply,
981 DBUS_TYPE_UINT32, &uid32,
985 if (! bus_transaction_send_from_driver (transaction, connection, reply))
988 dbus_message_unref (reply);
996 _DBUS_ASSERT_ERROR_IS_SET (error);
998 dbus_message_unref (reply);
1003 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1004 BusTransaction *transaction,
1005 DBusMessage *message,
1008 const char *service;
1010 BusRegistry *registry;
1012 DBusConnection *conn;
1015 dbus_uint32_t pid32;
1017 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1019 registry = bus_connection_get_registry (connection);
1024 if (! dbus_message_get_args (message, error,
1025 DBUS_TYPE_STRING, &service,
1029 _dbus_verbose ("asked for PID of connection %s\n", service);
1031 _dbus_string_init_const (&str, service);
1032 serv = bus_registry_lookup (registry, &str);
1035 dbus_set_error (error,
1036 DBUS_ERROR_NAME_HAS_NO_OWNER,
1037 "Could not get PID of name '%s': no such name", service);
1041 conn = bus_service_get_primary_owner (serv);
1043 reply = dbus_message_new_method_return (message);
1047 if (!dbus_connection_get_unix_process_id (conn, &pid))
1049 dbus_set_error (error,
1050 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1051 "Could not determine PID for '%s'", service);
1056 if (! dbus_message_append_args (reply,
1057 DBUS_TYPE_UINT32, &pid32,
1061 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1064 dbus_message_unref (reply);
1069 BUS_SET_OOM (error);
1072 _DBUS_ASSERT_ERROR_IS_SET (error);
1074 dbus_message_unref (reply);
1079 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1080 BusTransaction *transaction,
1081 DBusMessage *message,
1084 const char *service;
1086 BusRegistry *registry;
1088 DBusConnection *conn;
1090 BusSELinuxID *context;
1092 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1094 registry = bus_connection_get_registry (connection);
1099 if (! dbus_message_get_args (message, error,
1100 DBUS_TYPE_STRING, &service,
1104 _dbus_verbose ("asked for security context of connection %s\n", service);
1106 _dbus_string_init_const (&str, service);
1107 serv = bus_registry_lookup (registry, &str);
1110 dbus_set_error (error,
1111 DBUS_ERROR_NAME_HAS_NO_OWNER,
1112 "Could not get security context of name '%s': no such name", service);
1116 conn = bus_service_get_primary_owner (serv);
1118 reply = dbus_message_new_method_return (message);
1122 context = bus_connection_get_selinux_id (conn);
1125 dbus_set_error (error,
1126 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1127 "Could not determine security context for '%s'", service);
1131 if (! bus_selinux_append_context (reply, context, error))
1134 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1137 dbus_message_unref (reply);
1142 BUS_SET_OOM (error);
1145 _DBUS_ASSERT_ERROR_IS_SET (error);
1147 dbus_message_unref (reply);
1152 bus_driver_handle_reload_config (DBusConnection *connection,
1153 BusTransaction *transaction,
1154 DBusMessage *message,
1157 BusContext *context;
1160 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1164 context = bus_connection_get_context (connection);
1165 if (!bus_context_reload_config (context, error))
1168 reply = dbus_message_new_method_return (message);
1172 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1175 dbus_message_unref (reply);
1179 BUS_SET_OOM (error);
1182 _DBUS_ASSERT_ERROR_IS_SET (error);
1184 dbus_message_unref (reply);
1188 /* For speed it might be useful to sort this in order of
1189 * frequency of use (but doesn't matter with only a few items
1195 const char *in_args;
1196 const char *out_args;
1197 dbus_bool_t (* handler) (DBusConnection *connection,
1198 BusTransaction *transaction,
1199 DBusMessage *message,
1201 } message_handlers[] = {
1203 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1204 DBUS_TYPE_UINT32_AS_STRING,
1205 bus_driver_handle_acquire_service },
1207 DBUS_TYPE_STRING_AS_STRING,
1208 DBUS_TYPE_UINT32_AS_STRING,
1209 bus_driver_handle_release_service },
1210 { "StartServiceByName",
1211 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1212 DBUS_TYPE_UINT32_AS_STRING,
1213 bus_driver_handle_activate_service },
1216 DBUS_TYPE_STRING_AS_STRING,
1217 bus_driver_handle_hello },
1219 DBUS_TYPE_STRING_AS_STRING,
1220 DBUS_TYPE_BOOLEAN_AS_STRING,
1221 bus_driver_handle_service_exists },
1224 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1225 bus_driver_handle_list_services },
1227 DBUS_TYPE_STRING_AS_STRING,
1229 bus_driver_handle_add_match },
1231 DBUS_TYPE_STRING_AS_STRING,
1233 bus_driver_handle_remove_match },
1235 DBUS_TYPE_STRING_AS_STRING,
1236 DBUS_TYPE_STRING_AS_STRING,
1237 bus_driver_handle_get_service_owner },
1238 { "GetConnectionUnixUser",
1239 DBUS_TYPE_STRING_AS_STRING,
1240 DBUS_TYPE_UINT32_AS_STRING,
1241 bus_driver_handle_get_connection_unix_user },
1242 { "GetConnectionUnixProcessID",
1243 DBUS_TYPE_STRING_AS_STRING,
1244 DBUS_TYPE_UINT32_AS_STRING,
1245 bus_driver_handle_get_connection_unix_process_id },
1246 { "GetConnectionSELinuxSecurityContext",
1247 DBUS_TYPE_STRING_AS_STRING,
1248 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1249 bus_driver_handle_get_connection_selinux_security_context },
1253 bus_driver_handle_reload_config }
1257 write_args_for_direction (DBusString *xml,
1258 const char *signature,
1261 DBusTypeReader typereader;
1265 _dbus_string_init_const (&sigstr, signature);
1266 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1268 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1270 const DBusString *subsig;
1273 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1274 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1277 if (!_dbus_string_append_len (xml,
1278 _dbus_string_get_const_data (subsig) + start,
1281 if (!_dbus_string_append (xml, "\"/>\n"))
1284 _dbus_type_reader_next (&typereader);
1292 bus_driver_handle_introspect (DBusConnection *connection,
1293 BusTransaction *transaction,
1294 DBusMessage *message,
1299 const char *v_STRING;
1302 _dbus_verbose ("Introspect() on bus driver\n");
1304 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1308 if (! dbus_message_get_args (message, error,
1311 _DBUS_ASSERT_ERROR_IS_SET (error);
1315 if (!_dbus_string_init (&xml))
1317 BUS_SET_OOM (error);
1321 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1323 if (!_dbus_string_append (&xml, "<node>\n"))
1325 if (!_dbus_string_append_printf (&xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE))
1327 if (!_dbus_string_append (&xml, " <method name=\"Introspect\">\n"))
1329 if (!_dbus_string_append_printf (&xml, " <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING))
1331 if (!_dbus_string_append (&xml, " </method>\n"))
1333 if (!_dbus_string_append (&xml, " </interface>\n"))
1336 if (!_dbus_string_append_printf (&xml, " <interface name=\"%s\">\n",
1337 DBUS_INTERFACE_DBUS))
1341 while (i < _DBUS_N_ELEMENTS (message_handlers))
1344 if (!_dbus_string_append_printf (&xml, " <method name=\"%s\">\n",
1345 message_handlers[i].name))
1348 if (!write_args_for_direction (&xml, message_handlers[i].in_args, TRUE))
1351 if (!write_args_for_direction (&xml, message_handlers[i].out_args, FALSE))
1354 if (!_dbus_string_append (&xml, " </method>\n"))
1360 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameOwnerChanged\">\n"))
1363 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1366 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1369 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1372 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1377 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameLost\">\n"))
1380 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1383 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1388 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameAcquired\">\n"))
1391 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1394 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1399 if (!_dbus_string_append (&xml, " </interface>\n"))
1402 if (!_dbus_string_append (&xml, "</node>\n"))
1405 reply = dbus_message_new_method_return (message);
1409 v_STRING = _dbus_string_get_const_data (&xml);
1410 if (! dbus_message_append_args (reply,
1411 DBUS_TYPE_STRING, &v_STRING,
1415 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1418 dbus_message_unref (reply);
1419 _dbus_string_free (&xml);
1424 BUS_SET_OOM (error);
1427 dbus_message_unref (reply);
1429 _dbus_string_free (&xml);
1435 bus_driver_handle_message (DBusConnection *connection,
1436 BusTransaction *transaction,
1437 DBusMessage *message,
1440 const char *name, *sender, *interface;
1443 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1445 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1447 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1448 return TRUE; /* we just ignore this */
1451 if (dbus_message_is_method_call (message,
1452 DBUS_INTERFACE_INTROSPECTABLE,
1454 return bus_driver_handle_introspect (connection, transaction, message, error);
1456 interface = dbus_message_get_interface (message);
1457 if (interface == NULL)
1458 interface = DBUS_INTERFACE_DBUS;
1460 _dbus_assert (dbus_message_get_member (message) != NULL);
1462 name = dbus_message_get_member (message);
1463 sender = dbus_message_get_sender (message);
1465 if (strcmp (interface,
1466 DBUS_INTERFACE_DBUS) != 0)
1468 _dbus_verbose ("Driver got message to unknown interface \"%s\"\n",
1473 _dbus_verbose ("Driver got a method call: %s\n",
1474 dbus_message_get_member (message));
1476 /* security checks should have kept this from getting here */
1477 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1480 while (i < _DBUS_N_ELEMENTS (message_handlers))
1482 if (strcmp (message_handlers[i].name, name) == 0)
1484 _dbus_verbose ("Found driver handler for %s\n", name);
1486 if (!dbus_message_has_signature (message, message_handlers[i].in_args))
1488 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1489 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1490 name, dbus_message_get_signature (message),
1491 message_handlers[i].in_args);
1493 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1494 "Call to %s has wrong args (%s, expected %s)\n",
1495 name, dbus_message_get_signature (message),
1496 message_handlers[i].in_args);
1497 _DBUS_ASSERT_ERROR_IS_SET (error);
1501 if ((* message_handlers[i].handler) (connection, transaction, message, error))
1503 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1504 _dbus_verbose ("Driver handler succeeded\n");
1509 _DBUS_ASSERT_ERROR_IS_SET (error);
1510 _dbus_verbose ("Driver handler returned failure\n");
1519 _dbus_verbose ("No driver handler for message \"%s\"\n",
1522 dbus_set_error (error, DBUS_ERROR_UNKNOWN_METHOD,
1523 "%s does not understand message %s",
1524 DBUS_SERVICE_DBUS, name);
1530 bus_driver_remove_connection (DBusConnection *connection)
1532 /* FIXME Does nothing for now, should unregister the connection
1533 * with the bus driver.