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 /* If we go down, pull down everything that depends on us, too */
117 /* Garbage collect us we nobody wants or requires us anymore */
118 bool stop_when_unneeded
;
120 usec_t active_enter_timestamp
;
121 usec_t active_exit_timestamp
;
124 LIST_FIELDS(Meta
, load_queue
);
127 LIST_FIELDS(Meta
, units_per_type
);
136 #include "automount.h"
137 #include "snapshot.h"
154 int (*init
)(Unit
*u
);
155 void (*done
)(Unit
*u
);
156 int (*coldplug
)(Unit
*u
);
158 void (*dump
)(Unit
*u
, FILE *f
, const char *prefix
);
160 int (*start
)(Unit
*u
);
161 int (*stop
)(Unit
*u
);
162 int (*reload
)(Unit
*u
);
164 bool (*can_reload
)(Unit
*u
);
166 /* Boils down the more complex internal state of this unit to
167 * a simpler one that the engine can understand */
168 UnitActiveState (*active_state
)(Unit
*u
);
170 void (*fd_event
)(Unit
*u
, int fd
, uint32_t events
, Watch
*w
);
171 void (*sigchld_event
)(Unit
*u
, pid_t pid
, int code
, int status
);
172 void (*timer_event
)(Unit
*u
, uint64_t n_elapsed
, Watch
*w
);
174 /* This is called for each unit type and should be used to
175 * enumerate existing devices and load them. However,
176 * everything that is loaded here should still stay in
177 * inactive state. It is the job of the coldplug() call above
178 * to put the units into the initial state. */
179 int (*enumerate
)(Manager
*m
);
181 /* Type specific cleanups. */
182 void (*shutdown
)(Manager
*m
);
185 extern const UnitVTable
* const unit_vtable
[_UNIT_TYPE_MAX
];
187 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
189 /* For casting a unit into the various unit types */
190 #define DEFINE_CAST(UPPERCASE, MixedCase) \
191 static inline MixedCase* UPPERCASE(Unit *u) { \
192 if (!u || u->meta.type != UNIT_##UPPERCASE) \
195 return (MixedCase*) u; \
198 /* For casting the various unit types into a unit */
199 #define UNIT(u) ((Unit*) (u))
201 DEFINE_CAST(SOCKET
, Socket
);
202 DEFINE_CAST(TIMER
, Timer
);
203 DEFINE_CAST(SERVICE
, Service
);
204 DEFINE_CAST(TARGET
, Target
);
205 DEFINE_CAST(DEVICE
, Device
);
206 DEFINE_CAST(MOUNT
, Mount
);
207 DEFINE_CAST(AUTOMOUNT
, Automount
);
208 DEFINE_CAST(SNAPSHOT
, Snapshot
);
210 UnitType
unit_name_to_type(const char *n
);
211 bool unit_name_is_valid(const char *n
);
212 char *unit_name_change_suffix(const char *n
, const char *suffix
);
214 Unit
*unit_new(Manager
*m
);
215 void unit_free(Unit
*u
);
217 int unit_add_name(Unit
*u
, const char *name
);
218 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
);
219 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
);
221 int unit_choose_id(Unit
*u
, const char *name
);
222 int unit_set_description(Unit
*u
, const char *description
);
224 void unit_add_to_load_queue(Unit
*u
);
226 int unit_merge(Unit
*u
, Unit
*other
);
228 int unit_load_fragment_and_dropin(Unit
*u
);
229 int unit_load(Unit
*unit
);
231 const char* unit_id(Unit
*u
);
232 const char *unit_description(Unit
*u
);
234 UnitActiveState
unit_active_state(Unit
*u
);
236 void unit_dump(Unit
*u
, FILE *f
, const char *prefix
);
238 bool unit_can_reload(Unit
*u
);
239 bool unit_can_start(Unit
*u
);
241 int unit_start(Unit
*u
);
242 int unit_stop(Unit
*u
);
243 int unit_reload(Unit
*u
);
245 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
);
247 int unit_watch_fd(Unit
*u
, int fd
, uint32_t events
, Watch
*w
);
248 void unit_unwatch_fd(Unit
*u
, Watch
*w
);
250 int unit_watch_pid(Unit
*u
, pid_t pid
);
251 void unit_unwatch_pid(Unit
*u
, pid_t pid
);
253 int unit_watch_timer(Unit
*u
, usec_t delay
, Watch
*w
);
254 void unit_unwatch_timer(Unit
*u
, Watch
*w
);
256 bool unit_job_is_applicable(Unit
*u
, JobType j
);
258 const char *unit_path(void);
259 int set_unit_path(const char *p
);
261 char *unit_name_escape_path(const char *prefix
, const char *path
, const char *suffix
);