1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "path-util.h"
26 #include "bus-label.h"
28 #include "unit-name.h"
36 bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
37 const char *e, *i, *at;
39 assert((flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0);
41 if (_unlikely_(flags == 0))
47 if (strlen(n) >= UNIT_NAME_MAX)
54 if (unit_type_from_string(e + 1) < 0)
57 for (i = n, at = NULL; i < e; i++) {
62 if (!strchr("@" VALID_CHARS, *i))
69 if (flags & UNIT_NAME_PLAIN)
73 if (flags & UNIT_NAME_INSTANCE)
77 if (flags & UNIT_NAME_TEMPLATE)
78 if (at && e == at + 1)
84 bool unit_prefix_is_valid(const char *p) {
86 /* We don't allow additional @ in the prefix string */
91 return in_charset(p, VALID_CHARS);
94 bool unit_instance_is_valid(const char *i) {
96 /* The max length depends on the length of the string, so we
97 * don't really check this here. */
102 /* We allow additional @ in the instance string, we do not
103 * allow them in the prefix! */
105 return in_charset(i, "@" VALID_CHARS);
108 bool unit_suffix_is_valid(const char *s) {
115 if (unit_type_from_string(s + 1) < 0)
121 int unit_name_to_prefix(const char *n, char **ret) {
128 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
137 s = strndup(n, p - n);
145 /// UNNEEDED by elogind
147 int unit_name_to_instance(const char *n, char **instance) {
154 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
157 /* Everything past the first @ and before the last . is the instance */
178 int unit_name_to_prefix_and_instance(const char *n, char **ret) {
185 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
192 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 /// UNNEEDED by elogind
216 int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
224 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
227 if (!unit_suffix_is_valid(suffix))
230 assert_se(e = strrchr(n, '.'));
235 s = new(char, a + b + 1);
239 strcpy(mempcpy(s, n, a), suffix);
246 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) {
253 if (!unit_prefix_is_valid(prefix))
256 if (instance && !unit_instance_is_valid(instance))
259 if (!unit_suffix_is_valid(suffix))
263 s = strappend(prefix, suffix);
265 s = strjoin(prefix, "@", instance, suffix, NULL);
273 static char *do_escape_char(char c, char *t) {
278 *(t++) = hexchar(c >> 4);
284 static char *do_escape(const char *f, char *t) {
288 /* do not create units with a leading '.', like for "/.dotdir" mount points */
290 t = do_escape_char(*f, t);
297 else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f))
298 t = do_escape_char(*f, t);
306 char *unit_name_escape(const char *f) {
311 r = new(char, strlen(f)*4+1);
321 int unit_name_unescape(const char *f, char **ret) {
322 _cleanup_free_ char *r = NULL;
331 for (t = r; *f; f++) {
334 else if (*f == '\\') {
348 *(t++) = (char) (((uint8_t) a << 4U) | (uint8_t) b);
362 int unit_name_path_escape(const char *f, char **ret) {
372 path_kill_slashes(p);
374 if (STR_IN_SET(p, "/", ""))
379 if (!path_is_safe(p))
382 /* Truncate trailing slashes */
383 e = endswith(p, "/");
387 /* Truncate leading slashes */
391 s = unit_name_escape(p);
400 int unit_name_path_unescape(const char *f, char **ret) {
416 r = unit_name_unescape(f, &w);
420 /* Don't accept trailing or leading slashes */
421 if (startswith(w, "/") || endswith(w, "/")) {
426 /* Prefix a slash again */
427 s = strappend("/", w);
432 if (!path_is_safe(s)) {
446 /// UNNEEDED by elogind
448 int unit_name_replace_instance(const char *f, const char *i, char **ret) {
457 if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
459 if (!unit_instance_is_valid(i))
462 assert_se(p = strchr(f, '@'));
463 assert_se(e = strrchr(f, '.'));
468 s = new(char, a + 1 + b + strlen(e) + 1);
472 strcpy(mempcpy(mempcpy(s, f, a + 1), i, b), e);
478 int unit_name_template(const char *f, char **ret) {
486 if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
489 assert_se(p = strchr(f, '@'));
490 assert_se(e = strrchr(f, '.'));
494 s = new(char, a + 1 + strlen(e) + 1);
498 strcpy(mempcpy(s, f, a + 1), e);
505 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
506 _cleanup_free_ char *p = NULL;
514 if (!unit_suffix_is_valid(suffix))
517 r = unit_name_path_escape(path, &p);
521 s = strappend(p, suffix);
529 /// UNNEEDED by elogind
531 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret) {
532 _cleanup_free_ char *p = NULL;
541 if (!unit_prefix_is_valid(prefix))
544 if (!unit_suffix_is_valid(suffix))
547 r = unit_name_path_escape(path, &p);
551 s = strjoin(prefix, "@", p, suffix, NULL);
559 int unit_name_to_path(const char *name, char **ret) {
560 _cleanup_free_ char *prefix = NULL;
565 r = unit_name_to_prefix(name, &prefix);
569 return unit_name_path_unescape(prefix, ret);
573 char *unit_dbus_path_from_name(const char *name) {
574 _cleanup_free_ char *e = NULL;
578 e = bus_label_escape(name);
582 return strappend("/org/freedesktop/systemd1/unit/", e);
585 int unit_name_from_dbus_path(const char *path, char **name) {
589 e = startswith(path, "/org/freedesktop/systemd1/unit/");
593 n = bus_label_unescape(e);
601 const char* unit_dbus_interface_from_type(UnitType t) {
603 static const char *const table[_UNIT_TYPE_MAX] = {
604 [UNIT_SERVICE] = "org.freedesktop.systemd1.Service",
605 [UNIT_SOCKET] = "org.freedesktop.systemd1.Socket",
606 [UNIT_BUSNAME] = "org.freedesktop.systemd1.BusName",
607 [UNIT_TARGET] = "org.freedesktop.systemd1.Target",
608 [UNIT_SNAPSHOT] = "org.freedesktop.systemd1.Snapshot",
609 [UNIT_DEVICE] = "org.freedesktop.systemd1.Device",
610 [UNIT_MOUNT] = "org.freedesktop.systemd1.Mount",
611 [UNIT_AUTOMOUNT] = "org.freedesktop.systemd1.Automount",
612 [UNIT_SWAP] = "org.freedesktop.systemd1.Swap",
613 [UNIT_TIMER] = "org.freedesktop.systemd1.Timer",
614 [UNIT_PATH] = "org.freedesktop.systemd1.Path",
615 [UNIT_SLICE] = "org.freedesktop.systemd1.Slice",
616 [UNIT_SCOPE] = "org.freedesktop.systemd1.Scope",
621 if (t >= _UNIT_TYPE_MAX)
627 /// UNNEEDED by elogind
629 const char *unit_dbus_interface_from_name(const char *name) {
632 t = unit_name_to_type(name);
636 return unit_dbus_interface_from_type(t);
640 static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t) {
641 const char *valid_chars;
644 assert(IN_SET(allow_globs, UNIT_NAME_GLOB, UNIT_NAME_NOGLOB));
647 /* We'll only escape the obvious characters here, to play
650 valid_chars = allow_globs == UNIT_NAME_GLOB ? "@" VALID_CHARS "[]!-*?" : "@" VALID_CHARS;
655 else if (!strchr(valid_chars, *f))
656 t = do_escape_char(*f, t);
665 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
666 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
667 * except that @suffix is appended if a valid unit suffix is not present.
669 * If @allow_globs, globs characters are preserved. Otherwise they are escaped.
671 int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret) {
679 if (isempty(name)) /* We cannot mangle empty unit names to become valid, sorry. */
682 if (!unit_suffix_is_valid(suffix))
685 if (unit_name_is_valid(name, UNIT_NAME_ANY)) {
686 /* No mangling necessary... */
695 if (is_device_path(name)) {
696 r = unit_name_from_path(name, ".device", ret);
703 if (path_is_absolute(name)) {
704 r = unit_name_from_path(name, ".mount", ret);
711 s = new(char, strlen(name) * 4 + strlen(suffix) + 1);
715 t = do_escape_mangle(name, allow_globs, s);
718 if (unit_name_to_type(s) < 0)
725 /// UNNEEDED by elogind
727 int slice_build_parent_slice(const char *slice, char **ret) {
734 if (!slice_name_is_valid(slice))
737 if (streq(slice, "-.slice")) {
746 dash = strrchr(s, '-');
748 strcpy(dash, ".slice");
750 r = free_and_strdup(&s, "-.slice");
762 int slice_build_subslice(const char *slice, const char*name, char **ret) {
769 if (!slice_name_is_valid(slice))
772 if (!unit_prefix_is_valid(name))
775 if (streq(slice, "-.slice"))
776 subslice = strappend(name, ".slice");
780 assert_se(e = endswith(slice, ".slice"));
782 subslice = new(char, (e - slice) + 1 + strlen(name) + 6 + 1);
786 stpcpy(stpcpy(stpcpy(mempcpy(subslice, slice, e - slice), "-"), name), ".slice");
793 bool slice_name_is_valid(const char *name) {
797 if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
800 if (streq(name, "-.slice"))
803 e = endswith(name, ".slice");
807 for (p = name; p < e; p++) {
811 /* Don't allow initial dash */
815 /* Don't allow multiple dashes */
824 /* Don't allow trailing hash */
831 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
832 [UNIT_SERVICE] = "service",
833 [UNIT_SOCKET] = "socket",
834 [UNIT_BUSNAME] = "busname",
835 [UNIT_TARGET] = "target",
836 [UNIT_SNAPSHOT] = "snapshot",
837 [UNIT_DEVICE] = "device",
838 [UNIT_MOUNT] = "mount",
839 [UNIT_AUTOMOUNT] = "automount",
840 [UNIT_SWAP] = "swap",
841 [UNIT_TIMER] = "timer",
842 [UNIT_PATH] = "path",
843 [UNIT_SLICE] = "slice",
844 [UNIT_SCOPE] = "scope",
847 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
849 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
850 [UNIT_STUB] = "stub",
851 [UNIT_LOADED] = "loaded",
852 [UNIT_NOT_FOUND] = "not-found",
853 [UNIT_ERROR] = "error",
854 [UNIT_MERGED] = "merged",
855 [UNIT_MASKED] = "masked"
858 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
860 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
861 [UNIT_REQUIRES] = "Requires",
862 [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable",
863 [UNIT_REQUISITE] = "Requisite",
864 [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable",
865 [UNIT_WANTS] = "Wants",
866 [UNIT_BINDS_TO] = "BindsTo",
867 [UNIT_PART_OF] = "PartOf",
868 [UNIT_REQUIRED_BY] = "RequiredBy",
869 [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable",
870 [UNIT_REQUISITE_OF] = "RequisiteOf",
871 [UNIT_REQUISITE_OF_OVERRIDABLE] = "RequisiteOfOverridable",
872 [UNIT_WANTED_BY] = "WantedBy",
873 [UNIT_BOUND_BY] = "BoundBy",
874 [UNIT_CONSISTS_OF] = "ConsistsOf",
875 [UNIT_CONFLICTS] = "Conflicts",
876 [UNIT_CONFLICTED_BY] = "ConflictedBy",
877 [UNIT_BEFORE] = "Before",
878 [UNIT_AFTER] = "After",
879 [UNIT_ON_FAILURE] = "OnFailure",
880 [UNIT_TRIGGERS] = "Triggers",
881 [UNIT_TRIGGERED_BY] = "TriggeredBy",
882 [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
883 [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
884 [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf",
885 [UNIT_REFERENCES] = "References",
886 [UNIT_REFERENCED_BY] = "ReferencedBy",
889 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);