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_set_path (BusMatchRule *rule,
416 dbus_bool_t is_namespace)
420 _dbus_assert (path != NULL);
422 new = _dbus_strdup (path);
426 rule->flags &= ~(BUS_MATCH_PATH|BUS_MATCH_PATH_NAMESPACE);
429 rule->flags |= BUS_MATCH_PATH_NAMESPACE;
431 rule->flags |= BUS_MATCH_PATH;
433 dbus_free (rule->path);
440 bus_match_rule_set_arg (BusMatchRule *rule,
442 const DBusString *value,
444 dbus_bool_t is_namespace)
449 _dbus_assert (value != NULL);
451 /* args_len is the number of args not including null termination
454 if (arg >= rule->args_len)
456 unsigned int *new_arg_lens;
461 new_args_len = arg + 1;
463 /* add another + 1 here for null termination */
464 new_args = dbus_realloc (rule->args,
465 sizeof (char *) * (new_args_len + 1));
466 if (new_args == NULL)
469 /* NULL the new slots */
471 while (i <= new_args_len) /* <= for null termination */
477 rule->args = new_args;
479 /* and now add to the lengths */
480 new_arg_lens = dbus_realloc (rule->arg_lens,
481 sizeof (int) * (new_args_len + 1));
483 if (new_arg_lens == NULL)
486 /* zero the new slots */
488 while (i <= new_args_len) /* <= for null termination */
494 rule->arg_lens = new_arg_lens;
495 rule->args_len = new_args_len;
498 length = _dbus_string_get_length (value);
499 if (!_dbus_string_copy_data (value, &new))
502 rule->flags |= BUS_MATCH_ARGS;
504 dbus_free (rule->args[arg]);
505 rule->arg_lens[arg] = length;
506 rule->args[arg] = new;
509 rule->arg_lens[arg] |= BUS_MATCH_ARG_IS_PATH;
512 rule->arg_lens[arg] |= BUS_MATCH_ARG_NAMESPACE;
514 /* NULL termination didn't get busted */
515 _dbus_assert (rule->args[rule->args_len] == NULL);
516 _dbus_assert (rule->arg_lens[rule->args_len] == 0);
521 #define ISWHITE(c) (((c) == ' ') || ((c) == '\t') || ((c) == '\n') || ((c) == '\r'))
524 find_key (const DBusString *str,
532 const char *key_start;
535 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
537 s = _dbus_string_get_const_data (str);
541 while (*p && ISWHITE (*p))
546 while (*p && *p != '=' && !ISWHITE (*p))
551 while (*p && ISWHITE (*p))
554 if (key_start == key_end)
556 /* Empty match rules or trailing whitespace are OK */
563 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
564 "Match rule has a key with no subsequent '=' character");
569 if (!_dbus_string_append_len (key, key_start, key_end - key_start))
581 find_value (const DBusString *str,
593 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
595 orig_len = _dbus_string_get_length (value);
597 s = _dbus_string_get_const_data (str);
605 if (quote_char == '\0')
625 if (!_dbus_string_append_byte (value, *p))
632 else if (quote_char == '\\')
634 /* \ only counts as an escape if escaping a quote mark */
637 if (!_dbus_string_append_byte (value, '\\'))
644 if (!_dbus_string_append_byte (value, *p))
654 _dbus_assert (quote_char == '\'');
662 if (!_dbus_string_append_byte (value, *p))
676 if (quote_char == '\\')
678 if (!_dbus_string_append_byte (value, '\\'))
684 else if (quote_char == '\'')
686 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
687 "Unbalanced quotation marks in match rule");
691 _dbus_assert (quote_char == '\0');
693 /* Zero-length values are allowed */
700 _DBUS_ASSERT_ERROR_IS_SET (error);
701 _dbus_string_set_length (value, orig_len);
705 /* duplicates aren't allowed so the real legitimate max is only 6 or
706 * so. Leaving extra so we don't have to bother to update it.
707 * FIXME this is sort of busted now with arg matching, but we let
708 * you match on up to 10 args for now
710 #define MAX_RULE_TOKENS 16
712 /* this is slightly too high level to be termed a "token"
713 * but let's not be pedantic.
722 tokenize_rule (const DBusString *rule_text,
723 RuleToken tokens[MAX_RULE_TOKENS],
734 if (!_dbus_string_init (&key))
740 if (!_dbus_string_init (&value))
742 _dbus_string_free (&key);
749 while (i < MAX_RULE_TOKENS &&
750 pos < _dbus_string_get_length (rule_text))
752 _dbus_assert (tokens[i].key == NULL);
753 _dbus_assert (tokens[i].value == NULL);
755 if (!find_key (rule_text, pos, &key, &pos, error))
758 if (_dbus_string_get_length (&key) == 0)
761 if (!_dbus_string_steal_data (&key, &tokens[i].key))
767 if (!find_value (rule_text, pos, tokens[i].key, &value, &pos, error))
770 if (!_dbus_string_steal_data (&value, &tokens[i].value))
786 while (tokens[i].key || tokens[i].value)
788 dbus_free (tokens[i].key);
789 dbus_free (tokens[i].value);
790 tokens[i].key = NULL;
791 tokens[i].value = NULL;
796 _dbus_string_free (&key);
797 _dbus_string_free (&value);
803 bus_match_rule_parse_arg_match (BusMatchRule *rule,
805 const DBusString *value,
808 dbus_bool_t is_path = FALSE;
809 dbus_bool_t is_namespace = FALSE;
815 /* For now, arg0='foo' always implies that 'foo' is a
816 * DBUS_TYPE_STRING. Someday we could add an arg0type='int32' thing
817 * if we wanted, which would specify another type, in which case
818 * arg0='5' would have the 5 parsed as an int rather than string.
821 /* First we need to parse arg0 = 0, arg27 = 27 */
823 _dbus_string_init_const (&key_str, key);
824 length = _dbus_string_get_length (&key_str);
826 if (_dbus_string_get_length (&key_str) < 4)
828 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
829 "Key '%s' in match rule starts with 'arg' but lacks an arg number. Should be 'arg0' or 'arg7' for example.\n", key);
833 if (!_dbus_string_parse_uint (&key_str, 3, &arg, &end))
835 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
836 "Key '%s' in match rule starts with 'arg' but could not parse arg number. Should be 'arg0' or 'arg7' for example.\n", key);
842 if ((end + strlen ("path")) == length &&
843 _dbus_string_ends_with_c_str (&key_str, "path"))
847 else if (_dbus_string_equal_c_str (&key_str, "arg0namespace"))
849 int value_len = _dbus_string_get_length (value);
853 if (!_dbus_validate_bus_namespace (value, 0, value_len))
855 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
856 "arg0namespace='%s' is not a valid prefix of a bus name",
857 _dbus_string_get_const_data (value));
863 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
864 "Key '%s' in match rule contains junk after argument number (%u). Only 'arg%upath' (for example) or 'arg0namespace' are valid", key, arg, arg);
869 /* If we didn't check this we could allocate a huge amount of RAM */
870 if (arg > DBUS_MAXIMUM_MATCH_RULE_ARG_NUMBER)
872 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
873 "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);
877 if ((rule->flags & BUS_MATCH_ARGS) &&
878 rule->args_len > (int) arg &&
879 rule->args[arg] != NULL)
881 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
882 "Argument %d matched more than once in match rule\n", key);
886 if (!bus_match_rule_set_arg (rule, arg, value, is_path, is_namespace))
895 _DBUS_ASSERT_ERROR_IS_SET (error);
900 * The format is comma-separated with strings quoted with single quotes
901 * as for the shell (to escape a literal single quote, use '\'').
903 * type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',
904 * path='/bar/foo',destination=':452345.34'
908 bus_match_rule_parse (DBusConnection *matches_go_to,
909 const DBusString *rule_text,
913 RuleToken tokens[MAX_RULE_TOKENS+1]; /* NULL termination + 1 */
916 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
918 if (_dbus_string_get_length (rule_text) > DBUS_MAXIMUM_MATCH_RULE_LENGTH)
920 dbus_set_error (error, DBUS_ERROR_LIMITS_EXCEEDED,
921 "Match rule text is %d bytes, maximum is %d",
922 _dbus_string_get_length (rule_text),
923 DBUS_MAXIMUM_MATCH_RULE_LENGTH);
927 memset (tokens, '\0', sizeof (tokens));
929 rule = bus_match_rule_new (matches_go_to);
936 if (!tokenize_rule (rule_text, tokens, error))
940 while (tokens[i].key != NULL)
944 const char *key = tokens[i].key;
945 const char *value = tokens[i].value;
947 _dbus_string_init_const (&tmp_str, value);
948 len = _dbus_string_get_length (&tmp_str);
950 if (strcmp (key, "type") == 0)
954 if (rule->flags & BUS_MATCH_MESSAGE_TYPE)
956 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
957 "Key %s specified twice in match rule\n", key);
961 t = dbus_message_type_from_string (value);
963 if (t == DBUS_MESSAGE_TYPE_INVALID)
965 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
966 "Invalid message type (%s) in match rule\n", value);
970 if (!bus_match_rule_set_message_type (rule, t))
976 else if (strcmp (key, "sender") == 0)
978 if (rule->flags & BUS_MATCH_SENDER)
980 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
981 "Key %s specified twice in match rule\n", key);
985 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
987 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
988 "Sender name '%s' is invalid\n", value);
992 if (!bus_match_rule_set_sender (rule, value))
998 else if (strcmp (key, "interface") == 0)
1000 if (rule->flags & BUS_MATCH_INTERFACE)
1002 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1003 "Key %s specified twice in match rule\n", key);
1007 if (!_dbus_validate_interface (&tmp_str, 0, len))
1009 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1010 "Interface name '%s' is invalid\n", value);
1014 if (!bus_match_rule_set_interface (rule, value))
1016 BUS_SET_OOM (error);
1020 else if (strcmp (key, "member") == 0)
1022 if (rule->flags & BUS_MATCH_MEMBER)
1024 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1025 "Key %s specified twice in match rule\n", key);
1029 if (!_dbus_validate_member (&tmp_str, 0, len))
1031 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1032 "Member name '%s' is invalid\n", value);
1036 if (!bus_match_rule_set_member (rule, value))
1038 BUS_SET_OOM (error);
1042 else if (strcmp (key, "path") == 0 ||
1043 strcmp (key, "path_namespace") == 0)
1045 dbus_bool_t is_namespace = (strcmp (key, "path_namespace") == 0);
1047 if (rule->flags & (BUS_MATCH_PATH | BUS_MATCH_PATH_NAMESPACE))
1049 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1050 "path or path_namespace specified twice in match rule\n");
1054 if (!_dbus_validate_path (&tmp_str, 0, len))
1056 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1057 "Path '%s' is invalid\n", value);
1061 if (!bus_match_rule_set_path (rule, value, is_namespace))
1063 BUS_SET_OOM (error);
1067 else if (strcmp (key, "destination") == 0)
1069 if (rule->flags & BUS_MATCH_DESTINATION)
1071 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1072 "Key %s specified twice in match rule\n", key);
1076 if (!_dbus_validate_bus_name (&tmp_str, 0, len))
1078 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1079 "Destination name '%s' is invalid\n", value);
1083 if (!bus_match_rule_set_destination (rule, value))
1085 BUS_SET_OOM (error);
1089 else if (strcmp (key, "eavesdrop") == 0)
1091 /* do not detect "eavesdrop" being used more than once in rule:
1092 * 1) it's not possible, it's only in the flags
1093 * 2) it might be used twice to disable eavesdropping when it's
1094 * automatically added (eg dbus-monitor/bustle) */
1096 /* we accept only "true|false" as possible values */
1097 if ((strcmp (value, "true") == 0))
1099 bus_match_rule_set_client_is_eavesdropping (rule, TRUE);
1101 else if (strcmp (value, "false") == 0)
1103 bus_match_rule_set_client_is_eavesdropping (rule, FALSE);
1107 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1108 "eavesdrop='%s' is invalid, "
1109 "it should be 'true' or 'false'\n",
1114 else if (strncmp (key, "arg", 3) == 0)
1116 if (!bus_match_rule_parse_arg_match (rule, key, &tmp_str, error))
1121 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_INVALID,
1122 "Unknown key \"%s\" in match rule",
1134 _DBUS_ASSERT_ERROR_IS_SET (error);
1137 bus_match_rule_unref (rule);
1144 while (tokens[i].key || tokens[i].value)
1146 _dbus_assert (i < MAX_RULE_TOKENS);
1147 dbus_free (tokens[i].key);
1148 dbus_free (tokens[i].value);
1155 typedef struct RulePool RulePool;
1158 /* Maps non-NULL interface names to non-NULL (DBusList **)s */
1159 DBusHashTable *rules_by_iface;
1161 /* List of BusMatchRules which don't specify an interface */
1162 DBusList *rules_without_iface;
1165 struct BusMatchmaker
1169 /* Pools of rules, grouped by the type of message they match. 0
1170 * (DBUS_MESSAGE_TYPE_INVALID) represents rules that do not specify a message
1173 RulePool rules_by_type[DBUS_NUM_MESSAGE_TYPES];
1176 #ifdef DBUS_ENABLE_STATS
1178 bus_match_rule_dump (BusMatchmaker *matchmaker,
1179 DBusConnection *conn_filter,
1180 DBusMessageIter *arr_iter)
1184 for (i = 0 ; i < DBUS_NUM_MESSAGE_TYPES ; i++)
1190 _dbus_hash_iter_init (matchmaker->rules_by_type[i].rules_by_iface, &iter);
1191 while (_dbus_hash_iter_next (&iter))
1193 list = _dbus_hash_iter_get_value (&iter);
1194 for (link = _dbus_list_get_first_link (list);
1196 link = _dbus_list_get_next_link (list, link))
1198 BusMatchRule *rule = link->data;
1200 if (rule->matches_go_to == conn_filter)
1202 char *s = match_rule_to_string (rule);
1207 if (!dbus_message_iter_append_basic (arr_iter, DBUS_TYPE_STRING, &s))
1216 list = &matchmaker->rules_by_type[i].rules_without_iface;
1217 for (link = _dbus_list_get_first_link (list);
1219 link = _dbus_list_get_next_link (list, link))
1221 BusMatchRule *rule = link->data;
1223 if (rule->matches_go_to == conn_filter)
1225 char *s = match_rule_to_string (rule);
1230 if (!dbus_message_iter_append_basic (arr_iter, DBUS_TYPE_STRING, &s))
1245 rule_list_free (DBusList **rules)
1247 while (*rules != NULL)
1251 rule = (*rules)->data;
1252 bus_match_rule_unref (rule);
1253 _dbus_list_remove_link (rules, *rules);
1258 rule_list_ptr_free (DBusList **list)
1260 /* We have to cope with NULL because the hash table frees the "existing"
1261 * value (which is NULL) when creating a new table entry...
1265 rule_list_free (list);
1271 bus_matchmaker_new (void)
1273 BusMatchmaker *matchmaker;
1276 matchmaker = dbus_new0 (BusMatchmaker, 1);
1277 if (matchmaker == NULL)
1280 matchmaker->refcount = 1;
1282 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1284 RulePool *p = matchmaker->rules_by_type + i;
1286 p->rules_by_iface = _dbus_hash_table_new (DBUS_HASH_STRING,
1287 dbus_free, (DBusFreeFunction) rule_list_ptr_free);
1289 if (p->rules_by_iface == NULL)
1296 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1298 RulePool *p = matchmaker->rules_by_type + i;
1300 if (p->rules_by_iface == NULL)
1303 _dbus_hash_table_unref (p->rules_by_iface);
1305 dbus_free (matchmaker);
1311 bus_matchmaker_get_rules (BusMatchmaker *matchmaker,
1313 const char *interface,
1318 _dbus_assert (message_type >= 0);
1319 _dbus_assert (message_type < DBUS_NUM_MESSAGE_TYPES);
1321 _dbus_verbose ("Looking up rules for message_type %d, interface %s\n",
1323 interface != NULL ? interface : "<null>");
1325 p = matchmaker->rules_by_type + message_type;
1327 if (interface == NULL)
1329 return &p->rules_without_iface;
1335 list = _dbus_hash_table_lookup_string (p->rules_by_iface, interface);
1337 if (list == NULL && create)
1339 char *dupped_interface;
1341 list = dbus_new0 (DBusList *, 1);
1345 dupped_interface = _dbus_strdup (interface);
1346 if (dupped_interface == NULL)
1352 _dbus_verbose ("Adding list for type %d, iface %s\n", message_type,
1355 if (!_dbus_hash_table_insert_string (p->rules_by_iface,
1356 dupped_interface, list))
1359 dbus_free (dupped_interface);
1369 bus_matchmaker_gc_rules (BusMatchmaker *matchmaker,
1371 const char *interface,
1376 if (interface == NULL)
1382 _dbus_verbose ("GCing HT entry for message_type %u, interface %s\n",
1383 message_type, interface);
1385 p = matchmaker->rules_by_type + message_type;
1387 _dbus_assert (_dbus_hash_table_lookup_string (p->rules_by_iface, interface)
1390 _dbus_hash_table_remove_string (p->rules_by_iface, interface);
1394 bus_matchmaker_ref (BusMatchmaker *matchmaker)
1396 _dbus_assert (matchmaker->refcount > 0);
1398 matchmaker->refcount += 1;
1404 bus_matchmaker_unref (BusMatchmaker *matchmaker)
1406 _dbus_assert (matchmaker->refcount > 0);
1408 matchmaker->refcount -= 1;
1409 if (matchmaker->refcount == 0)
1413 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1415 RulePool *p = matchmaker->rules_by_type + i;
1417 _dbus_hash_table_unref (p->rules_by_iface);
1418 rule_list_free (&p->rules_without_iface);
1421 dbus_free (matchmaker);
1425 /* The rule can't be modified after it's added. */
1427 bus_matchmaker_add_rule (BusMatchmaker *matchmaker,
1432 _dbus_assert (bus_connection_is_active (rule->matches_go_to));
1434 _dbus_verbose ("Adding rule with message_type %d, interface %s\n",
1436 rule->interface != NULL ? rule->interface : "<null>");
1438 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1439 rule->interface, TRUE);
1444 if (!_dbus_list_append (rules, rule))
1447 if (!bus_connection_add_match_rule (rule->matches_go_to, rule))
1449 _dbus_list_remove_last (rules, rule);
1450 bus_matchmaker_gc_rules (matchmaker, rule->message_type,
1451 rule->interface, rules);
1455 bus_match_rule_ref (rule);
1457 #ifdef DBUS_ENABLE_VERBOSE_MODE
1459 char *s = match_rule_to_string (rule);
1461 _dbus_verbose ("Added match rule %s to connection %p\n",
1462 s ? s : "nomem", rule->matches_go_to);
1471 match_rule_equal (BusMatchRule *a,
1474 if (a->flags != b->flags)
1477 if (a->matches_go_to != b->matches_go_to)
1480 if ((a->flags & BUS_MATCH_MESSAGE_TYPE) &&
1481 a->message_type != b->message_type)
1484 if ((a->flags & BUS_MATCH_MEMBER) &&
1485 strcmp (a->member, b->member) != 0)
1488 if ((a->flags & BUS_MATCH_PATH) &&
1489 strcmp (a->path, b->path) != 0)
1492 if ((a->flags & BUS_MATCH_INTERFACE) &&
1493 strcmp (a->interface, b->interface) != 0)
1496 if ((a->flags & BUS_MATCH_SENDER) &&
1497 strcmp (a->sender, b->sender) != 0)
1500 if ((a->flags & BUS_MATCH_DESTINATION) &&
1501 strcmp (a->destination, b->destination) != 0)
1504 /* we already compared the value of flags, and
1505 * BUS_MATCH_CLIENT_IS_EAVESDROPPING does not have another struct member */
1507 if (a->flags & BUS_MATCH_ARGS)
1511 if (a->args_len != b->args_len)
1515 while (i < a->args_len)
1519 if ((a->args[i] != NULL) != (b->args[i] != NULL))
1522 if (a->arg_lens[i] != b->arg_lens[i])
1525 length = a->arg_lens[i] & ~BUS_MATCH_ARG_FLAGS;
1527 if (a->args[i] != NULL)
1529 _dbus_assert (b->args[i] != NULL);
1530 if (memcmp (a->args[i], b->args[i], length) != 0)
1542 bus_matchmaker_remove_rule_link (DBusList **rules,
1545 BusMatchRule *rule = link->data;
1547 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1548 _dbus_list_remove_link (rules, link);
1550 #ifdef DBUS_ENABLE_VERBOSE_MODE
1552 char *s = match_rule_to_string (rule);
1554 _dbus_verbose ("Removed match rule %s for connection %p\n",
1555 s ? s : "nomem", rule->matches_go_to);
1560 bus_match_rule_unref (rule);
1564 bus_matchmaker_remove_rule (BusMatchmaker *matchmaker,
1569 _dbus_verbose ("Removing rule with message_type %d, interface %s\n",
1571 rule->interface != NULL ? rule->interface : "<null>");
1573 bus_connection_remove_match_rule (rule->matches_go_to, rule);
1575 rules = bus_matchmaker_get_rules (matchmaker, rule->message_type,
1576 rule->interface, FALSE);
1578 /* We should only be asked to remove a rule by identity right after it was
1579 * added, so there should be a list for it.
1581 _dbus_assert (rules != NULL);
1583 _dbus_list_remove (rules, rule);
1584 bus_matchmaker_gc_rules (matchmaker, rule->message_type, rule->interface,
1587 #ifdef DBUS_ENABLE_VERBOSE_MODE
1589 char *s = match_rule_to_string (rule);
1591 _dbus_verbose ("Removed match rule %s for connection %p\n",
1592 s ? s : "nomem", rule->matches_go_to);
1597 bus_match_rule_unref (rule);
1600 /* Remove a single rule which is equal to the given rule by value */
1602 bus_matchmaker_remove_rule_by_value (BusMatchmaker *matchmaker,
1603 BusMatchRule *value,
1607 DBusList *link = NULL;
1609 _dbus_verbose ("Removing rule by value with message_type %d, interface %s\n",
1610 value->message_type,
1611 value->interface != NULL ? value->interface : "<null>");
1613 rules = bus_matchmaker_get_rules (matchmaker, value->message_type,
1614 value->interface, FALSE);
1618 /* we traverse backward because bus_connection_remove_match_rule()
1619 * removes the most-recently-added rule
1621 link = _dbus_list_get_last_link (rules);
1622 while (link != NULL)
1628 prev = _dbus_list_get_prev_link (rules, link);
1630 if (match_rule_equal (rule, value))
1632 bus_matchmaker_remove_rule_link (rules, link);
1642 dbus_set_error (error, DBUS_ERROR_MATCH_RULE_NOT_FOUND,
1643 "The given match rule wasn't found and can't be removed");
1647 bus_matchmaker_gc_rules (matchmaker, value->message_type, value->interface,
1654 rule_list_remove_by_connection (DBusList **rules,
1655 DBusConnection *connection)
1659 link = _dbus_list_get_first_link (rules);
1660 while (link != NULL)
1666 next = _dbus_list_get_next_link (rules, link);
1668 if (rule->matches_go_to == connection)
1670 bus_matchmaker_remove_rule_link (rules, link);
1672 else if (((rule->flags & BUS_MATCH_SENDER) && *rule->sender == ':') ||
1673 ((rule->flags & BUS_MATCH_DESTINATION) && *rule->destination == ':'))
1675 /* The rule matches to/from a base service, see if it's the
1676 * one being disconnected, since we know this service name
1677 * will never be recycled.
1681 name = bus_connection_get_name (connection);
1682 _dbus_assert (name != NULL); /* because we're an active connection */
1684 if (((rule->flags & BUS_MATCH_SENDER) &&
1685 strcmp (rule->sender, name) == 0) ||
1686 ((rule->flags & BUS_MATCH_DESTINATION) &&
1687 strcmp (rule->destination, name) == 0))
1689 bus_matchmaker_remove_rule_link (rules, link);
1698 bus_matchmaker_disconnected (BusMatchmaker *matchmaker,
1699 DBusConnection *connection)
1705 * This scans all match rules on the bus. We could avoid that
1706 * for the rules belonging to the connection, since we keep
1707 * a list of those; but for the rules that just refer to
1708 * the connection we'd need to do something more elaborate.
1711 _dbus_assert (bus_connection_is_active (connection));
1713 _dbus_verbose ("Removing all rules for connection %p\n", connection);
1715 for (i = DBUS_MESSAGE_TYPE_INVALID; i < DBUS_NUM_MESSAGE_TYPES; i++)
1717 RulePool *p = matchmaker->rules_by_type + i;
1720 rule_list_remove_by_connection (&p->rules_without_iface, connection);
1722 _dbus_hash_iter_init (p->rules_by_iface, &iter);
1723 while (_dbus_hash_iter_next (&iter))
1725 DBusList **items = _dbus_hash_iter_get_value (&iter);
1727 rule_list_remove_by_connection (items, connection);
1730 _dbus_hash_iter_remove_entry (&iter);
1736 connection_is_primary_owner (DBusConnection *connection,
1737 const char *service_name)
1739 BusService *service;
1741 BusRegistry *registry;
1743 _dbus_assert (connection != NULL);
1745 registry = bus_connection_get_registry (connection);
1747 _dbus_string_init_const (&str, service_name);
1748 service = bus_registry_lookup (registry, &str);
1750 if (service == NULL)
1751 return FALSE; /* Service doesn't exist so connection can't own it. */
1753 return bus_service_get_primary_owners_connection (service) == connection;
1757 str_has_prefix (const char *str, const char *prefix)
1760 prefix_len = strlen (prefix);
1761 if (strncmp (str, prefix, prefix_len) == 0)
1768 match_rule_matches (BusMatchRule *rule,
1769 DBusConnection *sender,
1770 DBusConnection *addressed_recipient,
1771 DBusMessage *message,
1772 BusMatchFlags already_matched)
1774 dbus_bool_t wants_to_eavesdrop = FALSE;
1777 /* All features of the match rule are AND'd together,
1778 * so FALSE if any of them don't match.
1781 /* sender/addressed_recipient of #NULL may mean bus driver,
1782 * or for addressed_recipient may mean a message with no
1783 * specific recipient (i.e. a signal)
1786 /* Don't bother re-matching features we've already checked implicitly. */
1787 flags = rule->flags & (~already_matched);
1789 if (flags & BUS_MATCH_CLIENT_IS_EAVESDROPPING)
1790 wants_to_eavesdrop = TRUE;
1792 if (flags & BUS_MATCH_MESSAGE_TYPE)
1794 _dbus_assert (rule->message_type != DBUS_MESSAGE_TYPE_INVALID);
1796 if (rule->message_type != dbus_message_get_type (message))
1800 if (flags & BUS_MATCH_INTERFACE)
1804 _dbus_assert (rule->interface != NULL);
1806 iface = dbus_message_get_interface (message);
1810 if (strcmp (iface, rule->interface) != 0)
1814 if (flags & BUS_MATCH_MEMBER)
1818 _dbus_assert (rule->member != NULL);
1820 member = dbus_message_get_member (message);
1824 if (strcmp (member, rule->member) != 0)
1828 if (flags & BUS_MATCH_SENDER)
1830 _dbus_assert (rule->sender != NULL);
1834 if (strcmp (rule->sender,
1835 DBUS_SERVICE_DBUS) != 0)
1840 if (!connection_is_primary_owner (sender, rule->sender))
1845 /* Note: this part is relevant for eavesdropper rules:
1847 * 1) rule has a destination to be matched
1848 * (flag BUS_MATCH_DESTINATION present). Rule will match if:
1849 * - rule->destination matches the addressed_recipient
1851 * - wants_to_eavesdrop=TRUE
1853 * Note: (the case in which addressed_recipient is the actual rule owner
1854 * is handled elsewere in dispatch.c:bus_dispatch_matches().
1856 * 2) rule has no destination. Rule will match if:
1857 * - message has no specified destination (ie broadcasts)
1858 * (Note: this will rule out unicast method calls and unicast signals,
1859 * fixing FDO#269748)
1861 * - wants_to_eavesdrop=TRUE (destination-catch-all situation)
1863 if (flags & BUS_MATCH_DESTINATION)
1865 const char *destination;
1867 _dbus_assert (rule->destination != NULL);
1869 destination = dbus_message_get_destination (message);
1870 if (destination == NULL)
1871 /* broadcast, but this rule specified a destination: no match */
1874 /* rule owner does not intend to eavesdrop: we'll deliver only msgs
1875 * directed to it, NOT MATCHING */
1876 if (!wants_to_eavesdrop)
1879 if (addressed_recipient == NULL)
1881 if (strcmp (rule->destination,
1882 DBUS_SERVICE_DBUS) != 0)
1887 if (!connection_is_primary_owner (addressed_recipient, rule->destination))
1890 } else { /* no destination in rule */
1891 dbus_bool_t msg_is_broadcast;
1893 _dbus_assert (rule->destination == NULL);
1895 msg_is_broadcast = (dbus_message_get_destination (message) == NULL);
1897 if (!wants_to_eavesdrop && !msg_is_broadcast)
1900 /* if we are here rule owner intends to eavesdrop
1902 * message is being broadcasted */
1905 if (flags & BUS_MATCH_PATH)
1909 _dbus_assert (rule->path != NULL);
1911 path = dbus_message_get_path (message);
1915 if (strcmp (path, rule->path) != 0)
1919 if (flags & BUS_MATCH_PATH_NAMESPACE)
1924 _dbus_assert (rule->path != NULL);
1926 path = dbus_message_get_path (message);
1930 if (!str_has_prefix (path, rule->path))
1933 len = strlen (rule->path);
1935 /* Check that the actual argument is within the expected
1936 * namespace, rather than just starting with that string,
1937 * by checking that the matched prefix is followed by a '/'
1938 * or the end of the path.
1940 * Special case: the only valid path of length 1, "/",
1941 * matches everything.
1943 if (len > 1 && path[len] != '\0' && path[len] != '/')
1947 if (flags & BUS_MATCH_ARGS)
1950 DBusMessageIter iter;
1952 _dbus_assert (rule->args != NULL);
1954 dbus_message_iter_init (message, &iter);
1957 while (i < rule->args_len)
1960 const char *expected_arg;
1961 int expected_length;
1962 dbus_bool_t is_path, is_namespace;
1964 expected_arg = rule->args[i];
1965 expected_length = rule->arg_lens[i] & ~BUS_MATCH_ARG_FLAGS;
1966 is_path = (rule->arg_lens[i] & BUS_MATCH_ARG_IS_PATH) != 0;
1967 is_namespace = (rule->arg_lens[i] & BUS_MATCH_ARG_NAMESPACE) != 0;
1969 current_type = dbus_message_iter_get_arg_type (&iter);
1971 if (expected_arg != NULL)
1973 const char *actual_arg;
1976 if (current_type != DBUS_TYPE_STRING &&
1977 (!is_path || current_type != DBUS_TYPE_OBJECT_PATH))
1981 dbus_message_iter_get_basic (&iter, &actual_arg);
1982 _dbus_assert (actual_arg != NULL);
1984 actual_length = strlen (actual_arg);
1988 if (actual_length < expected_length &&
1989 actual_arg[actual_length - 1] != '/')
1992 if (expected_length < actual_length &&
1993 expected_arg[expected_length - 1] != '/')
1996 if (memcmp (actual_arg, expected_arg,
1997 MIN (actual_length, expected_length)) != 0)
2000 else if (is_namespace)
2002 if (expected_length > actual_length)
2005 /* If the actual argument doesn't start with the expected
2006 * namespace, then we don't match.
2008 if (memcmp (expected_arg, actual_arg, expected_length) != 0)
2011 if (expected_length < actual_length)
2013 /* Check that the actual argument is within the expected
2014 * namespace, rather than just starting with that string,
2015 * by checking that the matched prefix ends in a '.'.
2017 * This doesn't stop "foo.bar." matching "foo.bar..baz"
2018 * which is an invalid namespace, but at some point the
2019 * daemon can't cover up for broken services.
2021 if (actual_arg[expected_length] != '.')
2024 /* otherwise we had an exact match. */
2028 if (expected_length != actual_length ||
2029 memcmp (expected_arg, actual_arg, expected_length) != 0)
2035 if (current_type != DBUS_TYPE_INVALID)
2036 dbus_message_iter_next (&iter);
2046 get_recipients_from_list (DBusList **rules,
2047 DBusConnection *sender,
2048 DBusConnection *addressed_recipient,
2049 DBusMessage *message,
2050 DBusList **recipients_p)
2057 link = _dbus_list_get_first_link (rules);
2058 while (link != NULL)
2064 #ifdef DBUS_ENABLE_VERBOSE_MODE
2066 char *s = match_rule_to_string (rule);
2068 _dbus_verbose ("Checking whether message matches rule %s for connection %p\n",
2069 s ? s : "nomem", rule->matches_go_to);
2074 if (match_rule_matches (rule,
2075 sender, addressed_recipient, message,
2076 BUS_MATCH_MESSAGE_TYPE | BUS_MATCH_INTERFACE))
2078 _dbus_verbose ("Rule matched\n");
2080 /* Append to the list if we haven't already */
2081 if (bus_connection_mark_stamp (rule->matches_go_to))
2083 if (!_dbus_list_append (recipients_p, rule->matches_go_to))
2088 _dbus_verbose ("Connection already receiving this message, so not adding again\n");
2092 link = _dbus_list_get_next_link (rules, link);
2099 bus_matchmaker_get_recipients (BusMatchmaker *matchmaker,
2100 BusConnections *connections,
2101 DBusConnection *sender,
2102 DBusConnection *addressed_recipient,
2103 DBusMessage *message,
2104 DBusList **recipients_p)
2107 const char *interface;
2108 DBusList **neither, **just_type, **just_iface, **both;
2110 _dbus_assert (*recipients_p == NULL);
2112 /* This avoids sending same message to the same connection twice.
2113 * Purpose of the stamp instead of a bool is to avoid iterating over
2114 * all connections resetting the bool each time.
2116 bus_connections_increment_stamp (connections);
2118 /* addressed_recipient is already receiving the message, don't add to list.
2119 * NULL addressed_recipient means either bus driver, or this is a signal
2120 * and thus lacks a specific addressed_recipient.
2122 if (addressed_recipient != NULL)
2123 bus_connection_mark_stamp (addressed_recipient);
2125 type = dbus_message_get_type (message);
2126 interface = dbus_message_get_interface (message);
2128 neither = bus_matchmaker_get_rules (matchmaker, DBUS_MESSAGE_TYPE_INVALID,
2130 just_type = just_iface = both = NULL;
2132 if (interface != NULL)
2133 just_iface = bus_matchmaker_get_rules (matchmaker,
2134 DBUS_MESSAGE_TYPE_INVALID, interface, FALSE);
2136 if (type > DBUS_MESSAGE_TYPE_INVALID && type < DBUS_NUM_MESSAGE_TYPES)
2138 just_type = bus_matchmaker_get_rules (matchmaker, type, NULL, FALSE);
2140 if (interface != NULL)
2141 both = bus_matchmaker_get_rules (matchmaker, type, interface, FALSE);
2144 if (!(get_recipients_from_list (neither, sender, addressed_recipient,
2145 message, recipients_p) &&
2146 get_recipients_from_list (just_iface, sender, addressed_recipient,
2147 message, recipients_p) &&
2148 get_recipients_from_list (just_type, sender, addressed_recipient,
2149 message, recipients_p) &&
2150 get_recipients_from_list (both, sender, addressed_recipient,
2151 message, recipients_p)))
2153 _dbus_list_clear (recipients_p);
2160 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
2164 static BusMatchRule*
2165 check_parse (dbus_bool_t should_succeed,
2172 dbus_error_init (&error);
2174 _dbus_string_init_const (&str, text);
2176 rule = bus_match_rule_parse (NULL, &str, &error);
2177 if (dbus_error_has_name (&error, DBUS_ERROR_NO_MEMORY))
2179 dbus_error_free (&error);
2183 if (should_succeed && rule == NULL)
2185 _dbus_warn ("Failed to parse: %s: %s: \"%s\"\n",
2186 error.name, error.message,
2187 _dbus_string_get_const_data (&str));
2191 if (!should_succeed && rule != NULL)
2193 _dbus_warn ("Failed to fail to parse: \"%s\"\n",
2194 _dbus_string_get_const_data (&str));
2198 dbus_error_free (&error);
2204 assert_large_rule (BusMatchRule *rule)
2206 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
2207 _dbus_assert (rule->flags & BUS_MATCH_SENDER);
2208 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
2209 _dbus_assert (rule->flags & BUS_MATCH_MEMBER);
2210 _dbus_assert (rule->flags & BUS_MATCH_DESTINATION);
2211 _dbus_assert (rule->flags & BUS_MATCH_PATH);
2213 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
2214 _dbus_assert (rule->interface != NULL);
2215 _dbus_assert (rule->member != NULL);
2216 _dbus_assert (rule->sender != NULL);
2217 _dbus_assert (rule->destination != NULL);
2218 _dbus_assert (rule->path != NULL);
2220 _dbus_assert (strcmp (rule->interface, "org.freedesktop.DBusInterface") == 0);
2221 _dbus_assert (strcmp (rule->sender, "org.freedesktop.DBusSender") == 0);
2222 _dbus_assert (strcmp (rule->member, "Foo") == 0);
2223 _dbus_assert (strcmp (rule->path, "/bar/foo") == 0);
2224 _dbus_assert (strcmp (rule->destination, ":452345.34") == 0);
2228 test_parsing (void *data)
2232 rule = check_parse (TRUE, "type='signal',sender='org.freedesktop.DBusSender',interface='org.freedesktop.DBusInterface',member='Foo',path='/bar/foo',destination=':452345.34'");
2235 assert_large_rule (rule);
2236 bus_match_rule_unref (rule);
2239 /* With extra whitespace and useless quotes */
2240 rule = check_parse (TRUE, " type='signal', \tsender='org.freedes''ktop.DBusSender', interface='org.freedesktop.DBusInterface''''', \tmember='Foo',path='/bar/foo',destination=':452345.34'''''");
2243 assert_large_rule (rule);
2244 bus_match_rule_unref (rule);
2248 /* A simple signal connection */
2249 rule = check_parse (TRUE, "type='signal',path='/foo',interface='org.Bar'");
2252 _dbus_assert (rule->flags & BUS_MATCH_MESSAGE_TYPE);
2253 _dbus_assert (rule->flags & BUS_MATCH_INTERFACE);
2254 _dbus_assert (rule->flags & BUS_MATCH_PATH);
2256 _dbus_assert (rule->message_type == DBUS_MESSAGE_TYPE_SIGNAL);
2257 _dbus_assert (rule->interface != NULL);
2258 _dbus_assert (rule->path != NULL);
2260 _dbus_assert (strcmp (rule->interface, "org.Bar") == 0);
2261 _dbus_assert (strcmp (rule->path, "/foo") == 0);
2263 bus_match_rule_unref (rule);
2267 rule = check_parse (TRUE, "arg0='foo'");
2270 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2271 _dbus_assert (rule->args != NULL);
2272 _dbus_assert (rule->args_len == 1);
2273 _dbus_assert (rule->args[0] != NULL);
2274 _dbus_assert (rule->args[1] == NULL);
2275 _dbus_assert (strcmp (rule->args[0], "foo") == 0);
2277 bus_match_rule_unref (rule);
2280 rule = check_parse (TRUE, "arg1='foo'");
2283 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2284 _dbus_assert (rule->args != NULL);
2285 _dbus_assert (rule->args_len == 2);
2286 _dbus_assert (rule->args[0] == NULL);
2287 _dbus_assert (rule->args[1] != NULL);
2288 _dbus_assert (rule->args[2] == NULL);
2289 _dbus_assert (strcmp (rule->args[1], "foo") == 0);
2291 bus_match_rule_unref (rule);
2294 rule = check_parse (TRUE, "arg2='foo'");
2297 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2298 _dbus_assert (rule->args != NULL);
2299 _dbus_assert (rule->args_len == 3);
2300 _dbus_assert (rule->args[0] == NULL);
2301 _dbus_assert (rule->args[1] == NULL);
2302 _dbus_assert (rule->args[2] != NULL);
2303 _dbus_assert (rule->args[3] == NULL);
2304 _dbus_assert (strcmp (rule->args[2], "foo") == 0);
2306 bus_match_rule_unref (rule);
2309 rule = check_parse (TRUE, "arg40='foo'");
2312 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2313 _dbus_assert (rule->args != NULL);
2314 _dbus_assert (rule->args_len == 41);
2315 _dbus_assert (rule->args[0] == NULL);
2316 _dbus_assert (rule->args[1] == NULL);
2317 _dbus_assert (rule->args[40] != NULL);
2318 _dbus_assert (rule->args[41] == NULL);
2319 _dbus_assert (strcmp (rule->args[40], "foo") == 0);
2321 bus_match_rule_unref (rule);
2324 rule = check_parse (TRUE, "arg63='foo'");
2327 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2328 _dbus_assert (rule->args != NULL);
2329 _dbus_assert (rule->args_len == 64);
2330 _dbus_assert (rule->args[0] == NULL);
2331 _dbus_assert (rule->args[1] == NULL);
2332 _dbus_assert (rule->args[63] != NULL);
2333 _dbus_assert (rule->args[64] == NULL);
2334 _dbus_assert (strcmp (rule->args[63], "foo") == 0);
2336 bus_match_rule_unref (rule);
2339 rule = check_parse (TRUE, "arg7path='/foo'");
2342 _dbus_assert (rule->flags = BUS_MATCH_ARGS);
2343 _dbus_assert (rule->args != NULL);
2344 _dbus_assert (rule->args_len == 8);
2345 _dbus_assert (rule->args[7] != NULL);
2346 _dbus_assert (rule->args[8] == NULL);
2347 _dbus_assert (strcmp (rule->args[7], "/foo") == 0);
2348 _dbus_assert ((rule->arg_lens[7] & BUS_MATCH_ARG_IS_PATH)
2349 == BUS_MATCH_ARG_IS_PATH);
2351 bus_match_rule_unref (rule);
2354 /* Arg 0 namespace matches */
2355 rule = check_parse (TRUE, "arg0namespace='foo'");
2358 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2359 _dbus_assert (rule->args != NULL);
2360 _dbus_assert (rule->args_len == 1);
2361 _dbus_assert (strcmp (rule->args[0], "foo") == 0);
2362 _dbus_assert ((rule->arg_lens[0] & BUS_MATCH_ARG_NAMESPACE)
2363 == BUS_MATCH_ARG_NAMESPACE);
2365 bus_match_rule_unref (rule);
2368 rule = check_parse (TRUE, "arg0namespace='foo.bar'");
2371 _dbus_assert (rule->flags == BUS_MATCH_ARGS);
2372 _dbus_assert (rule->args != NULL);
2373 _dbus_assert (rule->args_len == 1);
2374 _dbus_assert (strcmp (rule->args[0], "foo.bar") == 0);
2375 _dbus_assert ((rule->arg_lens[0] & BUS_MATCH_ARG_NAMESPACE)
2376 == BUS_MATCH_ARG_NAMESPACE);
2378 bus_match_rule_unref (rule);
2381 /* Only arg0namespace is supported. */
2382 rule = check_parse (FALSE, "arg1namespace='foo'");
2383 _dbus_assert (rule == NULL);
2385 /* An empty string isn't a valid namespace prefix (you should just not
2386 * specify this key at all).
2388 rule = check_parse (FALSE, "arg0namespace=''");
2389 _dbus_assert (rule == NULL);
2391 /* Trailing periods aren't allowed (earlier versions of the arg0namespace
2392 * spec allowed a single trailing period, which altered the semantics) */
2393 rule = check_parse (FALSE, "arg0namespace='foo.'");
2394 _dbus_assert (rule == NULL);
2396 rule = check_parse (FALSE, "arg0namespace='foo.bar.'");
2397 _dbus_assert (rule == NULL);
2399 rule = check_parse (FALSE, "arg0namespace='foo..'");
2400 _dbus_assert (rule == NULL);
2402 rule = check_parse (FALSE, "arg0namespace='foo.bar..'");
2403 _dbus_assert (rule == NULL);
2405 /* Too-large argN */
2406 rule = check_parse (FALSE, "arg300='foo'");
2407 _dbus_assert (rule == NULL);
2408 rule = check_parse (FALSE, "arg64='foo'");
2409 _dbus_assert (rule == NULL);
2412 rule = check_parse (FALSE, "arg='foo'");
2413 _dbus_assert (rule == NULL);
2414 rule = check_parse (FALSE, "argv='foo'");
2415 _dbus_assert (rule == NULL);
2416 rule = check_parse (FALSE, "arg3junk='foo'");
2417 _dbus_assert (rule == NULL);
2418 rule = check_parse (FALSE, "argument='foo'");
2419 _dbus_assert (rule == NULL);
2421 /* Reject duplicates */
2422 rule = check_parse (FALSE, "type='signal',type='method_call'");
2423 _dbus_assert (rule == NULL);
2425 rule = check_parse (TRUE, "path_namespace='/foo/bar'");
2428 _dbus_assert (rule->flags == BUS_MATCH_PATH_NAMESPACE);
2429 _dbus_assert (rule->path != NULL);
2430 _dbus_assert (strcmp (rule->path, "/foo/bar") == 0);
2432 bus_match_rule_unref (rule);
2435 /* Almost a duplicate */
2436 rule = check_parse (FALSE, "path='/foo',path_namespace='/foo'");
2437 _dbus_assert (rule == NULL);
2439 /* Trailing / was supported in the initial proposal, but now isn't */
2440 rule = check_parse (FALSE, "path_namespace='/foo/'");
2441 _dbus_assert (rule == NULL);
2443 /* Duplicates with the argN code */
2444 rule = check_parse (FALSE, "arg0='foo',arg0='bar'");
2445 _dbus_assert (rule == NULL);
2446 rule = check_parse (FALSE, "arg3='foo',arg3='bar'");
2447 _dbus_assert (rule == NULL);
2448 rule = check_parse (FALSE, "arg30='foo',arg30='bar'");
2449 _dbus_assert (rule == NULL);
2451 /* Reject broken keys */
2452 rule = check_parse (FALSE, "blah='signal'");
2453 _dbus_assert (rule == NULL);
2455 /* Reject broken values */
2456 rule = check_parse (FALSE, "type='chouin'");
2457 _dbus_assert (rule == NULL);
2458 rule = check_parse (FALSE, "interface='abc@def++'");
2459 _dbus_assert (rule == NULL);
2460 rule = check_parse (FALSE, "service='youpi'");
2461 _dbus_assert (rule == NULL);
2463 /* Allow empty rule */
2464 rule = check_parse (TRUE, "");
2467 _dbus_assert (rule->flags == 0);
2469 bus_match_rule_unref (rule);
2472 /* All-whitespace rule is the same as empty */
2473 rule = check_parse (TRUE, " \t");
2476 _dbus_assert (rule->flags == 0);
2478 bus_match_rule_unref (rule);
2481 /* But with non-whitespace chars and no =value, it's not OK */
2482 rule = check_parse (FALSE, "type");
2483 _dbus_assert (rule == NULL);
2491 } equality_tests[] = {
2492 { "type='signal'", "type='signal'" },
2493 { "type='signal',interface='foo.bar'", "interface='foo.bar',type='signal'" },
2494 { "type='signal',member='bar'", "member='bar',type='signal'" },
2495 { "type='method_call',sender=':1.0'", "sender=':1.0',type='method_call'" },
2496 { "type='method_call',destination=':1.0'", "destination=':1.0',type='method_call'" },
2497 { "type='method_call',path='/foo/bar'", "path='/foo/bar',type='method_call'" },
2498 { "type='method_call',arg0='blah'", "arg0='blah',type='method_call'" },
2499 { "type='method_call',arg0='boo'", "arg0='boo',type='method_call'" },
2500 { "type='method_call',arg0='blah',arg1='baz'", "arg0='blah',arg1='baz',type='method_call'" },
2501 { "type='method_call',arg3='foosh'", "arg3='foosh',type='method_call'" },
2502 { "arg3='fool'", "arg3='fool'" },
2503 { "arg0namespace='fool'", "arg0namespace='fool'" },
2504 { "member='food'", "member='food'" },
2505 { "member=escape", "member='escape'" },
2506 { "member=icecream", "member=ice'cream'" },
2507 { "arg0='comma,type=comma',type=signal", "type=signal,arg0='comma,type=comma'" },
2508 { "arg0=escap\\e", "arg0='escap\\e'" },
2509 { "arg0=Time: 8 o\\'clock", "arg0='Time: 8 o'\\''clock'" },
2513 test_equality (void)
2518 while (i < _DBUS_N_ELEMENTS (equality_tests))
2520 BusMatchRule *first;
2521 BusMatchRule *second;
2522 char *first_str, *second_str;
2523 BusMatchRule *first_reparsed, *second_reparsed;
2526 first = check_parse (TRUE, equality_tests[i].first);
2527 _dbus_assert (first != NULL);
2528 second = check_parse (TRUE, equality_tests[i].second);
2529 _dbus_assert (second != NULL);
2531 if (!match_rule_equal (first, second))
2533 _dbus_warn ("rule %s and %s should have been equal\n",
2534 equality_tests[i].first,
2535 equality_tests[i].second);
2539 /* Check match_rule_to_string */
2540 first_str = match_rule_to_string (first);
2541 _dbus_assert (first_str != NULL);
2542 second_str = match_rule_to_string (second);
2543 _dbus_assert (second_str != NULL);
2544 _dbus_assert (strcmp (first_str, second_str) == 0);
2545 first_reparsed = check_parse (TRUE, first_str);
2546 second_reparsed = check_parse (TRUE, second_str);
2547 _dbus_assert (match_rule_equal (first, first_reparsed));
2548 _dbus_assert (match_rule_equal (second, second_reparsed));
2549 bus_match_rule_unref (first_reparsed);
2550 bus_match_rule_unref (second_reparsed);
2551 dbus_free (first_str);
2552 dbus_free (second_str);
2554 bus_match_rule_unref (second);
2556 /* Check that the rule is not equal to any of the
2557 * others besides its pair match
2560 while (j < _DBUS_N_ELEMENTS (equality_tests))
2564 second = check_parse (TRUE, equality_tests[j].second);
2566 if (match_rule_equal (first, second))
2568 _dbus_warn ("rule %s and %s should not have been equal\n",
2569 equality_tests[i].first,
2570 equality_tests[j].second);
2574 bus_match_rule_unref (second);
2580 bus_match_rule_unref (first);
2587 should_match_message_1[] = {
2589 "member='Frobated'",
2591 "type='signal',member='Frobated'",
2592 "type='signal',member='Frobated',arg0='foobar'",
2593 "member='Frobated',arg0='foobar'",
2594 "type='signal',arg0='foobar'",
2595 /* The definition of argXpath matches says: "As with normal argument matches,
2596 * if the argument is exactly equal to the string given in the match rule
2597 * then the rule is satisfied." So this should match (even though the
2598 * argument is not a valid path)!
2600 "arg0path='foobar'",
2601 "arg0namespace='foobar'",
2606 should_not_match_message_1[] = {
2607 "type='method_call'",
2609 "type='method_return'",
2610 "type='signal',member='Oopsed'",
2617 "arg0='foobar',arg1='abcdef'",
2618 "arg0='foobar',arg1='abcdef',arg2='abcdefghi',arg3='abcdefghi',arg4='abcdefghi'",
2619 "arg0='foobar',arg1='abcdef',arg4='abcdefghi',arg3='abcdefghi',arg2='abcdefghi'",
2621 "arg0path='foobar/'",
2623 "arg0namespace='foo'",
2624 "arg0namespace='foo',arg1='abcdef'",
2625 "arg0namespace='moo'",
2629 #define EXAMPLE_NAME "com.example.backend.foo"
2632 should_match_message_2[] = {
2633 /* EXAMPLE_NAME is in all of these namespaces */
2634 "arg0namespace='com.example.backend'",
2635 "arg0namespace='com.example'",
2636 "arg0namespace='com'",
2638 /* If the client specifies the name exactly, with no trailing period, then
2641 "arg0namespace='com.example.backend.foo'",
2647 should_not_match_message_2[] = {
2648 /* These are not even prefixes */
2649 "arg0namespace='com.example.backend.foo.bar'",
2650 "arg0namespace='com.example.backend.foobar'",
2652 /* These are prefixes, but they're not parent namespaces. */
2653 "arg0namespace='com.example.backend.fo'",
2654 "arg0namespace='com.example.backen'",
2655 "arg0namespace='com.exampl'",
2656 "arg0namespace='co'",
2662 check_matches (dbus_bool_t expected_to_match,
2664 DBusMessage *message,
2665 const char *rule_text)
2668 dbus_bool_t matched;
2670 rule = check_parse (TRUE, rule_text);
2671 _dbus_assert (rule != NULL);
2673 /* We can't test sender/destination rules since we pass NULL here */
2674 matched = match_rule_matches (rule, NULL, NULL, message, 0);
2676 if (matched != expected_to_match)
2678 _dbus_warn ("Expected rule %s to %s message %d, failed\n",
2679 rule_text, expected_to_match ?
2680 "match" : "not match", number);
2684 bus_match_rule_unref (rule);
2688 check_matching (DBusMessage *message,
2690 const char **should_match,
2691 const char **should_not_match)
2696 while (should_match[i] != NULL)
2698 check_matches (TRUE, number, message, should_match[i]);
2703 while (should_not_match[i] != NULL)
2705 check_matches (FALSE, number, message, should_not_match[i]);
2711 test_matching (void)
2713 DBusMessage *message1, *message2;
2714 const char *v_STRING;
2715 dbus_int32_t v_INT32;
2717 message1 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2718 _dbus_assert (message1 != NULL);
2719 if (!dbus_message_set_member (message1, "Frobated"))
2720 _dbus_assert_not_reached ("oom");
2722 v_STRING = "foobar";
2724 if (!dbus_message_append_args (message1,
2725 DBUS_TYPE_STRING, &v_STRING,
2726 DBUS_TYPE_INT32, &v_INT32,
2728 _dbus_assert_not_reached ("oom");
2730 check_matching (message1, 1,
2731 should_match_message_1,
2732 should_not_match_message_1);
2734 dbus_message_unref (message1);
2736 message2 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2737 _dbus_assert (message2 != NULL);
2738 if (!dbus_message_set_member (message2, "NameOwnerChanged"))
2739 _dbus_assert_not_reached ("oom");
2741 /* Obviously this isn't really a NameOwnerChanged signal. */
2742 v_STRING = EXAMPLE_NAME;
2743 if (!dbus_message_append_args (message2,
2744 DBUS_TYPE_STRING, &v_STRING,
2746 _dbus_assert_not_reached ("oom");
2748 check_matching (message2, 2,
2749 should_match_message_2,
2750 should_not_match_message_2);
2752 dbus_message_unref (message2);
2755 #define PATH_MATCH_RULE "arg0path='/aa/bb/'"
2757 /* This is a list of paths that should be matched by PATH_MATCH_RULE, taken
2758 * from the specification. Notice that not all of them are actually legal D-Bus
2761 * The author of this test takes no responsibility for the semantics of
2762 * this match rule key.
2764 static const char *paths_that_should_be_matched[] = {
2768 #define FIRST_VALID_PATH_WHICH_SHOULD_MATCH 3
2774 /* These paths should not be matched by PATH_MATCH_RULE. */
2775 static const char *paths_that_should_not_be_matched[] = {
2784 test_path_match (int type,
2786 const char *rule_text,
2788 dbus_bool_t should_match)
2790 DBusMessage *message = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2791 dbus_bool_t matched;
2793 _dbus_assert (message != NULL);
2794 if (!dbus_message_set_member (message, "Foo"))
2795 _dbus_assert_not_reached ("oom");
2797 if (!dbus_message_append_args (message,
2800 _dbus_assert_not_reached ("oom");
2802 matched = match_rule_matches (rule, NULL, NULL, message, 0);
2804 if (matched != should_match)
2806 _dbus_warn ("Expected rule %s to %s message "
2807 "with first arg %s of type '%c', failed\n",
2809 should_match ? "match" : "not match",
2815 dbus_message_unref (message);
2819 test_path_matching (void)
2824 rule = check_parse (TRUE, PATH_MATCH_RULE);
2825 _dbus_assert (rule != NULL);
2827 for (s = paths_that_should_be_matched; *s != NULL; s++)
2828 test_path_match (DBUS_TYPE_STRING, *s, PATH_MATCH_RULE, rule, TRUE);
2830 for (s = paths_that_should_be_matched + FIRST_VALID_PATH_WHICH_SHOULD_MATCH;
2832 test_path_match (DBUS_TYPE_OBJECT_PATH, *s, PATH_MATCH_RULE, rule, TRUE);
2834 for (s = paths_that_should_not_be_matched; *s != NULL; s++)
2836 test_path_match (DBUS_TYPE_STRING, *s, PATH_MATCH_RULE, rule, FALSE);
2837 test_path_match (DBUS_TYPE_OBJECT_PATH, *s, PATH_MATCH_RULE, rule, FALSE);
2840 bus_match_rule_unref (rule);
2844 path_namespace_should_match_message_1[] = {
2845 "type='signal',path_namespace='/'",
2846 "type='signal',path_namespace='/foo'",
2847 "type='signal',path_namespace='/foo/TheObjectManager'",
2852 path_namespace_should_not_match_message_1[] = {
2853 "type='signal',path_namespace='/bar'",
2854 "type='signal',path_namespace='/bar/TheObjectManager'",
2859 path_namespace_should_match_message_2[] = {
2860 "type='signal',path_namespace='/'",
2861 "type='signal',path_namespace='/foo/TheObjectManager'",
2866 path_namespace_should_not_match_message_2[] = {
2871 path_namespace_should_match_message_3[] = {
2872 "type='signal',path_namespace='/'",
2877 path_namespace_should_not_match_message_3[] = {
2878 "type='signal',path_namespace='/foo/TheObjectManager'",
2883 path_namespace_should_match_message_4[] = {
2884 "type='signal',path_namespace='/'",
2889 path_namespace_should_not_match_message_4[] = {
2890 "type='signal',path_namespace='/foo/TheObjectManager'",
2895 test_matching_path_namespace (void)
2897 DBusMessage *message1;
2898 DBusMessage *message2;
2899 DBusMessage *message3;
2900 DBusMessage *message4;
2902 message1 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2903 _dbus_assert (message1 != NULL);
2904 if (!dbus_message_set_path (message1, "/foo/TheObjectManager"))
2905 _dbus_assert_not_reached ("oom");
2907 message2 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2908 _dbus_assert (message2 != NULL);
2909 if (!dbus_message_set_path (message2, "/foo/TheObjectManager/child_object"))
2910 _dbus_assert_not_reached ("oom");
2912 message3 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2913 _dbus_assert (message3 != NULL);
2914 if (!dbus_message_set_path (message3, "/foo/TheObjectManagerOther"))
2915 _dbus_assert_not_reached ("oom");
2917 message4 = dbus_message_new (DBUS_MESSAGE_TYPE_SIGNAL);
2918 _dbus_assert (message4 != NULL);
2919 if (!dbus_message_set_path (message4, "/"))
2920 _dbus_assert_not_reached ("oom");
2922 check_matching (message1, 1,
2923 path_namespace_should_match_message_1,
2924 path_namespace_should_not_match_message_1);
2925 check_matching (message2, 2,
2926 path_namespace_should_match_message_2,
2927 path_namespace_should_not_match_message_2);
2928 check_matching (message3, 3,
2929 path_namespace_should_match_message_3,
2930 path_namespace_should_not_match_message_3);
2931 check_matching (message4, 4,
2932 path_namespace_should_match_message_4,
2933 path_namespace_should_not_match_message_4);
2935 dbus_message_unref (message4);
2936 dbus_message_unref (message3);
2937 dbus_message_unref (message2);
2938 dbus_message_unref (message1);
2942 bus_signals_test (const DBusString *test_data_dir)
2944 BusMatchmaker *matchmaker;
2946 matchmaker = bus_matchmaker_new ();
2947 bus_matchmaker_ref (matchmaker);
2948 bus_matchmaker_unref (matchmaker);
2949 bus_matchmaker_unref (matchmaker);
2951 if (!_dbus_test_oom_handling ("parsing match rules", test_parsing, NULL))
2952 _dbus_assert_not_reached ("Parsing match rules test failed");
2956 test_path_matching ();
2957 test_matching_path_namespace ();
2962 #endif /* DBUS_ENABLE_EMBEDDED_TESTS */