From 1092e82bb74555da354d0507398a833a65c8f068 Mon Sep 17 00:00:00 2001 From: Luca Boccassi Date: Mon, 13 Mar 2023 21:34:37 +0000 Subject: [PATCH] core: append LogExtraFields= values to log_unit* messages This ensure messages from PID1 regarding a unit also contain those fields. For example, portable services have PORTABLE= as extra fields, which is useful to identify which version of a portable image produced a log message like an error or an oomd kill. --- man/systemd.exec.xml | 4 ++-- src/core/unit.h | 23 ++++++++++++++++++++--- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml index 653aa0d7921..4001123a962 100644 --- a/man/systemd.exec.xml +++ b/man/systemd.exec.xml @@ -2909,8 +2909,8 @@ StandardInputData=V2XigLJyZSBubyBzdHJhbmdlcnMgdG8gbG92ZQpZb3Uga25vdyB0aGUgcnVsZX LogExtraFields= Configures additional log metadata fields to include in all log records generated by - processes associated with this unit. This setting takes one or more journal field assignments in the - format FIELD=VALUE separated by whitespace. See + processes associated with this unit, including systemd. This setting takes one or more journal field + assignments in the format FIELD=VALUE separated by whitespace. See systemd.journal-fields7 for details on the journal field concept. Even though the underlying journal implementation permits binary field values, this setting accepts only valid UTF-8 values. To include space characters in a diff --git a/src/core/unit.h b/src/core/unit.h index 3f8377fbf64..d5a6d595e29 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -1078,7 +1078,13 @@ Condition *unit_find_failed_condition(Unit *u); ({ \ const Unit *_u = (unit); \ const int _l = (level); \ - (log_get_max_level() < LOG_PRI(_l) || (_u && !unit_log_level_test(_u, _l))) ? -ERRNO_VALUE(error) : \ + bool _do_log = !(log_get_max_level() < LOG_PRI(_l) || \ + (_u && !unit_log_level_test(_u, _l))); \ + const ExecContext *_c = _do_log && _u ? \ + unit_get_exec_context(_u) : NULL; \ + LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL, \ + _c ? _c->n_log_extra_fields : 0); \ + !_do_log ? -ERRNO_VALUE(error) : \ _u ? log_object_internal(_l, error, PROJECT_FILE, __LINE__, __func__, _u->manager->unit_log_field, _u->id, _u->manager->invocation_log_field, _u->invocation_id_string, ##__VA_ARGS__) : \ log_internal(_l, error, PROJECT_FILE, __LINE__, __func__, ##__VA_ARGS__); \ }) @@ -1116,7 +1122,12 @@ Condition *unit_find_failed_condition(Unit *u); ({ \ const Unit *_u = (unit); \ const int _l = (level); \ - unit_log_level_test(_u, _l) ? \ + bool _do_log = unit_log_level_test(_u, _l); \ + const ExecContext *_c = _do_log && _u ? \ + unit_get_exec_context(_u) : NULL; \ + LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL, \ + _c ? _c->n_log_extra_fields : 0); \ + _do_log ? \ log_struct_errno(_l, error, __VA_ARGS__, LOG_UNIT_ID(_u)) : \ -ERRNO_VALUE(error); \ }) @@ -1125,8 +1136,14 @@ Condition *unit_find_failed_condition(Unit *u); #define log_unit_struct_iovec_errno(unit, level, error, iovec, n_iovec) \ ({ \ + const Unit *_u = (unit); \ const int _l = (level); \ - unit_log_level_test(unit, _l) ? \ + bool _do_log = unit_log_level_test(_u, _l); \ + const ExecContext *_c = _do_log && _u ? \ + unit_get_exec_context(_u) : NULL; \ + LOG_CONTEXT_PUSH_IOV(_c ? _c->log_extra_fields : NULL, \ + _c ? _c->n_log_extra_fields : 0); \ + _do_log ? \ log_struct_iovec_errno(_l, error, iovec, n_iovec) : \ -ERRNO_VALUE(error); \ }) -- 2.47.3