Debian Codegraph @

src/journal/journalctl-filter.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sched.h>
4
5 #include "sd-device.h"
6 #include "sd-journal.h"
7
8 #include "alloc-util.h"
9 #include "chase.h"
10 #include "devnum-util.h"
11 #include "fileio.h"
12 #include "glob-util.h"
13 #include "journal-internal.h"
14 #include "journalctl.h"
15 #include "journalctl-filter.h"
16 #include "journalctl-util.h"
17 #include "log.h"
18 #include "logs-show.h"
19 #include "path-util.h"
20 #include "set.h"
21 #include "stat-util.h"
22 #include "string-util.h"
23 #include "strv.h"
24 #include "unit-name.h"
25
26 static int add_invocation(sd_journal *j) {
27 int r;
28
29 assert(j);
30
31 if (!arg_invocation)
32 return 0;
33
34 assert(!sd_id128_is_null(arg_invocation_id));
35
36 r = add_matches_for_invocation_id(j, arg_invocation_id);
37 if (r < 0)
38 return r;
39
40 return sd_journal_add_conjunction(j);
41 }
42
43 static int add_boot(sd_journal *j) {
44 int r;
45
46 assert(j);
47
48 if (!arg_boot)
49 return 0;
50
51 assert(!sd_id128_is_null(arg_boot_id));
52
53 r = add_match_boot_id(j, arg_boot_id);
54 if (r < 0)
55 return r;
56
57 return sd_journal_add_conjunction(j);
58 }
59
60 static int add_dmesg(sd_journal *j) {
61 int r;
62
63 assert(j);
64
65 if (!arg_dmesg)
66 return 0;
67
68 r = sd_journal_add_match(j, "_TRANSPORT=kernel", SIZE_MAX);
69 if (r < 0)
70 return r;
71
72 return sd_journal_add_conjunction(j);
73 }
74
75 int journal_add_unit_matches(
76 sd_journal *j,
77 MatchUnitFlag flags,
78 UnitNameMangle mangle_flags,
79 char * const *system_units,
80 uid_t uid,
81 char * const *user_units) {
82
83 _cleanup_strv_free_ char **patterns = NULL;
84 bool added = false;
85 int r;
86
87 assert(j);
88
89 if (strv_isempty(system_units) && strv_isempty(user_units))
90 return 0;
91
92 STRV_FOREACH(i, system_units) {
93 _cleanup_free_ char *u = NULL;
94
95 r = unit_name_mangle(*i, UNIT_NAME_MANGLE_GLOB | mangle_flags, &u);
96 if (r < 0)
97 return r;
98
99 if (string_is_glob(u)) {
100 r = strv_consume(&patterns, TAKE_PTR(u));
101 if (r < 0)
102 return r;
103 } else {
104 r = add_matches_for_unit_full(j, flags, u);
105 if (r < 0)
106 return r;
107 r = sd_journal_add_disjunction(j);
108 if (r < 0)
109 return r;
110 added = true;
111 }
112 }
113
114 if (!strv_isempty(patterns)) {
115 _cleanup_set_free_ Set *units = NULL;
116
117 r = get_possible_units(j, SYSTEM_UNITS_FULL, patterns, &units);
118 if (r < 0)
119 return r;
120
121 char *u;
122 SET_FOREACH(u, units) {
123 r = add_matches_for_unit_full(j, flags, u);
124 if (r < 0)
125 return r;
126 r = sd_journal_add_disjunction(j);
127 if (r < 0)
128 return r;
129 added = true;
130 }
131 }
132
133 patterns = strv_free(patterns);
134
135 STRV_FOREACH(i, user_units) {
136 _cleanup_free_ char *u = NULL;
137
138 r = unit_name_mangle(*i, UNIT_NAME_MANGLE_GLOB | mangle_flags, &u);
139 if (r < 0)
140 return r;
141
142 if (string_is_glob(u)) {
143 r = strv_consume(&patterns, TAKE_PTR(u));
144 if (r < 0)
145 return r;
146 } else {
147 r = add_matches_for_user_unit_full(j, flags, uid, u);
148 if (r < 0)
149 return r;
150 r = sd_journal_add_disjunction(j);
151 if (r < 0)
152 return r;
153 added = true;
154 }
155 }
156
157 if (!strv_isempty(patterns)) {
158 _cleanup_set_free_ Set *units = NULL;
159
160 r = get_possible_units(j, USER_UNITS_FULL, patterns, &units);
161 if (r < 0)
162 return r;
163
164 char *u;
165 SET_FOREACH(u, units) {
166 r = add_matches_for_user_unit_full(j, flags, uid, u);
167 if (r < 0)
168 return r;
169 r = sd_journal_add_disjunction(j);
170 if (r < 0)
171 return r;
172 added = true;
173 }
174 }
175
176 /* Complain if the user request matches but nothing whatsoever was found, since otherwise everything
177 * would be matched. */
178 if (!added)
179 return -ENODATA;
180
181 return sd_journal_add_conjunction(j);
182 }
183
184 static int add_units(sd_journal *j) {
185 MatchUnitFlag flags = MATCH_UNIT_ALL;
186
187 assert(j);
188
189 /* When --directory/-D, --root, --file/-i, or --machine/-M is specified, the opened journal file may
190 * be external, and the uid of the systemd-coredump user that generates the coredump entries may be
191 * different from the one in the current host. Let's relax the filter condition in such cases. */
192 if (arg_directory || arg_root || arg_file_stdin || arg_file || arg_machine)
193 flags &= ~MATCH_UNIT_COREDUMP_UID;
194
195 return journal_add_unit_matches(j, flags, arg_quiet ? 0 : UNIT_NAME_MANGLE_WARN,
196 arg_system_units,
197 UID_INVALID, arg_user_units);
198 }
199
200 static int add_syslog_identifier(sd_journal *j) {
201 int r;
202
203 assert(j);
204
205 if (strv_isempty(arg_syslog_identifier))
206 return 0;
207
208 STRV_FOREACH(i, arg_syslog_identifier) {
209 r = journal_add_match_pair(j, "SYSLOG_IDENTIFIER", *i);
210 if (r < 0)
211 return r;
212 r = sd_journal_add_disjunction(j);
213 if (r < 0)
214 return r;
215 }
216
217 return sd_journal_add_conjunction(j);
218 }
219
220 static int add_exclude_identifier(sd_journal *j) {
221 _cleanup_set_free_ Set *excludes = NULL;
222 int r;
223
224 assert(j);
225
226 r = set_put_strdupv(&excludes, arg_exclude_identifier);
227 if (r < 0)
228 return r;
229
230 return set_free_and_replace(j->exclude_syslog_identifiers, excludes);
231 }
232
233 static int add_priorities(sd_journal *j) {
234 int r;
235
236 assert(j);
237
238 if (arg_priorities == 0)
239 return 0;
240
241 for (int i = LOG_EMERG; i <= LOG_DEBUG; i++)
242 if (arg_priorities & (1 << i)) {
243 r = journal_add_matchf(j, "PRIORITY=%d", i);
244 if (r < 0)
245 return r;
246 }
247
248 return sd_journal_add_conjunction(j);
249 }
250
251 static int add_facilities(sd_journal *j) {
252 int r;
253
254 assert(j);
255
256 if (set_isempty(arg_facilities))
257 return 0;
258
259 void *p;
260 SET_FOREACH(p, arg_facilities) {
261 r = journal_add_matchf(j, "SYSLOG_FACILITY=%d", PTR_TO_INT(p));
262 if (r < 0)
263 return r;
264 }
265
266 return sd_journal_add_conjunction(j);
267 }
268
269 static int add_matches_for_executable(sd_journal *j, const char *path) {
270 _cleanup_free_ char *interpreter = NULL;
271 int r;
272
273 assert(j);
274 assert(path);
275
276 if (script_get_shebang_interpreter(path, &interpreter) >= 0) {
277 _cleanup_free_ char *comm = NULL;
278
279 r = path_extract_filename(path, &comm);
280 if (r < 0)
281 return log_error_errno(r, "Failed to extract filename of '%s': %m", path);
282
283 r = journal_add_match_pair(j, "_COMM", strshorten(comm, TASK_COMM_LEN-1));
284 if (r < 0)
285 return log_error_errno(r, "Failed to add match: %m");
286
287 /* Append _EXE only if the interpreter is not a link. Otherwise, it might be outdated often. */
288 if (is_symlink(interpreter) > 0)
289 return 0;
290
291 path = interpreter;
292 }
293
294 r = journal_add_match_pair(j, "_EXE", path);
295 if (r < 0)
296 return log_error_errno(r, "Failed to add match: %m");
297
298 return 0;
299 }
300
301 static int add_matches_for_device(sd_journal *j, const char *devpath) {
302 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
303 int r;
304
305 assert(j);
306 assert(devpath);
307
308 r = sd_device_new_from_devname(&device, devpath);
309 if (r < 0)
310 return log_error_errno(r, "Failed to get device '%s': %m", devpath);
311
312 for (sd_device *d = device; d; ) {
313 const char *subsys, *sysname;
314
315 r = sd_device_get_subsystem(d, &subsys);
316 if (r < 0)
317 goto get_parent;
318
319 r = sd_device_get_sysname(d, &sysname);
320 if (r < 0)
321 goto get_parent;
322
323 r = journal_add_matchf(j, "_KERNEL_DEVICE=+%s:%s", subsys, sysname);
324 if (r < 0)
325 return log_error_errno(r, "Failed to add match: %m");
326
327 dev_t devnum;
328 if (sd_device_get_devnum(d, &devnum) >= 0) {
329 r = journal_add_matchf(j, "_KERNEL_DEVICE=%c" DEVNUM_FORMAT_STR,
330 streq(subsys, "block") ? 'b' : 'c',
331 DEVNUM_FORMAT_VAL(devnum));
332 if (r < 0)
333 return log_error_errno(r, "Failed to add match: %m");
334 }
335
336 get_parent:
337 if (sd_device_get_parent(d, &d) < 0)
338 break;
339 }
340
341 return add_match_boot_id(j, SD_ID128_NULL);
342 }
343
344 static int add_matches_for_path(sd_journal *j, const char *path) {
345 _cleanup_free_ char *p = NULL;
346 struct stat st;
347 int r;
348
349 assert(j);
350 assert(path);
351
352 if (arg_root || arg_machine)
353 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
354 "An extra path in match filter is currently not supported with --root, --image, or -M/--machine.");
355
356 r = chase_and_stat(path, NULL, 0, &p, &st);
357 if (r < 0)
358 return log_error_errno(r, "Couldn't canonicalize path '%s': %m", path);
359
360 if (S_ISREG(st.st_mode) && (0111 & st.st_mode))
361 return add_matches_for_executable(j, p);
362
363 if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))
364 return add_matches_for_device(j, p);
365
366 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "File is neither a device node nor executable: %s", p);
367 }
368
369 static int add_matches(sd_journal *j, char **args) {
370 bool have_term = false;
371 int r;
372
373 assert(j);
374
375 if (strv_isempty(args))
376 return 0;
377
378 STRV_FOREACH(i, args)
379 if (streq(*i, "+")) {
380 if (!have_term)
381 break;
382
383 r = sd_journal_add_disjunction(j);
384 if (r < 0)
385 return log_error_errno(r, "Failed to add disjunction: %m");
386
387 have_term = false;
388
389 } else if (path_is_absolute(*i)) {
390 r = add_matches_for_path(j, *i);
391 if (r < 0)
392 return r;
393 have_term = true;
394
395 } else {
396 r = sd_journal_add_match(j, *i, SIZE_MAX);
397 if (r < 0)
398 return log_error_errno(r, "Failed to add match '%s': %m", *i);
399 have_term = true;
400 }
401
402 if (!have_term)
403 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "\"+\" can only be used between terms.");
404
405 return 0;
406 }
407
408 int add_filters(sd_journal *j, char **matches) {
409 int r;
410
411 assert(j);
412
413 /* First, search boot or invocation ID, as that may set and flush matches and seek journal. */
414 r = journal_acquire_boot(j);
415 if (r < 0)
416 return r;
417
418 r = journal_acquire_invocation(j);
419 if (r < 0)
420 return r;
421
422 /* Clear unexpected matches for safety. */
423 sd_journal_flush_matches(j);
424
425 /* Then, add filters in the below. */
426 if (arg_invocation) {
427 /* If an invocation ID is found, then it is not necessary to add matches for boot and units. */
428 r = add_invocation(j);
429 if (r < 0)
430 return log_error_errno(r, "Failed to add filter for invocation: %m");
431 } else {
432 r = add_boot(j);
433 if (r < 0)
434 return log_error_errno(r, "Failed to add filter for boot: %m");
435
436 r = add_units(j);
437 if (r < 0)
438 return log_error_errno(r, "Failed to add filter for units: %m");
439 }
440
441 r = add_dmesg(j);
442 if (r < 0)
443 return log_error_errno(r, "Failed to add filter for dmesg: %m");
444
445 r = add_syslog_identifier(j);
446 if (r < 0)
447 return log_error_errno(r, "Failed to add filter for syslog identifiers: %m");
448
449 r = add_exclude_identifier(j);
450 if (r < 0)
451 return log_error_errno(r, "Failed to add exclude filter for syslog identifiers: %m");
452
453 r = add_priorities(j);
454 if (r < 0)
455 return log_error_errno(r, "Failed to add filter for priorities: %m");
456
457 r = add_facilities(j);
458 if (r < 0)
459 return log_error_errno(r, "Failed to add filter for facilities: %m");
460
461 r = add_matches(j, matches);
462 if (r < 0)
463 return r;
464
465 if (DEBUG_LOGGING) {
466 _cleanup_free_ char *filter = NULL;
467
468 filter = journal_make_match_string(j);
469 if (!filter)
470 return log_oom();
471
472 log_debug("Journal filter: %s", filter);
473 }
474
475 return 0;
476 }