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