Debian Codegraph @

src/basic/unit-name.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-id128.h"
4
5 #include "alloc-util.h"
6 #include "glob-util.h"
7 #include "hexdecoct.h"
8 #include "log.h"
9 #include "memory-util.h"
10 #include "path-util.h"
11 #include "sparse-endian.h"
12 #include "special.h"
13 #include "siphash24.h"
14 #include "string-util.h"
15 #include "unit-def.h"
16 #include "unit-name.h"
17
18 /* Characters valid in a unit name. */
19 #define VALID_CHARS \
20 ALPHANUMERICAL \
21 ":-_.\\"
22
23 /* The same, but also permits the single @ character that may appear */
24 #define VALID_CHARS_WITH_AT \
25 "@" \
26 VALID_CHARS
27
28 /* All chars valid in a unit name glob */
29 #define VALID_CHARS_GLOB \
30 VALID_CHARS_WITH_AT \
31 "[]!-*?"
32
33 #define LONG_UNIT_NAME_HASH_KEY SD_ID128_MAKE(ec,f2,37,fb,58,32,4a,32,84,9f,06,9b,0d,21,eb,9a)
34 #define UNIT_NAME_HASH_LENGTH_CHARS 16
35
36 bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
37 const char *e, *i, *at;
38
39 assert((flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0);
40
41 if (_unlikely_(flags == 0))
42 return false;
43
44 if (isempty(n))
45 return false;
46
47 if (strlen(n) >= UNIT_NAME_MAX)
48 return false;
49
50 e = strrchr(n, '.');
51 if (!e || e == n)
52 return false;
53
54 if (unit_type_from_string(e + 1) < 0)
55 return false;
56
57 for (i = n, at = NULL; i < e; i++) {
58
59 if (*i == '@' && !at)
60 at = i;
61
62 if (!strchr(VALID_CHARS_WITH_AT, *i))
63 return false;
64 }
65
66 if (at == n)
67 return false;
68
69 if (flags & UNIT_NAME_PLAIN)
70 if (!at)
71 return true;
72
73 if (flags & UNIT_NAME_INSTANCE)
74 if (at && e > at + 1)
75 return true;
76
77 if (flags & UNIT_NAME_TEMPLATE)
78 if (at && e == at + 1)
79 return true;
80
81 return false;
82 }
83
84 bool unit_prefix_is_valid(const char *p) {
85
86 /* We don't allow additional @ in the prefix string */
87
88 if (isempty(p))
89 return false;
90
91 return in_charset(p, VALID_CHARS);
92 }
93
94 bool unit_instance_is_valid(const char *i) {
95
96 /* The max length depends on the length of the string, so we
97 * don't really check this here. */
98
99 if (isempty(i))
100 return false;
101
102 /* We allow additional @ in the instance string, we do not
103 * allow them in the prefix! */
104
105 return in_charset(i, "@" VALID_CHARS);
106 }
107
108 bool unit_suffix_is_valid(const char *s) {
109 if (isempty(s))
110 return false;
111
112 if (s[0] != '.')
113 return false;
114
115 if (unit_type_from_string(s + 1) < 0)
116 return false;
117
118 return true;
119 }
120
121 int unit_name_to_prefix(const char *n, char **ret) {
122 const char *p;
123 char *s;
124
125 assert(n);
126 assert(ret);
127
128 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
129 return -EINVAL;
130
131 p = strchr(n, '@');
132 if (!p)
133 p = strrchr(n, '.');
134
135 assert_se(p);
136
137 s = strndup(n, p - n);
138 if (!s)
139 return -ENOMEM;
140
141 *ret = s;
142 return 0;
143 }
144
145 UnitNameFlags unit_name_to_instance(const char *n, char **ret) {
146 const char *p, *d;
147
148 assert(n);
149
150 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
151 return -EINVAL;
152
153 /* Everything past the first @ and before the last . is the instance */
154 p = strchr(n, '@');
155 if (!p) {
156 if (ret)
157 *ret = NULL;
158 return UNIT_NAME_PLAIN;
159 }
160
161 p++;
162
163 d = strrchr(p, '.');
164 if (!d)
165 return -EINVAL;
166
167 if (ret) {
168 char *i = strndup(p, d-p);
169 if (!i)
170 return -ENOMEM;
171
172 *ret = i;
173 }
174 return d > p ? UNIT_NAME_INSTANCE : UNIT_NAME_TEMPLATE;
175 }
176
177 int unit_name_to_prefix_and_instance(const char *n, char **ret) {
178 const char *d;
179 char *s;
180
181 assert(n);
182 assert(ret);
183
184 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
185 return -EINVAL;
186
187 d = strrchr(n, '.');
188 if (!d)
189 return -EINVAL;
190
191 s = strndup(n, d - n);
192 if (!s)
193 return -ENOMEM;
194
195 *ret = s;
196 return 0;
197 }
198
199 UnitType unit_name_to_type(const char *n) {
200 const char *e;
201
202 assert(n);
203
204 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
205 return _UNIT_TYPE_INVALID;
206
207 assert_se(e = strrchr(n, '.'));
208
209 return unit_type_from_string(e + 1);
210 }
211
212 int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
213 _cleanup_free_ char *s = NULL;
214 size_t a, b;
215 const char *e;
216
217 assert(n);
218 assert(suffix);
219 assert(ret);
220
221 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
222 return -EINVAL;
223
224 if (!unit_suffix_is_valid(suffix))
225 return -EINVAL;
226
227 assert_se(e = strrchr(n, '.'));
228
229 a = e - n;
230 b = strlen(suffix);
231
232 s = new(char, a + b + 1);
233 if (!s)
234 return -ENOMEM;
235
236 strcpy(mempcpy(s, n, a), suffix);
237
238 /* Make sure the name is still valid (i.e. didn't grow too large due to longer suffix) */
239 if (!unit_name_is_valid(s, UNIT_NAME_ANY))
240 return -EINVAL;
241
242 *ret = TAKE_PTR(s);
243 return 0;
244 }
245
246 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) {
247 UnitType type;
248
249 assert(prefix);
250 assert(suffix);
251 assert(ret);
252
253 if (suffix[0] != '.')
254 return -EINVAL;
255
256 type = unit_type_from_string(suffix + 1);
257 if (type < 0)
258 return type;
259
260 return unit_name_build_from_type(prefix, instance, type, ret);
261 }
262
263 int unit_name_build_from_type(const char *prefix, const char *instance, UnitType type, char **ret) {
264 _cleanup_free_ char *s = NULL;
265 const char *ut;
266
267 assert(prefix);
268 assert(type >= 0);
269 assert(type < _UNIT_TYPE_MAX);
270 assert(ret);
271
272 if (!unit_prefix_is_valid(prefix))
273 return -EINVAL;
274
275 ut = unit_type_to_string(type);
276
277 if (instance) {
278 if (!unit_instance_is_valid(instance))
279 return -EINVAL;
280
281 s = strjoin(prefix, "@", instance, ".", ut);
282 } else
283 s = strjoin(prefix, ".", ut);
284 if (!s)
285 return -ENOMEM;
286
287 /* Verify that this didn't grow too large (or otherwise is invalid) */
288 if (!unit_name_is_valid(s, instance ? UNIT_NAME_INSTANCE : UNIT_NAME_PLAIN))
289 return -EINVAL;
290
291 *ret = TAKE_PTR(s);
292 return 0;
293 }
294
295 static char *do_escape_char(char c, char *t) {
296 assert(t);
297
298 *(t++) = '\\';
299 *(t++) = 'x';
300 *(t++) = hexchar(c >> 4);
301 *(t++) = hexchar(c);
302
303 return t;
304 }
305
306 static char *do_escape(const char *f, char *t) {
307 assert(f);
308 assert(t);
309
310 /* do not create units with a leading '.', like for "/.dotdir" mount points */
311 if (*f == '.') {
312 t = do_escape_char(*f, t);
313 f++;
314 }
315
316 for (; *f; f++) {
317 if (*f == '/')
318 *(t++) = '-';
319 else if (IN_SET(*f, '-', '\\') || !strchr(VALID_CHARS, *f))
320 t = do_escape_char(*f, t);
321 else
322 *(t++) = *f;
323 }
324
325 return t;
326 }
327
328 char* unit_name_escape(const char *f) {
329 char *r, *t;
330
331 assert(f);
332
333 r = new(char, strlen(f)*4+1);
334 if (!r)
335 return NULL;
336
337 t = do_escape(f, r);
338 *t = 0;
339
340 return r;
341 }
342
343 int unit_name_unescape(const char *f, char **ret) {
344 _cleanup_free_ char *r = NULL;
345 char *t;
346
347 assert(f);
348 assert(ret);
349
350 r = strdup(f);
351 if (!r)
352 return -ENOMEM;
353
354 for (t = r; *f; f++) {
355 if (*f == '-')
356 *(t++) = '/';
357 else if (*f == '\\') {
358 int a, b;
359
360 if (f[1] != 'x')
361 return -EINVAL;
362
363 a = unhexchar(f[2]);
364 if (a < 0)
365 return -EINVAL;
366
367 b = unhexchar(f[3]);
368 if (b < 0)
369 return -EINVAL;
370
371 *(t++) = (char) (((uint8_t) a << 4U) | (uint8_t) b);
372 f += 3;
373 } else
374 *(t++) = *f;
375 }
376
377 *t = 0;
378
379 *ret = TAKE_PTR(r);
380
381 return 0;
382 }
383
384 int unit_name_path_escape(const char *f, char **ret) {
385 _cleanup_free_ char *p = NULL;
386 char *s;
387 int r;
388
389 assert(f);
390 assert(ret);
391
392 r = path_simplify_alloc(f, &p);
393 if (r < 0)
394 return r;
395
396 if (empty_or_root(p))
397 s = strdup("-");
398 else {
399 if (!path_is_normalized(p))
400 return -EINVAL;
401
402 /* Truncate trailing slashes and skip leading slashes */
403 delete_trailing_chars(p, "/");
404 s = unit_name_escape(skip_leading_chars(p, "/"));
405 }
406 if (!s)
407 return -ENOMEM;
408
409 *ret = s;
410 return 0;
411 }
412
413 int unit_name_path_unescape(const char *f, char **ret) {
414 _cleanup_free_ char *s = NULL;
415 int r;
416
417 assert(f);
418
419 if (isempty(f))
420 return -EINVAL;
421
422 if (streq(f, "-")) {
423 s = strdup("/");
424 if (!s)
425 return -ENOMEM;
426 } else {
427 _cleanup_free_ char *w = NULL;
428
429 r = unit_name_unescape(f, &w);
430 if (r < 0)
431 return r;
432
433 /* Don't accept trailing or leading slashes */
434 if (startswith(w, "/") || endswith(w, "/"))
435 return -EINVAL;
436
437 /* Prefix a slash again */
438 s = strjoin("/", w);
439 if (!s)
440 return -ENOMEM;
441
442 if (!path_is_normalized(s))
443 return -EINVAL;
444 }
445
446 if (ret)
447 *ret = TAKE_PTR(s);
448
449 return 0;
450 }
451
452 int unit_name_replace_instance_full(
453 const char *original,
454 const char *instance,
455 bool accept_glob,
456 char **ret) {
457
458 _cleanup_free_ char *s = NULL;
459 const char *prefix, *suffix;
460 size_t pl;
461
462 assert(original);
463 assert(instance);
464 assert(ret);
465
466 if (!unit_name_is_valid(original, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
467 return -EINVAL;
468 if (!unit_instance_is_valid(instance) && !(accept_glob && in_charset(instance, VALID_CHARS_GLOB)))
469 return -EINVAL;
470
471 prefix = ASSERT_PTR(strchr(original, '@'));
472 suffix = ASSERT_PTR(strrchr(original, '.'));
473 assert(prefix < suffix);
474
475 pl = prefix - original + 1; /* include '@' */
476
477 s = new(char, pl + strlen(instance) + strlen(suffix) + 1);
478 if (!s)
479 return -ENOMEM;
480
481 #if HAS_FEATURE_MEMORY_SANITIZER
482 /* MSan doesn't like stpncpy... See also https://github.com/google/sanitizers/issues/926 */
483 memzero(s, pl + strlen(instance) + strlen(suffix) + 1);
484 #endif
485
486 strcpy(stpcpy(stpncpy(s, original, pl), instance), suffix);
487
488 /* Make sure the resulting name still is valid, i.e. didn't grow too large. Globs will be expanded
489 * by clients when used, so the check is pointless. */
490 if (!accept_glob && !unit_name_is_valid(s, UNIT_NAME_INSTANCE))
491 return -EINVAL;
492
493 *ret = TAKE_PTR(s);
494 return 0;
495 }
496
497 int unit_name_template(const char *f, char **ret) {
498 const char *p, *e;
499 char *s;
500 size_t a;
501
502 assert(f);
503 assert(ret);
504
505 if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
506 return -EINVAL;
507
508 assert_se(p = strchr(f, '@'));
509 assert_se(e = strrchr(f, '.'));
510
511 a = p - f;
512
513 s = new(char, a + 1 + strlen(e) + 1);
514 if (!s)
515 return -ENOMEM;
516
517 strcpy(mempcpy(s, f, a + 1), e);
518
519 *ret = s;
520 return 0;
521 }
522
523 bool unit_name_is_hashed(const char *name) {
524 const char *s;
525
526 if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
527 return false;
528
529 assert_se(s = strrchr(name, '.'));
530
531 if (s - name < UNIT_NAME_HASH_LENGTH_CHARS + 1)
532 return false;
533
534 s -= UNIT_NAME_HASH_LENGTH_CHARS;
535 if (s[-1] != '_')
536 return false;
537
538 for (size_t i = 0; i < UNIT_NAME_HASH_LENGTH_CHARS; i++)
539 if (!strchr(LOWERCASE_HEXDIGITS, s[i]))
540 return false;
541
542 return true;
543 }
544
545 int unit_name_hash_long(const char *name, char **ret) {
546 _cleanup_free_ char *n = NULL, *hash = NULL;
547 const char *suffix;
548 le64_t h;
549 size_t len;
550
551 assert(ret);
552
553 if (strlen(name) < UNIT_NAME_MAX)
554 return -EMSGSIZE;
555
556 suffix = strrchr(name, '.');
557 if (!suffix)
558 return -EINVAL;
559
560 if (unit_type_from_string(suffix+1) < 0)
561 return -EINVAL;
562
563 h = htole64(siphash24_string(name, LONG_UNIT_NAME_HASH_KEY.bytes));
564
565 hash = hexmem(&h, sizeof(h));
566 if (!hash)
567 return -ENOMEM;
568
569 assert_se(strlen(hash) == UNIT_NAME_HASH_LENGTH_CHARS);
570
571 len = UNIT_NAME_MAX - 1 - strlen(suffix+1) - UNIT_NAME_HASH_LENGTH_CHARS - 2;
572 assert(len > 0 && len < UNIT_NAME_MAX);
573
574 n = strndup(name, len);
575 if (!n)
576 return -ENOMEM;
577
578 if (!strextend(&n, "_", hash, suffix))
579 return -ENOMEM;
580 assert_se(unit_name_is_valid(n, UNIT_NAME_PLAIN));
581
582 *ret = TAKE_PTR(n);
583
584 return 0;
585 }
586
587 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
588 _cleanup_free_ char *p = NULL, *s = NULL;
589 int r;
590
591 assert(path);
592 assert(suffix);
593 assert(ret);
594
595 if (!unit_suffix_is_valid(suffix))
596 return -EINVAL;
597
598 r = unit_name_path_escape(path, &p);
599 if (r < 0)
600 return r;
601
602 s = strjoin(p, suffix);
603 if (!s)
604 return -ENOMEM;
605
606 if (strlen(s) >= UNIT_NAME_MAX) {
607 _cleanup_free_ char *n = NULL;
608
609 log_debug("Unit name \"%s\" too long, falling back to hashed unit name.", s);
610
611 r = unit_name_hash_long(s, &n);
612 if (r < 0)
613 return r;
614
615 free_and_replace(s, n);
616 }
617
618 /* Refuse if this for some other reason didn't result in a valid name */
619 if (!unit_name_is_valid(s, UNIT_NAME_PLAIN))
620 return -EINVAL;
621
622 *ret = TAKE_PTR(s);
623 return 0;
624 }
625
626 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret) {
627 _cleanup_free_ char *p = NULL, *s = NULL;
628 int r;
629
630 assert(prefix);
631 assert(path);
632 assert(suffix);
633 assert(ret);
634
635 if (!unit_prefix_is_valid(prefix))
636 return -EINVAL;
637
638 if (!unit_suffix_is_valid(suffix))
639 return -EINVAL;
640
641 r = unit_name_path_escape(path, &p);
642 if (r < 0)
643 return r;
644
645 s = strjoin(prefix, "@", p, suffix);
646 if (!s)
647 return -ENOMEM;
648
649 if (strlen(s) >= UNIT_NAME_MAX) /* Return a slightly more descriptive error for this specific condition */
650 return -ENAMETOOLONG;
651
652 /* Refuse if this for some other reason didn't result in a valid name */
653 if (!unit_name_is_valid(s, UNIT_NAME_INSTANCE))
654 return -EINVAL;
655
656 *ret = TAKE_PTR(s);
657 return 0;
658 }
659
660 int unit_name_to_path(const char *name, char **ret) {
661 _cleanup_free_ char *prefix = NULL;
662 int r;
663
664 assert(name);
665
666 r = unit_name_to_prefix(name, &prefix);
667 if (r < 0)
668 return r;
669
670 if (unit_name_is_hashed(name))
671 return -ENAMETOOLONG;
672
673 return unit_name_path_unescape(prefix, ret);
674 }
675
676 static bool do_escape_mangle(const char *f, bool allow_globs, char *t) {
677 const char *valid_chars;
678 bool mangled = false;
679
680 assert(f);
681 assert(t);
682
683 /* We'll only escape the obvious characters here, to play safe.
684 *
685 * Returns true if any characters were mangled, false otherwise.
686 */
687
688 valid_chars = allow_globs ? VALID_CHARS_GLOB : VALID_CHARS_WITH_AT;
689
690 for (; *f; f++)
691 if (*f == '/') {
692 *(t++) = '-';
693 mangled = true;
694 } else if (!strchr(valid_chars, *f)) {
695 t = do_escape_char(*f, t);
696 mangled = true;
697 } else
698 *(t++) = *f;
699 *t = 0;
700
701 return mangled;
702 }
703
704 /**
705 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
706 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
707 * except that @suffix is appended if a valid unit suffix is not present.
708 *
709 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
710 */
711 int unit_name_mangle_with_suffix(
712 const char *name,
713 const char *operation,
714 UnitNameMangle flags,
715 const char *suffix,
716 char **ret) {
717
718 _cleanup_free_ char *s = NULL;
719 bool mangled, suggest_escape = true, warn = flags & UNIT_NAME_MANGLE_WARN;
720 int r;
721
722 assert(name);
723 assert(suffix);
724 assert(ret);
725
726 if (isempty(name)) /* We cannot mangle empty unit names to become valid, sorry. */
727 return -EINVAL;
728
729 if (!unit_suffix_is_valid(suffix))
730 return -EINVAL;
731
732 /* Already a fully valid unit name? If so, no mangling is necessary... */
733 if (unit_name_is_valid(name, UNIT_NAME_ANY))
734 goto good;
735
736 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
737 if (string_is_glob(name) && in_charset(name, VALID_CHARS_GLOB)) {
738 if (flags & UNIT_NAME_MANGLE_GLOB)
739 goto good;
740 log_full(warn ? LOG_NOTICE : LOG_DEBUG,
741 "Glob pattern passed%s%s, but globs are not supported for this.",
742 operation ? " " : "", strempty(operation));
743 suggest_escape = false;
744 }
745
746 if (path_is_absolute(name)) {
747 _cleanup_free_ char *n = NULL;
748
749 r = path_simplify_alloc(name, &n);
750 if (r < 0)
751 return r;
752
753 if (is_device_path(n)) {
754 r = unit_name_from_path(n, ".device", ret);
755 if (r >= 0)
756 return 1;
757 if (r != -EINVAL)
758 return r;
759 }
760
761 r = unit_name_from_path(n, ".mount", ret);
762 if (r >= 0)
763 return 1;
764 if (r != -EINVAL)
765 return r;
766 }
767
768 s = new(char, strlen(name) * 4 + strlen(suffix) + 1);
769 if (!s)
770 return -ENOMEM;
771
772 mangled = do_escape_mangle(name, flags & UNIT_NAME_MANGLE_GLOB, s);
773 if (mangled)
774 log_full(warn ? LOG_NOTICE : LOG_DEBUG,
775 "Invalid unit name \"%s\" escaped as \"%s\"%s.",
776 name, s,
777 suggest_escape ? " (maybe you should use systemd-escape?)" : "");
778
779 /* Append a suffix if it doesn't have any, but only if this is not a glob, so that we can allow
780 * "foo.*" as a valid glob. */
781 if ((!(flags & UNIT_NAME_MANGLE_GLOB) || !string_is_glob(s)) && unit_name_to_type(s) < 0)
782 strcat(s, suffix);
783
784 /* Make sure mangling didn't grow this too large (but don't do this check if globbing is allowed,
785 * since globs generally do not qualify as valid unit names) */
786 if (!FLAGS_SET(flags, UNIT_NAME_MANGLE_GLOB) && !unit_name_is_valid(s, UNIT_NAME_ANY))
787 return -EINVAL;
788
789 *ret = TAKE_PTR(s);
790 return 1;
791
792 good:
793 return strdup_to(ret, name);
794 }
795
796 int slice_build_parent_slice(const char *slice, char **ret) {
797 assert(slice);
798 assert(ret);
799
800 if (!slice_name_is_valid(slice))
801 return -EINVAL;
802
803 if (streq(slice, SPECIAL_ROOT_SLICE)) {
804 *ret = NULL;
805 return 0;
806 }
807
808 _cleanup_free_ char *s = strdup(slice);
809 if (!s)
810 return -ENOMEM;
811
812 char *dash = strrchr(s, '-');
813 if (!dash)
814 return strdup_to_full(ret, SPECIAL_ROOT_SLICE);
815
816 /* We know that s ended with .slice before truncation, so we have enough space. */
817 strcpy(dash, ".slice");
818
819 *ret = TAKE_PTR(s);
820 return 1;
821 }
822
823 int slice_build_subslice(const char *slice, const char *name, char **ret) {
824 char *subslice;
825
826 assert(slice);
827 assert(name);
828 assert(ret);
829
830 if (!slice_name_is_valid(slice))
831 return -EINVAL;
832
833 if (!unit_prefix_is_valid(name))
834 return -EINVAL;
835
836 if (streq(slice, SPECIAL_ROOT_SLICE))
837 subslice = strjoin(name, ".slice");
838 else {
839 const char *e;
840
841 assert_se(e = endswith(slice, ".slice"));
842
843 subslice = new(char, (e - slice) + 1 + strlen(name) + 6 + 1);
844 if (!subslice)
845 return -ENOMEM;
846
847 stpcpy(stpcpy(stpcpy(mempcpy(subslice, slice, e - slice), "-"), name), ".slice");
848 }
849
850 *ret = subslice;
851 return 0;
852 }
853
854 bool slice_name_is_valid(const char *name) {
855 const char *p, *e;
856 bool dash = false;
857
858 if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
859 return false;
860
861 if (streq(name, SPECIAL_ROOT_SLICE))
862 return true;
863
864 e = endswith(name, ".slice");
865 if (!e)
866 return false;
867
868 for (p = name; p < e; p++) {
869
870 if (*p == '-') {
871
872 /* Don't allow initial dash */
873 if (p == name)
874 return false;
875
876 /* Don't allow multiple dashes */
877 if (dash)
878 return false;
879
880 dash = true;
881 } else
882 dash = false;
883 }
884
885 /* Don't allow trailing hash */
886 if (dash)
887 return false;
888
889 return true;
890 }
891
892 bool unit_name_prefix_equal(const char *a, const char *b) {
893 const char *p, *q;
894
895 assert(a);
896 assert(b);
897
898 if (!unit_name_is_valid(a, UNIT_NAME_ANY) || !unit_name_is_valid(b, UNIT_NAME_ANY))
899 return false;
900
901 p = strchr(a, '@');
902 if (!p)
903 p = strrchr(a, '.');
904
905 q = strchr(b, '@');
906 if (!q)
907 q = strrchr(b, '.');
908
909 assert(p);
910 assert(q);
911
912 return memcmp_nn(a, p - a, b, q - b) == 0;
913 }