chiark / gitweb /
dbus: export unit file state
[elogind.git] / src / unit.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #ifndef foounithfoo
4 #define foounithfoo
5
6 /***
7   This file is part of systemd.
8
9   Copyright 2010 Lennart Poettering
10
11   systemd is free software; you can redistribute it and/or modify it
12   under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15
16   systemd is distributed in the hope that it will be useful, but
17   WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   General Public License for more details.
20
21   You should have received a copy of the GNU General Public License
22   along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 ***/
24
25 #include <stdbool.h>
26 #include <stdlib.h>
27
28 typedef union Unit Unit;
29 typedef struct Meta Meta;
30 typedef struct UnitVTable UnitVTable;
31 typedef enum UnitType UnitType;
32 typedef enum UnitLoadState UnitLoadState;
33 typedef enum UnitActiveState UnitActiveState;
34 typedef enum UnitDependency UnitDependency;
35
36 #include "set.h"
37 #include "util.h"
38 #include "list.h"
39 #include "socket-util.h"
40 #include "execute.h"
41 #include "condition.h"
42 #include "install.h"
43
44 enum UnitType {
45         UNIT_SERVICE = 0,
46         UNIT_SOCKET,
47         UNIT_TARGET,
48         UNIT_DEVICE,
49         UNIT_MOUNT,
50         UNIT_AUTOMOUNT,
51         UNIT_SNAPSHOT,
52         UNIT_TIMER,
53         UNIT_SWAP,
54         UNIT_PATH,
55         _UNIT_TYPE_MAX,
56         _UNIT_TYPE_INVALID = -1
57 };
58
59 enum UnitLoadState {
60         UNIT_STUB,
61         UNIT_LOADED,
62         UNIT_ERROR,
63         UNIT_MERGED,
64         UNIT_MASKED,
65         _UNIT_LOAD_STATE_MAX,
66         _UNIT_LOAD_STATE_INVALID = -1
67 };
68
69 enum UnitActiveState {
70         UNIT_ACTIVE,
71         UNIT_RELOADING,
72         UNIT_INACTIVE,
73         UNIT_FAILED,
74         UNIT_ACTIVATING,
75         UNIT_DEACTIVATING,
76         _UNIT_ACTIVE_STATE_MAX,
77         _UNIT_ACTIVE_STATE_INVALID = -1
78 };
79
80 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
81         return t == UNIT_ACTIVE || t == UNIT_RELOADING;
82 }
83
84 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
85         return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_RELOADING;
86 }
87
88 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
89         return t == UNIT_INACTIVE || t == UNIT_FAILED || t == UNIT_DEACTIVATING;
90 }
91
92 static inline bool UNIT_IS_INACTIVE_OR_FAILED(UnitActiveState t) {
93         return t == UNIT_INACTIVE || t == UNIT_FAILED;
94 }
95
96 enum UnitDependency {
97         /* Positive dependencies */
98         UNIT_REQUIRES,
99         UNIT_REQUIRES_OVERRIDABLE,
100         UNIT_REQUISITE,
101         UNIT_REQUISITE_OVERRIDABLE,
102         UNIT_WANTS,
103         UNIT_BIND_TO,
104
105         /* Inverse of the above */
106         UNIT_REQUIRED_BY,             /* inverse of 'requires' and 'requisite' is 'required_by' */
107         UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'requires_overridable' and 'requisite_overridable' is 'soft_required_by' */
108         UNIT_WANTED_BY,               /* inverse of 'wants' */
109         UNIT_BOUND_BY,                /* inverse of 'bind_to' */
110
111         /* Negative dependencies */
112         UNIT_CONFLICTS,               /* inverse of 'conflicts' is 'conflicted_by' */
113         UNIT_CONFLICTED_BY,
114
115         /* Order */
116         UNIT_BEFORE,                  /* inverse of 'before' is 'after' and vice versa */
117         UNIT_AFTER,
118
119         /* On Failure */
120         UNIT_ON_FAILURE,
121
122         /* Reference information for GC logic */
123         UNIT_REFERENCES,              /* Inverse of 'references' is 'referenced_by' */
124         UNIT_REFERENCED_BY,
125
126         _UNIT_DEPENDENCY_MAX,
127         _UNIT_DEPENDENCY_INVALID = -1
128 };
129
130 #include "manager.h"
131 #include "job.h"
132 #include "cgroup.h"
133
134 struct Meta {
135         Manager *manager;
136
137         UnitType type;
138         UnitLoadState load_state;
139         Unit *merged_into;
140
141         char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
142         char *instance;
143
144         Set *names;
145         Set *dependencies[_UNIT_DEPENDENCY_MAX];
146
147         char *description;
148
149         char *fragment_path; /* if loaded from a config file this is the primary path to it */
150         usec_t fragment_mtime;
151
152         /* If there is something to do with this unit, then this is
153          * the job for it */
154         Job *job;
155
156         usec_t job_timeout;
157
158         /* Conditions to check */
159         LIST_HEAD(Condition, conditions);
160
161         dual_timestamp condition_timestamp;
162
163         dual_timestamp inactive_exit_timestamp;
164         dual_timestamp active_enter_timestamp;
165         dual_timestamp active_exit_timestamp;
166         dual_timestamp inactive_enter_timestamp;
167
168         /* Counterparts in the cgroup filesystem */
169         CGroupBonding *cgroup_bondings;
170
171         /* Per type list */
172         LIST_FIELDS(Meta, units_by_type);
173
174         /* Load queue */
175         LIST_FIELDS(Meta, load_queue);
176
177         /* D-Bus queue */
178         LIST_FIELDS(Meta, dbus_queue);
179
180         /* Cleanup queue */
181         LIST_FIELDS(Meta, cleanup_queue);
182
183         /* GC queue */
184         LIST_FIELDS(Meta, gc_queue);
185
186         /* Used during GC sweeps */
187         unsigned gc_marker;
188
189         /* When deserializing, temporarily store the job type for this
190          * unit here, if there was a job scheduled */
191         int deserialized_job; /* This is actually of type JobType */
192
193         /* Error code when we didn't manage to load the unit (negative) */
194         int load_error;
195
196         /* Cached unit file state */
197         UnitFileState unit_file_state;
198
199         /* Garbage collect us we nobody wants or requires us anymore */
200         bool stop_when_unneeded;
201
202         /* Create default dependencies */
203         bool default_dependencies;
204
205         /* Refuse manual starting, allow starting only indirectly via dependency. */
206         bool refuse_manual_start;
207
208         /* Don't allow the user to stop this unit manually, allow stopping only indirectly via dependency. */
209         bool refuse_manual_stop;
210
211         /* Allow isolation requests */
212         bool allow_isolate;
213
214         /* Isolate OnFailure unit */
215         bool on_failure_isolate;
216
217         /* Ignore this unit when isolating */
218         bool ignore_on_isolate;
219
220         /* Ignore this unit when snapshotting */
221         bool ignore_on_snapshot;
222
223         /* Did the last condition check suceed? */
224         bool condition_result;
225
226         bool in_load_queue:1;
227         bool in_dbus_queue:1;
228         bool in_cleanup_queue:1;
229         bool in_gc_queue:1;
230
231         bool sent_dbus_new_signal:1;
232
233         bool no_gc:1;
234
235         bool in_audit:1;
236 };
237
238 #include "service.h"
239 #include "timer.h"
240 #include "socket.h"
241 #include "target.h"
242 #include "device.h"
243 #include "mount.h"
244 #include "automount.h"
245 #include "snapshot.h"
246 #include "swap.h"
247 #include "path.h"
248
249 union Unit {
250         Meta meta;
251         Service service;
252         Timer timer;
253         Socket socket;
254         Target target;
255         Device device;
256         Mount mount;
257         Automount automount;
258         Snapshot snapshot;
259         Swap swap;
260         Path path;
261 };
262
263 struct UnitVTable {
264         const char *suffix;
265
266         /* This should reset all type-specific variables. This should
267          * not allocate memory, and is called with zero-initialized
268          * data. It should hence only initialize variables that need
269          * to be set != 0. */
270         void (*init)(Unit *u);
271
272         /* This should free all type-specific variables. It should be
273          * idempotent. */
274         void (*done)(Unit *u);
275
276         /* Actually load data from disk. This may fail, and should set
277          * load_state to UNIT_LOADED, UNIT_MERGED or leave it at
278          * UNIT_STUB if no configuration could be found. */
279         int (*load)(Unit *u);
280
281         /* If a a lot of units got created via enumerate(), this is
282          * where to actually set the state and call unit_notify(). */
283         int (*coldplug)(Unit *u);
284
285         void (*dump)(Unit *u, FILE *f, const char *prefix);
286
287         int (*start)(Unit *u);
288         int (*stop)(Unit *u);
289         int (*reload)(Unit *u);
290
291         int (*kill)(Unit *u, KillWho w, KillMode m, int signo, DBusError *error);
292
293         bool (*can_reload)(Unit *u);
294
295         /* Write all data that cannot be restored from other sources
296          * away using unit_serialize_item() */
297         int (*serialize)(Unit *u, FILE *f, FDSet *fds);
298
299         /* Restore one item from the serialization */
300         int (*deserialize_item)(Unit *u, const char *key, const char *data, FDSet *fds);
301
302         /* Boils down the more complex internal state of this unit to
303          * a simpler one that the engine can understand */
304         UnitActiveState (*active_state)(Unit *u);
305
306         /* Returns the substate specific to this unit type as
307          * string. This is purely information so that we can give the
308          * user a more fine grained explanation in which actual state a
309          * unit is in. */
310         const char* (*sub_state_to_string)(Unit *u);
311
312         /* Return true when there is reason to keep this entry around
313          * even nothing references it and it isn't active in any
314          * way */
315         bool (*check_gc)(Unit *u);
316
317         /* Return true when this unit is suitable for snapshotting */
318         bool (*check_snapshot)(Unit *u);
319
320         void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
321         void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
322         void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
323
324         /* Check whether unit needs a daemon reload */
325         bool (*need_daemon_reload)(Unit *u);
326
327         /* Reset failed state if we are in failed state */
328         void (*reset_failed)(Unit *u);
329
330         /* Called whenever any of the cgroups this unit watches for
331          * ran empty */
332         void (*cgroup_notify_empty)(Unit *u);
333
334         /* Called whenever a process of this unit sends us a message */
335         void (*notify_message)(Unit *u, pid_t pid, char **tags);
336
337         /* Called whenever a name thus Unit registered for comes or
338          * goes away. */
339         void (*bus_name_owner_change)(Unit *u, const char *name, const char *old_owner, const char *new_owner);
340
341         /* Called whenever a bus PID lookup finishes */
342         void (*bus_query_pid_done)(Unit *u, const char *name, pid_t pid);
343
344         /* Called for each message received on the bus */
345         DBusHandlerResult (*bus_message_handler)(Unit *u, DBusConnection *c, DBusMessage *message);
346
347         /* Return the unit this unit is following */
348         Unit *(*following)(Unit *u);
349
350         /* Return the set of units that are following each other */
351         int (*following_set)(Unit *u, Set **s);
352
353         /* This is called for each unit type and should be used to
354          * enumerate existing devices and load them. However,
355          * everything that is loaded here should still stay in
356          * inactive state. It is the job of the coldplug() call above
357          * to put the units into the initial state.  */
358         int (*enumerate)(Manager *m);
359
360         /* Type specific cleanups. */
361         void (*shutdown)(Manager *m);
362
363         /* When sending out PropertiesChanged signal, which properties
364          * shall be invalidated? This is a NUL separated list of
365          * strings, to minimize relocations a little. */
366         const char *bus_invalidating_properties;
367
368         /* The interface name */
369         const char *bus_interface;
370
371         /* Can units of this type have multiple names? */
372         bool no_alias:1;
373
374         /* Instances make no sense for this type */
375         bool no_instances:1;
376
377         /* Exclude from automatic gc */
378         bool no_gc:1;
379
380         /* Show status updates on the console */
381         bool show_status:1;
382 };
383
384 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
385
386 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
387
388 /* For casting a unit into the various unit types */
389 #define DEFINE_CAST(UPPERCASE, MixedCase)                               \
390         static inline MixedCase* UPPERCASE(Unit *u) {                   \
391                 if (_unlikely_(!u || u->meta.type != UNIT_##UPPERCASE)) \
392                         return NULL;                                    \
393                                                                         \
394                 return (MixedCase*) u;                                  \
395         }
396
397 /* For casting the various unit types into a unit */
398 #define UNIT(u) ((Unit*) (&(u)->meta))
399
400 DEFINE_CAST(SOCKET, Socket);
401 DEFINE_CAST(TIMER, Timer);
402 DEFINE_CAST(SERVICE, Service);
403 DEFINE_CAST(TARGET, Target);
404 DEFINE_CAST(DEVICE, Device);
405 DEFINE_CAST(MOUNT, Mount);
406 DEFINE_CAST(AUTOMOUNT, Automount);
407 DEFINE_CAST(SNAPSHOT, Snapshot);
408 DEFINE_CAST(SWAP, Swap);
409 DEFINE_CAST(PATH, Path);
410
411 Unit *unit_new(Manager *m);
412 void unit_free(Unit *u);
413
414 int unit_add_name(Unit *u, const char *name);
415
416 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);
417 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference);
418
419 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
420 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
421
422 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
423 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
424
425 int unit_add_exec_dependencies(Unit *u, ExecContext *c);
426
427 int unit_add_cgroup(Unit *u, CGroupBonding *b);
428 int unit_add_cgroup_from_text(Unit *u, const char *name);
429 int unit_add_default_cgroups(Unit *u);
430 CGroupBonding* unit_get_default_cgroup(Unit *u);
431
432 int unit_choose_id(Unit *u, const char *name);
433 int unit_set_description(Unit *u, const char *description);
434
435 bool unit_check_gc(Unit *u);
436
437 void unit_add_to_load_queue(Unit *u);
438 void unit_add_to_dbus_queue(Unit *u);
439 void unit_add_to_cleanup_queue(Unit *u);
440 void unit_add_to_gc_queue(Unit *u);
441
442 int unit_merge(Unit *u, Unit *other);
443 int unit_merge_by_name(Unit *u, const char *other);
444
445 Unit *unit_follow_merge(Unit *u);
446
447 int unit_load_fragment_and_dropin(Unit *u);
448 int unit_load_fragment_and_dropin_optional(Unit *u);
449 int unit_load(Unit *unit);
450
451 const char *unit_description(Unit *u);
452
453 bool unit_has_name(Unit *u, const char *name);
454
455 UnitActiveState unit_active_state(Unit *u);
456
457 const char* unit_sub_state_to_string(Unit *u);
458
459 void unit_dump(Unit *u, FILE *f, const char *prefix);
460
461 bool unit_can_reload(Unit *u);
462 bool unit_can_start(Unit *u);
463 bool unit_can_isolate(Unit *u);
464
465 int unit_start(Unit *u);
466 int unit_stop(Unit *u);
467 int unit_reload(Unit *u);
468
469 int unit_kill(Unit *u, KillWho w, KillMode m, int signo, DBusError *error);
470
471 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_success);
472
473 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
474 void unit_unwatch_fd(Unit *u, Watch *w);
475
476 int unit_watch_pid(Unit *u, pid_t pid);
477 void unit_unwatch_pid(Unit *u, pid_t pid);
478
479 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
480 void unit_unwatch_timer(Unit *u, Watch *w);
481
482 int unit_watch_bus_name(Unit *u, const char *name);
483 void unit_unwatch_bus_name(Unit *u, const char *name);
484
485 bool unit_job_is_applicable(Unit *u, JobType j);
486
487 int set_unit_path(const char *p);
488
489 char *unit_dbus_path(Unit *u);
490
491 int unit_load_related_unit(Unit *u, const char *type, Unit **_found);
492 int unit_get_related_unit(Unit *u, const char *type, Unit **_found);
493
494 char *unit_name_printf(Unit *u, const char* text);
495 char *unit_full_printf(Unit *u, const char *text);
496 char **unit_full_printf_strv(Unit *u, char **l);
497
498 bool unit_can_serialize(Unit *u);
499 int unit_serialize(Unit *u, FILE *f, FDSet *fds);
500 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *value, ...) _printf_attr_(4,5);
501 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value);
502 int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
503
504 int unit_add_node_link(Unit *u, const char *what, bool wants);
505
506 int unit_coldplug(Unit *u);
507
508 void unit_status_printf(Unit *u, const char *format, ...);
509
510 bool unit_need_daemon_reload(Unit *u);
511
512 void unit_reset_failed(Unit *u);
513
514 Unit *unit_following(Unit *u);
515
516 bool unit_pending_inactive(Unit *u);
517 bool unit_pending_active(Unit *u);
518
519 int unit_add_default_target_dependency(Unit *u, Unit *target);
520
521 int unit_following_set(Unit *u, Set **s);
522
523 UnitType unit_name_to_type(const char *n);
524 bool unit_name_is_valid(const char *n, bool template_ok);
525
526 void unit_trigger_on_failure(Unit *u);
527
528 bool unit_condition_test(Unit *u);
529
530 UnitFileState unit_get_unit_file_state(Unit *u);
531
532 const char *unit_load_state_to_string(UnitLoadState i);
533 UnitLoadState unit_load_state_from_string(const char *s);
534
535 const char *unit_active_state_to_string(UnitActiveState i);
536 UnitActiveState unit_active_state_from_string(const char *s);
537
538 const char *unit_dependency_to_string(UnitDependency i);
539 UnitDependency unit_dependency_from_string(const char *s);
540
541 #endif