chiark / gitweb /
basic: parse_timestamp UTC and fractional seconds support
[elogind.git] / src / basic / unit-name.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <stdbool.h>
25
26 #include "macro.h"
27
28 #define UNIT_NAME_MAX 256
29
30 typedef enum UnitType {
31         UNIT_SERVICE = 0,
32         UNIT_SOCKET,
33         UNIT_BUSNAME,
34         UNIT_TARGET,
35         UNIT_SNAPSHOT,
36         UNIT_DEVICE,
37         UNIT_MOUNT,
38         UNIT_AUTOMOUNT,
39         UNIT_SWAP,
40         UNIT_TIMER,
41         UNIT_PATH,
42         UNIT_SLICE,
43         UNIT_SCOPE,
44         _UNIT_TYPE_MAX,
45         _UNIT_TYPE_INVALID = -1
46 } UnitType;
47
48 /// UNNEEDED by elogind
49 #if 0
50 typedef enum UnitLoadState {
51         UNIT_STUB = 0,
52         UNIT_LOADED,
53         UNIT_NOT_FOUND,
54         UNIT_ERROR,
55         UNIT_MERGED,
56         UNIT_MASKED,
57         _UNIT_LOAD_STATE_MAX,
58         _UNIT_LOAD_STATE_INVALID = -1
59 } UnitLoadState;
60
61 typedef enum UnitActiveState {
62         UNIT_ACTIVE,
63         UNIT_RELOADING,
64         UNIT_INACTIVE,
65         UNIT_FAILED,
66         UNIT_ACTIVATING,
67         UNIT_DEACTIVATING,
68         _UNIT_ACTIVE_STATE_MAX,
69         _UNIT_ACTIVE_STATE_INVALID = -1
70 } UnitActiveState;
71
72 typedef enum AutomountState {
73         AUTOMOUNT_DEAD,
74         AUTOMOUNT_WAITING,
75         AUTOMOUNT_RUNNING,
76         AUTOMOUNT_FAILED,
77         _AUTOMOUNT_STATE_MAX,
78         _AUTOMOUNT_STATE_INVALID = -1
79 } AutomountState;
80
81 typedef enum BusNameState {
82         BUSNAME_DEAD,
83         BUSNAME_MAKING,
84         BUSNAME_REGISTERED,
85         BUSNAME_LISTENING,
86         BUSNAME_RUNNING,
87         BUSNAME_SIGTERM,
88         BUSNAME_SIGKILL,
89         BUSNAME_FAILED,
90         _BUSNAME_STATE_MAX,
91         _BUSNAME_STATE_INVALID = -1
92 } BusNameState;
93
94 /* We simply watch devices, we cannot plug/unplug them. That
95  * simplifies the state engine greatly */
96 typedef enum DeviceState {
97         DEVICE_DEAD,
98         DEVICE_TENTATIVE, /* mounted or swapped, but not (yet) announced by udev */
99         DEVICE_PLUGGED,   /* announced by udev */
100         _DEVICE_STATE_MAX,
101         _DEVICE_STATE_INVALID = -1
102 } DeviceState;
103
104 typedef enum MountState {
105         MOUNT_DEAD,
106         MOUNT_MOUNTING,               /* /usr/bin/mount is running, but the mount is not done yet. */
107         MOUNT_MOUNTING_DONE,          /* /usr/bin/mount is running, and the mount is done. */
108         MOUNT_MOUNTED,
109         MOUNT_REMOUNTING,
110         MOUNT_UNMOUNTING,
111         MOUNT_MOUNTING_SIGTERM,
112         MOUNT_MOUNTING_SIGKILL,
113         MOUNT_REMOUNTING_SIGTERM,
114         MOUNT_REMOUNTING_SIGKILL,
115         MOUNT_UNMOUNTING_SIGTERM,
116         MOUNT_UNMOUNTING_SIGKILL,
117         MOUNT_FAILED,
118         _MOUNT_STATE_MAX,
119         _MOUNT_STATE_INVALID = -1
120 } MountState;
121
122 typedef enum PathState {
123         PATH_DEAD,
124         PATH_WAITING,
125         PATH_RUNNING,
126         PATH_FAILED,
127         _PATH_STATE_MAX,
128         _PATH_STATE_INVALID = -1
129 } PathState;
130
131 typedef enum ScopeState {
132         SCOPE_DEAD,
133         SCOPE_RUNNING,
134         SCOPE_ABANDONED,
135         SCOPE_STOP_SIGTERM,
136         SCOPE_STOP_SIGKILL,
137         SCOPE_FAILED,
138         _SCOPE_STATE_MAX,
139         _SCOPE_STATE_INVALID = -1
140 } ScopeState;
141
142 typedef enum ServiceState {
143         SERVICE_DEAD,
144         SERVICE_START_PRE,
145         SERVICE_START,
146         SERVICE_START_POST,
147         SERVICE_RUNNING,
148         SERVICE_EXITED,            /* Nothing is running anymore, but RemainAfterExit is true hence this is OK */
149         SERVICE_RELOAD,
150         SERVICE_STOP,              /* No STOP_PRE state, instead just register multiple STOP executables */
151         SERVICE_STOP_SIGABRT,      /* Watchdog timeout */
152         SERVICE_STOP_SIGTERM,
153         SERVICE_STOP_SIGKILL,
154         SERVICE_STOP_POST,
155         SERVICE_FINAL_SIGTERM,     /* In case the STOP_POST executable hangs, we shoot that down, too */
156         SERVICE_FINAL_SIGKILL,
157         SERVICE_FAILED,
158         SERVICE_AUTO_RESTART,
159         _SERVICE_STATE_MAX,
160         _SERVICE_STATE_INVALID = -1
161 } ServiceState;
162
163 typedef enum SliceState {
164         SLICE_DEAD,
165         SLICE_ACTIVE,
166         _SLICE_STATE_MAX,
167         _SLICE_STATE_INVALID = -1
168 } SliceState;
169
170 typedef enum SnapshotState {
171         SNAPSHOT_DEAD,
172         SNAPSHOT_ACTIVE,
173         _SNAPSHOT_STATE_MAX,
174         _SNAPSHOT_STATE_INVALID = -1
175 } SnapshotState;
176
177 typedef enum SocketState {
178         SOCKET_DEAD,
179         SOCKET_START_PRE,
180         SOCKET_START_CHOWN,
181         SOCKET_START_POST,
182         SOCKET_LISTENING,
183         SOCKET_RUNNING,
184         SOCKET_STOP_PRE,
185         SOCKET_STOP_PRE_SIGTERM,
186         SOCKET_STOP_PRE_SIGKILL,
187         SOCKET_STOP_POST,
188         SOCKET_FINAL_SIGTERM,
189         SOCKET_FINAL_SIGKILL,
190         SOCKET_FAILED,
191         _SOCKET_STATE_MAX,
192         _SOCKET_STATE_INVALID = -1
193 } SocketState;
194
195 typedef enum SwapState {
196         SWAP_DEAD,
197         SWAP_ACTIVATING,               /* /sbin/swapon is running, but the swap not yet enabled. */
198         SWAP_ACTIVATING_DONE,          /* /sbin/swapon is running, and the swap is done. */
199         SWAP_ACTIVE,
200         SWAP_DEACTIVATING,
201         SWAP_ACTIVATING_SIGTERM,
202         SWAP_ACTIVATING_SIGKILL,
203         SWAP_DEACTIVATING_SIGTERM,
204         SWAP_DEACTIVATING_SIGKILL,
205         SWAP_FAILED,
206         _SWAP_STATE_MAX,
207         _SWAP_STATE_INVALID = -1
208 } SwapState;
209
210
211 typedef enum TargetState {
212         TARGET_DEAD,
213         TARGET_ACTIVE,
214         _TARGET_STATE_MAX,
215         _TARGET_STATE_INVALID = -1
216 } TargetState;
217
218 typedef enum TimerState {
219         TIMER_DEAD,
220         TIMER_WAITING,
221         TIMER_RUNNING,
222         TIMER_ELAPSED,
223         TIMER_FAILED,
224         _TIMER_STATE_MAX,
225         _TIMER_STATE_INVALID = -1
226 } TimerState;
227
228 typedef enum UnitDependency {
229         /* Positive dependencies */
230         UNIT_REQUIRES,
231         UNIT_REQUIRES_OVERRIDABLE,
232         UNIT_REQUISITE,
233         UNIT_REQUISITE_OVERRIDABLE,
234         UNIT_WANTS,
235         UNIT_BINDS_TO,
236         UNIT_PART_OF,
237
238         /* Inverse of the above */
239         UNIT_REQUIRED_BY,             /* inverse of 'requires' is 'required_by' */
240         UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'requires_overridable' is 'required_by_overridable' */
241         UNIT_REQUISITE_OF,            /* inverse of 'requisite' is 'requisite_of' */
242         UNIT_REQUISITE_OF_OVERRIDABLE,/* inverse of 'requisite_overridable' is 'requisite_of_overridable' */
243         UNIT_WANTED_BY,               /* inverse of 'wants' */
244         UNIT_BOUND_BY,                /* inverse of 'binds_to' */
245         UNIT_CONSISTS_OF,             /* inverse of 'part_of' */
246
247         /* Negative dependencies */
248         UNIT_CONFLICTS,               /* inverse of 'conflicts' is 'conflicted_by' */
249         UNIT_CONFLICTED_BY,
250
251         /* Order */
252         UNIT_BEFORE,                  /* inverse of 'before' is 'after' and vice versa */
253         UNIT_AFTER,
254
255         /* On Failure */
256         UNIT_ON_FAILURE,
257
258         /* Triggers (i.e. a socket triggers a service) */
259         UNIT_TRIGGERS,
260         UNIT_TRIGGERED_BY,
261
262         /* Propagate reloads */
263         UNIT_PROPAGATES_RELOAD_TO,
264         UNIT_RELOAD_PROPAGATED_FROM,
265
266         /* Joins namespace of */
267         UNIT_JOINS_NAMESPACE_OF,
268
269         /* Reference information for GC logic */
270         UNIT_REFERENCES,              /* Inverse of 'references' is 'referenced_by' */
271         UNIT_REFERENCED_BY,
272
273         _UNIT_DEPENDENCY_MAX,
274         _UNIT_DEPENDENCY_INVALID = -1
275 } UnitDependency;
276 #endif // 0
277
278 typedef enum UnitNameFlags {
279         UNIT_NAME_PLAIN = 1,      /* Allow foo.service */
280         UNIT_NAME_INSTANCE = 2,   /* Allow foo@bar.service */
281         UNIT_NAME_TEMPLATE = 4,   /* Allow foo@.service */
282         UNIT_NAME_ANY = UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE|UNIT_NAME_TEMPLATE,
283 } UnitNameFlags;
284
285 bool unit_name_is_valid(const char *n, UnitNameFlags flags) _pure_;
286 bool unit_prefix_is_valid(const char *p) _pure_;
287 bool unit_instance_is_valid(const char *i) _pure_;
288 bool unit_suffix_is_valid(const char *s) _pure_;
289
290 /// UNNEEDED by elogind
291 #if 0
292 static inline int unit_prefix_and_instance_is_valid(const char *p) {
293         /* For prefix+instance and instance the same rules apply */
294         return unit_instance_is_valid(p);
295 }
296 #endif // 0
297
298 // UNNEEDED int unit_name_to_prefix(const char *n, char **prefix);
299 // UNNEEDED int unit_name_to_instance(const char *n, char **instance);
300 // UNNEEDED int unit_name_to_prefix_and_instance(const char *n, char **ret);
301
302 // UNNEEDED UnitType unit_name_to_type(const char *n) _pure_;
303
304 // UNNEEDED int unit_name_change_suffix(const char *n, const char *suffix, char **ret);
305
306 int unit_name_build(const char *prefix, const char *instance, const char *suffix, char **ret);
307
308 // UNNEEDED char *unit_name_escape(const char *f);
309 // UNNEEDED int unit_name_unescape(const char *f, char **ret);
310 // UNNEEDED int unit_name_path_escape(const char *f, char **ret);
311 // UNNEEDED int unit_name_path_unescape(const char *f, char **ret);
312
313 // UNNEEDED int unit_name_replace_instance(const char *f, const char *i, char **ret);
314
315 // UNNEEDED int unit_name_template(const char *f, char **ret);
316
317 // UNNEEDED int unit_name_from_path(const char *path, const char *suffix, char **ret);
318 // UNNEEDED int unit_name_from_path_instance(const char *prefix, const char *path, const char *suffix, char **ret);
319 // UNNEEDED int unit_name_to_path(const char *name, char **ret);
320
321 // UNNEEDED char *unit_dbus_path_from_name(const char *name);
322 // UNNEEDED int unit_name_from_dbus_path(const char *path, char **name);
323
324 // UNNEEDED const char* unit_dbus_interface_from_type(UnitType t);
325 // UNNEEDED const char *unit_dbus_interface_from_name(const char *name);
326
327 /// UNNEEDED by elogind
328 #if 0
329 typedef enum UnitNameMangle {
330         UNIT_NAME_NOGLOB,
331         UNIT_NAME_GLOB,
332 } UnitNameMangle;
333
334 int unit_name_mangle_with_suffix(const char *name, UnitNameMangle allow_globs, const char *suffix, char **ret);
335
336 static inline int unit_name_mangle(const char *name, UnitNameMangle allow_globs, char **ret) {
337         return unit_name_mangle_with_suffix(name, allow_globs, ".service", ret);
338 }
339 #endif // 0
340
341 // UNNEEDED int slice_build_parent_slice(const char *slice, char **ret);
342 int slice_build_subslice(const char *slice, const char*name, char **subslice);
343 bool slice_name_is_valid(const char *name);
344
345 const char *unit_type_to_string(UnitType i) _const_;
346 UnitType unit_type_from_string(const char *s) _pure_;
347
348 // UNNEEDED const char *unit_load_state_to_string(UnitLoadState i) _const_;
349 // UNNEEDED UnitLoadState unit_load_state_from_string(const char *s) _pure_;
350
351 // UNNEEDED const char *unit_active_state_to_string(UnitActiveState i) _const_;
352 // UNNEEDED UnitActiveState unit_active_state_from_string(const char *s) _pure_;
353
354 // UNNEEDED const char* automount_state_to_string(AutomountState i) _const_;
355 // UNNEEDED AutomountState automount_state_from_string(const char *s) _pure_;
356
357 // UNNEEDED const char* busname_state_to_string(BusNameState i) _const_;
358 // UNNEEDED BusNameState busname_state_from_string(const char *s) _pure_;
359
360 // UNNEEDED const char* device_state_to_string(DeviceState i) _const_;
361 // UNNEEDED DeviceState device_state_from_string(const char *s) _pure_;
362
363 // UNNEEDED const char* mount_state_to_string(MountState i) _const_;
364 // UNNEEDED MountState mount_state_from_string(const char *s) _pure_;
365
366 // UNNEEDED const char* path_state_to_string(PathState i) _const_;
367 // UNNEEDED PathState path_state_from_string(const char *s) _pure_;
368
369 // UNNEEDED const char* scope_state_to_string(ScopeState i) _const_;
370 // UNNEEDED ScopeState scope_state_from_string(const char *s) _pure_;
371
372 // UNNEEDED const char* service_state_to_string(ServiceState i) _const_;
373 // UNNEEDED ServiceState service_state_from_string(const char *s) _pure_;
374
375 // UNNEEDED const char* slice_state_to_string(SliceState i) _const_;
376 // UNNEEDED SliceState slice_state_from_string(const char *s) _pure_;
377
378 // UNNEEDED const char* snapshot_state_to_string(SnapshotState i) _const_;
379 // UNNEEDED SnapshotState snapshot_state_from_string(const char *s) _pure_;
380
381 // UNNEEDED const char* socket_state_to_string(SocketState i) _const_;
382 // UNNEEDED SocketState socket_state_from_string(const char *s) _pure_;
383
384 // UNNEEDED const char* swap_state_to_string(SwapState i) _const_;
385 // UNNEEDED SwapState swap_state_from_string(const char *s) _pure_;
386
387 // UNNEEDED const char* target_state_to_string(TargetState i) _const_;
388 // UNNEEDED TargetState target_state_from_string(const char *s) _pure_;
389
390 // UNNEEDED const char *timer_state_to_string(TimerState i) _const_;
391 // UNNEEDED TimerState timer_state_from_string(const char *s) _pure_;
392
393 // UNNEEDED const char *unit_dependency_to_string(UnitDependency i) _const_;
394 // UNNEEDED UnitDependency unit_dependency_from_string(const char *s) _pure_;