• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 19381060735

14 Nov 2025 10:54PM UTC coverage: 72.37% (-0.02%) from 72.393%
19381060735

push

github

web-flow
5 TPM tweaks (#39712)

Fixes: #38939
Fixes: #39150

60 of 78 new or added lines in 3 files covered. (76.92%)

2631 existing lines in 50 files now uncovered.

307287 of 424606 relevant lines covered (72.37%)

1234902.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.59
/src/basic/log-context.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <threads.h>
4

5
#include "alloc-util.h"
6
#include "env-util.h"
7
#include "iovec-util.h"
8
#include "log.h"
9
#include "log-context.h"
10
#include "string-util.h"
11
#include "strv.h"
12

13
static int saved_log_context_enabled = -1;
14
static thread_local LIST_HEAD(LogContext, _log_context) = NULL;
15
static thread_local size_t _log_context_num_fields = 0;
16

17
bool log_context_enabled(void) {
839,210✔
18
        int r;
839,210✔
19

20
        if (log_get_max_level() == LOG_DEBUG)
839,210✔
21
                return true;
22

23
        if (saved_log_context_enabled >= 0)
131,365✔
24
                return saved_log_context_enabled;
122,802✔
25

26
        r = secure_getenv_bool("SYSTEMD_ENABLE_LOG_CONTEXT");
8,563✔
27
        if (r < 0 && r != -ENXIO)
8,563✔
28
                log_debug_errno(r, "Failed to parse $SYSTEMD_ENABLE_LOG_CONTEXT, ignoring: %m");
×
29

30
        saved_log_context_enabled = r > 0;
8,563✔
31

32
        return saved_log_context_enabled;
8,563✔
33
}
34

35
static LogContext* log_context_attach(LogContext *c) {
755,193✔
36
        assert(c);
755,193✔
37

38
        _log_context_num_fields += strv_length(c->fields);
755,193✔
39
        _log_context_num_fields += c->n_input_iovec;
755,193✔
40
        _log_context_num_fields += !!c->key;
755,193✔
41

42
        return LIST_PREPEND(ll, _log_context, c);
755,193✔
43
}
44

45
static LogContext* log_context_detach(LogContext *c) {
734,995✔
46
        if (!c)
734,995✔
47
                return NULL;
48

49
        assert(_log_context_num_fields >= strv_length(c->fields) + c->n_input_iovec +!!c->key);
734,995✔
50
        _log_context_num_fields -= strv_length(c->fields);
734,995✔
51
        _log_context_num_fields -= c->n_input_iovec;
734,995✔
52
        _log_context_num_fields -= !!c->key;
734,995✔
53

54
        LIST_REMOVE(ll, _log_context, c);
734,995✔
55
        return NULL;
734,995✔
56
}
57

58
LogContext* log_context_new(const char *key, const char *value) {
44,908✔
59
        assert(key);
44,908✔
60
        assert(endswith(key, "="));
44,908✔
61

62
        if (!value)
44,908✔
63
                return NULL;
64

65
        LIST_FOREACH(ll, i, _log_context)
67,822✔
66
                if (i->key == key && i->value == value)
42,901✔
67
                        return log_context_ref(i);
19,987✔
68

69
        LogContext *c = new(LogContext, 1);
24,921✔
70
        if (!c)
24,921✔
71
                return NULL;
72

73
        *c = (LogContext) {
24,921✔
74
                .n_ref = 1,
75
                .key = (char *) key,
76
                .value = (char *) value,
77
        };
78

79
        return log_context_attach(c);
24,921✔
80
}
81

82
LogContext* log_context_new_strv(char **fields, bool owned) {
729,131✔
83
        if (!fields)
729,131✔
84
                return NULL;
85

86
        LIST_FOREACH(ll, i, _log_context)
747,119✔
87
                if (i->fields == fields) {
19,084✔
88
                        assert(!owned);
18✔
89
                        return log_context_ref(i);
18✔
90
                }
91

92
        LogContext *c = new(LogContext, 1);
728,035✔
93
        if (!c)
728,035✔
94
                return NULL;
95

96
        *c = (LogContext) {
728,035✔
97
                .n_ref = 1,
98
                .fields = fields,
99
                .owned = owned,
100
        };
101

102
        return log_context_attach(c);
728,035✔
103
}
104

105
LogContext* log_context_new_iov(struct iovec *input_iovec, size_t n_input_iovec, bool owned) {
319,185✔
106
        if (!input_iovec || n_input_iovec == 0)
319,185✔
107
                return NULL;
108

109
        LIST_FOREACH(ll, i, _log_context)
3,774✔
110
                if (i->input_iovec == input_iovec && i->n_input_iovec == n_input_iovec) {
1,537✔
111
                        assert(!owned);
402✔
112
                        return log_context_ref(i);
402✔
113
                }
114

115
        LogContext *c = new(LogContext, 1);
2,237✔
116
        if (!c)
2,237✔
117
                return NULL;
118

119
        *c = (LogContext) {
2,237✔
120
                .n_ref = 1,
121
                .input_iovec = input_iovec,
122
                .n_input_iovec = n_input_iovec,
123
                .owned = owned,
124
        };
125

126
        return log_context_attach(c);
2,237✔
127
}
128

129
static LogContext* log_context_free(LogContext *c) {
734,995✔
130
        if (!c)
734,995✔
131
                return NULL;
132

133
        log_context_detach(c);
734,995✔
134

135
        if (c->owned) {
734,995✔
136
                strv_free(c->fields);
707,808✔
137
                iovec_array_free(c->input_iovec, c->n_input_iovec);
707,808✔
138
                free(c->key);
707,808✔
139
                free(c->value);
707,808✔
140
        }
141

142
        return mfree(c);
734,995✔
143
}
144

145
DEFINE_TRIVIAL_REF_UNREF_FUNC(LogContext, log_context, log_context_free);
755,666✔
146

147
LogContext* log_context_new_strv_consume(char **fields) {
707,854✔
148
        LogContext *c = log_context_new_strv(fields, /*owned=*/ true);
707,854✔
149
        if (!c)
707,854✔
UNCOV
150
                strv_free(fields);
×
151

152
        return c;
707,854✔
153
}
154

155
LogContext* log_context_new_iov_consume(struct iovec *input_iovec, size_t n_input_iovec) {
9✔
156
        LogContext *c = log_context_new_iov(input_iovec, n_input_iovec, /*owned=*/ true);
9✔
157
        if (!c)
9✔
UNCOV
158
                iovec_array_free(input_iovec, n_input_iovec);
×
159

160
        return c;
9✔
161
}
162

163
LogContext* log_context_head(void) {
5,394,494✔
164
        return _log_context;
5,394,494✔
165
}
166

167
size_t log_context_num_contexts(void) {
81✔
168
        size_t n = 0;
81✔
169

170
        LIST_FOREACH(ll, c, _log_context)
225✔
171
                n++;
144✔
172

173
        return n;
81✔
174
}
175

176
size_t log_context_num_fields(void) {
5,394,575✔
177
        return _log_context_num_fields;
5,394,575✔
178
}
179

180
void _reset_log_level(int *saved_log_level) {
299,246✔
181
        assert(saved_log_level);
299,246✔
182

183
        log_set_max_level(*saved_log_level);
299,246✔
184
}
299,246✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc