]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/core/unit.h
core: clearly refuse OnFailure= deps on units that can't fail
[thirdparty/systemd.git] / src / core / unit.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
c2f1db8f 2#pragma once
87f0e418
LP
3
4#include <stdbool.h>
5#include <stdlib.h>
bbc9006e 6#include <unistd.h>
87f0e418 7
6a48d82f 8#include "bpf-program.h"
134e56dc 9#include "condition.h"
87a47f99 10#include "emergency-action.h"
71d35b6b 11#include "list.h"
fab34748 12#include "set.h"
5cfa33e0 13#include "unit-file.h"
6b659ed8 14#include "cgroup.h"
87f0e418 15
57b7a260
FS
16typedef struct UnitRef UnitRef;
17
db2cb23b
UTL
18typedef enum KillOperation {
19 KILL_TERMINATE,
1d98fef1 20 KILL_TERMINATE_AND_LOG,
a232ebcc 21 KILL_RESTART,
db2cb23b 22 KILL_KILL,
c87700a1 23 KILL_WATCHDOG,
4940c0b0
LP
24 _KILL_OPERATION_MAX,
25 _KILL_OPERATION_INVALID = -1
db2cb23b
UTL
26} KillOperation;
27
5afe510c
LP
28typedef enum CollectMode {
29 COLLECT_INACTIVE,
30 COLLECT_INACTIVE_OR_FAILED,
31 _COLLECT_MODE_MAX,
32 _COLLECT_MODE_INVALID = -1,
33} CollectMode;
34
87f0e418 35static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t) {
3742095b 36 return IN_SET(t, UNIT_ACTIVE, UNIT_RELOADING);
87f0e418
LP
37}
38
39static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t) {
3742095b 40 return IN_SET(t, UNIT_ACTIVE, UNIT_ACTIVATING, UNIT_RELOADING);
87f0e418
LP
41}
42
43static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t) {
3742095b 44 return IN_SET(t, UNIT_INACTIVE, UNIT_FAILED, UNIT_DEACTIVATING);
6124958c
LP
45}
46
fdf20a31 47static inline bool UNIT_IS_INACTIVE_OR_FAILED(UnitActiveState t) {
3742095b 48 return IN_SET(t, UNIT_INACTIVE, UNIT_FAILED);
87f0e418
LP
49}
50
eef85c4a
LP
51/* Stores the 'reason' a dependency was created as a bit mask, i.e. due to which configuration source it came to be. We
52 * use this so that we can selectively flush out parts of dependencies again. Note that the same dependency might be
53 * created as a result of multiple "reasons", hence the bitmask. */
54typedef enum UnitDependencyMask {
5238e957 55 /* Configured directly by the unit file, .wants/.requires symlink or drop-in, or as an immediate result of a
eef85c4a
LP
56 * non-dependency option configured that way. */
57 UNIT_DEPENDENCY_FILE = 1 << 0,
58
59 /* As unconditional implicit dependency (not affected by unit configuration — except by the unit name and
60 * type) */
61 UNIT_DEPENDENCY_IMPLICIT = 1 << 1,
62
63 /* A dependency effected by DefaultDependencies=yes. Note that dependencies marked this way are conceptually
64 * just a subset of UNIT_DEPENDENCY_FILE, as DefaultDependencies= is itself a unit file setting that can only
65 * be set in unit files. We make this two separate bits only to help debugging how dependencies came to be. */
66 UNIT_DEPENDENCY_DEFAULT = 1 << 2,
67
68 /* A dependency created from udev rules */
69 UNIT_DEPENDENCY_UDEV = 1 << 3,
70
71 /* A dependency created because of some unit's RequiresMountsFor= setting */
72 UNIT_DEPENDENCY_PATH = 1 << 4,
73
74 /* A dependency created because of data read from /proc/self/mountinfo and no other configuration source */
75 UNIT_DEPENDENCY_MOUNTINFO_IMPLICIT = 1 << 5,
76
77 /* A dependency created because of data read from /proc/self/mountinfo, but conditionalized by
78 * DefaultDependencies= and thus also involving configuration from UNIT_DEPENDENCY_FILE sources */
79 UNIT_DEPENDENCY_MOUNTINFO_DEFAULT = 1 << 6,
80
81 /* A dependency created because of data read from /proc/swaps and no other configuration source */
82 UNIT_DEPENDENCY_PROC_SWAP = 1 << 7,
83
ef31828d 84 _UNIT_DEPENDENCY_MASK_FULL = (1 << 8) - 1,
eef85c4a
LP
85} UnitDependencyMask;
86
87/* The Unit's dependencies[] hashmaps use this structure as value. It has the same size as a void pointer, and thus can
88 * be stored directly as hashmap value, without any indirection. Note that this stores two masks, as both the origin
89 * and the destination of a dependency might have created it. */
90typedef union UnitDependencyInfo {
91 void *data;
92 struct {
93 UnitDependencyMask origin_mask:16;
94 UnitDependencyMask destination_mask:16;
95 } _packed_;
96} UnitDependencyInfo;
97
ef734fd6
LP
98#include "job.h"
99
a016b922
LP
100struct UnitRef {
101 /* Keeps tracks of references to a unit. This is useful so
102 * that we can merge two units if necessary and correct all
103 * references to them */
104
7f7d01ed
ZJS
105 Unit *source, *target;
106 LIST_FIELDS(UnitRef, refs_by_target);
a016b922
LP
107};
108
57b7a260 109typedef struct Unit {
87f0e418 110 Manager *manager;
23a177ef 111
87f0e418
LP
112 UnitType type;
113 UnitLoadState load_state;
23a177ef 114 Unit *merged_into;
87f0e418
LP
115
116 char *id; /* One name is special because we use it for identification. Points to an entry in the names set */
9e2f7c11 117 char *instance;
87f0e418
LP
118
119 Set *names;
87f0e418 120
eef85c4a
LP
121 /* For each dependency type we maintain a Hashmap whose key is the Unit* object, and the value encodes why the
122 * dependency exists, using the UnitDependencyInfo type */
123 Hashmap *dependencies[_UNIT_DEPENDENCY_MAX];
124
125 /* Similar, for RequiresMountsFor= path dependencies. The key is the path, the value the UnitDependencyInfo type */
126 Hashmap *requires_mounts_for;
7c8fa05c 127
87f0e418 128 char *description;
49dbfa7b 129 char **documentation;
faf919f1 130
6be1e7d5 131 char *fragment_path; /* if loaded from a config file this is the primary path to it */
1b64d026 132 char *source_path; /* if converted, the source file */
ae7a7182 133 char **dropin_paths;
f78f265f 134
45fb0699 135 usec_t fragment_mtime;
1b64d026 136 usec_t source_mtime;
ae7a7182 137 usec_t dropin_mtime;
87f0e418 138
4f4afc88
LP
139 /* If this is a transient unit we are currently writing, this is where we are writing it to */
140 FILE *transient_file;
141
e0209d83 142 /* If there is something to do with this unit, then this is the installed job for it */
87f0e418
LP
143 Job *job;
144
e0209d83
MS
145 /* JOB_NOP jobs are special and can be installed without disturbing the real job. */
146 Job *nop_job;
147
bbc29086
DM
148 /* The slot used for watching NameOwnerChanged signals */
149 sd_bus_slot *match_bus_slot;
a5a8776a 150 sd_bus_slot *get_name_owner_slot;
bbc29086 151
05a98afd
LP
152 /* References to this unit from clients */
153 sd_bus_track *bus_track;
154 char **deserialized_refs;
155
f189ab18 156 /* Job timeout and action to take */
faf919f1 157 usec_t job_timeout;
a2df3ea4 158 usec_t job_running_timeout;
eae51da3 159 bool job_running_timeout_set:1;
87a47f99 160 EmergencyAction job_timeout_action;
f189ab18 161 char *job_timeout_reboot_arg;
faf919f1 162
57020a3a 163 /* References to this */
7f7d01ed 164 LIST_HEAD(UnitRef, refs_by_target);
57020a3a 165
52661efd
LP
166 /* Conditions to check */
167 LIST_HEAD(Condition, conditions);
59fccdc5 168 LIST_HEAD(Condition, asserts);
52661efd 169
90bbc946 170 dual_timestamp condition_timestamp;
59fccdc5 171 dual_timestamp assert_timestamp;
90bbc946 172
a483fb59
LP
173 /* Updated whenever the low-level state changes */
174 dual_timestamp state_change_timestamp;
175
176 /* Updated whenever the (high-level) active state enters or leaves the active or inactive states */
63983207
LP
177 dual_timestamp inactive_exit_timestamp;
178 dual_timestamp active_enter_timestamp;
179 dual_timestamp active_exit_timestamp;
180 dual_timestamp inactive_enter_timestamp;
87f0e418 181
a016b922
LP
182 UnitRef slice;
183
ef734fd6 184 /* Per type list */
ac155bb8 185 LIST_FIELDS(Unit, units_by_type);
c1e1601e 186
701cc384 187 /* Load queue */
ac155bb8 188 LIST_FIELDS(Unit, load_queue);
701cc384 189
c1e1601e 190 /* D-Bus queue */
ac155bb8 191 LIST_FIELDS(Unit, dbus_queue);
23a177ef
LP
192
193 /* Cleanup queue */
ac155bb8 194 LIST_FIELDS(Unit, cleanup_queue);
9d58f1db 195
701cc384 196 /* GC queue */
ac155bb8 197 LIST_FIELDS(Unit, gc_queue);
701cc384 198
4ad49000 199 /* CGroup realize members queue */
91a6073e 200 LIST_FIELDS(Unit, cgroup_realize_queue);
4ad49000 201
09e24654
LP
202 /* cgroup empty queue */
203 LIST_FIELDS(Unit, cgroup_empty_queue);
204
afcfaa69
LP
205 /* cgroup OOM queue */
206 LIST_FIELDS(Unit, cgroup_oom_queue);
207
19496554
MS
208 /* Target dependencies queue */
209 LIST_FIELDS(Unit, target_deps_queue);
210
a3c1168a
LP
211 /* Queue of units with StopWhenUnneeded set that shell be checked for clean-up. */
212 LIST_FIELDS(Unit, stop_when_unneeded_queue);
213
a911bb9a
LP
214 /* PIDs we keep an eye on. Note that a unit might have many
215 * more, but these are the ones we care enough about to
216 * process SIGCHLD for */
217 Set *pids;
218
62a76913
LP
219 /* Used in SIGCHLD and sd_notify() message event invocation logic to avoid that we dispatch the same event
220 * multiple times on the same unit. */
221 unsigned sigchldgen;
222 unsigned notifygen;
36f20ae3 223
701cc384 224 /* Used during GC sweeps */
eced69b3 225 unsigned gc_marker;
701cc384 226
8821a00f
LP
227 /* Error code when we didn't manage to load the unit (negative) */
228 int load_error;
229
6bf0f408 230 /* Put a ratelimit on unit starting */
7bf081a1 231 RateLimit start_ratelimit;
87a47f99 232 EmergencyAction start_limit_action;
53c35a76 233
7af67e9a
LP
234 /* What to do on failure or success */
235 EmergencyAction success_action, failure_action;
236 int success_action_exit_status, failure_action_exit_status;
6bf0f408
LP
237 char *reboot_arg;
238
67bfdc97
LP
239 /* Make sure we never enter endless loops with the check unneeded logic, or the BindsTo= logic */
240 RateLimit auto_stop_ratelimit;
bea355da 241
00d9ef85
LP
242 /* Reference to a specific UID/GID */
243 uid_t ref_uid;
244 gid_t ref_gid;
245
d2dc52db 246 /* Cached unit file state and preset */
a4375746 247 UnitFileState unit_file_state;
d2dc52db 248 int unit_file_preset;
a4375746 249
66ebf6c0
TH
250 /* Where the cpu.stat or cpuacct.usage was at the time the unit was started */
251 nsec_t cpu_usage_base;
fe700f46 252 nsec_t cpu_usage_last; /* the most recently read value */
5ad096b3 253
afcfaa69
LP
254 /* The current counter of the oom_kill field in the memory.events cgroup attribute */
255 uint64_t oom_kill_last;
256
fbe14fc9
LP
257 /* Where the io.stat data was at the time the unit was started */
258 uint64_t io_accounting_base[_CGROUP_IO_ACCOUNTING_METRIC_MAX];
259 uint64_t io_accounting_last[_CGROUP_IO_ACCOUNTING_METRIC_MAX]; /* the most recently read value */
260
7c52a17b
ZJS
261 /* Counterparts in the cgroup filesystem */
262 char *cgroup_path;
4e1dfa45
CD
263 CGroupMask cgroup_realized_mask; /* In which hierarchies does this unit's cgroup exist? (only relevant on cgroup v1) */
264 CGroupMask cgroup_enabled_mask; /* Which controllers are enabled (or more correctly: enabled for the children) for this unit's cgroup? (only relevant on cgroup v2) */
5238e957 265 CGroupMask cgroup_invalidated_mask; /* A mask specifying controllers which shall be considered invalidated, and require re-realization */
5a62e5e2 266 CGroupMask cgroup_members_mask; /* A cache for the controllers required by all children of this cgroup (only relevant for slice units) */
afcfaa69
LP
267
268 /* Inotify watch descriptors for watching cgroup.events and memory.events on cgroupv2 */
0bb814c2 269 int cgroup_control_inotify_wd;
afcfaa69 270 int cgroup_memory_inotify_wd;
7c52a17b 271
084c7007
RG
272 /* Device Controller BPF program */
273 BPFProgram *bpf_device_control_installed;
274
6a48d82f
DM
275 /* IP BPF Firewalling/accounting */
276 int ip_accounting_ingress_map_fd;
277 int ip_accounting_egress_map_fd;
278
279 int ipv4_allow_map_fd;
280 int ipv6_allow_map_fd;
281 int ipv4_deny_map_fd;
282 int ipv6_deny_map_fd;
283
aa2b6f1d
LP
284 BPFProgram *ip_bpf_ingress, *ip_bpf_ingress_installed;
285 BPFProgram *ip_bpf_egress, *ip_bpf_egress_installed;
fab34748
KL
286 Set *ip_bpf_custom_ingress;
287 Set *ip_bpf_custom_ingress_installed;
288 Set *ip_bpf_custom_egress;
289 Set *ip_bpf_custom_egress_installed;
6a48d82f 290
6b659ed8
LP
291 uint64_t ip_accounting_extra[_CGROUP_IP_ACCOUNTING_METRIC_MAX];
292
50be4f4a
LP
293 /* Low-priority event source which is used to remove watched PIDs that have gone away, and subscribe to any new
294 * ones which might have appeared. */
295 sd_event_source *rewatch_pids_event_source;
296
7c52a17b
ZJS
297 /* How to start OnFailure units */
298 JobMode on_failure_job_mode;
299
5afe510c
LP
300 /* Tweaking the GC logic */
301 CollectMode collect_mode;
302
4b58153d
LP
303 /* The current invocation ID */
304 sd_id128_t invocation_id;
305 char invocation_id_string[SD_ID128_STRING_MAX]; /* useful when logging */
306
9d58f1db
LP
307 /* Garbage collect us we nobody wants or requires us anymore */
308 bool stop_when_unneeded;
309
35b8ca3a 310 /* Create default dependencies */
a40eb732
LP
311 bool default_dependencies;
312
b5e9dba8
LP
313 /* Refuse manual starting, allow starting only indirectly via dependency. */
314 bool refuse_manual_start;
315
316 /* Don't allow the user to stop this unit manually, allow stopping only indirectly via dependency. */
317 bool refuse_manual_stop;
318
2528a7a6
LP
319 /* Allow isolation requests */
320 bool allow_isolate;
321
c8f4d764
LP
322 /* Ignore this unit when isolating */
323 bool ignore_on_isolate;
324
49f43d5f 325 /* Did the last condition check succeed? */
90bbc946 326 bool condition_result;
59fccdc5 327 bool assert_result;
90bbc946 328
c2756a68
LP
329 /* Is this a transient unit? */
330 bool transient;
331
f5869324
LP
332 /* Is this a unit that is always running and cannot be stopped? */
333 bool perpetual;
334
66fa4bdd 335 /* Booleans indicating membership of this unit in the various queues */
9d58f1db
LP
336 bool in_load_queue:1;
337 bool in_dbus_queue:1;
338 bool in_cleanup_queue:1;
701cc384 339 bool in_gc_queue:1;
91a6073e 340 bool in_cgroup_realize_queue:1;
09e24654 341 bool in_cgroup_empty_queue:1;
afcfaa69 342 bool in_cgroup_oom_queue:1;
19496554 343 bool in_target_deps_queue:1;
a3c1168a 344 bool in_stop_when_unneeded_queue:1;
701cc384 345
9d58f1db 346 bool sent_dbus_new_signal:1;
6c073082 347
cd6d0a45 348 bool in_audit:1;
adefcf28 349 bool on_console:1;
a57f7e2c
LP
350
351 bool cgroup_realized:1;
bc432dc7 352 bool cgroup_members_mask_valid:1;
f78f265f 353
3c7416b6
LP
354 /* Reset cgroup accounting next time we fork something off */
355 bool reset_accounting:1;
356
6bf0f408
LP
357 bool start_limit_hit:1;
358
f78f265f 359 /* Did we already invoke unit_coldplug() for this unit? */
f8a30ce5 360 bool coldplugged:1;
05a98afd
LP
361
362 /* For transient units: whether to add a bus track reference after creating the unit */
363 bool bus_track_add:1;
d3070fbd
LP
364
365 /* Remember which unit state files we created */
366 bool exported_invocation_id:1;
367 bool exported_log_level_max:1;
368 bool exported_log_extra_fields:1;
5ac1530e
ZJS
369 bool exported_log_ratelimit_interval:1;
370 bool exported_log_ratelimit_burst:1;
2e59b241 371
527ede0c
FB
372 /* Whether we warned about clamping the CPU quota period */
373 bool warned_clamping_cpu_quota_period:1;
374
2e59b241
LP
375 /* When writing transient unit files, stores which section we stored last. If < 0, we didn't write any yet. If
376 * == 0 we are in the [Unit] section, if > 0 we are in the unit type-specific section. */
845d247a 377 signed int last_section_private:2;
57b7a260 378} Unit;
87f0e418 379
57b7a260 380typedef struct UnitStatusMessageFormats {
c6918296
MS
381 const char *starting_stopping[2];
382 const char *finished_start_job[_JOB_RESULT_MAX];
383 const char *finished_stop_job[_JOB_RESULT_MAX];
57b7a260 384} UnitStatusMessageFormats;
c6918296 385
2e59b241
LP
386/* Flags used when writing drop-in files or transient unit files */
387typedef enum UnitWriteFlags {
388 /* Write a runtime unit file or drop-in (i.e. one below /run) */
389 UNIT_RUNTIME = 1 << 0,
390
391 /* Write a persistent drop-in (i.e. one below /etc) */
392 UNIT_PERSISTENT = 1 << 1,
393
394 /* Place this item in the per-unit-type private section, instead of [Unit] */
395 UNIT_PRIVATE = 1 << 2,
396
397 /* Apply specifier escaping before writing */
398 UNIT_ESCAPE_SPECIFIERS = 1 << 3,
399
400 /* Apply C escaping before writing */
401 UNIT_ESCAPE_C = 1 << 4,
402} UnitWriteFlags;
403
404/* Returns true if neither persistent, nor runtime storage is requested, i.e. this is a check invocation only */
6529ccfa
LP
405static inline bool UNIT_WRITE_FLAGS_NOOP(UnitWriteFlags flags) {
406 return (flags & (UNIT_RUNTIME|UNIT_PERSISTENT)) == 0;
407}
8e2af478 408
57b7a260
FS
409#include "kill.h"
410
411typedef struct UnitVTable {
7d17cfbc
MS
412 /* How much memory does an object of this unit type need */
413 size_t object_size;
414
3ef63c31
LP
415 /* If greater than 0, the offset into the object where
416 * ExecContext is found, if the unit type has that */
417 size_t exec_context_offset;
418
4ad49000
LP
419 /* If greater than 0, the offset into the object where
420 * CGroupContext is found, if the unit type has that */
421 size_t cgroup_context_offset;
422
718db961
LP
423 /* If greater than 0, the offset into the object where
424 * KillContext is found, if the unit type has that */
425 size_t kill_context_offset;
426
613b411c
LP
427 /* If greater than 0, the offset into the object where the
428 * pointer to ExecRuntime is found, if the unit type has
429 * that */
430 size_t exec_runtime_offset;
431
29206d46
LP
432 /* If greater than 0, the offset into the object where the pointer to DynamicCreds is found, if the unit type
433 * has that. */
434 size_t dynamic_creds_offset;
435
ee33e53a 436 /* The name of the configuration file section with the private settings of this unit */
4ad49000 437 const char *private_section;
71645aca 438
f975e971
LP
439 /* Config file sections this unit type understands, separated
440 * by NUL chars */
441 const char *sections;
442
e537352b 443 /* This should reset all type-specific variables. This should
a16e1123
LP
444 * not allocate memory, and is called with zero-initialized
445 * data. It should hence only initialize variables that need
446 * to be set != 0. */
e537352b
LP
447 void (*init)(Unit *u);
448
a16e1123
LP
449 /* This should free all type-specific variables. It should be
450 * idempotent. */
451 void (*done)(Unit *u);
452
e537352b
LP
453 /* Actually load data from disk. This may fail, and should set
454 * load_state to UNIT_LOADED, UNIT_MERGED or leave it at
455 * UNIT_STUB if no configuration could be found. */
456 int (*load)(Unit *u);
457
f0831ed2 458 /* During deserialization we only record the intended state to return to. With coldplug() we actually put the
a95c0505
LP
459 * deserialized state in effect. This is where unit_notify() should be called to start things up. Note that
460 * this callback is invoked *before* we leave the reloading state of the manager, i.e. *before* we consider the
461 * reloading to be complete. Thus, this callback should just restore the exact same state for any unit that was
462 * in effect before the reload, i.e. units should not catch up with changes happened during the reload. That's
463 * what catchup() below is for. */
be847e82 464 int (*coldplug)(Unit *u);
87f0e418 465
a95c0505
LP
466 /* This is called shortly after all units' coldplug() call was invoked, and *after* the manager left the
467 * reloading state. It's supposed to catch up with state changes due to external events we missed so far (for
468 * example because they took place while we were reloading/reexecing) */
f0831ed2
LP
469 void (*catchup)(Unit *u);
470
87f0e418
LP
471 void (*dump)(Unit *u, FILE *f, const char *prefix);
472
473 int (*start)(Unit *u);
474 int (*stop)(Unit *u);
475 int (*reload)(Unit *u);
476
718db961 477 int (*kill)(Unit *u, KillWho w, int signo, sd_bus_error *error);
8a0867d6 478
380dc8b0
LP
479 /* Clear out the various runtime/state/cache/logs/configuration data */
480 int (*clean)(Unit *u, ExecCleanMask m);
481
482 /* Return which kind of data can be cleaned */
483 int (*can_clean)(Unit *u, ExecCleanMask *ret);
484
87f0e418
LP
485 bool (*can_reload)(Unit *u);
486
a16e1123
LP
487 /* Write all data that cannot be restored from other sources
488 * away using unit_serialize_item() */
489 int (*serialize)(Unit *u, FILE *f, FDSet *fds);
490
491 /* Restore one item from the serialization */
492 int (*deserialize_item)(Unit *u, const char *key, const char *data, FDSet *fds);
493
01e10de3 494 /* Try to match up fds with what we need for this unit */
9ff1a6f1 495 void (*distribute_fds)(Unit *u, FDSet *fds);
01e10de3 496
87f0e418
LP
497 /* Boils down the more complex internal state of this unit to
498 * a simpler one that the engine can understand */
499 UnitActiveState (*active_state)(Unit *u);
500
10a94420
LP
501 /* Returns the substate specific to this unit type as
502 * string. This is purely information so that we can give the
35b8ca3a 503 * user a more fine grained explanation in which actual state a
10a94420
LP
504 * unit is in. */
505 const char* (*sub_state_to_string)(Unit *u);
506
deb4e708
MK
507 /* Additionally to UnitActiveState determine whether unit is to be restarted. */
508 bool (*will_restart)(Unit *u);
509
f2f725e5
ZJS
510 /* Return false when there is a reason to prevent this unit from being gc'ed
511 * even though nothing references it and it isn't active in any way. */
512 bool (*may_gc)(Unit *u);
701cc384 513
7eb2a8a1
LP
514 /* When the unit is not running and no job for it queued we shall release its runtime resources */
515 void (*release_resources)(Unit *u);
a354329f 516
718db961 517 /* Invoked on every child that died */
87f0e418 518 void (*sigchld_event)(Unit *u, pid_t pid, int code, int status);
7824bbeb 519
fdf20a31
MM
520 /* Reset failed state if we are in failed state */
521 void (*reset_failed)(Unit *u);
5632e374 522
afcfaa69 523 /* Called whenever any of the cgroups this unit watches for ran empty */
4ad49000 524 void (*notify_cgroup_empty)(Unit *u);
8e274523 525
afcfaa69
LP
526 /* Called whenever an OOM kill event on this unit was seen */
527 void (*notify_cgroup_oom)(Unit *u);
528
8c47c732 529 /* Called whenever a process of this unit sends us a message */
db256aab 530 void (*notify_message)(Unit *u, const struct ucred *ucred, char **tags, FDSet *fds);
8c47c732 531
00d9ef85 532 /* Called whenever a name this Unit registered for comes or goes away. */
a5a8776a 533 void (*bus_name_owner_change)(Unit *u, const char *old_owner, const char *new_owner);
05e343b7 534
8e2af478 535 /* Called for each property that is being set */
2e59b241 536 int (*bus_set_property)(Unit *u, const char *name, sd_bus_message *message, UnitWriteFlags flags, sd_bus_error *error);
8e2af478
LP
537
538 /* Called after at least one property got changed to apply the necessary change */
539 int (*bus_commit_properties)(Unit *u);
540
a7f241db
LP
541 /* Return the unit this unit is following */
542 Unit *(*following)(Unit *u);
543
6210e7fc
LP
544 /* Return the set of units that are following each other */
545 int (*following_set)(Unit *u, Set **s);
546
3ecaa09b
LP
547 /* Invoked each time a unit this unit is triggering changes
548 * state or gains/loses a job */
549 void (*trigger_notify)(Unit *u, Unit *trigger);
550
8742514c
LP
551 /* Called whenever CLOCK_REALTIME made a jump */
552 void (*time_change)(Unit *u);
553
bbf5fd8e
LP
554 /* Called whenever /etc/localtime was modified */
555 void (*timezone_change)(Unit *u);
556
7a7821c8
LP
557 /* Returns the next timeout of a unit */
558 int (*get_timeout)(Unit *u, usec_t *timeout);
68db7a3b 559
291d565a
LP
560 /* Returns the main PID if there is any defined, or 0. */
561 pid_t (*main_pid)(Unit *u);
562
563 /* Returns the main PID if there is any defined, or 0. */
564 pid_t (*control_pid)(Unit *u);
565
bb2c7685
LP
566 /* Returns true if the unit currently needs access to the console */
567 bool (*needs_console)(Unit *u);
568
7af67e9a
LP
569 /* Returns the exit status to propagate in case of FailureAction=exit/SuccessAction=exit; usually returns the
570 * exit code of the "main" process of the service or similar. */
571 int (*exit_status)(Unit *u);
572
04eb582a
LP
573 /* Like the enumerate() callback further down, but only enumerates the perpetual units, i.e. all units that
574 * unconditionally exist and are always active. The main reason to keep both enumeration functions separate is
575 * philosophical: the state of perpetual units should be put in place by coldplug(), while the state of those
576 * discovered through regular enumeration should be put in place by catchup(), see below. */
577 void (*enumerate_perpetual)(Manager *m);
578
f0831ed2
LP
579 /* This is called for each unit type and should be used to enumerate units already existing in the system
580 * internally and load them. However, everything that is loaded here should still stay in inactive state. It is
04eb582a 581 * the job of the catchup() call above to put the units into the discovered state. */
ba64af90 582 void (*enumerate)(Manager *m);
f50e0a01
LP
583
584 /* Type specific cleanups. */
7824bbeb 585 void (*shutdown)(Manager *m);
9d58f1db 586
0faacd47
LP
587 /* If this function is set and return false all jobs for units
588 * of this type will immediately fail. */
1c2e9646 589 bool (*supported)(void);
0faacd47 590
718db961
LP
591 /* The bus vtable */
592 const sd_bus_vtable *bus_vtable;
593
718db961 594 /* The strings to print in status messages */
c6918296
MS
595 UnitStatusMessageFormats status_message_formats;
596
c2756a68
LP
597 /* True if transient units of this type are OK */
598 bool can_transient:1;
c5a97ed1 599
1d9cc876
LP
600 /* True if cgroup delegation is permissible */
601 bool can_delegate:1;
602
c80a9a33
LP
603 /* True if the unit type triggers other units, i.e. can have a UNIT_TRIGGERS dependency */
604 bool can_trigger:1;
605
606 /* True if the unit type knows a failure state, and thus can be source of an OnFailure= dependency */
607 bool can_fail:1;
608
609 /* True if After= dependencies should be refused */
610 bool refuse_after:1;
611
d4fd1cf2
LP
612 /* True if units of this type shall be startable only once and then never again */
613 bool once_only:1;
614
c5a97ed1
LP
615 /* True if queued jobs of this type should be GC'ed if no other job needs them anymore */
616 bool gc_jobs:1;
57b7a260 617} UnitVTable;
87f0e418
LP
618
619extern const UnitVTable * const unit_vtable[_UNIT_TYPE_MAX];
620
6529ccfa
LP
621static inline const UnitVTable* UNIT_VTABLE(Unit *u) {
622 return unit_vtable[u->type];
623}
87f0e418
LP
624
625/* For casting a unit into the various unit types */
626#define DEFINE_CAST(UPPERCASE, MixedCase) \
627 static inline MixedCase* UPPERCASE(Unit *u) { \
ac155bb8 628 if (_unlikely_(!u || u->type != UNIT_##UPPERCASE)) \
87f0e418
LP
629 return NULL; \
630 \
631 return (MixedCase*) u; \
632 }
633
634/* For casting the various unit types into a unit */
bbf11206
LP
635#define UNIT(u) \
636 ({ \
637 typeof(u) _u_ = (u); \
638 Unit *_w_ = _u_ ? &(_u_)->meta : NULL; \
639 _w_; \
640 })
87f0e418 641
35b7ff80
LP
642#define UNIT_HAS_EXEC_CONTEXT(u) (UNIT_VTABLE(u)->exec_context_offset > 0)
643#define UNIT_HAS_CGROUP_CONTEXT(u) (UNIT_VTABLE(u)->cgroup_context_offset > 0)
644#define UNIT_HAS_KILL_CONTEXT(u) (UNIT_VTABLE(u)->kill_context_offset > 0)
645
6529ccfa
LP
646static inline Unit* UNIT_TRIGGER(Unit *u) {
647 return hashmap_first_key(u->dependencies[UNIT_TRIGGERS]);
648}
3ecaa09b 649
7d17cfbc 650Unit *unit_new(Manager *m, size_t size);
87f0e418 651void unit_free(Unit *u);
dc409696 652DEFINE_TRIVIAL_CLEANUP_FUNC(Unit *, unit_free);
87f0e418 653
a581e45a 654int unit_new_for_name(Manager *m, size_t size, const char *name, Unit **ret);
87f0e418 655int unit_add_name(Unit *u, const char *name);
9e2f7c11 656
eef85c4a
LP
657int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_reference, UnitDependencyMask mask);
658int unit_add_two_dependencies(Unit *u, UnitDependency d, UnitDependency e, Unit *other, bool add_reference, UnitDependencyMask mask);
2c966c03 659
35d8c19a 660int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, bool add_reference, UnitDependencyMask mask);
5a724170 661int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, bool add_reference, UnitDependencyMask mask);
2c966c03 662
23a177ef
LP
663int unit_add_exec_dependencies(Unit *u, ExecContext *c);
664
0ae97ec1 665int unit_choose_id(Unit *u, const char *name);
f50e0a01 666int unit_set_description(Unit *u, const char *description);
87f0e418 667
f2f725e5 668bool unit_may_gc(Unit *u);
701cc384 669
87f0e418 670void unit_add_to_load_queue(Unit *u);
c1e1601e 671void unit_add_to_dbus_queue(Unit *u);
23a177ef 672void unit_add_to_cleanup_queue(Unit *u);
701cc384 673void unit_add_to_gc_queue(Unit *u);
19496554 674void unit_add_to_target_deps_queue(Unit *u);
fda09318 675void unit_submit_to_stop_when_unneeded_queue(Unit *u);
87f0e418
LP
676
677int unit_merge(Unit *u, Unit *other);
23a177ef
LP
678int unit_merge_by_name(Unit *u, const char *other);
679
44a6b1b6 680Unit *unit_follow_merge(Unit *u) _pure_;
87f0e418 681
c3620770 682int unit_load_fragment_and_dropin(Unit *u, bool fragment_required);
87f0e418
LP
683int unit_load(Unit *unit);
684
d79200e2
LP
685int unit_set_slice(Unit *u, Unit *slice);
686int unit_set_default_slice(Unit *u);
a016b922 687
44a6b1b6 688const char *unit_description(Unit *u) _pure_;
2a8f53c6 689const char *unit_status_string(Unit *u) _pure_;
87f0e418 690
303ee601 691bool unit_has_name(const Unit *u, const char *name);
f278026d 692
87f0e418
LP
693UnitActiveState unit_active_state(Unit *u);
694
10a94420
LP
695const char* unit_sub_state_to_string(Unit *u);
696
87f0e418
LP
697void unit_dump(Unit *u, FILE *f, const char *prefix);
698
44a6b1b6
ZJS
699bool unit_can_reload(Unit *u) _pure_;
700bool unit_can_start(Unit *u) _pure_;
f5869324 701bool unit_can_stop(Unit *u) _pure_;
44a6b1b6 702bool unit_can_isolate(Unit *u) _pure_;
87f0e418
LP
703
704int unit_start(Unit *u);
705int unit_stop(Unit *u);
706int unit_reload(Unit *u);
707
718db961
LP
708int unit_kill(Unit *u, KillWho w, int signo, sd_bus_error *error);
709int unit_kill_common(Unit *u, KillWho who, int signo, pid_t main_pid, pid_t control_pid, sd_bus_error *error);
8a0867d6 710
2ad2e41a 711typedef enum UnitNotifyFlags {
ef31828d
LP
712 UNIT_NOTIFY_RELOAD_FAILURE = 1 << 0,
713 UNIT_NOTIFY_WILL_AUTO_RESTART = 1 << 1,
31cd5f63 714 UNIT_NOTIFY_SKIP_CONDITION = 1 << 2,
2ad2e41a
LP
715} UnitNotifyFlags;
716
717void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, UnitNotifyFlags flags);
87f0e418 718
f75f613d 719int unit_watch_pid(Unit *u, pid_t pid, bool exclusive);
87f0e418 720void unit_unwatch_pid(Unit *u, pid_t pid);
a911bb9a
LP
721void unit_unwatch_all_pids(Unit *u);
722
50be4f4a
LP
723int unit_enqueue_rewatch_pids(Unit *u);
724void unit_dequeue_rewatch_pids(Unit *u);
87f0e418 725
9806e87d 726int unit_install_bus_match(Unit *u, sd_bus *bus, const char *name);
05e343b7
LP
727int unit_watch_bus_name(Unit *u, const char *name);
728void unit_unwatch_bus_name(Unit *u, const char *name);
729
87f0e418
LP
730bool unit_job_is_applicable(Unit *u, JobType j);
731
0301abf4
LP
732int set_unit_path(const char *p);
733
50159e6a 734char *unit_dbus_path(Unit *u);
4b58153d 735char *unit_dbus_path_invocation_id(Unit *u);
50159e6a 736
f6ff8c29
LP
737int unit_load_related_unit(Unit *u, const char *type, Unit **_found);
738
44a6b1b6 739bool unit_can_serialize(Unit *u) _pure_;
a34ceba6 740
6b78f9b4 741int unit_serialize(Unit *u, FILE *f, FDSet *fds, bool serialize_jobs);
a16e1123 742int unit_deserialize(Unit *u, FILE *f, FDSet *fds);
8948b341 743int unit_deserialize_skip(FILE *f);
a16e1123 744
d336ba9f 745int unit_add_node_dependency(Unit *u, const char *what, UnitDependency d, UnitDependencyMask mask);
6e2ef85b 746
be847e82 747int unit_coldplug(Unit *u);
f0831ed2 748void unit_catchup(Unit *u);
cca098b0 749
44b601bc 750void unit_status_printf(Unit *u, const char *status, const char *unit_status_msg_format) _printf_(3, 0);
9e58ff9c 751
45fb0699
LP
752bool unit_need_daemon_reload(Unit *u);
753
fdf20a31 754void unit_reset_failed(Unit *u);
5632e374 755
a7f241db 756Unit *unit_following(Unit *u);
eeaedb7c 757int unit_following_set(Unit *u, Set **s);
a7f241db 758
9444b1f2
LP
759const char *unit_slice_name(Unit *u);
760
44a6b1b6
ZJS
761bool unit_stop_pending(Unit *u) _pure_;
762bool unit_inactive_or_pending(Unit *u) _pure_;
31afa0a4 763bool unit_active_or_pending(Unit *u);
52a12341 764bool unit_will_restart_default(Unit *u);
deb4e708 765bool unit_will_restart(Unit *u);
18ffdfda 766
bba34eed
LP
767int unit_add_default_target_dependency(Unit *u, Unit *target);
768
3ecaa09b
LP
769void unit_start_on_failure(Unit *u);
770void unit_trigger_notify(Unit *u);
c0daa706 771
a4375746 772UnitFileState unit_get_unit_file_state(Unit *u);
d2dc52db 773int unit_get_unit_file_preset(Unit *u);
a4375746 774
7f7d01ed 775Unit* unit_ref_set(UnitRef *ref, Unit *source, Unit *target);
57020a3a
LP
776void unit_ref_unset(UnitRef *ref);
777
7f7d01ed
ZJS
778#define UNIT_DEREF(ref) ((ref).target)
779#define UNIT_ISSET(ref) (!!(ref).target)
57020a3a 780
598459ce 781int unit_patch_contexts(Unit *u);
e06c73cc 782
44a6b1b6 783ExecContext *unit_get_exec_context(Unit *u) _pure_;
718db961 784KillContext *unit_get_kill_context(Unit *u) _pure_;
4ad49000 785CGroupContext *unit_get_cgroup_context(Unit *u) _pure_;
598459ce 786
613b411c
LP
787ExecRuntime *unit_get_exec_runtime(Unit *u) _pure_;
788
789int unit_setup_exec_runtime(Unit *u);
29206d46 790int unit_setup_dynamic_creds(Unit *u);
3ef63c31 791
2e59b241
LP
792char* unit_escape_setting(const char *s, UnitWriteFlags flags, char **buf);
793char* unit_concat_strv(char **l, UnitWriteFlags flags);
b9ec9359 794
2e59b241
LP
795int unit_write_setting(Unit *u, UnitWriteFlags flags, const char *name, const char *data);
796int unit_write_settingf(Unit *u, UnitWriteFlags mode, const char *name, const char *format, ...) _printf_(4,5);
b9ec9359 797
db2cb23b 798int unit_kill_context(Unit *u, KillContext *c, KillOperation k, pid_t main_pid, pid_t control_pid, bool main_pid_alien);
cd2086fe 799
c2756a68
LP
800int unit_make_transient(Unit *u);
801
eef85c4a 802int unit_require_mounts_for(Unit *u, const char *path, UnitDependencyMask mask);
a57f7e2c 803
1c2e9646
LP
804bool unit_type_supported(UnitType t);
805
0f13f3bd
LP
806bool unit_is_pristine(Unit *u);
807
a3c1168a
LP
808bool unit_is_unneeded(Unit *u);
809
291d565a
LP
810pid_t unit_control_pid(Unit *u);
811pid_t unit_main_pid(Unit *u);
812
8b4305c7 813void unit_warn_if_dir_nonempty(Unit *u, const char* where);
25cd4964 814int unit_fail_if_noncanonical(Unit *u, const char* where);
8b4305c7 815
97a3f4ee 816int unit_test_start_limit(Unit *u);
07299350 817
00d9ef85
LP
818void unit_unref_uid(Unit *u, bool destroy_now);
819int unit_ref_uid(Unit *u, uid_t uid, bool clean_ipc);
820
821void unit_unref_gid(Unit *u, bool destroy_now);
822int unit_ref_gid(Unit *u, gid_t gid, bool clean_ipc);
823
824int unit_ref_uid_gid(Unit *u, uid_t uid, gid_t gid);
825void unit_unref_uid_gid(Unit *u, bool destroy_now);
826
827void unit_notify_user_lookup(Unit *u, uid_t uid, gid_t gid);
828
4b58153d
LP
829int unit_set_invocation_id(Unit *u, sd_id128_t id);
830int unit_acquire_invocation_id(Unit *u);
831
c891efaf
FB
832bool unit_shall_confirm_spawn(Unit *u);
833
1ad6e8b3 834int unit_set_exec_params(Unit *s, ExecParameters *p);
f0d47797 835
4c253ed1 836int unit_fork_helper_process(Unit *u, const char *name, pid_t *ret);
810ef318 837int unit_fork_and_watch_rm_rf(Unit *u, char **paths, pid_t *ret_pid);
a79279c7 838
c999cf38
LP
839void unit_remove_dependencies(Unit *u, UnitDependencyMask mask);
840
d3070fbd
LP
841void unit_export_state_files(Unit *u);
842void unit_unlink_state_files(Unit *u);
843
3c7416b6
LP
844int unit_prepare_exec(Unit *u);
845
c53d2d54 846int unit_warn_leftover_processes(Unit *u);
a4634b21 847
bb2c7685
LP
848bool unit_needs_console(Unit *u);
849
81e9871e
LP
850const char *unit_label_path(Unit *u);
851
6592b975
LP
852int unit_pid_attachable(Unit *unit, pid_t pid, sd_bus_error *error);
853
28a2dfe8
ZJS
854static inline bool unit_has_job_type(Unit *u, JobType type) {
855 return u && u->job && u->job->type == type;
856}
857
31cd5f63
AZ
858/* unit_log_skip is for cases like ExecCondition= where a unit is considered "done"
859 * after some execution, rather than succeeded or failed. */
860void unit_log_skip(Unit *u, const char *result);
523ee2d4 861void unit_log_success(Unit *u);
7c047d74 862void unit_log_failure(Unit *u, const char *result);
aac99f30
ZJS
863static inline void unit_log_result(Unit *u, bool success, const char *result) {
864 if (success)
865 unit_log_success(u);
866 else
867 unit_log_failure(u, result);
868}
869
5cc2cd1c 870void unit_log_process_exit(Unit *u, const char *kind, const char *command, bool success, int code, int status);
7c047d74 871
7af67e9a
LP
872int unit_exit_status(Unit *u);
873int unit_success_action_exit_status(Unit *u);
874int unit_failure_action_exit_status(Unit *u);
875
a4191c9f
LP
876int unit_test_trigger_loaded(Unit *u);
877
95939aed 878void unit_destroy_runtime_directory(Unit *u, const ExecContext *context);
380dc8b0
LP
879int unit_clean(Unit *u, ExecCleanMask mask);
880int unit_can_clean(Unit *u, ExecCleanMask *ret_mask);
881
e8e581bf
ZJS
882/* Macros which append UNIT= or USER_UNIT= to the message */
883
f2341e0a
LP
884#define log_unit_full(unit, level, error, ...) \
885 ({ \
8dec4a9d 886 const Unit *_u = (unit); \
62c6bbbc
ZJS
887 _u ? log_object_internal(level, error, PROJECT_FILE, __LINE__, __func__, _u->manager->unit_log_field, _u->id, _u->manager->invocation_log_field, _u->invocation_id_string, ##__VA_ARGS__) : \
888 log_internal(level, error, PROJECT_FILE, __LINE__, __func__, ##__VA_ARGS__); \
f2341e0a
LP
889 })
890
891#define log_unit_debug(unit, ...) log_unit_full(unit, LOG_DEBUG, 0, ##__VA_ARGS__)
892#define log_unit_info(unit, ...) log_unit_full(unit, LOG_INFO, 0, ##__VA_ARGS__)
893#define log_unit_notice(unit, ...) log_unit_full(unit, LOG_NOTICE, 0, ##__VA_ARGS__)
894#define log_unit_warning(unit, ...) log_unit_full(unit, LOG_WARNING, 0, ##__VA_ARGS__)
895#define log_unit_error(unit, ...) log_unit_full(unit, LOG_ERR, 0, ##__VA_ARGS__)
896
897#define log_unit_debug_errno(unit, error, ...) log_unit_full(unit, LOG_DEBUG, error, ##__VA_ARGS__)
898#define log_unit_info_errno(unit, error, ...) log_unit_full(unit, LOG_INFO, error, ##__VA_ARGS__)
899#define log_unit_notice_errno(unit, error, ...) log_unit_full(unit, LOG_NOTICE, error, ##__VA_ARGS__)
900#define log_unit_warning_errno(unit, error, ...) log_unit_full(unit, LOG_WARNING, error, ##__VA_ARGS__)
901#define log_unit_error_errno(unit, error, ...) log_unit_full(unit, LOG_ERR, error, ##__VA_ARGS__)
902
903#define LOG_UNIT_MESSAGE(unit, fmt, ...) "MESSAGE=%s: " fmt, (unit)->id, ##__VA_ARGS__
904#define LOG_UNIT_ID(unit) (unit)->manager->unit_log_format_string, (unit)->id
f1c50bec 905#define LOG_UNIT_INVOCATION_ID(unit) (unit)->manager->invocation_log_format_string, (unit)->invocation_id_string
5afe510c
LP
906
907const char* collect_mode_to_string(CollectMode m) _const_;
908CollectMode collect_mode_from_string(const char *s) _pure_;