chiark / gitweb /
s/name/unit
[elogind.git] / unit.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef foounithfoo
4 #define foounithfoo
5
6 #include <stdbool.h>
7 #include <stdlib.h>
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;
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
30 enum 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
43 enum UnitLoadState {
44         UNIT_STUB,
45         UNIT_LOADED,
46         UNIT_FAILED,
47         _UNIT_LOAD_STATE_MAX
48 };
49
50 enum UnitActiveState {
51         UNIT_ACTIVE,
52         UNIT_ACTIVE_RELOADING,
53         UNIT_INACTIVE,
54         UNIT_ACTIVATING,
55         UNIT_DEACTIVATING,
56         _UNIT_ACTIVE_STATE_MAX
57 };
58
59 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
60         return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
61 }
62
63 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
64         return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
65 }
66
67 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
68         return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
69 }
70
71 enum 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
95 struct 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;
106
107         /* If there is something to do with this unit, then this is
108          * the job for it */
109         Job *job;
110
111         bool in_load_queue:1;
112
113         usec_t active_enter_timestamp;
114         usec_t active_exit_timestamp;
115
116         /* Load queue */
117         LIST_FIELDS(Meta, load_queue);
118 };
119
120 #include "service.h"
121 #include "timer.h"
122 #include "socket.h"
123 #include "target.h"
124 #include "device.h"
125 #include "mount.h"
126 #include "automount.h"
127 #include "snapshot.h"
128
129 union Unit {
130         Meta meta;
131         Service service;
132         Timer timer;
133         Socket socket;
134         Target target;
135         Device device;
136         Mount mount;
137         Automount automount;
138         Snapshot snapshot;
139 };
140
141 struct UnitVTable {
142         const char *suffix;
143
144         int (*init)(Unit *u);
145         void (*done)(Unit *u);
146
147         void (*dump)(Unit *u, FILE *f, const char *prefix);
148
149         int (*start)(Unit *u);
150         int (*stop)(Unit *u);
151         int (*reload)(Unit *u);
152
153         bool (*can_reload)(Unit *u);
154
155         /* Boils down the more complex internal state of this unit to
156          * a simpler one that the engine can understand */
157         UnitActiveState (*active_state)(Unit *u);
158
159         void (*fd_event)(Unit *u, int fd, uint32_t events);
160         void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
161         void (*timer_event)(Unit *u, int id, uint64_t n_elapsed);
162
163         void (*retry)(Unit *u);
164 };
165
166 extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
167
168 #define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
169
170 /* For casting a unit into the various unit types */
171 #define DEFINE_CAST(UPPERCASE, MixedCase)                               \
172         static inline MixedCase* UPPERCASE(Unit *u) {                   \
173                 if (!u || u->meta.type != UNIT_##UPPERCASE)             \
174                         return NULL;                                    \
175                                                                         \
176                 return (MixedCase*) u;                                  \
177         }
178
179 /* For casting the various unit types into a unit */
180 #define UNIT(u) ((Unit*) (u))
181
182 DEFINE_CAST(SOCKET, Socket);
183 DEFINE_CAST(TIMER, Timer);
184 DEFINE_CAST(SERVICE, Service);
185 DEFINE_CAST(TARGET, Target);
186 DEFINE_CAST(DEVICE, Device);
187 DEFINE_CAST(MOUNT, Mount);
188 DEFINE_CAST(AUTOMOUNT, Automount);
189 DEFINE_CAST(SNAPSHOT, Snapshot);
190
191 UnitType unit_name_to_type(const char *n);
192 bool unit_name_is_valid(const char *n);
193 char *unit_name_change_suffix(const char *n, const char *suffix);
194
195 Unit *unit_new(Manager *m);
196 void unit_free(Unit *u);
197
198 int unit_add_name(Unit *u, const char *name);
199 int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
200
201 void unit_add_to_load_queue(Unit *u);
202
203 int unit_merge(Unit *u, Unit *other);
204
205 int unit_load_fragment_and_dropin(Unit *u);
206 int unit_load(Unit *unit);
207
208 const char* unit_id(Unit *u);
209 const char *unit_description(Unit *u);
210
211 UnitActiveState unit_active_state(Unit *u);
212
213 void unit_dump(Unit *u, FILE *f, const char *prefix);
214
215 bool unit_can_reload(Unit *u);
216 bool unit_can_start(Unit *u);
217
218 int unit_start(Unit *u);
219 int unit_stop(Unit *u);
220 int unit_reload(Unit *u);
221
222 void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
223
224 int unit_watch_fd(Unit *u, int fd, uint32_t events);
225 void unit_unwatch_fd(Unit *u, int fd);
226
227 int unit_watch_pid(Unit *u, pid_t pid);
228 void unit_unwatch_pid(Unit *u, pid_t pid);
229
230 int unit_watch_timer(Unit *u, usec_t delay, int *id);
231 void unit_unwatch_timer(Unit *u, int *id);
232
233 bool unit_job_is_applicable(Unit *u, JobType j);
234
235 #endif