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,
45 _UNIT_LOAD_STATE_INVALID
= -1
48 enum UnitActiveState
{
50 UNIT_ACTIVE_RELOADING
,
54 _UNIT_ACTIVE_STATE_MAX
,
55 _UNIT_ACTIVE_STATE_INVALID
= -1
58 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t
) {
59 return t
== UNIT_ACTIVE
|| t
== UNIT_ACTIVE_RELOADING
;
62 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t
) {
63 return t
== UNIT_ACTIVE
|| t
== UNIT_ACTIVATING
|| t
== UNIT_ACTIVE_RELOADING
;
66 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t
) {
67 return t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
;
71 /* Positive dependencies */
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' */
83 /* Negative dependencies */
84 UNIT_CONFLICTS
, /* inverse of 'conflicts' is 'conflicts' */
87 UNIT_BEFORE
, /* inverse of before is after and vice versa */
91 _UNIT_DEPENDENCY_INVALID
= -1
100 UnitLoadState load_state
;
102 char *id
; /* One name is special because we use it for identification. Points to an entry in the names set */
105 Set
*dependencies
[_UNIT_DEPENDENCY_MAX
];
108 char *load_path
; /* if loaded from a config file this is the primary path to it */
110 /* If there is something to do with this unit, then this is
114 bool in_load_queue
:1;
116 /* If we go down, pull down everything that depends on us, too */
119 /* Garbage collect us we nobody wants or requires us anymore */
120 bool stop_when_unneeded
;
122 usec_t active_enter_timestamp
;
123 usec_t active_exit_timestamp
;
126 LIST_FIELDS(Meta
, load_queue
);
129 LIST_FIELDS(Meta
, units_per_type
);
138 #include "automount.h"
139 #include "snapshot.h"
156 int (*init
)(Unit
*u
);
157 void (*done
)(Unit
*u
);
158 int (*coldplug
)(Unit
*u
);
160 void (*dump
)(Unit
*u
, FILE *f
, const char *prefix
);
162 int (*start
)(Unit
*u
);
163 int (*stop
)(Unit
*u
);
164 int (*reload
)(Unit
*u
);
166 bool (*can_reload
)(Unit
*u
);
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
);
172 void (*fd_event
)(Unit
*u
, int fd
, uint32_t events
, Watch
*w
);
173 void (*sigchld_event
)(Unit
*u
, pid_t pid
, int code
, int status
);
174 void (*timer_event
)(Unit
*u
, uint64_t n_elapsed
, Watch
*w
);
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. */
181 int (*enumerate
)(Manager
*m
);
183 /* Type specific cleanups. */
184 void (*shutdown
)(Manager
*m
);
187 extern const UnitVTable
* const unit_vtable
[_UNIT_TYPE_MAX
];
189 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
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) \
197 return (MixedCase*) u; \
200 /* For casting the various unit types into a unit */
201 #define UNIT(u) ((Unit*) (u))
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
);
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
);
216 Unit
*unit_new(Manager
*m
);
217 void unit_free(Unit
*u
);
219 int unit_add_name(Unit
*u
, const char *name
);
220 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
);
221 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
);
223 int unit_choose_id(Unit
*u
, const char *name
);
224 int unit_set_description(Unit
*u
, const char *description
);
226 void unit_add_to_load_queue(Unit
*u
);
228 int unit_merge(Unit
*u
, Unit
*other
);
230 int unit_load_fragment_and_dropin(Unit
*u
);
231 int unit_load(Unit
*unit
);
233 const char* unit_id(Unit
*u
);
234 const char *unit_description(Unit
*u
);
236 UnitActiveState
unit_active_state(Unit
*u
);
238 void unit_dump(Unit
*u
, FILE *f
, const char *prefix
);
240 bool unit_can_reload(Unit
*u
);
241 bool unit_can_start(Unit
*u
);
243 int unit_start(Unit
*u
);
244 int unit_stop(Unit
*u
);
245 int unit_reload(Unit
*u
);
247 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
);
249 int unit_watch_fd(Unit
*u
, int fd
, uint32_t events
, Watch
*w
);
250 void unit_unwatch_fd(Unit
*u
, Watch
*w
);
252 int unit_watch_pid(Unit
*u
, pid_t pid
);
253 void unit_unwatch_pid(Unit
*u
, pid_t pid
);
255 int unit_watch_timer(Unit
*u
, usec_t delay
, Watch
*w
);
256 void unit_unwatch_timer(Unit
*u
, Watch
*w
);
258 bool unit_job_is_applicable(Unit
*u
, JobType j
);
260 const char *unit_path(void);
261 int set_unit_path(const char *p
);
263 char *unit_name_escape_path(const char *prefix
, const char *path
, const char *suffix
);
265 const char *unit_type_to_string(UnitType i
);
266 UnitType
unit_type_from_string(const char *s
);
268 const char *unit_load_state_to_string(UnitLoadState i
);
269 UnitLoadState
unit_load_state_from_string(const char *s
);
271 const char *unit_active_state_to_string(UnitActiveState i
);
272 UnitActiveState
unit_active_state_from_string(const char *s
);
274 const char *unit_dependency_to_string(UnitDependency i
);
275 UnitDependency
unit_dependency_from_string(const char *s
);
277 char *unit_dbus_path(Unit
*u
);