1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "core-forward.h"
7 #include "emergency-action.h"
15 #include "log-context.h"
16 #include "ratelimit.h"
17 #include "time-util.h"
19 #include "unit-dependency-atom.h"
20 #include "unit-file.h"
22 typedef enum UnitMountDependencyType
{
25 _UNIT_MOUNT_DEPENDENCY_TYPE_MAX
,
26 _UNIT_MOUNT_DEPENDENCY_TYPE_INVALID
= -EINVAL
,
27 } UnitMountDependencyType
;
29 typedef enum KillOperation
{
31 KILL_TERMINATE_AND_LOG
,
36 _KILL_OPERATION_INVALID
= -EINVAL
,
39 typedef enum CollectMode
{
41 COLLECT_INACTIVE_OR_FAILED
,
43 _COLLECT_MODE_INVALID
= -EINVAL
,
46 typedef enum OOMPolicy
{
47 OOM_CONTINUE
, /* The kernel or systemd-oomd kills the process it wants to kill, and that's it */
48 OOM_STOP
, /* The kernel or systemd-oomd kills the process it wants to kill, and we stop the unit */
49 OOM_KILL
, /* The kernel or systemd-oomd kills the process it wants to kill, and all others in the unit, and we stop the unit */
51 _OOM_POLICY_INVALID
= -EINVAL
,
54 typedef enum StatusType
{
55 STATUS_TYPE_EPHEMERAL
, /* ordered by severity! Do not break order */
58 STATUS_TYPE_EMERGENCY
,
60 _STATUS_TYPE_INVALID
= -EINVAL
,
63 static inline bool UNIT_IS_ACTIVE_OR_RELOADING(UnitActiveState t
) {
64 return IN_SET(t
, UNIT_ACTIVE
, UNIT_RELOADING
, UNIT_REFRESHING
);
67 static inline bool UNIT_IS_ACTIVE_OR_ACTIVATING(UnitActiveState t
) {
68 return IN_SET(t
, UNIT_ACTIVE
, UNIT_ACTIVATING
, UNIT_RELOADING
, UNIT_REFRESHING
);
71 static inline bool UNIT_IS_INACTIVE_OR_DEACTIVATING(UnitActiveState t
) {
72 return IN_SET(t
, UNIT_INACTIVE
, UNIT_FAILED
, UNIT_DEACTIVATING
);
75 static inline bool UNIT_IS_INACTIVE_OR_FAILED(UnitActiveState t
) {
76 return IN_SET(t
, UNIT_INACTIVE
, UNIT_FAILED
);
79 static inline bool UNIT_IS_LOAD_COMPLETE(UnitLoadState t
) {
80 return t
>= 0 && t
< _UNIT_LOAD_STATE_MAX
&& !IN_SET(t
, UNIT_STUB
, UNIT_MERGED
);
83 static inline bool UNIT_IS_LOAD_ERROR(UnitLoadState t
) {
84 return IN_SET(t
, UNIT_NOT_FOUND
, UNIT_BAD_SETTING
, UNIT_ERROR
);
87 /* Stores the 'reason' a dependency was created as a bit mask, i.e. due to which configuration source it came to be. We
88 * use this so that we can selectively flush out parts of dependencies again. Note that the same dependency might be
89 * created as a result of multiple "reasons", hence the bitmask. */
90 typedef enum UnitDependencyMask
{
91 /* Configured directly by the unit file, .wants/.requires symlink or drop-in, or as an immediate result of a
92 * non-dependency option configured that way. */
93 UNIT_DEPENDENCY_FILE
= 1 << 0,
95 /* As unconditional implicit dependency (not affected by unit configuration — except by the unit name and
97 UNIT_DEPENDENCY_IMPLICIT
= 1 << 1,
99 /* A dependency affected by DefaultDependencies=yes. Note that dependencies marked this way are conceptually
100 * just a subset of UNIT_DEPENDENCY_FILE, as DefaultDependencies= is itself a unit file setting that can only
101 * be set in unit files. We make this two separate bits only to help debugging how dependencies came to be. */
102 UNIT_DEPENDENCY_DEFAULT
= 1 << 2,
104 /* A dependency created from udev rules */
105 UNIT_DEPENDENCY_UDEV
= 1 << 3,
107 /* A dependency created because of some unit's RequiresMountsFor= setting */
108 UNIT_DEPENDENCY_PATH
= 1 << 4,
110 /* A dependency initially configured from the mount unit file however the dependency will be updated
111 * from /proc/self/mountinfo as soon as the kernel will make the entry for that mount available in
113 UNIT_DEPENDENCY_MOUNT_FILE
= 1 << 5,
115 /* A dependency created or updated because of data read from /proc/self/mountinfo */
116 UNIT_DEPENDENCY_MOUNTINFO
= 1 << 6,
118 /* A dependency created because of data read from /proc/swaps and no other configuration source */
119 UNIT_DEPENDENCY_PROC_SWAP
= 1 << 7,
121 /* A dependency for units in slices assigned by directly setting Slice= */
122 UNIT_DEPENDENCY_SLICE_PROPERTY
= 1 << 8,
124 _UNIT_DEPENDENCY_MASK_FULL
= (1 << 9) - 1,
125 } UnitDependencyMask
;
127 /* The Unit's dependencies[] hashmaps use this structure as value. It has the same size as a void pointer, and thus can
128 * be stored directly as hashmap value, without any indirection. Note that this stores two masks, as both the origin
129 * and the destination of a dependency might have created it. */
130 typedef union UnitDependencyInfo
{
133 UnitDependencyMask origin_mask
:16;
134 UnitDependencyMask destination_mask
:16;
136 } UnitDependencyInfo
;
138 /* Store information about why a unit was activated.
139 * We start with trigger units (.path/.timer), eventually it will be expanded to include more metadata. */
140 typedef struct ActivationDetails
{
142 UnitType trigger_unit_type
;
143 char *trigger_unit_name
;
146 /* For casting an activation event into the various unit-specific types */
147 #define DEFINE_ACTIVATION_DETAILS_CAST(UPPERCASE, MixedCase, UNIT_TYPE) \
148 static inline MixedCase* UPPERCASE(const ActivationDetails *a) { \
149 if (_unlikely_(!a || a->trigger_unit_type != UNIT_##UNIT_TYPE)) \
152 return (MixedCase*) a; \
155 /* For casting the various unit types into a unit */
156 #define ACTIVATION_DETAILS(u) \
158 typeof(u) _u_ = (u); \
159 ActivationDetails *_w_ = _u_ ? &(_u_)->meta : NULL; \
163 ActivationDetails
*activation_details_new(Unit
*trigger_unit
);
164 ActivationDetails
*activation_details_ref(ActivationDetails
*p
);
165 ActivationDetails
*activation_details_unref(ActivationDetails
*p
);
166 void activation_details_serialize(const ActivationDetails
*p
, FILE *f
);
167 int activation_details_deserialize(const char *key
, const char *value
, ActivationDetails
**info
);
168 int activation_details_append_env(const ActivationDetails
*info
, char ***strv
);
169 int activation_details_append_pair(const ActivationDetails
*info
, char ***strv
);
170 DEFINE_TRIVIAL_CLEANUP_FUNC(ActivationDetails
*, activation_details_unref
);
172 typedef struct ActivationDetailsVTable
{
173 /* How much memory does an object of this activation type need */
176 /* This should reset all type-specific variables. This should not allocate memory, and is called
177 * with zero-initialized data. It should hence only initialize variables that need to be set != 0. */
178 void (*init
)(ActivationDetails
*info
, Unit
*trigger_unit
);
180 /* This should free all type-specific variables. It should be idempotent. */
181 void (*done
)(ActivationDetails
*info
);
183 /* This should serialize all type-specific variables. */
184 void (*serialize
)(const ActivationDetails
*info
, FILE *f
);
186 /* This should deserialize all type-specific variables, one at a time. */
187 int (*deserialize
)(const char *key
, const char *value
, ActivationDetails
**info
);
189 /* This should format the type-specific variables for the env block of the spawned service,
190 * and return the number of added items. */
191 int (*append_env
)(const ActivationDetails
*info
, char ***strv
);
193 /* This should append type-specific variables as key/value pairs for the D-Bus property of the job,
194 * and return the number of added pairs. */
195 int (*append_pair
)(const ActivationDetails
*info
, char ***strv
);
196 } ActivationDetailsVTable
;
198 extern const ActivationDetailsVTable
* const activation_details_vtable
[_UNIT_TYPE_MAX
];
200 static inline const ActivationDetailsVTable
* ACTIVATION_DETAILS_VTABLE(const ActivationDetails
*a
) {
202 assert(a
->trigger_unit_type
< _UNIT_TYPE_MAX
);
204 return activation_details_vtable
[a
->trigger_unit_type
];
207 /* Newer LLVM versions don't like implicit casts from large pointer types to smaller enums, hence let's add
208 * explicit type-safe helpers for that. */
209 static inline UnitDependency
UNIT_DEPENDENCY_FROM_PTR(const void *p
) {
210 return PTR_TO_INT(p
);
213 static inline void* UNIT_DEPENDENCY_TO_PTR(UnitDependency d
) {
214 return INT_TO_PTR(d
);
217 typedef struct UnitRef
{
218 /* Keeps tracks of references to a unit. This is useful so
219 * that we can merge two units if necessary and correct all
220 * references to them */
222 Unit
*source
, *target
;
223 LIST_FIELDS(UnitRef
, refs_by_target
);
226 /* The generic, dynamic definition of the unit */
227 typedef struct Unit
{
231 UnitLoadState load_state
;
234 char *id
; /* The one special name that we use for identification */
237 Set
*aliases
; /* All the other names. */
239 /* For each dependency type we can look up another Hashmap with this, whose key is a Unit* object,
240 * and whose value encodes why the dependency exists, using the UnitDependencyInfo type. i.e. a
241 * Hashmap(UnitDependency → Hashmap(Unit* → UnitDependencyInfo)) */
242 Hashmap
*dependencies
;
243 uint64_t dependency_generation
;
245 /* Similar, for RequiresMountsFor= and WantsMountsFor= path dependencies. The key is the path, the
246 * value the UnitDependencyInfo type */
247 Hashmap
*mounts_for
[_UNIT_MOUNT_DEPENDENCY_TYPE_MAX
];
250 char **documentation
;
252 /* The SELinux context used for checking access to this unit read off the unit file at load time (do
253 * not confuse with the selinux_context field in ExecContext which is the SELinux context we'll set
255 char *access_selinux_context
;
257 char *fragment_path
; /* if loaded from a config file this is the primary path to it */
258 char *source_path
; /* if converted, the source file */
261 usec_t fragment_not_found_timestamp_hash
;
262 usec_t fragment_mtime
;
266 /* If this is a transient unit we are currently writing, this is where we are writing it to */
267 FILE *transient_file
;
270 sd_bus_message
*pending_freezer_invocation
;
271 FreezerState freezer_state
;
273 /* Job timeout and action to take */
274 EmergencyAction job_timeout_action
;
276 usec_t job_running_timeout
;
277 char *job_timeout_reboot_arg
;
279 /* If there is something to do with this unit, then this is the installed job for it */
282 /* JOB_NOP jobs are special and can be installed without disturbing the real job. */
285 /* The slot used for watching NameOwnerChanged signals */
286 sd_bus_slot
*match_bus_slot
;
287 sd_bus_slot
*get_name_owner_slot
;
289 /* References to this unit from clients */
290 sd_bus_track
*bus_track
;
291 char **deserialized_refs
;
293 /* References to this */
294 LIST_HEAD(UnitRef
, refs_by_target
);
296 /* Conditions to check */
297 LIST_HEAD(Condition
, conditions
);
298 LIST_HEAD(Condition
, asserts
);
300 dual_timestamp condition_timestamp
;
301 dual_timestamp assert_timestamp
;
303 /* Updated whenever the low-level state changes */
304 dual_timestamp state_change_timestamp
;
306 /* Updated whenever the (high-level) active state enters or leaves the active or inactive states */
307 dual_timestamp inactive_exit_timestamp
;
308 dual_timestamp active_enter_timestamp
;
309 dual_timestamp active_exit_timestamp
;
310 dual_timestamp inactive_enter_timestamp
;
313 LIST_FIELDS(Unit
, units_by_type
);
316 LIST_FIELDS(Unit
, load_queue
);
319 LIST_FIELDS(Unit
, dbus_queue
);
322 LIST_FIELDS(Unit
, cleanup_queue
);
325 LIST_FIELDS(Unit
, gc_queue
);
327 /* CGroup realize members queue */
328 LIST_FIELDS(Unit
, cgroup_realize_queue
);
330 /* cgroup empty queue */
331 LIST_FIELDS(Unit
, cgroup_empty_queue
);
333 /* cgroup OOM queue */
334 LIST_FIELDS(Unit
, cgroup_oom_queue
);
336 /* Target dependencies queue */
337 LIST_FIELDS(Unit
, target_deps_queue
);
339 /* Queue of units with StopWhenUnneeded= set that shall be checked for clean-up. */
340 LIST_FIELDS(Unit
, stop_when_unneeded_queue
);
342 /* Queue of units that have an Uphold= dependency from some other unit, and should be checked for starting */
343 LIST_FIELDS(Unit
, start_when_upheld_queue
);
345 /* Queue of units that have a BindTo= dependency on some other unit, and should possibly be shut down */
346 LIST_FIELDS(Unit
, stop_when_bound_queue
);
348 /* Queue of units that should be checked if they can release resources now */
349 LIST_FIELDS(Unit
, release_resources_queue
);
351 /* Queue of units that should be informed when other units stop */
352 LIST_FIELDS(Unit
, stop_notify_queue
);
354 /* PIDs we keep an eye on. Note that a unit might have many more, but these are the ones we care
355 * enough about to process SIGCHLD for */
356 Set
*pids
; /* → PidRef* */
358 /* Used in SIGCHLD and sd_notify() message event invocation logic to avoid that we dispatch the same event
359 * multiple times on the same unit. */
363 /* Used during GC sweeps */
366 /* Error code when we didn't manage to load the unit (negative) */
369 /* Put a ratelimit on unit starting */
370 RateLimit start_ratelimit
;
371 EmergencyAction start_limit_action
;
373 /* The unit has been marked for reload, restart, etc. Stored as 1u << marker1 | 1u << marker2. */
376 /* What to do on failure or success */
377 EmergencyAction success_action
, failure_action
;
378 int success_action_exit_status
, failure_action_exit_status
;
381 /* Make sure we never enter endless loops with the StopWhenUnneeded=, BindsTo=, Uphold= logic */
382 RateLimit auto_start_stop_ratelimit
;
383 sd_event_source
*auto_start_stop_event_source
;
385 /* Reference to a specific UID/GID */
389 /* Cached unit file state and preset */
390 UnitFileState unit_file_state
;
391 PresetAction unit_file_preset
;
393 /* How to start OnSuccess=/OnFailure= units */
394 JobMode on_success_job_mode
;
395 JobMode on_failure_job_mode
;
397 /* If the job had a specific trigger that needs to be advertised (eg: a path unit), store it. */
398 ActivationDetails
*activation_details
;
400 /* Tweaking the GC logic */
401 CollectMode collect_mode
;
403 /* The current invocation ID */
404 sd_id128_t invocation_id
;
405 char invocation_id_string
[SD_ID128_STRING_MAX
]; /* useful when logging */
407 /* Garbage collect us we nobody wants or requires us anymore */
408 bool stop_when_unneeded
;
410 /* Create default dependencies */
411 bool default_dependencies
;
413 /* Configure so that the unit survives a system transition without stopping/starting. */
414 bool survive_final_kill_signal
;
416 /* Refuse manual starting, allow starting only indirectly via dependency. */
417 bool refuse_manual_start
;
419 /* Don't allow the user to stop this unit manually, allow stopping only indirectly via dependency. */
420 bool refuse_manual_stop
;
422 /* Allow isolation requests */
425 /* Ignore this unit when isolating */
426 bool ignore_on_isolate
;
428 /* Did the last condition check succeed? */
429 bool condition_result
;
432 /* Is this a transient unit? */
435 /* Is this a unit that is always running and cannot be stopped? */
438 /* When true logs about this unit will be at debug level regardless of other log level settings */
439 bool debug_invocation
;
441 /* Booleans indicating membership of this unit in the various queues */
442 bool in_load_queue
:1;
443 bool in_dbus_queue
:1;
444 bool in_cleanup_queue
:1;
446 bool in_cgroup_realize_queue
:1;
447 bool in_cgroup_empty_queue
:1;
448 bool in_cgroup_oom_queue
:1;
449 bool in_target_deps_queue
:1;
450 bool in_stop_when_unneeded_queue
:1;
451 bool in_start_when_upheld_queue
:1;
452 bool in_stop_when_bound_queue
:1;
453 bool in_release_resources_queue
:1;
454 bool in_stop_notify_queue
:1;
456 bool sent_dbus_new_signal
:1;
458 bool job_running_timeout_set
:1;
463 bool start_limit_hit
:1;
465 /* Did we already invoke unit_coldplug() for this unit? */
468 /* For transient units: whether to add a bus track reference after creating the unit */
469 bool bus_track_add
:1;
471 /* Remember which unit state files we created */
472 bool exported_invocation_id
:1;
473 bool exported_log_level_max
:1;
474 bool exported_log_extra_fields
:1;
475 bool exported_log_ratelimit_interval
:1;
476 bool exported_log_ratelimit_burst
:1;
478 /* When writing transient unit files, stores which section we stored last. If < 0, we didn't write any yet. If
479 * == 0 we are in the [Unit] section, if > 0 we are in the unit type-specific section. */
480 signed int last_section_private
:2;
483 typedef struct UnitStatusMessageFormats
{
484 const char *starting_stopping
[2];
485 const char *finished_start_job
[_JOB_RESULT_MAX
];
486 const char *finished_stop_job
[_JOB_RESULT_MAX
];
487 /* If this entry is present, it'll be called to provide a context-dependent format string,
488 * or NULL to fall back to finished_{start,stop}_job; if those are NULL too, fall back to generic. */
489 const char *(*finished_job
)(Unit
*u
, JobType t
, JobResult result
);
490 } UnitStatusMessageFormats
;
492 /* Flags used when writing drop-in files or transient unit files */
493 typedef enum UnitWriteFlags
{
494 /* Write a runtime unit file or drop-in (i.e. one below /run) */
495 UNIT_RUNTIME
= 1 << 0,
497 /* Write a persistent drop-in (i.e. one below /etc) */
498 UNIT_PERSISTENT
= 1 << 1,
500 /* Place this item in the per-unit-type private section, instead of [Unit] */
501 UNIT_PRIVATE
= 1 << 2,
503 /* Apply specifier escaping */
504 UNIT_ESCAPE_SPECIFIERS
= 1 << 3,
506 /* Escape elements of ExecStart= syntax, incl. prevention of variable expansion */
507 UNIT_ESCAPE_EXEC_SYNTAX_ENV
= 1 << 4,
509 /* Escape elements of ExecStart=: syntax (no variable expansion) */
510 UNIT_ESCAPE_EXEC_SYNTAX
= 1 << 5,
512 /* Apply C escaping before writing */
513 UNIT_ESCAPE_C
= 1 << 6,
516 /* Returns true if neither persistent, nor runtime storage is requested, i.e. this is a check invocation only */
517 static inline bool UNIT_WRITE_FLAGS_NOOP(UnitWriteFlags flags
) {
518 return (flags
& (UNIT_RUNTIME
|UNIT_PERSISTENT
)) == 0;
523 /* The static const, immutable data about a specific unit type */
524 typedef struct UnitVTable
{
525 /* How much memory does an object of this unit type need */
528 /* If greater than 0, the offset into the object where
529 * ExecContext is found, if the unit type has that */
530 size_t exec_context_offset
;
532 /* If greater than 0, the offset into the object where
533 * CGroupContext is found, if the unit type has that */
534 size_t cgroup_context_offset
;
536 /* If greater than 0, the offset into the object where
537 * KillContext is found, if the unit type has that */
538 size_t kill_context_offset
;
540 /* If greater than 0, the offset into the object where the pointer to ExecRuntime is found, if
541 * the unit type has that */
542 size_t exec_runtime_offset
;
544 /* If greater than 0, the offset into the object where the pointer to CGroupRuntime is found, if the
545 * unit type has that */
546 size_t cgroup_runtime_offset
;
548 /* The name of the configuration file section with the private settings of this unit */
549 const char *private_section
;
551 /* Config file sections this unit type understands, separated
553 const char *sections
;
555 /* This should reset all type-specific variables. This should
556 * not allocate memory, and is called with zero-initialized
557 * data. It should hence only initialize variables that need
559 void (*init
)(Unit
*u
);
561 /* This should free all type-specific variables. It should be
563 void (*done
)(Unit
*u
);
565 /* Actually load data from disk. This may fail, and should set
566 * load_state to UNIT_LOADED, UNIT_MERGED or leave it at
567 * UNIT_STUB if no configuration could be found. */
568 int (*load
)(Unit
*u
);
570 /* During deserialization we only record the intended state to return to. With coldplug() we actually put the
571 * deserialized state in effect. This is where unit_notify() should be called to start things up. Note that
572 * this callback is invoked *before* we leave the reloading state of the manager, i.e. *before* we consider the
573 * reloading to be complete. Thus, this callback should just restore the exact same state for any unit that was
574 * in effect before the reload, i.e. units should not catch up with changes happened during the reload. That's
575 * what catchup() below is for. */
576 int (*coldplug
)(Unit
*u
);
578 /* This is called shortly after all units' coldplug() call was invoked, and *after* the manager left the
579 * reloading state. It's supposed to catch up with state changes due to external events we missed so far (for
580 * example because they took place while we were reloading/reexecing) */
581 void (*catchup
)(Unit
*u
);
583 void (*dump
)(Unit
*u
, FILE *f
, const char *prefix
);
585 int (*start
)(Unit
*u
);
586 int (*stop
)(Unit
*u
);
587 int (*reload
)(Unit
*u
);
589 /* Clear out the various runtime/state/cache/logs/configuration data */
590 int (*clean
)(Unit
*u
, ExecCleanMask m
);
592 /* Freeze or thaw the unit. Returns > 0 to indicate that the request will be handled asynchronously; unit_frozen
593 * or unit_thawed should be called once the operation is done. Returns 0 if done successfully, or < 0 on error. */
594 int (*freezer_action
)(Unit
*u
, FreezerAction a
);
595 bool (*can_freeze
)(const Unit
*u
);
597 /* Return which kind of data can be cleaned */
598 int (*can_clean
)(Unit
*u
, ExecCleanMask
*ret
);
600 bool (*can_reload
)(Unit
*u
);
602 /* Add a bind/image mount into the unit namespace while it is running. */
603 int (*live_mount
)(Unit
*u
, const char *src
, const char *dst
, sd_bus_message
*message
, MountInNamespaceFlags flags
, const MountOptions
*options
, sd_bus_error
*error
);
604 int (*can_live_mount
)(Unit
*u
, sd_bus_error
*error
);
606 /* Serialize state and file descriptors that should be carried over into the new
607 * instance after reexecution. */
608 int (*serialize
)(Unit
*u
, FILE *f
, FDSet
*fds
);
610 /* Restore one item from the serialization */
611 int (*deserialize_item
)(Unit
*u
, const char *key
, const char *data
, FDSet
*fds
);
613 /* Try to match up fds with what we need for this unit */
614 void (*distribute_fds
)(Unit
*u
, FDSet
*fds
);
616 /* Boils down the more complex internal state of this unit to
617 * a simpler one that the engine can understand */
618 UnitActiveState (*active_state
)(Unit
*u
);
620 /* Returns the substate specific to this unit type as
621 * string. This is purely information so that we can give the
622 * user a more fine grained explanation in which actual state a
624 const char* (*sub_state_to_string
)(Unit
*u
);
626 /* Additionally to UnitActiveState determine whether unit is to be restarted. */
627 bool (*will_restart
)(Unit
*u
);
629 /* Return false when there is a reason to prevent this unit from being gc'ed
630 * even though nothing references it and it isn't active in any way. */
631 bool (*may_gc
)(Unit
*u
);
633 /* Return true when the unit is not controlled by the manager (e.g. extrinsic mounts). */
634 bool (*is_extrinsic
)(Unit
*u
);
636 /* When the unit is not running and no job for it queued we shall release its runtime resources */
637 void (*release_resources
)(Unit
*u
);
639 /* Invoked on every child that died */
640 void (*sigchld_event
)(Unit
*u
, pid_t pid
, int code
, int status
);
642 /* Reset failed state if we are in failed state */
643 void (*reset_failed
)(Unit
*u
);
645 /* Called whenever any of the cgroups this unit watches for ran empty */
646 void (*notify_cgroup_empty
)(Unit
*u
);
648 /* Called whenever an OOM kill event on this unit was seen */
649 void (*notify_cgroup_oom
)(Unit
*u
, bool managed_oom
);
651 /* Called whenever a process of this unit sends us a message */
652 void (*notify_message
)(Unit
*u
, PidRef
*pidref
, const struct ucred
*ucred
, char * const *tags
, FDSet
*fds
);
654 /* Called whenever we learn a handoff timestamp */
655 void (*notify_handoff_timestamp
)(Unit
*u
, const struct ucred
*ucred
, const dual_timestamp
*ts
);
657 /* Called whenever we learn about a child process */
658 void (*notify_pidref
)(Unit
*u
, PidRef
*parent_pidref
, PidRef
*child_pidref
);
660 /* Called whenever a name this Unit registered for comes or goes away. */
661 void (*bus_name_owner_change
)(Unit
*u
, const char *new_owner
);
663 /* Called for each property that is being set */
664 int (*bus_set_property
)(Unit
*u
, const char *name
, sd_bus_message
*message
, UnitWriteFlags flags
, sd_bus_error
*error
);
666 /* Called after at least one property got changed to apply the necessary change */
667 int (*bus_commit_properties
)(Unit
*u
);
669 /* Return the unit this unit is following */
670 Unit
* (*following
)(Unit
*u
);
672 /* Return the set of units that are following each other */
673 int (*following_set
)(Unit
*u
, Set
**s
);
675 /* Invoked each time a unit this unit is triggering changes
676 * state or gains/loses a job */
677 void (*trigger_notify
)(Unit
*u
, Unit
*trigger
);
679 /* Invoked when some other units stop */
680 bool (*stop_notify
)(Unit
*u
);
682 /* Called whenever CLOCK_REALTIME made a jump */
683 void (*time_change
)(Unit
*u
);
685 /* Called whenever /etc/localtime was modified */
686 void (*timezone_change
)(Unit
*u
);
688 /* Returns the next timeout of a unit */
689 int (*get_timeout
)(Unit
*u
, usec_t
*timeout
);
691 /* Returns the start timeout of a unit */
692 usec_t (*get_timeout_start_usec
)(Unit
*u
);
694 /* Returns the main PID if there is any defined, or NULL. */
695 PidRef
* (*main_pid
)(Unit
*u
, bool *ret_is_alien
);
697 /* Returns the control PID if there is any defined, or NULL. */
698 PidRef
* (*control_pid
)(Unit
*u
);
700 /* Returns true if the unit currently needs access to the console */
701 bool (*needs_console
)(Unit
*u
);
703 /* Returns the exit status to propagate in case of FailureAction=exit/SuccessAction=exit; usually returns the
704 * exit code of the "main" process of the service or similar. */
705 int (*exit_status
)(Unit
*u
);
707 /* Return a copy of the status string pointer. */
708 const char* (*status_text
)(Unit
*u
);
710 /* Like the enumerate() callback further down, but only enumerates the perpetual units, i.e. all units that
711 * unconditionally exist and are always active. The main reason to keep both enumeration functions separate is
712 * philosophical: the state of perpetual units should be put in place by coldplug(), while the state of those
713 * discovered through regular enumeration should be put in place by catchup(), see below. */
714 void (*enumerate_perpetual
)(Manager
*m
);
716 /* This is called for each unit type and should be used to enumerate units already existing in the system
717 * internally and load them. However, everything that is loaded here should still stay in inactive state. It is
718 * the job of the catchup() call above to put the units into the discovered state. */
719 void (*enumerate
)(Manager
*m
);
721 /* Type specific cleanups. */
722 void (*shutdown
)(Manager
*m
);
724 /* If this function is set and returns false all jobs for units
725 * of this type will immediately fail. */
726 bool (*supported
)(void);
728 /* If this function is set, it's invoked first as part of starting a unit to allow start rate
729 * limiting checks and unit state checks to occur before we do anything else. */
730 int (*test_startable
)(Unit
*u
);
732 /* Returns > 0 if the whole subsystem is ratelimited, and new start operations should not be started
733 * for this unit type right now. */
734 int (*subsystem_ratelimited
)(Manager
*m
);
736 /* The strings to print in status messages */
737 UnitStatusMessageFormats status_message_formats
;
739 /* True if transient units of this type are OK */
742 /* True if cgroup delegation is permissible */
745 /* True if the unit type triggers other units, i.e. can have a UNIT_TRIGGERS dependency */
748 /* True if the unit type knows a failure state, and thus can be source of an OnFailure= dependency */
751 /* True if units of this type shall be startable only once and then never again */
754 /* Do not serialize this unit when preparing for root switch */
755 bool exclude_from_switch_root_serialization
;
757 /* True if queued jobs of this type should be GC'ed if no other job needs them anymore */
760 /* True if systemd-oomd can monitor and act on this unit's recursive children's cgroups */
761 bool can_set_managed_oom
;
763 /* If true, we'll notify plymouth about this unit */
764 bool notify_plymouth
;
766 /* If true, we'll notify a surrounding VMM/container manager about this unit becoming available */
767 bool notify_supervisor
;
769 /* The audit events to generate on start + stop (or 0 if none shall be generated) */
770 int audit_start_message_type
;
771 int audit_stop_message_type
;
774 extern const UnitVTable
* const unit_vtable
[_UNIT_TYPE_MAX
];
776 static inline const UnitVTable
* UNIT_VTABLE(const Unit
*u
) {
777 return unit_vtable
[u
->type
];
780 /* For casting a unit into the various unit types */
781 #define DEFINE_CAST(UPPERCASE, MixedCase) \
782 static inline MixedCase* UPPERCASE(Unit *u) { \
783 if (_unlikely_(!u || u->type != UNIT_##UPPERCASE)) \
786 return (MixedCase*) u; \
789 /* For casting the various unit types into a unit */
792 typeof(u) _u_ = (u); \
793 Unit *_w_ = _u_ ? &(_u_)->meta : NULL; \
797 #define UNIT_HAS_EXEC_CONTEXT(u) (UNIT_VTABLE(u)->exec_context_offset > 0)
798 #define UNIT_HAS_CGROUP_CONTEXT(u) (UNIT_VTABLE(u)->cgroup_context_offset > 0)
799 #define UNIT_HAS_KILL_CONTEXT(u) (UNIT_VTABLE(u)->kill_context_offset > 0)
801 Unit
* unit_has_dependency(const Unit
*u
, UnitDependencyAtom atom
, Unit
*other
);
802 int unit_get_dependency_array(const Unit
*u
, UnitDependencyAtom atom
, Unit
***ret_array
);
803 int unit_get_transitive_dependency_set(Unit
*u
, UnitDependencyAtom atom
, Set
**ret
);
805 static inline Hashmap
* unit_get_dependencies(Unit
*u
, UnitDependency d
) {
806 return hashmap_get(u
->dependencies
, UNIT_DEPENDENCY_TO_PTR(d
));
809 static inline Unit
* UNIT_TRIGGER(Unit
*u
) {
810 return unit_has_dependency(u
, UNIT_ATOM_TRIGGERS
, NULL
);
813 static inline Unit
* UNIT_GET_SLICE(const Unit
*u
) {
814 return unit_has_dependency(u
, UNIT_ATOM_IN_SLICE
, NULL
);
817 Unit
* unit_new(Manager
*m
, size_t size
);
818 Unit
* unit_free(Unit
*u
);
819 DEFINE_TRIVIAL_CLEANUP_FUNC(Unit
*, unit_free
);
821 int unit_new_for_name(Manager
*m
, size_t size
, const char *name
, Unit
**ret
);
822 int unit_add_name(Unit
*u
, const char *name
);
824 int unit_add_dependency(Unit
*u
, UnitDependency d
, Unit
*other
, bool add_reference
, UnitDependencyMask mask
);
825 int unit_add_two_dependencies(Unit
*u
, UnitDependency d
, UnitDependency e
, Unit
*other
, bool add_reference
, UnitDependencyMask mask
);
827 int unit_add_dependency_by_name(Unit
*u
, UnitDependency d
, const char *name
, bool add_reference
, UnitDependencyMask mask
);
828 int unit_add_two_dependencies_by_name(Unit
*u
, UnitDependency d
, UnitDependency e
, const char *name
, bool add_reference
, UnitDependencyMask mask
);
830 int unit_add_exec_dependencies(Unit
*u
, ExecContext
*c
);
832 int unit_choose_id(Unit
*u
, const char *name
);
833 int unit_set_description(Unit
*u
, const char *description
);
835 void unit_release_resources(Unit
*u
);
837 bool unit_may_gc(Unit
*u
);
839 static inline bool unit_is_extrinsic(Unit
*u
) {
840 return u
->perpetual
||
841 (UNIT_VTABLE(u
)->is_extrinsic
&& UNIT_VTABLE(u
)->is_extrinsic(u
));
844 static inline const char* unit_status_text(Unit
*u
) {
845 if (u
&& UNIT_VTABLE(u
)->status_text
)
846 return UNIT_VTABLE(u
)->status_text(u
);
850 void unit_add_to_load_queue(Unit
*u
);
851 void unit_add_to_dbus_queue(Unit
*u
);
852 void unit_add_to_cleanup_queue(Unit
*u
);
853 void unit_add_to_gc_queue(Unit
*u
);
854 void unit_add_to_target_deps_queue(Unit
*u
);
855 void unit_submit_to_stop_when_unneeded_queue(Unit
*u
);
856 void unit_submit_to_start_when_upheld_queue(Unit
*u
);
857 void unit_submit_to_stop_when_bound_queue(Unit
*u
);
858 void unit_submit_to_release_resources_queue(Unit
*u
);
859 void unit_add_to_stop_notify_queue(Unit
*u
);
860 void unit_remove_from_stop_notify_queue(Unit
*u
);
862 int unit_merge(Unit
*u
, Unit
*other
);
863 int unit_merge_by_name(Unit
*u
, const char *other
);
865 Unit
*unit_follow_merge(Unit
*u
) _pure_
;
867 int unit_load_fragment_and_dropin(Unit
*u
, bool fragment_required
);
868 int unit_load(Unit
*unit
);
870 int unit_set_slice(Unit
*u
, Unit
*slice
);
871 int unit_set_default_slice(Unit
*u
);
873 const char* unit_description(Unit
*u
) _pure_
;
874 const char* unit_status_string(Unit
*u
, char **combined
);
876 bool unit_has_name(const Unit
*u
, const char *name
);
878 UnitActiveState
unit_active_state(Unit
*u
);
880 const char* unit_sub_state_to_string(Unit
*u
);
882 bool unit_can_reload(Unit
*u
) _pure_
;
883 bool unit_can_start(Unit
*u
) _pure_
;
884 bool unit_can_stop(Unit
*u
) _pure_
;
885 bool unit_can_isolate(Unit
*u
) _pure_
;
887 int unit_start(Unit
*u
, ActivationDetails
*details
);
888 int unit_stop(Unit
*u
);
889 int unit_reload(Unit
*u
);
891 int unit_kill(Unit
*u
, KillWhom w
, const char *subgroup
, int signo
, int code
, int value
, sd_bus_error
*ret_error
);
893 void unit_notify_cgroup_oom(Unit
*u
, bool managed_oom
);
895 void unit_notify(Unit
*u
, UnitActiveState os
, UnitActiveState ns
, bool reload_success
);
897 int unit_watch_pidref(Unit
*u
, const PidRef
*pid
, bool exclusive
);
898 void unit_unwatch_pidref(Unit
*u
, const PidRef
*pid
);
899 void unit_unwatch_all_pids(Unit
*u
);
900 void unit_unwatch_pidref_done(Unit
*u
, PidRef
*pidref
);
902 int unit_install_bus_match(Unit
*u
, sd_bus
*bus
, const char *name
);
903 int unit_watch_bus_name(Unit
*u
, const char *name
);
904 void unit_unwatch_bus_name(Unit
*u
, const char *name
);
906 bool unit_job_is_applicable(Unit
*u
, JobType j
);
908 int setenv_unit_path(const char *p
);
910 char* unit_dbus_path(Unit
*u
);
911 char* unit_dbus_path_invocation_id(Unit
*u
);
913 int unit_load_related_unit(Unit
*u
, const char *type
, Unit
**_found
);
915 int unit_add_node_dependency(Unit
*u
, const char *what
, UnitDependency d
, UnitDependencyMask mask
);
916 int unit_add_blockdev_dependency(Unit
*u
, const char *what
, UnitDependencyMask mask
);
918 int unit_coldplug(Unit
*u
);
919 void unit_catchup(Unit
*u
);
921 void unit_status_printf(Unit
*u
, StatusType status_type
, const char *status
, const char *format
, const char *ident
) _printf_(4, 0);
923 bool unit_need_daemon_reload(Unit
*u
);
925 void unit_reset_failed(Unit
*u
);
927 Unit
*unit_following(Unit
*u
);
928 int unit_following_set(Unit
*u
, Set
**s
);
930 const char* unit_slice_name(Unit
*u
);
932 bool unit_stop_pending(Unit
*u
) _pure_
;
933 bool unit_inactive_or_pending(Unit
*u
) _pure_
;
934 bool unit_active_or_pending(Unit
*u
);
935 bool unit_will_restart_default(Unit
*u
);
936 bool unit_will_restart(Unit
*u
);
938 int unit_add_default_target_dependency(Unit
*u
, Unit
*target
);
940 void unit_start_on_termination_deps(Unit
*u
, UnitDependencyAtom atom
);
941 void unit_trigger_notify(Unit
*u
);
943 int unit_get_exec_quota_stats(Unit
*u
, ExecContext
*c
, ExecDirectoryType dt
, uint64_t *ret_usage
, uint64_t *ret_limit
);
945 UnitFileState
unit_get_unit_file_state(Unit
*u
);
946 PresetAction
unit_get_unit_file_preset(Unit
*u
);
948 Unit
* unit_ref_set(UnitRef
*ref
, Unit
*source
, Unit
*target
);
949 void unit_ref_unset(UnitRef
*ref
);
951 #define UNIT_DEREF(ref) ((ref).target)
952 #define UNIT_ISSET(ref) (!!(ref).target)
954 int unit_patch_contexts(Unit
*u
);
956 ExecContext
* unit_get_exec_context(const Unit
*u
) _pure_
;
957 KillContext
* unit_get_kill_context(const Unit
*u
) _pure_
;
958 CGroupContext
* unit_get_cgroup_context(const Unit
*u
) _pure_
;
960 ExecRuntime
* unit_get_exec_runtime(const Unit
*u
) _pure_
;
961 CGroupRuntime
* unit_get_cgroup_runtime(const Unit
*u
) _pure_
;
963 int unit_setup_exec_runtime(Unit
*u
);
964 CGroupRuntime
* unit_setup_cgroup_runtime(Unit
*u
);
966 const char* unit_escape_setting(const char *s
, UnitWriteFlags flags
, char **buf
);
967 char* unit_concat_strv(char **l
, UnitWriteFlags flags
);
969 int unit_write_setting(Unit
*u
, UnitWriteFlags flags
, const char *name
, const char *data
);
970 int unit_write_settingf(Unit
*u
, UnitWriteFlags mode
, const char *name
, const char *format
, ...) _printf_(4,5);
972 int unit_kill_context(Unit
*u
, KillOperation k
);
974 int unit_make_transient(Unit
*u
);
976 int unit_add_mounts_for(Unit
*u
, const char *path
, UnitDependencyMask mask
, UnitMountDependencyType type
);
978 bool unit_type_supported(UnitType t
);
980 bool unit_is_pristine(Unit
*u
);
982 bool unit_is_unneeded(Unit
*u
);
983 bool unit_is_upheld_by_active(Unit
*u
, Unit
**ret_culprit
);
984 bool unit_is_bound_by_inactive(Unit
*u
, Unit
**ret_culprit
);
986 PidRef
* unit_control_pid(Unit
*u
);
987 PidRef
* unit_main_pid_full(Unit
*u
, bool *ret_is_alien
);
988 static inline PidRef
* unit_main_pid(Unit
*u
) {
989 return unit_main_pid_full(u
, NULL
);
992 void unit_warn_if_dir_nonempty(Unit
*u
, const char* where
);
993 int unit_log_noncanonical_mount_path(Unit
*u
, const char *where
);
994 int unit_fail_if_noncanonical_mount_path(Unit
*u
, const char* where
);
996 int unit_test_start_limit(Unit
*u
);
998 int unit_ref_uid_gid(Unit
*u
, uid_t uid
, gid_t gid
);
999 void unit_unref_uid_gid(Unit
*u
, bool destroy_now
);
1001 void unit_notify_user_lookup(Unit
*u
, uid_t uid
, gid_t gid
);
1003 int unit_set_invocation_id(Unit
*u
, sd_id128_t id
);
1004 int unit_acquire_invocation_id(Unit
*u
);
1006 int unit_set_exec_params(Unit
*s
, ExecParameters
*p
);
1008 int unit_fork_helper_process(Unit
*u
, const char *name
, bool into_cgroup
, PidRef
*ret
);
1009 int unit_fork_and_watch_rm_rf(Unit
*u
, char **paths
, PidRef
*ret
);
1011 void unit_remove_dependencies(Unit
*u
, UnitDependencyMask mask
);
1013 void unit_export_state_files(Unit
*u
);
1014 void unit_unlink_state_files(Unit
*u
);
1016 int unit_set_debug_invocation(Unit
*u
, bool enable
);
1018 int unit_prepare_exec(Unit
*u
);
1020 int unit_warn_leftover_processes(Unit
*u
, bool start
);
1022 bool unit_needs_console(Unit
*u
);
1024 int unit_pid_attachable(Unit
*unit
, PidRef
*pid
, sd_bus_error
*error
);
1026 static inline bool unit_has_job_type(Unit
*u
, JobType type
) {
1027 return u
&& u
->job
&& u
->job
->type
== type
;
1030 int unit_get_log_level_max(const Unit
*u
);
1032 bool unit_log_level_test(const Unit
*u
, int level
);
1034 /* unit_log_skip is for cases like ExecCondition= where a unit is considered "done"
1035 * after some execution, rather than succeeded or failed. */
1036 void unit_log_skip(Unit
*u
, const char *result
);
1037 void unit_log_success(Unit
*u
);
1038 void unit_log_failure(Unit
*u
, const char *result
);
1039 static inline void unit_log_result(Unit
*u
, bool success
, const char *result
) {
1041 unit_log_success(u
);
1043 unit_log_failure(u
, result
);
1046 void unit_log_process_exit(Unit
*u
, const char *kind
, const char *command
, bool success
, int code
, int status
);
1048 int unit_exit_status(Unit
*u
);
1049 int unit_success_action_exit_status(Unit
*u
);
1050 int unit_failure_action_exit_status(Unit
*u
);
1052 int unit_test_trigger_loaded(Unit
*u
);
1054 void unit_destroy_runtime_data(Unit
*u
, const ExecContext
*context
, bool destroy_runtime_dir
);
1055 int unit_clean(Unit
*u
, ExecCleanMask mask
);
1056 int unit_can_clean(Unit
*u
, ExecCleanMask
*ret_mask
);
1058 bool unit_can_start_refuse_manual(Unit
*u
);
1059 bool unit_can_stop_refuse_manual(Unit
*u
);
1060 bool unit_can_isolate_refuse_manual(Unit
*u
);
1062 bool unit_can_freeze(const Unit
*u
);
1063 int unit_freezer_action(Unit
*u
, FreezerAction action
);
1064 void unit_next_freezer_state(Unit
*u
, FreezerAction action
, FreezerState
*ret_next
, FreezerState
*ret_objective
);
1065 void unit_set_freezer_state(Unit
*u
, FreezerState state
);
1066 void unit_freezer_complete(Unit
*u
, FreezerState kernel_state
);
1068 int unit_can_live_mount(Unit
*u
, sd_bus_error
*error
);
1069 int unit_live_mount(Unit
*u
, const char *src
, const char *dst
, sd_bus_message
*message
, MountInNamespaceFlags flags
, const MountOptions
*options
, sd_bus_error
*error
);
1071 Condition
*unit_find_failed_condition(Unit
*u
);
1073 int unit_arm_timer(Unit
*u
, sd_event_source
**source
, bool relative
, usec_t usec
, sd_event_time_handler_t handler
);
1075 bool unit_passes_filter(Unit
*u
, char * const *states
, char * const *patterns
);
1077 int unit_compare_priority(Unit
*a
, Unit
*b
);
1079 const char* unit_log_field(const Unit
*u
);
1080 const char* unit_invocation_log_field(const Unit
*u
);
1082 UnitMountDependencyType
unit_mount_dependency_type_from_string(const char *s
) _const_
;
1083 const char* unit_mount_dependency_type_to_string(UnitMountDependencyType t
) _const_
;
1084 UnitDependency
unit_mount_dependency_type_to_dependency_type(UnitMountDependencyType t
) _pure_
;
1086 const char* oom_policy_to_string(OOMPolicy i
) _const_
;
1087 OOMPolicy
oom_policy_from_string(const char *s
) _pure_
;
1089 /* Macros which append UNIT= or USER_UNIT= to the message */
1091 #define log_unit_full_errno_zerook(unit, level, error, ...) \
1093 const Unit *_u = (unit); \
1094 const int _l = (level); \
1095 LOG_CONTEXT_SET_LOG_LEVEL(unit_get_log_level_max(_u)); \
1096 const ExecContext *_c = _u ? unit_get_exec_context(_u) : NULL; \
1097 LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL, \
1098 _c ? _c->n_log_extra_fields : 0); \
1099 _u ? log_object_internal(_l, error, PROJECT_FILE, __LINE__, __func__, unit_log_field(_u), _u->id, unit_invocation_log_field(_u), _u->invocation_id_string, ##__VA_ARGS__) : \
1100 log_internal(_l, error, PROJECT_FILE, __LINE__, __func__, ##__VA_ARGS__); \
1103 #define log_unit_full_errno(unit, level, error, ...) \
1105 int _error = (error); \
1106 ASSERT_NON_ZERO(_error); \
1107 log_unit_full_errno_zerook(unit, level, _error, ##__VA_ARGS__); \
1110 #define log_unit_full(unit, level, ...) (void) log_unit_full_errno_zerook(unit, level, 0, __VA_ARGS__)
1112 #define log_unit_debug(unit, ...) log_unit_full(unit, LOG_DEBUG, __VA_ARGS__)
1113 #define log_unit_info(unit, ...) log_unit_full(unit, LOG_INFO, __VA_ARGS__)
1114 #define log_unit_notice(unit, ...) log_unit_full(unit, LOG_NOTICE, __VA_ARGS__)
1115 #define log_unit_warning(unit, ...) log_unit_full(unit, LOG_WARNING, __VA_ARGS__)
1116 #define log_unit_error(unit, ...) log_unit_full(unit, LOG_ERR, __VA_ARGS__)
1118 #define log_unit_debug_errno(unit, error, ...) log_unit_full_errno(unit, LOG_DEBUG, error, __VA_ARGS__)
1119 #define log_unit_info_errno(unit, error, ...) log_unit_full_errno(unit, LOG_INFO, error, __VA_ARGS__)
1120 #define log_unit_notice_errno(unit, error, ...) log_unit_full_errno(unit, LOG_NOTICE, error, __VA_ARGS__)
1121 #define log_unit_warning_errno(unit, error, ...) log_unit_full_errno(unit, LOG_WARNING, error, __VA_ARGS__)
1122 #define log_unit_error_errno(unit, error, ...) log_unit_full_errno(unit, LOG_ERR, error, __VA_ARGS__)
1125 # define log_unit_trace(...) log_unit_debug(__VA_ARGS__)
1126 # define log_unit_trace_errno(...) log_unit_debug_errno(__VA_ARGS__)
1128 # define log_unit_trace(...) do {} while (0)
1129 # define log_unit_trace_errno(e, ...) (-ERRNO_VALUE(e))
1132 #define log_unit_struct_errno(unit, level, error, ...) \
1134 const Unit *_u = (unit); \
1135 const int _l = (level); \
1136 LOG_CONTEXT_SET_LOG_LEVEL(unit_get_log_level_max(_u)); \
1137 const ExecContext *_c = _u ? unit_get_exec_context(_u) : NULL; \
1138 LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL, \
1139 _c ? _c->n_log_extra_fields : 0); \
1140 log_struct_errno(_l, error, __VA_ARGS__, LOG_UNIT_ID(_u)); \
1143 #define log_unit_struct(unit, level, ...) log_unit_struct_errno(unit, level, 0, __VA_ARGS__)
1145 #define log_unit_struct_iovec_errno(unit, level, error, iovec, n_iovec) \
1147 const Unit *_u = (unit); \
1148 const int _l = (level); \
1149 LOG_CONTEXT_SET_LOG_LEVEL(unit_get_log_level_max(_u)); \
1150 const ExecContext *_c = _u ? unit_get_exec_context(_u) : NULL; \
1151 LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL, \
1152 _c ? _c->n_log_extra_fields : 0); \
1153 log_struct_iovec_errno(_l, error, iovec, n_iovec); \
1156 #define log_unit_struct_iovec(unit, level, iovec, n_iovec) log_unit_struct_iovec_errno(unit, level, 0, iovec, n_iovec)
1158 /* Like LOG_MESSAGE(), but with the unit name prefixed. */
1159 #define LOG_UNIT_MESSAGE(unit, fmt, ...) LOG_MESSAGE("%s: " fmt, (unit)->id, ##__VA_ARGS__)
1160 #define LOG_UNIT_ID(unit) LOG_ITEM("%s%s", unit_log_field((unit)), (unit)->id)
1161 #define LOG_UNIT_INVOCATION_ID(unit) LOG_ITEM("%s%s", unit_invocation_log_field((unit)), (unit)->invocation_id_string)
1163 const char* collect_mode_to_string(CollectMode m
) _const_
;
1164 CollectMode
collect_mode_from_string(const char *s
) _pure_
;
1166 typedef struct UnitForEachDependencyData
{
1167 /* Stores state for the FOREACH macro below for iterating through all deps that have any of the
1168 * specified dependency atom bits set */
1170 UnitDependencyAtom match_atom
;
1171 Hashmap
*by_type
, *by_unit
;
1173 Iterator by_type_iterator
, by_unit_iterator
;
1174 Unit
**current_unit
;
1175 uint64_t generation
;
1177 bool restart_on_generation_change
;
1178 } UnitForEachDependencyData
;
1180 /* Let's not restart the loop infinitely. */
1181 #define MAX_FOREACH_DEPENDENCY_RESTART 100000
1183 /* Iterates through all dependencies that have a specific atom in the dependency type set. This tries to be
1184 * smart: if the atom is unique, we'll directly go to right entry. Otherwise we'll iterate through the
1185 * per-dependency type hashmap and match all dep that have the right atom set. */
1186 #define _UNIT_FOREACH_DEPENDENCY(other, u, ma, restart, data) \
1187 for (UnitForEachDependencyData data = { \
1189 .match_atom = (ma), \
1190 .current_unit = &(other), \
1191 .restart_on_generation_change = (restart), \
1194 UnitDependency _dt = _UNIT_DEPENDENCY_INVALID; \
1197 if (data.generation == 0 || \
1198 (data.restart_on_generation_change && \
1199 data.generation != data.unit->dependency_generation)) { \
1200 data.generation = data.unit->dependency_generation; \
1201 data.by_type = data.unit->dependencies; \
1202 data.by_type_iterator = ITERATOR_FIRST; \
1203 assert_se(data.n_restart++ < MAX_FOREACH_DEPENDENCY_RESTART); \
1205 assert(data.generation == data.unit->dependency_generation); \
1207 if (data.by_type && ITERATOR_IS_FIRST(data.by_type_iterator)) { \
1208 _dt = unit_dependency_from_unique_atom(data.match_atom); \
1210 data.by_unit = hashmap_get(data.by_type, UNIT_DEPENDENCY_TO_PTR(_dt)); \
1211 data.current_type = UNIT_DEPENDENCY_TO_PTR(_dt); \
1212 data.by_type = NULL; \
1213 _found = !!data.by_unit; \
1217 _found = hashmap_iterate(data.by_type, \
1218 &data.by_type_iterator, \
1219 (void**) &(data.by_unit), \
1220 (const void**) &(data.current_type)); \
1223 if ((unit_dependency_to_atom(UNIT_DEPENDENCY_FROM_PTR(data.current_type)) & data.match_atom) != 0) \
1224 for (data.by_unit_iterator = ITERATOR_FIRST; \
1225 data.generation == data.unit->dependency_generation && \
1226 hashmap_iterate(data.by_unit, \
1227 &data.by_unit_iterator, \
1229 (const void**) data.current_unit); )
1231 /* Note: this matches deps that have *any* of the atoms specified in match_atom set */
1232 #define UNIT_FOREACH_DEPENDENCY(other, u, match_atom) \
1233 _UNIT_FOREACH_DEPENDENCY(other, u, match_atom, false, UNIQ_T(data, UNIQ))
1234 #define UNIT_FOREACH_DEPENDENCY_SAFE(other, u, match_atom) \
1235 _UNIT_FOREACH_DEPENDENCY(other, u, match_atom, true, UNIQ_T(data, UNIQ))
1237 #define _LOG_CONTEXT_PUSH_UNIT(unit, u, c) \
1238 const Unit *u = (unit); \
1239 const ExecContext *c = unit_get_exec_context(u); \
1240 LOG_CONTEXT_PUSH_KEY_VALUE(unit_log_field(u), u->id); \
1241 LOG_CONTEXT_PUSH_KEY_VALUE(unit_invocation_log_field(u), u->invocation_id_string); \
1242 LOG_CONTEXT_PUSH_IOV(c ? c->log_extra_fields : NULL, c ? c->n_log_extra_fields : 0); \
1243 LOG_CONTEXT_SET_LOG_LEVEL(unit_get_log_level_max(u))
1245 #define LOG_CONTEXT_PUSH_UNIT(unit) \
1246 _LOG_CONTEXT_PUSH_UNIT(unit, UNIQ_T(u, UNIQ), UNIQ_T(c, UNIQ))