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 if (!dbus_message_iter_close_container (&iter, &sub))
441 dbus_free_string_array (services);
442 dbus_message_unref (reply);
447 dbus_free_string_array (services);
449 if (!bus_transaction_send_from_driver (transaction, connection, reply))
451 dbus_message_unref (reply);
457 dbus_message_unref (reply);
463 bus_driver_handle_acquire_service (DBusConnection *connection,
464 BusTransaction *transaction,
465 DBusMessage *message,
469 DBusString service_name;
471 dbus_uint32_t service_reply;
474 BusRegistry *registry;
476 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
478 registry = bus_connection_get_registry (connection);
480 if (!dbus_message_get_args (message, error,
481 DBUS_TYPE_STRING, &name,
482 DBUS_TYPE_UINT32, &flags,
486 _dbus_verbose ("Trying to own name %s with flags 0x%x\n", name, flags);
491 _dbus_string_init_const (&service_name, name);
493 if (!bus_registry_acquire_service (registry, connection,
494 &service_name, flags,
495 &service_reply, transaction,
499 reply = dbus_message_new_method_return (message);
506 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
512 if (!bus_transaction_send_from_driver (transaction, connection, reply))
522 dbus_message_unref (reply);
527 bus_driver_handle_release_service (DBusConnection *connection,
528 BusTransaction *transaction,
529 DBusMessage *message,
533 DBusString service_name;
535 dbus_uint32_t service_reply;
537 BusRegistry *registry;
539 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
541 registry = bus_connection_get_registry (connection);
543 if (!dbus_message_get_args (message, error,
544 DBUS_TYPE_STRING, &name,
548 _dbus_verbose ("Trying to release name %s\n", name);
553 _dbus_string_init_const (&service_name, name);
555 if (!bus_registry_release_service (registry, connection,
556 &service_name, &service_reply,
560 reply = dbus_message_new_method_return (message);
567 if (!dbus_message_append_args (reply, DBUS_TYPE_UINT32, &service_reply, DBUS_TYPE_INVALID))
573 if (!bus_transaction_send_from_driver (transaction, connection, reply))
583 dbus_message_unref (reply);
588 bus_driver_handle_service_exists (DBusConnection *connection,
589 BusTransaction *transaction,
590 DBusMessage *message,
594 DBusString service_name;
596 dbus_bool_t service_exists;
599 BusRegistry *registry;
601 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
603 registry = bus_connection_get_registry (connection);
605 if (!dbus_message_get_args (message, error,
606 DBUS_TYPE_STRING, &name,
612 if (strcmp (name, DBUS_SERVICE_DBUS) == 0)
614 service_exists = TRUE;
618 _dbus_string_init_const (&service_name, name);
619 service = bus_registry_lookup (registry, &service_name);
620 service_exists = service != NULL;
623 reply = dbus_message_new_method_return (message);
630 if (!dbus_message_append_args (reply,
631 DBUS_TYPE_BOOLEAN, &service_exists,
638 if (!bus_transaction_send_from_driver (transaction, connection, reply))
648 dbus_message_unref (reply);
654 bus_driver_handle_activate_service (DBusConnection *connection,
655 BusTransaction *transaction,
656 DBusMessage *message,
662 BusActivation *activation;
664 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
666 activation = bus_connection_get_activation (connection);
668 if (!dbus_message_get_args (message, error,
669 DBUS_TYPE_STRING, &name,
670 DBUS_TYPE_UINT32, &flags,
673 _DBUS_ASSERT_ERROR_IS_SET (error);
674 _dbus_verbose ("No memory to get arguments to StartServiceByName\n");
680 if (!bus_activation_activate_service (activation, connection, transaction, FALSE,
681 message, name, error))
683 _DBUS_ASSERT_ERROR_IS_SET (error);
684 _dbus_verbose ("bus_activation_activate_service() failed\n");
695 send_ack_reply (DBusConnection *connection,
696 BusTransaction *transaction,
697 DBusMessage *message,
702 reply = dbus_message_new_method_return (message);
709 if (!bus_transaction_send_from_driver (transaction, connection, reply))
712 dbus_message_unref (reply);
716 dbus_message_unref (reply);
722 bus_driver_handle_add_match (DBusConnection *connection,
723 BusTransaction *transaction,
724 DBusMessage *message,
730 BusMatchmaker *matchmaker;
732 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
737 if (bus_connection_get_n_match_rules (connection) >=
738 bus_context_get_max_match_rules_per_connection (bus_transaction_get_context (transaction)))
740 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
741 "Connection \"%s\" is not allowed to add more match rules "
742 "(increase limits in configuration file if required)",
743 bus_connection_is_active (connection) ?
744 bus_connection_get_name (connection) :
749 if (!dbus_message_get_args (message, error,
750 DBUS_TYPE_STRING, &text,
753 _dbus_verbose ("No memory to get arguments to AddMatch\n");
757 _dbus_string_init_const (&str, text);
759 rule = bus_match_rule_parse (connection, &str, error);
763 matchmaker = bus_connection_get_matchmaker (connection);
765 if (!bus_matchmaker_add_rule (matchmaker, rule))
771 if (!send_ack_reply (connection, transaction,
774 bus_matchmaker_remove_rule (matchmaker, rule);
778 bus_match_rule_unref (rule);
783 _DBUS_ASSERT_ERROR_IS_SET (error);
785 bus_match_rule_unref (rule);
790 bus_driver_handle_remove_match (DBusConnection *connection,
791 BusTransaction *transaction,
792 DBusMessage *message,
798 BusMatchmaker *matchmaker;
800 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
805 if (!dbus_message_get_args (message, error,
806 DBUS_TYPE_STRING, &text,
809 _dbus_verbose ("No memory to get arguments to RemoveMatch\n");
813 _dbus_string_init_const (&str, text);
815 rule = bus_match_rule_parse (connection, &str, error);
819 /* Send the ack before we remove the rule, since the ack is undone
820 * on transaction cancel, but rule removal isn't.
822 if (!send_ack_reply (connection, transaction,
826 matchmaker = bus_connection_get_matchmaker (connection);
828 if (!bus_matchmaker_remove_rule_by_value (matchmaker, rule, error))
831 bus_match_rule_unref (rule);
836 _DBUS_ASSERT_ERROR_IS_SET (error);
838 bus_match_rule_unref (rule);
843 bus_driver_handle_get_service_owner (DBusConnection *connection,
844 BusTransaction *transaction,
845 DBusMessage *message,
849 const char *base_name;
851 BusRegistry *registry;
855 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
857 registry = bus_connection_get_registry (connection);
862 if (! dbus_message_get_args (message, error,
863 DBUS_TYPE_STRING, &text,
867 _dbus_string_init_const (&str, text);
868 service = bus_registry_lookup (registry, &str);
869 if (service == NULL &&
870 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
872 /* ORG_FREEDESKTOP_DBUS owns itself */
873 base_name = DBUS_SERVICE_DBUS;
875 else if (service == NULL)
877 dbus_set_error (error,
878 DBUS_ERROR_NAME_HAS_NO_OWNER,
879 "Could not get owner of name '%s': no such name", text);
884 base_name = bus_connection_get_name (bus_service_get_primary_owners_connection (service));
885 if (base_name == NULL)
887 /* FIXME - how is this error possible? */
888 dbus_set_error (error,
890 "Could not determine unique name for '%s'", text);
893 _dbus_assert (*base_name == ':');
896 _dbus_assert (base_name != NULL);
898 reply = dbus_message_new_method_return (message);
902 if (! dbus_message_append_args (reply,
903 DBUS_TYPE_STRING, &base_name,
907 if (! bus_transaction_send_from_driver (transaction, connection, reply))
910 dbus_message_unref (reply);
918 _DBUS_ASSERT_ERROR_IS_SET (error);
920 dbus_message_unref (reply);
925 bus_driver_handle_list_queued_owners (DBusConnection *connection,
926 BusTransaction *transaction,
927 DBusMessage *message,
931 DBusList *base_names;
934 BusRegistry *registry;
937 DBusMessageIter iter, array_iter;
938 char *dbus_service_name = DBUS_SERVICE_DBUS;
940 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
942 registry = bus_connection_get_registry (connection);
948 if (! dbus_message_get_args (message, error,
949 DBUS_TYPE_STRING, &text,
953 _dbus_string_init_const (&str, text);
954 service = bus_registry_lookup (registry, &str);
955 if (service == NULL &&
956 _dbus_string_equal_c_str (&str, DBUS_SERVICE_DBUS))
958 /* ORG_FREEDESKTOP_DBUS owns itself */
959 if (! _dbus_list_append (&base_names, dbus_service_name))
962 else if (service == NULL)
964 dbus_set_error (error,
965 DBUS_ERROR_NAME_HAS_NO_OWNER,
966 "Could not get owners of name '%s': no such name", text);
971 if (!bus_service_list_queued_owners (service,
977 _dbus_assert (base_names != NULL);
979 reply = dbus_message_new_method_return (message);
983 dbus_message_iter_init_append (reply, &iter);
984 if (!dbus_message_iter_open_container (&iter,
986 DBUS_TYPE_STRING_AS_STRING,
990 link = _dbus_list_get_first_link (&base_names);
995 _dbus_assert (link->data != NULL);
996 uname = (char *)link->data;
998 if (!dbus_message_iter_append_basic (&array_iter,
1003 link = _dbus_list_get_next_link (&base_names, link);
1006 if (! dbus_message_iter_close_container (&iter, &array_iter))
1010 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1013 dbus_message_unref (reply);
1018 BUS_SET_OOM (error);
1021 _DBUS_ASSERT_ERROR_IS_SET (error);
1023 dbus_message_unref (reply);
1026 _dbus_list_clear (&base_names);
1032 bus_driver_handle_get_connection_unix_user (DBusConnection *connection,
1033 BusTransaction *transaction,
1034 DBusMessage *message,
1037 const char *service;
1039 BusRegistry *registry;
1041 DBusConnection *conn;
1044 dbus_uint32_t uid32;
1046 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1048 registry = bus_connection_get_registry (connection);
1053 if (! dbus_message_get_args (message, error,
1054 DBUS_TYPE_STRING, &service,
1058 _dbus_verbose ("asked for UID of connection %s\n", service);
1060 _dbus_string_init_const (&str, service);
1061 serv = bus_registry_lookup (registry, &str);
1064 dbus_set_error (error,
1065 DBUS_ERROR_NAME_HAS_NO_OWNER,
1066 "Could not get UID of name '%s': no such name", service);
1070 conn = bus_service_get_primary_owners_connection (serv);
1072 reply = dbus_message_new_method_return (message);
1076 if (!dbus_connection_get_unix_user (conn, &uid))
1078 dbus_set_error (error,
1080 "Could not determine UID for '%s'", service);
1085 if (! dbus_message_append_args (reply,
1086 DBUS_TYPE_UINT32, &uid32,
1090 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1093 dbus_message_unref (reply);
1098 BUS_SET_OOM (error);
1101 _DBUS_ASSERT_ERROR_IS_SET (error);
1103 dbus_message_unref (reply);
1108 bus_driver_handle_get_connection_unix_process_id (DBusConnection *connection,
1109 BusTransaction *transaction,
1110 DBusMessage *message,
1113 const char *service;
1115 BusRegistry *registry;
1117 DBusConnection *conn;
1120 dbus_uint32_t pid32;
1122 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1124 registry = bus_connection_get_registry (connection);
1129 if (! dbus_message_get_args (message, error,
1130 DBUS_TYPE_STRING, &service,
1134 _dbus_verbose ("asked for PID of connection %s\n", service);
1136 _dbus_string_init_const (&str, service);
1137 serv = bus_registry_lookup (registry, &str);
1140 dbus_set_error (error,
1141 DBUS_ERROR_NAME_HAS_NO_OWNER,
1142 "Could not get PID of name '%s': no such name", service);
1146 conn = bus_service_get_primary_owners_connection (serv);
1148 reply = dbus_message_new_method_return (message);
1152 if (!dbus_connection_get_unix_process_id (conn, &pid))
1154 dbus_set_error (error,
1155 DBUS_ERROR_UNIX_PROCESS_ID_UNKNOWN,
1156 "Could not determine PID for '%s'", service);
1161 if (! dbus_message_append_args (reply,
1162 DBUS_TYPE_UINT32, &pid32,
1166 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1169 dbus_message_unref (reply);
1174 BUS_SET_OOM (error);
1177 _DBUS_ASSERT_ERROR_IS_SET (error);
1179 dbus_message_unref (reply);
1184 bus_driver_handle_get_connection_selinux_security_context (DBusConnection *connection,
1185 BusTransaction *transaction,
1186 DBusMessage *message,
1189 const char *service;
1191 BusRegistry *registry;
1193 DBusConnection *conn;
1195 BusSELinuxID *context;
1197 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1199 registry = bus_connection_get_registry (connection);
1204 if (! dbus_message_get_args (message, error,
1205 DBUS_TYPE_STRING, &service,
1209 _dbus_verbose ("asked for security context of connection %s\n", service);
1211 _dbus_string_init_const (&str, service);
1212 serv = bus_registry_lookup (registry, &str);
1215 dbus_set_error (error,
1216 DBUS_ERROR_NAME_HAS_NO_OWNER,
1217 "Could not get security context of name '%s': no such name", service);
1221 conn = bus_service_get_primary_owners_connection (serv);
1223 reply = dbus_message_new_method_return (message);
1227 context = bus_connection_get_selinux_id (conn);
1230 dbus_set_error (error,
1231 DBUS_ERROR_SELINUX_SECURITY_CONTEXT_UNKNOWN,
1232 "Could not determine security context for '%s'", service);
1236 if (! bus_selinux_append_context (reply, context, error))
1239 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1242 dbus_message_unref (reply);
1247 BUS_SET_OOM (error);
1250 _DBUS_ASSERT_ERROR_IS_SET (error);
1252 dbus_message_unref (reply);
1257 bus_driver_handle_reload_config (DBusConnection *connection,
1258 BusTransaction *transaction,
1259 DBusMessage *message,
1262 BusContext *context;
1265 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1269 context = bus_connection_get_context (connection);
1270 if (!bus_context_reload_config (context, error))
1273 reply = dbus_message_new_method_return (message);
1277 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1280 dbus_message_unref (reply);
1284 BUS_SET_OOM (error);
1287 _DBUS_ASSERT_ERROR_IS_SET (error);
1289 dbus_message_unref (reply);
1293 /* For speed it might be useful to sort this in order of
1294 * frequency of use (but doesn't matter with only a few items
1300 const char *in_args;
1301 const char *out_args;
1302 dbus_bool_t (* handler) (DBusConnection *connection,
1303 BusTransaction *transaction,
1304 DBusMessage *message,
1306 } message_handlers[] = {
1308 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1309 DBUS_TYPE_UINT32_AS_STRING,
1310 bus_driver_handle_acquire_service },
1312 DBUS_TYPE_STRING_AS_STRING,
1313 DBUS_TYPE_UINT32_AS_STRING,
1314 bus_driver_handle_release_service },
1315 { "StartServiceByName",
1316 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_UINT32_AS_STRING,
1317 DBUS_TYPE_UINT32_AS_STRING,
1318 bus_driver_handle_activate_service },
1321 DBUS_TYPE_STRING_AS_STRING,
1322 bus_driver_handle_hello },
1324 DBUS_TYPE_STRING_AS_STRING,
1325 DBUS_TYPE_BOOLEAN_AS_STRING,
1326 bus_driver_handle_service_exists },
1329 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1330 bus_driver_handle_list_services },
1332 DBUS_TYPE_STRING_AS_STRING,
1334 bus_driver_handle_add_match },
1336 DBUS_TYPE_STRING_AS_STRING,
1338 bus_driver_handle_remove_match },
1340 DBUS_TYPE_STRING_AS_STRING,
1341 DBUS_TYPE_STRING_AS_STRING,
1342 bus_driver_handle_get_service_owner },
1343 { "ListQueuedOwners",
1344 DBUS_TYPE_STRING_AS_STRING,
1345 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_STRING_AS_STRING,
1346 bus_driver_handle_list_queued_owners },
1347 { "GetConnectionUnixUser",
1348 DBUS_TYPE_STRING_AS_STRING,
1349 DBUS_TYPE_UINT32_AS_STRING,
1350 bus_driver_handle_get_connection_unix_user },
1351 { "GetConnectionUnixProcessID",
1352 DBUS_TYPE_STRING_AS_STRING,
1353 DBUS_TYPE_UINT32_AS_STRING,
1354 bus_driver_handle_get_connection_unix_process_id },
1355 { "GetConnectionSELinuxSecurityContext",
1356 DBUS_TYPE_STRING_AS_STRING,
1357 DBUS_TYPE_ARRAY_AS_STRING DBUS_TYPE_BYTE_AS_STRING,
1358 bus_driver_handle_get_connection_selinux_security_context },
1362 bus_driver_handle_reload_config }
1366 write_args_for_direction (DBusString *xml,
1367 const char *signature,
1370 DBusTypeReader typereader;
1374 _dbus_string_init_const (&sigstr, signature);
1375 _dbus_type_reader_init_types_only (&typereader, &sigstr, 0);
1377 while ((current_type = _dbus_type_reader_get_current_type (&typereader)) != DBUS_TYPE_INVALID)
1379 const DBusString *subsig;
1382 _dbus_type_reader_get_signature (&typereader, &subsig, &start, &len);
1383 if (!_dbus_string_append_printf (xml, " <arg direction=\"%s\" type=\"",
1386 if (!_dbus_string_append_len (xml,
1387 _dbus_string_get_const_data (subsig) + start,
1390 if (!_dbus_string_append (xml, "\"/>\n"))
1393 _dbus_type_reader_next (&typereader);
1401 bus_driver_handle_introspect (DBusConnection *connection,
1402 BusTransaction *transaction,
1403 DBusMessage *message,
1408 const char *v_STRING;
1411 _dbus_verbose ("Introspect() on bus driver\n");
1413 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1417 if (! dbus_message_get_args (message, error,
1420 _DBUS_ASSERT_ERROR_IS_SET (error);
1424 if (!_dbus_string_init (&xml))
1426 BUS_SET_OOM (error);
1430 if (!_dbus_string_append (&xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE))
1432 if (!_dbus_string_append (&xml, "<node>\n"))
1434 if (!_dbus_string_append_printf (&xml, " <interface name=\"%s\">\n", DBUS_INTERFACE_INTROSPECTABLE))
1436 if (!_dbus_string_append (&xml, " <method name=\"Introspect\">\n"))
1438 if (!_dbus_string_append_printf (&xml, " <arg name=\"data\" direction=\"out\" type=\"%s\"/>\n", DBUS_TYPE_STRING_AS_STRING))
1440 if (!_dbus_string_append (&xml, " </method>\n"))
1442 if (!_dbus_string_append (&xml, " </interface>\n"))
1445 if (!_dbus_string_append_printf (&xml, " <interface name=\"%s\">\n",
1446 DBUS_INTERFACE_DBUS))
1450 while (i < _DBUS_N_ELEMENTS (message_handlers))
1453 if (!_dbus_string_append_printf (&xml, " <method name=\"%s\">\n",
1454 message_handlers[i].name))
1457 if (!write_args_for_direction (&xml, message_handlers[i].in_args, TRUE))
1460 if (!write_args_for_direction (&xml, message_handlers[i].out_args, FALSE))
1463 if (!_dbus_string_append (&xml, " </method>\n"))
1469 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameOwnerChanged\">\n"))
1472 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1475 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1478 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1481 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1486 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameLost\">\n"))
1489 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1492 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1497 if (!_dbus_string_append_printf (&xml, " <signal name=\"NameAcquired\">\n"))
1500 if (!_dbus_string_append_printf (&xml, " <arg type=\"s\"/>\n"))
1503 if (!_dbus_string_append_printf (&xml, " </signal>\n"))
1508 if (!_dbus_string_append (&xml, " </interface>\n"))
1511 if (!_dbus_string_append (&xml, "</node>\n"))
1514 reply = dbus_message_new_method_return (message);
1518 v_STRING = _dbus_string_get_const_data (&xml);
1519 if (! dbus_message_append_args (reply,
1520 DBUS_TYPE_STRING, &v_STRING,
1524 if (! bus_transaction_send_from_driver (transaction, connection, reply))
1527 dbus_message_unref (reply);
1528 _dbus_string_free (&xml);
1533 BUS_SET_OOM (error);
1536 dbus_message_unref (reply);
1538 _dbus_string_free (&xml);
1544 bus_driver_handle_message (DBusConnection *connection,
1545 BusTransaction *transaction,
1546 DBusMessage *message,
1549 const char *name, *sender, *interface;
1552 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1554 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
1556 _dbus_verbose ("Driver got a non-method-call message, ignoring\n");
1557 return TRUE; /* we just ignore this */
1560 if (dbus_message_is_method_call (message,
1561 DBUS_INTERFACE_INTROSPECTABLE,
1563 return bus_driver_handle_introspect (connection, transaction, message, error);
1565 interface = dbus_message_get_interface (message);
1566 if (interface == NULL)
1567 interface = DBUS_INTERFACE_DBUS;
1569 _dbus_assert (dbus_message_get_member (message) != NULL);
1571 name = dbus_message_get_member (message);
1572 sender = dbus_message_get_sender (message);
1574 if (strcmp (interface,
1575 DBUS_INTERFACE_DBUS) != 0)
1577 _dbus_verbose ("Driver got message to unknown interface \"%s\"\n",
1582 _dbus_verbose ("Driver got a method call: %s\n",
1583 dbus_message_get_member (message));
1585 /* security checks should have kept this from getting here */
1586 _dbus_assert (sender != NULL || strcmp (name, "Hello") == 0);
1589 while (i < _DBUS_N_ELEMENTS (message_handlers))
1591 if (strcmp (message_handlers[i].name, name) == 0)
1593 _dbus_verbose ("Found driver handler for %s\n", name);
1595 if (!dbus_message_has_signature (message, message_handlers[i].in_args))
1597 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1598 _dbus_verbose ("Call to %s has wrong args (%s, expected %s)\n",
1599 name, dbus_message_get_signature (message),
1600 message_handlers[i].in_args);
1602 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1603 "Call to %s has wrong args (%s, expected %s)\n",
1604 name, dbus_message_get_signature (message),
1605 message_handlers[i].in_args);
1606 _DBUS_ASSERT_ERROR_IS_SET (error);
1610 if ((* message_handlers[i].handler) (connection, transaction, message, error))
1612 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1613 _dbus_verbose ("Driver handler succeeded\n");
1618 _DBUS_ASSERT_ERROR_IS_SET (error);
1619 _dbus_verbose ("Driver handler returned failure\n");
1628 _dbus_verbose ("No driver handler for message \"%s\"\n",
1631 dbus_set_error (error, DBUS_ERROR_UNKNOWN_METHOD,
1632 "%s does not understand message %s",
1633 DBUS_SERVICE_DBUS, name);
1639 bus_driver_remove_connection (DBusConnection *connection)
1641 /* FIXME Does nothing for now, should unregister the connection
1642 * with the bus driver.