1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "bus-label.h"
5 #include "string-table.h"
6 #include "unit-def.h"
7 #include "unit-name.h"
8
unit_dbus_path_from_name(const char * name)9 char *unit_dbus_path_from_name(const char *name) {
10 _cleanup_free_ char *e = NULL;
11
12 assert(name);
13
14 e = bus_label_escape(name);
15 if (!e)
16 return NULL;
17
18 return strjoin("/org/freedesktop/systemd1/unit/", e);
19 }
20
unit_name_from_dbus_path(const char * path,char ** name)21 int unit_name_from_dbus_path(const char *path, char **name) {
22 const char *e;
23 char *n;
24
25 e = startswith(path, "/org/freedesktop/systemd1/unit/");
26 if (!e)
27 return -EINVAL;
28
29 n = bus_label_unescape(e);
30 if (!n)
31 return -ENOMEM;
32
33 *name = n;
34 return 0;
35 }
36
unit_dbus_interface_from_type(UnitType t)37 const char* unit_dbus_interface_from_type(UnitType t) {
38
39 static const char *const table[_UNIT_TYPE_MAX] = {
40 [UNIT_SERVICE] = "org.freedesktop.systemd1.Service",
41 [UNIT_SOCKET] = "org.freedesktop.systemd1.Socket",
42 [UNIT_TARGET] = "org.freedesktop.systemd1.Target",
43 [UNIT_DEVICE] = "org.freedesktop.systemd1.Device",
44 [UNIT_MOUNT] = "org.freedesktop.systemd1.Mount",
45 [UNIT_AUTOMOUNT] = "org.freedesktop.systemd1.Automount",
46 [UNIT_SWAP] = "org.freedesktop.systemd1.Swap",
47 [UNIT_TIMER] = "org.freedesktop.systemd1.Timer",
48 [UNIT_PATH] = "org.freedesktop.systemd1.Path",
49 [UNIT_SLICE] = "org.freedesktop.systemd1.Slice",
50 [UNIT_SCOPE] = "org.freedesktop.systemd1.Scope",
51 };
52
53 if (t < 0)
54 return NULL;
55 if (t >= _UNIT_TYPE_MAX)
56 return NULL;
57
58 return table[t];
59 }
60
unit_dbus_interface_from_name(const char * name)61 const char *unit_dbus_interface_from_name(const char *name) {
62 UnitType t;
63
64 t = unit_name_to_type(name);
65 if (t < 0)
66 return NULL;
67
68 return unit_dbus_interface_from_type(t);
69 }
70
71 static const char* const unit_type_table[_UNIT_TYPE_MAX] = {
72 [UNIT_SERVICE] = "service",
73 [UNIT_SOCKET] = "socket",
74 [UNIT_TARGET] = "target",
75 [UNIT_DEVICE] = "device",
76 [UNIT_MOUNT] = "mount",
77 [UNIT_AUTOMOUNT] = "automount",
78 [UNIT_SWAP] = "swap",
79 [UNIT_TIMER] = "timer",
80 [UNIT_PATH] = "path",
81 [UNIT_SLICE] = "slice",
82 [UNIT_SCOPE] = "scope",
83 };
84
85 DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType);
86
87 static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = {
88 [UNIT_STUB] = "stub",
89 [UNIT_LOADED] = "loaded",
90 [UNIT_NOT_FOUND] = "not-found",
91 [UNIT_BAD_SETTING] = "bad-setting",
92 [UNIT_ERROR] = "error",
93 [UNIT_MERGED] = "merged",
94 [UNIT_MASKED] = "masked"
95 };
96
97 DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState);
98
99 static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = {
100 [UNIT_ACTIVE] = "active",
101 [UNIT_RELOADING] = "reloading",
102 [UNIT_INACTIVE] = "inactive",
103 [UNIT_FAILED] = "failed",
104 [UNIT_ACTIVATING] = "activating",
105 [UNIT_DEACTIVATING] = "deactivating",
106 [UNIT_MAINTENANCE] = "maintenance",
107 };
108
109 DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState);
110
111 static const char* const freezer_state_table[_FREEZER_STATE_MAX] = {
112 [FREEZER_RUNNING] = "running",
113 [FREEZER_FREEZING] = "freezing",
114 [FREEZER_FROZEN] = "frozen",
115 [FREEZER_THAWING] = "thawing",
116 };
117
118 DEFINE_STRING_TABLE_LOOKUP(freezer_state, FreezerState);
119
120 static const char* const unit_marker_table[_UNIT_MARKER_MAX] = {
121 [UNIT_MARKER_NEEDS_RELOAD] = "needs-reload",
122 [UNIT_MARKER_NEEDS_RESTART] = "needs-restart",
123 };
124
125 DEFINE_STRING_TABLE_LOOKUP(unit_marker, UnitMarker);
126
127 static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = {
128 [AUTOMOUNT_DEAD] = "dead",
129 [AUTOMOUNT_WAITING] = "waiting",
130 [AUTOMOUNT_RUNNING] = "running",
131 [AUTOMOUNT_FAILED] = "failed"
132 };
133
134 DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState);
135
136 static const char* const device_state_table[_DEVICE_STATE_MAX] = {
137 [DEVICE_DEAD] = "dead",
138 [DEVICE_TENTATIVE] = "tentative",
139 [DEVICE_PLUGGED] = "plugged",
140 };
141
142 DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState);
143
144 static const char* const mount_state_table[_MOUNT_STATE_MAX] = {
145 [MOUNT_DEAD] = "dead",
146 [MOUNT_MOUNTING] = "mounting",
147 [MOUNT_MOUNTING_DONE] = "mounting-done",
148 [MOUNT_MOUNTED] = "mounted",
149 [MOUNT_REMOUNTING] = "remounting",
150 [MOUNT_UNMOUNTING] = "unmounting",
151 [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm",
152 [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill",
153 [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm",
154 [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill",
155 [MOUNT_FAILED] = "failed",
156 [MOUNT_CLEANING] = "cleaning",
157 };
158
159 DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState);
160
161 static const char* const path_state_table[_PATH_STATE_MAX] = {
162 [PATH_DEAD] = "dead",
163 [PATH_WAITING] = "waiting",
164 [PATH_RUNNING] = "running",
165 [PATH_FAILED] = "failed"
166 };
167
168 DEFINE_STRING_TABLE_LOOKUP(path_state, PathState);
169
170 static const char* const scope_state_table[_SCOPE_STATE_MAX] = {
171 [SCOPE_DEAD] = "dead",
172 [SCOPE_RUNNING] = "running",
173 [SCOPE_ABANDONED] = "abandoned",
174 [SCOPE_STOP_SIGTERM] = "stop-sigterm",
175 [SCOPE_STOP_SIGKILL] = "stop-sigkill",
176 [SCOPE_FAILED] = "failed",
177 };
178
179 DEFINE_STRING_TABLE_LOOKUP(scope_state, ScopeState);
180
181 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
182 [SERVICE_DEAD] = "dead",
183 [SERVICE_CONDITION] = "condition",
184 [SERVICE_START_PRE] = "start-pre",
185 [SERVICE_START] = "start",
186 [SERVICE_START_POST] = "start-post",
187 [SERVICE_RUNNING] = "running",
188 [SERVICE_EXITED] = "exited",
189 [SERVICE_RELOAD] = "reload",
190 [SERVICE_STOP] = "stop",
191 [SERVICE_STOP_WATCHDOG] = "stop-watchdog",
192 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
193 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
194 [SERVICE_STOP_POST] = "stop-post",
195 [SERVICE_FINAL_WATCHDOG] = "final-watchdog",
196 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
197 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
198 [SERVICE_FAILED] = "failed",
199 [SERVICE_AUTO_RESTART] = "auto-restart",
200 [SERVICE_CLEANING] = "cleaning",
201 };
202
203 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
204
205 static const char* const slice_state_table[_SLICE_STATE_MAX] = {
206 [SLICE_DEAD] = "dead",
207 [SLICE_ACTIVE] = "active"
208 };
209
210 DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState);
211
212 static const char* const socket_state_table[_SOCKET_STATE_MAX] = {
213 [SOCKET_DEAD] = "dead",
214 [SOCKET_START_PRE] = "start-pre",
215 [SOCKET_START_CHOWN] = "start-chown",
216 [SOCKET_START_POST] = "start-post",
217 [SOCKET_LISTENING] = "listening",
218 [SOCKET_RUNNING] = "running",
219 [SOCKET_STOP_PRE] = "stop-pre",
220 [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm",
221 [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill",
222 [SOCKET_STOP_POST] = "stop-post",
223 [SOCKET_FINAL_SIGTERM] = "final-sigterm",
224 [SOCKET_FINAL_SIGKILL] = "final-sigkill",
225 [SOCKET_FAILED] = "failed",
226 [SOCKET_CLEANING] = "cleaning",
227 };
228
229 DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState);
230
231 static const char* const swap_state_table[_SWAP_STATE_MAX] = {
232 [SWAP_DEAD] = "dead",
233 [SWAP_ACTIVATING] = "activating",
234 [SWAP_ACTIVATING_DONE] = "activating-done",
235 [SWAP_ACTIVE] = "active",
236 [SWAP_DEACTIVATING] = "deactivating",
237 [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm",
238 [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill",
239 [SWAP_FAILED] = "failed",
240 [SWAP_CLEANING] = "cleaning",
241 };
242
243 DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState);
244
245 static const char* const target_state_table[_TARGET_STATE_MAX] = {
246 [TARGET_DEAD] = "dead",
247 [TARGET_ACTIVE] = "active"
248 };
249
250 DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState);
251
252 static const char* const timer_state_table[_TIMER_STATE_MAX] = {
253 [TIMER_DEAD] = "dead",
254 [TIMER_WAITING] = "waiting",
255 [TIMER_RUNNING] = "running",
256 [TIMER_ELAPSED] = "elapsed",
257 [TIMER_FAILED] = "failed"
258 };
259
260 DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState);
261
262 static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = {
263 [UNIT_REQUIRES] = "Requires",
264 [UNIT_REQUISITE] = "Requisite",
265 [UNIT_WANTS] = "Wants",
266 [UNIT_BINDS_TO] = "BindsTo",
267 [UNIT_PART_OF] = "PartOf",
268 [UNIT_UPHOLDS] = "Upholds",
269 [UNIT_REQUIRED_BY] = "RequiredBy",
270 [UNIT_REQUISITE_OF] = "RequisiteOf",
271 [UNIT_WANTED_BY] = "WantedBy",
272 [UNIT_BOUND_BY] = "BoundBy",
273 [UNIT_UPHELD_BY] = "UpheldBy",
274 [UNIT_CONSISTS_OF] = "ConsistsOf",
275 [UNIT_CONFLICTS] = "Conflicts",
276 [UNIT_CONFLICTED_BY] = "ConflictedBy",
277 [UNIT_BEFORE] = "Before",
278 [UNIT_AFTER] = "After",
279 [UNIT_ON_SUCCESS] = "OnSuccess",
280 [UNIT_ON_SUCCESS_OF] = "OnSuccessOf",
281 [UNIT_ON_FAILURE] = "OnFailure",
282 [UNIT_ON_FAILURE_OF] = "OnFailureOf",
283 [UNIT_TRIGGERS] = "Triggers",
284 [UNIT_TRIGGERED_BY] = "TriggeredBy",
285 [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo",
286 [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom",
287 [UNIT_PROPAGATES_STOP_TO] = "PropagatesStopTo",
288 [UNIT_STOP_PROPAGATED_FROM] = "StopPropagatedFrom",
289 [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf",
290 [UNIT_REFERENCES] = "References",
291 [UNIT_REFERENCED_BY] = "ReferencedBy",
292 [UNIT_IN_SLICE] = "InSlice",
293 [UNIT_SLICE_OF] = "SliceOf",
294 };
295
296 DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);
297
298 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
299 [NOTIFY_NONE] = "none",
300 [NOTIFY_MAIN] = "main",
301 [NOTIFY_EXEC] = "exec",
302 [NOTIFY_ALL] = "all"
303 };
304
305 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
306
unit_active_state_to_glyph(UnitActiveState state)307 SpecialGlyph unit_active_state_to_glyph(UnitActiveState state) {
308 static const SpecialGlyph map[_UNIT_ACTIVE_STATE_MAX] = {
309 [UNIT_ACTIVE] = SPECIAL_GLYPH_BLACK_CIRCLE,
310 [UNIT_RELOADING] = SPECIAL_GLYPH_CIRCLE_ARROW,
311 [UNIT_INACTIVE] = SPECIAL_GLYPH_WHITE_CIRCLE,
312 [UNIT_FAILED] = SPECIAL_GLYPH_MULTIPLICATION_SIGN,
313 [UNIT_ACTIVATING] = SPECIAL_GLYPH_BLACK_CIRCLE,
314 [UNIT_DEACTIVATING] = SPECIAL_GLYPH_BLACK_CIRCLE,
315 [UNIT_MAINTENANCE] = SPECIAL_GLYPH_WHITE_CIRCLE,
316 };
317
318 if (state < 0)
319 return _SPECIAL_GLYPH_INVALID;
320
321 assert(state < _UNIT_ACTIVE_STATE_MAX);
322 return map[state];
323 }
324