]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
523578aa LP |
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 | ||
ff3f2953 | 9 | char* unit_dbus_path_from_name(const char *name) { |
523578aa LP |
10 | _cleanup_free_ char *e = NULL; |
11 | ||
12 | assert(name); | |
13 | ||
14 | e = bus_label_escape(name); | |
15 | if (!e) | |
16 | return NULL; | |
17 | ||
b910cc72 | 18 | return strjoin("/org/freedesktop/systemd1/unit/", e); |
523578aa LP |
19 | } |
20 | ||
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 | ||
37 | const char* unit_dbus_interface_from_type(UnitType t) { | |
38 | ||
39 | static const char *const table[_UNIT_TYPE_MAX] = { | |
ef674d0e YW |
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", | |
523578aa | 45 | [UNIT_AUTOMOUNT] = "org.freedesktop.systemd1.Automount", |
ef674d0e YW |
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", | |
523578aa LP |
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 | ||
bfd5a068 | 61 | const char* unit_dbus_interface_from_name(const char *name) { |
523578aa LP |
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 | ||
1b17dd90 DDM |
71 | const char* unit_type_to_capitalized_string(UnitType t) { |
72 | const char *di = unit_dbus_interface_from_type(t); | |
73 | if (!di) | |
74 | return NULL; | |
75 | ||
76 | return ASSERT_PTR(startswith(di, "org.freedesktop.systemd1.")); | |
77 | } | |
78 | ||
523578aa | 79 | static const char* const unit_type_table[_UNIT_TYPE_MAX] = { |
ef674d0e YW |
80 | [UNIT_SERVICE] = "service", |
81 | [UNIT_SOCKET] = "socket", | |
82 | [UNIT_TARGET] = "target", | |
83 | [UNIT_DEVICE] = "device", | |
84 | [UNIT_MOUNT] = "mount", | |
523578aa | 85 | [UNIT_AUTOMOUNT] = "automount", |
ef674d0e YW |
86 | [UNIT_SWAP] = "swap", |
87 | [UNIT_TIMER] = "timer", | |
88 | [UNIT_PATH] = "path", | |
89 | [UNIT_SLICE] = "slice", | |
90 | [UNIT_SCOPE] = "scope", | |
523578aa LP |
91 | }; |
92 | ||
93 | DEFINE_STRING_TABLE_LOOKUP(unit_type, UnitType); | |
94 | ||
95 | static const char* const unit_load_state_table[_UNIT_LOAD_STATE_MAX] = { | |
ef674d0e YW |
96 | [UNIT_STUB] = "stub", |
97 | [UNIT_LOADED] = "loaded", | |
98 | [UNIT_NOT_FOUND] = "not-found", | |
c4555ad8 | 99 | [UNIT_BAD_SETTING] = "bad-setting", |
ef674d0e YW |
100 | [UNIT_ERROR] = "error", |
101 | [UNIT_MERGED] = "merged", | |
0966933b | 102 | [UNIT_MASKED] = "masked", |
523578aa LP |
103 | }; |
104 | ||
105 | DEFINE_STRING_TABLE_LOOKUP(unit_load_state, UnitLoadState); | |
106 | ||
107 | static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { | |
ef674d0e YW |
108 | [UNIT_ACTIVE] = "active", |
109 | [UNIT_RELOADING] = "reloading", | |
110 | [UNIT_INACTIVE] = "inactive", | |
111 | [UNIT_FAILED] = "failed", | |
112 | [UNIT_ACTIVATING] = "activating", | |
380dc8b0 | 113 | [UNIT_DEACTIVATING] = "deactivating", |
ef674d0e | 114 | [UNIT_MAINTENANCE] = "maintenance", |
523578aa LP |
115 | }; |
116 | ||
117 | DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); | |
118 | ||
d9e45bc3 | 119 | static const char* const freezer_state_table[_FREEZER_STATE_MAX] = { |
16b6af6a AV |
120 | [FREEZER_RUNNING] = "running", |
121 | [FREEZER_FREEZING] = "freezing", | |
122 | [FREEZER_FREEZING_BY_PARENT] = "freezing-by-parent", | |
123 | [FREEZER_FROZEN] = "frozen", | |
124 | [FREEZER_FROZEN_BY_PARENT] = "frozen-by-parent", | |
125 | [FREEZER_THAWING] = "thawing", | |
d9e45bc3 MS |
126 | }; |
127 | ||
128 | DEFINE_STRING_TABLE_LOOKUP(freezer_state, FreezerState); | |
129 | ||
16b6af6a AV |
130 | /* Maps in-progress freezer states to the corresponding finished state */ |
131 | static const FreezerState freezer_state_finish_table[_FREEZER_STATE_MAX] = { | |
132 | [FREEZER_FREEZING] = FREEZER_FROZEN, | |
133 | [FREEZER_FREEZING_BY_PARENT] = FREEZER_FROZEN_BY_PARENT, | |
134 | [FREEZER_THAWING] = FREEZER_RUNNING, | |
135 | ||
136 | /* Finished states trivially map to themselves */ | |
137 | [FREEZER_RUNNING] = FREEZER_RUNNING, | |
138 | [FREEZER_FROZEN] = FREEZER_FROZEN, | |
139 | [FREEZER_FROZEN_BY_PARENT] = FREEZER_FROZEN_BY_PARENT, | |
140 | }; | |
141 | ||
142 | FreezerState freezer_state_finish(FreezerState state) { | |
143 | assert(state >= 0 && state < _FREEZER_STATE_MAX); | |
144 | return freezer_state_finish_table[state]; | |
145 | } | |
146 | ||
ff68472a ZJS |
147 | static const char* const unit_marker_table[_UNIT_MARKER_MAX] = { |
148 | [UNIT_MARKER_NEEDS_RELOAD] = "needs-reload", | |
149 | [UNIT_MARKER_NEEDS_RESTART] = "needs-restart", | |
150 | }; | |
151 | ||
152 | DEFINE_STRING_TABLE_LOOKUP(unit_marker, UnitMarker); | |
153 | ||
523578aa | 154 | static const char* const automount_state_table[_AUTOMOUNT_STATE_MAX] = { |
ef674d0e | 155 | [AUTOMOUNT_DEAD] = "dead", |
523578aa LP |
156 | [AUTOMOUNT_WAITING] = "waiting", |
157 | [AUTOMOUNT_RUNNING] = "running", | |
0966933b | 158 | [AUTOMOUNT_FAILED] = "failed", |
523578aa LP |
159 | }; |
160 | ||
161 | DEFINE_STRING_TABLE_LOOKUP(automount_state, AutomountState); | |
162 | ||
163 | static const char* const device_state_table[_DEVICE_STATE_MAX] = { | |
ef674d0e | 164 | [DEVICE_DEAD] = "dead", |
523578aa | 165 | [DEVICE_TENTATIVE] = "tentative", |
ef674d0e | 166 | [DEVICE_PLUGGED] = "plugged", |
523578aa LP |
167 | }; |
168 | ||
169 | DEFINE_STRING_TABLE_LOOKUP(device_state, DeviceState); | |
170 | ||
171 | static const char* const mount_state_table[_MOUNT_STATE_MAX] = { | |
ef674d0e YW |
172 | [MOUNT_DEAD] = "dead", |
173 | [MOUNT_MOUNTING] = "mounting", | |
174 | [MOUNT_MOUNTING_DONE] = "mounting-done", | |
175 | [MOUNT_MOUNTED] = "mounted", | |
176 | [MOUNT_REMOUNTING] = "remounting", | |
177 | [MOUNT_UNMOUNTING] = "unmounting", | |
523578aa LP |
178 | [MOUNT_REMOUNTING_SIGTERM] = "remounting-sigterm", |
179 | [MOUNT_REMOUNTING_SIGKILL] = "remounting-sigkill", | |
180 | [MOUNT_UNMOUNTING_SIGTERM] = "unmounting-sigterm", | |
181 | [MOUNT_UNMOUNTING_SIGKILL] = "unmounting-sigkill", | |
ef674d0e YW |
182 | [MOUNT_FAILED] = "failed", |
183 | [MOUNT_CLEANING] = "cleaning", | |
523578aa LP |
184 | }; |
185 | ||
186 | DEFINE_STRING_TABLE_LOOKUP(mount_state, MountState); | |
187 | ||
188 | static const char* const path_state_table[_PATH_STATE_MAX] = { | |
ef674d0e | 189 | [PATH_DEAD] = "dead", |
523578aa LP |
190 | [PATH_WAITING] = "waiting", |
191 | [PATH_RUNNING] = "running", | |
0966933b | 192 | [PATH_FAILED] = "failed", |
523578aa LP |
193 | }; |
194 | ||
195 | DEFINE_STRING_TABLE_LOOKUP(path_state, PathState); | |
196 | ||
197 | static const char* const scope_state_table[_SCOPE_STATE_MAX] = { | |
ef674d0e | 198 | [SCOPE_DEAD] = "dead", |
03860190 | 199 | [SCOPE_START_CHOWN] = "start-chown", |
ef674d0e YW |
200 | [SCOPE_RUNNING] = "running", |
201 | [SCOPE_ABANDONED] = "abandoned", | |
523578aa LP |
202 | [SCOPE_STOP_SIGTERM] = "stop-sigterm", |
203 | [SCOPE_STOP_SIGKILL] = "stop-sigkill", | |
ef674d0e | 204 | [SCOPE_FAILED] = "failed", |
523578aa LP |
205 | }; |
206 | ||
207 | DEFINE_STRING_TABLE_LOOKUP(scope_state, ScopeState); | |
208 | ||
209 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { | |
a1d31573 LP |
210 | [SERVICE_DEAD] = "dead", |
211 | [SERVICE_CONDITION] = "condition", | |
212 | [SERVICE_START_PRE] = "start-pre", | |
213 | [SERVICE_START] = "start", | |
214 | [SERVICE_START_POST] = "start-post", | |
215 | [SERVICE_RUNNING] = "running", | |
216 | [SERVICE_EXITED] = "exited", | |
217 | [SERVICE_RELOAD] = "reload", | |
218 | [SERVICE_RELOAD_SIGNAL] = "reload-signal", | |
219 | [SERVICE_RELOAD_NOTIFY] = "reload-notify", | |
220 | [SERVICE_STOP] = "stop", | |
221 | [SERVICE_STOP_WATCHDOG] = "stop-watchdog", | |
222 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
223 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
224 | [SERVICE_STOP_POST] = "stop-post", | |
225 | [SERVICE_FINAL_WATCHDOG] = "final-watchdog", | |
226 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
227 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
228 | [SERVICE_FAILED] = "failed", | |
229 | [SERVICE_DEAD_BEFORE_AUTO_RESTART] = "dead-before-auto-restart", | |
230 | [SERVICE_FAILED_BEFORE_AUTO_RESTART] = "failed-before-auto-restart", | |
b9c1883a | 231 | [SERVICE_DEAD_RESOURCES_PINNED] = "dead-resources-pinned", |
a1d31573 | 232 | [SERVICE_AUTO_RESTART] = "auto-restart", |
09d04ad3 | 233 | [SERVICE_AUTO_RESTART_QUEUED] = "auto-restart-queued", |
a1d31573 | 234 | [SERVICE_CLEANING] = "cleaning", |
523578aa LP |
235 | }; |
236 | ||
237 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
238 | ||
239 | static const char* const slice_state_table[_SLICE_STATE_MAX] = { | |
ef674d0e | 240 | [SLICE_DEAD] = "dead", |
0966933b | 241 | [SLICE_ACTIVE] = "active", |
523578aa LP |
242 | }; |
243 | ||
244 | DEFINE_STRING_TABLE_LOOKUP(slice_state, SliceState); | |
245 | ||
246 | static const char* const socket_state_table[_SOCKET_STATE_MAX] = { | |
ef674d0e YW |
247 | [SOCKET_DEAD] = "dead", |
248 | [SOCKET_START_PRE] = "start-pre", | |
249 | [SOCKET_START_CHOWN] = "start-chown", | |
250 | [SOCKET_START_POST] = "start-post", | |
251 | [SOCKET_LISTENING] = "listening", | |
252 | [SOCKET_RUNNING] = "running", | |
253 | [SOCKET_STOP_PRE] = "stop-pre", | |
523578aa LP |
254 | [SOCKET_STOP_PRE_SIGTERM] = "stop-pre-sigterm", |
255 | [SOCKET_STOP_PRE_SIGKILL] = "stop-pre-sigkill", | |
ef674d0e YW |
256 | [SOCKET_STOP_POST] = "stop-post", |
257 | [SOCKET_FINAL_SIGTERM] = "final-sigterm", | |
258 | [SOCKET_FINAL_SIGKILL] = "final-sigkill", | |
259 | [SOCKET_FAILED] = "failed", | |
260 | [SOCKET_CLEANING] = "cleaning", | |
523578aa LP |
261 | }; |
262 | ||
263 | DEFINE_STRING_TABLE_LOOKUP(socket_state, SocketState); | |
264 | ||
265 | static const char* const swap_state_table[_SWAP_STATE_MAX] = { | |
ef674d0e YW |
266 | [SWAP_DEAD] = "dead", |
267 | [SWAP_ACTIVATING] = "activating", | |
268 | [SWAP_ACTIVATING_DONE] = "activating-done", | |
269 | [SWAP_ACTIVE] = "active", | |
270 | [SWAP_DEACTIVATING] = "deactivating", | |
523578aa LP |
271 | [SWAP_DEACTIVATING_SIGTERM] = "deactivating-sigterm", |
272 | [SWAP_DEACTIVATING_SIGKILL] = "deactivating-sigkill", | |
ef674d0e YW |
273 | [SWAP_FAILED] = "failed", |
274 | [SWAP_CLEANING] = "cleaning", | |
523578aa LP |
275 | }; |
276 | ||
277 | DEFINE_STRING_TABLE_LOOKUP(swap_state, SwapState); | |
278 | ||
279 | static const char* const target_state_table[_TARGET_STATE_MAX] = { | |
ef674d0e | 280 | [TARGET_DEAD] = "dead", |
0966933b | 281 | [TARGET_ACTIVE] = "active", |
523578aa LP |
282 | }; |
283 | ||
284 | DEFINE_STRING_TABLE_LOOKUP(target_state, TargetState); | |
285 | ||
286 | static const char* const timer_state_table[_TIMER_STATE_MAX] = { | |
ef674d0e | 287 | [TIMER_DEAD] = "dead", |
523578aa LP |
288 | [TIMER_WAITING] = "waiting", |
289 | [TIMER_RUNNING] = "running", | |
290 | [TIMER_ELAPSED] = "elapsed", | |
0966933b | 291 | [TIMER_FAILED] = "failed", |
523578aa LP |
292 | }; |
293 | ||
294 | DEFINE_STRING_TABLE_LOOKUP(timer_state, TimerState); | |
295 | ||
296 | static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { | |
ef674d0e YW |
297 | [UNIT_REQUIRES] = "Requires", |
298 | [UNIT_REQUISITE] = "Requisite", | |
299 | [UNIT_WANTS] = "Wants", | |
300 | [UNIT_BINDS_TO] = "BindsTo", | |
301 | [UNIT_PART_OF] = "PartOf", | |
302 | [UNIT_UPHOLDS] = "Upholds", | |
303 | [UNIT_REQUIRED_BY] = "RequiredBy", | |
304 | [UNIT_REQUISITE_OF] = "RequisiteOf", | |
305 | [UNIT_WANTED_BY] = "WantedBy", | |
306 | [UNIT_BOUND_BY] = "BoundBy", | |
307 | [UNIT_UPHELD_BY] = "UpheldBy", | |
308 | [UNIT_CONSISTS_OF] = "ConsistsOf", | |
309 | [UNIT_CONFLICTS] = "Conflicts", | |
310 | [UNIT_CONFLICTED_BY] = "ConflictedBy", | |
311 | [UNIT_BEFORE] = "Before", | |
312 | [UNIT_AFTER] = "After", | |
313 | [UNIT_ON_SUCCESS] = "OnSuccess", | |
314 | [UNIT_ON_SUCCESS_OF] = "OnSuccessOf", | |
315 | [UNIT_ON_FAILURE] = "OnFailure", | |
316 | [UNIT_ON_FAILURE_OF] = "OnFailureOf", | |
317 | [UNIT_TRIGGERS] = "Triggers", | |
318 | [UNIT_TRIGGERED_BY] = "TriggeredBy", | |
319 | [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo", | |
523578aa | 320 | [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom", |
ef674d0e YW |
321 | [UNIT_PROPAGATES_STOP_TO] = "PropagatesStopTo", |
322 | [UNIT_STOP_PROPAGATED_FROM] = "StopPropagatedFrom", | |
323 | [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf", | |
324 | [UNIT_REFERENCES] = "References", | |
325 | [UNIT_REFERENCED_BY] = "ReferencedBy", | |
326 | [UNIT_IN_SLICE] = "InSlice", | |
327 | [UNIT_SLICE_OF] = "SliceOf", | |
523578aa LP |
328 | }; |
329 | ||
330 | DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency); | |
331 | ||
332 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { | |
333 | [NOTIFY_NONE] = "none", | |
334 | [NOTIFY_MAIN] = "main", | |
335 | [NOTIFY_EXEC] = "exec", | |
0966933b | 336 | [NOTIFY_ALL] = "all", |
523578aa LP |
337 | }; |
338 | ||
339 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
9ae5fed6 J |
340 | |
341 | SpecialGlyph unit_active_state_to_glyph(UnitActiveState state) { | |
eeb6923d LP |
342 | static const SpecialGlyph map[_UNIT_ACTIVE_STATE_MAX] = { |
343 | [UNIT_ACTIVE] = SPECIAL_GLYPH_BLACK_CIRCLE, | |
344 | [UNIT_RELOADING] = SPECIAL_GLYPH_CIRCLE_ARROW, | |
345 | [UNIT_INACTIVE] = SPECIAL_GLYPH_WHITE_CIRCLE, | |
346 | [UNIT_FAILED] = SPECIAL_GLYPH_MULTIPLICATION_SIGN, | |
347 | [UNIT_ACTIVATING] = SPECIAL_GLYPH_BLACK_CIRCLE, | |
348 | [UNIT_DEACTIVATING] = SPECIAL_GLYPH_BLACK_CIRCLE, | |
349 | [UNIT_MAINTENANCE] = SPECIAL_GLYPH_WHITE_CIRCLE, | |
350 | }; | |
351 | ||
352 | if (state < 0) | |
353 | return _SPECIAL_GLYPH_INVALID; | |
354 | ||
355 | assert(state < _UNIT_ACTIVE_STATE_MAX); | |
356 | return map[state]; | |
9ae5fed6 | 357 | } |