]> git.ipfire.org Git - people/ms/systemd.git/blob - unit.h
942de5f36f484950a150d5fb57aa16a3dbeb64ee
[people/ms/systemd.git] / unit.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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 UNIT_NAME_MAX 128
43 #define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
44 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
45
46 enum UnitType {
47 UNIT_SERVICE = 0,
48 UNIT_TIMER,
49 UNIT_SOCKET,
50 UNIT_TARGET,
51 UNIT_DEVICE,
52 UNIT_MOUNT,
53 UNIT_AUTOMOUNT,
54 UNIT_SNAPSHOT,
55 _UNIT_TYPE_MAX,
56 _UNIT_TYPE_INVALID = -1,
57 };
58
59 enum UnitLoadState {
60 UNIT_STUB,
61 UNIT_LOADED,
62 UNIT_FAILED,
63 UNIT_MERGED,
64 _UNIT_LOAD_STATE_MAX,
65 _UNIT_LOAD_STATE_INVALID = -1
66 };
67
68 enum UnitActiveState {
69 UNIT_ACTIVE,
70 UNIT_ACTIVE_RELOADING,
71 UNIT_INACTIVE,
72 UNIT_ACTIVATING,
73 UNIT_DEACTIVATING,
74 _UNIT_ACTIVE_STATE_MAX,
75 _UNIT_ACTIVE_STATE_INVALID = -1
76 };
77
78 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
79 return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
80 }
81
82 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
83 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
84 }
85
86 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
87 return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
88 }
89
90 enum UnitDependency {
91 /* Positive dependencies */
92 UNIT_REQUIRES,
93 UNIT_SOFT_REQUIRES,
94 UNIT_WANTS,
95 UNIT_REQUISITE,
96 UNIT_SOFT_REQUISITE,
97
98 /* Inverse of the above */
99 UNIT_REQUIRED_BY, /* inverse of 'requires' and 'requisite' is 'required_by' */
100 UNIT_SOFT_REQUIRED_BY, /* inverse of 'soft_requires' and 'soft_requisite' is 'soft_required_by' */
101 UNIT_WANTED_BY, /* inverse of 'wants' */
102
103 /* Negative dependencies */
104 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
105
106 /* Order */
107 UNIT_BEFORE, /* inverse of before is after and vice versa */
108 UNIT_AFTER,
109
110 _UNIT_DEPENDENCY_MAX,
111 _UNIT_DEPENDENCY_INVALID = -1
112 };
113
114 #include "manager.h"
115 #include "job.h"
116 #include "cgroup.h"
117
118 struct Meta {
119 Manager *manager;
120
121 UnitType type;
122 UnitLoadState load_state;
123 Unit *merged_into;
124
125 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
126
127 Set *names;
128 Set *dependencies[_UNIT_DEPENDENCY_MAX];
129
130 char *description;
131 char *fragment_path; /* if loaded from a config file this is the primary path to it */
132
133 /* If there is something to do with this unit, then this is
134 * the job for it */
135 Job *job;
136
137 bool in_load_queue:1;
138 bool in_dbus_queue:1;
139 bool in_cleanup_queue:1;
140 bool sent_dbus_new_signal:1;
141
142 /* If we go down, pull down everything that depends on us, too */
143 bool recursive_stop;
144
145 /* Garbage collect us we nobody wants or requires us anymore */
146 bool stop_when_unneeded;
147
148 usec_t active_enter_timestamp;
149 usec_t active_exit_timestamp;
150
151 /* Counterparts in the cgroup filesystem */
152 CGroupBonding *cgroup_bondings;
153
154 /* Load queue */
155 LIST_FIELDS(Meta, load_queue);
156
157 /* Per type list */
158 LIST_FIELDS(Meta, units_per_type);
159
160 /* D-Bus queue */
161 LIST_FIELDS(Meta, dbus_queue);
162
163 /* Cleanup queue */
164 LIST_FIELDS(Meta, cleanup_queue);
165 };
166
167 #include "service.h"
168 #include "timer.h"
169 #include "socket.h"
170 #include "target.h"
171 #include "device.h"
172 #include "mount.h"
173 #include "automount.h"
174 #include "snapshot.h"
175
176 union Unit {
177 Meta meta;
178 Service service;
179 Timer timer;
180 Socket socket;
181 Target target;
182 Device device;
183 Mount mount;
184 Automount automount;
185 Snapshot snapshot;
186 };
187
188 struct UnitVTable {
189 const char *suffix;
190
191 int (*init)(Unit *u, UnitLoadState *new_state);
192 void (*done)(Unit *u);
193 int (*coldplug)(Unit *u);
194
195 void (*dump)(Unit *u, FILE *f, const char *prefix);
196
197 int (*start)(Unit *u);
198 int (*stop)(Unit *u);
199 int (*reload)(Unit *u);
200
201 bool (*can_reload)(Unit *u);
202
203 /* Boils down the more complex internal state of this unit to
204 * a simpler one that the engine can understand */
205 UnitActiveState (*active_state)(Unit *u);
206
207 void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
208 void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
209 void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
210
211 void (*cgroup_notify_empty)(Unit *u);
212
213 /* This is called for each unit type and should be used to
214 * enumerate existing devices and load them. However,
215 * everything that is loaded here should still stay in
216 * inactive state. It is the job of the coldplug() call above
217 * to put the units into the initial state. */
218 int (*enumerate)(Manager *m);
219
220 /* Type specific cleanups. */
221 void (*shutdown)(Manager *m);
222 };
223
224 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
225
226 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
227
228 /* For casting a unit into the various unit types */
229 #define DEFINE_CAST(UPPERCASE, MixedCase) \
230 static inline MixedCase* UPPERCASE(Unit *u) { \
231 if (!u || u->meta.type != UNIT_##UPPERCASE) \
232 return NULL; \
233 \
234 return (MixedCase*) u; \
235 }
236
237 /* For casting the various unit types into a unit */
238 #define UNIT(u) ((Unit*) (u))
239
240 DEFINE_CAST(SOCKET, Socket);
241 DEFINE_CAST(TIMER, Timer);
242 DEFINE_CAST(SERVICE, Service);
243 DEFINE_CAST(TARGET, Target);
244 DEFINE_CAST(DEVICE, Device);
245 DEFINE_CAST(MOUNT, Mount);
246 DEFINE_CAST(AUTOMOUNT, Automount);
247 DEFINE_CAST(SNAPSHOT, Snapshot);
248
249 UnitType unit_name_to_type(const char *n);
250 bool unit_name_is_valid(const char *n);
251 char *unit_name_change_suffix(const char *n, const char *suffix);
252
253 Unit *unit_new(Manager *m);
254 void unit_free(Unit *u);
255
256 int unit_add_name(Unit *u, const char *name);
257 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
258 int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name);
259 int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name);
260
261 int unit_add_exec_dependencies(Unit *u, ExecContext *c);
262
263 int unit_add_cgroup(Unit *u, CGroupBonding *b);
264 int unit_add_cgroup_from_text(Unit *u, const char *name);
265 int unit_add_default_cgroup(Unit *u);
266 CGroupBonding* unit_get_default_cgroup(Unit *u);
267
268 int unit_choose_id(Unit *u, const char *name);
269 int unit_set_description(Unit *u, const char *description);
270
271 void unit_add_to_load_queue(Unit *u);
272 void unit_add_to_dbus_queue(Unit *u);
273 void unit_add_to_cleanup_queue(Unit *u);
274
275 int unit_merge(Unit *u, Unit *other);
276 int unit_merge_by_name(Unit *u, const char *other);
277
278 Unit *unit_follow_merge(Unit *u);
279
280 int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state);
281 int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state);
282 int unit_load(Unit *unit);
283
284 const char* unit_id(Unit *u);
285 const char *unit_description(Unit *u);
286
287 bool unit_has_name(Unit *u, const char *name);
288
289 UnitActiveState unit_active_state(Unit *u);
290
291 void unit_dump(Unit *u, FILE *f, const char *prefix);
292
293 bool unit_can_reload(Unit *u);
294 bool unit_can_start(Unit *u);
295
296 int unit_start(Unit *u);
297 int unit_stop(Unit *u);
298 int unit_reload(Unit *u);
299
300 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
301
302 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
303 void unit_unwatch_fd(Unit *u, Watch *w);
304
305 int unit_watch_pid(Unit *u, pid_t pid);
306 void unit_unwatch_pid(Unit *u, pid_t pid);
307
308 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
309 void unit_unwatch_timer(Unit *u, Watch *w);
310
311 bool unit_job_is_applicable(Unit *u, JobType j);
312
313 int set_unit_path(const char *p);
314
315 char *unit_name_escape_path(const char *path, const char *suffix);
316
317 const char *unit_type_to_string(UnitType i);
318 UnitType unit_type_from_string(const char *s);
319
320 const char *unit_load_state_to_string(UnitLoadState i);
321 UnitLoadState unit_load_state_from_string(const char *s);
322
323 const char *unit_active_state_to_string(UnitActiveState i);
324 UnitActiveState unit_active_state_from_string(const char *s);
325
326 const char *unit_dependency_to_string(UnitDependency i);
327 UnitDependency unit_dependency_from_string(const char *s);
328
329 char *unit_dbus_path(Unit *u);
330
331 #endif