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 #if 0 /// UNNEEDED by elogind
160 char* service_name_startswith(const char *a, const char *b) {
163 if (!service_name_is_valid(a) ||
164 !service_name_is_valid(b))
167 p = startswith(a, b);
175 return (char*) p + 1;
181 bool member_name_is_valid(const char *p) {
187 for (q = p; *q; q++) {
191 (*q >= 'a' && *q <= 'z') ||
192 (*q >= 'A' && *q <= 'Z') ||
193 (*q >= '0' && *q <= '9') ||
207 * Complex pattern match
208 * This checks whether @a is a 'complex-prefix' of @b, or @b is a
209 * 'complex-prefix' of @a, based on strings that consist of labels with @c as
210 * spearator. This function returns true if:
211 * - both strings are equal
212 * - either is a prefix of the other and ends with @c
213 * The second rule makes sure that either string needs to be fully included in
214 * the other, and the string which is considered the prefix needs to end with a
217 static bool complex_pattern_check(char c, const char *a, const char *b) {
218 bool separator = false;
228 return (separator && (*a == 0 || *b == 0));
239 bool namespace_complex_pattern(const char *pattern, const char *value) {
240 return complex_pattern_check('.', pattern, value);
243 bool path_complex_pattern(const char *pattern, const char *value) {
244 return complex_pattern_check('/', pattern, value);
248 * Simple pattern match
249 * This checks whether @a is a 'simple-prefix' of @b, based on strings that
250 * consist of labels with @c as separator. This function returns true, if:
251 * - if @a and @b are equal
252 * - if @a is a prefix of @b, and the first following character in @b (or the
253 * last character in @a) is @c
254 * The second rule basically makes sure that if @a is a prefix of @b, then @b
255 * must follow with a new label separated by @c. It cannot extend the label.
257 static bool simple_pattern_check(char c, const char *a, const char *b) {
258 bool separator = false;
268 return *a == 0 && (*b == c || separator);
279 bool namespace_simple_pattern(const char *pattern, const char *value) {
280 return simple_pattern_check('.', pattern, value);
283 bool path_simple_pattern(const char *pattern, const char *value) {
284 return simple_pattern_check('/', pattern, value);
287 int bus_message_type_from_string(const char *s, uint8_t *u) {
288 if (streq(s, "signal"))
289 *u = SD_BUS_MESSAGE_SIGNAL;
290 else if (streq(s, "method_call"))
291 *u = SD_BUS_MESSAGE_METHOD_CALL;
292 else if (streq(s, "error"))
293 *u = SD_BUS_MESSAGE_METHOD_ERROR;
294 else if (streq(s, "method_return"))
295 *u = SD_BUS_MESSAGE_METHOD_RETURN;
302 const char *bus_message_type_to_string(uint8_t u) {
303 if (u == SD_BUS_MESSAGE_SIGNAL)
305 else if (u == SD_BUS_MESSAGE_METHOD_CALL)
306 return "method_call";
307 else if (u == SD_BUS_MESSAGE_METHOD_ERROR)
309 else if (u == SD_BUS_MESSAGE_METHOD_RETURN)
310 return "method_return";
315 char *bus_address_escape(const char *v) {
319 r = new(char, strlen(v)*3+1);
323 for (a = v, b = r; *a; a++) {
325 if ((*a >= '0' && *a <= '9') ||
326 (*a >= 'a' && *a <= 'z') ||
327 (*a >= 'A' && *a <= 'Z') ||
332 *(b++) = hexchar(*a >> 4);
333 *(b++) = hexchar(*a & 0xF);
341 int bus_maybe_reply_error(sd_bus_message *m, int r, sd_bus_error *error) {
345 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
346 sd_bus_reply_method_errno(m, r, error);
348 } else if (sd_bus_error_is_set(error)) {
349 if (m->header->type == SD_BUS_MESSAGE_METHOD_CALL)
350 sd_bus_reply_method_error(m, error);
354 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",
355 bus_message_type_to_string(m->header->type),
356 strna(sd_bus_message_get_sender(m)),
357 strna(sd_bus_message_get_destination(m)),
358 strna(sd_bus_message_get_path(m)),
359 strna(sd_bus_message_get_interface(m)),
360 strna(sd_bus_message_get_member(m)),
361 BUS_MESSAGE_COOKIE(m),
363 strna(m->root_container.signature),
364 strna(m->error.name),
365 strna(m->error.message),
366 bus_error_message(error, r));