1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
8 #include "alloc-util.h"
9 #include "bus-internal.h"
10 #include "bus-message.h"
11 #include "hexdecoct.h"
12 #include "string-util.h"
14 bool object_path_is_valid(const char *p
) {
27 for (slash
= true, q
= p
+1; *q
; q
++)
37 (*q
>= 'a' && *q
<= 'z') ||
38 (*q
>= 'A' && *q
<= 'Z') ||
39 (*q
>= '0' && *q
<= '9') ||
54 char* object_path_startswith(const char *a
, const char *b
) {
57 if (!object_path_is_valid(a
) ||
58 !object_path_is_valid(b
))
77 bool interface_name_is_valid(const char *p
) {
79 bool dot
, found_dot
= false;
84 for (dot
= true, q
= p
; *q
; q
++)
89 found_dot
= dot
= true;
94 (*q
>= 'a' && *q
<= 'z') ||
95 (*q
>= 'A' && *q
<= 'Z') ||
96 (!dot
&& *q
>= '0' && *q
<= '9') ||
117 bool service_name_is_valid(const char *p
) {
119 bool dot
, found_dot
= false, unique
;
124 unique
= p
[0] == ':';
126 for (dot
= true, q
= unique
? p
+1 : p
; *q
; q
++)
131 found_dot
= dot
= true;
136 (*q
>= 'a' && *q
<= 'z') ||
137 (*q
>= 'A' && *q
<= 'Z') ||
138 ((!dot
|| unique
) && *q
>= '0' && *q
<= '9') ||
139 IN_SET(*q
, '_', '-');
159 char* service_name_startswith(const char *a
, const char *b
) {
162 if (!service_name_is_valid(a
) ||
163 !service_name_is_valid(b
))
166 p
= startswith(a
, b
);
174 return (char*) p
+ 1;
179 bool member_name_is_valid(const char *p
) {
185 for (q
= p
; *q
; q
++) {
189 (*q
>= 'a' && *q
<= 'z') ||
190 (*q
>= 'A' && *q
<= 'Z') ||
191 (*q
>= '0' && *q
<= '9') ||
205 * Complex pattern match
206 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
207 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
208 * spearator. This function returns true if:
209 * - both strings are equal
210 * - either is a prefix of the other and ends with @c
211 * The second rule makes sure that either string needs to be fully included in
212 * the other, and the string which is considered the prefix needs to end with a
215 static bool complex_pattern_check(char c
, const char *a
, const char *b
) {
216 bool separator
= false;
226 return (separator
&& (*a
== 0 || *b
== 0));
237 bool namespace_complex_pattern(const char *pattern
, const char *value
) {
238 return complex_pattern_check('.', pattern
, value
);
241 bool path_complex_pattern(const char *pattern
, const char *value
) {
242 return complex_pattern_check('/', pattern
, value
);
246 * Simple pattern match
247 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
248 * consist of labels with @c as separator. This function returns true, if:
249 * - if @a and @b are equal
250 * - if @a is a prefix of @b, and the first following character in @b (or the
251 * last character in @a) is @c
252 * The second rule basically makes sure that if @a is a prefix of @b, then @b
253 * must follow with a new label separated by @c. It cannot extend the label.
255 static bool simple_pattern_check(char c
, const char *a
, const char *b
) {
256 bool separator
= false;
266 return *a
== 0 && (*b
== c
|| separator
);
277 bool namespace_simple_pattern(const char *pattern
, const char *value
) {
278 return simple_pattern_check('.', pattern
, value
);
281 bool path_simple_pattern(const char *pattern
, const char *value
) {
282 return simple_pattern_check('/', pattern
, value
);
285 int bus_message_type_from_string(const char *s
, uint8_t *u
) {
286 if (streq(s
, "signal"))
287 *u
= SD_BUS_MESSAGE_SIGNAL
;
288 else if (streq(s
, "method_call"))
289 *u
= SD_BUS_MESSAGE_METHOD_CALL
;
290 else if (streq(s
, "error"))
291 *u
= SD_BUS_MESSAGE_METHOD_ERROR
;
292 else if (streq(s
, "method_return"))
293 *u
= SD_BUS_MESSAGE_METHOD_RETURN
;
300 const char *bus_message_type_to_string(uint8_t u
) {
301 if (u
== SD_BUS_MESSAGE_SIGNAL
)
303 else if (u
== SD_BUS_MESSAGE_METHOD_CALL
)
304 return "method_call";
305 else if (u
== SD_BUS_MESSAGE_METHOD_ERROR
)
307 else if (u
== SD_BUS_MESSAGE_METHOD_RETURN
)
308 return "method_return";
313 char *bus_address_escape(const char *v
) {
317 r
= new(char, strlen(v
)*3+1);
321 for (a
= v
, b
= r
; *a
; a
++) {
323 if ((*a
>= '0' && *a
<= '9') ||
324 (*a
>= 'a' && *a
<= 'z') ||
325 (*a
>= 'A' && *a
<= 'Z') ||
330 *(b
++) = hexchar(*a
>> 4);
331 *(b
++) = hexchar(*a
& 0xF);
339 int bus_maybe_reply_error(sd_bus_message
*m
, int r
, sd_bus_error
*error
) {
343 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
344 sd_bus_reply_method_errno(m
, r
, error
);
346 } else if (sd_bus_error_is_set(error
)) {
347 if (m
->header
->type
== SD_BUS_MESSAGE_METHOD_CALL
)
348 sd_bus_reply_method_error(m
, error
);
352 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",
353 bus_message_type_to_string(m
->header
->type
),
354 strna(sd_bus_message_get_sender(m
)),
355 strna(sd_bus_message_get_destination(m
)),
356 strna(sd_bus_message_get_path(m
)),
357 strna(sd_bus_message_get_interface(m
)),
358 strna(sd_bus_message_get_member(m
)),
359 BUS_MESSAGE_COOKIE(m
),
361 strna(m
->root_container
.signature
),
362 strna(m
->error
.name
),
363 strna(m
->error
.message
),
364 bus_error_message(error
, r
));