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