1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2013 Lennart Poettering
6 #include "alloc-util.h"
7 #include "bus-internal.h"
8 #include "bus-message.h"
10 #include "string-util.h"
12 bool object_path_is_valid(const char *p
) {
25 for (slash
= true, q
= p
+1; *q
; q
++)
35 (*q
>= 'a' && *q
<= 'z') ||
36 (*q
>= 'A' && *q
<= 'Z') ||
37 (*q
>= '0' && *q
<= '9') ||
52 char* object_path_startswith(const char *a
, const char *b
) {
55 if (!object_path_is_valid(a
) ||
56 !object_path_is_valid(b
))
75 bool interface_name_is_valid(const char *p
) {
77 bool dot
, found_dot
= false;
82 for (dot
= true, q
= p
; *q
; q
++)
87 found_dot
= dot
= true;
92 (*q
>= 'a' && *q
<= 'z') ||
93 (*q
>= 'A' && *q
<= 'Z') ||
94 (!dot
&& *q
>= '0' && *q
<= '9') ||
115 bool service_name_is_valid(const char *p
) {
117 bool dot
, found_dot
= false, unique
;
122 unique
= p
[0] == ':';
124 for (dot
= true, q
= unique
? p
+1 : p
; *q
; q
++)
129 found_dot
= dot
= true;
134 (*q
>= 'a' && *q
<= 'z') ||
135 (*q
>= 'A' && *q
<= 'Z') ||
136 ((!dot
|| unique
) && *q
>= '0' && *q
<= '9') ||
137 IN_SET(*q
, '_', '-');
157 char* service_name_startswith(const char *a
, const char *b
) {
160 if (!service_name_is_valid(a
) ||
161 !service_name_is_valid(b
))
164 p
= startswith(a
, b
);
172 return (char*) p
+ 1;
177 bool member_name_is_valid(const char *p
) {
183 for (q
= p
; *q
; q
++) {
187 (*q
>= 'a' && *q
<= 'z') ||
188 (*q
>= 'A' && *q
<= 'Z') ||
189 (*q
>= '0' && *q
<= '9') ||
203 * Complex pattern match
204 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
205 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
206 * spearator. This function returns true if:
207 * - both strings are equal
208 * - either is a prefix of the other and ends with @c
209 * The second rule makes sure that either string needs to be fully included in
210 * the other, and the string which is considered the prefix needs to end with a
213 static bool complex_pattern_check(char c
, const char *a
, const char *b
) {
214 bool separator
= false;
224 return (separator
&& (*a
== 0 || *b
== 0));
235 bool namespace_complex_pattern(const char *pattern
, const char *value
) {
236 return complex_pattern_check('.', pattern
, value
);
239 bool path_complex_pattern(const char *pattern
, const char *value
) {
240 return complex_pattern_check('/', pattern
, value
);
244 * Simple pattern match
245 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
246 * consist of labels with @c as separator. This function returns true, if:
247 * - if @a and @b are equal
248 * - if @a is a prefix of @b, and the first following character in @b (or the
249 * last character in @a) is @c
250 * The second rule basically makes sure that if @a is a prefix of @b, then @b
251 * must follow with a new label separated by @c. It cannot extend the label.
253 static bool simple_pattern_check(char c
, const char *a
, const char *b
) {
254 bool separator
= false;
264 return *a
== 0 && (*b
== c
|| separator
);
275 bool namespace_simple_pattern(const char *pattern
, const char *value
) {
276 return simple_pattern_check('.', pattern
, value
);
279 bool path_simple_pattern(const char *pattern
, const char *value
) {
280 return simple_pattern_check('/', pattern
, value
);
283 int bus_message_type_from_string(const char *s
, uint8_t *u
) {
284 if (streq(s
, "signal"))
285 *u
= SD_BUS_MESSAGE_SIGNAL
;
286 else if (streq(s
, "method_call"))
287 *u
= SD_BUS_MESSAGE_METHOD_CALL
;
288 else if (streq(s
, "error"))
289 *u
= SD_BUS_MESSAGE_METHOD_ERROR
;
290 else if (streq(s
, "method_return"))
291 *u
= SD_BUS_MESSAGE_METHOD_RETURN
;
298 const char *bus_message_type_to_string(uint8_t u
) {
299 if (u
== SD_BUS_MESSAGE_SIGNAL
)
301 else if (u
== SD_BUS_MESSAGE_METHOD_CALL
)
302 return "method_call";
303 else if (u
== SD_BUS_MESSAGE_METHOD_ERROR
)
305 else if (u
== SD_BUS_MESSAGE_METHOD_RETURN
)
306 return "method_return";
311 char *bus_address_escape(const char *v
) {
315 r
= new(char, strlen(v
)*3+1);
319 for (a
= v
, b
= r
; *a
; a
++) {
321 if ((*a
>= '0' && *a
<= '9') ||
322 (*a
>= 'a' && *a
<= 'z') ||
323 (*a
>= 'A' && *a
<= 'Z') ||
328 *(b
++) = hexchar(*a
>> 4);
329 *(b
++) = hexchar(*a
& 0xF);
337 int bus_maybe_reply_error(sd_bus_message
*m
, int r
, sd_bus_error
*error
) {
341 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
342 sd_bus_reply_method_errno(m
, r
, error
);
344 } else if (sd_bus_error_is_set(error
)) {
345 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
346 sd_bus_reply_method_error(m
, error
);
350 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",
351 bus_message_type_to_string(m
->header
->type
),
352 strna(sd_bus_message_get_sender(m
)),
353 strna(sd_bus_message_get_destination(m
)),
354 strna(sd_bus_message_get_path(m
)),
355 strna(sd_bus_message_get_interface(m
)),
356 strna(sd_bus_message_get_member(m
)),
357 BUS_MESSAGE_COOKIE(m
),
359 strna(m
->root_container
.signature
),
360 strna(m
->error
.name
),
361 strna(m
->error
.message
),
362 bus_error_message(error
, r
));