]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/unit-name.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
10 #include "glob-util.h"
11 #include "hexdecoct.h"
12 #include "path-util.h"
14 #include "string-util.h"
16 #include "unit-name.h"
18 /* Characters valid in a unit name. */
24 /* The same, but also permits the single @ character that may appear */
25 #define VALID_CHARS_WITH_AT \
29 /* All chars valid in a unit name glob */
30 #define VALID_CHARS_GLOB \
34 bool unit_name_is_valid(const char *n
, UnitNameFlags flags
) {
35 const char *e
, *i
, *at
;
37 assert((flags
& ~(UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
)) == 0);
39 if (_unlikely_(flags
== 0))
45 if (strlen(n
) >= UNIT_NAME_MAX
)
52 if (unit_type_from_string(e
+ 1) < 0)
55 for (i
= n
, at
= NULL
; i
< e
; i
++) {
60 if (!strchr("@" VALID_CHARS
, *i
))
67 if (flags
& UNIT_NAME_PLAIN
)
71 if (flags
& UNIT_NAME_INSTANCE
)
75 if (flags
& UNIT_NAME_TEMPLATE
)
76 if (at
&& e
== at
+ 1)
82 bool unit_prefix_is_valid(const char *p
) {
84 /* We don't allow additional @ in the prefix string */
89 return in_charset(p
, VALID_CHARS
);
92 bool unit_instance_is_valid(const char *i
) {
94 /* The max length depends on the length of the string, so we
95 * don't really check this here. */
100 /* We allow additional @ in the instance string, we do not
101 * allow them in the prefix! */
103 return in_charset(i
, "@" VALID_CHARS
);
106 bool unit_suffix_is_valid(const char *s
) {
113 if (unit_type_from_string(s
+ 1) < 0)
119 int unit_name_to_prefix(const char *n
, char **ret
) {
126 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
135 s
= strndup(n
, p
- n
);
143 int unit_name_to_instance(const char *n
, char **ret
) {
148 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
151 /* Everything past the first @ and before the last . is the instance */
156 return UNIT_NAME_PLAIN
;
166 char *i
= strndup(p
, d
-p
);
172 return d
> p
? UNIT_NAME_INSTANCE
: UNIT_NAME_TEMPLATE
;
175 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
182 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
189 s
= strndup(n
, d
- n
);
197 UnitType
unit_name_to_type(const char *n
) {
202 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
203 return _UNIT_TYPE_INVALID
;
205 assert_se(e
= strrchr(n
, '.'));
207 return unit_type_from_string(e
+ 1);
210 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
218 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
221 if (!unit_suffix_is_valid(suffix
))
224 assert_se(e
= strrchr(n
, '.'));
229 s
= new(char, a
+ b
+ 1);
233 strcpy(mempcpy(s
, n
, a
), suffix
);
239 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
246 if (suffix
[0] != '.')
249 type
= unit_type_from_string(suffix
+ 1);
253 return unit_name_build_from_type(prefix
, instance
, type
, ret
);
256 int unit_name_build_from_type(const char *prefix
, const char *instance
, UnitType type
, char **ret
) {
262 assert(type
< _UNIT_TYPE_MAX
);
265 if (!unit_prefix_is_valid(prefix
))
268 if (instance
&& !unit_instance_is_valid(instance
))
271 ut
= unit_type_to_string(type
);
274 s
= strjoin(prefix
, ".", ut
);
276 s
= strjoin(prefix
, "@", instance
, ".", ut
);
284 static char *do_escape_char(char c
, char *t
) {
289 *(t
++) = hexchar(c
>> 4);
295 static char *do_escape(const char *f
, char *t
) {
299 /* do not create units with a leading '.', like for "/.dotdir" mount points */
301 t
= do_escape_char(*f
, t
);
308 else if (IN_SET(*f
, '-', '\\') || !strchr(VALID_CHARS
, *f
))
309 t
= do_escape_char(*f
, t
);
317 char *unit_name_escape(const char *f
) {
322 r
= new(char, strlen(f
)*4+1);
332 int unit_name_unescape(const char *f
, char **ret
) {
333 _cleanup_free_
char *r
= NULL
;
342 for (t
= r
; *f
; f
++) {
345 else if (*f
== '\\') {
359 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
372 int unit_name_path_escape(const char *f
, char **ret
) {
382 path_simplify(p
, false);
384 if (empty_or_root(p
))
387 if (!path_is_normalized(p
))
390 /* Truncate trailing slashes */
391 delete_trailing_chars(p
, "/");
393 /* Truncate leading slashes */
394 p
= skip_leading_chars(p
, "/");
396 s
= unit_name_escape(p
);
405 int unit_name_path_unescape(const char *f
, char **ret
) {
406 _cleanup_free_
char *s
= NULL
;
419 _cleanup_free_
char *w
= NULL
;
421 r
= unit_name_unescape(f
, &w
);
425 /* Don't accept trailing or leading slashes */
426 if (startswith(w
, "/") || endswith(w
, "/"))
429 /* Prefix a slash again */
434 if (!path_is_normalized(s
))
444 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
453 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
455 if (!unit_instance_is_valid(i
))
458 assert_se(p
= strchr(f
, '@'));
459 assert_se(e
= strrchr(f
, '.'));
464 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
468 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
474 int unit_name_template(const char *f
, char **ret
) {
482 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
485 assert_se(p
= strchr(f
, '@'));
486 assert_se(e
= strrchr(f
, '.'));
490 s
= new(char, a
+ 1 + strlen(e
) + 1);
494 strcpy(mempcpy(s
, f
, a
+ 1), e
);
500 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
501 _cleanup_free_
char *p
= NULL
;
509 if (!unit_suffix_is_valid(suffix
))
512 r
= unit_name_path_escape(path
, &p
);
516 s
= strjoin(p
, suffix
);
524 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
525 _cleanup_free_
char *p
= NULL
;
534 if (!unit_prefix_is_valid(prefix
))
537 if (!unit_suffix_is_valid(suffix
))
540 r
= unit_name_path_escape(path
, &p
);
544 s
= strjoin(prefix
, "@", p
, suffix
);
552 int unit_name_to_path(const char *name
, char **ret
) {
553 _cleanup_free_
char *prefix
= NULL
;
558 r
= unit_name_to_prefix(name
, &prefix
);
562 return unit_name_path_unescape(prefix
, ret
);
565 static bool do_escape_mangle(const char *f
, bool allow_globs
, char *t
) {
566 const char *valid_chars
;
567 bool mangled
= false;
572 /* We'll only escape the obvious characters here, to play safe.
574 * Returns true if any characters were mangled, false otherwise.
577 valid_chars
= allow_globs
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
583 } else if (!strchr(valid_chars
, *f
)) {
584 t
= do_escape_char(*f
, t
);
594 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
595 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
596 * except that @suffix is appended if a valid unit suffix is not present.
598 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
600 int unit_name_mangle_with_suffix(const char *name
, UnitNameMangle flags
, const char *suffix
, char **ret
) {
609 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
612 if (!unit_suffix_is_valid(suffix
))
615 /* Already a fully valid unit name? If so, no mangling is necessary... */
616 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
619 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
620 if ((flags
& UNIT_NAME_MANGLE_GLOB
) &&
621 string_is_glob(name
) &&
622 in_charset(name
, VALID_CHARS_GLOB
))
625 if (is_device_path(name
)) {
626 r
= unit_name_from_path(name
, ".device", ret
);
633 if (path_is_absolute(name
)) {
634 r
= unit_name_from_path(name
, ".mount", ret
);
641 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
645 mangled
= do_escape_mangle(name
, flags
& UNIT_NAME_MANGLE_GLOB
, s
);
647 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
648 "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)",
651 /* 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
653 if ((!(flags
& UNIT_NAME_MANGLE_GLOB
) || !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
668 int slice_build_parent_slice(const char *slice
, char **ret
) {
675 if (!slice_name_is_valid(slice
))
678 if (streq(slice
, SPECIAL_ROOT_SLICE
)) {
687 dash
= strrchr(s
, '-');
689 strcpy(dash
, ".slice");
691 r
= free_and_strdup(&s
, SPECIAL_ROOT_SLICE
);
702 int slice_build_subslice(const char *slice
, const char *name
, char **ret
) {
709 if (!slice_name_is_valid(slice
))
712 if (!unit_prefix_is_valid(name
))
715 if (streq(slice
, SPECIAL_ROOT_SLICE
))
716 subslice
= strjoin(name
, ".slice");
720 assert_se(e
= endswith(slice
, ".slice"));
722 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
726 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
733 bool slice_name_is_valid(const char *name
) {
737 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
740 if (streq(name
, SPECIAL_ROOT_SLICE
))
743 e
= endswith(name
, ".slice");
747 for (p
= name
; p
< e
; p
++) {
751 /* Don't allow initial dash */
755 /* Don't allow multiple dashes */
764 /* Don't allow trailing hash */