Skip to content

Commit 8ad2012

Browse files
authored
Merge pull request systemd#14966 from keszybz/journalctl-facilities
journalctl: filtering by facility
2 parents b3ce4e2 + 196dedd commit 8ad2012

File tree

3 files changed

+89
-1
lines changed

3 files changed

+89
-1
lines changed

man/journalctl.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,16 @@
597597
priorities.</para></listitem>
598598
</varlistentry>
599599

600+
<varlistentry>
601+
<term><option>--facility=</option></term>
602+
603+
<listitem><para>Filter output by syslog facility. Takes a comma-separated list of numbers or facility
604+
names. The names are the usual syslog facilities as documented in
605+
<citerefentry project='man-pages'><refentrytitle>syslog</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
606+
<option>--facility=help</option> may be used to display a list of known facility names and exit.
607+
</para></listitem>
608+
</varlistentry>
609+
600610
<varlistentry>
601611
<term><option>-g</option></term>
602612
<term><option>--grep=</option></term>

src/basic/string-table.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ssize_t string_table_lookup(const char * const *table, size_t len, const char *k
4444
char *s; \
4545
if (i < 0 || i > max) \
4646
return -ERANGE; \
47-
if (i < (type) ELEMENTSOF(name##_table)) { \
47+
if (i < (type) ELEMENTSOF(name##_table) && name##_table[i]) { \
4848
s = strdup(name##_table[i]); \
4949
if (!s) \
5050
return -ENOMEM; \

src/journal/journalctl.c

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
#include "sigbus.h"
6363
#include "string-table.h"
6464
#include "strv.h"
65+
#include "stdio-util.h"
6566
#include "syslog-util.h"
6667
#include "terminal-util.h"
6768
#include "tmpfile-util.h"
@@ -101,6 +102,7 @@ static const char *arg_directory = NULL;
101102
static char **arg_file = NULL;
102103
static bool arg_file_stdin = false;
103104
static int arg_priorities = 0xFF;
105+
static Set *arg_facilities = NULL;
104106
static char *arg_verify_key = NULL;
105107
#if HAVE_GCRYPT
106108
static usec_t arg_interval = DEFAULT_FSS_INTERVAL_USEC;
@@ -303,6 +305,21 @@ static int parse_boot_descriptor(const char *x, sd_id128_t *boot_id, int *offset
303305
return 1;
304306
}
305307

308+
static int help_facilities(void) {
309+
if (!arg_quiet)
310+
puts("Available facilities:");
311+
312+
for (int i = 0; i < LOG_NFACILITIES; i++) {
313+
_cleanup_free_ char *t = NULL;
314+
315+
if (log_facility_unshifted_to_string_alloc(i, &t))
316+
return log_oom();
317+
puts(t);
318+
}
319+
320+
return 0;
321+
}
322+
306323
static int help(void) {
307324
_cleanup_free_ char *link = NULL;
308325
int r;
@@ -332,6 +349,7 @@ static int help(void) {
332349
" --user-unit=UNIT Show logs from the specified user unit\n"
333350
" -t --identifier=STRING Show entries with the specified syslog identifier\n"
334351
" -p --priority=RANGE Show entries with the specified priority\n"
352+
" --facility=FACILITY... Show entries with the specified facilities\n"
335353
" -g --grep=PATTERN Show entries with MESSAGE matching PATTERN\n"
336354
" --case-sensitive[=BOOL] Force case sensitive or insenstive matching\n"
337355
" -e --pager-end Immediately jump to the end in the pager\n"
@@ -404,6 +422,7 @@ static int parse_argv(int argc, char *argv[]) {
404422
ARG_SYSTEM,
405423
ARG_ROOT,
406424
ARG_HEADER,
425+
ARG_FACILITY,
407426
ARG_SETUP_KEYS,
408427
ARG_FILE,
409428
ARG_INTERVAL,
@@ -461,6 +480,7 @@ static int parse_argv(int argc, char *argv[]) {
461480
{ "header", no_argument, NULL, ARG_HEADER },
462481
{ "identifier", required_argument, NULL, 't' },
463482
{ "priority", required_argument, NULL, 'p' },
483+
{ "facility", required_argument, NULL, ARG_FACILITY },
464484
{ "grep", required_argument, NULL, 'g' },
465485
{ "case-sensitive", optional_argument, NULL, ARG_CASE_SENSITIVE },
466486
{ "setup-keys", no_argument, NULL, ARG_SETUP_KEYS },
@@ -832,6 +852,41 @@ static int parse_argv(int argc, char *argv[]) {
832852
break;
833853
}
834854

855+
case ARG_FACILITY: {
856+
const char *p;
857+
858+
for (p = optarg;;) {
859+
_cleanup_free_ char *fac = NULL;
860+
int num;
861+
862+
r = extract_first_word(&p, &fac, ",", 0);
863+
if (r < 0)
864+
return log_error_errno(r, "Failed to parse facilities: %s", optarg);
865+
if (r == 0)
866+
break;
867+
868+
if (streq(fac, "help")) {
869+
help_facilities();
870+
return 0;
871+
}
872+
873+
num = log_facility_unshifted_from_string(fac);
874+
if (num < 0)
875+
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
876+
"Bad --facility= argument \"%s\".", fac);
877+
878+
r = set_ensure_allocated(&arg_facilities, NULL);
879+
if (r < 0)
880+
return log_oom();
881+
882+
r = set_put(arg_facilities, INT_TO_PTR(num));
883+
if (r < 0)
884+
return log_oom();
885+
}
886+
887+
break;
888+
}
889+
835890
#if HAVE_PCRE2
836891
case 'g':
837892
arg_pattern = optarg;
@@ -1676,6 +1731,24 @@ static int add_priorities(sd_journal *j) {
16761731
return 0;
16771732
}
16781733

1734+
static int add_facilities(sd_journal *j) {
1735+
void *p;
1736+
Iterator it;
1737+
int r;
1738+
1739+
SET_FOREACH(p, arg_facilities, it) {
1740+
char match[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
1741+
1742+
xsprintf(match, "SYSLOG_FACILITY=%d", PTR_TO_INT(p));
1743+
1744+
r = sd_journal_add_match(j, match, strlen(match));
1745+
if (r < 0)
1746+
return log_error_errno(r, "Failed to add match: %m");
1747+
}
1748+
1749+
return 0;
1750+
}
1751+
16791752
static int add_syslog_identifier(sd_journal *j) {
16801753
int r;
16811754
char **i;
@@ -2314,6 +2387,10 @@ int main(int argc, char *argv[]) {
23142387
if (r < 0)
23152388
goto finish;
23162389

2390+
r = add_facilities(j);
2391+
if (r < 0)
2392+
goto finish;
2393+
23172394
r = add_matches(j, argv + optind);
23182395
if (r < 0)
23192396
goto finish;
@@ -2681,6 +2758,7 @@ int main(int argc, char *argv[]) {
26812758

26822759
strv_free(arg_file);
26832760

2761+
set_free(arg_facilities);
26842762
strv_free(arg_syslog_identifier);
26852763
strv_free(arg_system_units);
26862764
strv_free(arg_user_units);

0 commit comments

Comments
 (0)