chiark / gitweb /
c12ce0b482d784542eaf5a1da3ca4af448a64b0d
[elogind.git] / unit.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foounithfoo
4 #define foounithfoo
5
6 #include <stdbool.h>
7 #include <stdlib.h>
8
9 typedef union Unit Unit;
10 typedef struct Meta Meta;
11 typedef struct UnitVTable UnitVTable;
12 typedef enum UnitType UnitType;
13 typedef enum UnitLoadState UnitLoadState;
14 typedef enum UnitActiveState UnitActiveState;
15 typedef enum UnitDependency UnitDependency;
16
17 #include "set.h"
18 #include "util.h"
19 #include "list.h"
20 #include "socket-util.h"
21 #include "execute.h"
22
23 #define UNIT_NAME_MAX 128
24 #define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
25 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
26
27 enum UnitType {
28         UNIT_SERVICE = 0,
29         UNIT_TIMER,
30         UNIT_SOCKET,
31         UNIT_TARGET,
32         UNIT_DEVICE,
33         UNIT_MOUNT,
34         UNIT_AUTOMOUNT,
35         UNIT_SNAPSHOT,
36         _UNIT_TYPE_MAX,
37         _UNIT_TYPE_INVALID = -1,
38 };
39
40 enum UnitLoadState {
41         UNIT_STUB,
42         UNIT_LOADED,
43         UNIT_FAILED,
44         _UNIT_LOAD_STATE_MAX
45 };
46
47 enum UnitActiveState {
48         UNIT_ACTIVE,
49         UNIT_ACTIVE_RELOADING,
50         UNIT_INACTIVE,
51         UNIT_ACTIVATING,
52         UNIT_DEACTIVATING,
53         _UNIT_ACTIVE_STATE_MAX
54 };
55
56 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
57         return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
58 }
59
60 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
61         return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
62 }
63
64 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
65         return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
66 }
67
68 enum UnitDependency {
69         /* Positive dependencies */
70         UNIT_REQUIRES,
71         UNIT_SOFT_REQUIRES,
72         UNIT_WANTS,
73         UNIT_REQUISITE,
74         UNIT_SOFT_REQUISITE,
75
76         /* Inverse of the above */
77         UNIT_REQUIRED_BY,       /* inverse of 'requires' and 'requisite' is 'required_by' */
78         UNIT_SOFT_REQUIRED_BY,  /* inverse of 'soft_requires' and 'soft_requisite' is 'soft_required_by' */
79         UNIT_WANTED_BY,         /* inverse of 'wants' */
80
81         /* Negative dependencies */
82         UNIT_CONFLICTS,         /* inverse of 'conflicts' is 'conflicts' */
83
84         /* Order */
85         UNIT_BEFORE,            /* inverse of before is after and vice versa */
86         UNIT_AFTER,
87
88         _UNIT_DEPENDENCY_MAX,
89         _UNIT_DEPENDENCY_INVALID = -1
90 };
91
92 #include "manager.h"
93 #include "job.h"
94
95 struct Meta {
96         Manager *manager;
97         UnitType type;
98         UnitLoadState load_state;
99
100         char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
101
102         Set *names;
103         Set *dependencies[_UNIT_DEPENDENCY_MAX];
104
105         char *description;
106         char *load_path; /* if loaded from a config file this is the primary path to it */
107
108         /* If there is something to do with this unit, then this is
109          * the job for it */
110         Job *job;
111
112         bool in_load_queue:1;
113
114         /* If we go down, pull down everything that depends on us, too */
115         bool recursive_stop;
116
117         /* Garbage collect us we nobody wants or requires us anymore */
118         bool stop_when_unneeded;
119
120         usec_t active_enter_timestamp;
121         usec_t active_exit_timestamp;
122
123         /* Load queue */
124         LIST_FIELDS(Meta, load_queue);
125
126         /* Per type list */
127         LIST_FIELDS(Meta, units_per_type);
128 };
129
130 #include "service.h"
131 #include "timer.h"
132 #include "socket.h"
133 #include "target.h"
134 #include "device.h"
135 #include "mount.h"
136 #include "automount.h"
137 #include "snapshot.h"
138
139 union Unit {
140         Meta meta;
141         Service service;
142         Timer timer;
143         Socket socket;
144         Target target;
145         Device device;
146         Mount mount;
147         Automount automount;
148         Snapshot snapshot;
149 };
150
151 struct UnitVTable {
152         const char *suffix;
153
154         int (*init)(Unit *u);
155         void (*done)(Unit *u);
156         int (*coldplug)(Unit *u);
157
158         void (*dump)(Unit *u, FILE *f, const char *prefix);
159
160         int (*start)(Unit *u);
161         int (*stop)(Unit *u);
162         int (*reload)(Unit *u);
163
164         bool (*can_reload)(Unit *u);
165
166         /* Boils down the more complex internal state of this unit to
167          * a simpler one that the engine can understand */
168         UnitActiveState (*active_state)(Unit *u);
169
170         void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
171         void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
172         void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
173
174         /* This is called for each unit type and should be used to
175          * enumerate existing devices and load them. However,
176          * everything that is loaded here should still stay in
177          * inactive state. It is the job of the coldplug() call above
178          * to put the units into the initial state.  */
179         int (*enumerate)(Manager *m);
180
181         /* Type specific cleanups. */
182         void (*shutdown)(Manager *m);
183 };
184
185 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
186
187 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
188
189 /* For casting a unit into the various unit types */
190 #define DEFINE_CAST(UPPERCASE, MixedCase)                               \
191         static inline MixedCase* UPPERCASE(Unit *u) {                   \
192                 if (!u || u->meta.type != UNIT_##UPPERCASE)             \
193                         return NULL;                                    \
194                                                                         \
195                 return (MixedCase*) u;                                  \
196         }
197
198 /* For casting the various unit types into a unit */
199 #define UNIT(u) ((Unit*) (u))
200
201 DEFINE_CAST(SOCKET, Socket);
202 DEFINE_CAST(TIMER, Timer);
203 DEFINE_CAST(SERVICE, Service);
204 DEFINE_CAST(TARGET, Target);
205 DEFINE_CAST(DEVICE, Device);
206 DEFINE_CAST(MOUNT, Mount);
207 DEFINE_CAST(AUTOMOUNT, Automount);
208 DEFINE_CAST(SNAPSHOT, Snapshot);
209
210 UnitType unit_name_to_type(const char *n);
211 bool unit_name_is_valid(const char *n);
212 char *unit_name_change_suffix(const char *n, const char *suffix);
213
214 Unit *unit_new(Manager *m);
215 void unit_free(Unit *u);
216
217 int unit_add_name(Unit *u, const char *name);
218 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
219 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name);
220
221 int unit_choose_id(Unit *u, const char *name);
222 int unit_set_description(Unit *u, const char *description);
223
224 void unit_add_to_load_queue(Unit *u);
225
226 int unit_merge(Unit *u, Unit *other);
227
228 int unit_load_fragment_and_dropin(Unit *u);
229 int unit_load(Unit *unit);
230
231 const char* unit_id(Unit *u);
232 const char *unit_description(Unit *u);
233
234 UnitActiveState unit_active_state(Unit *u);
235
236 void unit_dump(Unit *u, FILE *f, const char *prefix);
237
238 bool unit_can_reload(Unit *u);
239 bool unit_can_start(Unit *u);
240
241 int unit_start(Unit *u);
242 int unit_stop(Unit *u);
243 int unit_reload(Unit *u);
244
245 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
246
247 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
248 void unit_unwatch_fd(Unit *u, Watch *w);
249
250 int unit_watch_pid(Unit *u, pid_t pid);
251 void unit_unwatch_pid(Unit *u, pid_t pid);
252
253 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
254 void unit_unwatch_timer(Unit *u, Watch *w);
255
256 bool unit_job_is_applicable(Unit *u, JobType j);
257
258 const char *unit_path(void);
259 int set_unit_path(const char *p);
260
261 char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix);
262
263 #endif