]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/unit-name.c
43d8b3477e17ed4ece831387afdf4aa63ac7cf24
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
10 #include "hexdecoct.h"
11 #include "path-util.h"
13 #include "string-util.h"
15 #include "unit-name.h"
17 /* Characters valid in a unit name. */
23 /* The same, but also permits the single @ character that may appear */
24 #define VALID_CHARS_WITH_AT \
28 /* All chars valid in a unit name glob */
29 #define VALID_CHARS_GLOB \
33 bool unit_name_is_valid(const char *n
, UnitNameFlags flags
) {
34 const char *e
, *i
, *at
;
36 assert((flags
& ~(UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
)) == 0);
38 if (_unlikely_(flags
== 0))
44 if (strlen(n
) >= UNIT_NAME_MAX
)
51 if (unit_type_from_string(e
+ 1) < 0)
54 for (i
= n
, at
= NULL
; i
< e
; i
++) {
59 if (!strchr("@" VALID_CHARS
, *i
))
66 if (flags
& UNIT_NAME_PLAIN
)
70 if (flags
& UNIT_NAME_INSTANCE
)
74 if (flags
& UNIT_NAME_TEMPLATE
)
75 if (at
&& e
== at
+ 1)
81 bool unit_prefix_is_valid(const char *p
) {
83 /* We don't allow additional @ in the prefix string */
88 return in_charset(p
, VALID_CHARS
);
91 bool unit_instance_is_valid(const char *i
) {
93 /* The max length depends on the length of the string, so we
94 * don't really check this here. */
99 /* We allow additional @ in the instance string, we do not
100 * allow them in the prefix! */
102 return in_charset(i
, "@" VALID_CHARS
);
105 bool unit_suffix_is_valid(const char *s
) {
112 if (unit_type_from_string(s
+ 1) < 0)
118 int unit_name_to_prefix(const char *n
, char **ret
) {
125 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
134 s
= strndup(n
, p
- n
);
142 int unit_name_to_instance(const char *n
, char **ret
) {
147 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
150 /* Everything past the first @ and before the last . is the instance */
155 return UNIT_NAME_PLAIN
;
165 char *i
= strndup(p
, d
-p
);
171 return d
> p
? UNIT_NAME_INSTANCE
: UNIT_NAME_TEMPLATE
;
174 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
181 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
188 s
= strndup(n
, d
- n
);
196 UnitType
unit_name_to_type(const char *n
) {
201 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
202 return _UNIT_TYPE_INVALID
;
204 assert_se(e
= strrchr(n
, '.'));
206 return unit_type_from_string(e
+ 1);
209 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
210 _cleanup_free_
char *s
= NULL
;
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
);
235 /* Make sure the name is still valid (i.e. didn't grow too large due to longer suffix) */
236 if (!unit_name_is_valid(s
, UNIT_NAME_ANY
))
243 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
250 if (suffix
[0] != '.')
253 type
= unit_type_from_string(suffix
+ 1);
257 return unit_name_build_from_type(prefix
, instance
, type
, ret
);
260 int unit_name_build_from_type(const char *prefix
, const char *instance
, UnitType type
, char **ret
) {
261 _cleanup_free_
char *s
= NULL
;
266 assert(type
< _UNIT_TYPE_MAX
);
269 if (!unit_prefix_is_valid(prefix
))
272 ut
= unit_type_to_string(type
);
275 if (!unit_instance_is_valid(instance
))
278 s
= strjoin(prefix
, "@", instance
, ".", ut
);
280 s
= strjoin(prefix
, ".", ut
);
284 /* Verify that this didn't grow too large (or otherwise is invalid) */
285 if (!unit_name_is_valid(s
, instance
? UNIT_NAME_INSTANCE
: UNIT_NAME_PLAIN
))
292 static char *do_escape_char(char c
, char *t
) {
297 *(t
++) = hexchar(c
>> 4);
303 static char *do_escape(const char *f
, char *t
) {
307 /* do not create units with a leading '.', like for "/.dotdir" mount points */
309 t
= do_escape_char(*f
, t
);
316 else if (IN_SET(*f
, '-', '\\') || !strchr(VALID_CHARS
, *f
))
317 t
= do_escape_char(*f
, t
);
325 char *unit_name_escape(const char *f
) {
330 r
= new(char, strlen(f
)*4+1);
340 int unit_name_unescape(const char *f
, char **ret
) {
341 _cleanup_free_
char *r
= NULL
;
350 for (t
= r
; *f
; f
++) {
353 else if (*f
== '\\') {
367 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
380 int unit_name_path_escape(const char *f
, char **ret
) {
390 path_simplify(p
, false);
392 if (empty_or_root(p
))
395 if (!path_is_normalized(p
))
398 /* Truncate trailing slashes */
399 delete_trailing_chars(p
, "/");
401 /* Truncate leading slashes */
402 p
= skip_leading_chars(p
, "/");
404 s
= unit_name_escape(p
);
413 int unit_name_path_unescape(const char *f
, char **ret
) {
414 _cleanup_free_
char *s
= NULL
;
427 _cleanup_free_
char *w
= NULL
;
429 r
= unit_name_unescape(f
, &w
);
433 /* Don't accept trailing or leading slashes */
434 if (startswith(w
, "/") || endswith(w
, "/"))
437 /* Prefix a slash again */
442 if (!path_is_normalized(s
))
452 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
453 _cleanup_free_
char *s
= NULL
;
461 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
463 if (!unit_instance_is_valid(i
))
466 assert_se(p
= strchr(f
, '@'));
467 assert_se(e
= strrchr(f
, '.'));
472 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
476 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
478 /* Make sure the resulting name still is valid, i.e. didn't grow too large */
479 if (!unit_name_is_valid(s
, UNIT_NAME_INSTANCE
))
486 int unit_name_template(const char *f
, char **ret
) {
494 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
497 assert_se(p
= strchr(f
, '@'));
498 assert_se(e
= strrchr(f
, '.'));
502 s
= new(char, a
+ 1 + strlen(e
) + 1);
506 strcpy(mempcpy(s
, f
, a
+ 1), e
);
512 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
513 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
520 if (!unit_suffix_is_valid(suffix
))
523 r
= unit_name_path_escape(path
, &p
);
527 s
= strjoin(p
, suffix
);
531 /* Refuse this if this got too long or for some other reason didn't result in a valid name */
532 if (!unit_name_is_valid(s
, UNIT_NAME_PLAIN
))
539 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
540 _cleanup_free_
char *p
= NULL
, *s
= NULL
;
548 if (!unit_prefix_is_valid(prefix
))
551 if (!unit_suffix_is_valid(suffix
))
554 r
= unit_name_path_escape(path
, &p
);
558 s
= strjoin(prefix
, "@", p
, suffix
);
562 /* Refuse this if this got too long or for some other reason didn't result in a valid name */
563 if (!unit_name_is_valid(s
, UNIT_NAME_INSTANCE
))
570 int unit_name_to_path(const char *name
, char **ret
) {
571 _cleanup_free_
char *prefix
= NULL
;
576 r
= unit_name_to_prefix(name
, &prefix
);
580 return unit_name_path_unescape(prefix
, ret
);
583 static bool do_escape_mangle(const char *f
, bool allow_globs
, char *t
) {
584 const char *valid_chars
;
585 bool mangled
= false;
590 /* We'll only escape the obvious characters here, to play safe.
592 * Returns true if any characters were mangled, false otherwise.
595 valid_chars
= allow_globs
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
601 } else if (!strchr(valid_chars
, *f
)) {
602 t
= do_escape_char(*f
, t
);
612 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
613 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
614 * except that @suffix is appended if a valid unit suffix is not present.
616 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
618 int unit_name_mangle_with_suffix(const char *name
, const char *operation
, UnitNameMangle flags
, const char *suffix
, char **ret
) {
619 _cleanup_free_
char *s
= NULL
;
620 bool mangled
, suggest_escape
= true;
627 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
630 if (!unit_suffix_is_valid(suffix
))
633 /* Already a fully valid unit name? If so, no mangling is necessary... */
634 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
637 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
638 if (string_is_glob(name
) && in_charset(name
, VALID_CHARS_GLOB
)) {
639 if (flags
& UNIT_NAME_MANGLE_GLOB
)
641 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
642 "Glob pattern passed%s%s, but globs are not supported for this.",
643 operation
? " " : "", strempty(operation
));
644 suggest_escape
= false;
647 if (is_device_path(name
)) {
648 r
= unit_name_from_path(name
, ".device", ret
);
655 if (path_is_absolute(name
)) {
656 r
= unit_name_from_path(name
, ".mount", ret
);
663 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
667 mangled
= do_escape_mangle(name
, flags
& UNIT_NAME_MANGLE_GLOB
, s
);
669 log_full(flags
& UNIT_NAME_MANGLE_WARN
? LOG_NOTICE
: LOG_DEBUG
,
670 "Invalid unit name \"%s\" escaped as \"%s\"%s.",
672 suggest_escape
? " (maybe you should use systemd-escape?)" : "");
674 /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow
675 * "foo.*" as a valid glob. */
676 if ((!(flags
& UNIT_NAME_MANGLE_GLOB
) || !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
679 /* Make sure mangling didn't grow this too large (but don't do this check if globbing is allowed,
680 * since globs generally do not qualify as valid unit names) */
681 if (!FLAGS_SET(flags
, UNIT_NAME_MANGLE_GLOB
) && !unit_name_is_valid(s
, UNIT_NAME_ANY
))
696 int slice_build_parent_slice(const char *slice
, char **ret
) {
697 _cleanup_free_
char *s
= NULL
;
704 if (!slice_name_is_valid(slice
))
707 if (streq(slice
, SPECIAL_ROOT_SLICE
)) {
716 dash
= strrchr(s
, '-');
718 strcpy(dash
, ".slice");
720 r
= free_and_strdup(&s
, SPECIAL_ROOT_SLICE
);
729 int slice_build_subslice(const char *slice
, const char *name
, char **ret
) {
736 if (!slice_name_is_valid(slice
))
739 if (!unit_prefix_is_valid(name
))
742 if (streq(slice
, SPECIAL_ROOT_SLICE
))
743 subslice
= strjoin(name
, ".slice");
747 assert_se(e
= endswith(slice
, ".slice"));
749 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
753 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
760 bool slice_name_is_valid(const char *name
) {
764 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
767 if (streq(name
, SPECIAL_ROOT_SLICE
))
770 e
= endswith(name
, ".slice");
774 for (p
= name
; p
< e
; p
++) {
778 /* Don't allow initial dash */
782 /* Don't allow multiple dashes */
791 /* Don't allow trailing hash */