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
28 #include <dbus/dbus-marshal-validate.h>
32 int refcount; /**< reference count */
34 DBusConnection *matches_go_to; /**< Owner of the rule */
36 unsigned int flags; /**< BusMatchFlags */
45 unsigned int *arg_lens;
50 #define BUS_MATCH_ARG_IS_PATH 0x8000000u
53 bus_match_rule_new (DBusConnection *matches_go_to)
57 rule = dbus_new0 (BusMatchRule, 1);
62 rule->matches_go_to = matches_go_to;
64 #ifndef DBUS_BUILD_TESTS
65 _dbus_assert (rule->matches_go_to != NULL);
72 bus_match_rule_ref (BusMatchRule *rule)
74 _dbus_assert (rule->refcount > 0);
82 bus_match_rule_unref (BusMatchRule *rule)
84 _dbus_assert (rule->refcount > 0);
87 if (rule->refcount == 0)
89 dbus_free (rule->interface);
90 dbus_free (rule->member);
91 dbus_free (rule->sender);
92 dbus_free (rule->destination);
93 dbus_free (rule->path);
94 dbus_free (rule->arg_lens);
96 /* can't use dbus_free_string_array() since there
104 while (i < rule->args_len)
107 dbus_free (rule->args[i]);
111 dbus_free (rule->args);
118 #ifdef DBUS_ENABLE_VERBOSE_MODE
119 /* Note this function does not do escaping, so it's only
120 * good for debug spew at the moment
123 match_rule_to_string (BusMatchRule *rule)
128 if (!_dbus_string_init (&str))
131 while ((s = _dbus_strdup ("nomem")) == NULL)
132 ; /* only OK for debug spew... */
136 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
138 /* FIXME make type readable */
139 if (!_dbus_string_append_printf (&str, "type='%d'", rule->message_type))
143 if (rule->flags & BUS_MATCH_INTERFACE)
145 if (_dbus_string_get_length (&str) > 0)
147 if (!_dbus_string_append (&str, ","))
151 if (!_dbus_string_append_printf (&str, "interface='%s'", rule->interface))
155 if (rule->flags & BUS_MATCH_MEMBER)
157 if (_dbus_string_get_length (&str) > 0)
159 if (!_dbus_string_append (&str, ","))
163 if (!_dbus_string_append_printf (&str, "member='%s'", rule->member))
167 if (rule->flags & BUS_MATCH_PATH)
169 if (_dbus_string_get_length (&str) > 0)
171 if (!_dbus_string_append (&str, ","))
175 if (!_dbus_string_append_printf (&str, "path='%s'", rule->path))
179 if (rule->flags & BUS_MATCH_SENDER)
181 if (_dbus_string_get_length (&str) > 0)
183 if (!_dbus_string_append (&str, ","))
187 if (!_dbus_string_append_printf (&str, "sender='%s'", rule->sender))
191 if (rule->flags & BUS_MATCH_DESTINATION)
193 if (_dbus_string_get_length (&str) > 0)
195 if (!_dbus_string_append (&str, ","))
199 if (!_dbus_string_append_printf (&str, "destination='%s'", rule->destination))
203 if (rule->flags & BUS_MATCH_ARGS)
207 _dbus_assert (rule->args != NULL);
210 while (i < rule->args_len)
212 if (rule->args[i] != NULL)
216 if (_dbus_string_get_length (&str) > 0)
218 if (!_dbus_string_append (&str, ","))
222 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
224 if (!_dbus_string_append_printf (&str,
226 i, is_path ? "path" : "",
235 if (!_dbus_string_steal_data (&str, &ret))
238 _dbus_string_free (&str);
242 _dbus_string_free (&str);
245 while ((s = _dbus_strdup ("nomem")) == NULL)
246 ; /* only OK for debug spew... */
250 #endif /* DBUS_ENABLE_VERBOSE_MODE */
253 bus_match_rule_set_message_type (BusMatchRule *rule,
256 rule->flags |= BUS_MATCH_MESSAGE_TYPE;
258 rule->message_type = type;
264 bus_match_rule_set_interface (BusMatchRule *rule,
265 const char *interface)
269 _dbus_assert (interface != NULL);
271 new = _dbus_strdup (interface);
275 rule->flags |= BUS_MATCH_INTERFACE;
276 dbus_free (rule->interface);
277 rule->interface = new;
283 bus_match_rule_set_member (BusMatchRule *rule,
288 _dbus_assert (member != NULL);
290 new = _dbus_strdup (member);
294 rule->flags |= BUS_MATCH_MEMBER;
295 dbus_free (rule->member);
302 bus_match_rule_set_sender (BusMatchRule *rule,
307 _dbus_assert (sender != NULL);
309 new = _dbus_strdup (sender);
313 rule->flags |= BUS_MATCH_SENDER;
314 dbus_free (rule->sender);
321 bus_match_rule_set_destination (BusMatchRule *rule,
322 const char *destination)
326 _dbus_assert (destination != NULL);
328 new = _dbus_strdup (destination);
332 rule->flags |= BUS_MATCH_DESTINATION;
333 dbus_free (rule->destination);
334 rule->destination = new;
340 bus_match_rule_set_path (BusMatchRule *rule,
345 _dbus_assert (path != NULL);
347 new = _dbus_strdup (path);
351 rule->flags |= BUS_MATCH_PATH;
352 dbus_free (rule->path);
359 bus_match_rule_set_arg (BusMatchRule *rule,
361 const DBusString *value,
367 _dbus_assert (value != NULL);
369 /* args_len is the number of args not including null termination
372 if (arg >= rule->args_len)
374 unsigned int *new_arg_lens;
379 new_args_len = arg + 1;
381 /* add another + 1 here for null termination */
382 new_args = dbus_realloc (rule->args,
383 sizeof (char *) * (new_args_len + 1));
384 if (new_args == NULL)
387 /* NULL the new slots */
389 while (i <= new_args_len) /* <= for null termination */
395 rule->args = new_args;
397 /* and now add to the lengths */
398 new_arg_lens = dbus_realloc (rule->arg_lens,
399 sizeof (int) * (new_args_len + 1));
401 if (new_arg_lens == NULL)
404 /* zero the new slots */
406 while (i <= new_args_len) /* <= for null termination */
412 rule->arg_lens = new_arg_lens;
413 rule->args_len = new_args_len;
416 length = _dbus_string_get_length (value);
417 if (!_dbus_string_copy_data (value, &new))
420 rule->flags |= BUS_MATCH_ARGS;
422 dbus_free (rule->args[arg]);
423 rule->arg_lens[arg] = length;
424 rule->args[arg] = new;
427 rule->arg_lens[arg] |= BUS_MATCH_ARG_IS_PATH;
429 /* NULL termination didn't get busted */
430 _dbus_assert (rule->args[rule->args_len] == NULL);
431 _dbus_assert (rule->arg_lens[rule->args_len] == 0);
436 #define ISWHITE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
439 find_key (const DBusString *str,
447 const char *key_start;
450 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
452 s = _dbus_string_get_const_data (str);
456 while (*p && ISWHITE (*p))
461 while (*p && *p != '=' && !ISWHITE (*p))
466 while (*p && ISWHITE (*p))
469 if (key_start == key_end)
471 /* Empty match rules or trailing whitespace are OK */
478 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
479 "Match rule has a key with no subsequent '=' character");
484 if (!_dbus_string_append_len (key, key_start, key_end - key_start))
496 find_value (const DBusString *str,
508 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
510 orig_len = _dbus_string_get_length (value);
512 s = _dbus_string_get_const_data (str);
520 if (quote_char == '\0')
540 if (!_dbus_string_append_byte (value, *p))
547 else if (quote_char == '\\')
549 /* \ only counts as an escape if escaping a quote mark */
552 if (!_dbus_string_append_byte (value, '\\'))
559 if (!_dbus_string_append_byte (value, *p))
569 _dbus_assert (quote_char == '\'');
577 if (!_dbus_string_append_byte (value, *p))
591 if (quote_char == '\\')
593 if (!_dbus_string_append_byte (value, '\\'))
599 else if (quote_char == '\'')
601 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
602 "Unbalanced quotation marks in match rule");
606 _dbus_assert (quote_char == '\0');
608 /* Zero-length values are allowed */
615 _DBUS_ASSERT_ERROR_IS_SET (error);
616 _dbus_string_set_length (value, orig_len);
620 /* duplicates aren't allowed so the real legitimate max is only 6 or
621 * so. Leaving extra so we don't have to bother to update it.
622 * FIXME this is sort of busted now with arg matching, but we let
623 * you match on up to 10 args for now
625 #define MAX_RULE_TOKENS 16
627 /* this is slightly too high level to be termed a "token"
628 * but let's not be pedantic.
637 tokenize_rule (const DBusString *rule_text,
638 RuleToken tokens[MAX_RULE_TOKENS],
649 if (!_dbus_string_init (&key))
655 if (!_dbus_string_init (&value))
657 _dbus_string_free (&key);
664 while (i < MAX_RULE_TOKENS &&
665 pos < _dbus_string_get_length (rule_text))
667 _dbus_assert (tokens[i].key == NULL);
668 _dbus_assert (tokens[i].value == NULL);
670 if (!find_key (rule_text, pos, &key, &pos, error))
673 if (_dbus_string_get_length (&key) == 0)
676 if (!_dbus_string_steal_data (&key, &tokens[i].key))
682 if (!find_value (rule_text, pos, tokens[i].key, &value, &pos, error))
685 if (!_dbus_string_steal_data (&value, &tokens[i].value))
701 while (tokens[i].key || tokens[i].value)
703 dbus_free (tokens[i].key);
704 dbus_free (tokens[i].value);
705 tokens[i].key = NULL;
706 tokens[i].value = NULL;
711 _dbus_string_free (&key);
712 _dbus_string_free (&value);
718 bus_match_rule_parse_arg_match (BusMatchRule *rule,
720 const DBusString *value,
729 /* For now, arg0='foo' always implies that 'foo' is a
730 * DBUS_TYPE_STRING. Someday we could add an arg0type='int32' thing
731 * if we wanted, which would specify another type, in which case
732 * arg0='5' would have the 5 parsed as an int rather than string.
735 /* First we need to parse arg0 = 0, arg27 = 27 */
737 _dbus_string_init_const (&key_str, key);
738 length = _dbus_string_get_length (&key_str);
740 if (_dbus_string_get_length (&key_str) < 4)
742 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
743 "Key '%s' in match rule starts with 'arg' but lacks an arg number. Should be 'arg0' or 'arg7' for example.\n", key);
747 if (!_dbus_string_parse_uint (&key_str, 3, &arg, &end))
749 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
750 "Key '%s' in match rule starts with 'arg' but could not parse arg number. Should be 'arg0' or 'arg7' for example.\n", key);
755 ((end + 4) != length ||
756 !_dbus_string_ends_with_c_str (&key_str, "path")))
758 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
759 "Key '%s' in match rule contains junk after argument number. Only 'path' is optionally valid ('arg0path' for example).\n", key);
763 is_path = end != length;
765 /* If we didn't check this we could allocate a huge amount of RAM */
766 if (arg > DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER)
768 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
769 "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);
773 if ((rule->flags & BUS_MATCH_ARGS) &&
774 rule->args_len > (int) arg &&
775 rule->args[arg] != NULL)
777 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
778 "Argument %d matched more than once in match rule\n", key);
782 if (!bus_match_rule_set_arg (rule, arg, value, is_path))
791 _DBUS_ASSERT_ERROR_IS_SET (error);
796 * The format is comma-separated with strings quoted with single quotes
797 * as for the shell (to escape a literal single quote, use '\'').
799 * type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',
800 * path='/bar/foo',destination=':452345.34'
804 bus_match_rule_parse (DBusConnection *matches_go_to,
805 const DBusString *rule_text,
809 RuleToken tokens[MAX_RULE_TOKENS+1]; /* NULL termination + 1 */
812 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
814 if (_dbus_string_get_length (rule_text) > DBUS_MAXIMUM_MATCH_RULE_LENGTH)
816 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
817 "Match rule text is %d bytes, maximum is %d",
818 _dbus_string_get_length (rule_text),
819 DBUS_MAXIMUM_MATCH_RULE_LENGTH);
823 memset (tokens, '\0', sizeof (tokens));
825 rule = bus_match_rule_new (matches_go_to);
832 if (!tokenize_rule (rule_text, tokens, error))
836 while (tokens[i].key != NULL)
840 const char *key = tokens[i].key;
841 const char *value = tokens[i].value;
843 _dbus_string_init_const (&tmp_str, value);
844 len = _dbus_string_get_length (&tmp_str);
846 if (strcmp (key, "type") == 0)
850 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
852 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
853 "Key %s specified twice in match rule\n", key);
857 t = dbus_message_type_from_string (value);
859 if (t == DBUS_MESSAGE_TYPE_INVALID)
861 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
862 "Invalid message type (%s) in match rule\n", value);
866 if (!bus_match_rule_set_message_type (rule, t))
872 else if (strcmp (key, "sender") == 0)
874 if (rule->flags & BUS_MATCH_SENDER)
876 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
877 "Key %s specified twice in match rule\n", key);
881 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
883 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
884 "Sender name '%s' is invalid\n", value);
888 if (!bus_match_rule_set_sender (rule, value))
894 else if (strcmp (key, "interface") == 0)
896 if (rule->flags & BUS_MATCH_INTERFACE)
898 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
899 "Key %s specified twice in match rule\n", key);
903 if (!_dbus_validate_interface (&tmp_str, 0, len))
905 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
906 "Interface name '%s' is invalid\n", value);
910 if (!bus_match_rule_set_interface (rule, value))
916 else if (strcmp (key, "member") == 0)
918 if (rule->flags & BUS_MATCH_MEMBER)
920 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
921 "Key %s specified twice in match rule\n", key);
925 if (!_dbus_validate_member (&tmp_str, 0, len))
927 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
928 "Member name '%s' is invalid\n", value);
932 if (!bus_match_rule_set_member (rule, value))
938 else if (strcmp (key, "path") == 0)
940 if (rule->flags & BUS_MATCH_PATH)
942 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
943 "Key %s specified twice in match rule\n", key);
947 if (!_dbus_validate_path (&tmp_str, 0, len))
949 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
950 "Path '%s' is invalid\n", value);
954 if (!bus_match_rule_set_path (rule, value))
960 else if (strcmp (key, "destination") == 0)
962 if (rule->flags & BUS_MATCH_DESTINATION)
964 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
965 "Key %s specified twice in match rule\n", key);
969 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
971 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
972 "Destination name '%s' is invalid\n", value);
976 if (!bus_match_rule_set_destination (rule, value))
982 else if (strncmp (key, "arg", 3) == 0)
984 if (!bus_match_rule_parse_arg_match (rule, key, &tmp_str, error))
989 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
990 "Unknown key \"%s\" in match rule",
1002 _DBUS_ASSERT_ERROR_IS_SET (error);
1005 bus_match_rule_unref (rule);
1012 while (tokens[i].key || tokens[i].value)
1014 _dbus_assert (i < MAX_RULE_TOKENS);
1015 dbus_free (tokens[i].key);
1016 dbus_free (tokens[i].value);
1023 typedef struct RulePool RulePool;
1026 /* Maps non-NULL interface names to non-NULL (DBusList **)s */
1027 DBusHashTable *rules_by_iface;
1029 /* List of BusMatchRules which don't specify an interface */
1030 DBusList *rules_without_iface;
1033 struct BusMatchmaker
1037 /* Pools of rules, grouped by the type of message they match. 0
1038 * (DBUS_MESSAGE_TYPE_INVALID) represents rules that do not specify a message
1041 RulePool rules_by_type[DBUS_NUM_MESSAGE_TYPES];
1045 rule_list_free (DBusList **rules)
1047 while (*rules != NULL)
1051 rule = (*rules)->data;
1052 bus_match_rule_unref (rule);
1053 _dbus_list_remove_link (rules, *rules);
1058 rule_list_ptr_free (DBusList **list)
1060 /* We have to cope with NULL because the hash table frees the "existing"
1061 * value (which is NULL) when creating a new table entry...
1065 rule_list_free (list);
1071 bus_matchmaker_new (void)
1073 BusMatchmaker *matchmaker;
1076 matchmaker = dbus_new0 (BusMatchmaker, 1);
1077 if (matchmaker == NULL)
1080 matchmaker->refcount = 1;
1082 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1084 RulePool *p = matchmaker->rules_by_type + i;
1086 p->rules_by_iface = _dbus_hash_table_new (DBUS_HASH_STRING,
1087 dbus_free, (DBusFreeFunction) rule_list_ptr_free);
1089 if (p->rules_by_iface == NULL)
1096 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1098 RulePool *p = matchmaker->rules_by_type + i;
1100 if (p->rules_by_iface == NULL)
1103 _dbus_hash_table_unref (p->rules_by_iface);
1110 bus_matchmaker_get_rules (BusMatchmaker *matchmaker,
1112 const char *interface,
1117 _dbus_assert (message_type >= 0);
1118 _dbus_assert (message_type < DBUS_NUM_MESSAGE_TYPES);
1120 _dbus_verbose ("Looking up rules for message_type %d, interface %s\n",
1122 interface != NULL ? interface : "<null>");
1124 p = matchmaker->rules_by_type + message_type;
1126 if (interface == NULL)
1128 return &p->rules_without_iface;
1134 list = _dbus_hash_table_lookup_string (p->rules_by_iface, interface);
1136 if (list == NULL && create)
1138 char *dupped_interface;
1140 list = dbus_new0 (DBusList *, 1);
1144 dupped_interface = _dbus_strdup (interface);
1145 if (dupped_interface == NULL)
1151 _dbus_verbose ("Adding list for type %d, iface %s\n", message_type,
1154 if (!_dbus_hash_table_insert_string (p->rules_by_iface,
1155 dupped_interface, list))
1158 dbus_free (dupped_interface);
1168 bus_matchmaker_gc_rules (BusMatchmaker *matchmaker,
1170 const char *interface,
1175 if (interface == NULL)
1181 _dbus_verbose ("GCing HT entry for message_type %u, interface %s\n",
1182 message_type, interface);
1184 p = matchmaker->rules_by_type + message_type;
1186 _dbus_assert (_dbus_hash_table_lookup_string (p->rules_by_iface, interface)
1189 _dbus_hash_table_remove_string (p->rules_by_iface, interface);
1193 bus_matchmaker_ref (BusMatchmaker *matchmaker)
1195 _dbus_assert (matchmaker->refcount > 0);
1197 matchmaker->refcount += 1;
1203 bus_matchmaker_unref (BusMatchmaker *matchmaker)
1205 _dbus_assert (matchmaker->refcount > 0);
1207 matchmaker->refcount -= 1;
1208 if (matchmaker->refcount == 0)
1212 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1214 RulePool *p = matchmaker->rules_by_type + i;
1216 _dbus_hash_table_unref (p->rules_by_iface);
1217 rule_list_free (&p->rules_without_iface);
1220 dbus_free (matchmaker);
1224 /* The rule can't be modified after it's added. */
1226 bus_matchmaker_add_rule (BusMatchmaker *matchmaker,
1231 _dbus_assert (bus_connection_is_active (rule->matches_go_to));
1233 _dbus_verbose ("Adding rule with message_type %d, interface %s\n",
1235 rule->interface != NULL ? rule->interface : "<null>");
1237 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1238 rule->interface, TRUE);
1243 if (!_dbus_list_append (rules, rule))
1246 if (!bus_connection_add_match_rule (rule->matches_go_to, rule))
1248 _dbus_list_remove_last (rules, rule);
1249 bus_matchmaker_gc_rules (matchmaker, rule->message_type,
1250 rule->interface, rules);
1254 bus_match_rule_ref (rule);
1256 #ifdef DBUS_ENABLE_VERBOSE_MODE
1258 char *s = match_rule_to_string (rule);
1260 _dbus_verbose ("Added match rule %s to connection %p\n",
1261 s, rule->matches_go_to);
1270 match_rule_equal (BusMatchRule *a,
1273 if (a->flags != b->flags)
1276 if (a->matches_go_to != b->matches_go_to)
1279 if ((a->flags & BUS_MATCH_MESSAGE_TYPE) &&
1280 a->message_type != b->message_type)
1283 if ((a->flags & BUS_MATCH_MEMBER) &&
1284 strcmp (a->member, b->member) != 0)
1287 if ((a->flags & BUS_MATCH_PATH) &&
1288 strcmp (a->path, b->path) != 0)
1291 if ((a->flags & BUS_MATCH_INTERFACE) &&
1292 strcmp (a->interface, b->interface) != 0)
1295 if ((a->flags & BUS_MATCH_SENDER) &&
1296 strcmp (a->sender, b->sender) != 0)
1299 if ((a->flags & BUS_MATCH_DESTINATION) &&
1300 strcmp (a->destination, b->destination) != 0)
1303 if (a->flags & BUS_MATCH_ARGS)
1307 if (a->args_len != b->args_len)
1311 while (i < a->args_len)
1315 if ((a->args[i] != NULL) != (b->args[i] != NULL))
1318 if (a->arg_lens[i] != b->arg_lens[i])
1321 length = a->arg_lens[i] & ~BUS_MATCH_ARG_IS_PATH;
1323 if (a->args[i] != NULL)
1325 _dbus_assert (b->args[i] != NULL);
1326 if (memcmp (a->args[i], b->args[i], length) != 0)
1338 bus_matchmaker_remove_rule_link (DBusList **rules,
1341 BusMatchRule *rule = link->data;
1343 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1344 _dbus_list_remove_link (rules, link);
1346 #ifdef DBUS_ENABLE_VERBOSE_MODE
1348 char *s = match_rule_to_string (rule);
1350 _dbus_verbose ("Removed match rule %s for connection %p\n",
1351 s, rule->matches_go_to);
1356 bus_match_rule_unref (rule);
1360 bus_matchmaker_remove_rule (BusMatchmaker *matchmaker,
1365 _dbus_verbose ("Removing rule with message_type %d, interface %s\n",
1367 rule->interface != NULL ? rule->interface : "<null>");
1369 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1371 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1372 rule->interface, FALSE);
1374 /* We should only be asked to remove a rule by identity right after it was
1375 * added, so there should be a list for it.
1377 _dbus_assert (rules != NULL);
1379 _dbus_list_remove (rules, rule);
1380 bus_matchmaker_gc_rules (matchmaker, rule->message_type, rule->interface,
1383 #ifdef DBUS_ENABLE_VERBOSE_MODE
1385 char *s = match_rule_to_string (rule);
1387 _dbus_verbose ("Removed match rule %s for connection %p\n",
1388 s, rule->matches_go_to);
1393 bus_match_rule_unref (rule);
1396 /* Remove a single rule which is equal to the given rule by value */
1398 bus_matchmaker_remove_rule_by_value (BusMatchmaker *matchmaker,
1399 BusMatchRule *value,
1403 DBusList *link = NULL;
1405 _dbus_verbose ("Removing rule by value with message_type %d, interface %s\n",
1406 value->message_type,
1407 value->interface != NULL ? value->interface : "<null>");
1409 rules = bus_matchmaker_get_rules (matchmaker, value->message_type,
1410 value->interface, FALSE);
1414 /* we traverse backward because bus_connection_remove_match_rule()
1415 * removes the most-recently-added rule
1417 link = _dbus_list_get_last_link (rules);
1418 while (link != NULL)
1424 prev = _dbus_list_get_prev_link (rules, link);
1426 if (match_rule_equal (rule, value))
1428 bus_matchmaker_remove_rule_link (rules, link);
1438 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_NOT_FOUND,
1439 "The given match rule wasn't found and can't be removed");
1443 bus_matchmaker_gc_rules (matchmaker, value->message_type, value->interface,
1450 rule_list_remove_by_connection (DBusList **rules,
1451 DBusConnection *connection)
1455 link = _dbus_list_get_first_link (rules);
1456 while (link != NULL)
1462 next = _dbus_list_get_next_link (rules, link);
1464 if (rule->matches_go_to == connection)
1466 bus_matchmaker_remove_rule_link (rules, link);
1468 else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') ||
1469 ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':'))
1471 /* The rule matches to/from a base service, see if it's the
1472 * one being disconnected, since we know this service name
1473 * will never be recycled.
1477 name = bus_connection_get_name (connection);
1478 _dbus_assert (name != NULL); /* because we're an active connection */
1480 if (((rule->flags & BUS_MATCH_SENDER) &&
1481 strcmp (rule->sender, name) == 0) ||
1482 ((rule->flags & BUS_MATCH_DESTINATION) &&
1483 strcmp (rule->destination, name) == 0))
1485 bus_matchmaker_remove_rule_link (rules, link);
1494 bus_matchmaker_disconnected (BusMatchmaker *matchmaker,
1495 DBusConnection *connection)
1501 * This scans all match rules on the bus. We could avoid that
1502 * for the rules belonging to the connection, since we keep
1503 * a list of those; but for the rules that just refer to
1504 * the connection we'd need to do something more elaborate.
1507 _dbus_assert (bus_connection_is_active (connection));
1509 _dbus_verbose ("Removing all rules for connection %p\n", connection);
1511 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1513 RulePool *p = matchmaker->rules_by_type + i;
1516 rule_list_remove_by_connection (&p->rules_without_iface, connection);
1518 _dbus_hash_iter_init (p->rules_by_iface, &iter);
1519 while (_dbus_hash_iter_next (&iter))
1521 DBusList **items = _dbus_hash_iter_get_value (&iter);
1523 rule_list_remove_by_connection (items, connection);
1526 _dbus_hash_iter_remove_entry (&iter);
1532 connection_is_primary_owner (DBusConnection *connection,
1533 const char *service_name)
1535 BusService *service;
1537 BusRegistry *registry;
1539 _dbus_assert (connection != NULL);
1541 registry = bus_connection_get_registry (connection);
1543 _dbus_string_init_const (&str, service_name);
1544 service = bus_registry_lookup (registry, &str);
1546 if (service == NULL)
1547 return FALSE; /* Service doesn't exist so connection can't own it. */
1549 return bus_service_get_primary_owners_connection (service) == connection;
1553 match_rule_matches (BusMatchRule *rule,
1554 DBusConnection *sender,
1555 DBusConnection *addressed_recipient,
1556 DBusMessage *message,
1557 BusMatchFlags already_matched)
1561 /* All features of the match rule are AND'd together,
1562 * so FALSE if any of them don't match.
1565 /* sender/addressed_recipient of #NULL may mean bus driver,
1566 * or for addressed_recipient may mean a message with no
1567 * specific recipient (i.e. a signal)
1570 /* Don't bother re-matching features we've already checked implicitly. */
1571 flags = rule->flags & (~already_matched);
1573 if (flags & BUS_MATCH_MESSAGE_TYPE)
1575 _dbus_assert (rule->message_type != DBUS_MESSAGE_TYPE_INVALID);
1577 if (rule->message_type != dbus_message_get_type (message))
1581 if (flags & BUS_MATCH_INTERFACE)
1585 _dbus_assert (rule->interface != NULL);
1587 iface = dbus_message_get_interface (message);
1591 if (strcmp (iface, rule->interface) != 0)
1595 if (flags & BUS_MATCH_MEMBER)
1599 _dbus_assert (rule->member != NULL);
1601 member = dbus_message_get_member (message);
1605 if (strcmp (member, rule->member) != 0)
1609 if (flags & BUS_MATCH_SENDER)
1611 _dbus_assert (rule->sender != NULL);
1615 if (strcmp (rule->sender,
1616 DBUS_SERVICE_DBUS) != 0)
1621 if (!connection_is_primary_owner (sender, rule->sender))
1626 if (flags & BUS_MATCH_DESTINATION)
1628 const char *destination;
1630 _dbus_assert (rule->destination != NULL);
1632 destination = dbus_message_get_destination (message);
1633 if (destination == NULL)
1636 if (addressed_recipient == NULL)
1638 if (strcmp (rule->destination,
1639 DBUS_SERVICE_DBUS) != 0)
1644 if (!connection_is_primary_owner (addressed_recipient, rule->destination))
1649 if (flags & BUS_MATCH_PATH)
1653 _dbus_assert (rule->path != NULL);
1655 path = dbus_message_get_path (message);
1659 if (strcmp (path, rule->path) != 0)
1663 if (flags & BUS_MATCH_ARGS)
1666 DBusMessageIter iter;
1668 _dbus_assert (rule->args != NULL);
1670 dbus_message_iter_init (message, &iter);
1673 while (i < rule->args_len)
1676 const char *expected_arg;
1677 int expected_length;
1678 dbus_bool_t is_path;
1680 expected_arg = rule->args[i];
1681 expected_length = rule->arg_lens[i] & ~BUS_MATCH_ARG_IS_PATH;
1682 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
1684 current_type = dbus_message_iter_get_arg_type (&iter);
1686 if (expected_arg != NULL)
1688 const char *actual_arg;
1691 if (current_type != DBUS_TYPE_STRING)
1695 dbus_message_iter_get_basic (&iter, &actual_arg);
1696 _dbus_assert (actual_arg != NULL);
1698 actual_length = strlen (actual_arg);
1702 if (actual_length < expected_length &&
1703 actual_arg[actual_length - 1] != '/')
1706 if (expected_length < actual_length &&
1707 expected_arg[expected_length - 1] != '/')
1710 if (memcmp (actual_arg, expected_arg,
1711 MIN (actual_length, expected_length)) != 0)
1716 if (expected_length != actual_length ||
1717 memcmp (expected_arg, actual_arg, expected_length) != 0)
1723 if (current_type != DBUS_TYPE_INVALID)
1724 dbus_message_iter_next (&iter);
1734 get_recipients_from_list (DBusList **rules,
1735 DBusConnection *sender,
1736 DBusConnection *addressed_recipient,
1737 DBusMessage *message,
1738 DBusList **recipients_p)
1745 link = _dbus_list_get_first_link (rules);
1746 while (link != NULL)
1752 #ifdef DBUS_ENABLE_VERBOSE_MODE
1754 char *s = match_rule_to_string (rule);
1756 _dbus_verbose ("Checking whether message matches rule %s for connection %p\n",
1757 s, rule->matches_go_to);
1762 if (match_rule_matches (rule,
1763 sender, addressed_recipient, message,
1764 BUS_MATCH_MESSAGE_TYPE | BUS_MATCH_INTERFACE))
1766 _dbus_verbose ("Rule matched\n");
1768 /* Append to the list if we haven't already */
1769 if (bus_connection_mark_stamp (rule->matches_go_to))
1771 if (!_dbus_list_append (recipients_p, rule->matches_go_to))
1774 #ifdef DBUS_ENABLE_VERBOSE_MODE
1777 _dbus_verbose ("Connection already receiving this message, so not adding again\n");
1779 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1782 link = _dbus_list_get_next_link (rules, link);
1789 bus_matchmaker_get_recipients (BusMatchmaker *matchmaker,
1790 BusConnections *connections,
1791 DBusConnection *sender,
1792 DBusConnection *addressed_recipient,
1793 DBusMessage *message,
1794 DBusList **recipients_p)
1797 const char *interface;
1798 DBusList **neither, **just_type, **just_iface, **both;
1800 _dbus_assert (*recipients_p == NULL);
1802 /* This avoids sending same message to the same connection twice.
1803 * Purpose of the stamp instead of a bool is to avoid iterating over
1804 * all connections resetting the bool each time.
1806 bus_connections_increment_stamp (connections);
1808 /* addressed_recipient is already receiving the message, don't add to list.
1809 * NULL addressed_recipient means either bus driver, or this is a signal
1810 * and thus lacks a specific addressed_recipient.
1812 if (addressed_recipient != NULL)
1813 bus_connection_mark_stamp (addressed_recipient);
1815 type = dbus_message_get_type (message);
1816 interface = dbus_message_get_interface (message);
1818 neither = bus_matchmaker_get_rules (matchmaker, DBUS_MESSAGE_TYPE_INVALID,
1820 just_type = just_iface = both = NULL;
1822 if (interface != NULL)
1823 just_iface = bus_matchmaker_get_rules (matchmaker,
1824 DBUS_MESSAGE_TYPE_INVALID, interface, FALSE);
1826 if (type > DBUS_MESSAGE_TYPE_INVALID && type < DBUS_NUM_MESSAGE_TYPES)
1828 just_type = bus_matchmaker_get_rules (matchmaker, type, NULL, FALSE);
1830 if (interface != NULL)
1831 both = bus_matchmaker_get_rules (matchmaker, type, interface, FALSE);
1834 if (!(get_recipients_from_list (neither, sender, addressed_recipient,
1835 message, recipients_p) &&
1836 get_recipients_from_list (just_iface, sender, addressed_recipient,
1837 message, recipients_p) &&
1838 get_recipients_from_list (just_type, sender, addressed_recipient,
1839 message, recipients_p) &&
1840 get_recipients_from_list (both, sender, addressed_recipient,
1841 message, recipients_p)))
1843 _dbus_list_clear (recipients_p);
1850 #ifdef DBUS_BUILD_TESTS
1854 static BusMatchRule*
1855 check_parse (dbus_bool_t should_succeed,
1862 dbus_error_init (&error);
1864 _dbus_string_init_const (&str, text);
1866 rule = bus_match_rule_parse (NULL, &str, &error);
1867 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
1869 dbus_error_free (&error);
1873 if (should_succeed && rule == NULL)
1875 _dbus_warn ("Failed to parse: %s: %s: \"%s\"\n",
1876 error.name, error.message,
1877 _dbus_string_get_const_data (&str));
1881 if (!should_succeed && rule != NULL)
1883 _dbus_warn ("Failed to fail to parse: \"%s\"\n",
1884 _dbus_string_get_const_data (&str));
1888 dbus_error_free (&error);
1894 assert_large_rule (BusMatchRule *rule)
1896 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
1897 _dbus_assert (rule->flags & BUS_MATCH_SENDER);
1898 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
1899 _dbus_assert (rule->flags & BUS_MATCH_MEMBER);
1900 _dbus_assert (rule->flags & BUS_MATCH_DESTINATION);
1901 _dbus_assert (rule->flags & BUS_MATCH_PATH);
1903 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
1904 _dbus_assert (rule->interface != NULL);
1905 _dbus_assert (rule->member != NULL);
1906 _dbus_assert (rule->sender != NULL);
1907 _dbus_assert (rule->destination != NULL);
1908 _dbus_assert (rule->path != NULL);
1910 _dbus_assert (strcmp (rule->interface, "org.freedesktop.DBusInterface") == 0);
1911 _dbus_assert (strcmp (rule->sender, "org.freedesktop.DBusSender") == 0);
1912 _dbus_assert (strcmp (rule->member, "Foo") == 0);
1913 _dbus_assert (strcmp (rule->path, "/bar/foo") == 0);
1914 _dbus_assert (strcmp (rule->destination, ":452345.34") == 0);
1918 test_parsing (void *data)
1922 rule = check_parse (TRUE, "type='signal',sender='org.freedesktop.DBusSender',interface='org.freedesktop.DBusInterface',member='Foo',path='/bar/foo',destination=':452345.34'");
1925 assert_large_rule (rule);
1926 bus_match_rule_unref (rule);
1929 /* With extra whitespace and useless quotes */
1930 rule = check_parse (TRUE, " type='signal', \tsender='org.freedes''ktop.DBusSender', interface='org.freedesktop.DBusInterface''''', \tmember='Foo',path='/bar/foo',destination=':452345.34'''''");
1933 assert_large_rule (rule);
1934 bus_match_rule_unref (rule);
1938 /* A simple signal connection */
1939 rule = check_parse (TRUE, "type='signal',path='/foo',interface='org.Bar'");
1942 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
1943 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
1944 _dbus_assert (rule->flags & BUS_MATCH_PATH);
1946 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
1947 _dbus_assert (rule->interface != NULL);
1948 _dbus_assert (rule->path != NULL);
1950 _dbus_assert (strcmp (rule->interface, "org.Bar") == 0);
1951 _dbus_assert (strcmp (rule->path, "/foo") == 0);
1953 bus_match_rule_unref (rule);
1957 rule = check_parse (TRUE, "arg0='foo'");
1960 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
1961 _dbus_assert (rule->args != NULL);
1962 _dbus_assert (rule->args_len == 1);
1963 _dbus_assert (rule->args[0] != NULL);
1964 _dbus_assert (rule->args[1] == NULL);
1965 _dbus_assert (strcmp (rule->args[0], "foo") == 0);
1967 bus_match_rule_unref (rule);
1970 rule = check_parse (TRUE, "arg1='foo'");
1973 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
1974 _dbus_assert (rule->args != NULL);
1975 _dbus_assert (rule->args_len == 2);
1976 _dbus_assert (rule->args[0] == NULL);
1977 _dbus_assert (rule->args[1] != NULL);
1978 _dbus_assert (rule->args[2] == NULL);
1979 _dbus_assert (strcmp (rule->args[1], "foo") == 0);
1981 bus_match_rule_unref (rule);
1984 rule = check_parse (TRUE, "arg2='foo'");
1987 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
1988 _dbus_assert (rule->args != NULL);
1989 _dbus_assert (rule->args_len == 3);
1990 _dbus_assert (rule->args[0] == NULL);
1991 _dbus_assert (rule->args[1] == NULL);
1992 _dbus_assert (rule->args[2] != NULL);
1993 _dbus_assert (rule->args[3] == NULL);
1994 _dbus_assert (strcmp (rule->args[2], "foo") == 0);
1996 bus_match_rule_unref (rule);
1999 rule = check_parse (TRUE, "arg40='foo'");
2002 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2003 _dbus_assert (rule->args != NULL);
2004 _dbus_assert (rule->args_len == 41);
2005 _dbus_assert (rule->args[0] == NULL);
2006 _dbus_assert (rule->args[1] == NULL);
2007 _dbus_assert (rule->args[40] != NULL);
2008 _dbus_assert (rule->args[41] == NULL);
2009 _dbus_assert (strcmp (rule->args[40], "foo") == 0);
2011 bus_match_rule_unref (rule);
2014 rule = check_parse (TRUE, "arg63='foo'");
2017 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2018 _dbus_assert (rule->args != NULL);
2019 _dbus_assert (rule->args_len == 64);
2020 _dbus_assert (rule->args[0] == NULL);
2021 _dbus_assert (rule->args[1] == NULL);
2022 _dbus_assert (rule->args[63] != NULL);
2023 _dbus_assert (rule->args[64] == NULL);
2024 _dbus_assert (strcmp (rule->args[63], "foo") == 0);
2026 bus_match_rule_unref (rule);
2029 /* Too-large argN */
2030 rule = check_parse (FALSE, "arg300='foo'");
2031 _dbus_assert (rule == NULL);
2032 rule = check_parse (FALSE, "arg64='foo'");
2033 _dbus_assert (rule == NULL);
2036 rule = check_parse (FALSE, "arg='foo'");
2037 _dbus_assert (rule == NULL);
2038 rule = check_parse (FALSE, "argv='foo'");
2039 _dbus_assert (rule == NULL);
2040 rule = check_parse (FALSE, "arg3junk='foo'");
2041 _dbus_assert (rule == NULL);
2042 rule = check_parse (FALSE, "argument='foo'");
2043 _dbus_assert (rule == NULL);
2045 /* Reject duplicates */
2046 rule = check_parse (FALSE, "type='signal',type='method_call'");
2047 _dbus_assert (rule == NULL);
2049 /* Duplicates with the argN code */
2050 rule = check_parse (FALSE, "arg0='foo',arg0='bar'");
2051 _dbus_assert (rule == NULL);
2052 rule = check_parse (FALSE, "arg3='foo',arg3='bar'");
2053 _dbus_assert (rule == NULL);
2054 rule = check_parse (FALSE, "arg30='foo',arg30='bar'");
2055 _dbus_assert (rule == NULL);
2057 /* Reject broken keys */
2058 rule = check_parse (FALSE, "blah='signal'");
2059 _dbus_assert (rule == NULL);
2061 /* Reject broken values */
2062 rule = check_parse (FALSE, "type='chouin'");
2063 _dbus_assert (rule == NULL);
2064 rule = check_parse (FALSE, "interface='abc@def++'");
2065 _dbus_assert (rule == NULL);
2066 rule = check_parse (FALSE, "service='youpi'");
2067 _dbus_assert (rule == NULL);
2069 /* Allow empty rule */
2070 rule = check_parse (TRUE, "");
2073 _dbus_assert (rule->flags == 0);
2075 bus_match_rule_unref (rule);
2078 /* All-whitespace rule is the same as empty */
2079 rule = check_parse (TRUE, " \t");
2082 _dbus_assert (rule->flags == 0);
2084 bus_match_rule_unref (rule);
2087 /* But with non-whitespace chars and no =value, it's not OK */
2088 rule = check_parse (FALSE, "type");
2089 _dbus_assert (rule == NULL);
2097 } equality_tests[] = {
2098 { "type='signal'", "type='signal'" },
2099 { "type='signal',interface='foo.bar'", "interface='foo.bar',type='signal'" },
2100 { "type='signal',member='bar'", "member='bar',type='signal'" },
2101 { "type='method_call',sender=':1.0'", "sender=':1.0',type='method_call'" },
2102 { "type='method_call',destination=':1.0'", "destination=':1.0',type='method_call'" },
2103 { "type='method_call',path='/foo/bar'", "path='/foo/bar',type='method_call'" },
2104 { "type='method_call',arg0='blah'", "arg0='blah',type='method_call'" },
2105 { "type='method_call',arg0='boo'", "arg0='boo',type='method_call'" },
2106 { "type='method_call',arg0='blah',arg1='baz'", "arg0='blah',arg1='baz',type='method_call'" },
2107 { "type='method_call',arg3='foosh'", "arg3='foosh',type='method_call'" },
2108 { "arg3='fool'", "arg3='fool'" },
2109 { "member='food'", "member='food'" }
2113 test_equality (void)
2118 while (i < _DBUS_N_ELEMENTS (equality_tests))
2120 BusMatchRule *first;
2121 BusMatchRule *second;
2124 first = check_parse (TRUE, equality_tests[i].first);
2125 _dbus_assert (first != NULL);
2126 second = check_parse (TRUE, equality_tests[i].second);
2127 _dbus_assert (second != NULL);
2129 if (!match_rule_equal (first, second))
2131 _dbus_warn ("rule %s and %s should have been equal\n",
2132 equality_tests[i].first,
2133 equality_tests[i].second);
2137 bus_match_rule_unref (second);
2139 /* Check that the rule is not equal to any of the
2140 * others besides its pair match
2143 while (j < _DBUS_N_ELEMENTS (equality_tests))
2147 second = check_parse (TRUE, equality_tests[j].second);
2149 if (match_rule_equal (first, second))
2151 _dbus_warn ("rule %s and %s should not have been equal\n",
2152 equality_tests[i].first,
2153 equality_tests[j].second);
2157 bus_match_rule_unref (second);
2163 bus_match_rule_unref (first);
2170 should_match_message_1[] = {
2172 "member='Frobated'",
2174 "type='signal',member='Frobated'",
2175 "type='signal',member='Frobated',arg0='foobar'",
2176 "member='Frobated',arg0='foobar'",
2177 "type='signal',arg0='foobar'",
2182 should_not_match_message_1[] = {
2183 "type='method_call'",
2185 "type='method_return'",
2186 "type='signal',member='Oopsed'",
2193 "arg0='foobar',arg1='abcdef'",
2194 "arg0='foobar',arg1='abcdef',arg2='abcdefghi',arg3='abcdefghi',arg4='abcdefghi'",
2195 "arg0='foobar',arg1='abcdef',arg4='abcdefghi',arg3='abcdefghi',arg2='abcdefghi'",
2200 check_matches (dbus_bool_t expected_to_match,
2202 DBusMessage *message,
2203 const char *rule_text)
2206 dbus_bool_t matched;
2208 rule = check_parse (TRUE, rule_text);
2209 _dbus_assert (rule != NULL);
2211 /* We can't test sender/destination rules since we pass NULL here */
2212 matched = match_rule_matches (rule, NULL, NULL, message, 0);
2214 if (matched != expected_to_match)
2216 _dbus_warn ("Expected rule %s to %s message %d, failed\n",
2217 rule_text, expected_to_match ?
2218 "match" : "not match", number);
2222 bus_match_rule_unref (rule);
2226 check_matching (DBusMessage *message,
2228 const char **should_match,
2229 const char **should_not_match)
2234 while (should_match[i] != NULL)
2236 check_matches (TRUE, number, message, should_match[i]);
2241 while (should_not_match[i] != NULL)
2243 check_matches (FALSE, number, message, should_not_match[i]);
2249 test_matching (void)
2251 DBusMessage *message1;
2252 const char *v_STRING;
2253 dbus_int32_t v_INT32;
2255 message1 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2256 _dbus_assert (message1 != NULL);
2257 if (!dbus_message_set_member (message1, "Frobated"))
2258 _dbus_assert_not_reached ("oom");
2260 v_STRING = "foobar";
2262 if (!dbus_message_append_args (message1,
2263 DBUS_TYPE_STRING, &v_STRING,
2264 DBUS_TYPE_INT32, &v_INT32,
2266 _dbus_assert_not_reached ("oom");
2268 check_matching (message1, 1,
2269 should_match_message_1,
2270 should_not_match_message_1);
2272 dbus_message_unref (message1);
2276 bus_signals_test (const DBusString *test_data_dir)
2278 BusMatchmaker *matchmaker;
2280 matchmaker = bus_matchmaker_new ();
2281 bus_matchmaker_ref (matchmaker);
2282 bus_matchmaker_unref (matchmaker);
2283 bus_matchmaker_unref (matchmaker);
2285 if (!_dbus_test_oom_handling ("parsing match rules", test_parsing, NULL))
2286 _dbus_assert_not_reached ("Parsing match rules test failed");
2295 #endif /* DBUS_BUILD_TESTS */