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
;
22 #include "socket-util.h"
26 #define UNIT_NAME_MAX 128
27 #define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
28 #define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
40 _UNIT_TYPE_INVALID
= -1,
50 enum UnitActiveState
{
52 UNIT_ACTIVE_RELOADING
,
56 _UNIT_ACTIVE_STATE_MAX
59 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t
) {
60 return t
== UNIT_ACTIVE
|| t
== UNIT_ACTIVE_RELOADING
;
63 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t
) {
64 return t
== UNIT_ACTIVE
|| t
== UNIT_ACTIVATING
|| t
== UNIT_ACTIVE_RELOADING
;
67 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t
) {
68 return t
== UNIT_INACTIVE
|| t
== UNIT_DEACTIVATING
;
72 /* Positive dependencies */
79 /* Inverse of the above */
80 UNIT_REQUIRED_BY
, /* inverse of 'requires' and 'requisite' is 'required_by' */
81 UNIT_SOFT_REQUIRED_BY
, /* inverse of 'soft_requires' and 'soft_requisite' is 'soft_required_by' */
82 UNIT_WANTED_BY
, /* inverse of 'wants' */
84 /* Negative dependencies */
85 UNIT_CONFLICTS
, /* inverse of 'conflicts' is 'conflicts' */
88 UNIT_BEFORE
, /* inverse of before is after and vice versa */
92 _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
);
127 #include "automount.h"
128 #include "snapshot.h"
145 int (*init
)(Unit
*u
);
146 void (*done
)(Unit
*u
);
147 int (*coldplug
)(Unit
*u
);
149 void (*dump
)(Unit
*u
, FILE *f
, const char *prefix
);
151 int (*start
)(Unit
*u
);
152 int (*stop
)(Unit
*u
);
153 int (*reload
)(Unit
*u
);
155 bool (*can_reload
)(Unit
*u
);
157 /* Boils down the more complex internal state of this unit to
158 * a simpler one that the engine can understand */
159 UnitActiveState (*active_state
)(Unit
*u
);
161 void (*fd_event
)(Unit
*u
, int fd
, uint32_t events
, Watch
*w
);
162 void (*sigchld_event
)(Unit
*u
, pid_t pid
, int code
, int status
);
163 void (*timer_event
)(Unit
*u
, uint64_t n_elapsed
, Watch
*w
);
165 /* This is called for each unit type and should be used to
166 * enumerate existing devices and load them. However,
167 * everything that is loaded here should still stay in
168 * inactive state. It is the job of the coldplug() call above
169 * to put the units into the initial state. */
170 int (*enumerate
)(Manager
*m
);
172 /* Type specific cleanups. */
173 void (*shutdown
)(Manager
*m
);
176 extern const UnitVTable
* const unit_vtable
[_UNIT_TYPE_MAX
];
178 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
180 /* For casting a unit into the various unit types */
181 #define DEFINE_CAST(UPPERCASE, MixedCase) \
182 static inline MixedCase* UPPERCASE(Unit *u) { \
183 if (!u || u->meta.type != UNIT_##UPPERCASE) \
186 return (MixedCase*) u; \
189 /* For casting the various unit types into a unit */
190 #define UNIT(u) ((Unit*) (u))
192 DEFINE_CAST(SOCKET
, Socket
);
193 DEFINE_CAST(TIMER
, Timer
);
194 DEFINE_CAST(SERVICE
, Service
);
195 DEFINE_CAST(TARGET
, Target
);
196 DEFINE_CAST(DEVICE
, Device
);
197 DEFINE_CAST(MOUNT
, Mount
);
198 DEFINE_CAST(AUTOMOUNT
, Automount
);
199 DEFINE_CAST(SNAPSHOT
, Snapshot
);
201 UnitType
unit_name_to_type(const char *n
);
202 bool unit_name_is_valid(const char *n
);
203 char *unit_name_change_suffix(const char *n
, const char *suffix
);
205 Unit
*unit_new(Manager
*m
);
206 void unit_free(Unit
*u
);
208 int unit_add_name(Unit
*u
, const char *name
);
209 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
);
210 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
);
212 int unit_choose_id(Unit
*u
, const char *name
);
213 int unit_set_description(Unit
*u
, const char *description
);
215 void unit_add_to_load_queue(Unit
*u
);
217 int unit_merge(Unit
*u
, Unit
*other
);
219 int unit_load_fragment_and_dropin(Unit
*u
);
220 int unit_load(Unit
*unit
);
222 const char* unit_id(Unit
*u
);
223 const char *unit_description(Unit
*u
);
225 UnitActiveState
unit_active_state(Unit
*u
);
227 void unit_dump(Unit
*u
, FILE *f
, const char *prefix
);
229 bool unit_can_reload(Unit
*u
);
230 bool unit_can_start(Unit
*u
);
232 int unit_start(Unit
*u
);
233 int unit_stop(Unit
*u
);
234 int unit_reload(Unit
*u
);
236 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
);
238 int unit_watch_fd(Unit
*u
, int fd
, uint32_t events
, Watch
*w
);
239 void unit_unwatch_fd(Unit
*u
, Watch
*w
);
241 int unit_watch_pid(Unit
*u
, pid_t pid
);
242 void unit_unwatch_pid(Unit
*u
, pid_t pid
);
244 int unit_watch_timer(Unit
*u
, usec_t delay
, Watch
*w
);
245 void unit_unwatch_timer(Unit
*u
, Watch
*w
);
247 bool unit_job_is_applicable(Unit
*u
, JobType j
);
249 const char *unit_path(void);
250 int set_unit_path(const char *p
);
252 char *unit_name_escape_path(const char *prefix
, const char *path
, const char *suffix
);