1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* signals.c Bus signal connection implementation
4 * Copyright (C) 2003, 2005 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <dbus/dbus-marshal-validate.h>
35 int refcount; /**< reference count */
37 DBusConnection *matches_go_to; /**< Owner of the rule */
39 unsigned int flags; /**< BusMatchFlags */
48 unsigned int *arg_lens;
53 #define BUS_MATCH_ARG_NAMESPACE 0x4000000u
54 #define BUS_MATCH_ARG_IS_PATH 0x8000000u
56 #define BUS_MATCH_ARG_FLAGS (BUS_MATCH_ARG_NAMESPACE | BUS_MATCH_ARG_IS_PATH)
59 bus_match_rule_new (DBusConnection *matches_go_to)
63 rule = dbus_new0 (BusMatchRule, 1);
68 rule->matches_go_to = matches_go_to;
70 #ifndef DBUS_ENABLE_EMBEDDED_TESTS
71 _dbus_assert (rule->matches_go_to != NULL);
78 bus_match_rule_ref (BusMatchRule *rule)
80 _dbus_assert (rule->refcount > 0);
88 bus_match_rule_unref (BusMatchRule *rule)
90 _dbus_assert (rule->refcount > 0);
93 if (rule->refcount == 0)
95 dbus_free (rule->interface);
96 dbus_free (rule->member);
97 dbus_free (rule->sender);
98 dbus_free (rule->destination);
99 dbus_free (rule->path);
100 dbus_free (rule->arg_lens);
102 /* can't use dbus_free_string_array() since there
110 while (i < rule->args_len)
113 dbus_free (rule->args[i]);
117 dbus_free (rule->args);
124 #if defined(DBUS_ENABLE_VERBOSE_MODE) || defined(DBUS_ENABLE_STATS)
126 append_key_and_escaped_value (DBusString *str, const char *token, const char *value)
128 const char *p = value;
130 if (!_dbus_string_append_printf (str, "%s='", token))
135 const char *next = strchr (p, '\'');
139 if (!_dbus_string_append_printf (str, "%.*s", (int) (next - p), p))
141 /* Horrible escape sequence: single quote cannot be escaped inside
142 * a single quoted string. So we close the single quote, escape the
143 * single quote, and reopen a single quote.
145 if (!_dbus_string_append_printf (str, "'\\''"))
151 if (!_dbus_string_append_printf (str, "%s", p))
157 if (!_dbus_string_append_byte (str, '\''))
163 /* returns NULL if no memory */
165 match_rule_to_string (BusMatchRule *rule)
170 if (!_dbus_string_init (&str))
175 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
177 if (!append_key_and_escaped_value (&str, "type",
178 dbus_message_type_to_string (rule->message_type)))
182 if (rule->flags & BUS_MATCH_INTERFACE)
184 if (_dbus_string_get_length (&str) > 0)
186 if (!_dbus_string_append (&str, ","))
190 if (!append_key_and_escaped_value (&str, "interface", rule->interface))
194 if (rule->flags & BUS_MATCH_MEMBER)
196 if (_dbus_string_get_length (&str) > 0)
198 if (!_dbus_string_append (&str, ","))
202 if (!append_key_and_escaped_value (&str, "member", rule->member))
206 if (rule->flags & BUS_MATCH_PATH)
208 if (_dbus_string_get_length (&str) > 0)
210 if (!_dbus_string_append (&str, ","))
214 if (!append_key_and_escaped_value (&str, "path", rule->path))
218 if (rule->flags & BUS_MATCH_PATH_NAMESPACE)
220 if (_dbus_string_get_length (&str) > 0)
222 if (!_dbus_string_append (&str, ","))
226 if (!append_key_and_escaped_value (&str, "path_namespace", rule->path))
230 if (rule->flags & BUS_MATCH_SENDER)
232 if (_dbus_string_get_length (&str) > 0)
234 if (!_dbus_string_append (&str, ","))
238 if (!append_key_and_escaped_value (&str, "sender", rule->sender))
242 if (rule->flags & BUS_MATCH_DESTINATION)
244 if (_dbus_string_get_length (&str) > 0)
246 if (!_dbus_string_append (&str, ","))
250 if (!append_key_and_escaped_value (&str, "destination", rule->destination))
254 if (rule->flags & BUS_MATCH_CLIENT_IS_EAVESDROPPING)
256 if (_dbus_string_get_length (&str) > 0)
258 if (!_dbus_string_append (&str, ","))
262 if (!append_key_and_escaped_value (&str, "eavesdrop",
263 (rule->flags & BUS_MATCH_CLIENT_IS_EAVESDROPPING) ?
268 if (rule->flags & BUS_MATCH_ARGS)
272 _dbus_assert (rule->args != NULL);
275 while (i < rule->args_len)
277 if (rule->args[i] != NULL)
279 dbus_bool_t is_path, is_namespace;
281 if (_dbus_string_get_length (&str) > 0)
283 if (!_dbus_string_append (&str, ","))
287 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
288 is_namespace = (rule->arg_lens[i] & BUS_MATCH_ARG_NAMESPACE) != 0;
290 if (!_dbus_string_append_printf (&str,
294 is_namespace ? "namespace" : ""))
296 if (!append_key_and_escaped_value (&str, "", rule->args[i]))
304 if (!_dbus_string_steal_data (&str, &ret))
307 _dbus_string_free (&str);
311 _dbus_string_free (&str);
314 #endif /* defined(DBUS_ENABLE_VERBOSE_MODE) || defined(DBUS_ENABLE_STATS) */
317 bus_match_rule_set_message_type (BusMatchRule *rule,
320 rule->flags |= BUS_MATCH_MESSAGE_TYPE;
322 rule->message_type = type;
328 bus_match_rule_set_interface (BusMatchRule *rule,
329 const char *interface)
333 _dbus_assert (interface != NULL);
335 new = _dbus_strdup (interface);
339 rule->flags |= BUS_MATCH_INTERFACE;
340 dbus_free (rule->interface);
341 rule->interface = new;
347 bus_match_rule_set_member (BusMatchRule *rule,
352 _dbus_assert (member != NULL);
354 new = _dbus_strdup (member);
358 rule->flags |= BUS_MATCH_MEMBER;
359 dbus_free (rule->member);
366 bus_match_rule_set_sender (BusMatchRule *rule,
371 _dbus_assert (sender != NULL);
373 new = _dbus_strdup (sender);
377 rule->flags |= BUS_MATCH_SENDER;
378 dbus_free (rule->sender);
385 bus_match_rule_set_destination (BusMatchRule *rule,
386 const char *destination)
390 _dbus_assert (destination != NULL);
392 new = _dbus_strdup (destination);
396 rule->flags |= BUS_MATCH_DESTINATION;
397 dbus_free (rule->destination);
398 rule->destination = new;
404 bus_match_rule_set_client_is_eavesdropping (BusMatchRule *rule,
405 dbus_bool_t is_eavesdropping)
407 if (is_eavesdropping)
408 rule->flags |= BUS_MATCH_CLIENT_IS_EAVESDROPPING;
410 rule->flags &= ~(BUS_MATCH_CLIENT_IS_EAVESDROPPING);
414 bus_match_rule_get_client_is_eavesdropping (BusMatchRule *rule)
416 if (rule->flags & BUS_MATCH_CLIENT_IS_EAVESDROPPING)
423 bus_match_rule_set_path (BusMatchRule *rule,
425 dbus_bool_t is_namespace)
429 _dbus_assert (path != NULL);
431 new = _dbus_strdup (path);
435 rule->flags &= ~(BUS_MATCH_PATH|BUS_MATCH_PATH_NAMESPACE);
438 rule->flags |= BUS_MATCH_PATH_NAMESPACE;
440 rule->flags |= BUS_MATCH_PATH;
442 dbus_free (rule->path);
449 bus_match_rule_set_arg (BusMatchRule *rule,
451 const DBusString *value,
453 dbus_bool_t is_namespace)
458 _dbus_assert (value != NULL);
460 /* args_len is the number of args not including null termination
463 if (arg >= rule->args_len)
465 unsigned int *new_arg_lens;
470 new_args_len = arg + 1;
472 /* add another + 1 here for null termination */
473 new_args = dbus_realloc (rule->args,
474 sizeof (char *) * (new_args_len + 1));
475 if (new_args == NULL)
478 /* NULL the new slots */
480 while (i <= new_args_len) /* <= for null termination */
486 rule->args = new_args;
488 /* and now add to the lengths */
489 new_arg_lens = dbus_realloc (rule->arg_lens,
490 sizeof (int) * (new_args_len + 1));
492 if (new_arg_lens == NULL)
495 /* zero the new slots */
497 while (i <= new_args_len) /* <= for null termination */
503 rule->arg_lens = new_arg_lens;
504 rule->args_len = new_args_len;
507 length = _dbus_string_get_length (value);
508 if (!_dbus_string_copy_data (value, &new))
511 rule->flags |= BUS_MATCH_ARGS;
513 dbus_free (rule->args[arg]);
514 rule->arg_lens[arg] = length;
515 rule->args[arg] = new;
518 rule->arg_lens[arg] |= BUS_MATCH_ARG_IS_PATH;
521 rule->arg_lens[arg] |= BUS_MATCH_ARG_NAMESPACE;
523 /* NULL termination didn't get busted */
524 _dbus_assert (rule->args[rule->args_len] == NULL);
525 _dbus_assert (rule->arg_lens[rule->args_len] == 0);
530 #define ISWHITE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
533 find_key (const DBusString *str,
541 const char *key_start;
544 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
546 s = _dbus_string_get_const_data (str);
550 while (*p && ISWHITE (*p))
555 while (*p && *p != '=' && !ISWHITE (*p))
560 while (*p && ISWHITE (*p))
563 if (key_start == key_end)
565 /* Empty match rules or trailing whitespace are OK */
572 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
573 "Match rule has a key with no subsequent '=' character");
578 if (!_dbus_string_append_len (key, key_start, key_end - key_start))
590 find_value (const DBusString *str,
602 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
604 orig_len = _dbus_string_get_length (value);
606 s = _dbus_string_get_const_data (str);
614 if (quote_char == '\0')
634 if (!_dbus_string_append_byte (value, *p))
641 else if (quote_char == '\\')
643 /* \ only counts as an escape if escaping a quote mark */
646 if (!_dbus_string_append_byte (value, '\\'))
653 if (!_dbus_string_append_byte (value, *p))
663 _dbus_assert (quote_char == '\'');
671 if (!_dbus_string_append_byte (value, *p))
685 if (quote_char == '\\')
687 if (!_dbus_string_append_byte (value, '\\'))
693 else if (quote_char == '\'')
695 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
696 "Unbalanced quotation marks in match rule");
700 _dbus_assert (quote_char == '\0');
702 /* Zero-length values are allowed */
709 _DBUS_ASSERT_ERROR_IS_SET (error);
710 _dbus_string_set_length (value, orig_len);
714 /* duplicates aren't allowed so the real legitimate max is only 6 or
715 * so. Leaving extra so we don't have to bother to update it.
716 * FIXME this is sort of busted now with arg matching, but we let
717 * you match on up to 10 args for now
719 #define MAX_RULE_TOKENS 16
721 /* this is slightly too high level to be termed a "token"
722 * but let's not be pedantic.
731 tokenize_rule (const DBusString *rule_text,
732 RuleToken tokens[MAX_RULE_TOKENS],
743 if (!_dbus_string_init (&key))
749 if (!_dbus_string_init (&value))
751 _dbus_string_free (&key);
758 while (i < MAX_RULE_TOKENS &&
759 pos < _dbus_string_get_length (rule_text))
761 _dbus_assert (tokens[i].key == NULL);
762 _dbus_assert (tokens[i].value == NULL);
764 if (!find_key (rule_text, pos, &key, &pos, error))
767 if (_dbus_string_get_length (&key) == 0)
770 if (!_dbus_string_steal_data (&key, &tokens[i].key))
776 if (!find_value (rule_text, pos, tokens[i].key, &value, &pos, error))
779 if (!_dbus_string_steal_data (&value, &tokens[i].value))
795 while (tokens[i].key || tokens[i].value)
797 dbus_free (tokens[i].key);
798 dbus_free (tokens[i].value);
799 tokens[i].key = NULL;
800 tokens[i].value = NULL;
805 _dbus_string_free (&key);
806 _dbus_string_free (&value);
812 bus_match_rule_parse_arg_match (BusMatchRule *rule,
814 const DBusString *value,
817 dbus_bool_t is_path = FALSE;
818 dbus_bool_t is_namespace = FALSE;
824 /* For now, arg0='foo' always implies that 'foo' is a
825 * DBUS_TYPE_STRING. Someday we could add an arg0type='int32' thing
826 * if we wanted, which would specify another type, in which case
827 * arg0='5' would have the 5 parsed as an int rather than string.
830 /* First we need to parse arg0 = 0, arg27 = 27 */
832 _dbus_string_init_const (&key_str, key);
833 length = _dbus_string_get_length (&key_str);
835 if (_dbus_string_get_length (&key_str) < 4)
837 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
838 "Key '%s' in match rule starts with 'arg' but lacks an arg number. Should be 'arg0' or 'arg7' for example.\n", key);
842 if (!_dbus_string_parse_uint (&key_str, 3, &arg, &end))
844 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
845 "Key '%s' in match rule starts with 'arg' but could not parse arg number. Should be 'arg0' or 'arg7' for example.\n", key);
851 int len1 = strlen ("path");
852 if ((end + len1) == length &&
853 _dbus_string_ends_with_c_str (&key_str, "path"))
857 else if (_dbus_string_equal_c_str (&key_str, "arg0namespace"))
859 int value_len = _dbus_string_get_length (value);
863 if (!_dbus_validate_bus_namespace (value, 0, value_len))
865 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
866 "arg0namespace='%s' is not a valid prefix of a bus name",
867 _dbus_string_get_const_data (value));
873 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
874 "Key '%s' in match rule contains junk after argument number (%u). Only 'arg%upath' (for example) or 'arg0namespace' are valid", key, arg, arg);
879 /* If we didn't check this we could allocate a huge amount of RAM */
880 if (arg > DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER)
882 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
883 "Key '%s' in match rule has arg number %lu but the maximum is %d.\n", key, (unsigned long) arg, DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER);
887 if ((rule->flags & BUS_MATCH_ARGS) &&
888 rule->args_len > (int) arg &&
889 rule->args[arg] != NULL)
891 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
892 "Argument %d matched more than once in match rule\n", key);
896 if (!bus_match_rule_set_arg (rule, arg, value, is_path, is_namespace))
905 _DBUS_ASSERT_ERROR_IS_SET (error);
910 * The format is comma-separated with strings quoted with single quotes
911 * as for the shell (to escape a literal single quote, use '\'').
913 * type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',
914 * path='/bar/foo',destination=':452345.34'
918 bus_match_rule_parse (DBusConnection *matches_go_to,
919 const DBusString *rule_text,
923 RuleToken tokens[MAX_RULE_TOKENS+1]; /* NULL termination + 1 */
926 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
928 if (_dbus_string_get_length (rule_text) > DBUS_MAXIMUM_MATCH_RULE_LENGTH)
930 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
931 "Match rule text is %d bytes, maximum is %d",
932 _dbus_string_get_length (rule_text),
933 DBUS_MAXIMUM_MATCH_RULE_LENGTH);
937 memset (tokens, '\0', sizeof (tokens));
939 rule = bus_match_rule_new (matches_go_to);
946 if (!tokenize_rule (rule_text, tokens, error))
950 while (tokens[i].key != NULL)
954 const char *key = tokens[i].key;
955 const char *value = tokens[i].value;
957 _dbus_string_init_const (&tmp_str, value);
958 len = _dbus_string_get_length (&tmp_str);
960 if (strcmp (key, "type") == 0)
964 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
966 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
967 "Key %s specified twice in match rule\n", key);
971 t = dbus_message_type_from_string (value);
973 if (t == DBUS_MESSAGE_TYPE_INVALID)
975 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
976 "Invalid message type (%s) in match rule\n", value);
980 if (!bus_match_rule_set_message_type (rule, t))
986 else if (strcmp (key, "sender") == 0)
988 if (rule->flags & BUS_MATCH_SENDER)
990 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
991 "Key %s specified twice in match rule\n", key);
995 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
997 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
998 "Sender name '%s' is invalid\n", value);
1002 if (!bus_match_rule_set_sender (rule, value))
1004 BUS_SET_OOM (error);
1008 else if (strcmp (key, "interface") == 0)
1010 if (rule->flags & BUS_MATCH_INTERFACE)
1012 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1013 "Key %s specified twice in match rule\n", key);
1017 if (!_dbus_validate_interface (&tmp_str, 0, len))
1019 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1020 "Interface name '%s' is invalid\n", value);
1024 if (!bus_match_rule_set_interface (rule, value))
1026 BUS_SET_OOM (error);
1030 else if (strcmp (key, "member") == 0)
1032 if (rule->flags & BUS_MATCH_MEMBER)
1034 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1035 "Key %s specified twice in match rule\n", key);
1039 if (!_dbus_validate_member (&tmp_str, 0, len))
1041 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1042 "Member name '%s' is invalid\n", value);
1046 if (!bus_match_rule_set_member (rule, value))
1048 BUS_SET_OOM (error);
1052 else if (strcmp (key, "path") == 0 ||
1053 strcmp (key, "path_namespace") == 0)
1055 dbus_bool_t is_namespace = (strcmp (key, "path_namespace") == 0);
1057 if (rule->flags & (BUS_MATCH_PATH | BUS_MATCH_PATH_NAMESPACE))
1059 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1060 "path or path_namespace specified twice in match rule\n");
1064 if (!_dbus_validate_path (&tmp_str, 0, len))
1066 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1067 "Path '%s' is invalid\n", value);
1071 if (!bus_match_rule_set_path (rule, value, is_namespace))
1073 BUS_SET_OOM (error);
1077 else if (strcmp (key, "destination") == 0)
1079 if (rule->flags & BUS_MATCH_DESTINATION)
1081 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1082 "Key %s specified twice in match rule\n", key);
1086 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
1088 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1089 "Destination name '%s' is invalid\n", value);
1093 if (!bus_match_rule_set_destination (rule, value))
1095 BUS_SET_OOM (error);
1099 else if (strcmp (key, "eavesdrop") == 0)
1101 /* do not detect "eavesdrop" being used more than once in rule:
1102 * 1) it's not possible, it's only in the flags
1103 * 2) it might be used twice to disable eavesdropping when it's
1104 * automatically added (eg dbus-monitor/bustle) */
1106 /* we accept only "true|false" as possible values */
1107 if ((strcmp (value, "true") == 0))
1109 bus_match_rule_set_client_is_eavesdropping (rule, TRUE);
1111 else if (strcmp (value, "false") == 0)
1113 bus_match_rule_set_client_is_eavesdropping (rule, FALSE);
1117 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1118 "eavesdrop='%s' is invalid, "
1119 "it should be 'true' or 'false'\n",
1124 else if (strncmp (key, "arg", 3) == 0)
1126 if (!bus_match_rule_parse_arg_match (rule, key, &tmp_str, error))
1131 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1132 "Unknown key \"%s\" in match rule",
1144 _DBUS_ASSERT_ERROR_IS_SET (error);
1147 bus_match_rule_unref (rule);
1154 while (tokens[i].key || tokens[i].value)
1156 _dbus_assert (i < MAX_RULE_TOKENS);
1157 dbus_free (tokens[i].key);
1158 dbus_free (tokens[i].value);
1165 typedef struct RulePool RulePool;
1168 /* Maps non-NULL interface names to non-NULL (DBusList **)s */
1169 DBusHashTable *rules_by_iface;
1171 /* List of BusMatchRules which don't specify an interface */
1172 DBusList *rules_without_iface;
1175 struct BusMatchmaker
1179 /* Pools of rules, grouped by the type of message they match. 0
1180 * (DBUS_MESSAGE_TYPE_INVALID) represents rules that do not specify a message
1183 RulePool rules_by_type[DBUS_NUM_MESSAGE_TYPES];
1186 #ifdef DBUS_ENABLE_STATS
1188 bus_match_rule_dump (BusMatchmaker *matchmaker,
1189 DBusConnection *conn_filter,
1190 DBusMessageIter *arr_iter)
1194 for (i = 0 ; i < DBUS_NUM_MESSAGE_TYPES ; i++)
1200 _dbus_hash_iter_init (matchmaker->rules_by_type[i].rules_by_iface, &iter);
1201 while (_dbus_hash_iter_next (&iter))
1203 list = _dbus_hash_iter_get_value (&iter);
1204 for (link = _dbus_list_get_first_link (list);
1206 link = _dbus_list_get_next_link (list, link))
1208 BusMatchRule *rule = link->data;
1210 if (rule->matches_go_to == conn_filter)
1212 char *s = match_rule_to_string (rule);
1217 if (!dbus_message_iter_append_basic (arr_iter, DBUS_TYPE_STRING, &s))
1226 list = &matchmaker->rules_by_type[i].rules_without_iface;
1227 for (link = _dbus_list_get_first_link (list);
1229 link = _dbus_list_get_next_link (list, link))
1231 BusMatchRule *rule = link->data;
1233 if (rule->matches_go_to == conn_filter)
1235 char *s = match_rule_to_string (rule);
1240 if (!dbus_message_iter_append_basic (arr_iter, DBUS_TYPE_STRING, &s))
1255 rule_list_free (DBusList **rules)
1257 while (*rules != NULL)
1261 rule = (*rules)->data;
1262 bus_match_rule_unref (rule);
1263 _dbus_list_remove_link (rules, *rules);
1268 rule_list_ptr_free (DBusList **list)
1270 /* We have to cope with NULL because the hash table frees the "existing"
1271 * value (which is NULL) when creating a new table entry...
1275 rule_list_free (list);
1281 bus_matchmaker_new (void)
1283 BusMatchmaker *matchmaker;
1286 matchmaker = dbus_new0 (BusMatchmaker, 1);
1287 if (matchmaker == NULL)
1290 matchmaker->refcount = 1;
1292 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1294 RulePool *p = matchmaker->rules_by_type + i;
1296 p->rules_by_iface = _dbus_hash_table_new (DBUS_HASH_STRING,
1297 dbus_free, (DBusFreeFunction) rule_list_ptr_free);
1299 if (p->rules_by_iface == NULL)
1306 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1308 RulePool *p = matchmaker->rules_by_type + i;
1310 if (p->rules_by_iface == NULL)
1313 _dbus_hash_table_unref (p->rules_by_iface);
1315 dbus_free (matchmaker);
1321 bus_matchmaker_get_rules (BusMatchmaker *matchmaker,
1323 const char *interface,
1328 _dbus_assert (message_type >= 0);
1329 _dbus_assert (message_type < DBUS_NUM_MESSAGE_TYPES);
1331 _dbus_verbose ("Looking up rules for message_type %d, interface %s\n",
1333 interface != NULL ? interface : "<null>");
1335 p = matchmaker->rules_by_type + message_type;
1337 if (interface == NULL)
1339 return &p->rules_without_iface;
1345 list = _dbus_hash_table_lookup_string (p->rules_by_iface, interface);
1347 if (list == NULL && create)
1349 char *dupped_interface;
1351 list = dbus_new0 (DBusList *, 1);
1355 dupped_interface = _dbus_strdup (interface);
1356 if (dupped_interface == NULL)
1362 _dbus_verbose ("Adding list for type %d, iface %s\n", message_type,
1365 if (!_dbus_hash_table_insert_string (p->rules_by_iface,
1366 dupped_interface, list))
1369 dbus_free (dupped_interface);
1379 bus_matchmaker_gc_rules (BusMatchmaker *matchmaker,
1381 const char *interface,
1386 if (interface == NULL)
1392 _dbus_verbose ("GCing HT entry for message_type %u, interface %s\n",
1393 message_type, interface);
1395 p = matchmaker->rules_by_type + message_type;
1397 _dbus_assert (_dbus_hash_table_lookup_string (p->rules_by_iface, interface)
1400 _dbus_hash_table_remove_string (p->rules_by_iface, interface);
1404 bus_matchmaker_ref (BusMatchmaker *matchmaker)
1406 _dbus_assert (matchmaker->refcount > 0);
1408 matchmaker->refcount += 1;
1414 bus_matchmaker_unref (BusMatchmaker *matchmaker)
1416 _dbus_assert (matchmaker->refcount > 0);
1418 matchmaker->refcount -= 1;
1419 if (matchmaker->refcount == 0)
1423 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1425 RulePool *p = matchmaker->rules_by_type + i;
1427 _dbus_hash_table_unref (p->rules_by_iface);
1428 rule_list_free (&p->rules_without_iface);
1431 dbus_free (matchmaker);
1435 /* The rule can't be modified after it's added. */
1437 bus_matchmaker_add_rule (BusMatchmaker *matchmaker,
1442 _dbus_assert (bus_connection_is_active (rule->matches_go_to));
1444 _dbus_verbose ("Adding rule with message_type %d, interface %s\n",
1446 rule->interface != NULL ? rule->interface : "<null>");
1448 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1449 rule->interface, TRUE);
1454 if (!_dbus_list_append (rules, rule))
1457 if (!bus_connection_add_match_rule (rule->matches_go_to, rule))
1459 _dbus_list_remove_last (rules, rule);
1460 bus_matchmaker_gc_rules (matchmaker, rule->message_type,
1461 rule->interface, rules);
1465 bus_match_rule_ref (rule);
1467 #ifdef DBUS_ENABLE_VERBOSE_MODE
1469 char *s = match_rule_to_string (rule);
1471 _dbus_verbose ("Added match rule %s to connection %p\n",
1472 s ? s : "nomem", rule->matches_go_to);
1481 match_rule_equal (BusMatchRule *a,
1484 if (a->flags != b->flags)
1487 if (a->matches_go_to != b->matches_go_to)
1490 if ((a->flags & BUS_MATCH_MESSAGE_TYPE) &&
1491 a->message_type != b->message_type)
1494 if ((a->flags & BUS_MATCH_MEMBER) &&
1495 strcmp (a->member, b->member) != 0)
1498 if ((a->flags & BUS_MATCH_PATH) &&
1499 strcmp (a->path, b->path) != 0)
1502 if ((a->flags & BUS_MATCH_INTERFACE) &&
1503 strcmp (a->interface, b->interface) != 0)
1506 if ((a->flags & BUS_MATCH_SENDER) &&
1507 strcmp (a->sender, b->sender) != 0)
1510 if ((a->flags & BUS_MATCH_DESTINATION) &&
1511 strcmp (a->destination, b->destination) != 0)
1514 /* we already compared the value of flags, and
1515 * BUS_MATCH_CLIENT_IS_EAVESDROPPING does not have another struct member */
1517 if (a->flags & BUS_MATCH_ARGS)
1521 if (a->args_len != b->args_len)
1525 while (i < a->args_len)
1529 if ((a->args[i] != NULL) != (b->args[i] != NULL))
1532 if (a->arg_lens[i] != b->arg_lens[i])
1535 length = a->arg_lens[i] & ~BUS_MATCH_ARG_FLAGS;
1537 if (a->args[i] != NULL)
1539 _dbus_assert (b->args[i] != NULL);
1540 if (memcmp (a->args[i], b->args[i], length) != 0)
1552 bus_matchmaker_remove_rule_link (DBusList **rules,
1555 BusMatchRule *rule = link->data;
1557 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1558 _dbus_list_remove_link (rules, link);
1560 #ifdef DBUS_ENABLE_VERBOSE_MODE
1562 char *s = match_rule_to_string (rule);
1564 _dbus_verbose ("Removed match rule %s for connection %p\n",
1565 s ? s : "nomem", rule->matches_go_to);
1570 bus_match_rule_unref (rule);
1574 bus_matchmaker_remove_rule (BusMatchmaker *matchmaker,
1579 _dbus_verbose ("Removing rule with message_type %d, interface %s\n",
1581 rule->interface != NULL ? rule->interface : "<null>");
1583 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1585 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1586 rule->interface, FALSE);
1588 /* We should only be asked to remove a rule by identity right after it was
1589 * added, so there should be a list for it.
1591 _dbus_assert (rules != NULL);
1593 _dbus_list_remove (rules, rule);
1594 bus_matchmaker_gc_rules (matchmaker, rule->message_type, rule->interface,
1597 #ifdef DBUS_ENABLE_VERBOSE_MODE
1599 char *s = match_rule_to_string (rule);
1601 _dbus_verbose ("Removed match rule %s for connection %p\n",
1602 s ? s : "nomem", rule->matches_go_to);
1607 bus_match_rule_unref (rule);
1610 /* Remove a single rule which is equal to the given rule by value */
1612 bus_matchmaker_remove_rule_by_value (BusMatchmaker *matchmaker,
1613 BusMatchRule *value,
1617 DBusList *link = NULL;
1619 _dbus_verbose ("Removing rule by value with message_type %d, interface %s\n",
1620 value->message_type,
1621 value->interface != NULL ? value->interface : "<null>");
1623 rules = bus_matchmaker_get_rules (matchmaker, value->message_type,
1624 value->interface, FALSE);
1628 /* we traverse backward because bus_connection_remove_match_rule()
1629 * removes the most-recently-added rule
1631 link = _dbus_list_get_last_link (rules);
1632 while (link != NULL)
1638 prev = _dbus_list_get_prev_link (rules, link);
1640 if (match_rule_equal (rule, value))
1642 bus_matchmaker_remove_rule_link (rules, link);
1652 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_NOT_FOUND,
1653 "The given match rule wasn't found and can't be removed");
1657 bus_matchmaker_gc_rules (matchmaker, value->message_type, value->interface,
1664 rule_list_remove_by_connection (DBusList **rules,
1665 DBusConnection *connection)
1669 link = _dbus_list_get_first_link (rules);
1670 while (link != NULL)
1676 next = _dbus_list_get_next_link (rules, link);
1678 if (rule->matches_go_to == connection)
1680 bus_matchmaker_remove_rule_link (rules, link);
1682 else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') ||
1683 ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':'))
1685 /* The rule matches to/from a base service, see if it's the
1686 * one being disconnected, since we know this service name
1687 * will never be recycled.
1691 name = bus_connection_get_name (connection);
1692 _dbus_assert (name != NULL); /* because we're an active connection */
1694 if (((rule->flags & BUS_MATCH_SENDER) &&
1695 strcmp (rule->sender, name) == 0) ||
1696 ((rule->flags & BUS_MATCH_DESTINATION) &&
1697 strcmp (rule->destination, name) == 0))
1699 bus_matchmaker_remove_rule_link (rules, link);
1708 bus_matchmaker_disconnected (BusMatchmaker *matchmaker,
1709 DBusConnection *connection)
1715 * This scans all match rules on the bus. We could avoid that
1716 * for the rules belonging to the connection, since we keep
1717 * a list of those; but for the rules that just refer to
1718 * the connection we'd need to do something more elaborate.
1721 _dbus_assert (bus_connection_is_active (connection));
1723 _dbus_verbose ("Removing all rules for connection %p\n", connection);
1725 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1727 RulePool *p = matchmaker->rules_by_type + i;
1730 rule_list_remove_by_connection (&p->rules_without_iface, connection);
1732 _dbus_hash_iter_init (p->rules_by_iface, &iter);
1733 while (_dbus_hash_iter_next (&iter))
1735 DBusList **items = _dbus_hash_iter_get_value (&iter);
1737 rule_list_remove_by_connection (items, connection);
1740 _dbus_hash_iter_remove_entry (&iter);
1746 connection_is_primary_owner (DBusConnection *connection,
1747 const char *service_name)
1749 BusService *service;
1751 BusRegistry *registry;
1753 _dbus_assert (connection != NULL);
1755 registry = bus_connection_get_registry (connection);
1757 _dbus_string_init_const (&str, service_name);
1758 service = bus_registry_lookup (registry, &str);
1760 if (service == NULL)
1761 return FALSE; /* Service doesn't exist so connection can't own it. */
1763 return bus_service_get_primary_owners_connection (service) == connection;
1767 str_has_prefix (const char *str, const char *prefix)
1770 prefix_len = strlen (prefix);
1771 if (strncmp (str, prefix, prefix_len) == 0)
1778 match_rule_matches (BusMatchRule *rule,
1779 DBusConnection *sender,
1780 DBusConnection *addressed_recipient,
1781 DBusMessage *message,
1782 BusMatchFlags already_matched)
1784 dbus_bool_t wants_to_eavesdrop = FALSE;
1787 /* All features of the match rule are AND'd together,
1788 * so FALSE if any of them don't match.
1791 /* sender/addressed_recipient of #NULL may mean bus driver,
1792 * or for addressed_recipient may mean a message with no
1793 * specific recipient (i.e. a signal)
1796 /* Don't bother re-matching features we've already checked implicitly. */
1797 flags = rule->flags & (~already_matched);
1799 if (flags & BUS_MATCH_CLIENT_IS_EAVESDROPPING)
1800 wants_to_eavesdrop = TRUE;
1802 if (flags & BUS_MATCH_MESSAGE_TYPE)
1804 _dbus_assert (rule->message_type != DBUS_MESSAGE_TYPE_INVALID);
1806 if (rule->message_type != dbus_message_get_type (message))
1810 if (flags & BUS_MATCH_INTERFACE)
1814 _dbus_assert (rule->interface != NULL);
1816 iface = dbus_message_get_interface (message);
1820 if (strcmp (iface, rule->interface) != 0)
1824 if (flags & BUS_MATCH_MEMBER)
1828 _dbus_assert (rule->member != NULL);
1830 member = dbus_message_get_member (message);
1834 if (strcmp (member, rule->member) != 0)
1838 if (flags & BUS_MATCH_SENDER)
1840 _dbus_assert (rule->sender != NULL);
1844 if (strcmp (rule->sender,
1845 DBUS_SERVICE_DBUS) != 0)
1850 if (!connection_is_primary_owner (sender, rule->sender))
1855 /* Note: this part is relevant for eavesdropper rules:
1857 * 1) rule has a destination to be matched
1858 * (flag BUS_MATCH_DESTINATION present). Rule will match if:
1859 * - rule->destination matches the addressed_recipient
1861 * - wants_to_eavesdrop=TRUE
1863 * Note: (the case in which addressed_recipient is the actual rule owner
1864 * is handled elsewere in dispatch.c:bus_dispatch_matches().
1866 * 2) rule has no destination. Rule will match if:
1867 * - message has no specified destination (ie broadcasts)
1868 * (Note: this will rule out unicast method calls and unicast signals,
1869 * fixing FDO#269748)
1871 * - wants_to_eavesdrop=TRUE (destination-catch-all situation)
1873 if (flags & BUS_MATCH_DESTINATION)
1875 const char *destination;
1877 _dbus_assert (rule->destination != NULL);
1879 destination = dbus_message_get_destination (message);
1880 if (destination == NULL)
1881 /* broadcast, but this rule specified a destination: no match */
1884 /* rule owner does not intend to eavesdrop: we'll deliver only msgs
1885 * directed to it, NOT MATCHING */
1886 if (!wants_to_eavesdrop)
1889 if (addressed_recipient == NULL)
1891 /* If the message is going to be delivered to the dbus-daemon
1892 * itself, its destination will be "org.freedesktop.DBus",
1893 * which we again match against the rule (see bus_dispatch()
1894 * in bus/dispatch.c, which checks for o.fd.DBus first).
1896 * If we are monitoring and we don't know who is going to receive
1897 * the message (for instance because they haven't been activated yet),
1898 * assume they will own the requested destination name and no other,
1899 * and match the rule's destination against that.
1901 if (strcmp (rule->destination, destination) != 0)
1906 if (!connection_is_primary_owner (addressed_recipient, rule->destination))
1909 } else { /* no destination in rule */
1910 dbus_bool_t msg_is_broadcast;
1912 _dbus_assert (rule->destination == NULL);
1914 msg_is_broadcast = (dbus_message_get_destination (message) == NULL);
1916 if (!wants_to_eavesdrop && !msg_is_broadcast)
1919 /* if we are here rule owner intends to eavesdrop
1921 * message is being broadcasted */
1924 if (flags & BUS_MATCH_PATH)
1928 _dbus_assert (rule->path != NULL);
1930 path = dbus_message_get_path (message);
1934 if (strcmp (path, rule->path) != 0)
1938 if (flags & BUS_MATCH_PATH_NAMESPACE)
1943 _dbus_assert (rule->path != NULL);
1945 path = dbus_message_get_path (message);
1949 if (!str_has_prefix (path, rule->path))
1952 len = strlen (rule->path);
1954 /* Check that the actual argument is within the expected
1955 * namespace, rather than just starting with that string,
1956 * by checking that the matched prefix is followed by a '/'
1957 * or the end of the path.
1959 * Special case: the only valid path of length 1, "/",
1960 * matches everything.
1962 if (len > 1 && path[len] != '\0' && path[len] != '/')
1966 if (flags & BUS_MATCH_ARGS)
1969 DBusMessageIter iter;
1971 _dbus_assert (rule->args != NULL);
1973 dbus_message_iter_init (message, &iter);
1976 while (i < rule->args_len)
1979 const char *expected_arg;
1980 int expected_length;
1981 dbus_bool_t is_path, is_namespace;
1983 expected_arg = rule->args[i];
1984 expected_length = rule->arg_lens[i] & ~BUS_MATCH_ARG_FLAGS;
1985 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
1986 is_namespace = (rule->arg_lens[i] & BUS_MATCH_ARG_NAMESPACE) != 0;
1988 current_type = dbus_message_iter_get_arg_type (&iter);
1990 if (expected_arg != NULL)
1992 const char *actual_arg;
1995 if (current_type != DBUS_TYPE_STRING &&
1996 (!is_path || current_type != DBUS_TYPE_OBJECT_PATH))
2000 dbus_message_iter_get_basic (&iter, &actual_arg);
2001 _dbus_assert (actual_arg != NULL);
2003 actual_length = strlen (actual_arg);
2007 if (actual_length < expected_length &&
2008 actual_arg[actual_length - 1] != '/')
2011 if (expected_length < actual_length &&
2012 expected_arg[expected_length - 1] != '/')
2015 if (memcmp (actual_arg, expected_arg,
2016 MIN (actual_length, expected_length)) != 0)
2019 else if (is_namespace)
2021 if (expected_length > actual_length)
2024 /* If the actual argument doesn't start with the expected
2025 * namespace, then we don't match.
2027 if (memcmp (expected_arg, actual_arg, expected_length) != 0)
2030 if (expected_length < actual_length)
2032 /* Check that the actual argument is within the expected
2033 * namespace, rather than just starting with that string,
2034 * by checking that the matched prefix ends in a '.'.
2036 * This doesn't stop "foo.bar." matching "foo.bar..baz"
2037 * which is an invalid namespace, but at some point the
2038 * daemon can't cover up for broken services.
2040 if (actual_arg[expected_length] != '.')
2043 /* otherwise we had an exact match. */
2047 if (expected_length != actual_length ||
2048 memcmp (expected_arg, actual_arg, expected_length) != 0)
2054 if (current_type != DBUS_TYPE_INVALID)
2055 dbus_message_iter_next (&iter);
2065 get_recipients_from_list (DBusList **rules,
2066 DBusConnection *sender,
2067 DBusConnection *addressed_recipient,
2068 DBusMessage *message,
2069 DBusList **recipients_p)
2076 link = _dbus_list_get_first_link (rules);
2077 while (link != NULL)
2083 #ifdef DBUS_ENABLE_VERBOSE_MODE
2085 char *s = match_rule_to_string (rule);
2087 _dbus_verbose ("Checking whether message matches rule %s for connection %p\n",
2088 s ? s : "nomem", rule->matches_go_to);
2093 if (match_rule_matches (rule,
2094 sender, addressed_recipient, message,
2095 BUS_MATCH_MESSAGE_TYPE | BUS_MATCH_INTERFACE))
2097 _dbus_verbose ("Rule matched\n");
2099 /* Append to the list if we haven't already */
2100 if (bus_connection_mark_stamp (rule->matches_go_to))
2102 if (!_dbus_list_append (recipients_p, rule->matches_go_to))
2107 _dbus_verbose ("Connection already receiving this message, so not adding again\n");
2111 link = _dbus_list_get_next_link (rules, link);
2118 bus_matchmaker_get_recipients (BusMatchmaker *matchmaker,
2119 BusConnections *connections,
2120 DBusConnection *sender,
2121 DBusConnection *addressed_recipient,
2122 DBusMessage *message,
2123 DBusList **recipients_p)
2126 const char *interface;
2127 DBusList **neither, **just_type, **just_iface, **both;
2129 _dbus_assert (*recipients_p == NULL);
2131 /* This avoids sending same message to the same connection twice.
2132 * Purpose of the stamp instead of a bool is to avoid iterating over
2133 * all connections resetting the bool each time.
2135 bus_connections_increment_stamp (connections);
2137 /* addressed_recipient is already receiving the message, don't add to list.
2138 * NULL addressed_recipient means either bus driver, or this is a signal
2139 * and thus lacks a specific addressed_recipient.
2141 if (addressed_recipient != NULL)
2142 bus_connection_mark_stamp (addressed_recipient);
2144 type = dbus_message_get_type (message);
2145 interface = dbus_message_get_interface (message);
2147 neither = bus_matchmaker_get_rules (matchmaker, DBUS_MESSAGE_TYPE_INVALID,
2149 just_type = just_iface = both = NULL;
2151 if (interface != NULL)
2152 just_iface = bus_matchmaker_get_rules (matchmaker,
2153 DBUS_MESSAGE_TYPE_INVALID, interface, FALSE);
2155 if (type > DBUS_MESSAGE_TYPE_INVALID && type < DBUS_NUM_MESSAGE_TYPES)
2157 just_type = bus_matchmaker_get_rules (matchmaker, type, NULL, FALSE);
2159 if (interface != NULL)
2160 both = bus_matchmaker_get_rules (matchmaker, type, interface, FALSE);
2163 if (!(get_recipients_from_list (neither, sender, addressed_recipient,
2164 message, recipients_p) &&
2165 get_recipients_from_list (just_iface, sender, addressed_recipient,
2166 message, recipients_p) &&
2167 get_recipients_from_list (just_type, sender, addressed_recipient,
2168 message, recipients_p) &&
2169 get_recipients_from_list (both, sender, addressed_recipient,
2170 message, recipients_p)))
2172 _dbus_list_clear (recipients_p);
2179 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2183 static BusMatchRule*
2184 check_parse (dbus_bool_t should_succeed,
2191 dbus_error_init (&error);
2193 _dbus_string_init_const (&str, text);
2195 rule = bus_match_rule_parse (NULL, &str, &error);
2196 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2198 dbus_error_free (&error);
2202 if (should_succeed && rule == NULL)
2204 _dbus_warn ("Failed to parse: %s: %s: \"%s\"\n",
2205 error.name, error.message,
2206 _dbus_string_get_const_data (&str));
2210 if (!should_succeed && rule != NULL)
2212 _dbus_warn ("Failed to fail to parse: \"%s\"\n",
2213 _dbus_string_get_const_data (&str));
2217 dbus_error_free (&error);
2223 assert_large_rule (BusMatchRule *rule)
2225 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
2226 _dbus_assert (rule->flags & BUS_MATCH_SENDER);
2227 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
2228 _dbus_assert (rule->flags & BUS_MATCH_MEMBER);
2229 _dbus_assert (rule->flags & BUS_MATCH_DESTINATION);
2230 _dbus_assert (rule->flags & BUS_MATCH_PATH);
2232 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
2233 _dbus_assert (rule->interface != NULL);
2234 _dbus_assert (rule->member != NULL);
2235 _dbus_assert (rule->sender != NULL);
2236 _dbus_assert (rule->destination != NULL);
2237 _dbus_assert (rule->path != NULL);
2239 _dbus_assert (strcmp (rule->interface, "org.freedesktop.DBusInterface") == 0);
2240 _dbus_assert (strcmp (rule->sender, "org.freedesktop.DBusSender") == 0);
2241 _dbus_assert (strcmp (rule->member, "Foo") == 0);
2242 _dbus_assert (strcmp (rule->path, "/bar/foo") == 0);
2243 _dbus_assert (strcmp (rule->destination, ":452345.34") == 0);
2247 test_parsing (void *data)
2251 rule = check_parse (TRUE, "type='signal',sender='org.freedesktop.DBusSender',interface='org.freedesktop.DBusInterface',member='Foo',path='/bar/foo',destination=':452345.34'");
2254 assert_large_rule (rule);
2255 bus_match_rule_unref (rule);
2258 /* With extra whitespace and useless quotes */
2259 rule = check_parse (TRUE, " type='signal', \tsender='org.freedes''ktop.DBusSender', interface='org.freedesktop.DBusInterface''''', \tmember='Foo',path='/bar/foo',destination=':452345.34'''''");
2262 assert_large_rule (rule);
2263 bus_match_rule_unref (rule);
2267 /* A simple signal connection */
2268 rule = check_parse (TRUE, "type='signal',path='/foo',interface='org.Bar'");
2271 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
2272 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
2273 _dbus_assert (rule->flags & BUS_MATCH_PATH);
2275 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
2276 _dbus_assert (rule->interface != NULL);
2277 _dbus_assert (rule->path != NULL);
2279 _dbus_assert (strcmp (rule->interface, "org.Bar") == 0);
2280 _dbus_assert (strcmp (rule->path, "/foo") == 0);
2282 bus_match_rule_unref (rule);
2286 rule = check_parse (TRUE, "arg0='foo'");
2289 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2290 _dbus_assert (rule->args != NULL);
2291 _dbus_assert (rule->args_len == 1);
2292 _dbus_assert (rule->args[0] != NULL);
2293 _dbus_assert (rule->args[1] == NULL);
2294 _dbus_assert (strcmp (rule->args[0], "foo") == 0);
2296 bus_match_rule_unref (rule);
2299 rule = check_parse (TRUE, "arg1='foo'");
2302 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2303 _dbus_assert (rule->args != NULL);
2304 _dbus_assert (rule->args_len == 2);
2305 _dbus_assert (rule->args[0] == NULL);
2306 _dbus_assert (rule->args[1] != NULL);
2307 _dbus_assert (rule->args[2] == NULL);
2308 _dbus_assert (strcmp (rule->args[1], "foo") == 0);
2310 bus_match_rule_unref (rule);
2313 rule = check_parse (TRUE, "arg2='foo'");
2316 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2317 _dbus_assert (rule->args != NULL);
2318 _dbus_assert (rule->args_len == 3);
2319 _dbus_assert (rule->args[0] == NULL);
2320 _dbus_assert (rule->args[1] == NULL);
2321 _dbus_assert (rule->args[2] != NULL);
2322 _dbus_assert (rule->args[3] == NULL);
2323 _dbus_assert (strcmp (rule->args[2], "foo") == 0);
2325 bus_match_rule_unref (rule);
2328 rule = check_parse (TRUE, "arg40='foo'");
2331 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2332 _dbus_assert (rule->args != NULL);
2333 _dbus_assert (rule->args_len == 41);
2334 _dbus_assert (rule->args[0] == NULL);
2335 _dbus_assert (rule->args[1] == NULL);
2336 _dbus_assert (rule->args[40] != NULL);
2337 _dbus_assert (rule->args[41] == NULL);
2338 _dbus_assert (strcmp (rule->args[40], "foo") == 0);
2340 bus_match_rule_unref (rule);
2343 rule = check_parse (TRUE, "arg63='foo'");
2346 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2347 _dbus_assert (rule->args != NULL);
2348 _dbus_assert (rule->args_len == 64);
2349 _dbus_assert (rule->args[0] == NULL);
2350 _dbus_assert (rule->args[1] == NULL);
2351 _dbus_assert (rule->args[63] != NULL);
2352 _dbus_assert (rule->args[64] == NULL);
2353 _dbus_assert (strcmp (rule->args[63], "foo") == 0);
2355 bus_match_rule_unref (rule);
2358 rule = check_parse (TRUE, "arg7path='/foo'");
2361 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2362 _dbus_assert (rule->args != NULL);
2363 _dbus_assert (rule->args_len == 8);
2364 _dbus_assert (rule->args[7] != NULL);
2365 _dbus_assert (rule->args[8] == NULL);
2366 _dbus_assert (strcmp (rule->args[7], "/foo") == 0);
2367 _dbus_assert ((rule->arg_lens[7] & BUS_MATCH_ARG_IS_PATH)
2368 == BUS_MATCH_ARG_IS_PATH);
2370 bus_match_rule_unref (rule);
2373 /* Arg 0 namespace matches */
2374 rule = check_parse (TRUE, "arg0namespace='foo'");
2377 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2378 _dbus_assert (rule->args != NULL);
2379 _dbus_assert (rule->args_len == 1);
2380 _dbus_assert (strcmp (rule->args[0], "foo") == 0);
2381 _dbus_assert ((rule->arg_lens[0] & BUS_MATCH_ARG_NAMESPACE)
2382 == BUS_MATCH_ARG_NAMESPACE);
2384 bus_match_rule_unref (rule);
2387 rule = check_parse (TRUE, "arg0namespace='foo.bar'");
2390 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2391 _dbus_assert (rule->args != NULL);
2392 _dbus_assert (rule->args_len == 1);
2393 _dbus_assert (strcmp (rule->args[0], "foo.bar") == 0);
2394 _dbus_assert ((rule->arg_lens[0] & BUS_MATCH_ARG_NAMESPACE)
2395 == BUS_MATCH_ARG_NAMESPACE);
2397 bus_match_rule_unref (rule);
2400 /* Only arg0namespace is supported. */
2401 rule = check_parse (FALSE, "arg1namespace='foo'");
2402 _dbus_assert (rule == NULL);
2404 /* An empty string isn't a valid namespace prefix (you should just not
2405 * specify this key at all).
2407 rule = check_parse (FALSE, "arg0namespace=''");
2408 _dbus_assert (rule == NULL);
2410 /* Trailing periods aren't allowed (earlier versions of the arg0namespace
2411 * spec allowed a single trailing period, which altered the semantics) */
2412 rule = check_parse (FALSE, "arg0namespace='foo.'");
2413 _dbus_assert (rule == NULL);
2415 rule = check_parse (FALSE, "arg0namespace='foo.bar.'");
2416 _dbus_assert (rule == NULL);
2418 rule = check_parse (FALSE, "arg0namespace='foo..'");
2419 _dbus_assert (rule == NULL);
2421 rule = check_parse (FALSE, "arg0namespace='foo.bar..'");
2422 _dbus_assert (rule == NULL);
2424 /* Too-large argN */
2425 rule = check_parse (FALSE, "arg300='foo'");
2426 _dbus_assert (rule == NULL);
2427 rule = check_parse (FALSE, "arg64='foo'");
2428 _dbus_assert (rule == NULL);
2431 rule = check_parse (FALSE, "arg='foo'");
2432 _dbus_assert (rule == NULL);
2433 rule = check_parse (FALSE, "argv='foo'");
2434 _dbus_assert (rule == NULL);
2435 rule = check_parse (FALSE, "arg3junk='foo'");
2436 _dbus_assert (rule == NULL);
2437 rule = check_parse (FALSE, "argument='foo'");
2438 _dbus_assert (rule == NULL);
2440 /* Reject duplicates */
2441 rule = check_parse (FALSE, "type='signal',type='method_call'");
2442 _dbus_assert (rule == NULL);
2444 rule = check_parse (TRUE, "path_namespace='/foo/bar'");
2447 _dbus_assert (rule->flags == BUS_MATCH_PATH_NAMESPACE);
2448 _dbus_assert (rule->path != NULL);
2449 _dbus_assert (strcmp (rule->path, "/foo/bar") == 0);
2451 bus_match_rule_unref (rule);
2454 /* Almost a duplicate */
2455 rule = check_parse (FALSE, "path='/foo',path_namespace='/foo'");
2456 _dbus_assert (rule == NULL);
2458 /* Trailing / was supported in the initial proposal, but now isn't */
2459 rule = check_parse (FALSE, "path_namespace='/foo/'");
2460 _dbus_assert (rule == NULL);
2462 /* Duplicates with the argN code */
2463 rule = check_parse (FALSE, "arg0='foo',arg0='bar'");
2464 _dbus_assert (rule == NULL);
2465 rule = check_parse (FALSE, "arg3='foo',arg3='bar'");
2466 _dbus_assert (rule == NULL);
2467 rule = check_parse (FALSE, "arg30='foo',arg30='bar'");
2468 _dbus_assert (rule == NULL);
2470 /* Reject broken keys */
2471 rule = check_parse (FALSE, "blah='signal'");
2472 _dbus_assert (rule == NULL);
2474 /* Reject broken values */
2475 rule = check_parse (FALSE, "type='chouin'");
2476 _dbus_assert (rule == NULL);
2477 rule = check_parse (FALSE, "interface='abc@def++'");
2478 _dbus_assert (rule == NULL);
2479 rule = check_parse (FALSE, "service='youpi'");
2480 _dbus_assert (rule == NULL);
2482 /* Allow empty rule */
2483 rule = check_parse (TRUE, "");
2486 _dbus_assert (rule->flags == 0);
2488 bus_match_rule_unref (rule);
2491 /* All-whitespace rule is the same as empty */
2492 rule = check_parse (TRUE, " \t");
2495 _dbus_assert (rule->flags == 0);
2497 bus_match_rule_unref (rule);
2500 /* But with non-whitespace chars and no =value, it's not OK */
2501 rule = check_parse (FALSE, "type");
2502 _dbus_assert (rule == NULL);
2510 } equality_tests[] = {
2511 { "type='signal'", "type='signal'" },
2512 { "type='signal',interface='foo.bar'", "interface='foo.bar',type='signal'" },
2513 { "type='signal',member='bar'", "member='bar',type='signal'" },
2514 { "type='method_call',sender=':1.0'", "sender=':1.0',type='method_call'" },
2515 { "type='method_call',destination=':1.0'", "destination=':1.0',type='method_call'" },
2516 { "type='method_call',path='/foo/bar'", "path='/foo/bar',type='method_call'" },
2517 { "type='method_call',arg0='blah'", "arg0='blah',type='method_call'" },
2518 { "type='method_call',arg0='boo'", "arg0='boo',type='method_call'" },
2519 { "type='method_call',arg0='blah',arg1='baz'", "arg0='blah',arg1='baz',type='method_call'" },
2520 { "type='method_call',arg3='foosh'", "arg3='foosh',type='method_call'" },
2521 { "arg3='fool'", "arg3='fool'" },
2522 { "arg0namespace='fool'", "arg0namespace='fool'" },
2523 { "member='food'", "member='food'" },
2524 { "member=escape", "member='escape'" },
2525 { "member=icecream", "member=ice'cream'" },
2526 { "arg0='comma,type=comma',type=signal", "type=signal,arg0='comma,type=comma'" },
2527 { "arg0=escap\\e", "arg0='escap\\e'" },
2528 { "arg0=Time: 8 o\\'clock", "arg0='Time: 8 o'\\''clock'" },
2532 test_equality (void)
2537 while (i < _DBUS_N_ELEMENTS (equality_tests))
2539 BusMatchRule *first;
2540 BusMatchRule *second;
2541 char *first_str, *second_str;
2542 BusMatchRule *first_reparsed, *second_reparsed;
2545 first = check_parse (TRUE, equality_tests[i].first);
2546 _dbus_assert (first != NULL);
2547 second = check_parse (TRUE, equality_tests[i].second);
2548 _dbus_assert (second != NULL);
2550 if (!match_rule_equal (first, second))
2552 _dbus_warn ("rule %s and %s should have been equal\n",
2553 equality_tests[i].first,
2554 equality_tests[i].second);
2558 /* Check match_rule_to_string */
2559 first_str = match_rule_to_string (first);
2560 _dbus_assert (first_str != NULL);
2561 second_str = match_rule_to_string (second);
2562 _dbus_assert (second_str != NULL);
2563 _dbus_assert (strcmp (first_str, second_str) == 0);
2564 first_reparsed = check_parse (TRUE, first_str);
2565 second_reparsed = check_parse (TRUE, second_str);
2566 _dbus_assert (match_rule_equal (first, first_reparsed));
2567 _dbus_assert (match_rule_equal (second, second_reparsed));
2568 bus_match_rule_unref (first_reparsed);
2569 bus_match_rule_unref (second_reparsed);
2570 dbus_free (first_str);
2571 dbus_free (second_str);
2573 bus_match_rule_unref (second);
2575 /* Check that the rule is not equal to any of the
2576 * others besides its pair match
2579 while (j < _DBUS_N_ELEMENTS (equality_tests))
2583 second = check_parse (TRUE, equality_tests[j].second);
2585 if (match_rule_equal (first, second))
2587 _dbus_warn ("rule %s and %s should not have been equal\n",
2588 equality_tests[i].first,
2589 equality_tests[j].second);
2593 bus_match_rule_unref (second);
2599 bus_match_rule_unref (first);
2606 should_match_message_1[] = {
2608 "member='Frobated'",
2610 "type='signal',member='Frobated'",
2611 "type='signal',member='Frobated',arg0='foobar'",
2612 "member='Frobated',arg0='foobar'",
2613 "type='signal',arg0='foobar'",
2614 /* The definition of argXpath matches says: "As with normal argument matches,
2615 * if the argument is exactly equal to the string given in the match rule
2616 * then the rule is satisfied." So this should match (even though the
2617 * argument is not a valid path)!
2619 "arg0path='foobar'",
2620 "arg0namespace='foobar'",
2625 should_not_match_message_1[] = {
2626 "type='method_call'",
2628 "type='method_return'",
2629 "type='signal',member='Oopsed'",
2636 "arg0='foobar',arg1='abcdef'",
2637 "arg0='foobar',arg1='abcdef',arg2='abcdefghi',arg3='abcdefghi',arg4='abcdefghi'",
2638 "arg0='foobar',arg1='abcdef',arg4='abcdefghi',arg3='abcdefghi',arg2='abcdefghi'",
2640 "arg0path='foobar/'",
2642 "arg0namespace='foo'",
2643 "arg0namespace='foo',arg1='abcdef'",
2644 "arg0namespace='moo'",
2648 #define EXAMPLE_NAME "com.example.backend.foo"
2651 should_match_message_2[] = {
2652 /* EXAMPLE_NAME is in all of these namespaces */
2653 "arg0namespace='com.example.backend'",
2654 "arg0namespace='com.example'",
2655 "arg0namespace='com'",
2657 /* If the client specifies the name exactly, with no trailing period, then
2660 "arg0namespace='com.example.backend.foo'",
2666 should_not_match_message_2[] = {
2667 /* These are not even prefixes */
2668 "arg0namespace='com.example.backend.foo.bar'",
2669 "arg0namespace='com.example.backend.foobar'",
2671 /* These are prefixes, but they're not parent namespaces. */
2672 "arg0namespace='com.example.backend.fo'",
2673 "arg0namespace='com.example.backen'",
2674 "arg0namespace='com.exampl'",
2675 "arg0namespace='co'",
2681 check_matches (dbus_bool_t expected_to_match,
2683 DBusMessage *message,
2684 const char *rule_text)
2687 dbus_bool_t matched;
2689 rule = check_parse (TRUE, rule_text);
2690 _dbus_assert (rule != NULL);
2692 /* We can't test sender/destination rules since we pass NULL here */
2693 matched = match_rule_matches (rule, NULL, NULL, message, 0);
2695 if (matched != expected_to_match)
2697 _dbus_warn ("Expected rule %s to %s message %d, failed\n",
2698 rule_text, expected_to_match ?
2699 "match" : "not match", number);
2703 bus_match_rule_unref (rule);
2707 check_matching (DBusMessage *message,
2709 const char **should_match,
2710 const char **should_not_match)
2715 while (should_match[i] != NULL)
2717 check_matches (TRUE, number, message, should_match[i]);
2722 while (should_not_match[i] != NULL)
2724 check_matches (FALSE, number, message, should_not_match[i]);
2730 test_matching (void)
2732 DBusMessage *message1, *message2;
2733 const char *v_STRING;
2734 dbus_int32_t v_INT32;
2736 message1 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2737 _dbus_assert (message1 != NULL);
2738 if (!dbus_message_set_member (message1, "Frobated"))
2739 _dbus_assert_not_reached ("oom");
2741 v_STRING = "foobar";
2743 if (!dbus_message_append_args (message1,
2744 DBUS_TYPE_STRING, &v_STRING,
2745 DBUS_TYPE_INT32, &v_INT32,
2747 _dbus_assert_not_reached ("oom");
2749 check_matching (message1, 1,
2750 should_match_message_1,
2751 should_not_match_message_1);
2753 dbus_message_unref (message1);
2755 message2 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2756 _dbus_assert (message2 != NULL);
2757 if (!dbus_message_set_member (message2, "NameOwnerChanged"))
2758 _dbus_assert_not_reached ("oom");
2760 /* Obviously this isn't really a NameOwnerChanged signal. */
2761 v_STRING = EXAMPLE_NAME;
2762 if (!dbus_message_append_args (message2,
2763 DBUS_TYPE_STRING, &v_STRING,
2765 _dbus_assert_not_reached ("oom");
2767 check_matching (message2, 2,
2768 should_match_message_2,
2769 should_not_match_message_2);
2771 dbus_message_unref (message2);
2774 #define PATH_MATCH_RULE "arg0path='/aa/bb/'"
2776 /* This is a list of paths that should be matched by PATH_MATCH_RULE, taken
2777 * from the specification. Notice that not all of them are actually legal D-Bus
2780 * The author of this test takes no responsibility for the semantics of
2781 * this match rule key.
2783 static const char *paths_that_should_be_matched[] = {
2787 #define FIRST_VALID_PATH_WHICH_SHOULD_MATCH 3
2793 /* These paths should not be matched by PATH_MATCH_RULE. */
2794 static const char *paths_that_should_not_be_matched[] = {
2803 test_path_match (int type,
2805 const char *rule_text,
2807 dbus_bool_t should_match)
2809 DBusMessage *message = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2810 dbus_bool_t matched;
2812 _dbus_assert (message != NULL);
2813 if (!dbus_message_set_member (message, "Foo"))
2814 _dbus_assert_not_reached ("oom");
2816 if (!dbus_message_append_args (message,
2819 _dbus_assert_not_reached ("oom");
2821 matched = match_rule_matches (rule, NULL, NULL, message, 0);
2823 if (matched != should_match)
2825 _dbus_warn ("Expected rule %s to %s message "
2826 "with first arg %s of type '%c', failed\n",
2828 should_match ? "match" : "not match",
2834 dbus_message_unref (message);
2838 test_path_matching (void)
2843 rule = check_parse (TRUE, PATH_MATCH_RULE);
2844 _dbus_assert (rule != NULL);
2846 for (s = paths_that_should_be_matched; *s != NULL; s++)
2847 test_path_match (DBUS_TYPE_STRING, *s, PATH_MATCH_RULE, rule, TRUE);
2849 for (s = paths_that_should_be_matched + FIRST_VALID_PATH_WHICH_SHOULD_MATCH;
2851 test_path_match (DBUS_TYPE_OBJECT_PATH, *s, PATH_MATCH_RULE, rule, TRUE);
2853 for (s = paths_that_should_not_be_matched; *s != NULL; s++)
2855 test_path_match (DBUS_TYPE_STRING, *s, PATH_MATCH_RULE, rule, FALSE);
2856 test_path_match (DBUS_TYPE_OBJECT_PATH, *s, PATH_MATCH_RULE, rule, FALSE);
2859 bus_match_rule_unref (rule);
2863 path_namespace_should_match_message_1[] = {
2864 "type='signal',path_namespace='/'",
2865 "type='signal',path_namespace='/foo'",
2866 "type='signal',path_namespace='/foo/TheObjectManager'",
2871 path_namespace_should_not_match_message_1[] = {
2872 "type='signal',path_namespace='/bar'",
2873 "type='signal',path_namespace='/bar/TheObjectManager'",
2878 path_namespace_should_match_message_2[] = {
2879 "type='signal',path_namespace='/'",
2880 "type='signal',path_namespace='/foo/TheObjectManager'",
2885 path_namespace_should_not_match_message_2[] = {
2890 path_namespace_should_match_message_3[] = {
2891 "type='signal',path_namespace='/'",
2896 path_namespace_should_not_match_message_3[] = {
2897 "type='signal',path_namespace='/foo/TheObjectManager'",
2902 path_namespace_should_match_message_4[] = {
2903 "type='signal',path_namespace='/'",
2908 path_namespace_should_not_match_message_4[] = {
2909 "type='signal',path_namespace='/foo/TheObjectManager'",
2914 test_matching_path_namespace (void)
2916 DBusMessage *message1;
2917 DBusMessage *message2;
2918 DBusMessage *message3;
2919 DBusMessage *message4;
2921 message1 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2922 _dbus_assert (message1 != NULL);
2923 if (!dbus_message_set_path (message1, "/foo/TheObjectManager"))
2924 _dbus_assert_not_reached ("oom");
2926 message2 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2927 _dbus_assert (message2 != NULL);
2928 if (!dbus_message_set_path (message2, "/foo/TheObjectManager/child_object"))
2929 _dbus_assert_not_reached ("oom");
2931 message3 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2932 _dbus_assert (message3 != NULL);
2933 if (!dbus_message_set_path (message3, "/foo/TheObjectManagerOther"))
2934 _dbus_assert_not_reached ("oom");
2936 message4 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2937 _dbus_assert (message4 != NULL);
2938 if (!dbus_message_set_path (message4, "/"))
2939 _dbus_assert_not_reached ("oom");
2941 check_matching (message1, 1,
2942 path_namespace_should_match_message_1,
2943 path_namespace_should_not_match_message_1);
2944 check_matching (message2, 2,
2945 path_namespace_should_match_message_2,
2946 path_namespace_should_not_match_message_2);
2947 check_matching (message3, 3,
2948 path_namespace_should_match_message_3,
2949 path_namespace_should_not_match_message_3);
2950 check_matching (message4, 4,
2951 path_namespace_should_match_message_4,
2952 path_namespace_should_not_match_message_4);
2954 dbus_message_unref (message4);
2955 dbus_message_unref (message3);
2956 dbus_message_unref (message2);
2957 dbus_message_unref (message1);
2961 bus_signals_test (const DBusString *test_data_dir)
2963 BusMatchmaker *matchmaker;
2965 matchmaker = bus_matchmaker_new ();
2966 bus_matchmaker_ref (matchmaker);
2967 bus_matchmaker_unref (matchmaker);
2968 bus_matchmaker_unref (matchmaker);
2970 if (!_dbus_test_oom_handling ("parsing match rules", test_parsing, NULL))
2971 _dbus_assert_not_reached ("Parsing match rules test failed");
2975 test_path_matching ();
2976 test_matching_path_namespace ();
2981 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */