]> git.ipfire.org Git - people/ms/systemd.git/blame - unit.h
main: set $PATH if it isn't set
[people/ms/systemd.git] / unit.h
CommitLineData
87f0e418
LP
1/*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3#ifndef foounithfoo
4#define foounithfoo
5
a7334b09
LP
6/***
7 This file is part of systemd.
8
9 Copyright 2010 Lennart Poettering
10
11 systemd is free software; you can redistribute it and/or modify it
12 under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
15
16 systemd is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23***/
24
87f0e418
LP
25#include <stdbool.h>
26#include <stdlib.h>
27
28typedef union Unit Unit;
29typedef struct Meta Meta;
30typedef struct UnitVTable UnitVTable;
31typedef enum UnitType UnitType;
32typedef enum UnitLoadState UnitLoadState;
33typedef enum UnitActiveState UnitActiveState;
34typedef enum UnitDependency UnitDependency;
35
87f0e418
LP
36#include "set.h"
37#include "util.h"
38#include "list.h"
39#include "socket-util.h"
40#include "execute.h"
87f0e418 41
b5ea5d95 42#define UNIT_NAME_MAX 128
87f0e418
LP
43#define DEFAULT_TIMEOUT_USEC (20*USEC_PER_SEC)
44#define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
45
46enum UnitType {
47 UNIT_SERVICE = 0,
48 UNIT_TIMER,
49 UNIT_SOCKET,
50 UNIT_TARGET,
51 UNIT_DEVICE,
52 UNIT_MOUNT,
53 UNIT_AUTOMOUNT,
54 UNIT_SNAPSHOT,
55 _UNIT_TYPE_MAX,
56 _UNIT_TYPE_INVALID = -1,
57};
58
59enum UnitLoadState {
60 UNIT_STUB,
61 UNIT_LOADED,
62 UNIT_FAILED,
23a177ef 63 UNIT_MERGED,
94f04347
LP
64 _UNIT_LOAD_STATE_MAX,
65 _UNIT_LOAD_STATE_INVALID = -1
87f0e418
LP
66};
67
68enum UnitActiveState {
69 UNIT_ACTIVE,
70 UNIT_ACTIVE_RELOADING,
71 UNIT_INACTIVE,
72 UNIT_ACTIVATING,
73 UNIT_DEACTIVATING,
94f04347
LP
74 _UNIT_ACTIVE_STATE_MAX,
75 _UNIT_ACTIVE_STATE_INVALID = -1
87f0e418
LP
76};
77
78static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
79 return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
80}
81
82static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
83 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
84}
85
86static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
87 return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
88}
89
90enum UnitDependency {
91 /* Positive dependencies */
92 UNIT_REQUIRES,
93 UNIT_SOFT_REQUIRES,
94 UNIT_WANTS,
95 UNIT_REQUISITE,
96 UNIT_SOFT_REQUISITE,
97
98 /* Inverse of the above */
99 UNIT_REQUIRED_BY, /* inverse of 'requires' and 'requisite' is 'required_by' */
100 UNIT_SOFT_REQUIRED_BY, /* inverse of 'soft_requires' and 'soft_requisite' is 'soft_required_by' */
101 UNIT_WANTED_BY, /* inverse of 'wants' */
102
103 /* Negative dependencies */
104 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
105
106 /* Order */
107 UNIT_BEFORE, /* inverse of before is after and vice versa */
108 UNIT_AFTER,
109
110 _UNIT_DEPENDENCY_MAX,
111 _UNIT_DEPENDENCY_INVALID = -1
112};
113
ef734fd6
LP
114#include "manager.h"
115#include "job.h"
8e274523 116#include "cgroup.h"
ef734fd6 117
87f0e418
LP
118struct Meta {
119 Manager *manager;
23a177ef 120
87f0e418
LP
121 UnitType type;
122 UnitLoadState load_state;
23a177ef 123 Unit *merged_into;
87f0e418
LP
124
125 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
126
127 Set *names;
128 Set *dependencies[_UNIT_DEPENDENCY_MAX];
129
130 char *description;
6be1e7d5 131 char *fragment_path; /* if loaded from a config file this is the primary path to it */
87f0e418
LP
132
133 /* If there is something to do with this unit, then this is
134 * the job for it */
135 Job *job;
136
137 bool in_load_queue:1;
c1e1601e 138 bool in_dbus_queue:1;
23a177ef 139 bool in_cleanup_queue:1;
c1e1601e 140 bool sent_dbus_new_signal:1;
87f0e418 141
f3bff0eb
LP
142 /* If we go down, pull down everything that depends on us, too */
143 bool recursive_stop;
144
145 /* Garbage collect us we nobody wants or requires us anymore */
146 bool stop_when_unneeded;
147
87f0e418
LP
148 usec_t active_enter_timestamp;
149 usec_t active_exit_timestamp;
150
8e274523
LP
151 /* Counterparts in the cgroup filesystem */
152 CGroupBonding *cgroup_bondings;
153
87f0e418
LP
154 /* Load queue */
155 LIST_FIELDS(Meta, load_queue);
ef734fd6
LP
156
157 /* Per type list */
158 LIST_FIELDS(Meta, units_per_type);
c1e1601e
LP
159
160 /* D-Bus queue */
161 LIST_FIELDS(Meta, dbus_queue);
23a177ef
LP
162
163 /* Cleanup queue */
164 LIST_FIELDS(Meta, cleanup_queue);
87f0e418
LP
165};
166
167#include "service.h"
168#include "timer.h"
169#include "socket.h"
170#include "target.h"
171#include "device.h"
172#include "mount.h"
173#include "automount.h"
174#include "snapshot.h"
175
176union Unit {
177 Meta meta;
178 Service service;
179 Timer timer;
180 Socket socket;
181 Target target;
182 Device device;
183 Mount mount;
184 Automount automount;
185 Snapshot snapshot;
186};
187
188struct UnitVTable {
189 const char *suffix;
190
23a177ef 191 int (*init)(Unit *u, UnitLoadState *new_state);
87f0e418 192 void (*done)(Unit *u);
f50e0a01 193 int (*coldplug)(Unit *u);
87f0e418
LP
194
195 void (*dump)(Unit *u, FILE *f, const char *prefix);
196
197 int (*start)(Unit *u);
198 int (*stop)(Unit *u);
199 int (*reload)(Unit *u);
200
201 bool (*can_reload)(Unit *u);
202
203 /* Boils down the more complex internal state of this unit to
204 * a simpler one that the engine can understand */
205 UnitActiveState (*active_state)(Unit *u);
206
acbb0225 207 void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
87f0e418 208 void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
acbb0225 209 void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
7824bbeb 210
8e274523
LP
211 void (*cgroup_notify_empty)(Unit *u);
212
f50e0a01
LP
213 /* This is called for each unit type and should be used to
214 * enumerate existing devices and load them. However,
215 * everything that is loaded here should still stay in
216 * inactive state. It is the job of the coldplug() call above
217 * to put the units into the initial state. */
7824bbeb 218 int (*enumerate)(Manager *m);
f50e0a01
LP
219
220 /* Type specific cleanups. */
7824bbeb 221 void (*shutdown)(Manager *m);
87f0e418
LP
222};
223
224extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
225
226#define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
227
228/* For casting a unit into the various unit types */
229#define DEFINE_CAST(UPPERCASE, MixedCase) \
230 static inline MixedCase* UPPERCASE(Unit *u) { \
231 if (!u || u->meta.type != UNIT_##UPPERCASE) \
232 return NULL; \
233 \
234 return (MixedCase*) u; \
235 }
236
237/* For casting the various unit types into a unit */
238#define UNIT(u) ((Unit*) (u))
239
240DEFINE_CAST(SOCKET, Socket);
241DEFINE_CAST(TIMER, Timer);
242DEFINE_CAST(SERVICE, Service);
243DEFINE_CAST(TARGET, Target);
244DEFINE_CAST(DEVICE, Device);
245DEFINE_CAST(MOUNT, Mount);
246DEFINE_CAST(AUTOMOUNT, Automount);
247DEFINE_CAST(SNAPSHOT, Snapshot);
248
249UnitType unit_name_to_type(const char *n);
250bool unit_name_is_valid(const char *n);
251char *unit_name_change_suffix(const char *n, const char *suffix);
252
253Unit *unit_new(Manager *m);
254void unit_free(Unit *u);
255
256int unit_add_name(Unit *u, const char *name);
257int unit_add_dependency(Unit *u, UnitDependency d, Unit *other);
09b6b09f 258int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name);
bd77d0fc 259int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name);
0ae97ec1 260
23a177ef
LP
261int unit_add_exec_dependencies(Unit *u, ExecContext *c);
262
8e274523
LP
263int unit_add_cgroup(Unit *u, CGroupBonding *b);
264int unit_add_cgroup_from_text(Unit *u, const char *name);
265int unit_add_default_cgroup(Unit *u);
266CGroupBonding* unit_get_default_cgroup(Unit *u);
267
0ae97ec1 268int unit_choose_id(Unit *u, const char *name);
f50e0a01 269int unit_set_description(Unit *u, const char *description);
87f0e418
LP
270
271void unit_add_to_load_queue(Unit *u);
c1e1601e 272void unit_add_to_dbus_queue(Unit *u);
23a177ef 273void unit_add_to_cleanup_queue(Unit *u);
87f0e418
LP
274
275int unit_merge(Unit *u, Unit *other);
23a177ef
LP
276int unit_merge_by_name(Unit *u, const char *other);
277
278Unit *unit_follow_merge(Unit *u);
87f0e418 279
23a177ef
LP
280int unit_load_fragment_and_dropin(Unit *u, UnitLoadState *new_state);
281int unit_load_fragment_and_dropin_optional(Unit *u, UnitLoadState *new_state);
87f0e418
LP
282int unit_load(Unit *unit);
283
284const char* unit_id(Unit *u);
285const char *unit_description(Unit *u);
286
f278026d
LP
287bool unit_has_name(Unit *u, const char *name);
288
87f0e418
LP
289UnitActiveState unit_active_state(Unit *u);
290
291void unit_dump(Unit *u, FILE *f, const char *prefix);
292
293bool unit_can_reload(Unit *u);
294bool unit_can_start(Unit *u);
295
296int unit_start(Unit *u);
297int unit_stop(Unit *u);
298int unit_reload(Unit *u);
299
300void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
301
acbb0225
LP
302int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
303void unit_unwatch_fd(Unit *u, Watch *w);
87f0e418
LP
304
305int unit_watch_pid(Unit *u, pid_t pid);
306void unit_unwatch_pid(Unit *u, pid_t pid);
307
acbb0225
LP
308int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
309void unit_unwatch_timer(Unit *u, Watch *w);
87f0e418
LP
310
311bool unit_job_is_applicable(Unit *u, JobType j);
312
0301abf4
LP
313int set_unit_path(const char *p);
314
2e478a46 315char *unit_name_escape_path(const char *path, const char *suffix);
0301abf4 316
94f04347
LP
317const char *unit_type_to_string(UnitType i);
318UnitType unit_type_from_string(const char *s);
319
320const char *unit_load_state_to_string(UnitLoadState i);
321UnitLoadState unit_load_state_from_string(const char *s);
322
323const char *unit_active_state_to_string(UnitActiveState i);
324UnitActiveState unit_active_state_from_string(const char *s);
325
326const char *unit_dependency_to_string(UnitDependency i);
327UnitDependency unit_dependency_from_string(const char *s);
328
ea430986
LP
329char *unit_dbus_path(Unit *u);
330
87f0e418 331#endif