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