2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
27 #include "glob-util.h"
28 #include "hexdecoct.h"
29 #include "path-util.h"
30 #include "string-util.h"
32 #include "unit-name.h"
34 /* Characters valid in a unit name. */
40 /* The same, but also permits the single @ character that may appear */
41 #define VALID_CHARS_WITH_AT \
45 /* All chars valid in a unit name glob */
46 #define VALID_CHARS_GLOB \
50 bool unit_name_is_valid(const char *n
, UnitNameFlags flags
) {
51 const char *e
, *i
, *at
;
53 assert((flags
& ~(UNIT_NAME_PLAIN
|UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
)) == 0);
55 if (_unlikely_(flags
== 0))
61 if (strlen(n
) >= UNIT_NAME_MAX
)
68 if (unit_type_from_string(e
+ 1) < 0)
71 for (i
= n
, at
= NULL
; i
< e
; i
++) {
76 if (!strchr("@" VALID_CHARS
, *i
))
83 if (flags
& UNIT_NAME_PLAIN
)
87 if (flags
& UNIT_NAME_INSTANCE
)
91 if (flags
& UNIT_NAME_TEMPLATE
)
92 if (at
&& e
== at
+ 1)
98 bool unit_prefix_is_valid(const char *p
) {
100 /* We don't allow additional @ in the prefix string */
105 return in_charset(p
, VALID_CHARS
);
108 bool unit_instance_is_valid(const char *i
) {
110 /* The max length depends on the length of the string, so we
111 * don't really check this here. */
116 /* We allow additional @ in the instance string, we do not
117 * allow them in the prefix! */
119 return in_charset(i
, "@" VALID_CHARS
);
122 bool unit_suffix_is_valid(const char *s
) {
129 if (unit_type_from_string(s
+ 1) < 0)
135 int unit_name_to_prefix(const char *n
, char **ret
) {
142 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
151 s
= strndup(n
, p
- n
);
159 int unit_name_to_instance(const char *n
, char **instance
) {
166 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
169 /* Everything past the first @ and before the last . is the instance */
190 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
197 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
204 s
= strndup(n
, d
- n
);
212 UnitType
unit_name_to_type(const char *n
) {
217 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
218 return _UNIT_TYPE_INVALID
;
220 assert_se(e
= strrchr(n
, '.'));
222 return unit_type_from_string(e
+ 1);
225 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
233 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
236 if (!unit_suffix_is_valid(suffix
))
239 assert_se(e
= strrchr(n
, '.'));
244 s
= new(char, a
+ b
+ 1);
248 strcpy(mempcpy(s
, n
, a
), suffix
);
254 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
261 if (!unit_prefix_is_valid(prefix
))
264 if (instance
&& !unit_instance_is_valid(instance
))
267 if (!unit_suffix_is_valid(suffix
))
271 s
= strappend(prefix
, suffix
);
273 s
= strjoin(prefix
, "@", instance
, suffix
);
281 static char *do_escape_char(char c
, char *t
) {
286 *(t
++) = hexchar(c
>> 4);
292 static char *do_escape(const char *f
, char *t
) {
296 /* do not create units with a leading '.', like for "/.dotdir" mount points */
298 t
= do_escape_char(*f
, t
);
305 else if (IN_SET(*f
, '-', '\\') || !strchr(VALID_CHARS
, *f
))
306 t
= do_escape_char(*f
, t
);
314 char *unit_name_escape(const char *f
) {
319 r
= new(char, strlen(f
)*4+1);
329 int unit_name_unescape(const char *f
, char **ret
) {
330 _cleanup_free_
char *r
= NULL
;
339 for (t
= r
; *f
; f
++) {
342 else if (*f
== '\\') {
356 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
370 int unit_name_path_escape(const char *f
, char **ret
) {
380 path_kill_slashes(p
);
382 if (STR_IN_SET(p
, "/", ""))
387 if (!path_is_safe(p
))
390 /* Truncate trailing slashes */
391 e
= endswith(p
, "/");
395 /* Truncate leading slashes */
399 s
= unit_name_escape(p
);
408 int unit_name_path_unescape(const char *f
, char **ret
) {
424 r
= unit_name_unescape(f
, &w
);
428 /* Don't accept trailing or leading slashes */
429 if (startswith(w
, "/") || endswith(w
, "/")) {
434 /* Prefix a slash again */
435 s
= strappend("/", w
);
440 if (!path_is_safe(s
)) {
454 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
463 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
465 if (!unit_instance_is_valid(i
))
468 assert_se(p
= strchr(f
, '@'));
469 assert_se(e
= strrchr(f
, '.'));
474 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
478 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
484 int unit_name_template(const char *f
, char **ret
) {
492 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
495 assert_se(p
= strchr(f
, '@'));
496 assert_se(e
= strrchr(f
, '.'));
500 s
= new(char, a
+ 1 + strlen(e
) + 1);
504 strcpy(mempcpy(s
, f
, a
+ 1), e
);
510 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
511 _cleanup_free_
char *p
= NULL
;
519 if (!unit_suffix_is_valid(suffix
))
522 r
= unit_name_path_escape(path
, &p
);
526 s
= strappend(p
, suffix
);
534 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
535 _cleanup_free_
char *p
= NULL
;
544 if (!unit_prefix_is_valid(prefix
))
547 if (!unit_suffix_is_valid(suffix
))
550 r
= unit_name_path_escape(path
, &p
);
554 s
= strjoin(prefix
, "@", p
, suffix
);
562 int unit_name_to_path(const char *name
, char **ret
) {
563 _cleanup_free_
char *prefix
= NULL
;
568 r
= unit_name_to_prefix(name
, &prefix
);
572 return unit_name_path_unescape(prefix
, ret
);
575 static char *do_escape_mangle(const char *f
, UnitNameMangle allow_globs
, char *t
) {
576 const char *valid_chars
;
579 assert(IN_SET(allow_globs
, UNIT_NAME_GLOB
, UNIT_NAME_NOGLOB
));
582 /* We'll only escape the obvious characters here, to play
585 valid_chars
= allow_globs
== UNIT_NAME_GLOB
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
590 else if (!strchr(valid_chars
, *f
))
591 t
= do_escape_char(*f
, t
);
600 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
601 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
602 * except that @suffix is appended if a valid unit suffix is not present.
604 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
606 int unit_name_mangle_with_suffix(const char *name
, UnitNameMangle allow_globs
, const char *suffix
, char **ret
) {
614 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
617 if (!unit_suffix_is_valid(suffix
))
620 /* Already a fully valid unit name? If so, no mangling is necessary... */
621 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
624 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
625 if (allow_globs
== UNIT_NAME_GLOB
&&
626 string_is_glob(name
) &&
627 in_charset(name
, VALID_CHARS_GLOB
))
630 if (is_device_path(name
)) {
631 r
= unit_name_from_path(name
, ".device", ret
);
638 if (path_is_absolute(name
)) {
639 r
= unit_name_from_path(name
, ".mount", ret
);
646 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
650 t
= do_escape_mangle(name
, allow_globs
, s
);
653 /* 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
655 if ((allow_globs
!= UNIT_NAME_GLOB
|| !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
670 int slice_build_parent_slice(const char *slice
, char **ret
) {
677 if (!slice_name_is_valid(slice
))
680 if (streq(slice
, "-.slice")) {
689 dash
= strrchr(s
, '-');
691 strcpy(dash
, ".slice");
693 r
= free_and_strdup(&s
, "-.slice");
704 int slice_build_subslice(const char *slice
, const char*name
, char **ret
) {
711 if (!slice_name_is_valid(slice
))
714 if (!unit_prefix_is_valid(name
))
717 if (streq(slice
, "-.slice"))
718 subslice
= strappend(name
, ".slice");
722 assert_se(e
= endswith(slice
, ".slice"));
724 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
728 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
735 bool slice_name_is_valid(const char *name
) {
739 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
742 if (streq(name
, "-.slice"))
745 e
= endswith(name
, ".slice");
749 for (p
= name
; p
< e
; p
++) {
753 /* Don't allow initial dash */
757 /* Don't allow multiple dashes */
766 /* Don't allow trailing hash */