|
62 | 62 | #include "sigbus.h" |
63 | 63 | #include "string-table.h" |
64 | 64 | #include "strv.h" |
| 65 | +#include "stdio-util.h" |
65 | 66 | #include "syslog-util.h" |
66 | 67 | #include "terminal-util.h" |
67 | 68 | #include "tmpfile-util.h" |
@@ -101,6 +102,7 @@ static const char *arg_directory = NULL; |
101 | 102 | static char **arg_file = NULL; |
102 | 103 | static bool arg_file_stdin = false; |
103 | 104 | static int arg_priorities = 0xFF; |
| 105 | +static Set *arg_facilities = NULL; |
104 | 106 | static char *arg_verify_key = NULL; |
105 | 107 | #if HAVE_GCRYPT |
106 | 108 | 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 |
303 | 305 | return 1; |
304 | 306 | } |
305 | 307 |
|
| 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 | + |
306 | 323 | static int help(void) { |
307 | 324 | _cleanup_free_ char *link = NULL; |
308 | 325 | int r; |
@@ -332,6 +349,7 @@ static int help(void) { |
332 | 349 | " --user-unit=UNIT Show logs from the specified user unit\n" |
333 | 350 | " -t --identifier=STRING Show entries with the specified syslog identifier\n" |
334 | 351 | " -p --priority=RANGE Show entries with the specified priority\n" |
| 352 | + " --facility=FACILITY... Show entries with the specified facilities\n" |
335 | 353 | " -g --grep=PATTERN Show entries with MESSAGE matching PATTERN\n" |
336 | 354 | " --case-sensitive[=BOOL] Force case sensitive or insenstive matching\n" |
337 | 355 | " -e --pager-end Immediately jump to the end in the pager\n" |
@@ -404,6 +422,7 @@ static int parse_argv(int argc, char *argv[]) { |
404 | 422 | ARG_SYSTEM, |
405 | 423 | ARG_ROOT, |
406 | 424 | ARG_HEADER, |
| 425 | + ARG_FACILITY, |
407 | 426 | ARG_SETUP_KEYS, |
408 | 427 | ARG_FILE, |
409 | 428 | ARG_INTERVAL, |
@@ -461,6 +480,7 @@ static int parse_argv(int argc, char *argv[]) { |
461 | 480 | { "header", no_argument, NULL, ARG_HEADER }, |
462 | 481 | { "identifier", required_argument, NULL, 't' }, |
463 | 482 | { "priority", required_argument, NULL, 'p' }, |
| 483 | + { "facility", required_argument, NULL, ARG_FACILITY }, |
464 | 484 | { "grep", required_argument, NULL, 'g' }, |
465 | 485 | { "case-sensitive", optional_argument, NULL, ARG_CASE_SENSITIVE }, |
466 | 486 | { "setup-keys", no_argument, NULL, ARG_SETUP_KEYS }, |
@@ -832,6 +852,41 @@ static int parse_argv(int argc, char *argv[]) { |
832 | 852 | break; |
833 | 853 | } |
834 | 854 |
|
| 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 | + |
835 | 890 | #if HAVE_PCRE2 |
836 | 891 | case 'g': |
837 | 892 | arg_pattern = optarg; |
@@ -1676,6 +1731,24 @@ static int add_priorities(sd_journal *j) { |
1676 | 1731 | return 0; |
1677 | 1732 | } |
1678 | 1733 |
|
| 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 | + |
1679 | 1752 | static int add_syslog_identifier(sd_journal *j) { |
1680 | 1753 | int r; |
1681 | 1754 | char **i; |
@@ -2314,6 +2387,10 @@ int main(int argc, char *argv[]) { |
2314 | 2387 | if (r < 0) |
2315 | 2388 | goto finish; |
2316 | 2389 |
|
| 2390 | + r = add_facilities(j); |
| 2391 | + if (r < 0) |
| 2392 | + goto finish; |
| 2393 | + |
2317 | 2394 | r = add_matches(j, argv + optind); |
2318 | 2395 | if (r < 0) |
2319 | 2396 | goto finish; |
@@ -2681,6 +2758,7 @@ int main(int argc, char *argv[]) { |
2681 | 2758 |
|
2682 | 2759 | strv_free(arg_file); |
2683 | 2760 |
|
| 2761 | + set_free(arg_facilities); |
2684 | 2762 | strv_free(arg_syslog_identifier); |
2685 | 2763 | strv_free(arg_system_units); |
2686 | 2764 | strv_free(arg_user_units); |
|
0 commit comments