]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/unit.h
socket: make various socket/pipe options configurable
[thirdparty/systemd.git] / src / 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
dfd8eeed 43#define DEFAULT_TIMEOUT_USEC (60*USEC_PER_SEC)
87f0e418
LP
44#define DEFAULT_RESTART_USEC (100*USEC_PER_MSEC)
45
50159e6a
LP
46typedef enum KillMode {
47 KILL_CONTROL_GROUP = 0,
48 KILL_PROCESS_GROUP,
49 KILL_PROCESS,
80876c20 50 KILL_NONE,
50159e6a
LP
51 _KILL_MODE_MAX,
52 _KILL_MODE_INVALID = -1
53} KillMode;
54
87f0e418
LP
55enum UnitType {
56 UNIT_SERVICE = 0,
87f0e418
LP
57 UNIT_SOCKET,
58 UNIT_TARGET,
59 UNIT_DEVICE,
60 UNIT_MOUNT,
61 UNIT_AUTOMOUNT,
62 UNIT_SNAPSHOT,
41447faf 63 UNIT_TIMER,
07b0b134 64 UNIT_SWAP,
01f78473 65 UNIT_PATH,
87f0e418 66 _UNIT_TYPE_MAX,
50159e6a 67 _UNIT_TYPE_INVALID = -1
87f0e418
LP
68};
69
70enum UnitLoadState {
71 UNIT_STUB,
72 UNIT_LOADED,
73 UNIT_FAILED,
23a177ef 74 UNIT_MERGED,
94f04347
LP
75 _UNIT_LOAD_STATE_MAX,
76 _UNIT_LOAD_STATE_INVALID = -1
87f0e418
LP
77};
78
79enum UnitActiveState {
80 UNIT_ACTIVE,
81 UNIT_ACTIVE_RELOADING,
82 UNIT_INACTIVE,
83 UNIT_ACTIVATING,
84 UNIT_DEACTIVATING,
94f04347
LP
85 _UNIT_ACTIVE_STATE_MAX,
86 _UNIT_ACTIVE_STATE_INVALID = -1
87f0e418
LP
87};
88
89static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
90 return t == UNIT_ACTIVE || t == UNIT_ACTIVE_RELOADING;
91}
92
93static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
94 return t == UNIT_ACTIVE || t == UNIT_ACTIVATING || t == UNIT_ACTIVE_RELOADING;
95}
96
97static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
98 return t == UNIT_INACTIVE || t == UNIT_DEACTIVATING;
99}
100
101enum UnitDependency {
102 /* Positive dependencies */
103 UNIT_REQUIRES,
9e2f7c11 104 UNIT_REQUIRES_OVERRIDABLE,
87f0e418 105 UNIT_REQUISITE,
9e2f7c11
LP
106 UNIT_REQUISITE_OVERRIDABLE,
107 UNIT_WANTS,
87f0e418
LP
108
109 /* Inverse of the above */
9e2f7c11
LP
110 UNIT_REQUIRED_BY, /* inverse of 'requires' and 'requisite' is 'required_by' */
111 UNIT_REQUIRED_BY_OVERRIDABLE, /* inverse of 'soft_requires' and 'soft_requisite' is 'soft_required_by' */
112 UNIT_WANTED_BY, /* inverse of 'wants' */
87f0e418
LP
113
114 /* Negative dependencies */
9e2f7c11 115 UNIT_CONFLICTS, /* inverse of 'conflicts' is 'conflicts' */
87f0e418
LP
116
117 /* Order */
701cc384 118 UNIT_BEFORE, /* inverse of 'before' is 'after' and vice versa */
87f0e418
LP
119 UNIT_AFTER,
120
701cc384
LP
121 /* Reference information for GC logic */
122 UNIT_REFERENCES, /* Inverse of 'references' is 'referenced_by' */
123 UNIT_REFERENCED_BY,
124
87f0e418
LP
125 _UNIT_DEPENDENCY_MAX,
126 _UNIT_DEPENDENCY_INVALID = -1
127};
128
ef734fd6
LP
129#include "manager.h"
130#include "job.h"
8e274523 131#include "cgroup.h"
ef734fd6 132
87f0e418
LP
133struct Meta {
134 Manager *manager;
23a177ef 135
87f0e418
LP
136 UnitType type;
137 UnitLoadState load_state;
23a177ef 138 Unit *merged_into;
87f0e418 139
bc0f8771
LP
140 /* Refuse manual starting, allow starting only indirectly via dependency. */
141 bool only_by_dependency;
142
87f0e418 143 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
9e2f7c11 144 char *instance;
87f0e418
LP
145
146 Set *names;
147 Set *dependencies[_UNIT_DEPENDENCY_MAX];
148
149 char *description;
6be1e7d5 150 char *fragment_path; /* if loaded from a config file this is the primary path to it */
87f0e418
LP
151
152 /* If there is something to do with this unit, then this is
153 * the job for it */
154 Job *job;
155
63983207
LP
156 dual_timestamp inactive_exit_timestamp;
157 dual_timestamp active_enter_timestamp;
158 dual_timestamp active_exit_timestamp;
159 dual_timestamp inactive_enter_timestamp;
87f0e418 160
8e274523
LP
161 /* Counterparts in the cgroup filesystem */
162 CGroupBonding *cgroup_bondings;
163
ef734fd6
LP
164 /* Per type list */
165 LIST_FIELDS(Meta, units_per_type);
c1e1601e 166
701cc384
LP
167 /* Load queue */
168 LIST_FIELDS(Meta, load_queue);
169
c1e1601e
LP
170 /* D-Bus queue */
171 LIST_FIELDS(Meta, dbus_queue);
23a177ef
LP
172
173 /* Cleanup queue */
174 LIST_FIELDS(Meta, cleanup_queue);
9d58f1db 175
701cc384
LP
176 /* GC queue */
177 LIST_FIELDS(Meta, gc_queue);
178
179 /* Used during GC sweeps */
eced69b3 180 unsigned gc_marker;
701cc384 181
9d58f1db
LP
182 /* If we go down, pull down everything that depends on us, too */
183 bool recursive_stop;
184
185 /* Garbage collect us we nobody wants or requires us anymore */
186 bool stop_when_unneeded;
187
cca098b0
LP
188 /* When deserializing, temporarily store the job type for this
189 * unit here, if there was a job scheduled */
2f630e5f 190 int deserialized_job; /* This is actually of type JobType */
cca098b0 191
9d58f1db
LP
192 bool in_load_queue:1;
193 bool in_dbus_queue:1;
194 bool in_cleanup_queue:1;
701cc384
LP
195 bool in_gc_queue:1;
196
9d58f1db 197 bool sent_dbus_new_signal:1;
87f0e418
LP
198};
199
200#include "service.h"
201#include "timer.h"
202#include "socket.h"
203#include "target.h"
204#include "device.h"
205#include "mount.h"
206#include "automount.h"
207#include "snapshot.h"
07b0b134 208#include "swap.h"
01f78473 209#include "path.h"
87f0e418
LP
210
211union Unit {
212 Meta meta;
213 Service service;
214 Timer timer;
215 Socket socket;
216 Target target;
217 Device device;
218 Mount mount;
219 Automount automount;
220 Snapshot snapshot;
07b0b134 221 Swap swap;
01f78473 222 Path path;
87f0e418
LP
223};
224
225struct UnitVTable {
226 const char *suffix;
227
e537352b 228 /* This should reset all type-specific variables. This should
a16e1123
LP
229 * not allocate memory, and is called with zero-initialized
230 * data. It should hence only initialize variables that need
231 * to be set != 0. */
e537352b
LP
232 void (*init)(Unit *u);
233
a16e1123
LP
234 /* This should free all type-specific variables. It should be
235 * idempotent. */
236 void (*done)(Unit *u);
237
e537352b
LP
238 /* Actually load data from disk. This may fail, and should set
239 * load_state to UNIT_LOADED, UNIT_MERGED or leave it at
240 * UNIT_STUB if no configuration could be found. */
241 int (*load)(Unit *u);
242
e537352b
LP
243 /* If a a lot of units got created via enumerate(), this is
244 * where to actually set the state and call unit_notify(). */
f50e0a01 245 int (*coldplug)(Unit *u);
87f0e418
LP
246
247 void (*dump)(Unit *u, FILE *f, const char *prefix);
248
249 int (*start)(Unit *u);
250 int (*stop)(Unit *u);
251 int (*reload)(Unit *u);
252
253 bool (*can_reload)(Unit *u);
254
a16e1123
LP
255 /* Write all data that cannot be restored from other sources
256 * away using unit_serialize_item() */
257 int (*serialize)(Unit *u, FILE *f, FDSet *fds);
258
259 /* Restore one item from the serialization */
260 int (*deserialize_item)(Unit *u, const char *key, const char *data, FDSet *fds);
261
87f0e418
LP
262 /* Boils down the more complex internal state of this unit to
263 * a simpler one that the engine can understand */
264 UnitActiveState (*active_state)(Unit *u);
265
10a94420
LP
266 /* Returns the substate specific to this unit type as
267 * string. This is purely information so that we can give the
268 * user a more finegrained explanation in which actual state a
269 * unit is in. */
270 const char* (*sub_state_to_string)(Unit *u);
271
701cc384
LP
272 /* Return true when there is reason to keep this entry around
273 * even nothing references it and it isn't active in any
274 * way */
275 bool (*check_gc)(Unit *u);
276
277 /* Return true when this unit is suitable for snapshotting */
278 bool (*check_snapshot)(Unit *u);
279
acbb0225 280 void (*fd_event)(Unit *u, int fd, uint32_t events, Watch *w);
87f0e418 281 void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
acbb0225 282 void (*timer_event)(Unit *u, uint64_t n_elapsed, Watch *w);
7824bbeb 283
05e343b7
LP
284 /* Called whenever any of the cgroups this unit watches for
285 * ran empty */
8e274523
LP
286 void (*cgroup_notify_empty)(Unit *u);
287
8c47c732 288 /* Called whenever a process of this unit sends us a message */
c952c6ec 289 void (*notify_message)(Unit *u, pid_t pid, char **tags);
8c47c732 290
05e343b7
LP
291 /* Called whenever a name thus Unit registered for comes or
292 * goes away. */
293 void (*bus_name_owner_change)(Unit *u, const char *name, const char *old_owner, const char *new_owner);
294
295 /* Called whenever a bus PID lookup finishes */
296 void (*bus_query_pid_done)(Unit *u, const char *name, pid_t pid);
297
4139c1b2 298 /* Called for each message received on the bus */
5e8d1c9a 299 DBusHandlerResult (*bus_message_handler)(Unit *u, DBusConnection *c, DBusMessage *message);
4139c1b2 300
f50e0a01
LP
301 /* This is called for each unit type and should be used to
302 * enumerate existing devices and load them. However,
303 * everything that is loaded here should still stay in
304 * inactive state. It is the job of the coldplug() call above
305 * to put the units into the initial state. */
7824bbeb 306 int (*enumerate)(Manager *m);
f50e0a01
LP
307
308 /* Type specific cleanups. */
7824bbeb 309 void (*shutdown)(Manager *m);
9d58f1db
LP
310
311 /* Can units of this type have multiple names? */
312 bool no_alias:1;
313
314 /* If true units of this types can never have "Requires"
315 * dependencies, because state changes can only be observed,
316 * not triggered */
317 bool no_requires:1;
318
319 /* Instances make no sense for this type */
320 bool no_instances:1;
321
322 /* Exclude this type from snapshots */
323 bool no_snapshots:1;
701cc384
LP
324
325 /* Exclude from automatic gc */
326 bool no_gc:1;
c497c7a9
LP
327
328 /* Exclude from isolation requests */
329 bool no_isolate:1;
87f0e418
LP
330};
331
332extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
333
334#define UNIT_VTABLE(u) unit_vtable[(u)->meta.type]
335
336/* For casting a unit into the various unit types */
337#define DEFINE_CAST(UPPERCASE, MixedCase) \
338 static inline MixedCase* UPPERCASE(Unit *u) { \
399ab2b1 339 if (_unlikely_(!u || u->meta.type != UNIT_##UPPERCASE)) \
87f0e418
LP
340 return NULL; \
341 \
342 return (MixedCase*) u; \
343 }
344
345/* For casting the various unit types into a unit */
399ab2b1 346#define UNIT(u) ((Unit*) (&(u)->meta))
87f0e418
LP
347
348DEFINE_CAST(SOCKET, Socket);
349DEFINE_CAST(TIMER, Timer);
350DEFINE_CAST(SERVICE, Service);
351DEFINE_CAST(TARGET, Target);
352DEFINE_CAST(DEVICE, Device);
353DEFINE_CAST(MOUNT, Mount);
354DEFINE_CAST(AUTOMOUNT, Automount);
355DEFINE_CAST(SNAPSHOT, Snapshot);
07b0b134 356DEFINE_CAST(SWAP, Swap);
01f78473 357DEFINE_CAST(PATH, Path);
87f0e418 358
87f0e418
LP
359Unit *unit_new(Manager *m);
360void unit_free(Unit *u);
361
362int unit_add_name(Unit *u, const char *name);
9e2f7c11 363
701cc384
LP
364int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference);
365int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
366int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *filename, bool add_reference);
0ae97ec1 367
23a177ef
LP
368int unit_add_exec_dependencies(Unit *u, ExecContext *c);
369
8e274523
LP
370int unit_add_cgroup(Unit *u, CGroupBonding *b);
371int unit_add_cgroup_from_text(Unit *u, const char *name);
372int unit_add_default_cgroup(Unit *u);
373CGroupBonding* unit_get_default_cgroup(Unit *u);
374
0ae97ec1 375int unit_choose_id(Unit *u, const char *name);
f50e0a01 376int unit_set_description(Unit *u, const char *description);
87f0e418 377
701cc384
LP
378bool unit_check_gc(Unit *u);
379
87f0e418 380void unit_add_to_load_queue(Unit *u);
c1e1601e 381void unit_add_to_dbus_queue(Unit *u);
23a177ef 382void unit_add_to_cleanup_queue(Unit *u);
701cc384 383void unit_add_to_gc_queue(Unit *u);
87f0e418
LP
384
385int unit_merge(Unit *u, Unit *other);
23a177ef
LP
386int unit_merge_by_name(Unit *u, const char *other);
387
388Unit *unit_follow_merge(Unit *u);
87f0e418 389
e537352b
LP
390int unit_load_fragment_and_dropin(Unit *u);
391int unit_load_fragment_and_dropin_optional(Unit *u);
a16e1123 392int unit_load_nop(Unit *u);
87f0e418
LP
393int unit_load(Unit *unit);
394
87f0e418
LP
395const char *unit_description(Unit *u);
396
f278026d
LP
397bool unit_has_name(Unit *u, const char *name);
398
87f0e418
LP
399UnitActiveState unit_active_state(Unit *u);
400
10a94420
LP
401const char* unit_sub_state_to_string(Unit *u);
402
87f0e418
LP
403void unit_dump(Unit *u, FILE *f, const char *prefix);
404
405bool unit_can_reload(Unit *u);
406bool unit_can_start(Unit *u);
407
408int unit_start(Unit *u);
409int unit_stop(Unit *u);
410int unit_reload(Unit *u);
411
412void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns);
413
acbb0225
LP
414int unit_watch_fd(Unit *u, int fd, uint32_t events, Watch *w);
415void unit_unwatch_fd(Unit *u, Watch *w);
87f0e418
LP
416
417int unit_watch_pid(Unit *u, pid_t pid);
418void unit_unwatch_pid(Unit *u, pid_t pid);
419
acbb0225
LP
420int unit_watch_timer(Unit *u, usec_t delay, Watch *w);
421void unit_unwatch_timer(Unit *u, Watch *w);
87f0e418 422
05e343b7
LP
423int unit_watch_bus_name(Unit *u, const char *name);
424void unit_unwatch_bus_name(Unit *u, const char *name);
425
87f0e418
LP
426bool unit_job_is_applicable(Unit *u, JobType j);
427
0301abf4
LP
428int set_unit_path(const char *p);
429
50159e6a
LP
430char *unit_dbus_path(Unit *u);
431
f6ff8c29 432int unit_load_related_unit(Unit *u, const char *type, Unit **_found);
a16e1123 433int unit_get_related_unit(Unit *u, const char *type, Unit **_found);
f6ff8c29 434
9e2f7c11
LP
435char *unit_name_printf(Unit *u, const char* text);
436char *unit_full_printf(Unit *u, const char *text);
437char **unit_full_printf_strv(Unit *u, char **l);
438
a16e1123
LP
439bool unit_can_serialize(Unit *u);
440int unit_serialize(Unit *u, FILE *f, FDSet *fds);
93a46b0b 441void unit_serialize_item_format(Unit *u, FILE *f, const char *key, const char *value, ...) _printf_attr_(4,5);
a16e1123
LP
442void unit_serialize_item(Unit *u, FILE *f, const char *key, const char *value);
443int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
444
6e2ef85b
LP
445int unit_add_node_link(Unit *u, const char *what, bool wants);
446
cca098b0
LP
447int unit_coldplug(Unit *u);
448
94f04347
LP
449const char *unit_type_to_string(UnitType i);
450UnitType unit_type_from_string(const char *s);
451
452const char *unit_load_state_to_string(UnitLoadState i);
453UnitLoadState unit_load_state_from_string(const char *s);
454
455const char *unit_active_state_to_string(UnitActiveState i);
456UnitActiveState unit_active_state_from_string(const char *s);
457
458const char *unit_dependency_to_string(UnitDependency i);
459UnitDependency unit_dependency_from_string(const char *s);
460
50159e6a
LP
461const char *kill_mode_to_string(KillMode k);
462KillMode kill_mode_from_string(const char *s);
ea430986 463
87f0e418 464#endif