1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
7 This file is part of systemd.
9 Copyright 2010 Lennart Poettering
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.
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.
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/>.
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;
39 #include "socket-util.h"
42 #define UNIT_NAME_MAX 128
43 #define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
44 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
56 _UNIT_TYPE_INVALID = -1,
65 _UNIT_LOAD_STATE_INVALID = -1
68 enum UnitActiveState {
70 UNIT_ACTIVE_RELOADING,
74 _UNIT_ACTIVE_STATE_MAX,
75 _UNIT_ACTIVE_STATE_INVALID = -1
78 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
79 return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
82 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
83 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
86 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
87 return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
91 /* Positive dependencies */
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' */
103 /* Negative dependencies */
104 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
107 UNIT_BEFORE, /* inverse of before is after and vice versa */
110 _UNIT_DEPENDENCY_MAX,
111 _UNIT_DEPENDENCY_INVALID = -1
122 UnitLoadState load_state;
125 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
128 Set *dependencies[_UNIT_DEPENDENCY_MAX];
131 char *fragment_path; /* if loaded from a config file this is the primary path to it */
133 /* If there is something to do with this unit, then this is
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;
142 /* If we go down, pull down everything that depends on us, too */
145 /* Garbage collect us we nobody wants or requires us anymore */
146 bool stop_when_unneeded;
148 usec_t active_enter_timestamp;
149 usec_t active_exit_timestamp;
151 /* Counterparts in the cgroup filesystem */
152 CGroupBonding *cgroup_bondings;
155 LIST_FIELDS(Meta, load_queue);
158 LIST_FIELDS(Meta, units_per_type);
161 LIST_FIELDS(Meta, dbus_queue);
164 LIST_FIELDS(Meta, cleanup_queue);
173 #include "automount.h"
174 #include "snapshot.h"
191 int (*init)(Unit *u, UnitLoadState *new_state);
192 void (*done)(Unit *u);
193 int (*coldplug)(Unit *u);
195 void (*dump)(Unit *u, FILE *f, const char *prefix);
197 int (*start)(Unit *u);
198 int (*stop)(Unit *u);
199 int (*reload)(Unit *u);
201 bool (*can_reload)(Unit *u);
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);
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);
211 void (*cgroup_notify_empty)(Unit *u);
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);
220 /* Type specific cleanups. */
221 void (*shutdown)(Manager *m);
224 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
226 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
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) \
234 return (MixedCase*) u; \
237 /* For casting the various unit types into a unit */
238 #define UNIT(u) ((Unit*) (u))
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);
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);
253 Unit *unit_new(Manager *m);
254 void unit_free(Unit *u);
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);
261 int unit_add_exec_dependencies(Unit *u, ExecContext *c);
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);
268 int unit_choose_id(Unit *u, const char *name);
269 int unit_set_description(Unit *u, const char *description);
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);
275 int unit_merge(Unit *u, Unit *other);
276 int unit_merge_by_name(Unit *u, const char *other);
278 Unit *unit_follow_merge(Unit *u);
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);
284 const char* unit_id(Unit *u);
285 const char *unit_description(Unit *u);
287 bool unit_has_name(Unit *u, const char *name);
289 UnitActiveState unit_active_state(Unit *u);
291 void unit_dump(Unit *u, FILE *f, const char *prefix);
293 bool unit_can_reload(Unit *u);
294 bool unit_can_start(Unit *u);
296 int unit_start(Unit *u);
297 int unit_stop(Unit *u);
298 int unit_reload(Unit *u);
300 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
302 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
303 void unit_unwatch_fd(Unit *u, Watch *w);
305 int unit_watch_pid(Unit *u, pid_t pid);
306 void unit_unwatch_pid(Unit *u, pid_t pid);
308 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
309 void unit_unwatch_timer(Unit *u, Watch *w);
311 bool unit_job_is_applicable(Unit *u, JobType j);
313 int set_unit_path(const char *p);
315 char *unit_name_escape_path(const char *path, const char *suffix);
317 const char *unit_type_to_string(UnitType i);
318 UnitType unit_type_from_string(const char *s);
320 const char *unit_load_state_to_string(UnitLoadState i);
321 UnitLoadState unit_load_state_from_string(const char *s);
323 const char *unit_active_state_to_string(UnitActiveState i);
324 UnitActiveState unit_active_state_from_string(const char *s);
326 const char *unit_dependency_to_string(UnitDependency i);
327 UnitDependency unit_dependency_from_string(const char *s);
329 char *unit_dbus_path(Unit *u);