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 int unit_name_to_prefix(const char *n
, char **ret
) {
145 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
154 s
= strndup(n
, p
- n
);
162 int unit_name_to_instance(const char *n
, char **instance
) {
169 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
172 /* Everything past the first @ and before the last . is the instance */
193 int unit_name_to_prefix_and_instance(const char *n
, char **ret
) {
200 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
207 s
= strndup(n
, d
- n
);
215 UnitType
unit_name_to_type(const char *n
) {
220 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
221 return _UNIT_TYPE_INVALID
;
223 assert_se(e
= strrchr(n
, '.'));
225 return unit_type_from_string(e
+ 1);
228 int unit_name_change_suffix(const char *n
, const char *suffix
, char **ret
) {
236 if (!unit_name_is_valid(n
, UNIT_NAME_ANY
))
239 if (!unit_suffix_is_valid(suffix
))
242 assert_se(e
= strrchr(n
, '.'));
247 s
= new(char, a
+ b
+ 1);
251 strcpy(mempcpy(s
, n
, a
), suffix
);
257 int unit_name_build(const char *prefix
, const char *instance
, const char *suffix
, char **ret
) {
264 if (!unit_prefix_is_valid(prefix
))
267 if (instance
&& !unit_instance_is_valid(instance
))
270 if (!unit_suffix_is_valid(suffix
))
274 s
= strappend(prefix
, suffix
);
276 s
= strjoin(prefix
, "@", instance
, suffix
, NULL
);
284 static char *do_escape_char(char c
, char *t
) {
289 *(t
++) = hexchar(c
>> 4);
295 static char *do_escape(const char *f
, char *t
) {
299 /* do not create units with a leading '.', like for "/.dotdir" mount points */
301 t
= do_escape_char(*f
, t
);
308 else if (*f
== '-' || *f
== '\\' || !strchr(VALID_CHARS
, *f
))
309 t
= do_escape_char(*f
, t
);
317 char *unit_name_escape(const char *f
) {
322 r
= new(char, strlen(f
)*4+1);
332 int unit_name_unescape(const char *f
, char **ret
) {
333 _cleanup_free_
char *r
= NULL
;
342 for (t
= r
; *f
; f
++) {
345 else if (*f
== '\\') {
359 *(t
++) = (char) (((uint8_t) a
<< 4U) | (uint8_t) b
);
373 int unit_name_path_escape(const char *f
, char **ret
) {
383 path_kill_slashes(p
);
385 if (STR_IN_SET(p
, "/", ""))
390 if (!path_is_safe(p
))
393 /* Truncate trailing slashes */
394 e
= endswith(p
, "/");
398 /* Truncate leading slashes */
402 s
= unit_name_escape(p
);
411 int unit_name_path_unescape(const char *f
, char **ret
) {
427 r
= unit_name_unescape(f
, &w
);
431 /* Don't accept trailing or leading slashes */
432 if (startswith(w
, "/") || endswith(w
, "/")) {
437 /* Prefix a slash again */
438 s
= strappend("/", w
);
443 if (!path_is_safe(s
)) {
457 int unit_name_replace_instance(const char *f
, const char *i
, char **ret
) {
466 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
468 if (!unit_instance_is_valid(i
))
471 assert_se(p
= strchr(f
, '@'));
472 assert_se(e
= strrchr(f
, '.'));
477 s
= new(char, a
+ 1 + b
+ strlen(e
) + 1);
481 strcpy(mempcpy(mempcpy(s
, f
, a
+ 1), i
, b
), e
);
487 int unit_name_template(const char *f
, char **ret
) {
495 if (!unit_name_is_valid(f
, UNIT_NAME_INSTANCE
|UNIT_NAME_TEMPLATE
))
498 assert_se(p
= strchr(f
, '@'));
499 assert_se(e
= strrchr(f
, '.'));
503 s
= new(char, a
+ 1 + strlen(e
) + 1);
507 strcpy(mempcpy(s
, f
, a
+ 1), e
);
513 int unit_name_from_path(const char *path
, const char *suffix
, char **ret
) {
514 _cleanup_free_
char *p
= NULL
;
522 if (!unit_suffix_is_valid(suffix
))
525 r
= unit_name_path_escape(path
, &p
);
529 s
= strappend(p
, suffix
);
537 int unit_name_from_path_instance(const char *prefix
, const char *path
, const char *suffix
, char **ret
) {
538 _cleanup_free_
char *p
= NULL
;
547 if (!unit_prefix_is_valid(prefix
))
550 if (!unit_suffix_is_valid(suffix
))
553 r
= unit_name_path_escape(path
, &p
);
557 s
= strjoin(prefix
, "@", p
, suffix
, NULL
);
565 int unit_name_to_path(const char *name
, char **ret
) {
566 _cleanup_free_
char *prefix
= NULL
;
571 r
= unit_name_to_prefix(name
, &prefix
);
575 return unit_name_path_unescape(prefix
, ret
);
578 char *unit_dbus_path_from_name(const char *name
) {
579 _cleanup_free_
char *e
= NULL
;
583 e
= bus_label_escape(name
);
587 return strappend("/org/freedesktop/systemd1/unit/", e
);
590 int unit_name_from_dbus_path(const char *path
, char **name
) {
594 e
= startswith(path
, "/org/freedesktop/systemd1/unit/");
598 n
= bus_label_unescape(e
);
606 const char* unit_dbus_interface_from_type(UnitType t
) {
608 static const char *const table
[_UNIT_TYPE_MAX
] = {
609 [UNIT_SERVICE
] = "org.freedesktop.systemd1.Service",
610 [UNIT_SOCKET
] = "org.freedesktop.systemd1.Socket",
611 [UNIT_BUSNAME
] = "org.freedesktop.systemd1.BusName",
612 [UNIT_TARGET
] = "org.freedesktop.systemd1.Target",
613 [UNIT_DEVICE
] = "org.freedesktop.systemd1.Device",
614 [UNIT_MOUNT
] = "org.freedesktop.systemd1.Mount",
615 [UNIT_AUTOMOUNT
] = "org.freedesktop.systemd1.Automount",
616 [UNIT_SWAP
] = "org.freedesktop.systemd1.Swap",
617 [UNIT_TIMER
] = "org.freedesktop.systemd1.Timer",
618 [UNIT_PATH
] = "org.freedesktop.systemd1.Path",
619 [UNIT_SLICE
] = "org.freedesktop.systemd1.Slice",
620 [UNIT_SCOPE
] = "org.freedesktop.systemd1.Scope",
625 if (t
>= _UNIT_TYPE_MAX
)
631 const char *unit_dbus_interface_from_name(const char *name
) {
634 t
= unit_name_to_type(name
);
638 return unit_dbus_interface_from_type(t
);
641 static char *do_escape_mangle(const char *f
, UnitNameMangle allow_globs
, char *t
) {
642 const char *valid_chars
;
645 assert(IN_SET(allow_globs
, UNIT_NAME_GLOB
, UNIT_NAME_NOGLOB
));
648 /* We'll only escape the obvious characters here, to play
651 valid_chars
= allow_globs
== UNIT_NAME_GLOB
? VALID_CHARS_GLOB
: VALID_CHARS_WITH_AT
;
656 else if (!strchr(valid_chars
, *f
))
657 t
= do_escape_char(*f
, t
);
666 * Convert a string to a unit name. /dev/blah is converted to dev-blah.device,
667 * /blah/blah is converted to blah-blah.mount, anything else is left alone,
668 * except that @suffix is appended if a valid unit suffix is not present.
670 * If @allow_globs, globs characters are preserved. Otherwise, they are escaped.
672 int unit_name_mangle_with_suffix(const char *name
, UnitNameMangle allow_globs
, const char *suffix
, char **ret
) {
680 if (isempty(name
)) /* We cannot mangle empty unit names to become valid, sorry. */
683 if (!unit_suffix_is_valid(suffix
))
686 /* Already a fully valid unit name? If so, no mangling is necessary... */
687 if (unit_name_is_valid(name
, UNIT_NAME_ANY
))
690 /* Already a fully valid globbing expression? If so, no mangling is necessary either... */
691 if (allow_globs
== UNIT_NAME_GLOB
&&
692 string_is_glob(name
) &&
693 in_charset(name
, VALID_CHARS_GLOB
))
696 if (is_device_path(name
)) {
697 r
= unit_name_from_path(name
, ".device", ret
);
704 if (path_is_absolute(name
)) {
705 r
= unit_name_from_path(name
, ".mount", ret
);
712 s
= new(char, strlen(name
) * 4 + strlen(suffix
) + 1);
716 t
= do_escape_mangle(name
, allow_globs
, s
);
719 /* 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
721 if ((allow_globs
!= UNIT_NAME_GLOB
|| !string_is_glob(s
)) && unit_name_to_type(s
) < 0)
736 int slice_build_parent_slice(const char *slice
, char **ret
) {
743 if (!slice_name_is_valid(slice
))
746 if (streq(slice
, "-.slice")) {
755 dash
= strrchr(s
, '-');
757 strcpy(dash
, ".slice");
759 r
= free_and_strdup(&s
, "-.slice");
770 int slice_build_subslice(const char *slice
, const char*name
, char **ret
) {
777 if (!slice_name_is_valid(slice
))
780 if (!unit_prefix_is_valid(name
))
783 if (streq(slice
, "-.slice"))
784 subslice
= strappend(name
, ".slice");
788 assert_se(e
= endswith(slice
, ".slice"));
790 subslice
= new(char, (e
- slice
) + 1 + strlen(name
) + 6 + 1);
794 stpcpy(stpcpy(stpcpy(mempcpy(subslice
, slice
, e
- slice
), "-"), name
), ".slice");
801 bool slice_name_is_valid(const char *name
) {
805 if (!unit_name_is_valid(name
, UNIT_NAME_PLAIN
))
808 if (streq(name
, "-.slice"))
811 e
= endswith(name
, ".slice");
815 for (p
= name
; p
< e
; p
++) {
819 /* Don't allow initial dash */
823 /* Don't allow multiple dashes */
832 /* Don't allow trailing hash */
839 static const char* const unit_type_table
[_UNIT_TYPE_MAX
] = {
840 [UNIT_SERVICE
] = "service",
841 [UNIT_SOCKET
] = "socket",
842 [UNIT_BUSNAME
] = "busname",
843 [UNIT_TARGET
] = "target",
844 [UNIT_DEVICE
] = "device",
845 [UNIT_MOUNT
] = "mount",
846 [UNIT_AUTOMOUNT
] = "automount",
847 [UNIT_SWAP
] = "swap",
848 [UNIT_TIMER
] = "timer",
849 [UNIT_PATH
] = "path",
850 [UNIT_SLICE
] = "slice",
851 [UNIT_SCOPE
] = "scope",
854 DEFINE_STRING_TABLE_LOOKUP(unit_type
, UnitType
);
856 static const char* const unit_load_state_table
[_UNIT_LOAD_STATE_MAX
] = {
857 [UNIT_STUB
] = "stub",
858 [UNIT_LOADED
] = "loaded",
859 [UNIT_NOT_FOUND
] = "not-found",
860 [UNIT_ERROR
] = "error",
861 [UNIT_MERGED
] = "merged",
862 [UNIT_MASKED
] = "masked"
865 DEFINE_STRING_TABLE_LOOKUP(unit_load_state
, UnitLoadState
);
867 static const char* const unit_active_state_table
[_UNIT_ACTIVE_STATE_MAX
] = {
868 [UNIT_ACTIVE
] = "active",
869 [UNIT_RELOADING
] = "reloading",
870 [UNIT_INACTIVE
] = "inactive",
871 [UNIT_FAILED
] = "failed",
872 [UNIT_ACTIVATING
] = "activating",
873 [UNIT_DEACTIVATING
] = "deactivating"
876 DEFINE_STRING_TABLE_LOOKUP(unit_active_state
, UnitActiveState
);
878 static const char* const automount_state_table
[_AUTOMOUNT_STATE_MAX
] = {
879 [AUTOMOUNT_DEAD
] = "dead",
880 [AUTOMOUNT_WAITING
] = "waiting",
881 [AUTOMOUNT_RUNNING
] = "running",
882 [AUTOMOUNT_FAILED
] = "failed"
885 DEFINE_STRING_TABLE_LOOKUP(automount_state
, AutomountState
);
887 static const char* const busname_state_table
[_BUSNAME_STATE_MAX
] = {
888 [BUSNAME_DEAD
] = "dead",
889 [BUSNAME_MAKING
] = "making",
890 [BUSNAME_REGISTERED
] = "registered",
891 [BUSNAME_LISTENING
] = "listening",
892 [BUSNAME_RUNNING
] = "running",
893 [BUSNAME_SIGTERM
] = "sigterm",
894 [BUSNAME_SIGKILL
] = "sigkill",
895 [BUSNAME_FAILED
] = "failed",
898 DEFINE_STRING_TABLE_LOOKUP(busname_state
, BusNameState
);
900 static const char* const device_state_table
[_DEVICE_STATE_MAX
] = {
901 [DEVICE_DEAD
] = "dead",
902 [DEVICE_TENTATIVE
] = "tentative",
903 [DEVICE_PLUGGED
] = "plugged",
906 DEFINE_STRING_TABLE_LOOKUP(device_state
, DeviceState
);
908 static const char* const mount_state_table
[_MOUNT_STATE_MAX
] = {
909 [MOUNT_DEAD
] = "dead",
910 [MOUNT_MOUNTING
] = "mounting",
911 [MOUNT_MOUNTING_DONE
] = "mounting-done",
912 [MOUNT_MOUNTED
] = "mounted",
913 [MOUNT_REMOUNTING
] = "remounting",
914 [MOUNT_UNMOUNTING
] = "unmounting",
915 [MOUNT_MOUNTING_SIGTERM
] = "mounting-sigterm",
916 [MOUNT_MOUNTING_SIGKILL
] = "mounting-sigkill",
917 [MOUNT_REMOUNTING_SIGTERM
] = "remounting-sigterm",
918 [MOUNT_REMOUNTING_SIGKILL
] = "remounting-sigkill",
919 [MOUNT_UNMOUNTING_SIGTERM
] = "unmounting-sigterm",
920 [MOUNT_UNMOUNTING_SIGKILL
] = "unmounting-sigkill",
921 [MOUNT_FAILED
] = "failed"
924 DEFINE_STRING_TABLE_LOOKUP(mount_state
, MountState
);
926 static const char* const path_state_table
[_PATH_STATE_MAX
] = {
927 [PATH_DEAD
] = "dead",
928 [PATH_WAITING
] = "waiting",
929 [PATH_RUNNING
] = "running",
930 [PATH_FAILED
] = "failed"
933 DEFINE_STRING_TABLE_LOOKUP(path_state
, PathState
);
935 static const char* const scope_state_table
[_SCOPE_STATE_MAX
] = {
936 [SCOPE_DEAD
] = "dead",
937 [SCOPE_RUNNING
] = "running",
938 [SCOPE_ABANDONED
] = "abandoned",
939 [SCOPE_STOP_SIGTERM
] = "stop-sigterm",
940 [SCOPE_STOP_SIGKILL
] = "stop-sigkill",
941 [SCOPE_FAILED
] = "failed",
944 DEFINE_STRING_TABLE_LOOKUP(scope_state
, ScopeState
);
946 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
947 [SERVICE_DEAD
] = "dead",
948 [SERVICE_START_PRE
] = "start-pre",
949 [SERVICE_START
] = "start",
950 [SERVICE_START_POST
] = "start-post",
951 [SERVICE_RUNNING
] = "running",
952 [SERVICE_EXITED
] = "exited",
953 [SERVICE_RELOAD
] = "reload",
954 [SERVICE_STOP
] = "stop",
955 [SERVICE_STOP_SIGABRT
] = "stop-sigabrt",
956 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
957 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
958 [SERVICE_STOP_POST
] = "stop-post",
959 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
960 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
961 [SERVICE_FAILED
] = "failed",
962 [SERVICE_AUTO_RESTART
] = "auto-restart",
965 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
967 static const char* const slice_state_table
[_SLICE_STATE_MAX
] = {
968 [SLICE_DEAD
] = "dead",
969 [SLICE_ACTIVE
] = "active"
972 DEFINE_STRING_TABLE_LOOKUP(slice_state
, SliceState
);
974 static const char* const socket_state_table
[_SOCKET_STATE_MAX
] = {
975 [SOCKET_DEAD
] = "dead",
976 [SOCKET_START_PRE
] = "start-pre",
977 [SOCKET_START_CHOWN
] = "start-chown",
978 [SOCKET_START_POST
] = "start-post",
979 [SOCKET_LISTENING
] = "listening",
980 [SOCKET_RUNNING
] = "running",
981 [SOCKET_STOP_PRE
] = "stop-pre",
982 [SOCKET_STOP_PRE_SIGTERM
] = "stop-pre-sigterm",
983 [SOCKET_STOP_PRE_SIGKILL
] = "stop-pre-sigkill",
984 [SOCKET_STOP_POST
] = "stop-post",
985 [SOCKET_FINAL_SIGTERM
] = "final-sigterm",
986 [SOCKET_FINAL_SIGKILL
] = "final-sigkill",
987 [SOCKET_FAILED
] = "failed"
990 DEFINE_STRING_TABLE_LOOKUP(socket_state
, SocketState
);
992 static const char* const swap_state_table
[_SWAP_STATE_MAX
] = {
993 [SWAP_DEAD
] = "dead",
994 [SWAP_ACTIVATING
] = "activating",
995 [SWAP_ACTIVATING_DONE
] = "activating-done",
996 [SWAP_ACTIVE
] = "active",
997 [SWAP_DEACTIVATING
] = "deactivating",
998 [SWAP_ACTIVATING_SIGTERM
] = "activating-sigterm",
999 [SWAP_ACTIVATING_SIGKILL
] = "activating-sigkill",
1000 [SWAP_DEACTIVATING_SIGTERM
] = "deactivating-sigterm",
1001 [SWAP_DEACTIVATING_SIGKILL
] = "deactivating-sigkill",
1002 [SWAP_FAILED
] = "failed"
1005 DEFINE_STRING_TABLE_LOOKUP(swap_state
, SwapState
);
1007 static const char* const target_state_table
[_TARGET_STATE_MAX
] = {
1008 [TARGET_DEAD
] = "dead",
1009 [TARGET_ACTIVE
] = "active"
1012 DEFINE_STRING_TABLE_LOOKUP(target_state
, TargetState
);
1014 static const char* const timer_state_table
[_TIMER_STATE_MAX
] = {
1015 [TIMER_DEAD
] = "dead",
1016 [TIMER_WAITING
] = "waiting",
1017 [TIMER_RUNNING
] = "running",
1018 [TIMER_ELAPSED
] = "elapsed",
1019 [TIMER_FAILED
] = "failed"
1022 DEFINE_STRING_TABLE_LOOKUP(timer_state
, TimerState
);
1024 static const char* const unit_dependency_table
[_UNIT_DEPENDENCY_MAX
] = {
1025 [UNIT_REQUIRES
] = "Requires",
1026 [UNIT_REQUISITE
] = "Requisite",
1027 [UNIT_WANTS
] = "Wants",
1028 [UNIT_BINDS_TO
] = "BindsTo",
1029 [UNIT_PART_OF
] = "PartOf",
1030 [UNIT_REQUIRED_BY
] = "RequiredBy",
1031 [UNIT_REQUISITE_OF
] = "RequisiteOf",
1032 [UNIT_WANTED_BY
] = "WantedBy",
1033 [UNIT_BOUND_BY
] = "BoundBy",
1034 [UNIT_CONSISTS_OF
] = "ConsistsOf",
1035 [UNIT_CONFLICTS
] = "Conflicts",
1036 [UNIT_CONFLICTED_BY
] = "ConflictedBy",
1037 [UNIT_BEFORE
] = "Before",
1038 [UNIT_AFTER
] = "After",
1039 [UNIT_ON_FAILURE
] = "OnFailure",
1040 [UNIT_TRIGGERS
] = "Triggers",
1041 [UNIT_TRIGGERED_BY
] = "TriggeredBy",
1042 [UNIT_PROPAGATES_RELOAD_TO
] = "PropagatesReloadTo",
1043 [UNIT_RELOAD_PROPAGATED_FROM
] = "ReloadPropagatedFrom",
1044 [UNIT_JOINS_NAMESPACE_OF
] = "JoinsNamespaceOf",
1045 [UNIT_REFERENCES
] = "References",
1046 [UNIT_REFERENCED_BY
] = "ReferencedBy",
1049 DEFINE_STRING_TABLE_LOOKUP(unit_dependency
, UnitDependency
);