Skip to content

Commit f843f85

Browse files
committed
tree-wide: FORMAT_TIMESTAMP() or friends must be used as a function argument
Follow-ups for systemd#20109.
1 parent fa3f917 commit f843f85

File tree

5 files changed

+64
-93
lines changed

5 files changed

+64
-93
lines changed

src/journal/journald-server.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -209,29 +209,27 @@ void server_space_usage_message(Server *s, JournalStorage *storage) {
209209

210210
const JournalMetrics *metrics = &storage->metrics;
211211

212-
const char
213-
*vfs_used = FORMAT_BYTES(storage->space.vfs_used),
214-
*limit = FORMAT_BYTES(storage->space.limit),
215-
*available = FORMAT_BYTES(storage->space.available);
216-
217212
server_driver_message(s, 0,
218213
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_USAGE_STR,
219214
LOG_MESSAGE("%s (%s) is %s, max %s, %s free.",
220-
storage->name, storage->path, vfs_used, limit, available),
215+
storage->name, storage->path,
216+
FORMAT_BYTES(storage->space.vfs_used),
217+
FORMAT_BYTES(storage->space.limit),
218+
FORMAT_BYTES(storage->space.available)),
221219
"JOURNAL_NAME=%s", storage->name,
222220
"JOURNAL_PATH=%s", storage->path,
223221
"CURRENT_USE=%"PRIu64, storage->space.vfs_used,
224-
"CURRENT_USE_PRETTY=%s", vfs_used,
222+
"CURRENT_USE_PRETTY=%s", FORMAT_BYTES(storage->space.vfs_used),
225223
"MAX_USE=%"PRIu64, metrics->max_use,
226224
"MAX_USE_PRETTY=%s", FORMAT_BYTES(metrics->max_use),
227225
"DISK_KEEP_FREE=%"PRIu64, metrics->keep_free,
228226
"DISK_KEEP_FREE_PRETTY=%s", FORMAT_BYTES(metrics->keep_free),
229227
"DISK_AVAILABLE=%"PRIu64, storage->space.vfs_available,
230228
"DISK_AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage->space.vfs_available),
231229
"LIMIT=%"PRIu64, storage->space.limit,
232-
"LIMIT_PRETTY=%s", limit,
230+
"LIMIT_PRETTY=%s", FORMAT_BYTES(storage->space.limit),
233231
"AVAILABLE=%"PRIu64, storage->space.available,
234-
"AVAILABLE_PRETTY=%s", available,
232+
"AVAILABLE_PRETTY=%s", FORMAT_BYTES(storage->space.available),
235233
NULL);
236234
}
237235

src/login/loginctl.c

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,6 @@ static int print_session_status_info(sd_bus *bus, const char *path, bool *new_li
450450

451451
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
452452
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
453-
const char *s1, *s2;
454453
SessionStatusInfo i = {};
455454
int r;
456455

@@ -470,13 +469,10 @@ static int print_session_status_info(sd_bus *bus, const char *path, bool *new_li
470469
else
471470
printf("%"PRIu32"\n", i.uid);
472471

473-
s1 = FORMAT_TIMESTAMP_RELATIVE(i.timestamp.realtime);
474-
s2 = FORMAT_TIMESTAMP(i.timestamp.realtime);
475-
476-
if (s1)
477-
printf("\t Since: %s; %s\n", s2, s1);
478-
else if (s2)
479-
printf("\t Since: %s\n", s2);
472+
if (i.timestamp.realtime > 0 && i.timestamp.realtime < USEC_INFINITY)
473+
printf("\t Since: %s; %s\n",
474+
FORMAT_TIMESTAMP(i.timestamp.realtime),
475+
FORMAT_TIMESTAMP_RELATIVE(i.timestamp.realtime));
480476

481477
if (i.leader > 0) {
482478
_cleanup_free_ char *t = NULL;
@@ -579,7 +575,6 @@ static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line)
579575

580576
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
581577
_cleanup_(sd_bus_message_unrefp) sd_bus_message *m = NULL;
582-
const char *s1, *s2;
583578
_cleanup_(user_status_info_clear) UserStatusInfo i = {};
584579
int r;
585580

@@ -597,13 +592,10 @@ static int print_user_status_info(sd_bus *bus, const char *path, bool *new_line)
597592
else
598593
printf("%"PRIu32"\n", i.uid);
599594

600-
s1 = FORMAT_TIMESTAMP_RELATIVE(i.timestamp.realtime);
601-
s2 = FORMAT_TIMESTAMP(i.timestamp.realtime);
602-
603-
if (s1)
604-
printf("\t Since: %s; %s\n", s2, s1);
605-
else if (s2)
606-
printf("\t Since: %s\n", s2);
595+
if (i.timestamp.realtime > 0 && i.timestamp.realtime < USEC_INFINITY)
596+
printf("\t Since: %s; %s\n",
597+
FORMAT_TIMESTAMP(i.timestamp.realtime),
598+
FORMAT_TIMESTAMP_RELATIVE(i.timestamp.realtime));
607599

608600
if (!isempty(i.state))
609601
printf("\t State: %s\n", i.state);

src/machine/machinectl.c

Lines changed: 33 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,7 @@ static void machine_status_info_clear(MachineStatusInfo *info) {
507507
}
508508

509509
static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
510-
_cleanup_free_ char *addresses = NULL;
511-
const char *s1, *s2;
510+
_cleanup_free_ char *addresses = NULL, *s1 = NULL, *s2 = NULL;
512511
int ifi = -1;
513512

514513
assert(bus);
@@ -521,12 +520,12 @@ static void print_machine_status_info(sd_bus *bus, MachineStatusInfo *i) {
521520
else
522521
putchar('\n');
523522

524-
s1 = FORMAT_TIMESTAMP_RELATIVE(i->timestamp.realtime);
525-
s2 = FORMAT_TIMESTAMP(i->timestamp.realtime);
523+
s1 = strdup(strempty(FORMAT_TIMESTAMP_RELATIVE(i->timestamp.realtime)));
524+
s2 = strdup(strempty(FORMAT_TIMESTAMP(i->timestamp.realtime)));
526525

527-
if (s1)
528-
printf("\t Since: %s; %s\n", s2, s1);
529-
else if (s2)
526+
if (!isempty(s1))
527+
printf("\t Since: %s; %s\n", strna(s2), s1);
528+
else if (!isempty(s2))
530529
printf("\t Since: %s\n", s2);
531530

532531
if (i->leader > 0) {
@@ -826,8 +825,6 @@ typedef struct ImageStatusInfo {
826825
} ImageStatusInfo;
827826

828827
static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
829-
const char *s1, *s2, *s3, *s4;
830-
831828
assert(bus);
832829
assert(i);
833830

@@ -853,33 +850,29 @@ static void print_image_status_info(sd_bus *bus, ImageStatusInfo *i) {
853850
i->read_only ? "read-only" : "writable",
854851
i->read_only ? ansi_normal() : "");
855852

856-
s1 = FORMAT_TIMESTAMP_RELATIVE(i->crtime);
857-
s2 = FORMAT_TIMESTAMP(i->crtime);
858-
if (s1 && s2)
859-
printf("\t Created: %s; %s\n", s2, s1);
860-
else if (s2)
861-
printf("\t Created: %s\n", s2);
862-
863-
s1 = FORMAT_TIMESTAMP_RELATIVE(i->mtime);
864-
s2 = FORMAT_TIMESTAMP(i->mtime);
865-
if (s1 && s2)
866-
printf("\tModified: %s; %s\n", s2, s1);
867-
else if (s2)
868-
printf("\tModified: %s\n", s2);
869-
870-
s3 = FORMAT_BYTES(i->usage);
871-
s4 = i->usage_exclusive != i->usage ? FORMAT_BYTES(i->usage_exclusive) : NULL;
872-
if (s3 && s4)
873-
printf("\t Usage: %s (exclusive: %s)\n", s3, s4);
874-
else if (s3)
875-
printf("\t Usage: %s\n", s3);
876-
877-
s3 = FORMAT_BYTES(i->limit);
878-
s4 = i->limit_exclusive != i->limit ? FORMAT_BYTES(i->limit_exclusive) : NULL;
879-
if (s3 && s4)
880-
printf("\t Limit: %s (exclusive: %s)\n", s3, s4);
881-
else if (s3)
882-
printf("\t Limit: %s\n", s3);
853+
if (i->crtime > 0 && i->crtime < USEC_INFINITY)
854+
printf("\t Created: %s; %s\n",
855+
FORMAT_TIMESTAMP(i->crtime), FORMAT_TIMESTAMP_RELATIVE(i->crtime));
856+
857+
if (i->mtime > 0 && i->mtime < USEC_INFINITY)
858+
printf("\tModified: %s; %s\n",
859+
FORMAT_TIMESTAMP(i->mtime), FORMAT_TIMESTAMP_RELATIVE(i->mtime));
860+
861+
if (i->usage != UINT64_MAX) {
862+
if (i->usage_exclusive != i->usage && i->usage_exclusive != UINT64_MAX)
863+
printf("\t Usage: %s (exclusive: %s)\n",
864+
FORMAT_BYTES(i->usage), FORMAT_BYTES(i->usage_exclusive));
865+
else
866+
printf("\t Usage: %s\n", FORMAT_BYTES(i->usage));
867+
}
868+
869+
if (i->limit != UINT64_MAX) {
870+
if (i->limit_exclusive != i->limit && i->limit_exclusive != UINT64_MAX)
871+
printf("\t Limit: %s (exclusive: %s)\n",
872+
FORMAT_BYTES(i->limit), FORMAT_BYTES(i->limit_exclusive));
873+
else
874+
printf("\t Limit: %s\n", FORMAT_BYTES(i->limit));
875+
}
883876
}
884877

885878
static int show_image_info(sd_bus *bus, const char *path, bool *new_line) {
@@ -934,18 +927,14 @@ typedef struct PoolStatusInfo {
934927
} PoolStatusInfo;
935928

936929
static void print_pool_status_info(sd_bus *bus, PoolStatusInfo *i) {
937-
char *s;
938-
939930
if (i->path)
940931
printf("\t Path: %s\n", i->path);
941932

942-
s = FORMAT_BYTES(i->usage);
943-
if (s)
944-
printf("\t Usage: %s\n", s);
933+
if (i->usage != UINT64_MAX)
934+
printf("\t Usage: %s\n", FORMAT_BYTES(i->usage));
945935

946-
s = FORMAT_BYTES(i->limit);
947-
if (s)
948-
printf("\t Limit: %s\n", s);
936+
if (i->limit != UINT64_MAX)
937+
printf("\t Limit: %s\n", FORMAT_BYTES(i->limit));
949938
}
950939

951940
static int show_pool_info(sd_bus *bus) {

src/systemctl/systemctl-show.c

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ static void print_status_info(
300300
UnitStatusInfo *i,
301301
bool *ellipsized) {
302302

303-
const char *s1, *s2, *active_on, *active_off, *on, *off, *ss, *fs;
303+
const char *active_on, *active_off, *on, *off, *ss, *fs;
304304
_cleanup_free_ char *formatted_path = NULL;
305305
ExecStatusInfo *p;
306306
usec_t timestamp;
@@ -418,12 +418,10 @@ static void print_status_info(
418418
STRPTR_IN_SET(i->active_state, "activating") ? i->inactive_exit_timestamp :
419419
i->active_exit_timestamp;
420420

421-
s1 = FORMAT_TIMESTAMP_RELATIVE(timestamp);
422-
s2 = FORMAT_TIMESTAMP_STYLE(timestamp, arg_timestamp_style);
423-
if (s1)
424-
printf(" since %s; %s\n", s2, s1);
425-
else if (s2)
426-
printf(" since %s\n", s2);
421+
if (timestamp > 0 && timestamp < USEC_INFINITY)
422+
printf(" since %s; %s\n",
423+
FORMAT_TIMESTAMP_STYLE(timestamp, arg_timestamp_style),
424+
FORMAT_TIMESTAMP_RELATIVE(timestamp));
427425
else
428426
printf("\n");
429427

@@ -440,18 +438,18 @@ static void print_status_info(
440438
}
441439

442440
if (endswith(i->id, ".timer")) {
443-
const char *next_time;
444441
dual_timestamp nw, next = {i->next_elapse_real, i->next_elapse_monotonic};
445442
usec_t next_elapse;
446443

447444
dual_timestamp_get(&nw);
448445
next_elapse = calc_next_elapse(&nw, &next);
449-
next_time = FORMAT_TIMESTAMP_STYLE(next_elapse, arg_timestamp_style);
450446

451-
printf(" Trigger: %s%s%s\n",
452-
strna(next_time),
453-
next_time ? "; " : "",
454-
next_time ? strna(FORMAT_TIMESTAMP_RELATIVE(next_elapse)) : "");
447+
if (next_elapse > 0 && next_elapse < USEC_INFINITY)
448+
printf(" Trigger: %s; %s\n",
449+
FORMAT_TIMESTAMP_STYLE(next_elapse, arg_timestamp_style),
450+
FORMAT_TIMESTAMP_RELATIVE(next_elapse));
451+
else
452+
printf(" Trigger: n/a\n");
455453
}
456454

457455
STRV_FOREACH(t, i->triggers) {
@@ -469,13 +467,11 @@ static void print_status_info(
469467
if (!i->condition_result && i->condition_timestamp > 0) {
470468
UnitCondition *c;
471469
int n = 0;
472-
const char *rel;
473470

474-
rel = FORMAT_TIMESTAMP_RELATIVE(i->condition_timestamp);
475-
printf(" Condition: start %scondition failed%s at %s%s%s\n",
471+
printf(" Condition: start %scondition failed%s at %s; %s\n",
476472
ansi_highlight_yellow(), ansi_normal(),
477473
FORMAT_TIMESTAMP_STYLE(i->condition_timestamp, arg_timestamp_style),
478-
rel ? "; " : "", strempty(rel));
474+
FORMAT_TIMESTAMP_RELATIVE(i->condition_timestamp));
479475

480476
LIST_FOREACH(conditions, c, i->conditions)
481477
if (c->tristate < 0)
@@ -492,13 +488,10 @@ static void print_status_info(
492488
}
493489

494490
if (!i->assert_result && i->assert_timestamp > 0) {
495-
const char *rel;
496-
497-
rel = FORMAT_TIMESTAMP_RELATIVE(i->assert_timestamp);
498-
printf(" Assert: start %sassertion failed%s at %s%s%s\n",
491+
printf(" Assert: start %sassertion failed%s at %s; %s\n",
499492
ansi_highlight_red(), ansi_normal(),
500493
FORMAT_TIMESTAMP_STYLE(i->assert_timestamp, arg_timestamp_style),
501-
rel ? "; " : "", strempty(rel));
494+
FORMAT_TIMESTAMP_RELATIVE(i->assert_timestamp));
502495
if (i->failed_assert_trigger)
503496
printf(" none of the trigger assertions were met\n");
504497
else if (i->failed_assert)

src/test/test-time-util.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ static void test_FORMAT_TIMESTAMP(void) {
384384
assert_se(parse_timestamp(buf, &y) >= 0);
385385
assert_se(x / USEC_PER_SEC == y / USEC_PER_SEC);
386386

387-
char *t = FORMAT_TIMESTAMP(x);
388-
assert_se(streq(t, buf));
387+
assert_se(streq(FORMAT_TIMESTAMP(x), buf));
389388
}
390389
}
391390

0 commit comments

Comments
 (0)