HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-journal/sd-journal.c
Git blob: 3208ce9733f473c236071d28f6f34639076b6b14
Lines 2001–3000 of 3,837
Directory,
directory_free);
static int add_directory_impl(sd_journal *j, const char *path, bool is_root, Directory **ret) {
_cleanup_(directory_freep) Directory *m = NULL;
Directory *existing;
int r;
assert(j);
assert(path);
assert(ret);
existing = hashmap_get(j->directories_by_path, path);
if (existing) {
if (existing->is_root != is_root) {
/* Don't 'downgrade' from root directory */
*ret = NULL;
return 0;
}
*ret = existing;
return 1;
}
m = new(Directory, 1);
if (!m)
return -ENOMEM;
*m = (Directory) {
.journal = j,
.is_root = is_root,
.path = strdup(path),
.wd = -1,
};
if (!m->path)
return -ENOMEM;
r = hashmap_ensure_put(&j->directories_by_path, &directories_by_path_hash_ops, m->path, m);
if (r < 0)
return r;
j->current_invalidate_counter++;
if (is_root)
log_debug("Root directory %s added.", m->path);
else
log_debug("Directory %s added.", m->path);
*ret = TAKE_PTR(m);
return 1;
}
static int add_directory(sd_journal *j, const char *prefix, const char *dirname);
static void directory_enumerate(sd_journal *j, Directory *m, DIR *d) {
assert(j);
assert(m);
assert(d);
FOREACH_DIRENT_ALL(de, d, goto fail) {
if (dirent_is_journal_file(de))
(void) add_file_by_name(j, m->path, de->d_name);
if (m->is_root && dirent_is_journal_subdir(de))
(void) add_directory(j, m->path, de->d_name);
}
return;
fail:
log_debug_errno(errno, "Failed to enumerate directory %s, ignoring: %m", m->path);
}
static void directory_watch(sd_journal *j, Directory *m, int fd, uint32_t mask) {
int r;
assert(j);
assert(m);
assert(fd >= 0);
/* Watch this directory if that's enabled and if it not being watched yet. */
if (m->wd > 0) /* Already have a watch? */
return;
if (j->inotify_fd < 0) /* Not watching at all? */
return;
m->wd = inotify_add_watch_fd(j->inotify_fd, fd, mask);
if (m->wd < 0) {
log_debug_errno(m->wd, "Failed to watch journal directory '%s', ignoring: %m", m->path);
return;
}
r = hashmap_ensure_put(&j->directories_by_wd, &directories_by_wd_hash_ops, INT_TO_PTR(m->wd), m);
if (r < 0) {
if (r == -EEXIST)
log_debug_errno(r, "Directory '%s' already being watched under a different path, ignoring: %m", m->path);
else {
log_debug_errno(r, "Failed to add watch for journal directory '%s' to hashmap, ignoring: %m", m->path);
(void) inotify_rm_watch(j->inotify_fd, m->wd);
}
m->wd = -1;
}
}
static int add_directory(
sd_journal *j,
const char *prefix,
const char *dirname) {
_cleanup_free_ char *path = NULL;
_cleanup_closedir_ DIR *d = NULL;
Directory *m;
int r, k;
assert(j);
assert(prefix);
/* Adds a journal file directory to watch. If the directory is already tracked this updates the inotify watch
* and reenumerates directory contents */
path = path_join(prefix, dirname);
if (!path) {
r = -ENOMEM;
goto fail;
}
log_debug("Considering directory '%s'.", path);
/* We consider everything local that is in a directory for the local machine ID, or that is stored in /run */
if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
!((dirname && dirname_is_machine_id(dirname) > 0) || path_has_prefix(j, path, "/run")))
return 0;
if (dirname &&
(!(FLAGS_SET(j->flags, SD_JOURNAL_ALL_NAMESPACES) ||
dirname_has_namespace(dirname, j->namespace) > 0 ||
(FLAGS_SET(j->flags, SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE) && dirname_has_namespace(dirname, NULL) > 0))))
return 0;
r = directory_open(j, path, &d);
if (r < 0) {
log_debug_errno(r, "Failed to open directory '%s': %m", path);
goto fail;
}
r = add_directory_impl(j, path, /* is_root= */ false, &m);
if (r < 0)
goto fail;
if (r == 0)
return 0;
m->last_seen_generation = j->generation;
directory_watch(j, m, dirfd(d),
IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
IN_ONLYDIR);
if (!j->no_new_files)
directory_enumerate(j, m, d);
check_network(j, dirfd(d));
return 0;
fail:
k = journal_put_error(j, r, path ?: prefix);
if (k < 0)
return k;
return r;
}
static int add_root_directory(sd_journal *j, const char *p, bool missing_ok) {
_cleanup_closedir_ DIR *d = NULL;
Directory *m;
int r, k;
assert(j);
/* Adds a root directory to our set of directories to use. If the root directory is already in the set, we
* update the inotify logic, and renumerate the directory entries. This call may hence be called to initially
* populate the set, as well as to update it later. */
if (p) {
/* If there's a path specified, use it. */
log_debug("Considering root directory '%s'.", p);
if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
!path_has_prefix(j, p, "/run"))
return -EINVAL;
if (j->prefix)
p = strjoina(j->prefix, p);
r = directory_open(j, p, &d);
if (r == -ENOENT && missing_ok)
return 0;
if (r < 0) {
log_debug_errno(r, "Failed to open root directory %s: %m", p);
goto fail;
}
} else {
_cleanup_close_ int dfd = -EBADF;
/* If there's no path specified, then we use the top-level fd itself. We duplicate the fd here, since
* opendir() will take possession of the fd, and close it, which we don't want. */
p = "."; /* store this as "." in the directories hashmap */
dfd = fcntl(j->toplevel_fd, F_DUPFD_CLOEXEC, 3);
if (dfd < 0) {
r = -errno;
goto fail;
}
d = take_fdopendir(&dfd);
if (!d) {
r = -errno;
goto fail;
}
rewinddir(d);
}
r = add_directory_impl(j, p, /* is_root= */ true, &m);
if (r < 0)
goto fail;
if (r == 0)
return 0;
directory_watch(j, m, dirfd(d),
IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
IN_ONLYDIR);
if (!j->no_new_files)
directory_enumerate(j, m, d);
check_network(j, dirfd(d));
return 0;
fail:
k = journal_put_error(j, r, p);
if (k < 0)
return k;
return r;
}
static int add_search_paths(sd_journal *j) {
static const char search_paths[] =
"/run/log/journal\0"
"/var/log/journal\0";
assert(j);
/* We ignore most errors here, since the idea is to only open
* what's actually accessible, and ignore the rest. */
NULSTR_FOREACH(p, search_paths)
(void) add_root_directory(j, p, true);
if (!(j->flags & SD_JOURNAL_LOCAL_ONLY))
(void) add_root_directory(j, "/var/log/journal/remote", true);
return 0;
}
static int add_current_paths(sd_journal *j) {
JournalFile *f;
assert(j);
assert(j->no_new_files);
/* Simply adds all directories for files we have open as directories. We don't expect errors here, so we
* treat them as fatal. */
ORDERED_HASHMAP_FOREACH(f, j->files) {
_cleanup_free_ char *dir = NULL;
int r;
r = path_extract_directory(f->path, &dir);
if (r < 0)
return r;
r = add_directory(j, dir, NULL);
if (r < 0)
return r;
}
return 0;
}
static int allocate_inotify(sd_journal *j) {
assert(j);
if (j->inotify_fd < 0) {
j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
if (j->inotify_fd < 0)
return -errno;
}
return 0;
}
static sd_journal *journal_new(int flags, const char *path, const char *namespace) {
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
j = new(sd_journal, 1);
if (!j)
return NULL;
*j = (sd_journal) {
.origin_id = origin_id_query(),
.toplevel_fd = -EBADF,
.inotify_fd = -EBADF,
.flags = flags,
.data_threshold = DEFAULT_DATA_THRESHOLD,
};
if (path) {
char *t;
t = strdup(path);
if (!t)
return NULL;
if (flags & SD_JOURNAL_OS_ROOT)
j->prefix = t;
else
j->path = t;
}
if (namespace) {
j->namespace = strdup(namespace);
if (!j->namespace)
return NULL;
}
j->files = ordered_hashmap_new(&journal_file_hash_ops_by_path);
if (!j->files)
return NULL;
j->files_cache = ordered_hashmap_iterated_cache_new(j->files);
j->mmap = mmap_cache_new();
if (!j->files_cache || !j->mmap)
return NULL;
return TAKE_PTR(j);
}
#define OPEN_ALLOWED_FLAGS \
(SD_JOURNAL_LOCAL_ONLY | \
SD_JOURNAL_RUNTIME_ONLY | \
SD_JOURNAL_SYSTEM | \
SD_JOURNAL_CURRENT_USER | \
SD_JOURNAL_ALL_NAMESPACES | \
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE | \
SD_JOURNAL_ASSUME_IMMUTABLE)
_public_ int sd_journal_open_namespace(sd_journal **ret, const char *name_space, int flags) {
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
int r;
assert_return(ret, -EINVAL);
assert_return((flags & ~OPEN_ALLOWED_FLAGS) == 0, -EINVAL);
j = journal_new(flags, NULL, name_space);
if (!j)
return -ENOMEM;
r = add_search_paths(j);
if (r < 0)
return r;
*ret = TAKE_PTR(j);
return 0;
}
_public_ int sd_journal_open(sd_journal **ret, int flags) {
return sd_journal_open_namespace(ret, NULL, flags);
}
#define OPEN_CONTAINER_ALLOWED_FLAGS \
(SD_JOURNAL_LOCAL_ONLY | \
SD_JOURNAL_SYSTEM | \
SD_JOURNAL_ASSUME_IMMUTABLE)
_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
_cleanup_free_ char *root = NULL, *class = NULL;
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
char *p;
int r;
/* This is deprecated, people should use machined's OpenMachineRootDirectory() call instead in
* combination with sd_journal_open_directory_fd(). */
assert_return(machine, -EINVAL);
assert_return(ret, -EINVAL);
assert_return((flags & ~OPEN_CONTAINER_ALLOWED_FLAGS) == 0, -EINVAL);
assert_return(hostname_is_valid(machine, 0), -EINVAL);
p = strjoina("/run/systemd/machines/", machine);
r = parse_env_file(NULL, p,
"ROOT", &root,
"CLASS", &class);
if (r == -ENOENT)
return -EHOSTDOWN;
if (r < 0)
return r;
if (!root)
return -ENODATA;
if (!streq_ptr(class, "container"))
return -EIO;
j = journal_new(flags, root, NULL);
if (!j)
return -ENOMEM;
r = add_search_paths(j);
if (r < 0)
return r;
*ret = TAKE_PTR(j);
return 0;
}
#define OPEN_DIRECTORY_ALLOWED_FLAGS \
(SD_JOURNAL_OS_ROOT | \
SD_JOURNAL_SYSTEM | \
SD_JOURNAL_CURRENT_USER | \
SD_JOURNAL_ASSUME_IMMUTABLE)
_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
int r;
assert_return(ret, -EINVAL);
assert_return(path, -EINVAL);
assert_return((flags & ~OPEN_DIRECTORY_ALLOWED_FLAGS) == 0, -EINVAL);
j = journal_new(flags, path, NULL);
if (!j)
return -ENOMEM;
if (flags & SD_JOURNAL_OS_ROOT)
r = add_search_paths(j);
else
r = add_root_directory(j, path, false);
if (r < 0)
return r;
*ret = TAKE_PTR(j);
return 0;
}
#define OPEN_FILES_ALLOWED_FLAGS \
(SD_JOURNAL_ASSUME_IMMUTABLE)
_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
int r;
assert_return(ret, -EINVAL);
assert_return((flags & ~OPEN_FILES_ALLOWED_FLAGS) == 0, -EINVAL);
j = journal_new(flags, NULL, NULL);
if (!j)
return -ENOMEM;
STRV_FOREACH(path, paths) {
r = add_any_file(j, /* fd= */ -EBADF, *path);
if (r < 0)
return r;
}
j->no_new_files = true;
*ret = TAKE_PTR(j);
return 0;
}
#define OPEN_DIRECTORY_FD_ALLOWED_FLAGS \
(SD_JOURNAL_OS_ROOT | \
SD_JOURNAL_SYSTEM | \
SD_JOURNAL_CURRENT_USER | \
SD_JOURNAL_TAKE_DIRECTORY_FD | \
SD_JOURNAL_ASSUME_IMMUTABLE)
_public_ int sd_journal_open_directory_fd(sd_journal **ret, int fd, int flags) {
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
bool take_fd;
int r;
assert_return(ret, -EINVAL);
assert_return(fd >= 0, -EBADF);
assert_return((flags & ~OPEN_DIRECTORY_FD_ALLOWED_FLAGS) == 0, -EINVAL);
r = fd_verify_directory(fd);
if (r < 0)
return r;
take_fd = FLAGS_SET(flags, SD_JOURNAL_TAKE_DIRECTORY_FD);
j = journal_new(flags & ~SD_JOURNAL_TAKE_DIRECTORY_FD, NULL, NULL);
if (!j)
return -ENOMEM;
j->toplevel_fd = fd;
if (flags & SD_JOURNAL_OS_ROOT)
r = add_search_paths(j);
else
r = add_root_directory(j, NULL, false);
if (r < 0)
return r;
SET_FLAG(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD, take_fd);
*ret = TAKE_PTR(j);
return 0;
}
#define OPEN_FILES_FD_ALLOWED_FLAGS \
(SD_JOURNAL_ASSUME_IMMUTABLE)
_public_ int sd_journal_open_files_fd(sd_journal **ret, int fds[], unsigned n_fds, int flags) {
JournalFile *f;
_cleanup_(sd_journal_closep) sd_journal *j = NULL;
int r;
assert_return(ret, -EINVAL);
assert_return(n_fds > 0, -EBADF);
assert_return((flags & ~OPEN_FILES_FD_ALLOWED_FLAGS) == 0, -EINVAL);
j = journal_new(flags, NULL, NULL);
if (!j)
return -ENOMEM;
for (unsigned i = 0; i < n_fds; i++) {
struct stat st;
if (fds[i] < 0) {
r = -EBADF;
goto fail;
}
if (fstat(fds[i], &st) < 0) {
r = -errno;
goto fail;
}
r = stat_verify_regular(&st);
if (r < 0)
goto fail;
r = add_any_file(j, fds[i], /* path= */ NULL);
if (r < 0)
goto fail;
}
j->no_new_files = true;
j->no_inotify = true;
*ret = TAKE_PTR(j);
return 0;
fail:
/* If we fail, make sure we don't take possession of the files we managed to make use of successfully, and they
* remain open */
ORDERED_HASHMAP_FOREACH(f, j->files)
f->close_fd = false;
return r;
}
_public_ void sd_journal_close(sd_journal *j) {
if (!j || journal_origin_changed(j))
return;
journal_clear_newest_by_boot_id(j);
sd_journal_flush_matches(j);
/* log stats before closing files so we can see the windows state */
if (j->mmap)
mmap_cache_stats_log_debug(j->mmap);
ordered_hashmap_free(j->files);
iterated_cache_free(j->files_cache);
hashmap_free(j->directories_by_path);
hashmap_free(j->directories_by_wd);
if (FLAGS_SET(j->flags, SD_JOURNAL_TAKE_DIRECTORY_FD))
safe_close(j->toplevel_fd);
safe_close(j->inotify_fd);
if (j->mmap)
mmap_cache_unref(j->mmap);
hashmap_free(j->errors);
set_free(j->exclude_syslog_identifiers);
free(j->path);
free(j->prefix);
free(j->namespace);
free(j->unique_field);
free(j->fields_buffer);
free(j);
}
static int journal_file_entry_get_machine_id(JournalFile *f, Object *o, sd_id128_t *ret) {
assert(f);
assert(o);
assert(o->object.type == OBJECT_ENTRY);
assert(ret);
uint64_t n = journal_file_entry_n_items(f, o);
for (uint64_t i = 0; i < n; i++) {
uint64_t p;
const void *d;
size_t l;
int r;
p = journal_file_entry_item_object_offset(f, o, i);
r = journal_file_data_payload(f, /* o= */ NULL, p, "_MACHINE_ID", STRLEN("_MACHINE_ID"),
SIZE_MAX, &d, &l);
if (r == 0)
continue;
if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", i);
continue;
}
if (r < 0)
return r;
if (l != STRLEN("_MACHINE_ID=") + SD_ID128_STRING_MAX - 1)
return -EBADMSG;
/* The data payload is not null-terminated, copy the hex ID to a local buffer. */
char id_string[SD_ID128_STRING_MAX] = {};
memcpy(id_string, (const char*) d + STRLEN("_MACHINE_ID="), SD_ID128_STRING_MAX - 1);
return id128_from_string_nonzero(id_string, ret);
}
return -ENOENT;
}
static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f) {
uint64_t offset, mo, rt;
sd_id128_t id;
ObjectType type;
Object *o;
int r;
assert(j);
assert(f);
assert(f->header);
/* Tries to read the timestamp of the most recently written entry. */
if (FLAGS_SET(j->flags, SD_JOURNAL_ASSUME_IMMUTABLE) && f->newest_entry_offset != 0)
return 0; /* We have already read the file, and we assume that the file is immutable. */
if (f->header->state == f->newest_state &&
f->header->state == STATE_ARCHIVED &&
f->newest_entry_offset != 0)
return 0; /* We have already read archived file. */
if (JOURNAL_HEADER_CONTAINS(f->header, tail_entry_offset)) {
offset = le64toh(READ_NOW(f->header->tail_entry_offset));
type = OBJECT_ENTRY;
} else {
offset = le64toh(READ_NOW(f->header->tail_object_offset));
type = OBJECT_UNUSED;
}
if (offset == 0)
return -ENODATA; /* not a single object/entry, hence no tail timestamp */
if (offset == f->newest_entry_offset)
return 0; /* No new entry is added after we read last time. */
/* Move to the last object in the journal file, in the hope it is an entry (which it usually will
* be). If we lack the "tail_entry_offset" field in the header, we specify the type as OBJECT_UNUSED
* here, since we cannot be sure what the last object will be, and want no noisy logging if it isn't
* an entry. We instead check after figuring out the pointer. */
r = journal_file_move_to_object(f, type, offset, &o);
if (r < 0) {
log_debug_errno(r, "Failed to move to last object in journal file, ignoring: %m");
o = NULL;
offset = 0;
}
if (o && o->object.type == OBJECT_ENTRY) {
/* Yay, last object is an entry, let's use the data. */
id = o->entry.boot_id;
mo = le64toh(o->entry.monotonic);
rt = le64toh(o->entry.realtime);
} else {
/* So the object is not an entry or we couldn't access it? In that case, let's read the most
* recent entry timestamps from the header. It's equally good. Unfortunately though, in old
* versions of the journal the boot ID in the header doesn't have to match the monotonic
* timestamp of the header. Let's check the header flag that indicates whether this strictly
* matches first hence, before using the data. */
if (JOURNAL_HEADER_TAIL_ENTRY_BOOT_ID(f->header) && f->header->state == STATE_ARCHIVED) {
mo = le64toh(f->header->tail_entry_monotonic);
rt = le64toh(f->header->tail_entry_realtime);
id = f->header->tail_entry_boot_id;
offset = UINT64_MAX;
} else {
/* Otherwise let's find the last entry manually (this possibly means traversing the
* chain of entry arrays, till the end */
r = journal_file_next_entry(f, 0, DIRECTION_UP, &o, offset == 0 ? &offset : NULL);
if (r < 0)
return r;
if (r == 0)
return -ENODATA;
id = o->entry.boot_id;
mo = le64toh(o->entry.monotonic);
rt = le64toh(o->entry.realtime);
}
}
if (mo > rt) /* monotonic clock is further ahead than realtime? that's weird, refuse to use the data */
return -ENODATA;
/* Try to get the machine ID from the tail entry's _MACHINE_ID= field rather than from the file
* header, as the header always reflects the machine that *wrote* the file, not necessarily the
* machine that *originated* the entries. This distinction matters for journal files created by
* systemd-journal-remote, which stamps all files with the receiving machine's ID while the entries
* inside carry the source machine's _MACHINE_ID=. Without this, compare_boot_ids() would
* incorrectly consider boot IDs from different source machines as comparable (since they'd all
* share the receiver's machine ID), leading to boot-grouped rather than realtime-interleaved
* iteration order when merging cross-machine journals.
*
* If we don't have an entry object (header-only fallback for archived files) or the entry lacks
* the _MACHINE_ID= field (older journals), fall back to the header's machine_id. */
sd_id128_t mid = f->header->machine_id;
if (o && o->object.type == OBJECT_ENTRY) {
r = journal_file_entry_get_machine_id(f, o, &mid);
if (r < 0 && r != -ENOENT)
log_debug_errno(r, "Failed to read _MACHINE_ID from tail entry, using header value: %m");
}
if (offset == f->newest_entry_offset) {
/* Cached data and the current one should be equivalent. */
if (!sd_id128_equal(f->newest_machine_id, mid) ||
!sd_id128_equal(f->newest_boot_id, id) ||
f->newest_monotonic_usec != mo ||
f->newest_realtime_usec != rt)
return -EBADMSG;
return 0; /* No new entry is added after we read last time. */
}
if (!sd_id128_equal(f->newest_boot_id, id))
journal_file_unlink_newest_by_boot_id(j, f);
f->newest_boot_id = id;
f->newest_monotonic_usec = mo;
f->newest_realtime_usec = rt;
f->newest_machine_id = mid;
f->newest_entry_offset = offset;
f->newest_state = f->header->state;
r = journal_file_reshuffle_newest_by_boot_id(j, f);
if (r < 0)
return r;
return 1; /* Updated. */
}
_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
JournalFile *f;
Object *o;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
f = j->current_file;
if (!f)
return -EADDRNOTAVAIL;
if (f->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
if (r < 0)
return r;
uint64_t t = le64toh(o->entry.realtime);
if (!VALID_REALTIME(t))
return -EBADMSG;
if (ret)
*ret = t;
return 0;
}
_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret_monotonic, sd_id128_t *ret_boot_id) {
JournalFile *f;
Object *o;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
f = j->current_file;
if (!f)
return -EADDRNOTAVAIL;
if (f->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
if (r < 0)
return r;
if (!ret_boot_id) {
sd_id128_t id;
r = sd_id128_get_boot(&id);
if (r < 0)
return r;
if (!sd_id128_equal(id, o->entry.boot_id))
return -ESTALE;
}
uint64_t t = le64toh(o->entry.monotonic);
if (!VALID_MONOTONIC(t))
return -EBADMSG;
if (ret_monotonic)
*ret_monotonic = t;
if (ret_boot_id)
*ret_boot_id = o->entry.boot_id;
return 0;
}
_public_ int sd_journal_get_seqnum(
sd_journal *j,
uint64_t *ret_seqnum,
sd_id128_t *ret_seqnum_id) {
JournalFile *f;
Object *o;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
f = j->current_file;
if (!f)
return -EADDRNOTAVAIL;
if (f->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
if (r < 0)
return r;
if (ret_seqnum_id)
*ret_seqnum_id = f->header->seqnum_id;
if (ret_seqnum)
*ret_seqnum = le64toh(o->entry.seqnum);
return 0;
}
static bool field_is_valid(const char *field) {
assert(field);
if (isempty(field))
return false;
if (startswith(field, "__"))
return false;
for (const char *p = field; *p; p++) {
if (*p == '_')
continue;
if (*p >= 'A' && *p <= 'Z')
continue;
if (ascii_isdigit(*p))
continue;
return false;
}
return true;
}
_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **ret_data, size_t *ret_size) {
JournalFile *f;
size_t field_length;
Object *o;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(field, -EINVAL);
assert_return(field_is_valid(field), -EINVAL);
f = j->current_file;
if (!f)
return -EADDRNOTAVAIL;
if (f->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
if (r < 0)
return r;
field_length = strlen(field);
uint64_t n = journal_file_entry_n_items(f, o);
for (uint64_t i = 0; i < n; i++) {
uint64_t p;
const void *d;
size_t l;
p = journal_file_entry_item_object_offset(f, o, i);
r = journal_file_data_payload(f, NULL, p, field, field_length, j->data_threshold,
ret_data ? &d : NULL, ret_size ? &l : NULL);
if (r == 0)
continue;
if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", i);
continue;
}
if (r < 0)
return r;
if (ret_data)
*ret_data = d;
if (ret_size)
*ret_size = l;
return 0;
}
return -ENOENT;
}
_public_ int sd_journal_enumerate_data(sd_journal *j, const void **ret_data, size_t *ret_size) {
JournalFile *f;
Object *o;
int r;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(ret_data, -EINVAL);
assert_return(ret_size, -EINVAL);
f = j->current_file;
if (!f)
return -EADDRNOTAVAIL;
if (f->current_offset <= 0)
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
if (r < 0)
return r;
for (uint64_t n = journal_file_entry_n_items(f, o); j->current_field < n; j->current_field++) {
uint64_t p;
const void *d;
size_t l;
p = journal_file_entry_item_object_offset(f, o, j->current_field);
r = journal_file_data_payload(f, NULL, p, NULL, 0, j->data_threshold, &d, &l);
if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) {
log_debug_errno(r, "Entry item %"PRIu64" data object is bad, skipping over it: %m", j->current_field);
continue;
}
if (r < 0)
return r;
assert(r > 0);
*ret_data = d;
*ret_size = l;
j->current_field++;