]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/unit-name.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
14 #include "alloc-util.h"
15 #include "glob-util.h"
16 #include "hexdecoct.h"
17 #include "path-util.h"
19 #include "string-util.h"
21 #include "unit-name.h"
23 /* Characters valid in a unit name. */
29 /* The same, but also permits the single @ character that may appear */
30 #define VALID_CHARS_WITH_AT \
34 /* All chars valid in a unit name glob */
35 #define VALID_CHARS_GLOB \
39 bool unit_name_is_valid(const char *n
, UnitNameFlags flags
) {
40 const char *e
, *i
, *at
;
42 assert((flags
& ~(UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
)) == 0);
44 if (_unlikely_(flags
== 0))
50 if (strlen(n
) >= UNIT_NAME_MAX
)
57 if (unit_type_from_string(e
+ 1) < 0)
60 for (i
= n
, at
= NULL
; i
< e
; i
++) {
65 if (!strchr("@" VALID_CHARS
, *i
))
72 if (flags
& UNIT_NAME_PLAIN
)
76 if (flags
& UNIT_NAME_INSTANCE
)
80 if (flags
& UNIT_NAME_TEMPLATE
)
81 if (at
&& e
== at
+ 1)
87 bool unit_prefix_is_valid(const char *p
) {
89 /* We don't allow additional @ in the prefix string */
94 return in_charset(p
, VALID_CHARS
);
97 bool unit_instance_is_valid(const char *i
) {
99 /* The max length depends on the length of the string, so we
100 * don't really check this here. */
105 /* We allow additional @ in the instance string, we do not
106 * allow them in the prefix! */
108 return in_charset(i
, "@" VALID_CHARS
);
111 bool unit_suffix_is_valid(const char *s
) {
118 if (unit_type_from_string(s
+ 1) < 0)
124 int unit_name_to_prefix(const char *n
, char **ret
) {
131 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
140 s
= strndup(n
, p
- n
);
148 int unit_name_to_instance(const char *n
, char **instance
) {
155 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
158 /* Everything past the first @ and before the last . is the instance */
179 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
186 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
193 s
= strndup(n
, d
- n
);
201 UnitType
unit_name_to_type(const char *n
) {
206 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
207 return _UNIT_TYPE_INVALID
;
209 assert_se(e
= strrchr(n
, '.'));
211 return unit_type_from_string(e
+ 1);
214 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
222 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
225 if (!unit_suffix_is_valid(suffix
))
228 assert_se(e
= strrchr(n
, '.'));
233 s
= new(char, a
+ b
+ 1);
237 strcpy(mempcpy(s
, n
, a
), suffix
);
243 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
252 if (suffix
[0] != '.')
255 type
= unit_type_from_string(suffix
+ 1);
259 return unit_name_build_from_type(prefix
, instance
, type
, ret
);
262 int unit_name_build_from_type(const char *prefix
, const char *instance
, UnitType type
, char **ret
) {
268 assert(type
< _UNIT_TYPE_MAX
);
271 if (!unit_prefix_is_valid(prefix
))
274 if (instance
&& !unit_instance_is_valid(instance
))
277 ut
= unit_type_to_string(type
);
280 s
= strjoin(prefix
, ".", ut
);
282 s
= strjoin(prefix
, "@", instance
, ".", ut
);
290 static char *do_escape_char(char c
, char *t
) {
295 *(t
++) = hexchar(c
>> 4);
301 static char *do_escape(const char *f
, char *t
) {
305 /* do not create units with a leading '.', like for "/.dotdir" mount points */
307 t
= do_escape_char(*f
, t
);
314 else if (IN_SET(*f
, '-', '\\') || !strchr(VALID_CHARS
, *f
))
315 t
= do_escape_char(*f
, t
);
323 char *unit_name_escape(const char *f
) {
328 r
= new(char, strlen(f
)*4+1);
338 int unit_name_unescape(const char *f
, char **ret
) {
339 _cleanup_free_
char *r
= NULL
;
348 for (t
= r
; *f
; f
++) {
351 else if (*f
== '\\') {
365 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
378 int unit_name_path_escape(const char *f
, char **ret
) {
388 path_kill_slashes(p
);
390 if (STR_IN_SET(p
, "/", ""))
393 if (!path_is_normalized(p
))
396 /* Truncate trailing slashes */
397 delete_trailing_chars(p
, "/");
399 /* Truncate leading slashes */
400 p
= skip_leading_chars(p
, "/");
402 s
= unit_name_escape(p
);
411 int unit_name_path_unescape(const char *f
, char **ret
) {
427 r
= unit_name_unescape(f
, &w
);
431 /* Don't accept trailing or leading slashes */
432 if (startswith(w
, "/") || endswith(w
, "/")) {
437 /* Prefix a slash again */
438 s
= strappend("/", w
);
443 if (!path_is_normalized(s
)) {
457 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
466 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
468 if (!unit_instance_is_valid(i
))
471 assert_se(p
= strchr(f
, '@'));
472 assert_se(e
= strrchr(f
, '.'));
477 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
481 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
487 int unit_name_template(const char *f
, char **ret
) {
495 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
498 assert_se(p
= strchr(f
, '@'));
499 assert_se(e
= strrchr(f
, '.'));
503 s
= new(char, a
+ 1 + strlen(e
) + 1);
507 strcpy(mempcpy(s
, f
, a
+ 1), e
);
513 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
514 _cleanup_free_
char *p
= NULL
;
522 if (!unit_suffix_is_valid(suffix
))
525 r
= unit_name_path_escape(path
, &p
);
529 s
= strappend(p
, suffix
);
537 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
538 _cleanup_free_
char *p
= NULL
;
547 if (!unit_prefix_is_valid(prefix
))
550 if (!unit_suffix_is_valid(suffix
))
553 r
= unit_name_path_escape(path
, &p
);
557 s
= strjoin(prefix
, "@", p
, suffix
);
565 int unit_name_to_path(const char *name
, char **ret
) {
566 _cleanup_free_
char *prefix
= NULL
;
571 r
= unit_name_to_prefix(name
, &prefix
);
575 return unit_name_path_unescape(prefix
, ret
);
578 static bool do_escape_mangle(const char *f
, bool allow_globs
, char *t
) {
579 const char *valid_chars
;
580 bool mangled
= false;
585 /* We'll only escape the obvious characters here, to play safe.
587 * Returns true if any characters were mangled, false otherwise.
590 valid_chars
= allow_globs
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
596 } else if (!strchr(valid_chars
, *f
)) {
597 t
= do_escape_char(*f
, t
);
607 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
608 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
609 * except that @suffix is appended if a valid unit suffix is not present.
611 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
613 int unit_name_mangle_with_suffix(const char *name
, UnitNameMangle flags
, const char *suffix
, char **ret
) {
622 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
625 if (!unit_suffix_is_valid(suffix
))
628 /* Already a fully valid unit name? If so, no mangling is necessary... */
629 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
632 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
633 if ((flags
& UNIT_NAME_MANGLE_GLOB
) &&
634 string_is_glob(name
) &&
635 in_charset(name
, VALID_CHARS_GLOB
))
638 if (is_device_path(name
)) {
639 r
= unit_name_from_path(name
, ".device", ret
);
646 if (path_is_absolute(name
)) {
647 r
= unit_name_from_path(name
, ".mount", ret
);
654 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
658 mangled
= do_escape_mangle(name
, flags
& UNIT_NAME_MANGLE_GLOB
, s
);
660 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
661 "Invalid unit name \"%s\" was escaped as \"%s\" (maybe you should use systemd-escape?)",
664 /* 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
666 if ((!(flags
& UNIT_NAME_MANGLE_GLOB
) || !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
681 int slice_build_parent_slice(const char *slice
, char **ret
) {
688 if (!slice_name_is_valid(slice
))
691 if (streq(slice
, SPECIAL_ROOT_SLICE
)) {
700 dash
= strrchr(s
, '-');
702 strcpy(dash
, ".slice");
704 r
= free_and_strdup(&s
, SPECIAL_ROOT_SLICE
);
715 int slice_build_subslice(const char *slice
, const char*name
, char **ret
) {
722 if (!slice_name_is_valid(slice
))
725 if (!unit_prefix_is_valid(name
))
728 if (streq(slice
, SPECIAL_ROOT_SLICE
))
729 subslice
= strappend(name
, ".slice");
733 assert_se(e
= endswith(slice
, ".slice"));
735 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
739 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
746 bool slice_name_is_valid(const char *name
) {
750 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
753 if (streq(name
, SPECIAL_ROOT_SLICE
))
756 e
= endswith(name
, ".slice");
760 for (p
= name
; p
< e
; p
++) {
764 /* Don't allow initial dash */
768 /* Don't allow multiple dashes */
777 /* Don't allow trailing hash */