]> git.ipfire.org Git - people/ms/systemd.git/blame - unit.h
make use of unit_add_dependency_by_name() where applicable
[people/ms/systemd.git] / unit.h
CommitLineData
87f0e418
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#ifndef foounithfoo
4#define foounithfoo
5
6#include <stdbool.h>
7#include <stdlib.h>
8
9typedef union Unit Unit;
10typedef struct Meta Meta;
11typedef struct UnitVTable UnitVTable;
12typedef enum UnitType UnitType;
13typedef enum UnitLoadState UnitLoadState;
14typedef enum UnitActiveState UnitActiveState;
15typedef enum UnitDependency UnitDependency;
16
17#include "job.h"
18#include "manager.h"
19#include "set.h"
20#include "util.h"
21#include "list.h"
22#include "socket-util.h"
23#include "execute.h"
24#include "util.h"
25
26#define UNIT_NAME_MAX 32
27#define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
28#define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
29
30enum UnitType {
31 UNIT_SERVICE = 0,
32 UNIT_TIMER,
33 UNIT_SOCKET,
34 UNIT_TARGET,
35 UNIT_DEVICE,
36 UNIT_MOUNT,
37 UNIT_AUTOMOUNT,
38 UNIT_SNAPSHOT,
39 _UNIT_TYPE_MAX,
40 _UNIT_TYPE_INVALID = -1,
41};
42
43enum UnitLoadState {
44 UNIT_STUB,
45 UNIT_LOADED,
46 UNIT_FAILED,
47 _UNIT_LOAD_STATE_MAX
48};
49
50enum UnitActiveState {
51 UNIT_ACTIVE,
52 UNIT_ACTIVE_RELOADING,
53 UNIT_INACTIVE,
54 UNIT_ACTIVATING,
55 UNIT_DEACTIVATING,
56 _UNIT_ACTIVE_STATE_MAX
57};
58
59static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
60 return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
61}
62
63static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
64 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
65}
66
67static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
68 return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
69}
70
71enum UnitDependency {
72 /* Positive dependencies */
73 UNIT_REQUIRES,
74 UNIT_SOFT_REQUIRES,
75 UNIT_WANTS,
76 UNIT_REQUISITE,
77 UNIT_SOFT_REQUISITE,
78
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' */
83
84 /* Negative dependencies */
85 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
86
87 /* Order */
88 UNIT_BEFORE, /* inverse of before is after and vice versa */
89 UNIT_AFTER,
90
91 _UNIT_DEPENDENCY_MAX,
92 _UNIT_DEPENDENCY_INVALID = -1
93};
94
95struct Meta {
96 Manager *manager;
97 UnitType type;
98 UnitLoadState load_state;
99
100 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
101
102 Set *names;
103 Set *dependencies[_UNIT_DEPENDENCY_MAX];
104
105 char *description;
0301abf4 106 char *load_path; /* if loaded from a config file this is the primary path to it */
87f0e418
LP
107
108 /* If there is something to do with this unit, then this is
109 * the job for it */
110 Job *job;
111
112 bool in_load_queue:1;
113
114 usec_t active_enter_timestamp;
115 usec_t active_exit_timestamp;
116
117 /* Load queue */
118 LIST_FIELDS(Meta, load_queue);
119};
120
121#include "service.h"
122#include "timer.h"
123#include "socket.h"
124#include "target.h"
125#include "device.h"
126#include "mount.h"
127#include "automount.h"
128#include "snapshot.h"
129
130union Unit {
131 Meta meta;
132 Service service;
133 Timer timer;
134 Socket socket;
135 Target target;
136 Device device;
137 Mount mount;
138 Automount automount;
139 Snapshot snapshot;
140};
141
142struct UnitVTable {
143 const char *suffix;
144
145 int (*init)(Unit *u);
146 void (*done)(Unit *u);
147
148 void (*dump)(Unit *u, FILE *f, const char *prefix);
149
150 int (*start)(Unit *u);
151 int (*stop)(Unit *u);
152 int (*reload)(Unit *u);
153
154 bool (*can_reload)(Unit *u);
155
156 /* Boils down the more complex internal state of this unit to
157 * a simpler one that the engine can understand */
158 UnitActiveState (*active_state)(Unit *u);
159
acbb0225 160 void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
87f0e418 161 void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
acbb0225 162 void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
87f0e418
LP
163};
164
165extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
166
167#define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
168
169/* For casting a unit into the various unit types */
170#define DEFINE_CAST(UPPERCASE, MixedCase) \
171 static inline MixedCase* UPPERCASE(Unit *u) { \
172 if (!u || u->meta.type != UNIT_##UPPERCASE) \
173 return NULL; \
174 \
175 return (MixedCase*) u; \
176 }
177
178/* For casting the various unit types into a unit */
179#define UNIT(u) ((Unit*) (u))
180
181DEFINE_CAST(SOCKET, Socket);
182DEFINE_CAST(TIMER, Timer);
183DEFINE_CAST(SERVICE, Service);
184DEFINE_CAST(TARGET, Target);
185DEFINE_CAST(DEVICE, Device);
186DEFINE_CAST(MOUNT, Mount);
187DEFINE_CAST(AUTOMOUNT, Automount);
188DEFINE_CAST(SNAPSHOT, Snapshot);
189
190UnitType unit_name_to_type(const char *n);
191bool unit_name_is_valid(const char *n);
192char *unit_name_change_suffix(const char *n, const char *suffix);
193
194Unit *unit_new(Manager *m);
195void unit_free(Unit *u);
196
197int unit_add_name(Unit *u, const char *name);
198int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
09b6b09f 199int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name);
87f0e418
LP
200
201void unit_add_to_load_queue(Unit *u);
202
203int unit_merge(Unit *u, Unit *other);
204
205int unit_load_fragment_and_dropin(Unit *u);
206int unit_load(Unit *unit);
207
208const char* unit_id(Unit *u);
209const char *unit_description(Unit *u);
210
211UnitActiveState unit_active_state(Unit *u);
212
213void unit_dump(Unit *u, FILE *f, const char *prefix);
214
215bool unit_can_reload(Unit *u);
216bool unit_can_start(Unit *u);
217
218int unit_start(Unit *u);
219int unit_stop(Unit *u);
220int unit_reload(Unit *u);
221
222void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
223
acbb0225
LP
224int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
225void unit_unwatch_fd(Unit *u, Watch *w);
87f0e418
LP
226
227int unit_watch_pid(Unit *u, pid_t pid);
228void unit_unwatch_pid(Unit *u, pid_t pid);
229
acbb0225
LP
230int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
231void unit_unwatch_timer(Unit *u, Watch *w);
87f0e418
LP
232
233bool unit_job_is_applicable(Unit *u, JobType j);
234
0301abf4
LP
235const char *unit_path(void);
236int set_unit_path(const char *p);
237
238
87f0e418 239#endif