chiark / gitweb /
afae580409d90a0bb753b2ad29349cf607a7206c
[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
42 #define DEFAULT_TIMEOUT_USEC (60*USEC_PER_SEC)
43 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
44
45 enum UnitType {
46         UNIT_SERVICE = 0,
47         UNIT_SOCKET,
48         UNIT_TARGET,
49         UNIT_DEVICE,
50         UNIT_MOUNT,
51         UNIT_AUTOMOUNT,
52         UNIT_SNAPSHOT,
53         UNIT_TIMER,
54         UNIT_SWAP,
55         UNIT_PATH,
56         _UNIT_TYPE_MAX,
57         _UNIT_TYPE_INVALID = -1
58 };
59
60 enum UnitLoadState {
61         UNIT_STUB,
62         UNIT_LOADED,
63         UNIT_ERROR,
64         UNIT_MERGED,
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
104         /* Inverse of the above */
105         UNIT_REQUIRED_BY,             /* inverse of 'requires' and 'requisite' is 'required_by' */
106         UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'soft_requires' and 'soft_requisite' is 'soft_required_by' */
107         UNIT_WANTED_BY,               /* inverse of 'wants' */
108
109         /* Negative dependencies */
110         UNIT_CONFLICTS,               /* inverse of 'conflicts' is 'conflicted_by' */
111         UNIT_CONFLICTED_BY,
112
113         /* Order */
114         UNIT_BEFORE,                  /* inverse of 'before' is 'after' and vice versa */
115         UNIT_AFTER,
116
117         /* On Failure */
118         UNIT_ON_FAILURE,
119
120         /* Reference information for GC logic */
121         UNIT_REFERENCES,              /* Inverse of 'references' is 'referenced_by' */
122         UNIT_REFERENCED_BY,
123
124         _UNIT_DEPENDENCY_MAX,
125         _UNIT_DEPENDENCY_INVALID = -1
126 };
127
128 #include "manager.h"
129 #include "job.h"
130 #include "cgroup.h"
131
132 struct Meta {
133         Manager *manager;
134
135         UnitType type;
136         UnitLoadState load_state;
137         Unit *merged_into;
138
139         char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
140         char *instance;
141
142         Set *names;
143         Set *dependencies[_UNIT_DEPENDENCY_MAX];
144
145         char *description;
146
147         char *fragment_path; /* if loaded from a config file this is the primary path to it */
148         usec_t fragment_mtime;
149
150         /* If there is something to do with this unit, then this is
151          * the job for it */
152         Job *job;
153
154         usec_t job_timeout;
155
156         dual_timestamp inactive_exit_timestamp;
157         dual_timestamp active_enter_timestamp;
158         dual_timestamp active_exit_timestamp;
159         dual_timestamp inactive_enter_timestamp;
160
161         /* Counterparts in the cgroup filesystem */
162         CGroupBonding *cgroup_bondings;
163
164         /* Per type list */
165         LIST_FIELDS(Meta, units_per_type);
166
167         /* Load queue */
168         LIST_FIELDS(Meta, load_queue);
169
170         /* D-Bus queue */
171         LIST_FIELDS(Meta, dbus_queue);
172
173         /* Cleanup queue */
174         LIST_FIELDS(Meta, cleanup_queue);
175
176         /* GC queue */
177         LIST_FIELDS(Meta, gc_queue);
178
179         /* Used during GC sweeps */
180         unsigned gc_marker;
181
182         /* When deserializing, temporarily store the job type for this
183          * unit here, if there was a job scheduled */
184         int deserialized_job; /* This is actually of type JobType */
185
186         /* Error code when we didn't manage to load the unit (negative) */
187         int load_error;
188
189         /* If we go down, pull down everything that depends on us, too */
190         bool recursive_stop;
191
192         /* Garbage collect us we nobody wants or requires us anymore */
193         bool stop_when_unneeded;
194
195         /* Create default depedencies */
196         bool default_dependencies;
197
198         /* Bring up this unit even if a dependency fails to start */
199         bool ignore_dependency_failure;
200
201         /* Refuse manual starting, allow starting only indirectly via dependency. */
202         bool refuse_manual_start;
203
204         /* Don't allow the user to stop this unit manually, allow stopping only indirectly via dependency. */
205         bool refuse_manual_stop;
206
207         /* Allow isolation requests */
208         bool allow_isolate;
209
210         bool in_load_queue:1;
211         bool in_dbus_queue:1;
212         bool in_cleanup_queue:1;
213         bool in_gc_queue:1;
214
215         bool sent_dbus_new_signal:1;
216
217         bool no_gc:1;
218
219         bool in_audit:1;
220 };
221
222 #include "service.h"
223 #include "timer.h"
224 #include "socket.h"
225 #include "target.h"
226 #include "device.h"
227 #include "mount.h"
228 #include "automount.h"
229 #include "snapshot.h"
230 #include "swap.h"
231 #include "path.h"
232
233 union Unit {
234         Meta meta;
235         Service service;
236         Timer timer;
237         Socket socket;
238         Target target;
239         Device device;
240         Mount mount;
241         Automount automount;
242         Snapshot snapshot;
243         Swap swap;
244         Path path;
245 };
246
247 struct UnitVTable {
248         const char *suffix;
249
250         /* This should reset all type-specific variables. This should
251          * not allocate memory, and is called with zero-initialized
252          * data. It should hence only initialize variables that need
253          * to be set != 0. */
254         void (*init)(Unit *u);
255
256         /* This should free all type-specific variables. It should be
257          * idempotent. */
258         void (*done)(Unit *u);
259
260         /* Actually load data from disk. This may fail, and should set
261          * load_state to UNIT_LOADED, UNIT_MERGED or leave it at
262          * UNIT_STUB if no configuration could be found. */
263         int (*load)(Unit *u);
264
265         /* If a a lot of units got created via enumerate(), this is
266          * where to actually set the state and call unit_notify(). */
267         int (*coldplug)(Unit *u);
268
269         void (*dump)(Unit *u, FILE *f, const char *prefix);
270
271         int (*start)(Unit *u);
272         int (*stop)(Unit *u);
273         int (*reload)(Unit *u);
274
275         bool (*can_reload)(Unit *u);
276
277         /* Write all data that cannot be restored from other sources
278          * away using unit_serialize_item() */
279         int (*serialize)(Unit *u, FILE *f, FDSet *fds);
280
281         /* Restore one item from the serialization */
282         int (*deserialize_item)(Unit *u, const char *key, const char *data, FDSet *fds);
283
284         /* Boils down the more complex internal state of this unit to
285          * a simpler one that the engine can understand */
286         UnitActiveState (*active_state)(Unit *u);
287
288         /* Returns the substate specific to this unit type as
289          * string. This is purely information so that we can give the
290          * user a more finegrained explanation in which actual state a
291          * unit is in. */
292         const char* (*sub_state_to_string)(Unit *u);
293
294         /* Return true when there is reason to keep this entry around
295          * even nothing references it and it isn't active in any
296          * way */
297         bool (*check_gc)(Unit *u);
298
299         /* Return true when this unit is suitable for snapshotting */
300         bool (*check_snapshot)(Unit *u);
301
302         void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
303         void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
304         void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
305
306         /* Reset failed state if we are in failed state */
307         void (*reset_failed)(Unit *u);
308
309         /* Called whenever any of the cgroups this unit watches for
310          * ran empty */
311         void (*cgroup_notify_empty)(Unit *u);
312
313         /* Called whenever a process of this unit sends us a message */
314         void (*notify_message)(Unit *u, pid_t pid, char **tags);
315
316         /* Called whenever a name thus Unit registered for comes or
317          * goes away. */
318         void (*bus_name_owner_change)(Unit *u, const char *name, const char *old_owner, const char *new_owner);
319
320         /* Called whenever a bus PID lookup finishes */
321         void (*bus_query_pid_done)(Unit *u, const char *name, pid_t pid);
322
323         /* Called for each message received on the bus */
324         DBusHandlerResult (*bus_message_handler)(Unit *u, DBusConnection *c, DBusMessage *message);
325
326         /* Return the unit this unit is following */
327         Unit *(*following)(Unit *u);
328
329         /* This is called for each unit type and should be used to
330          * enumerate existing devices and load them. However,
331          * everything that is loaded here should still stay in
332          * inactive state. It is the job of the coldplug() call above
333          * to put the units into the initial state.  */
334         int (*enumerate)(Manager *m);
335
336         /* Type specific cleanups. */
337         void (*shutdown)(Manager *m);
338
339         /* When sending out PropertiesChanged signal, which properties
340          * shall be invalidated? This is a NUL separated list of
341          * strings, to minimize relocations a little. */
342         const char *bus_invalidating_properties;
343
344         /* The interface name */
345         const char *bus_interface;
346
347         /* Can units of this type have multiple names? */
348         bool no_alias:1;
349
350         /* If true units of this types can never have "Requires"
351          * dependencies, because state changes can only be observed,
352          * not triggered */
353         bool no_requires:1;
354
355         /* Instances make no sense for this type */
356         bool no_instances:1;
357
358         /* Exclude this type from snapshots */
359         bool no_snapshots:1;
360
361         /* Exclude from automatic gc */
362         bool no_gc:1;
363
364         /* Exclude from stopping on isolation requests */
365         bool no_isolate:1;
366
367         /* Show status updates on the console */
368         bool show_status:1;
369 };
370
371 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
372
373 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
374
375 /* For casting a unit into the various unit types */
376 #define DEFINE_CAST(UPPERCASE, MixedCase)                               \
377         static inline MixedCase* UPPERCASE(Unit *u) {                   \
378                 if (_unlikely_(!u || u->meta.type != UNIT_##UPPERCASE)) \
379                         return NULL;                                    \
380                                                                         \
381                 return (MixedCase*) u;                                  \
382         }
383
384 /* For casting the various unit types into a unit */
385 #define UNIT(u) ((Unit*) (&(u)->meta))
386
387 DEFINE_CAST(SOCKET, Socket);
388 DEFINE_CAST(TIMER, Timer);
389 DEFINE_CAST(SERVICE, Service);
390 DEFINE_CAST(TARGET, Target);
391 DEFINE_CAST(DEVICE, Device);
392 DEFINE_CAST(MOUNT, Mount);
393 DEFINE_CAST(AUTOMOUNT, Automount);
394 DEFINE_CAST(SNAPSHOT, Snapshot);
395 DEFINE_CAST(SWAP, Swap);
396 DEFINE_CAST(PATH, Path);
397
398 Unit *unit_new(Manager *m);
399 void unit_free(Unit *u);
400
401 int unit_add_name(Unit *u, const char *name);
402
403 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);
404 int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference);
405
406 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
407 int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
408
409 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
410 int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference);
411
412 int unit_add_exec_dependencies(Unit *u, ExecContext *c);
413
414 int unit_add_cgroup(Unit *u, CGroupBonding *b);
415 int unit_add_cgroup_from_text(Unit *u, const char *name);
416 int unit_add_default_cgroup(Unit *u);
417 CGroupBonding* unit_get_default_cgroup(Unit *u);
418
419 int unit_choose_id(Unit *u, const char *name);
420 int unit_set_description(Unit *u, const char *description);
421
422 bool unit_check_gc(Unit *u);
423
424 void unit_add_to_load_queue(Unit *u);
425 void unit_add_to_dbus_queue(Unit *u);
426 void unit_add_to_cleanup_queue(Unit *u);
427 void unit_add_to_gc_queue(Unit *u);
428
429 int unit_merge(Unit *u, Unit *other);
430 int unit_merge_by_name(Unit *u, const char *other);
431
432 Unit *unit_follow_merge(Unit *u);
433
434 int unit_load_fragment_and_dropin(Unit *u);
435 int unit_load_fragment_and_dropin_optional(Unit *u);
436 int unit_load(Unit *unit);
437
438 const char *unit_description(Unit *u);
439
440 bool unit_has_name(Unit *u, const char *name);
441
442 UnitActiveState unit_active_state(Unit *u);
443
444 const char* unit_sub_state_to_string(Unit *u);
445
446 void unit_dump(Unit *u, FILE *f, const char *prefix);
447
448 bool unit_can_reload(Unit *u);
449 bool unit_can_start(Unit *u);
450 bool unit_can_isolate(Unit *u);
451
452 int unit_start(Unit *u);
453 int unit_stop(Unit *u);
454 int unit_reload(Unit *u);
455
456 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
457
458 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
459 void unit_unwatch_fd(Unit *u, Watch *w);
460
461 int unit_watch_pid(Unit *u, pid_t pid);
462 void unit_unwatch_pid(Unit *u, pid_t pid);
463
464 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
465 void unit_unwatch_timer(Unit *u, Watch *w);
466
467 int unit_watch_bus_name(Unit *u, const char *name);
468 void unit_unwatch_bus_name(Unit *u, const char *name);
469
470 bool unit_job_is_applicable(Unit *u, JobType j);
471
472 int set_unit_path(const char *p);
473
474 char *unit_dbus_path(Unit *u);
475
476 int unit_load_related_unit(Unit *u, const char *type, Unit **_found);
477 int unit_get_related_unit(Unit *u, const char *type, Unit **_found);
478
479 char *unit_name_printf(Unit *u, const char* text);
480 char *unit_full_printf(Unit *u, const char *text);
481 char **unit_full_printf_strv(Unit *u, char **l);
482
483 bool unit_can_serialize(Unit *u);
484 int unit_serialize(Unit *u, FILE *f, FDSet *fds);
485 void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *value, ...) _printf_attr_(4,5);
486 void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value);
487 int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
488
489 int unit_add_node_link(Unit *u, const char *what, bool wants);
490
491 int unit_coldplug(Unit *u);
492
493 void unit_status_printf(Unit *u, const char *format, ...);
494
495 bool unit_need_daemon_reload(Unit *u);
496
497 void unit_reset_failed(Unit *u);
498
499 Unit *unit_following(Unit *u);
500
501 bool unit_pending_inactive(Unit *u);
502 bool unit_pending_active(Unit *u);
503
504 int unit_add_default_target_dependency(Unit *u, Unit *target);
505
506 UnitType unit_name_to_type(const char *n);
507 bool unit_name_is_valid(const char *n);
508
509 const char *unit_load_state_to_string(UnitLoadState i);
510 UnitLoadState unit_load_state_from_string(const char *s);
511
512 const char *unit_active_state_to_string(UnitActiveState i);
513 UnitActiveState unit_active_state_from_string(const char *s);
514
515 const char *unit_dependency_to_string(UnitDependency i);
516 UnitDependency unit_dependency_from_string(const char *s);
517
518 const char *kill_mode_to_string(KillMode k);
519 KillMode kill_mode_from_string(const char *s);
520
521 #endif