chiark / gitweb /
2433cc7d7cf7e1ca6afcf436d63e898a6e2f358c
[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         usec_t active_enter_timestamp;
115         usec_t active_exit_timestamp;
116
117         /* Load queue */
118         LIST_FIELDS(Meta, load_queue);
119
120         /* Per type list */
121         LIST_FIELDS(Meta, units_per_type);
122 };
123
124 #include "service.h"
125 #include "timer.h"
126 #include "socket.h"
127 #include "target.h"
128 #include "device.h"
129 #include "mount.h"
130 #include "automount.h"
131 #include "snapshot.h"
132
133 union Unit {
134         Meta meta;
135         Service service;
136         Timer timer;
137         Socket socket;
138         Target target;
139         Device device;
140         Mount mount;
141         Automount automount;
142         Snapshot snapshot;
143 };
144
145 struct UnitVTable {
146         const char *suffix;
147
148         int (*init)(Unit *u);
149         void (*done)(Unit *u);
150         int (*coldplug)(Unit *u);
151
152         void (*dump)(Unit *u, FILE *f, const char *prefix);
153
154         int (*start)(Unit *u);
155         int (*stop)(Unit *u);
156         int (*reload)(Unit *u);
157
158         bool (*can_reload)(Unit *u);
159
160         /* Boils down the more complex internal state of this unit to
161          * a simpler one that the engine can understand */
162         UnitActiveState (*active_state)(Unit *u);
163
164         void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
165         void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
166         void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
167
168         /* This is called for each unit type and should be used to
169          * enumerate existing devices and load them. However,
170          * everything that is loaded here should still stay in
171          * inactive state. It is the job of the coldplug() call above
172          * to put the units into the initial state.  */
173         int (*enumerate)(Manager *m);
174
175         /* Type specific cleanups. */
176         void (*shutdown)(Manager *m);
177 };
178
179 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
180
181 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
182
183 /* For casting a unit into the various unit types */
184 #define DEFINE_CAST(UPPERCASE, MixedCase)                               \
185         static inline MixedCase* UPPERCASE(Unit *u) {                   \
186                 if (!u || u->meta.type != UNIT_##UPPERCASE)             \
187                         return NULL;                                    \
188                                                                         \
189                 return (MixedCase*) u;                                  \
190         }
191
192 /* For casting the various unit types into a unit */
193 #define UNIT(u) ((Unit*) (u))
194
195 DEFINE_CAST(SOCKET, Socket);
196 DEFINE_CAST(TIMER, Timer);
197 DEFINE_CAST(SERVICE, Service);
198 DEFINE_CAST(TARGET, Target);
199 DEFINE_CAST(DEVICE, Device);
200 DEFINE_CAST(MOUNT, Mount);
201 DEFINE_CAST(AUTOMOUNT, Automount);
202 DEFINE_CAST(SNAPSHOT, Snapshot);
203
204 UnitType unit_name_to_type(const char *n);
205 bool unit_name_is_valid(const char *n);
206 char *unit_name_change_suffix(const char *n, const char *suffix);
207
208 Unit *unit_new(Manager *m);
209 void unit_free(Unit *u);
210
211 int unit_add_name(Unit *u, const char *name);
212 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
213 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name);
214
215 int unit_choose_id(Unit *u, const char *name);
216 int unit_set_description(Unit *u, const char *description);
217
218 void unit_add_to_load_queue(Unit *u);
219
220 int unit_merge(Unit *u, Unit *other);
221
222 int unit_load_fragment_and_dropin(Unit *u);
223 int unit_load(Unit *unit);
224
225 const char* unit_id(Unit *u);
226 const char *unit_description(Unit *u);
227
228 UnitActiveState unit_active_state(Unit *u);
229
230 void unit_dump(Unit *u, FILE *f, const char *prefix);
231
232 bool unit_can_reload(Unit *u);
233 bool unit_can_start(Unit *u);
234
235 int unit_start(Unit *u);
236 int unit_stop(Unit *u);
237 int unit_reload(Unit *u);
238
239 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
240
241 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
242 void unit_unwatch_fd(Unit *u, Watch *w);
243
244 int unit_watch_pid(Unit *u, pid_t pid);
245 void unit_unwatch_pid(Unit *u, pid_t pid);
246
247 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
248 void unit_unwatch_timer(Unit *u, Watch *w);
249
250 bool unit_job_is_applicable(Unit *u, JobType j);
251
252 const char *unit_path(void);
253 int set_unit_path(const char *p);
254
255 char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix);
256
257 #endif