+static int list_boots(sd_journal *j) {
+ int r;
+ const void *data;
+ unsigned int count = 0;
+ int w, i;
+ size_t length, allocated = 0;
+ boot_id_t *id;
+ _cleanup_free_ boot_id_t *all_ids = NULL;
+
+ r = sd_journal_query_unique(j, "_BOOT_ID");
+ if (r < 0)
+ return r;
+
+ SD_JOURNAL_FOREACH_UNIQUE(j, data, length) {
+ if (length < strlen("_BOOT_ID="))
+ continue;
+
+ if (!GREEDY_REALLOC(all_ids, allocated, count + 1))
+ return log_oom();
+
+ id = &all_ids[count];
+
+ r = sd_id128_from_string(((const char *)data) + strlen("_BOOT_ID="), &id->id);
+ if (r < 0)
+ continue;
+
+ r = sd_journal_add_match(j, data, length);
+ if (r < 0)
+ return r;
+
+ r = sd_journal_seek_head(j);
+ if (r < 0)
+ return r;
+
+ r = sd_journal_next(j);
+ if (r < 0)
+ return r;
+ else if (r == 0)
+ goto flush;
+
+ r = sd_journal_get_realtime_usec(j, &id->first);
+ if (r < 0)
+ return r;
+
+ r = sd_journal_seek_tail(j);
+ if (r < 0)
+ return r;
+
+ r = sd_journal_previous(j);
+ if (r < 0)
+ return r;
+ else if (r == 0)
+ goto flush;
+
+ r = sd_journal_get_realtime_usec(j, &id->last);
+ if (r < 0)
+ return r;
+
+ count++;
+ flush:
+ sd_journal_flush_matches(j);
+ }
+
+ qsort_safe(all_ids, count, sizeof(boot_id_t), boot_id_cmp);
+
+ /* numbers are one less, but we need an extra char for the sign */
+ w = DECIMAL_STR_WIDTH(count - 1) + 1;
+
+ for (id = all_ids, i = 0; id < all_ids + count; id++, i++) {
+ char a[FORMAT_TIMESTAMP_MAX], b[FORMAT_TIMESTAMP_MAX];
+
+ printf("% *i " SD_ID128_FORMAT_STR " %s—%s\n",
+ w, i - count + 1,
+ SD_ID128_FORMAT_VAL(id->id),
+ format_timestamp(a, sizeof(a), id->first),
+ format_timestamp(b, sizeof(b), id->last));
+ }
+
+ return 0;
+}
+