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
26 #include <dbus/dbus-marshal-validate.h>
30 int refcount; /**< reference count */
32 DBusConnection *matches_go_to; /**< Owner of the rule */
34 unsigned int flags; /**< BusMatchFlags */
43 unsigned int *arg_lens;
48 #define BUS_MATCH_ARG_IS_PATH 0x8000000u
51 bus_match_rule_new (DBusConnection *matches_go_to)
55 rule = dbus_new0 (BusMatchRule, 1);
60 rule->matches_go_to = matches_go_to;
62 #ifndef DBUS_BUILD_TESTS
63 _dbus_assert (rule->matches_go_to != NULL);
70 bus_match_rule_ref (BusMatchRule *rule)
72 _dbus_assert (rule->refcount > 0);
80 bus_match_rule_unref (BusMatchRule *rule)
82 _dbus_assert (rule->refcount > 0);
85 if (rule->refcount == 0)
87 dbus_free (rule->interface);
88 dbus_free (rule->member);
89 dbus_free (rule->sender);
90 dbus_free (rule->destination);
91 dbus_free (rule->path);
92 dbus_free (rule->arg_lens);
94 /* can't use dbus_free_string_array() since there
102 while (i < rule->args_len)
105 dbus_free (rule->args[i]);
109 dbus_free (rule->args);
116 #ifdef DBUS_ENABLE_VERBOSE_MODE
117 /* Note this function does not do escaping, so it's only
118 * good for debug spew at the moment
121 match_rule_to_string (BusMatchRule *rule)
126 if (!_dbus_string_init (&str))
129 while ((s = _dbus_strdup ("nomem")) == NULL)
130 ; /* only OK for debug spew... */
134 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
136 /* FIXME make type readable */
137 if (!_dbus_string_append_printf (&str, "type='%d'", rule->message_type))
141 if (rule->flags & BUS_MATCH_INTERFACE)
143 if (_dbus_string_get_length (&str) > 0)
145 if (!_dbus_string_append (&str, ","))
149 if (!_dbus_string_append_printf (&str, "interface='%s'", rule->interface))
153 if (rule->flags & BUS_MATCH_MEMBER)
155 if (_dbus_string_get_length (&str) > 0)
157 if (!_dbus_string_append (&str, ","))
161 if (!_dbus_string_append_printf (&str, "member='%s'", rule->member))
165 if (rule->flags & BUS_MATCH_PATH)
167 if (_dbus_string_get_length (&str) > 0)
169 if (!_dbus_string_append (&str, ","))
173 if (!_dbus_string_append_printf (&str, "path='%s'", rule->path))
177 if (rule->flags & BUS_MATCH_SENDER)
179 if (_dbus_string_get_length (&str) > 0)
181 if (!_dbus_string_append (&str, ","))
185 if (!_dbus_string_append_printf (&str, "sender='%s'", rule->sender))
189 if (rule->flags & BUS_MATCH_DESTINATION)
191 if (_dbus_string_get_length (&str) > 0)
193 if (!_dbus_string_append (&str, ","))
197 if (!_dbus_string_append_printf (&str, "destination='%s'", rule->destination))
201 if (rule->flags & BUS_MATCH_ARGS)
205 _dbus_assert (rule->args != NULL);
208 while (i < rule->args_len)
210 if (rule->args[i] != NULL)
214 if (_dbus_string_get_length (&str) > 0)
216 if (!_dbus_string_append (&str, ","))
220 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
222 if (!_dbus_string_append_printf (&str,
224 i, is_path ? "path" : "",
233 if (!_dbus_string_steal_data (&str, &ret))
236 _dbus_string_free (&str);
240 _dbus_string_free (&str);
243 while ((s = _dbus_strdup ("nomem")) == NULL)
244 ; /* only OK for debug spew... */
248 #endif /* DBUS_ENABLE_VERBOSE_MODE */
251 bus_match_rule_set_message_type (BusMatchRule *rule,
254 rule->flags |= BUS_MATCH_MESSAGE_TYPE;
256 rule->message_type = type;
262 bus_match_rule_set_interface (BusMatchRule *rule,
263 const char *interface)
267 _dbus_assert (interface != NULL);
269 new = _dbus_strdup (interface);
273 rule->flags |= BUS_MATCH_INTERFACE;
274 dbus_free (rule->interface);
275 rule->interface = new;
281 bus_match_rule_set_member (BusMatchRule *rule,
286 _dbus_assert (member != NULL);
288 new = _dbus_strdup (member);
292 rule->flags |= BUS_MATCH_MEMBER;
293 dbus_free (rule->member);
300 bus_match_rule_set_sender (BusMatchRule *rule,
305 _dbus_assert (sender != NULL);
307 new = _dbus_strdup (sender);
311 rule->flags |= BUS_MATCH_SENDER;
312 dbus_free (rule->sender);
319 bus_match_rule_set_destination (BusMatchRule *rule,
320 const char *destination)
324 _dbus_assert (destination != NULL);
326 new = _dbus_strdup (destination);
330 rule->flags |= BUS_MATCH_DESTINATION;
331 dbus_free (rule->destination);
332 rule->destination = new;
338 bus_match_rule_set_path (BusMatchRule *rule,
343 _dbus_assert (path != NULL);
345 new = _dbus_strdup (path);
349 rule->flags |= BUS_MATCH_PATH;
350 dbus_free (rule->path);
357 bus_match_rule_set_arg (BusMatchRule *rule,
359 const DBusString *value,
365 _dbus_assert (value != NULL);
367 /* args_len is the number of args not including null termination
370 if (arg >= rule->args_len)
372 unsigned int *new_arg_lens;
377 new_args_len = arg + 1;
379 /* add another + 1 here for null termination */
380 new_args = dbus_realloc (rule->args,
381 sizeof (char *) * (new_args_len + 1));
382 if (new_args == NULL)
385 /* NULL the new slots */
387 while (i <= new_args_len) /* <= for null termination */
393 rule->args = new_args;
395 /* and now add to the lengths */
396 new_arg_lens = dbus_realloc (rule->arg_lens,
397 sizeof (int) * (new_args_len + 1));
399 if (new_arg_lens == NULL)
402 /* zero the new slots */
404 while (i <= new_args_len) /* <= for null termination */
410 rule->arg_lens = new_arg_lens;
411 rule->args_len = new_args_len;
414 length = _dbus_string_get_length (value);
415 if (!_dbus_string_copy_data (value, &new))
418 rule->flags |= BUS_MATCH_ARGS;
420 dbus_free (rule->args[arg]);
421 rule->arg_lens[arg] = length;
422 rule->args[arg] = new;
425 rule->arg_lens[arg] |= BUS_MATCH_ARG_IS_PATH;
427 /* NULL termination didn't get busted */
428 _dbus_assert (rule->args[rule->args_len] == NULL);
429 _dbus_assert (rule->arg_lens[rule->args_len] == 0);
434 #define ISWHITE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
437 find_key (const DBusString *str,
445 const char *key_start;
448 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
450 s = _dbus_string_get_const_data (str);
454 while (*p && ISWHITE (*p))
459 while (*p && *p != '=' && !ISWHITE (*p))
464 while (*p && ISWHITE (*p))
467 if (key_start == key_end)
469 /* Empty match rules or trailing whitespace are OK */
476 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
477 "Match rule has a key with no subsequent '=' character");
482 if (!_dbus_string_append_len (key, key_start, key_end - key_start))
494 find_value (const DBusString *str,
506 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
508 orig_len = _dbus_string_get_length (value);
510 s = _dbus_string_get_const_data (str);
518 if (quote_char == '\0')
538 if (!_dbus_string_append_byte (value, *p))
545 else if (quote_char == '\\')
547 /* \ only counts as an escape if escaping a quote mark */
550 if (!_dbus_string_append_byte (value, '\\'))
557 if (!_dbus_string_append_byte (value, *p))
567 _dbus_assert (quote_char == '\'');
575 if (!_dbus_string_append_byte (value, *p))
589 if (quote_char == '\\')
591 if (!_dbus_string_append_byte (value, '\\'))
597 else if (quote_char == '\'')
599 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
600 "Unbalanced quotation marks in match rule");
604 _dbus_assert (quote_char == '\0');
606 /* Zero-length values are allowed */
613 _DBUS_ASSERT_ERROR_IS_SET (error);
614 _dbus_string_set_length (value, orig_len);
618 /* duplicates aren't allowed so the real legitimate max is only 6 or
619 * so. Leaving extra so we don't have to bother to update it.
620 * FIXME this is sort of busted now with arg matching, but we let
621 * you match on up to 10 args for now
623 #define MAX_RULE_TOKENS 16
625 /* this is slightly too high level to be termed a "token"
626 * but let's not be pedantic.
635 tokenize_rule (const DBusString *rule_text,
636 RuleToken tokens[MAX_RULE_TOKENS],
647 if (!_dbus_string_init (&key))
653 if (!_dbus_string_init (&value))
655 _dbus_string_free (&key);
662 while (i < MAX_RULE_TOKENS &&
663 pos < _dbus_string_get_length (rule_text))
665 _dbus_assert (tokens[i].key == NULL);
666 _dbus_assert (tokens[i].value == NULL);
668 if (!find_key (rule_text, pos, &key, &pos, error))
671 if (_dbus_string_get_length (&key) == 0)
674 if (!_dbus_string_steal_data (&key, &tokens[i].key))
680 if (!find_value (rule_text, pos, tokens[i].key, &value, &pos, error))
683 if (!_dbus_string_steal_data (&value, &tokens[i].value))
699 while (tokens[i].key || tokens[i].value)
701 dbus_free (tokens[i].key);
702 dbus_free (tokens[i].value);
703 tokens[i].key = NULL;
704 tokens[i].value = NULL;
709 _dbus_string_free (&key);
710 _dbus_string_free (&value);
716 bus_match_rule_parse_arg_match (BusMatchRule *rule,
718 const DBusString *value,
727 /* For now, arg0='foo' always implies that 'foo' is a
728 * DBUS_TYPE_STRING. Someday we could add an arg0type='int32' thing
729 * if we wanted, which would specify another type, in which case
730 * arg0='5' would have the 5 parsed as an int rather than string.
733 /* First we need to parse arg0 = 0, arg27 = 27 */
735 _dbus_string_init_const (&key_str, key);
736 length = _dbus_string_get_length (&key_str);
738 if (_dbus_string_get_length (&key_str) < 4)
740 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
741 "Key '%s' in match rule starts with 'arg' but lacks an arg number. Should be 'arg0' or 'arg7' for example.\n", key);
745 if (!_dbus_string_parse_uint (&key_str, 3, &arg, &end))
747 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
748 "Key '%s' in match rule starts with 'arg' but could not parse arg number. Should be 'arg0' or 'arg7' for example.\n", key);
753 ((end + 4) != length ||
754 !_dbus_string_ends_with_c_str (&key_str, "path")))
756 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
757 "Key '%s' in match rule contains junk after argument number. Only 'path' is optionally valid ('arg0path' for example).\n", key);
761 is_path = end != length;
763 /* If we didn't check this we could allocate a huge amount of RAM */
764 if (arg > DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER)
766 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
767 "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);
771 if ((rule->flags & BUS_MATCH_ARGS) &&
772 rule->args_len > (int) arg &&
773 rule->args[arg] != NULL)
775 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
776 "Argument %d matched more than once in match rule\n", key);
780 if (!bus_match_rule_set_arg (rule, arg, value, is_path))
789 _DBUS_ASSERT_ERROR_IS_SET (error);
794 * The format is comma-separated with strings quoted with single quotes
795 * as for the shell (to escape a literal single quote, use '\'').
797 * type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',
798 * path='/bar/foo',destination=':452345.34'
802 bus_match_rule_parse (DBusConnection *matches_go_to,
803 const DBusString *rule_text,
807 RuleToken tokens[MAX_RULE_TOKENS+1]; /* NULL termination + 1 */
810 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
812 if (_dbus_string_get_length (rule_text) > DBUS_MAXIMUM_MATCH_RULE_LENGTH)
814 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
815 "Match rule text is %d bytes, maximum is %d",
816 _dbus_string_get_length (rule_text),
817 DBUS_MAXIMUM_MATCH_RULE_LENGTH);
821 memset (tokens, '\0', sizeof (tokens));
823 rule = bus_match_rule_new (matches_go_to);
830 if (!tokenize_rule (rule_text, tokens, error))
834 while (tokens[i].key != NULL)
838 const char *key = tokens[i].key;
839 const char *value = tokens[i].value;
841 _dbus_string_init_const (&tmp_str, value);
842 len = _dbus_string_get_length (&tmp_str);
844 if (strcmp (key, "type") == 0)
848 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
850 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
851 "Key %s specified twice in match rule\n", key);
855 t = dbus_message_type_from_string (value);
857 if (t == DBUS_MESSAGE_TYPE_INVALID)
859 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
860 "Invalid message type (%s) in match rule\n", value);
864 if (!bus_match_rule_set_message_type (rule, t))
870 else if (strcmp (key, "sender") == 0)
872 if (rule->flags & BUS_MATCH_SENDER)
874 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
875 "Key %s specified twice in match rule\n", key);
879 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
881 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
882 "Sender name '%s' is invalid\n", value);
886 if (!bus_match_rule_set_sender (rule, value))
892 else if (strcmp (key, "interface") == 0)
894 if (rule->flags & BUS_MATCH_INTERFACE)
896 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
897 "Key %s specified twice in match rule\n", key);
901 if (!_dbus_validate_interface (&tmp_str, 0, len))
903 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
904 "Interface name '%s' is invalid\n", value);
908 if (!bus_match_rule_set_interface (rule, value))
914 else if (strcmp (key, "member") == 0)
916 if (rule->flags & BUS_MATCH_MEMBER)
918 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
919 "Key %s specified twice in match rule\n", key);
923 if (!_dbus_validate_member (&tmp_str, 0, len))
925 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
926 "Member name '%s' is invalid\n", value);
930 if (!bus_match_rule_set_member (rule, value))
936 else if (strcmp (key, "path") == 0)
938 if (rule->flags & BUS_MATCH_PATH)
940 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
941 "Key %s specified twice in match rule\n", key);
945 if (!_dbus_validate_path (&tmp_str, 0, len))
947 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
948 "Path '%s' is invalid\n", value);
952 if (!bus_match_rule_set_path (rule, value))
958 else if (strcmp (key, "destination") == 0)
960 if (rule->flags & BUS_MATCH_DESTINATION)
962 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
963 "Key %s specified twice in match rule\n", key);
967 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
969 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
970 "Destination name '%s' is invalid\n", value);
974 if (!bus_match_rule_set_destination (rule, value))
980 else if (strncmp (key, "arg", 3) == 0)
982 if (!bus_match_rule_parse_arg_match (rule, key, &tmp_str, error))
987 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
988 "Unknown key \"%s\" in match rule",
1000 _DBUS_ASSERT_ERROR_IS_SET (error);
1003 bus_match_rule_unref (rule);
1010 while (tokens[i].key || tokens[i].value)
1012 _dbus_assert (i < MAX_RULE_TOKENS);
1013 dbus_free (tokens[i].key);
1014 dbus_free (tokens[i].value);
1021 typedef struct RulePool RulePool;
1024 /* Maps non-NULL interface names to non-NULL (DBusList **)s */
1025 DBusHashTable *rules_by_iface;
1027 /* List of BusMatchRules which don't specify an interface */
1028 DBusList *rules_without_iface;
1031 struct BusMatchmaker
1035 /* Pools of rules, grouped by the type of message they match. 0
1036 * (DBUS_MESSAGE_TYPE_INVALID) represents rules that do not specify a message
1039 RulePool rules_by_type[DBUS_NUM_MESSAGE_TYPES];
1043 rule_list_free (DBusList **rules)
1045 while (*rules != NULL)
1049 rule = (*rules)->data;
1050 bus_match_rule_unref (rule);
1051 _dbus_list_remove_link (rules, *rules);
1056 rule_list_ptr_free (DBusList **list)
1058 /* We have to cope with NULL because the hash table frees the "existing"
1059 * value (which is NULL) when creating a new table entry...
1063 rule_list_free (list);
1069 bus_matchmaker_new (void)
1071 BusMatchmaker *matchmaker;
1074 matchmaker = dbus_new0 (BusMatchmaker, 1);
1075 if (matchmaker == NULL)
1078 matchmaker->refcount = 1;
1080 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1082 RulePool *p = matchmaker->rules_by_type + i;
1084 p->rules_by_iface = _dbus_hash_table_new (DBUS_HASH_STRING,
1085 dbus_free, (DBusFreeFunction) rule_list_ptr_free);
1087 if (p->rules_by_iface == NULL)
1094 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1096 RulePool *p = matchmaker->rules_by_type + i;
1098 if (p->rules_by_iface == NULL)
1101 _dbus_hash_table_unref (p->rules_by_iface);
1108 bus_matchmaker_get_rules (BusMatchmaker *matchmaker,
1110 const char *interface,
1115 _dbus_assert (message_type >= 0);
1116 _dbus_assert (message_type < DBUS_NUM_MESSAGE_TYPES);
1118 _dbus_verbose ("Looking up rules for message_type %d, interface %s\n",
1120 interface != NULL ? interface : "<null>");
1122 p = matchmaker->rules_by_type + message_type;
1124 if (interface == NULL)
1126 return &p->rules_without_iface;
1132 list = _dbus_hash_table_lookup_string (p->rules_by_iface, interface);
1134 if (list == NULL && create)
1136 char *dupped_interface;
1138 list = dbus_new0 (DBusList *, 1);
1142 dupped_interface = _dbus_strdup (interface);
1143 if (dupped_interface == NULL)
1149 _dbus_verbose ("Adding list for type %d, iface %s\n", message_type,
1152 if (!_dbus_hash_table_insert_string (p->rules_by_iface,
1153 dupped_interface, list))
1156 dbus_free (dupped_interface);
1166 bus_matchmaker_gc_rules (BusMatchmaker *matchmaker,
1168 const char *interface,
1173 if (interface == NULL)
1179 _dbus_verbose ("GCing HT entry for message_type %u, interface %s\n",
1180 message_type, interface);
1182 p = matchmaker->rules_by_type + message_type;
1184 _dbus_assert (_dbus_hash_table_lookup_string (p->rules_by_iface, interface)
1187 _dbus_hash_table_remove_string (p->rules_by_iface, interface);
1191 bus_matchmaker_ref (BusMatchmaker *matchmaker)
1193 _dbus_assert (matchmaker->refcount > 0);
1195 matchmaker->refcount += 1;
1201 bus_matchmaker_unref (BusMatchmaker *matchmaker)
1203 _dbus_assert (matchmaker->refcount > 0);
1205 matchmaker->refcount -= 1;
1206 if (matchmaker->refcount == 0)
1210 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1212 RulePool *p = matchmaker->rules_by_type + i;
1214 _dbus_hash_table_unref (p->rules_by_iface);
1215 rule_list_free (&p->rules_without_iface);
1218 dbus_free (matchmaker);
1222 /* The rule can't be modified after it's added. */
1224 bus_matchmaker_add_rule (BusMatchmaker *matchmaker,
1229 _dbus_assert (bus_connection_is_active (rule->matches_go_to));
1231 _dbus_verbose ("Adding rule with message_type %d, interface %s\n",
1233 rule->interface != NULL ? rule->interface : "<null>");
1235 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1236 rule->interface, TRUE);
1241 if (!_dbus_list_append (rules, rule))
1244 if (!bus_connection_add_match_rule (rule->matches_go_to, rule))
1246 _dbus_list_remove_last (rules, rule);
1247 bus_matchmaker_gc_rules (matchmaker, rule->message_type,
1248 rule->interface, rules);
1252 bus_match_rule_ref (rule);
1254 #ifdef DBUS_ENABLE_VERBOSE_MODE
1256 char *s = match_rule_to_string (rule);
1258 _dbus_verbose ("Added match rule %s to connection %p\n",
1259 s, rule->matches_go_to);
1268 match_rule_equal (BusMatchRule *a,
1271 if (a->flags != b->flags)
1274 if (a->matches_go_to != b->matches_go_to)
1277 if ((a->flags & BUS_MATCH_MESSAGE_TYPE) &&
1278 a->message_type != b->message_type)
1281 if ((a->flags & BUS_MATCH_MEMBER) &&
1282 strcmp (a->member, b->member) != 0)
1285 if ((a->flags & BUS_MATCH_PATH) &&
1286 strcmp (a->path, b->path) != 0)
1289 if ((a->flags & BUS_MATCH_INTERFACE) &&
1290 strcmp (a->interface, b->interface) != 0)
1293 if ((a->flags & BUS_MATCH_SENDER) &&
1294 strcmp (a->sender, b->sender) != 0)
1297 if ((a->flags & BUS_MATCH_DESTINATION) &&
1298 strcmp (a->destination, b->destination) != 0)
1301 if (a->flags & BUS_MATCH_ARGS)
1305 if (a->args_len != b->args_len)
1309 while (i < a->args_len)
1313 if ((a->args[i] != NULL) != (b->args[i] != NULL))
1316 if (a->arg_lens[i] != b->arg_lens[i])
1319 length = a->arg_lens[i] & ~BUS_MATCH_ARG_IS_PATH;
1321 if (a->args[i] != NULL)
1323 _dbus_assert (b->args[i] != NULL);
1324 if (memcmp (a->args[i], b->args[i], length) != 0)
1336 bus_matchmaker_remove_rule_link (DBusList **rules,
1339 BusMatchRule *rule = link->data;
1341 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1342 _dbus_list_remove_link (rules, link);
1344 #ifdef DBUS_ENABLE_VERBOSE_MODE
1346 char *s = match_rule_to_string (rule);
1348 _dbus_verbose ("Removed match rule %s for connection %p\n",
1349 s, rule->matches_go_to);
1354 bus_match_rule_unref (rule);
1358 bus_matchmaker_remove_rule (BusMatchmaker *matchmaker,
1363 _dbus_verbose ("Removing rule with message_type %d, interface %s\n",
1365 rule->interface != NULL ? rule->interface : "<null>");
1367 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1369 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1370 rule->interface, FALSE);
1372 /* We should only be asked to remove a rule by identity right after it was
1373 * added, so there should be a list for it.
1375 _dbus_assert (rules != NULL);
1377 _dbus_list_remove (rules, rule);
1378 bus_matchmaker_gc_rules (matchmaker, rule->message_type, rule->interface,
1381 #ifdef DBUS_ENABLE_VERBOSE_MODE
1383 char *s = match_rule_to_string (rule);
1385 _dbus_verbose ("Removed match rule %s for connection %p\n",
1386 s, rule->matches_go_to);
1391 bus_match_rule_unref (rule);
1394 /* Remove a single rule which is equal to the given rule by value */
1396 bus_matchmaker_remove_rule_by_value (BusMatchmaker *matchmaker,
1397 BusMatchRule *value,
1401 DBusList *link = NULL;
1403 _dbus_verbose ("Removing rule by value with message_type %d, interface %s\n",
1404 value->message_type,
1405 value->interface != NULL ? value->interface : "<null>");
1407 rules = bus_matchmaker_get_rules (matchmaker, value->message_type,
1408 value->interface, FALSE);
1412 /* we traverse backward because bus_connection_remove_match_rule()
1413 * removes the most-recently-added rule
1415 link = _dbus_list_get_last_link (rules);
1416 while (link != NULL)
1422 prev = _dbus_list_get_prev_link (rules, link);
1424 if (match_rule_equal (rule, value))
1426 bus_matchmaker_remove_rule_link (rules, link);
1436 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_NOT_FOUND,
1437 "The given match rule wasn't found and can't be removed");
1441 bus_matchmaker_gc_rules (matchmaker, value->message_type, value->interface,
1448 rule_list_remove_by_connection (DBusList **rules,
1449 DBusConnection *connection)
1453 link = _dbus_list_get_first_link (rules);
1454 while (link != NULL)
1460 next = _dbus_list_get_next_link (rules, link);
1462 if (rule->matches_go_to == connection)
1464 bus_matchmaker_remove_rule_link (rules, link);
1466 else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') ||
1467 ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':'))
1469 /* The rule matches to/from a base service, see if it's the
1470 * one being disconnected, since we know this service name
1471 * will never be recycled.
1475 name = bus_connection_get_name (connection);
1476 _dbus_assert (name != NULL); /* because we're an active connection */
1478 if (((rule->flags & BUS_MATCH_SENDER) &&
1479 strcmp (rule->sender, name) == 0) ||
1480 ((rule->flags & BUS_MATCH_DESTINATION) &&
1481 strcmp (rule->destination, name) == 0))
1483 bus_matchmaker_remove_rule_link (rules, link);
1492 bus_matchmaker_disconnected (BusMatchmaker *matchmaker,
1493 DBusConnection *connection)
1499 * This scans all match rules on the bus. We could avoid that
1500 * for the rules belonging to the connection, since we keep
1501 * a list of those; but for the rules that just refer to
1502 * the connection we'd need to do something more elaborate.
1505 _dbus_assert (bus_connection_is_active (connection));
1507 _dbus_verbose ("Removing all rules for connection %p\n", connection);
1509 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1511 RulePool *p = matchmaker->rules_by_type + i;
1514 rule_list_remove_by_connection (&p->rules_without_iface, connection);
1516 _dbus_hash_iter_init (p->rules_by_iface, &iter);
1517 while (_dbus_hash_iter_next (&iter))
1519 DBusList **items = _dbus_hash_iter_get_value (&iter);
1521 rule_list_remove_by_connection (items, connection);
1524 _dbus_hash_iter_remove_entry (&iter);
1530 connection_is_primary_owner (DBusConnection *connection,
1531 const char *service_name)
1533 BusService *service;
1535 BusRegistry *registry;
1537 _dbus_assert (connection != NULL);
1539 registry = bus_connection_get_registry (connection);
1541 _dbus_string_init_const (&str, service_name);
1542 service = bus_registry_lookup (registry, &str);
1544 if (service == NULL)
1545 return FALSE; /* Service doesn't exist so connection can't own it. */
1547 return bus_service_get_primary_owners_connection (service) == connection;
1551 match_rule_matches (BusMatchRule *rule,
1552 DBusConnection *sender,
1553 DBusConnection *addressed_recipient,
1554 DBusMessage *message,
1555 BusMatchFlags already_matched)
1559 /* All features of the match rule are AND'd together,
1560 * so FALSE if any of them don't match.
1563 /* sender/addressed_recipient of #NULL may mean bus driver,
1564 * or for addressed_recipient may mean a message with no
1565 * specific recipient (i.e. a signal)
1568 /* Don't bother re-matching features we've already checked implicitly. */
1569 flags = rule->flags & (~already_matched);
1571 if (flags & BUS_MATCH_MESSAGE_TYPE)
1573 _dbus_assert (rule->message_type != DBUS_MESSAGE_TYPE_INVALID);
1575 if (rule->message_type != dbus_message_get_type (message))
1579 if (flags & BUS_MATCH_INTERFACE)
1583 _dbus_assert (rule->interface != NULL);
1585 iface = dbus_message_get_interface (message);
1589 if (strcmp (iface, rule->interface) != 0)
1593 if (flags & BUS_MATCH_MEMBER)
1597 _dbus_assert (rule->member != NULL);
1599 member = dbus_message_get_member (message);
1603 if (strcmp (member, rule->member) != 0)
1607 if (flags & BUS_MATCH_SENDER)
1609 _dbus_assert (rule->sender != NULL);
1613 if (strcmp (rule->sender,
1614 DBUS_SERVICE_DBUS) != 0)
1619 if (!connection_is_primary_owner (sender, rule->sender))
1624 if (flags & BUS_MATCH_DESTINATION)
1626 const char *destination;
1628 _dbus_assert (rule->destination != NULL);
1630 destination = dbus_message_get_destination (message);
1631 if (destination == NULL)
1634 if (addressed_recipient == NULL)
1636 if (strcmp (rule->destination,
1637 DBUS_SERVICE_DBUS) != 0)
1642 if (!connection_is_primary_owner (addressed_recipient, rule->destination))
1647 if (flags & BUS_MATCH_PATH)
1651 _dbus_assert (rule->path != NULL);
1653 path = dbus_message_get_path (message);
1657 if (strcmp (path, rule->path) != 0)
1661 if (flags & BUS_MATCH_ARGS)
1664 DBusMessageIter iter;
1666 _dbus_assert (rule->args != NULL);
1668 dbus_message_iter_init (message, &iter);
1671 while (i < rule->args_len)
1674 const char *expected_arg;
1675 int expected_length;
1676 dbus_bool_t is_path;
1678 expected_arg = rule->args[i];
1679 expected_length = rule->arg_lens[i] & ~BUS_MATCH_ARG_IS_PATH;
1680 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
1682 current_type = dbus_message_iter_get_arg_type (&iter);
1684 if (expected_arg != NULL)
1686 const char *actual_arg;
1689 if (current_type != DBUS_TYPE_STRING)
1693 dbus_message_iter_get_basic (&iter, &actual_arg);
1694 _dbus_assert (actual_arg != NULL);
1696 actual_length = strlen (actual_arg);
1700 if (actual_length < expected_length &&
1701 actual_arg[actual_length - 1] != '/')
1704 if (expected_length < actual_length &&
1705 expected_arg[expected_length - 1] != '/')
1708 if (memcmp (actual_arg, expected_arg,
1709 MIN (actual_length, expected_length)) != 0)
1714 if (expected_length != actual_length ||
1715 memcmp (expected_arg, actual_arg, expected_length) != 0)
1721 if (current_type != DBUS_TYPE_INVALID)
1722 dbus_message_iter_next (&iter);
1732 get_recipients_from_list (DBusList **rules,
1733 DBusConnection *sender,
1734 DBusConnection *addressed_recipient,
1735 DBusMessage *message,
1736 DBusList **recipients_p)
1743 link = _dbus_list_get_first_link (rules);
1744 while (link != NULL)
1750 #ifdef DBUS_ENABLE_VERBOSE_MODE
1752 char *s = match_rule_to_string (rule);
1754 _dbus_verbose ("Checking whether message matches rule %s for connection %p\n",
1755 s, rule->matches_go_to);
1760 if (match_rule_matches (rule,
1761 sender, addressed_recipient, message,
1762 BUS_MATCH_MESSAGE_TYPE | BUS_MATCH_INTERFACE))
1764 _dbus_verbose ("Rule matched\n");
1766 /* Append to the list if we haven't already */
1767 if (bus_connection_mark_stamp (rule->matches_go_to))
1769 if (!_dbus_list_append (recipients_p, rule->matches_go_to))
1772 #ifdef DBUS_ENABLE_VERBOSE_MODE
1775 _dbus_verbose ("Connection already receiving this message, so not adding again\n");
1777 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1780 link = _dbus_list_get_next_link (rules, link);
1787 bus_matchmaker_get_recipients (BusMatchmaker *matchmaker,
1788 BusConnections *connections,
1789 DBusConnection *sender,
1790 DBusConnection *addressed_recipient,
1791 DBusMessage *message,
1792 DBusList **recipients_p)
1795 const char *interface;
1796 DBusList **neither, **just_type, **just_iface, **both;
1798 _dbus_assert (*recipients_p == NULL);
1800 /* This avoids sending same message to the same connection twice.
1801 * Purpose of the stamp instead of a bool is to avoid iterating over
1802 * all connections resetting the bool each time.
1804 bus_connections_increment_stamp (connections);
1806 /* addressed_recipient is already receiving the message, don't add to list.
1807 * NULL addressed_recipient means either bus driver, or this is a signal
1808 * and thus lacks a specific addressed_recipient.
1810 if (addressed_recipient != NULL)
1811 bus_connection_mark_stamp (addressed_recipient);
1813 type = dbus_message_get_type (message);
1814 interface = dbus_message_get_interface (message);
1816 neither = bus_matchmaker_get_rules (matchmaker, DBUS_MESSAGE_TYPE_INVALID,
1818 just_type = just_iface = both = NULL;
1820 if (interface != NULL)
1821 just_iface = bus_matchmaker_get_rules (matchmaker,
1822 DBUS_MESSAGE_TYPE_INVALID, interface, FALSE);
1824 if (type > DBUS_MESSAGE_TYPE_INVALID && type < DBUS_NUM_MESSAGE_TYPES)
1826 just_type = bus_matchmaker_get_rules (matchmaker, type, NULL, FALSE);
1828 if (interface != NULL)
1829 both = bus_matchmaker_get_rules (matchmaker, type, interface, FALSE);
1832 if (!(get_recipients_from_list (neither, sender, addressed_recipient,
1833 message, recipients_p) &&
1834 get_recipients_from_list (just_iface, sender, addressed_recipient,
1835 message, recipients_p) &&
1836 get_recipients_from_list (just_type, sender, addressed_recipient,
1837 message, recipients_p) &&
1838 get_recipients_from_list (both, sender, addressed_recipient,
1839 message, recipients_p)))
1841 _dbus_list_clear (recipients_p);
1848 #ifdef DBUS_BUILD_TESTS
1852 static BusMatchRule*
1853 check_parse (dbus_bool_t should_succeed,
1860 dbus_error_init (&error);
1862 _dbus_string_init_const (&str, text);
1864 rule = bus_match_rule_parse (NULL, &str, &error);
1865 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1867 dbus_error_free (&error);
1871 if (should_succeed && rule == NULL)
1873 _dbus_warn ("Failed to parse: %s: %s: \"%s\"\n",
1874 error.name, error.message,
1875 _dbus_string_get_const_data (&str));
1879 if (!should_succeed && rule != NULL)
1881 _dbus_warn ("Failed to fail to parse: \"%s\"\n",
1882 _dbus_string_get_const_data (&str));
1886 dbus_error_free (&error);
1892 assert_large_rule (BusMatchRule *rule)
1894 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
1895 _dbus_assert (rule->flags & BUS_MATCH_SENDER);
1896 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
1897 _dbus_assert (rule->flags & BUS_MATCH_MEMBER);
1898 _dbus_assert (rule->flags & BUS_MATCH_DESTINATION);
1899 _dbus_assert (rule->flags & BUS_MATCH_PATH);
1901 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
1902 _dbus_assert (rule->interface != NULL);
1903 _dbus_assert (rule->member != NULL);
1904 _dbus_assert (rule->sender != NULL);
1905 _dbus_assert (rule->destination != NULL);
1906 _dbus_assert (rule->path != NULL);
1908 _dbus_assert (strcmp (rule->interface, "org.freedesktop.DBusInterface") == 0);
1909 _dbus_assert (strcmp (rule->sender, "org.freedesktop.DBusSender") == 0);
1910 _dbus_assert (strcmp (rule->member, "Foo") == 0);
1911 _dbus_assert (strcmp (rule->path, "/bar/foo") == 0);
1912 _dbus_assert (strcmp (rule->destination, ":452345.34") == 0);
1916 test_parsing (void *data)
1920 rule = check_parse (TRUE, "type='signal',sender='org.freedesktop.DBusSender',interface='org.freedesktop.DBusInterface',member='Foo',path='/bar/foo',destination=':452345.34'");
1923 assert_large_rule (rule);
1924 bus_match_rule_unref (rule);
1927 /* With extra whitespace and useless quotes */
1928 rule = check_parse (TRUE, " type='signal', \tsender='org.freedes''ktop.DBusSender', interface='org.freedesktop.DBusInterface''''', \tmember='Foo',path='/bar/foo',destination=':452345.34'''''");
1931 assert_large_rule (rule);
1932 bus_match_rule_unref (rule);
1936 /* A simple signal connection */
1937 rule = check_parse (TRUE, "type='signal',path='/foo',interface='org.Bar'");
1940 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
1941 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
1942 _dbus_assert (rule->flags & BUS_MATCH_PATH);
1944 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
1945 _dbus_assert (rule->interface != NULL);
1946 _dbus_assert (rule->path != NULL);
1948 _dbus_assert (strcmp (rule->interface, "org.Bar") == 0);
1949 _dbus_assert (strcmp (rule->path, "/foo") == 0);
1951 bus_match_rule_unref (rule);
1955 rule = check_parse (TRUE, "arg0='foo'");
1958 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
1959 _dbus_assert (rule->args != NULL);
1960 _dbus_assert (rule->args_len == 1);
1961 _dbus_assert (rule->args[0] != NULL);
1962 _dbus_assert (rule->args[1] == NULL);
1963 _dbus_assert (strcmp (rule->args[0], "foo") == 0);
1965 bus_match_rule_unref (rule);
1968 rule = check_parse (TRUE, "arg1='foo'");
1971 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
1972 _dbus_assert (rule->args != NULL);
1973 _dbus_assert (rule->args_len == 2);
1974 _dbus_assert (rule->args[0] == NULL);
1975 _dbus_assert (rule->args[1] != NULL);
1976 _dbus_assert (rule->args[2] == NULL);
1977 _dbus_assert (strcmp (rule->args[1], "foo") == 0);
1979 bus_match_rule_unref (rule);
1982 rule = check_parse (TRUE, "arg2='foo'");
1985 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
1986 _dbus_assert (rule->args != NULL);
1987 _dbus_assert (rule->args_len == 3);
1988 _dbus_assert (rule->args[0] == NULL);
1989 _dbus_assert (rule->args[1] == NULL);
1990 _dbus_assert (rule->args[2] != NULL);
1991 _dbus_assert (rule->args[3] == NULL);
1992 _dbus_assert (strcmp (rule->args[2], "foo") == 0);
1994 bus_match_rule_unref (rule);
1997 rule = check_parse (TRUE, "arg40='foo'");
2000 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2001 _dbus_assert (rule->args != NULL);
2002 _dbus_assert (rule->args_len == 41);
2003 _dbus_assert (rule->args[0] == NULL);
2004 _dbus_assert (rule->args[1] == NULL);
2005 _dbus_assert (rule->args[40] != NULL);
2006 _dbus_assert (rule->args[41] == NULL);
2007 _dbus_assert (strcmp (rule->args[40], "foo") == 0);
2009 bus_match_rule_unref (rule);
2012 rule = check_parse (TRUE, "arg63='foo'");
2015 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2016 _dbus_assert (rule->args != NULL);
2017 _dbus_assert (rule->args_len == 64);
2018 _dbus_assert (rule->args[0] == NULL);
2019 _dbus_assert (rule->args[1] == NULL);
2020 _dbus_assert (rule->args[63] != NULL);
2021 _dbus_assert (rule->args[64] == NULL);
2022 _dbus_assert (strcmp (rule->args[63], "foo") == 0);
2024 bus_match_rule_unref (rule);
2027 /* Too-large argN */
2028 rule = check_parse (FALSE, "arg300='foo'");
2029 _dbus_assert (rule == NULL);
2030 rule = check_parse (FALSE, "arg64='foo'");
2031 _dbus_assert (rule == NULL);
2034 rule = check_parse (FALSE, "arg='foo'");
2035 _dbus_assert (rule == NULL);
2036 rule = check_parse (FALSE, "argv='foo'");
2037 _dbus_assert (rule == NULL);
2038 rule = check_parse (FALSE, "arg3junk='foo'");
2039 _dbus_assert (rule == NULL);
2040 rule = check_parse (FALSE, "argument='foo'");
2041 _dbus_assert (rule == NULL);
2043 /* Reject duplicates */
2044 rule = check_parse (FALSE, "type='signal',type='method_call'");
2045 _dbus_assert (rule == NULL);
2047 /* Duplicates with the argN code */
2048 rule = check_parse (FALSE, "arg0='foo',arg0='bar'");
2049 _dbus_assert (rule == NULL);
2050 rule = check_parse (FALSE, "arg3='foo',arg3='bar'");
2051 _dbus_assert (rule == NULL);
2052 rule = check_parse (FALSE, "arg30='foo',arg30='bar'");
2053 _dbus_assert (rule == NULL);
2055 /* Reject broken keys */
2056 rule = check_parse (FALSE, "blah='signal'");
2057 _dbus_assert (rule == NULL);
2059 /* Reject broken values */
2060 rule = check_parse (FALSE, "type='chouin'");
2061 _dbus_assert (rule == NULL);
2062 rule = check_parse (FALSE, "interface='abc@def++'");
2063 _dbus_assert (rule == NULL);
2064 rule = check_parse (FALSE, "service='youpi'");
2065 _dbus_assert (rule == NULL);
2067 /* Allow empty rule */
2068 rule = check_parse (TRUE, "");
2071 _dbus_assert (rule->flags == 0);
2073 bus_match_rule_unref (rule);
2076 /* All-whitespace rule is the same as empty */
2077 rule = check_parse (TRUE, " \t");
2080 _dbus_assert (rule->flags == 0);
2082 bus_match_rule_unref (rule);
2085 /* But with non-whitespace chars and no =value, it's not OK */
2086 rule = check_parse (FALSE, "type");
2087 _dbus_assert (rule == NULL);
2095 } equality_tests[] = {
2096 { "type='signal'", "type='signal'" },
2097 { "type='signal',interface='foo.bar'", "interface='foo.bar',type='signal'" },
2098 { "type='signal',member='bar'", "member='bar',type='signal'" },
2099 { "type='method_call',sender=':1.0'", "sender=':1.0',type='method_call'" },
2100 { "type='method_call',destination=':1.0'", "destination=':1.0',type='method_call'" },
2101 { "type='method_call',path='/foo/bar'", "path='/foo/bar',type='method_call'" },
2102 { "type='method_call',arg0='blah'", "arg0='blah',type='method_call'" },
2103 { "type='method_call',arg0='boo'", "arg0='boo',type='method_call'" },
2104 { "type='method_call',arg0='blah',arg1='baz'", "arg0='blah',arg1='baz',type='method_call'" },
2105 { "type='method_call',arg3='foosh'", "arg3='foosh',type='method_call'" },
2106 { "arg3='fool'", "arg3='fool'" },
2107 { "member='food'", "member='food'" }
2111 test_equality (void)
2116 while (i < _DBUS_N_ELEMENTS (equality_tests))
2118 BusMatchRule *first;
2119 BusMatchRule *second;
2122 first = check_parse (TRUE, equality_tests[i].first);
2123 _dbus_assert (first != NULL);
2124 second = check_parse (TRUE, equality_tests[i].second);
2125 _dbus_assert (second != NULL);
2127 if (!match_rule_equal (first, second))
2129 _dbus_warn ("rule %s and %s should have been equal\n",
2130 equality_tests[i].first,
2131 equality_tests[i].second);
2135 bus_match_rule_unref (second);
2137 /* Check that the rule is not equal to any of the
2138 * others besides its pair match
2141 while (j < _DBUS_N_ELEMENTS (equality_tests))
2145 second = check_parse (TRUE, equality_tests[j].second);
2147 if (match_rule_equal (first, second))
2149 _dbus_warn ("rule %s and %s should not have been equal\n",
2150 equality_tests[i].first,
2151 equality_tests[j].second);
2155 bus_match_rule_unref (second);
2161 bus_match_rule_unref (first);
2168 should_match_message_1[] = {
2170 "member='Frobated'",
2172 "type='signal',member='Frobated'",
2173 "type='signal',member='Frobated',arg0='foobar'",
2174 "member='Frobated',arg0='foobar'",
2175 "type='signal',arg0='foobar'",
2180 should_not_match_message_1[] = {
2181 "type='method_call'",
2183 "type='method_return'",
2184 "type='signal',member='Oopsed'",
2191 "arg0='foobar',arg1='abcdef'",
2192 "arg0='foobar',arg1='abcdef',arg2='abcdefghi',arg3='abcdefghi',arg4='abcdefghi'",
2193 "arg0='foobar',arg1='abcdef',arg4='abcdefghi',arg3='abcdefghi',arg2='abcdefghi'",
2198 check_matches (dbus_bool_t expected_to_match,
2200 DBusMessage *message,
2201 const char *rule_text)
2204 dbus_bool_t matched;
2206 rule = check_parse (TRUE, rule_text);
2207 _dbus_assert (rule != NULL);
2209 /* We can't test sender/destination rules since we pass NULL here */
2210 matched = match_rule_matches (rule, NULL, NULL, message, 0);
2212 if (matched != expected_to_match)
2214 _dbus_warn ("Expected rule %s to %s message %d, failed\n",
2215 rule_text, expected_to_match ?
2216 "match" : "not match", number);
2220 bus_match_rule_unref (rule);
2224 check_matching (DBusMessage *message,
2226 const char **should_match,
2227 const char **should_not_match)
2232 while (should_match[i] != NULL)
2234 check_matches (TRUE, number, message, should_match[i]);
2239 while (should_not_match[i] != NULL)
2241 check_matches (FALSE, number, message, should_not_match[i]);
2247 test_matching (void)
2249 DBusMessage *message1;
2250 const char *v_STRING;
2251 dbus_int32_t v_INT32;
2253 message1 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2254 _dbus_assert (message1 != NULL);
2255 if (!dbus_message_set_member (message1, "Frobated"))
2256 _dbus_assert_not_reached ("oom");
2258 v_STRING = "foobar";
2260 if (!dbus_message_append_args (message1,
2261 DBUS_TYPE_STRING, &v_STRING,
2262 DBUS_TYPE_INT32, &v_INT32,
2264 _dbus_assert_not_reached ("oom");
2266 check_matching (message1, 1,
2267 should_match_message_1,
2268 should_not_match_message_1);
2270 dbus_message_unref (message1);
2274 bus_signals_test (const DBusString *test_data_dir)
2276 BusMatchmaker *matchmaker;
2278 matchmaker = bus_matchmaker_new ();
2279 bus_matchmaker_ref (matchmaker);
2280 bus_matchmaker_unref (matchmaker);
2281 bus_matchmaker_unref (matchmaker);
2283 if (!_dbus_test_oom_handling ("parsing match rules", test_parsing, NULL))
2284 _dbus_assert_not_reached ("Parsing match rules test failed");
2293 #endif /* DBUS_BUILD_TESTS */