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 "bus-label.h"
28 //#include "glob-util.h"
29 #include "hexdecoct.h"
31 #include "path-util.h"
32 #include "string-table.h"
33 #include "string-util.h"
35 #include "unit-name.h"
37 /* Characters valid in a unit name. */
43 /* The same, but also permits the single @ character that may appear */
44 #define VALID_CHARS_WITH_AT \
48 /* All chars valid in a unit name glob */
49 #define VALID_CHARS_GLOB \
53 bool unit_name_is_valid(const char *n, UnitNameFlags flags) {
54 const char *e, *i, *at;
56 assert((flags & ~(UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE)) == 0);
58 if (_unlikely_(flags == 0))
64 if (strlen(n) >= UNIT_NAME_MAX)
71 if (unit_type_from_string(e + 1) < 0)
74 for (i = n, at = NULL; i < e; i++) {
79 if (!strchr("@" VALID_CHARS, *i))
86 if (flags & UNIT_NAME_PLAIN)
90 if (flags & UNIT_NAME_INSTANCE)
94 if (flags & UNIT_NAME_TEMPLATE)
95 if (at && e == at + 1)
101 bool unit_prefix_is_valid(const char *p) {
103 /* We don't allow additional @ in the prefix string */
108 return in_charset(p, VALID_CHARS);
111 bool unit_instance_is_valid(const char *i) {
113 /* The max length depends on the length of the string, so we
114 * don't really check this here. */
119 /* We allow additional @ in the instance string, we do not
120 * allow them in the prefix! */
122 return in_charset(i, "@" VALID_CHARS);
125 bool unit_suffix_is_valid(const char *s) {
132 if (unit_type_from_string(s + 1) < 0)
138 #if 0 /// UNNEEDED by elogind
139 int unit_name_to_prefix(const char *n, char **ret) {
146 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
155 s = strndup(n, p - n);
163 int unit_name_to_instance(const char *n, char **instance) {
170 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
173 /* Everything past the first @ and before the last . is the instance */
194 int unit_name_to_prefix_and_instance(const char *n, char **ret) {
201 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
208 s = strndup(n, d - n);
216 UnitType unit_name_to_type(const char *n) {
221 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
222 return _UNIT_TYPE_INVALID;
224 assert_se(e = strrchr(n, '.'));
226 return unit_type_from_string(e + 1);
229 int unit_name_change_suffix(const char *n, const char *suffix, char **ret) {
237 if (!unit_name_is_valid(n, UNIT_NAME_ANY))
240 if (!unit_suffix_is_valid(suffix))
243 assert_se(e = strrchr(n, '.'));
248 s = new(char, a + b + 1);
252 strcpy(mempcpy(s, n, a), suffix);
259 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret) {
266 if (!unit_prefix_is_valid(prefix))
269 if (instance && !unit_instance_is_valid(instance))
272 if (!unit_suffix_is_valid(suffix))
276 s = strappend(prefix, suffix);
278 s = strjoin(prefix, "@", instance, suffix);
286 #if 0 /// UNNEEDED by elogind
287 static char *do_escape_char(char c, char *t) {
292 *(t++) = hexchar(c >> 4);
298 static char *do_escape(const char *f, char *t) {
302 /* do not create units with a leading '.', like for "/.dotdir" mount points */
304 t = do_escape_char(*f, t);
311 else if (*f == '-' || *f == '\\' || !strchr(VALID_CHARS, *f))
312 t = do_escape_char(*f, t);
320 char *unit_name_escape(const char *f) {
325 r = new(char, strlen(f)*4+1);
335 int unit_name_unescape(const char *f, char **ret) {
336 _cleanup_free_ char *r = NULL;
345 for (t = r; *f; f++) {
348 else if (*f == '\\') {
362 *(t++) = (char) (((uint8_t) a << 4U) | (uint8_t) b);
376 int unit_name_path_escape(const char *f, char **ret) {
386 path_kill_slashes(p);
388 if (STR_IN_SET(p, "/", ""))
393 if (!path_is_safe(p))
396 /* Truncate trailing slashes */
397 e = endswith(p, "/");
401 /* Truncate leading slashes */
405 s = unit_name_escape(p);
414 int unit_name_path_unescape(const char *f, char **ret) {
430 r = unit_name_unescape(f, &w);
434 /* Don't accept trailing or leading slashes */
435 if (startswith(w, "/") || endswith(w, "/")) {
440 /* Prefix a slash again */
441 s = strappend("/", w);
446 if (!path_is_safe(s)) {
460 int unit_name_replace_instance(const char *f, const char *i, char **ret) {
469 if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
471 if (!unit_instance_is_valid(i))
474 assert_se(p = strchr(f, '@'));
475 assert_se(e = strrchr(f, '.'));
480 s = new(char, a + 1 + b + strlen(e) + 1);
484 strcpy(mempcpy(mempcpy(s, f, a + 1), i, b), e);
490 int unit_name_template(const char *f, char **ret) {
498 if (!unit_name_is_valid(f, UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE))
501 assert_se(p = strchr(f, '@'));
502 assert_se(e = strrchr(f, '.'));
506 s = new(char, a + 1 + strlen(e) + 1);
510 strcpy(mempcpy(s, f, a + 1), e);
516 int unit_name_from_path(const char *path, const char *suffix, char **ret) {
517 _cleanup_free_ char *p = NULL;
525 if (!unit_suffix_is_valid(suffix))
528 r = unit_name_path_escape(path, &p);
532 s = strappend(p, suffix);
540 int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret) {
541 _cleanup_free_ char *p = NULL;
550 if (!unit_prefix_is_valid(prefix))
553 if (!unit_suffix_is_valid(suffix))
556 r = unit_name_path_escape(path, &p);
560 s = strjoin(prefix, "@", p, suffix);
568 int unit_name_to_path(const char *name, char **ret) {
569 _cleanup_free_ char *prefix = NULL;
574 r = unit_name_to_prefix(name, &prefix);
578 return unit_name_path_unescape(prefix, ret);
581 char *unit_dbus_path_from_name(const char *name) {
582 _cleanup_free_ char *e = NULL;
586 e = bus_label_escape(name);
590 return strappend("/org/freedesktop/systemd1/unit/", e);
593 int unit_name_from_dbus_path(const char *path, char **name) {
597 e = startswith(path, "/org/freedesktop/systemd1/unit/");
601 n = bus_label_unescape(e);
609 const char* unit_dbus_interface_from_type(UnitType t) {
611 static const char *const table[_UNIT_TYPE_MAX] = {
612 [UNIT_SERVICE] = "org.freedesktop.systemd1.Service",
613 [UNIT_SOCKET] = "org.freedesktop.systemd1.Socket",
614 [UNIT_BUSNAME] = "org.freedesktop.systemd1.BusName",
615 [UNIT_TARGET] = "org.freedesktop.systemd1.Target",
616 [UNIT_DEVICE] = "org.freedesktop.systemd1.Device",
617 [UNIT_MOUNT] = "org.freedesktop.systemd1.Mount",
618 [UNIT_AUTOMOUNT] = "org.freedesktop.systemd1.Automount",
619 [UNIT_SWAP] = "org.freedesktop.systemd1.Swap",
620 [UNIT_TIMER] = "org.freedesktop.systemd1.Timer",
621 [UNIT_PATH] = "org.freedesktop.systemd1.Path",
622 [UNIT_SLICE] = "org.freedesktop.systemd1.Slice",
623 [UNIT_SCOPE] = "org.freedesktop.systemd1.Scope",
628 if (t >= _UNIT_TYPE_MAX)
634 const char *unit_dbus_interface_from_name(const char *name) {
637 t = unit_name_to_type(name);
641 return unit_dbus_interface_from_type(t);
644 static char *do_escape_mangle(const char *f, UnitNameMangle allow_globs, char *t) {
645 const char *valid_chars;
648 assert(IN_SET(allow_globs, UNIT_NAME_GLOB, UNIT_NAME_NOGLOB));
651 /* We'll only escape the obvious characters here, to play
654 valid_chars = allow_globs == UNIT_NAME_GLOB ? VALID_CHARS_GLOB : VALID_CHARS_WITH_AT;
659 else if (!strchr(valid_chars, *f))
660 t = do_escape_char(*f, t);
669 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
670 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
671 * except that @suffix is appended if a valid unit suffix is not present.
673 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
675 int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret) {
683 if (isempty(name)) /* We cannot mangle empty unit names to become valid, sorry. */
686 if (!unit_suffix_is_valid(suffix))
689 /* Already a fully valid unit name? If so, no mangling is necessary... */
690 if (unit_name_is_valid(name, UNIT_NAME_ANY))
693 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
694 if (allow_globs == UNIT_NAME_GLOB &&
695 string_is_glob(name) &&
696 in_charset(name, VALID_CHARS_GLOB))
699 if (is_device_path(name)) {
700 r = unit_name_from_path(name, ".device", ret);
707 if (path_is_absolute(name)) {
708 r = unit_name_from_path(name, ".mount", ret);
715 s = new(char, strlen(name) * 4 + strlen(suffix) + 1);
719 t = do_escape_mangle(name, allow_globs, s);
722 /* 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
724 if ((allow_globs != UNIT_NAME_GLOB || !string_is_glob(s)) && unit_name_to_type(s) < 0)
739 int slice_build_parent_slice(const char *slice, char **ret) {
746 if (!slice_name_is_valid(slice))
749 if (streq(slice, "-.slice")) {
758 dash = strrchr(s, '-');
760 strcpy(dash, ".slice");
762 r = free_and_strdup(&s, "-.slice");
774 int slice_build_subslice(const char *slice, const char*name, char **ret) {
781 if (!slice_name_is_valid(slice))
784 if (!unit_prefix_is_valid(name))
787 if (streq(slice, "-.slice"))
788 subslice = strappend(name, ".slice");
792 assert_se(e = endswith(slice, ".slice"));
794 subslice = new(char, (e - slice) + 1 + strlen(name) + 6 + 1);
798 stpcpy(stpcpy(stpcpy(mempcpy(subslice, slice, e - slice), "-"), name), ".slice");
805 bool slice_name_is_valid(const char *name) {
809 if (!unit_name_is_valid(name, UNIT_NAME_PLAIN))
812 if (streq(name, "-.slice"))
815 e = endswith(name, ".slice");
819 for (p = name; p < e; p++) {
823 /* Don't allow initial dash */
827 /* Don't allow multiple dashes */
836 /* Don't allow trailing hash */
843 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
844 [UNIT_SERVICE] = "service",
845 [UNIT_SOCKET] = "socket",
846 [UNIT_BUSNAME] = "busname",
847 [UNIT_TARGET] = "target",
848 [UNIT_DEVICE] = "device",
849 [UNIT_MOUNT] = "mount",
850 [UNIT_AUTOMOUNT] = "automount",
851 [UNIT_SWAP] = "swap",
852 [UNIT_TIMER] = "timer",
853 [UNIT_PATH] = "path",
854 [UNIT_SLICE] = "slice",
855 [UNIT_SCOPE] = "scope",
858 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
860 #if 0 /// UNNEEDED by elogind
861 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
862 [UNIT_STUB] = "stub",
863 [UNIT_LOADED] = "loaded",
864 [UNIT_NOT_FOUND] = "not-found",
865 [UNIT_ERROR] = "error",
866 [UNIT_MERGED] = "merged",
867 [UNIT_MASKED] = "masked"
870 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
872 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
873 [UNIT_ACTIVE] = "active",
874 [UNIT_RELOADING] = "reloading",
875 [UNIT_INACTIVE] = "inactive",
876 [UNIT_FAILED] = "failed",
877 [UNIT_ACTIVATING] = "activating",
878 [UNIT_DEACTIVATING] = "deactivating"
881 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
883 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
884 [AUTOMOUNT_DEAD] = "dead",
885 [AUTOMOUNT_WAITING] = "waiting",
886 [AUTOMOUNT_RUNNING] = "running",
887 [AUTOMOUNT_FAILED] = "failed"
890 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
892 static const char* const busname_state_table[_BUSNAME_STATE_MAX] = {
893 [BUSNAME_DEAD] = "dead",
894 [BUSNAME_MAKING] = "making",
895 [BUSNAME_REGISTERED] = "registered",
896 [BUSNAME_LISTENING] = "listening",
897 [BUSNAME_RUNNING] = "running",
898 [BUSNAME_SIGTERM] = "sigterm",
899 [BUSNAME_SIGKILL] = "sigkill",
900 [BUSNAME_FAILED] = "failed",
903 DEFINE_STRING_TABLE_LOOKUP(busname_state, BusNameState);
905 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
906 [DEVICE_DEAD] = "dead",
907 [DEVICE_TENTATIVE] = "tentative",
908 [DEVICE_PLUGGED] = "plugged",
911 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
913 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
914 [MOUNT_DEAD] = "dead",
915 [MOUNT_MOUNTING] = "mounting",
916 [MOUNT_MOUNTING_DONE] = "mounting-done",
917 [MOUNT_MOUNTED] = "mounted",
918 [MOUNT_REMOUNTING] = "remounting",
919 [MOUNT_UNMOUNTING] = "unmounting",
920 [MOUNT_MOUNTING_SIGTERM] = "mounting-sigterm",
921 [MOUNT_MOUNTING_SIGKILL] = "mounting-sigkill",
922 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
923 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
924 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
925 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
926 [MOUNT_FAILED] = "failed"
929 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
931 static const char* const path_state_table[_PATH_STATE_MAX] = {
932 [PATH_DEAD] = "dead",
933 [PATH_WAITING] = "waiting",
934 [PATH_RUNNING] = "running",
935 [PATH_FAILED] = "failed"
938 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
940 static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
941 [SCOPE_DEAD] = "dead",
942 [SCOPE_RUNNING] = "running",
943 [SCOPE_ABANDONED] = "abandoned",
944 [SCOPE_STOP_SIGTERM] = "stop-sigterm",
945 [SCOPE_STOP_SIGKILL] = "stop-sigkill",
946 [SCOPE_FAILED] = "failed",
949 DEFINE_STRING_TABLE_LOOKUP(scope_state, ScopeState);
951 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
952 [SERVICE_DEAD] = "dead",
953 [SERVICE_START_PRE] = "start-pre",
954 [SERVICE_START] = "start",
955 [SERVICE_START_POST] = "start-post",
956 [SERVICE_RUNNING] = "running",
957 [SERVICE_EXITED] = "exited",
958 [SERVICE_RELOAD] = "reload",
959 [SERVICE_STOP] = "stop",
960 [SERVICE_STOP_SIGABRT] = "stop-sigabrt",
961 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
962 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
963 [SERVICE_STOP_POST] = "stop-post",
964 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
965 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
966 [SERVICE_FAILED] = "failed",
967 [SERVICE_AUTO_RESTART] = "auto-restart",
970 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
972 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
973 [SLICE_DEAD] = "dead",
974 [SLICE_ACTIVE] = "active"
977 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
979 static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
980 [SOCKET_DEAD] = "dead",
981 [SOCKET_START_PRE] = "start-pre",
982 [SOCKET_START_CHOWN] = "start-chown",
983 [SOCKET_START_POST] = "start-post",
984 [SOCKET_LISTENING] = "listening",
985 [SOCKET_RUNNING] = "running",
986 [SOCKET_STOP_PRE] = "stop-pre",
987 [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
988 [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
989 [SOCKET_STOP_POST] = "stop-post",
990 [SOCKET_FINAL_SIGTERM] = "final-sigterm",
991 [SOCKET_FINAL_SIGKILL] = "final-sigkill",
992 [SOCKET_FAILED] = "failed"
995 DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState);
997 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
998 [SWAP_DEAD] = "dead",
999 [SWAP_ACTIVATING] = "activating",
1000 [SWAP_ACTIVATING_DONE] = "activating-done",
1001 [SWAP_ACTIVE] = "active",
1002 [SWAP_DEACTIVATING] = "deactivating",
1003 [SWAP_ACTIVATING_SIGTERM] = "activating-sigterm",
1004 [SWAP_ACTIVATING_SIGKILL] = "activating-sigkill",
1005 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
1006 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
1007 [SWAP_FAILED] = "failed"
1010 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
1012 static const char* const target_state_table[_TARGET_STATE_MAX] = {
1013 [TARGET_DEAD] = "dead",
1014 [TARGET_ACTIVE] = "active"
1017 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
1019 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
1020 [TIMER_DEAD] = "dead",
1021 [TIMER_WAITING] = "waiting",
1022 [TIMER_RUNNING] = "running",
1023 [TIMER_ELAPSED] = "elapsed",
1024 [TIMER_FAILED] = "failed"
1027 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
1029 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
1030 [UNIT_REQUIRES] = "Requires",
1031 [UNIT_REQUISITE] = "Requisite",
1032 [UNIT_WANTS] = "Wants",
1033 [UNIT_BINDS_TO] = "BindsTo",
1034 [UNIT_PART_OF] = "PartOf",
1035 [UNIT_REQUIRED_BY] = "RequiredBy",
1036 [UNIT_REQUISITE_OF] = "RequisiteOf",
1037 [UNIT_WANTED_BY] = "WantedBy",
1038 [UNIT_BOUND_BY] = "BoundBy",
1039 [UNIT_CONSISTS_OF] = "ConsistsOf",
1040 [UNIT_CONFLICTS] = "Conflicts",
1041 [UNIT_CONFLICTED_BY] = "ConflictedBy",
1042 [UNIT_BEFORE] = "Before",
1043 [UNIT_AFTER] = "After",
1044 [UNIT_ON_FAILURE] = "OnFailure",
1045 [UNIT_TRIGGERS] = "Triggers",
1046 [UNIT_TRIGGERED_BY] = "TriggeredBy",
1047 [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
1048 [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
1049 [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf",
1050 [UNIT_REFERENCES] = "References",
1051 [UNIT_REFERENCED_BY] = "ReferencedBy",
1054 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);