1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 #include "alloc-util.h"
4 #include "bus-internal.h"
5 #include "bus-message.h"
7 #include "string-util.h"
9 bool object_path_is_valid(const char *p
) {
22 for (slash
= true, q
= p
+1; *q
; q
++)
32 (*q
>= 'a' && *q
<= 'z') ||
33 (*q
>= 'A' && *q
<= 'Z') ||
34 (*q
>= '0' && *q
<= '9') ||
46 return (q
- p
) <= BUS_PATH_SIZE_MAX
;
49 char* object_path_startswith(const char *a
, const char *b
) {
52 if (!object_path_is_valid(a
) ||
53 !object_path_is_valid(b
))
72 bool interface_name_is_valid(const char *p
) {
74 bool dot
, found_dot
= false;
79 for (dot
= true, q
= p
; *q
; q
++)
84 found_dot
= dot
= true;
89 (*q
>= 'a' && *q
<= 'z') ||
90 (*q
>= 'A' && *q
<= 'Z') ||
91 (!dot
&& *q
>= '0' && *q
<= '9') ||
112 bool service_name_is_valid(const char *p
) {
114 bool dot
, found_dot
= false, unique
;
119 unique
= p
[0] == ':';
121 for (dot
= true, q
= unique
? p
+1 : p
; *q
; q
++)
126 found_dot
= dot
= true;
131 (*q
>= 'a' && *q
<= 'z') ||
132 (*q
>= 'A' && *q
<= 'Z') ||
133 ((!dot
|| unique
) && *q
>= '0' && *q
<= '9') ||
134 IN_SET(*q
, '_', '-');
154 bool member_name_is_valid(const char *p
) {
160 for (q
= p
; *q
; q
++) {
164 (*q
>= 'a' && *q
<= 'z') ||
165 (*q
>= 'A' && *q
<= 'Z') ||
166 (*q
>= '0' && *q
<= '9') ||
180 * Complex pattern match
181 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
182 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
183 * spearator. This function returns true if:
184 * - both strings are equal
185 * - either is a prefix of the other and ends with @c
186 * The second rule makes sure that either string needs to be fully included in
187 * the other, and the string which is considered the prefix needs to end with a
190 static bool complex_pattern_check(char c
, const char *a
, const char *b
) {
191 bool separator
= false;
201 return (separator
&& (*a
== 0 || *b
== 0));
212 bool namespace_complex_pattern(const char *pattern
, const char *value
) {
213 return complex_pattern_check('.', pattern
, value
);
216 bool path_complex_pattern(const char *pattern
, const char *value
) {
217 return complex_pattern_check('/', pattern
, value
);
221 * Simple pattern match
222 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
223 * consist of labels with @c as separator. This function returns true, if:
224 * - if @a and @b are equal
225 * - if @a is a prefix of @b, and the first following character in @b (or the
226 * last character in @a) is @c
227 * The second rule basically makes sure that if @a is a prefix of @b, then @b
228 * must follow with a new label separated by @c. It cannot extend the label.
230 static bool simple_pattern_check(char c
, const char *a
, const char *b
) {
231 bool separator
= false;
241 return *a
== 0 && (*b
== c
|| separator
);
252 bool namespace_simple_pattern(const char *pattern
, const char *value
) {
253 return simple_pattern_check('.', pattern
, value
);
256 bool path_simple_pattern(const char *pattern
, const char *value
) {
257 return simple_pattern_check('/', pattern
, value
);
260 int bus_message_type_from_string(const char *s
, uint8_t *u
) {
261 if (streq(s
, "signal"))
262 *u
= SD_BUS_MESSAGE_SIGNAL
;
263 else if (streq(s
, "method_call"))
264 *u
= SD_BUS_MESSAGE_METHOD_CALL
;
265 else if (streq(s
, "error"))
266 *u
= SD_BUS_MESSAGE_METHOD_ERROR
;
267 else if (streq(s
, "method_return"))
268 *u
= SD_BUS_MESSAGE_METHOD_RETURN
;
275 const char *bus_message_type_to_string(uint8_t u
) {
276 if (u
== SD_BUS_MESSAGE_SIGNAL
)
278 else if (u
== SD_BUS_MESSAGE_METHOD_CALL
)
279 return "method_call";
280 else if (u
== SD_BUS_MESSAGE_METHOD_ERROR
)
282 else if (u
== SD_BUS_MESSAGE_METHOD_RETURN
)
283 return "method_return";
288 char *bus_address_escape(const char *v
) {
292 r
= new(char, strlen(v
)*3+1);
296 for (a
= v
, b
= r
; *a
; a
++) {
298 if ((*a
>= '0' && *a
<= '9') ||
299 (*a
>= 'a' && *a
<= 'z') ||
300 (*a
>= 'A' && *a
<= 'Z') ||
305 *(b
++) = hexchar(*a
>> 4);
306 *(b
++) = hexchar(*a
& 0xF);
314 int bus_maybe_reply_error(sd_bus_message
*m
, int r
, sd_bus_error
*error
) {
318 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
319 sd_bus_reply_method_errno(m
, r
, error
);
321 } else if (sd_bus_error_is_set(error
)) {
322 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
323 sd_bus_reply_method_error(m
, error
);
327 log_debug("Failed to process message type=%s sender=%s destination=%s path=%s interface=%s member=%s cookie=%" PRIu64
" reply_cookie=%" PRIu64
" signature=%s error-name=%s error-message=%s: %s",
328 bus_message_type_to_string(m
->header
->type
),
329 strna(sd_bus_message_get_sender(m
)),
330 strna(sd_bus_message_get_destination(m
)),
331 strna(sd_bus_message_get_path(m
)),
332 strna(sd_bus_message_get_interface(m
)),
333 strna(sd_bus_message_get_member(m
)),
334 BUS_MESSAGE_COOKIE(m
),
336 strna(m
->root_container
.signature
),
337 strna(m
->error
.name
),
338 strna(m
->error
.message
),
339 bus_error_message(error
, r
));