1 /*-*- Mode: C; c-basic-offset: 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;
20 #include "socket-util.h"
23 #define UNIT_NAME_MAX 128
24 #define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
25 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
37 _UNIT_TYPE_INVALID = -1,
47 enum UnitActiveState {
49 UNIT_ACTIVE_RELOADING,
53 _UNIT_ACTIVE_STATE_MAX
56 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
57 return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
60 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
61 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
64 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
65 return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
69 /* Positive dependencies */
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' */
81 /* Negative dependencies */
82 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
85 UNIT_BEFORE, /* inverse of before is after and vice versa */
89 _UNIT_DEPENDENCY_INVALID = -1
98 UnitLoadState load_state;
100 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
103 Set *dependencies[_UNIT_DEPENDENCY_MAX];
106 char *load_path; /* if loaded from a config file this is the primary path to it */
108 /* If there is something to do with this unit, then this is
112 bool in_load_queue:1;
114 usec_t active_enter_timestamp;
115 usec_t active_exit_timestamp;
118 LIST_FIELDS(Meta, load_queue);
121 LIST_FIELDS(Meta, units_per_type);
130 #include "automount.h"
131 #include "snapshot.h"
148 int (*init)(Unit *u);
149 void (*done)(Unit *u);
150 int (*coldplug)(Unit *u);
152 void (*dump)(Unit *u, FILE *f, const char *prefix);
154 int (*start)(Unit *u);
155 int (*stop)(Unit *u);
156 int (*reload)(Unit *u);
158 bool (*can_reload)(Unit *u);
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);
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);
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);
175 /* Type specific cleanups. */
176 void (*shutdown)(Manager *m);
179 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
181 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
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) \
189 return (MixedCase*) u; \
192 /* For casting the various unit types into a unit */
193 #define UNIT(u) ((Unit*) (u))
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);
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);
208 Unit *unit_new(Manager *m);
209 void unit_free(Unit *u);
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);
215 int unit_choose_id(Unit *u, const char *name);
216 int unit_set_description(Unit *u, const char *description);
218 void unit_add_to_load_queue(Unit *u);
220 int unit_merge(Unit *u, Unit *other);
222 int unit_load_fragment_and_dropin(Unit *u);
223 int unit_load(Unit *unit);
225 const char* unit_id(Unit *u);
226 const char *unit_description(Unit *u);
228 UnitActiveState unit_active_state(Unit *u);
230 void unit_dump(Unit *u, FILE *f, const char *prefix);
232 bool unit_can_reload(Unit *u);
233 bool unit_can_start(Unit *u);
235 int unit_start(Unit *u);
236 int unit_stop(Unit *u);
237 int unit_reload(Unit *u);
239 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
241 int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
242 void unit_unwatch_fd(Unit *u, Watch *w);
244 int unit_watch_pid(Unit *u, pid_t pid);
245 void unit_unwatch_pid(Unit *u, pid_t pid);
247 int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
248 void unit_unwatch_timer(Unit *u, Watch *w);
250 bool unit_job_is_applicable(Unit *u, JobType j);
252 const char *unit_path(void);
253 int set_unit_path(const char *p);
255 char *unit_name_escape_path(const char *prefix, const char *path, const char *suffix);