Skip to content

Commit

Permalink
Merge pull request #32592 from yuwata/journal-timestamp
Browse files Browse the repository at this point in the history
journal: several timestamp cleanups and improvements
  • Loading branch information
bluca committed May 2, 2024
2 parents 57188d1 + affde1d commit afb1c42
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 95 deletions.
1 change: 1 addition & 0 deletions src/basic/time-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ typedef enum TimestampStyle {
#define TIME_T_MAX (time_t)((UINTMAX_C(1) << ((sizeof(time_t) << 3) - 1)) - 1)

#define DUAL_TIMESTAMP_NULL ((dual_timestamp) {})
#define DUAL_TIMESTAMP_INFINITY ((dual_timestamp) { USEC_INFINITY, USEC_INFINITY })
#define TRIPLE_TIMESTAMP_NULL ((triple_timestamp) {})

usec_t now(clockid_t clock);
Expand Down
19 changes: 8 additions & 11 deletions src/journal/journald-audit.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,10 +335,9 @@ void process_audit_string(Server *s, int type, const char *data, size_t size) {
size_t n = 0, z;
uint64_t seconds, msec, id;
const char *p, *type_name;
char id_field[sizeof("_AUDIT_ID=") + DECIMAL_STR_MAX(uint64_t)],
type_field[sizeof("_AUDIT_TYPE=") + DECIMAL_STR_MAX(int)],
source_time_field[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)];
struct iovec iovec[N_IOVEC_META_FIELDS + 8 + N_IOVEC_AUDIT_FIELDS];
char id_field[STRLEN("_AUDIT_ID=") + DECIMAL_STR_MAX(uint64_t)],
type_field[STRLEN("_AUDIT_TYPE=") + DECIMAL_STR_MAX(int)];
struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_AUDIT_FIELDS];
char *m, *type_field_name;
int k;

Expand Down Expand Up @@ -375,14 +374,10 @@ void process_audit_string(Server *s, int type, const char *data, size_t size) {

iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=audit");

sprintf(source_time_field, "_SOURCE_REALTIME_TIMESTAMP=%" PRIu64,
(usec_t) seconds * USEC_PER_SEC + (usec_t) msec * USEC_PER_MSEC);
iovec[n++] = IOVEC_MAKE_STRING(source_time_field);

sprintf(type_field, "_AUDIT_TYPE=%i", type);
xsprintf(type_field, "_AUDIT_TYPE=%i", type);
iovec[n++] = IOVEC_MAKE_STRING(type_field);

sprintf(id_field, "_AUDIT_ID=%" PRIu64, id);
xsprintf(id_field, "_AUDIT_ID=%" PRIu64, id);
iovec[n++] = IOVEC_MAKE_STRING(id_field);

assert_cc(4 == LOG_FAC(LOG_AUTH));
Expand All @@ -401,7 +396,9 @@ void process_audit_string(Server *s, int type, const char *data, size_t size) {

map_all_fields(p, map_fields_kernel, "_AUDIT_FIELD_", true, iovec, &n, n + N_IOVEC_AUDIT_FIELDS);

server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, LOG_NOTICE, 0);
server_dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL,
TIMEVAL_STORE((usec_t) seconds * USEC_PER_SEC + (usec_t) msec * USEC_PER_MSEC),
LOG_NOTICE, 0);

/* free() all entries that map_all_fields() added. All others
* are allocated on the stack or are constant. */
Expand Down
17 changes: 10 additions & 7 deletions src/journal/journald-kmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static bool is_us(const char *identifier, const char *pid) {

void dev_kmsg_record(Server *s, char *p, size_t l) {

_cleanup_free_ char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL, *identifier = NULL, *pid = NULL;
_cleanup_free_ char *message = NULL, *syslog_pid = NULL, *syslog_identifier = NULL, *identifier = NULL, *pid = NULL;
struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS];
char *kernel_device = NULL;
unsigned long long usec;
Expand Down Expand Up @@ -253,16 +253,19 @@ void dev_kmsg_record(Server *s, char *p, size_t l) {
}
}

if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec) >= 0)
iovec[n++] = IOVEC_MAKE_STRING(source_time);
char source_time[STRLEN("_SOURCE_MONOTONIC_TIMESTAMP=") + DECIMAL_STR_MAX(unsigned long long)];
xsprintf(source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu", usec);
iovec[n++] = IOVEC_MAKE_STRING(source_time);

iovec[n++] = IOVEC_MAKE_STRING("_TRANSPORT=kernel");

if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
iovec[n++] = IOVEC_MAKE_STRING(syslog_priority);
char syslog_priority[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int)];
xsprintf(syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK);
iovec[n++] = IOVEC_MAKE_STRING(syslog_priority);

if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
iovec[n++] = IOVEC_MAKE_STRING(syslog_facility);
char syslog_facility[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
xsprintf(syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority));
iovec[n++] = IOVEC_MAKE_STRING(syslog_facility);

if (LOG_FAC(priority) == LOG_KERN)
iovec[n++] = IOVEC_MAKE_STRING("SYSLOG_IDENTIFIER=kernel");
Expand Down
4 changes: 2 additions & 2 deletions src/journal/journald-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,7 @@ static void server_dispatch_message_real(
int priority,
pid_t object_pid) {

char source_time[sizeof("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)];
char source_time[STRLEN("_SOURCE_REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t)];
_unused_ _cleanup_free_ char *cmdline1 = NULL, *cmdline2 = NULL;
uid_t journal_uid;
ClientContext *o;
Expand Down Expand Up @@ -1153,7 +1153,7 @@ static void server_dispatch_message_real(
assert(n <= m);

if (tv) {
sprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=" USEC_FMT, timeval_load(tv));
xsprintf(source_time, "_SOURCE_REALTIME_TIMESTAMP=" USEC_FMT, timeval_load(tv));
iovec[n++] = IOVEC_MAKE_STRING(source_time);
}

Expand Down
4 changes: 2 additions & 2 deletions src/journal/journald-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ struct Server {
bool sent_notify_ready:1;
bool sync_scheduled:1;

char machine_id_field[sizeof("_MACHINE_ID=") + 32];
char boot_id_field[sizeof("_BOOT_ID=") + 32];
char machine_id_field[STRLEN("_MACHINE_ID=") + SD_ID128_STRING_MAX];
char boot_id_field[STRLEN("_BOOT_ID=") + SD_ID128_STRING_MAX];
char *hostname_field;
char *namespace_field;
char *runtime_directory;
Expand Down
2 changes: 1 addition & 1 deletion src/journal/journald-socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int server_forward_socket(

/* Synthesise __REALTIME_TIMESTAMP as the last argument so systemd-journal-upload can receive these
* export messages. */
char buf[sizeof("__REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t) + 2];
char buf[STRLEN("__REALTIME_TIMESTAMP=") + DECIMAL_STR_MAX(usec_t) + 2];
xsprintf(buf, "__REALTIME_TIMESTAMP="USEC_FMT"\n\n", now(CLOCK_REALTIME));
iov[iov_idx++] = IOVEC_MAKE_STRING(buf);

Expand Down
4 changes: 2 additions & 2 deletions src/journal/journald-syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ void server_process_syslog_message(
const char *label,
size_t label_len) {

char *t, syslog_priority[sizeof("PRIORITY=") + DECIMAL_STR_MAX(int)],
syslog_facility[sizeof("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
char *t, syslog_priority[STRLEN("PRIORITY=") + DECIMAL_STR_MAX(int)],
syslog_facility[STRLEN("SYSLOG_FACILITY=") + DECIMAL_STR_MAX(int)];
const char *msg, *syslog_ts, *a;
_cleanup_free_ char *identifier = NULL, *pid = NULL,
*dummy = NULL, *msg_msg = NULL, *msg_raw = NULL;
Expand Down
6 changes: 3 additions & 3 deletions src/libsystemd/sd-journal/sd-journal.c
Original file line number Diff line number Diff line change
Expand Up @@ -2656,9 +2656,7 @@ _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id12
if (r < 0)
return r;

if (ret_boot_id)
*ret_boot_id = o->entry.boot_id;
else {
if (!ret_boot_id) {
sd_id128_t id;

r = sd_id128_get_boot(&id);
Expand All @@ -2675,6 +2673,8 @@ _public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id12

if (ret)
*ret = t;
if (ret_boot_id)
*ret_boot_id = o->entry.boot_id;

return 0;
}
Expand Down

0 comments on commit afb1c42

Please sign in to comment.