1
0
mirror of https://github.com/systemd/systemd synced 2026-04-22 23:15:20 +02:00

Compare commits

...

4 Commits

Author SHA1 Message Date
Yu Watanabe
d4782b51ec
Merge pull request #22721 from mrc0mmand/journalctl-tweaks
journal: convert --list-boots to a table
2022-03-14 10:59:13 +09:00
Frantisek Sumsal
a77d351e4d test: extend format-table tests with negative numbers 2022-03-13 21:03:08 +01:00
Frantisek Sumsal
5a1355d848 journal: convert --list-boots to a table
so it can be output as JSON as well.

```
$ build-san/journalctl --list-boots --file boot-test.journal
IDX BOOT ID                          FIRST ENTRY                 LAST ENTRY
 -3 39d66eb1925f4d01b8464d502650a714 Sat 2022-03-05 15:20:33 CET Sat 2022-03-05 16:19:21 CET
 -2 5dffeb08a27344d5ae9e2fc244bbcbc5 Fri 2022-03-11 17:23:57 CET Fri 2022-03-11 17:38:31 CET
 -1 c8ebd52915b642c39eda4bf00f864f79 Fri 2022-03-11 17:38:41 CET Fri 2022-03-11 20:03:46 CET
  0 00bcba97c7094fa88cc5d1cf2a389057 Sat 2022-03-12 20:39:08 CET Sat 2022-03-12 20:40:18 CET

$ build-san/journalctl --list-boots --file boot-test.journal -q
-3 39d66eb1925f4d01b8464d502650a714 Sat 2022-03-05 15:20:33 CET Sat 2022-03-05 16:19:21 CET
-2 5dffeb08a27344d5ae9e2fc244bbcbc5 Fri 2022-03-11 17:23:57 CET Fri 2022-03-11 17:38:31 CET
-1 c8ebd52915b642c39eda4bf00f864f79 Fri 2022-03-11 17:38:41 CET Fri 2022-03-11 20:03:46 CET
 0 00bcba97c7094fa88cc5d1cf2a389057 Sat 2022-03-12 20:39:08 CET Sat 2022-03-12 20:40:18 CET

$ build-san/journalctl --list-boots --file boot-test.journal -o json-pretty
[
        {
                "index" : -3,
                "boot_id" : "39d66eb1925f4d01b8464d502650a714",
                "first_entry" : 1646490033438495,
                "last_entry" : 1646493561047353
        },
        {
                "index" : -2,
                "boot_id" : "5dffeb08a27344d5ae9e2fc244bbcbc5",
                "first_entry" : 1647015837289036,
                "last_entry" : 1647016711595489
        },
        {
                "index" : -1,
                "boot_id" : "c8ebd52915b642c39eda4bf00f864f79",
                "first_entry" : 1647016721056382,
                "last_entry" : 1647025426397414
        },
        {
                "index" : 0,
                "boot_id" : "00bcba97c7094fa88cc5d1cf2a389057",
                "first_entry" : 1647113948506002,
                "last_entry" : 1647114018943637
        }
]
```

Resolves: #14625
2022-03-13 19:25:21 +01:00
Frantisek Sumsal
e3dd9ea8ea macro: account for negative values in DECIMAL_STR_WIDTH()
With negative numbers we wouldn't account for the minus sign, thus
returning a string with one character too short, triggering buffer
overflows in certain situations.
2022-03-13 19:25:18 +01:00
3 changed files with 74 additions and 57 deletions

View File

@ -322,7 +322,7 @@ static inline int __coverity_check_and_return__(int condition) {
#define DECIMAL_STR_WIDTH(x) \ #define DECIMAL_STR_WIDTH(x) \
({ \ ({ \
typeof(x) _x_ = (x); \ typeof(x) _x_ = (x); \
size_t ans = 1; \ size_t ans = IS_SIGNED_INTEGER_TYPE(_x_) ? 2 : 1; \
while ((_x_ /= 10) != 0) \ while ((_x_ /= 10) != 0) \
ans++; \ ans++; \
ans; \ ans; \

View File

@ -33,6 +33,7 @@
#include "dissect-image.h" #include "dissect-image.h"
#include "fd-util.h" #include "fd-util.h"
#include "fileio.h" #include "fileio.h"
#include "format-table.h"
#include "format-util.h" #include "format-util.h"
#include "fs-util.h" #include "fs-util.h"
#include "fsprg.h" #include "fsprg.h"
@ -84,6 +85,7 @@ enum {
}; };
static OutputMode arg_output = OUTPUT_SHORT; static OutputMode arg_output = OUTPUT_SHORT;
static JsonFormatFlags arg_json_format_flags = JSON_FORMAT_OFF;
static bool arg_utc = false; static bool arg_utc = false;
static bool arg_follow = false; static bool arg_follow = false;
static bool arg_full = true; static bool arg_full = true;
@ -577,6 +579,8 @@ static int parse_argv(int argc, char *argv[]) {
if (IN_SET(arg_output, OUTPUT_EXPORT, OUTPUT_JSON, OUTPUT_JSON_PRETTY, OUTPUT_JSON_SSE, OUTPUT_JSON_SEQ, OUTPUT_CAT)) if (IN_SET(arg_output, OUTPUT_EXPORT, OUTPUT_JSON, OUTPUT_JSON_PRETTY, OUTPUT_JSON_SSE, OUTPUT_JSON_SEQ, OUTPUT_CAT))
arg_quiet = true; arg_quiet = true;
if (OUTPUT_MODE_IS_JSON(arg_output))
arg_json_format_flags = output_mode_to_json_format_flags(arg_output) | JSON_FORMAT_COLOR_AUTO;
break; break;
case 'l': case 'l':
@ -1441,8 +1445,9 @@ finish:
} }
static int list_boots(sd_journal *j) { static int list_boots(sd_journal *j) {
int w, i, count; _cleanup_(table_unrefp) Table *table = NULL;
BootId *id, *all_ids; BootId *id, *all_ids;
int count, i, r;
assert(j); assert(j);
@ -1452,23 +1457,30 @@ static int list_boots(sd_journal *j) {
if (count == 0) if (count == 0)
return count; return count;
pager_open(arg_pager_flags); table = table_new(OUTPUT_MODE_IS_JSON(arg_output) ? "index" : "idx", "boot id", "first entry", "last entry");
if (!table)
return log_oom();
/* numbers are one less, but we need an extra char for the sign */ if (arg_full)
w = DECIMAL_STR_WIDTH(count - 1) + 1; table_set_width(table, 0);
i = 0; i = 0;
LIST_FOREACH(boot_list, id, all_ids) { LIST_FOREACH(boot_list, id, all_ids) {
char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX]; r = table_add_many(table,
TABLE_INT, i - count + 1,
printf("% *i " SD_ID128_FORMAT_STR " %s—%s\n", TABLE_SET_ALIGN_PERCENT, 100,
w, i - count + 1, TABLE_ID128, id->id,
SD_ID128_FORMAT_VAL(id->id), TABLE_TIMESTAMP, id->first,
format_timestamp_maybe_utc(a, sizeof(a), id->first), TABLE_TIMESTAMP, id->last);
format_timestamp_maybe_utc(b, sizeof(b), id->last)); if (r < 0)
return table_log_add_error(r);
i++; i++;
} }
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, !arg_quiet);
if (r < 0)
return table_log_print_error(r);
boot_id_free_all(all_ids); boot_id_free_all(all_ids);
return 0; return 0;

View File

@ -396,28 +396,30 @@ TEST(table) {
_cleanup_(table_unrefp) Table *t = NULL; _cleanup_(table_unrefp) Table *t = NULL;
_cleanup_free_ char *formatted = NULL; _cleanup_free_ char *formatted = NULL;
assert_se(t = table_new("one", "two", "three")); assert_se(t = table_new("one", "two", "three", "four"));
assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0); assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(3), 100) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "xxx", TABLE_STRING, "xxx",
TABLE_STRING, "yyy", TABLE_STRING, "yyy",
TABLE_BOOLEAN, true) >= 0); TABLE_BOOLEAN, true,
TABLE_INT, -1) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "a long field", TABLE_STRING, "a long field",
TABLE_STRING, "yyy", TABLE_STRING, "yyy",
TABLE_SET_UPPERCASE, 1, TABLE_SET_UPPERCASE, 1,
TABLE_BOOLEAN, false) >= 0); TABLE_BOOLEAN, false,
TABLE_INT, -999999) >= 0);
assert_se(table_format(t, &formatted) >= 0); assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THREE\n" "ONE TWO THREE FOUR\n"
"xxx yyy yes\n" "xxx yyy yes -1\n"
"a long field YYY no\n")); "a long field YYY no -999999\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -427,20 +429,20 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THREE\n" "ONE TWO THREE FOUR\n"
"xxx yyy yes\n" "xxx yyy yes -1\n"
"a long field YYY no\n")); "a long field YYY no -999999\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
table_set_width(t, 12); table_set_width(t, 15);
assert_se(table_format(t, &formatted) >= 0); assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THR\n" "ONE TWO TH… FO\n"
"xxx yyy yes\n" "xxx yyy yes -1\n"
"a … YYY no\n")); "a … YYY no -9…\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -449,9 +451,9 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"… … …\n" "… … …\n"
"… … …\n" "… … …\n"
"… … …\n")); "… … …\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -460,9 +462,9 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"… … …\n" "… … …\n"
"… … …\n" "… … …\n"
"… … …\n")); "… … …\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -473,9 +475,9 @@ TEST(table) {
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"ONE TWO THREE\n" "ONE TWO THREE FOUR\n"
"a long field YYY no\n" "a long field YYY no -999999\n"
"xxx yyy yes\n")); "xxx yyy yes -1\n"));
formatted = mfree(formatted); formatted = mfree(formatted);
@ -484,27 +486,30 @@ TEST(table) {
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "fäää", TABLE_STRING, "fäää",
TABLE_STRING, "uuu", TABLE_STRING, "uuu",
TABLE_BOOLEAN, true) >= 0); TABLE_BOOLEAN, true,
TABLE_INT, 42) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_STRING, "fäää", TABLE_STRING, "fäää",
TABLE_STRING, "zzz", TABLE_STRING, "zzz",
TABLE_BOOLEAN, false) >= 0); TABLE_BOOLEAN, false,
TABLE_INT, 0) >= 0);
assert_se(table_add_many(t, assert_se(table_add_many(t,
TABLE_EMPTY, TABLE_EMPTY,
TABLE_SIZE, (uint64_t) 4711, TABLE_SIZE, (uint64_t) 4711,
TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0); TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE,
TABLE_INT64, (uint64_t) -123456789) >= 0);
assert_se(table_format(t, &formatted) >= 0); assert_se(table_format(t, &formatted) >= 0);
printf("%s\n", formatted); printf("%s\n", formatted);
assert_se(streq(formatted, assert_se(streq(formatted,
"a long field YYY no\n" "a long field YYY no -999999\n"
"fäää zzz no\n" "fäää zzz no 0\n"
"fäää uuu yes\n" "fäää uuu yes 42\n"
"xxx yyy yes\n" "xxx yyy yes -1\n"
" 4.6K 5min\n")); " 4.6K 5min -123456789\n"));
formatted = mfree(formatted); formatted = mfree(formatted);