HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-journal/sd-journal.c
Git blob: 3208ce9733f473c236071d28f6f34639076b6b14
Lines 3001–3837 of 3,837
return 1;
}
return 0;
}
_public_ int sd_journal_enumerate_available_data(sd_journal *j, const void **ret_data, size_t *ret_size) {
for (;;) {
int r;
r = sd_journal_enumerate_data(j, ret_data, ret_size);
if (r >= 0)
return r;
if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
return r;
j->current_field++; /* Try with the next field */
}
}
_public_ void sd_journal_restart_data(sd_journal *j) {
if (!j || journal_origin_changed(j))
return;
j->current_field = 0;
}
static int reiterate_all_paths(sd_journal *j) {
assert(j);
if (j->no_new_files)
return add_current_paths(j);
if (j->flags & SD_JOURNAL_OS_ROOT)
return add_search_paths(j);
if (j->toplevel_fd >= 0)
return add_root_directory(j, NULL, false);
if (j->path)
return add_root_directory(j, j->path, true);
return add_search_paths(j);
}
_public_ int sd_journal_get_fd(sd_journal *j) {
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!FLAGS_SET(j->flags, SD_JOURNAL_ASSUME_IMMUTABLE), -EUNATCH);
if (j->no_inotify)
return -EMEDIUMTYPE;
if (j->inotify_fd >= 0)
return j->inotify_fd;
r = allocate_inotify(j);
if (r < 0)
return r;
log_debug("Reiterating files to get inotify watches established.");
/* Iterate through all dirs again, to add them to the inotify */
r = reiterate_all_paths(j);
if (r < 0)
return r;
return j->inotify_fd;
}
_public_ int sd_journal_get_events(sd_journal *j) {
int fd;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!FLAGS_SET(j->flags, SD_JOURNAL_ASSUME_IMMUTABLE), -EUNATCH);
fd = sd_journal_get_fd(j);
if (fd < 0)
return fd;
return POLLIN;
}
_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
int fd;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!FLAGS_SET(j->flags, SD_JOURNAL_ASSUME_IMMUTABLE), -EUNATCH);
assert_return(timeout_usec, -EINVAL);
fd = sd_journal_get_fd(j);
if (fd < 0)
return fd;
if (!j->on_network) {
*timeout_usec = UINT64_MAX;
return 0;
}
/* If we are on the network we need to regularly check for
* changes manually */
*timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
return 1;
}
static void process_q_overflow(sd_journal *j) {
JournalFile *f;
Directory *m;
assert(j);
/* When the inotify queue overruns we need to enumerate and re-validate all journal files to bring our list
* back in sync with what's on disk. For this we pick a new generation counter value. It'll be assigned to all
* journal files we encounter. All journal files and all directories that don't carry it after reenumeration
* are subject for unloading. */
log_debug("Inotify queue overrun, reiterating everything.");
j->generation++;
(void) reiterate_all_paths(j);
ORDERED_HASHMAP_FOREACH(f, j->files) {
if (f->last_seen_generation == j->generation)
continue;
log_debug("File '%s' hasn't been seen in this enumeration, removing.", f->path);
remove_file_real(j, f);
}
HASHMAP_FOREACH(m, j->directories_by_path) {
if (m->last_seen_generation == j->generation)
continue;
if (m->is_root) /* Never GC root directories */
continue;
log_debug("Directory '%s' hasn't been seen in this enumeration, removing.", m->path);
directory_free(m);
}
log_debug("Reiteration complete.");
}
static void process_inotify_event(sd_journal *j, const struct inotify_event *e) {
Directory *d;
assert(j);
assert(e);
if (e->mask & IN_Q_OVERFLOW) {
process_q_overflow(j);
return;
}
/* Is this a subdirectory we watch? */
d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
if (d) {
if (!(e->mask & IN_ISDIR) && e->len > 0 &&
(endswith(e->name, ".journal") ||
endswith(e->name, ".journal~"))) {
/* Event for a journal file */
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
(void) add_file_by_name(j, d->path, e->name);
else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT))
(void) remove_file_by_name(j, d->path, e->name);
} else if (!d->is_root && e->len == 0) {
/* Event for a subdirectory */
if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
directory_free(d);
} else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && id128_is_valid(e->name)) {
/* Event for root directory */
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB))
(void) add_directory(j, d->path, e->name);
}
return;
}
if (e->mask & IN_IGNORED)
return;
log_debug("Unexpected inotify event.");
}
static int determine_change(sd_journal *j) {
bool b;
assert(j);
b = j->current_invalidate_counter != j->last_invalidate_counter;
j->last_invalidate_counter = j->current_invalidate_counter;
return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
}
_public_ int sd_journal_process(sd_journal *j) {
bool got_something = false;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
if (j->inotify_fd < 0) /* We have no inotify fd yet? Then there's noting to process. */
return 0;
assert_return(!FLAGS_SET(j->flags, SD_JOURNAL_ASSUME_IMMUTABLE), -EUNATCH);
j->last_process_usec = now(CLOCK_MONOTONIC);
j->last_invalidate_counter = j->current_invalidate_counter;
for (;;) {
union inotify_event_buffer buffer;
ssize_t l;
l = read(j->inotify_fd, &buffer, sizeof(buffer));
if (l < 0) {
if (ERRNO_IS_TRANSIENT(errno))
return got_something ? determine_change(j) : SD_JOURNAL_NOP;
return -errno;
}
got_something = true;
FOREACH_INOTIFY_EVENT(e, buffer, l)
process_inotify_event(j, e);
}
}
_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
int r;
uint64_t t;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(!FLAGS_SET(j->flags, SD_JOURNAL_ASSUME_IMMUTABLE), -EUNATCH);
if (j->inotify_fd < 0) {
JournalFile *f;
/* This is the first invocation, hence create the inotify watch */
r = sd_journal_get_fd(j);
if (r < 0)
return r;
/* journald might have done some vacuuming while we weren't watching. Get rid of the deleted
* files now so they don't stay around indefinitely. */
ORDERED_HASHMAP_FOREACH(f, j->files) {
r = journal_file_fstat(f);
if (r == -EIDRM)
remove_file_real(j, f);
else if (r < 0)
log_debug_errno(r, "Failed to fstat() journal file '%s', ignoring: %m", f->path);
}
/* The journal might have changed since the context object was created and we weren't
* watching before, hence don't wait for anything, and return immediately. */
return determine_change(j);
}
r = sd_journal_get_timeout(j, &t);
if (r < 0)
return r;
if (t != UINT64_MAX) {
t = usec_sub_unsigned(t, now(CLOCK_MONOTONIC));
if (timeout_usec == UINT64_MAX || timeout_usec > t)
timeout_usec = t;
}
do {
r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
} while (r == -EINTR);
if (r < 0)
return r;
return sd_journal_process(j);
}
_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
JournalFile *f;
bool first = true;
uint64_t fmin = 0, tmax = 0;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(from || to, -EINVAL);
assert_return(from != to, -EINVAL);
ORDERED_HASHMAP_FOREACH(f, j->files) {
usec_t fr, t;
r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
if (r == -ENOENT)
continue;
if (r < 0)
return r;
if (r == 0)
continue;
if (first) {
fmin = fr;
tmax = t;
first = false;
} else {
fmin = MIN(fr, fmin);
tmax = MAX(t, tmax);
}
}
if (from)
*from = fmin;
if (to)
*to = tmax;
return first ? 0 : 1;
}
_public_ int sd_journal_get_cutoff_monotonic_usec(
sd_journal *j,
sd_id128_t boot_id,
uint64_t *ret_from,
uint64_t *ret_to) {
uint64_t from = UINT64_MAX, to = UINT64_MAX;
bool found = false;
JournalFile *f;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret_from != ret_to, -EINVAL);
ORDERED_HASHMAP_FOREACH(f, j->files) {
usec_t ff, tt;
r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &ff, &tt);
if (r == -ENOENT)
continue;
if (r < 0)
return r;
if (r == 0)
continue;
if (found) {
from = MIN(ff, from);
to = MAX(tt, to);
} else {
from = ff;
to = tt;
found = true;
}
}
if (ret_from)
*ret_from = from;
if (ret_to)
*ret_to = to;
return found;
}
void journal_print_header(sd_journal *j) {
JournalFile *f;
bool newline = false;
assert(j);
ORDERED_HASHMAP_FOREACH(f, j->files) {
if (newline)
putchar('\n');
else
newline = true;
journal_file_print_header(f);
}
}
_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *ret_bytes) {
JournalFile *f;
uint64_t sum = 0;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret_bytes, -EINVAL);
ORDERED_HASHMAP_FOREACH(f, j->files) {
struct stat st;
uint64_t b;
if (fstat(f->fd, &st) < 0)
return -errno;
b = (uint64_t) st.st_blocks;
if (b > UINT64_MAX / 512)
return -EOVERFLOW;
b *= 512;
if (sum > UINT64_MAX - b)
return -EOVERFLOW;
sum += b;
}
*ret_bytes = sum;
return 0;
}
_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
if (!field_is_valid(field))
return -EINVAL;
r = free_and_strdup(&j->unique_field, field);
if (r < 0)
return r;
j->unique_file = NULL;
j->unique_offset = 0;
j->unique_file_lost = false;
return 0;
}
_public_ int sd_journal_enumerate_unique(
sd_journal *j,
const void **ret_data,
size_t *ret_size) {
size_t k;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(j->unique_field, -EINVAL);
assert_return(ret_data, -EINVAL);
assert_return(ret_size, -EINVAL);
k = strlen(j->unique_field);
if (!j->unique_file) {
if (j->unique_file_lost)
return 0;
j->unique_file = ordered_hashmap_first(j->files);
if (!j->unique_file)
return 0;
j->unique_offset = 0;
}
for (;;) {
JournalFile *of;
Object *o;
const void *odata;
size_t ol;
bool found;
int r;
/* Proceed to next data object in the field's linked list */
if (j->unique_offset == 0) {
r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
if (r < 0)
return r;
j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
} else {
r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
if (r < 0)
return r;
j->unique_offset = le64toh(o->data.next_field_offset);
}
/* We reached the end of the list? Then start again, with the next file */
if (j->unique_offset == 0) {
j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
if (!j->unique_file)
return 0;
continue;
}
r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
if (r < 0)
return r;
/* Let's pin the data object, so we can look at it at the same time as one on another file. */
r = journal_file_pin_object(j->unique_file, o);
if (r < 0)
return r;
r = journal_file_data_payload(j->unique_file, o, j->unique_offset, NULL, 0,
j->data_threshold, &odata, &ol);
if (r < 0)
return r;
/* Check if we have at least the field name and "=". */
if (ol <= k)
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
j->unique_file->path,
j->unique_offset, ol, k + 1);
if (memcmp(odata, j->unique_field, k) != 0 || ((const char*) odata)[k] != '=')
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"%s:offset " OFSfmt ": object does not start with \"%s=\"",
j->unique_file->path,
j->unique_offset,
j->unique_field);
/* OK, now let's see if we already returned this data object by checking if it exists in the
* earlier traversed files. */
found = false;
ORDERED_HASHMAP_FOREACH(of, j->files) {
if (of == j->unique_file)
break;
/* Skip this file it didn't have any fields indexed */
if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
continue;
/* We can reuse the hash from our current file only on old-style journal files
* without keyed hashes. On new-style files we have to calculate the hash anew, to
* take the per-file hash seed into consideration. */
if (!JOURNAL_HEADER_KEYED_HASH(j->unique_file->header) && !JOURNAL_HEADER_KEYED_HASH(of->header))
r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), NULL, NULL);
else
r = journal_file_find_data_object(of, odata, ol, NULL, NULL);
if (r < 0)
return r;
if (r > 0) {
found = true;
break;
}
}
if (found)
continue;
*ret_data = odata;
*ret_size = ol;
return 1;
}
}
_public_ int sd_journal_enumerate_available_unique(sd_journal *j, const void **ret_data, size_t *ret_size) {
for (;;) {
int r;
r = sd_journal_enumerate_unique(j, ret_data, ret_size);
if (r >= 0)
return r;
if (!JOURNAL_ERRNO_IS_UNAVAILABLE_FIELD(r))
return r;
/* Try with the next field. sd_journal_enumerate_unique() modifies state, so on the next try
* we will access the next field. */
}
}
_public_ void sd_journal_restart_unique(sd_journal *j) {
if (!j || journal_origin_changed(j))
return;
j->unique_file = NULL;
j->unique_offset = 0;
j->unique_file_lost = false;
}
_public_ int sd_journal_enumerate_fields(sd_journal *j, const char **ret) {
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret, -EINVAL);
if (!j->fields_file) {
if (j->fields_file_lost)
return 0;
j->fields_file = ordered_hashmap_first(j->files);
if (!j->fields_file)
return 0;
j->fields_hash_table_index = 0;
j->fields_offset = 0;
}
for (;;) {
JournalFile *f, *of;
uint64_t m;
Object *o;
size_t sz;
bool found;
f = j->fields_file;
if (j->fields_offset == 0) {
bool eof = false;
/* We are not yet positioned at any field. Let's pick the first one */
r = journal_file_map_field_hash_table(f);
if (r < 0)
return r;
m = le64toh(f->header->field_hash_table_size) / sizeof(HashItem);
for (;;) {
if (j->fields_hash_table_index >= m) {
/* Reached the end of the hash table, go to the next file. */
eof = true;
break;
}
j->fields_offset = le64toh(f->field_hash_table[j->fields_hash_table_index].head_hash_offset);
if (j->fields_offset != 0)
break;
/* Empty hash table bucket, go to next one */
j->fields_hash_table_index++;
}
if (eof) {
/* Proceed with next file */
j->fields_file = ordered_hashmap_next(j->files, f->path);
if (!j->fields_file) {
*ret = NULL;
return 0;
}
j->fields_offset = 0;
j->fields_hash_table_index = 0;
continue;
}
} else {
/* We are already positioned at a field. If so, let's figure out the next field from it */
r = journal_file_move_to_object(f, OBJECT_FIELD, j->fields_offset, &o);
if (r < 0)
return r;
j->fields_offset = le64toh(o->field.next_hash_offset);
if (j->fields_offset == 0) {
/* Reached the end of the hash table chain */
j->fields_hash_table_index++;
continue;
}
}
/* We use OBJECT_UNUSED here, so that the iterator below doesn't remove our mmap window */
r = journal_file_move_to_object(f, OBJECT_UNUSED, j->fields_offset, &o);
if (r < 0)
return r;
/* Because we used OBJECT_UNUSED above, we need to do our type check manually */
if (o->object.type != OBJECT_FIELD)
return log_debug_errno(SYNTHETIC_ERRNO(EBADMSG),
"%s:offset " OFSfmt ": object has type %i, expected %i",
f->path, j->fields_offset,
o->object.type, OBJECT_FIELD);
sz = le64toh(o->object.size) - offsetof(Object, field.payload);
/* Let's see if we already returned this field name before. */
found = false;
ORDERED_HASHMAP_FOREACH(of, j->files) {
if (of == f)
break;
/* Skip this file it didn't have any fields indexed */
if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) && le64toh(of->header->n_fields) <= 0)
continue;
if (!JOURNAL_HEADER_KEYED_HASH(f->header) && !JOURNAL_HEADER_KEYED_HASH(of->header))
r = journal_file_find_field_object_with_hash(of, o->field.payload, sz,
le64toh(o->field.hash), NULL, NULL);
else
r = journal_file_find_field_object(of, o->field.payload, sz, NULL, NULL);
if (r < 0)
return r;
if (r > 0) {
found = true;
break;
}
}
if (found)
continue;
/* Check if this is really a valid string containing no NUL byte */
if (memchr(o->field.payload, 0, sz))
return -EBADMSG;
if (j->data_threshold > 0 && sz > j->data_threshold)
sz = j->data_threshold;
if (!GREEDY_REALLOC(j->fields_buffer, sz + 1))
return -ENOMEM;
memcpy(j->fields_buffer, o->field.payload, sz);
j->fields_buffer[sz] = 0;
if (!field_is_valid(j->fields_buffer))
return -EBADMSG;
*ret = j->fields_buffer;
return 1;
}
}
_public_ void sd_journal_restart_fields(sd_journal *j) {
if (!j || journal_origin_changed(j))
return;
j->fields_file = NULL;
j->fields_hash_table_index = 0;
j->fields_offset = 0;
j->fields_file_lost = false;
}
_public_ int sd_journal_reliable_fd(sd_journal *j) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
return !j->on_network;
}
static char *lookup_field(const char *field, void *userdata) {
sd_journal *j = ASSERT_PTR(userdata);
const void *data;
size_t size, d;
int r;
assert(field);
r = sd_journal_get_data(j, field, &data, &size);
if (r < 0 ||
size > REPLACE_VAR_MAX)
return strdup(field);
d = strlen(field) + 1;
return strndup((const char*) data + d, size - d);
}
_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
const void *data;
size_t size;
sd_id128_t id;
_cleanup_free_ char *text = NULL, *cid = NULL;
char *t;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret, -EINVAL);
r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
if (r < 0)
return r;
cid = strndup((const char*) data + 11, size - 11);
if (!cid)
return -ENOMEM;
r = sd_id128_from_string(cid, &id);
if (r < 0)
return r;
r = catalog_get(secure_getenv("SYSTEMD_CATALOG") ?: CATALOG_DATABASE, id, &text);
if (r < 0)
return r;
t = replace_var(text, lookup_field, j);
if (!t)
return -ENOMEM;
*ret = t;
return 0;
}
_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
assert_return(ret, -EINVAL);
return catalog_get(CATALOG_DATABASE, id, ret);
}
_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
j->data_threshold = sz;
return 0;
}
_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(sz, -EINVAL);
*sz = j->data_threshold;
return 0;
}
_public_ int sd_journal_has_runtime_files(sd_journal *j) {
assert_return(j, -EINVAL);
return j->has_runtime_files;
}
_public_ int sd_journal_has_persistent_files(sd_journal *j) {
assert_return(j, -EINVAL);
return j->has_persistent_files;
}