X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fjournal%2Fjournal-file.c;h=bc72fca72561fadad7e5d5fb9d5fdb2821189dcc;hb=26fefda707e6511733f48da03b281a004dba6abf;hp=ca217f93fef962b7067ea71ef3d85d3adc840125;hpb=ed375bebf46c1251f4baa170b39ee93761dbdb19;p=elogind.git diff --git a/src/journal/journal-file.c b/src/journal/journal-file.c index ca217f93f..bc72fca72 100644 --- a/src/journal/journal-file.c +++ b/src/journal/journal-file.c @@ -68,7 +68,7 @@ /* How many entries to keep in the entry array chain cache at max */ #define CHAIN_CACHE_MAX 20 -int journal_file_set_online(JournalFile *f) { +static int journal_file_set_online(JournalFile *f) { assert(f); if (!f->writable) @@ -907,6 +907,8 @@ static int journal_file_append_field( osize = offsetof(Object, field.payload) + size; r = journal_file_append_object(f, OBJECT_FIELD, osize, &o, &p); + if (r < 0) + return r; o->field.hash = htole64(hash); memcpy(o->field.payload, field, size); @@ -1342,7 +1344,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st /* Order by the position on disk, in order to improve seek * times for rotating media. */ - qsort(items, n_iovec, sizeof(EntryItem), entry_item_cmp); + qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp); r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset); @@ -2270,6 +2272,15 @@ fail: log_error("File corrupt"); } +static const char* format_timestamp_safe(char *buf, size_t l, usec_t t) { + const char *x; + + x = format_timestamp(buf, l, t); + if (x) + return x; + return " --- "; +} + void journal_file_print_header(JournalFile *f) { char a[33], b[33], c[33], d[33]; char x[FORMAT_TIMESTAMP_MAX], y[FORMAT_TIMESTAMP_MAX], z[FORMAT_TIMESTAMP_MAX]; @@ -2317,8 +2328,8 @@ void journal_file_print_header(JournalFile *f) { yes_no(journal_file_rotate_suggested(f, 0)), le64toh(f->header->head_entry_seqnum), le64toh(f->header->tail_entry_seqnum), - format_timestamp(x, sizeof(x), le64toh(f->header->head_entry_realtime)), - format_timestamp(y, sizeof(y), le64toh(f->header->tail_entry_realtime)), + format_timestamp_safe(x, sizeof(x), le64toh(f->header->head_entry_realtime)), + format_timestamp_safe(y, sizeof(y), le64toh(f->header->tail_entry_realtime)), format_timespan(z, sizeof(z), le64toh(f->header->tail_entry_monotonic), USEC_PER_MSEC), le64toh(f->header->n_objects), le64toh(f->header->n_entries)); @@ -2540,7 +2551,7 @@ fail: } int journal_file_rotate(JournalFile **f, bool compress, bool seal) { - char *p; + _cleanup_free_ char *p = NULL; size_t l; JournalFile *old_file, *new_file = NULL; int r; @@ -2557,22 +2568,15 @@ int journal_file_rotate(JournalFile **f, bool compress, bool seal) { return -EINVAL; l = strlen(old_file->path); - - p = new(char, l + 1 + 32 + 1 + 16 + 1 + 16 + 1); - if (!p) + r = asprintf(&p, "%.*s@" SD_ID128_FORMAT_STR "-%016"PRIx64"-%016"PRIx64".journal", + (int) l - 8, old_file->path, + SD_ID128_FORMAT_VAL(old_file->header->seqnum_id), + le64toh((*f)->header->head_entry_seqnum), + le64toh((*f)->header->head_entry_realtime)); + if (r < 0) return -ENOMEM; - memcpy(p, old_file->path, l - 8); - p[l-8] = '@'; - sd_id128_to_string(old_file->header->seqnum_id, p + l - 8 + 1); - snprintf(p + l - 8 + 1 + 32, 1 + 16 + 1 + 16 + 8 + 1, - "-%016"PRIx64"-%016"PRIx64".journal", - le64toh((*f)->header->head_entry_seqnum), - le64toh((*f)->header->head_entry_realtime)); - r = rename(old_file->path, p); - free(p); - if (r < 0) return -errno; @@ -2623,7 +2627,7 @@ int journal_file_open_reliably( l = strlen(fname); if (asprintf(&p, "%.*s@%016llx-%016llx.journal~", - (int) (l-8), fname, + (int) l - 8, fname, (unsigned long long) now(CLOCK_REALTIME), random_ull()) < 0) return -ENOMEM;