1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 #include "alloc-util.h"
6 #include "bus-internal.h"
7 #include "bus-message.h"
9 #include "string-util.h"
11 bool object_path_is_valid(const char *p
) {
24 for (slash
= true, q
= p
+1; *q
; q
++)
34 (*q
>= 'a' && *q
<= 'z') ||
35 (*q
>= 'A' && *q
<= 'Z') ||
36 (*q
>= '0' && *q
<= '9') ||
51 char* object_path_startswith(const char *a
, const char *b
) {
54 if (!object_path_is_valid(a
) ||
55 !object_path_is_valid(b
))
74 bool interface_name_is_valid(const char *p
) {
76 bool dot
, found_dot
= false;
81 for (dot
= true, q
= p
; *q
; q
++)
86 found_dot
= dot
= true;
91 (*q
>= 'a' && *q
<= 'z') ||
92 (*q
>= 'A' && *q
<= 'Z') ||
93 (!dot
&& *q
>= '0' && *q
<= '9') ||
114 bool service_name_is_valid(const char *p
) {
116 bool dot
, found_dot
= false, unique
;
121 unique
= p
[0] == ':';
123 for (dot
= true, q
= unique
? p
+1 : p
; *q
; q
++)
128 found_dot
= dot
= true;
133 (*q
>= 'a' && *q
<= 'z') ||
134 (*q
>= 'A' && *q
<= 'Z') ||
135 ((!dot
|| unique
) && *q
>= '0' && *q
<= '9') ||
136 IN_SET(*q
, '_', '-');
156 char* service_name_startswith(const char *a
, const char *b
) {
159 if (!service_name_is_valid(a
) ||
160 !service_name_is_valid(b
))
163 p
= startswith(a
, b
);
171 return (char*) p
+ 1;
176 bool member_name_is_valid(const char *p
) {
182 for (q
= p
; *q
; q
++) {
186 (*q
>= 'a' && *q
<= 'z') ||
187 (*q
>= 'A' && *q
<= 'Z') ||
188 (*q
>= '0' && *q
<= '9') ||
202 * Complex pattern match
203 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
204 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
205 * spearator. This function returns true if:
206 * - both strings are equal
207 * - either is a prefix of the other and ends with @c
208 * The second rule makes sure that either string needs to be fully included in
209 * the other, and the string which is considered the prefix needs to end with a
212 static bool complex_pattern_check(char c
, const char *a
, const char *b
) {
213 bool separator
= false;
223 return (separator
&& (*a
== 0 || *b
== 0));
234 bool namespace_complex_pattern(const char *pattern
, const char *value
) {
235 return complex_pattern_check('.', pattern
, value
);
238 bool path_complex_pattern(const char *pattern
, const char *value
) {
239 return complex_pattern_check('/', pattern
, value
);
243 * Simple pattern match
244 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
245 * consist of labels with @c as separator. This function returns true, if:
246 * - if @a and @b are equal
247 * - if @a is a prefix of @b, and the first following character in @b (or the
248 * last character in @a) is @c
249 * The second rule basically makes sure that if @a is a prefix of @b, then @b
250 * must follow with a new label separated by @c. It cannot extend the label.
252 static bool simple_pattern_check(char c
, const char *a
, const char *b
) {
253 bool separator
= false;
263 return *a
== 0 && (*b
== c
|| separator
);
274 bool namespace_simple_pattern(const char *pattern
, const char *value
) {
275 return simple_pattern_check('.', pattern
, value
);
278 bool path_simple_pattern(const char *pattern
, const char *value
) {
279 return simple_pattern_check('/', pattern
, value
);
282 int bus_message_type_from_string(const char *s
, uint8_t *u
) {
283 if (streq(s
, "signal"))
284 *u
= SD_BUS_MESSAGE_SIGNAL
;
285 else if (streq(s
, "method_call"))
286 *u
= SD_BUS_MESSAGE_METHOD_CALL
;
287 else if (streq(s
, "error"))
288 *u
= SD_BUS_MESSAGE_METHOD_ERROR
;
289 else if (streq(s
, "method_return"))
290 *u
= SD_BUS_MESSAGE_METHOD_RETURN
;
297 const char *bus_message_type_to_string(uint8_t u
) {
298 if (u
== SD_BUS_MESSAGE_SIGNAL
)
300 else if (u
== SD_BUS_MESSAGE_METHOD_CALL
)
301 return "method_call";
302 else if (u
== SD_BUS_MESSAGE_METHOD_ERROR
)
304 else if (u
== SD_BUS_MESSAGE_METHOD_RETURN
)
305 return "method_return";
310 char *bus_address_escape(const char *v
) {
314 r
= new(char, strlen(v
)*3+1);
318 for (a
= v
, b
= r
; *a
; a
++) {
320 if ((*a
>= '0' && *a
<= '9') ||
321 (*a
>= 'a' && *a
<= 'z') ||
322 (*a
>= 'A' && *a
<= 'Z') ||
327 *(b
++) = hexchar(*a
>> 4);
328 *(b
++) = hexchar(*a
& 0xF);
336 int bus_maybe_reply_error(sd_bus_message
*m
, int r
, sd_bus_error
*error
) {
340 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
341 sd_bus_reply_method_errno(m
, r
, error
);
343 } else if (sd_bus_error_is_set(error
)) {
344 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
345 sd_bus_reply_method_error(m
, error
);
349 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",
350 bus_message_type_to_string(m
->header
->type
),
351 strna(sd_bus_message_get_sender(m
)),
352 strna(sd_bus_message_get_destination(m
)),
353 strna(sd_bus_message_get_path(m
)),
354 strna(sd_bus_message_get_interface(m
)),
355 strna(sd_bus_message_get_member(m
)),
356 BUS_MESSAGE_COOKIE(m
),
358 strna(m
->root_container
.signature
),
359 strna(m
->error
.name
),
360 strna(m
->error
.message
),
361 bus_error_message(error
, r
));