]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/unit-name.c
8115bff14ae8953cc861bd5aabf4095e49537179
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2010 Lennart Poettering
12 #include "alloc-util.h"
13 #include "glob-util.h"
14 #include "hexdecoct.h"
15 #include "path-util.h"
17 #include "string-util.h"
19 #include "unit-name.h"
21 /* Characters valid in a unit name. */
27 /* The same, but also permits the single @ character that may appear */
28 #define VALID_CHARS_WITH_AT \
32 /* All chars valid in a unit name glob */
33 #define VALID_CHARS_GLOB \
37 bool unit_name_is_valid(const char *n
, UnitNameFlags flags
) {
38 const char *e
, *i
, *at
;
40 assert((flags
& ~(UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
)) == 0);
42 if (_unlikely_(flags
== 0))
48 if (strlen(n
) >= UNIT_NAME_MAX
)
55 if (unit_type_from_string(e
+ 1) < 0)
58 for (i
= n
, at
= NULL
; i
< e
; i
++) {
63 if (!strchr("@" VALID_CHARS
, *i
))
70 if (flags
& UNIT_NAME_PLAIN
)
74 if (flags
& UNIT_NAME_INSTANCE
)
78 if (flags
& UNIT_NAME_TEMPLATE
)
79 if (at
&& e
== at
+ 1)
85 bool unit_prefix_is_valid(const char *p
) {
87 /* We don't allow additional @ in the prefix string */
92 return in_charset(p
, VALID_CHARS
);
95 bool unit_instance_is_valid(const char *i
) {
97 /* The max length depends on the length of the string, so we
98 * don't really check this here. */
103 /* We allow additional @ in the instance string, we do not
104 * allow them in the prefix! */
106 return in_charset(i
, "@" VALID_CHARS
);
109 bool unit_suffix_is_valid(const char *s
) {
116 if (unit_type_from_string(s
+ 1) < 0)
122 int unit_name_to_prefix(const char *n
, char **ret
) {
129 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
138 s
= strndup(n
, p
- n
);
146 int unit_name_to_instance(const char *n
, char **instance
) {
153 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
156 /* Everything past the first @ and before the last . is the instance */
177 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
184 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
191 s
= strndup(n
, d
- n
);
199 UnitType
unit_name_to_type(const char *n
) {
204 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
205 return _UNIT_TYPE_INVALID
;
207 assert_se(e
= strrchr(n
, '.'));
209 return unit_type_from_string(e
+ 1);
212 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
220 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
223 if (!unit_suffix_is_valid(suffix
))
226 assert_se(e
= strrchr(n
, '.'));
231 s
= new(char, a
+ b
+ 1);
235 strcpy(mempcpy(s
, n
, a
), suffix
);
241 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
248 if (suffix
[0] != '.')
251 type
= unit_type_from_string(suffix
+ 1);
255 return unit_name_build_from_type(prefix
, instance
, type
, ret
);
258 int unit_name_build_from_type(const char *prefix
, const char *instance
, UnitType type
, char **ret
) {
264 assert(type
< _UNIT_TYPE_MAX
);
267 if (!unit_prefix_is_valid(prefix
))
270 if (instance
&& !unit_instance_is_valid(instance
))
273 ut
= unit_type_to_string(type
);
276 s
= strjoin(prefix
, ".", ut
);
278 s
= strjoin(prefix
, "@", instance
, ".", ut
);
286 static char *do_escape_char(char c
, char *t
) {
291 *(t
++) = hexchar(c
>> 4);
297 static char *do_escape(const char *f
, char *t
) {
301 /* do not create units with a leading '.', like for "/.dotdir" mount points */
303 t
= do_escape_char(*f
, t
);
310 else if (IN_SET(*f
, '-', '\\') || !strchr(VALID_CHARS
, *f
))
311 t
= do_escape_char(*f
, t
);
319 char *unit_name_escape(const char *f
) {
324 r
= new(char, strlen(f
)*4+1);
334 int unit_name_unescape(const char *f
, char **ret
) {
335 _cleanup_free_
char *r
= NULL
;
344 for (t
= r
; *f
; f
++) {
347 else if (*f
== '\\') {
361 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
374 int unit_name_path_escape(const char *f
, char **ret
) {
384 path_simplify(p
, false);
386 if (empty_or_root(p
))
389 if (!path_is_normalized(p
))
392 /* Truncate trailing slashes */
393 delete_trailing_chars(p
, "/");
395 /* Truncate leading slashes */
396 p
= skip_leading_chars(p
, "/");
398 s
= unit_name_escape(p
);
407 int unit_name_path_unescape(const char *f
, char **ret
) {
423 r
= unit_name_unescape(f
, &w
);
427 /* Don't accept trailing or leading slashes */
428 if (startswith(w
, "/") || endswith(w
, "/")) {
433 /* Prefix a slash again */
434 s
= strappend("/", w
);
439 if (!path_is_normalized(s
)) {
453 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
462 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
464 if (!unit_instance_is_valid(i
))
467 assert_se(p
= strchr(f
, '@'));
468 assert_se(e
= strrchr(f
, '.'));
473 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
477 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
483 int unit_name_template(const char *f
, char **ret
) {
491 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
494 assert_se(p
= strchr(f
, '@'));
495 assert_se(e
= strrchr(f
, '.'));
499 s
= new(char, a
+ 1 + strlen(e
) + 1);
503 strcpy(mempcpy(s
, f
, a
+ 1), e
);
509 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
510 _cleanup_free_
char *p
= NULL
;
518 if (!unit_suffix_is_valid(suffix
))
521 r
= unit_name_path_escape(path
, &p
);
525 s
= strappend(p
, suffix
);
533 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
534 _cleanup_free_
char *p
= NULL
;
543 if (!unit_prefix_is_valid(prefix
))
546 if (!unit_suffix_is_valid(suffix
))
549 r
= unit_name_path_escape(path
, &p
);
553 s
= strjoin(prefix
, "@", p
, suffix
);
561 int unit_name_to_path(const char *name
, char **ret
) {
562 _cleanup_free_
char *prefix
= NULL
;
567 r
= unit_name_to_prefix(name
, &prefix
);
571 return unit_name_path_unescape(prefix
, ret
);
574 static bool do_escape_mangle(const char *f
, bool allow_globs
, char *t
) {
575 const char *valid_chars
;
576 bool mangled
= false;
581 /* We'll only escape the obvious characters here, to play safe.
583 * Returns true if any characters were mangled, false otherwise.
586 valid_chars
= allow_globs
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
592 } else if (!strchr(valid_chars
, *f
)) {
593 t
= do_escape_char(*f
, t
);
603 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
604 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
605 * except that @suffix is appended if a valid unit suffix is not present.
607 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
609 int unit_name_mangle_with_suffix(const char *name
, UnitNameMangle flags
, const char *suffix
, char **ret
) {
618 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
621 if (!unit_suffix_is_valid(suffix
))
624 /* Already a fully valid unit name? If so, no mangling is necessary... */
625 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
628 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
629 if ((flags
& UNIT_NAME_MANGLE_GLOB
) &&
630 string_is_glob(name
) &&
631 in_charset(name
, VALID_CHARS_GLOB
))
634 if (is_device_path(name
)) {
635 r
= unit_name_from_path(name
, ".device", ret
);
642 if (path_is_absolute(name
)) {
643 r
= unit_name_from_path(name
, ".mount", ret
);
650 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
654 mangled
= do_escape_mangle(name
, flags
& UNIT_NAME_MANGLE_GLOB
, s
);
656 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
657 "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)",
660 /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow "foo.*" as a
662 if ((!(flags
& UNIT_NAME_MANGLE_GLOB
) || !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
677 int slice_build_parent_slice(const char *slice
, char **ret
) {
684 if (!slice_name_is_valid(slice
))
687 if (streq(slice
, SPECIAL_ROOT_SLICE
)) {
696 dash
= strrchr(s
, '-');
698 strcpy(dash
, ".slice");
700 r
= free_and_strdup(&s
, SPECIAL_ROOT_SLICE
);
711 int slice_build_subslice(const char *slice
, const char *name
, char **ret
) {
718 if (!slice_name_is_valid(slice
))
721 if (!unit_prefix_is_valid(name
))
724 if (streq(slice
, SPECIAL_ROOT_SLICE
))
725 subslice
= strappend(name
, ".slice");
729 assert_se(e
= endswith(slice
, ".slice"));
731 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
735 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
742 bool slice_name_is_valid(const char *name
) {
746 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
749 if (streq(name
, SPECIAL_ROOT_SLICE
))
752 e
= endswith(name
, ".slice");
756 for (p
= name
; p
< e
; p
++) {
760 /* Don't allow initial dash */
764 /* Don't allow multiple dashes */
773 /* Don't allow trailing hash */