chiark
/
gitweb
/
~ianmdlvl
/
elogind.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
journal: simplify set_location()
[elogind.git]
/
src
/
journal
/
sd-journal.c
diff --git
a/src/journal/sd-journal.c
b/src/journal/sd-journal.c
index 1fc9f01d0aa8810f333bf571783190987b14a95d..5a00cb0c9475699565124432f94706e7ce48ece4 100644
(file)
--- a/
src/journal/sd-journal.c
+++ b/
src/journal/sd-journal.c
@@
-86,8
+86,8
@@
static void detach_location(sd_journal *j) {
j->current_file = NULL;
j->current_field = 0;
j->current_file = NULL;
j->current_field = 0;
- HASHMAP_FOREACH(f, j->files, i)
-
f->current_offset = 0
;
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i)
+
journal_file_reset_location(f)
;
}
static void reset_location(sd_journal *j) {
}
static void reset_location(sd_journal *j) {
@@
-114,20
+114,19
@@
static void init_location(Location *l, LocationType type, JournalFile *f, Object
l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
}
l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
}
-static void set_location(sd_journal *j, LocationType type, JournalFile *f, Object *o,
- direction_t direction, uint64_t offset) {
+static void set_location(sd_journal *j, JournalFile *f, Object *o) {
assert(j);
assert(j);
- assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
assert(f);
assert(o);
assert(f);
assert(o);
- init_location(&j->current_location,
type
, f, o);
+ init_location(&j->current_location,
LOCATION_DISCRETE
, f, o);
j->current_file = f;
j->current_field = 0;
j->current_file = f;
j->current_field = 0;
- f->last_direction = direction;
- f->current_offset = offset;
+ /* Let f know its candidate entry was picked. */
+ assert(f->location_type == LOCATION_SEEK);
+ f->location_type = LOCATION_DISCRETE;
}
static int match_is_valid(const void *data, size_t size) {
}
static int match_is_valid(const void *data, size_t size) {
@@
-849,14
+848,14
@@
static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direc
int k;
k = compare_with_location(f, c, &j->current_location);
int k;
k = compare_with_location(f, c, &j->current_location);
- if (direction == DIRECTION_DOWN)
- found = k > 0;
- else
- found = k < 0;
+
+ found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
} else
found = true;
if (found) {
} else
found = true;
if (found) {
+ journal_file_save_location(f, direction, c, cp);
+
if (ret)
*ret = c;
if (offset)
if (ret)
*ret = c;
if (offset)
@@
-881,16
+880,18
@@
static int real_journal_next(sd_journal *j, direction_t direction) {
assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD);
assert_return(j, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD);
- HASHMAP_FOREACH(f, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i) {
bool found;
r = next_beyond_location(j, f, direction, &o, &p);
if (r < 0) {
bool found;
r = next_beyond_location(j, f, direction, &o, &p);
if (r < 0) {
- log_debug
("Can't iterate through %s, ignoring: %s", f->path, strerror(-r)
);
+ log_debug
_errno(r, "Can't iterate through %s, ignoring: %m", f->path
);
remove_file_real(j, f);
continue;
remove_file_real(j, f);
continue;
- } else if (r == 0)
+ } else if (r == 0) {
+ f->location_type = LOCATION_TAIL;
continue;
continue;
+ }
if (!new_file)
found = true;
if (!new_file)
found = true;
@@
-915,7
+916,7
@@
static int real_journal_next(sd_journal *j, direction_t direction) {
if (r < 0)
return r;
if (r < 0)
return r;
- set_location(j,
LOCATION_DISCRETE, new_file, o, direction, new_offset
);
+ set_location(j,
new_file, o
);
return 1;
}
return 1;
}
@@
-1290,10
+1291,10
@@
static int add_any_file(sd_journal *j, const char *path) {
assert(j);
assert(path);
assert(j);
assert(path);
- if (hashmap_get(j->files, path))
+ if (
ordered_
hashmap_get(j->files, path))
return 0;
return 0;
- if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
+ if (
ordered_
hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
log_warning("Too many open journal files, not adding %s.", path);
return set_put_error(j, -ETOOMANYREFS);
}
log_warning("Too many open journal files, not adding %s.", path);
return set_put_error(j, -ETOOMANYREFS);
}
@@
-1304,7
+1305,7
@@
static int add_any_file(sd_journal *j, const char *path) {
/* journal_file_dump(f); */
/* journal_file_dump(f); */
- r = hashmap_put(j->files, f->path, f);
+ r =
ordered_
hashmap_put(j->files, f->path, f);
if (r < 0) {
journal_file_close(f);
return r;
if (r < 0) {
journal_file_close(f);
return r;
@@
-1353,7
+1354,7
@@
static int remove_file(sd_journal *j, const char *prefix, const char *filename)
if (!path)
return -ENOMEM;
if (!path)
return -ENOMEM;
- f = hashmap_get(j->files, path);
+ f =
ordered_
hashmap_get(j->files, path);
if (!f)
return 0;
if (!f)
return 0;
@@
-1365,7
+1366,7
@@
static void remove_file_real(sd_journal *j, JournalFile *f) {
assert(j);
assert(f);
assert(j);
assert(f);
- hashmap_remove(j->files, f->path);
+
ordered_
hashmap_remove(j->files, f->path);
log_debug("File %s removed.", f->path);
log_debug("File %s removed.", f->path);
@@
-1375,8
+1376,11
@@
static void remove_file_real(sd_journal *j, JournalFile *f) {
}
if (j->unique_file == f) {
}
if (j->unique_file == f) {
- j->unique_file = NULL;
+ /* Jump to the next unique_file or NULL if that one was last */
+ j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
j->unique_offset = 0;
j->unique_offset = 0;
+ if (!j->unique_file)
+ j->unique_file_lost = true;
}
journal_file_close(f);
}
journal_file_close(f);
@@
-1409,7
+1413,7
@@
static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
d = opendir(path);
if (!d) {
d = opendir(path);
if (!d) {
- log_debug
(
"Failed to open %s: %m", path);
+ log_debug
_errno(errno,
"Failed to open %s: %m", path);
if (errno == ENOENT)
return 0;
return -errno;
if (errno == ENOENT)
return 0;
return -errno;
@@
-1455,7
+1459,7
@@
static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
de = readdir(d);
if (!de && errno != 0) {
r = -errno;
de = readdir(d);
if (!de && errno != 0) {
r = -errno;
- log_debug
(
"Failed to read directory %s: %m", m->path);
+ log_debug
_errno(errno,
"Failed to read directory %s: %m", m->path);
return r;
}
if (!de)
return r;
}
if (!de)
@@
-1465,8
+1469,8
@@
static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
dirent_is_file_with_suffix(de, ".journal~")) {
r = add_file(j, m->path, de->d_name);
if (r < 0) {
dirent_is_file_with_suffix(de, ".journal~")) {
r = add_file(j, m->path, de->d_name);
if (r < 0) {
- log_debug
("Failed to add file %s/%s: %s
",
-
m->path, de->d_name, strerror(-r)
);
+ log_debug
_errno(r, "Failed to add file %s/%s: %m
",
+
m->path, de->d_name
);
r = set_put_error(j, r);
if (r < 0)
return r;
r = set_put_error(j, r);
if (r < 0)
return r;
@@
-1545,7
+1549,7
@@
static int add_root_directory(sd_journal *j, const char *p) {
de = readdir(d);
if (!de && errno != 0) {
r = -errno;
de = readdir(d);
if (!de && errno != 0) {
r = -errno;
- log_debug
(
"Failed to read directory %s: %m", m->path);
+ log_debug
_errno(errno,
"Failed to read directory %s: %m", m->path);
return r;
}
if (!de)
return r;
}
if (!de)
@@
-1555,8
+1559,8
@@
static int add_root_directory(sd_journal *j, const char *p) {
dirent_is_file_with_suffix(de, ".journal~")) {
r = add_file(j, m->path, de->d_name);
if (r < 0) {
dirent_is_file_with_suffix(de, ".journal~")) {
r = add_file(j, m->path, de->d_name);
if (r < 0) {
- log_debug
("Failed to add file %s/%s: %s
",
-
m->path, de->d_name, strerror(-r)
);
+ log_debug
_errno(r, "Failed to add file %s/%s: %m
",
+
m->path, de->d_name
);
r = set_put_error(j, r);
if (r < 0)
return r;
r = set_put_error(j, r);
if (r < 0)
return r;
@@
-1566,7
+1570,7
@@
static int add_root_directory(sd_journal *j, const char *p) {
r = add_directory(j, m->path, de->d_name);
if (r < 0)
r = add_directory(j, m->path, de->d_name);
if (r < 0)
- log_debug
("Failed to add directory %s/%s: %s", m->path, de->d_name, strerror(-r)
);
+ log_debug
_errno(r, "Failed to add directory %s/%s: %m", m->path, de->d_name
);
}
}
}
}
@@
-1633,7
+1637,7
@@
static int add_current_paths(sd_journal *j) {
* "root" directories. We don't expect errors here, so we
* treat them as fatal. */
* "root" directories. We don't expect errors here, so we
* treat them as fatal. */
- HASHMAP_FOREACH(f, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i) {
_cleanup_free_ char *dir;
int r;
_cleanup_free_ char *dir;
int r;
@@
-1688,7
+1692,7
@@
static sd_journal *journal_new(int flags, const char *path) {
goto fail;
}
goto fail;
}
- j->files = hashmap_new(&string_hash_ops);
+ j->files =
ordered_
hashmap_new(&string_hash_ops);
j->directories_by_path = hashmap_new(&string_hash_ops);
j->mmap = mmap_cache_new();
if (!j->files || !j->directories_by_path || !j->mmap)
j->directories_by_path = hashmap_new(&string_hash_ops);
j->mmap = mmap_cache_new();
if (!j->files || !j->directories_by_path || !j->mmap)
@@
-1734,7
+1738,7
@@
_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, in
assert_return(machine, -EINVAL);
assert_return(ret, -EINVAL);
assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL);
assert_return(machine, -EINVAL);
assert_return(ret, -EINVAL);
assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL);
- assert_return(
filename_is_safe
(machine), -EINVAL);
+ assert_return(
machine_name_is_valid
(machine), -EINVAL);
p = strappenda("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
p = strappenda("/run/systemd/machines/", machine);
r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
@@
-1809,7
+1813,7
@@
_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int fla
STRV_FOREACH(path, paths) {
r = add_any_file(j, *path);
if (r < 0) {
STRV_FOREACH(path, paths) {
r = add_any_file(j, *path);
if (r < 0) {
- log_error
("Failed to open %s: %s", *path, strerror(-r)
);
+ log_error
_errno(r, "Failed to open %s: %m", *path
);
goto fail;
}
}
goto fail;
}
}
@@
-1834,10
+1838,10
@@
_public_ void sd_journal_close(sd_journal *j) {
sd_journal_flush_matches(j);
sd_journal_flush_matches(j);
- while ((f = hashmap_steal_first(j->files)))
+ while ((f =
ordered_
hashmap_steal_first(j->files)))
journal_file_close(f);
journal_file_close(f);
- hashmap_free(j->files);
+
ordered_
hashmap_free(j->files);
while ((d = hashmap_first(j->directories_by_path)))
remove_directory(j, d);
while ((d = hashmap_first(j->directories_by_path)))
remove_directory(j, d);
@@
-2217,8
+2221,8
@@
static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
r = add_file(j, d->path, e->name);
if (r < 0) {
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
r = add_file(j, d->path, e->name);
if (r < 0) {
- log_debug
("Failed to add file %s/%s: %s
",
-
d->path, e->name, strerror(-r)
);
+ log_debug
_errno(r, "Failed to add file %s/%s: %m
",
+
d->path, e->name
);
set_put_error(j, r);
}
set_put_error(j, r);
}
@@
-2226,7
+2230,7
@@
static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
r = remove_file(j, d->path, e->name);
if (r < 0)
r = remove_file(j, d->path, e->name);
if (r < 0)
- log_debug
("Failed to remove file %s/%s: %s", d->path, e->name, strerror(-r)
);
+ log_debug
_errno(r, "Failed to remove file %s/%s: %m", d->path, e->name
);
}
} else if (!d->is_root && e->len == 0) {
}
} else if (!d->is_root && e->len == 0) {
@@
-2236,7
+2240,7
@@
static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) {
r = remove_directory(j, d);
if (r < 0)
if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) {
r = remove_directory(j, d);
if (r < 0)
- log_debug
("Failed to remove directory %s: %s", d->path, strerror(-r)
);
+ log_debug
_errno(r, "Failed to remove directory %s: %m", d->path
);
}
}
@@
-2247,7
+2251,7
@@
static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
r = add_directory(j, d->path, e->name);
if (r < 0)
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
r = add_directory(j, d->path, e->name);
if (r < 0)
- log_debug
("Failed to add directory %s/%s: %s", d->path, e->name, strerror(-r)
);
+ log_debug
_errno(r, "Failed to add directory %s/%s: %m", d->path, e->name
);
}
}
}
}
@@
-2272,7
+2276,6
@@
static int determine_change(sd_journal *j) {
}
_public_ int sd_journal_process(sd_journal *j) {
}
_public_ int sd_journal_process(sd_journal *j) {
- uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX] _alignas_(struct inotify_event);
bool got_something = false;
assert_return(j, -EINVAL);
bool got_something = false;
assert_return(j, -EINVAL);
@@
-2281,6
+2284,7
@@
_public_ int sd_journal_process(sd_journal *j) {
j->last_process_usec = now(CLOCK_MONOTONIC);
for (;;) {
j->last_process_usec = now(CLOCK_MONOTONIC);
for (;;) {
+ uint8_t buffer[INOTIFY_EVENT_MAX] _alignas_(struct inotify_event);
struct inotify_event *e;
ssize_t l;
struct inotify_event *e;
ssize_t l;
@@
-2294,18
+2298,8
@@
_public_ int sd_journal_process(sd_journal *j) {
got_something = true;
got_something = true;
- e = (struct inotify_event*) buffer;
- while (l > 0) {
- size_t step;
-
+ FOREACH_INOTIFY_EVENT(e, buffer, l)
process_inotify_event(j, e);
process_inotify_event(j, e);
-
- step = sizeof(struct inotify_event) + e->len;
- assert(step <= (size_t) l);
-
- e = (struct inotify_event*) ((uint8_t*) e + step);
- l -= step;
- }
}
}
}
}
@@
-2367,7
+2361,7
@@
_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from,
assert_return(from || to, -EINVAL);
assert_return(from != to, -EINVAL);
assert_return(from || to, -EINVAL);
assert_return(from != to, -EINVAL);
- HASHMAP_FOREACH(f, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i) {
usec_t fr, t;
r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
usec_t fr, t;
r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
@@
-2407,7
+2401,7
@@
_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot
assert_return(from || to, -EINVAL);
assert_return(from != to, -EINVAL);
assert_return(from || to, -EINVAL);
assert_return(from != to, -EINVAL);
- HASHMAP_FOREACH(f, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i) {
usec_t fr, t;
r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
usec_t fr, t;
r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
@@
-2442,7
+2436,7
@@
void journal_print_header(sd_journal *j) {
assert(j);
assert(j);
- HASHMAP_FOREACH(f, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i) {
if (newline)
putchar('\n');
else
if (newline)
putchar('\n');
else
@@
-2461,7
+2455,7
@@
_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
assert_return(!journal_pid_changed(j), -ECHILD);
assert_return(bytes, -EINVAL);
assert_return(!journal_pid_changed(j), -ECHILD);
assert_return(bytes, -EINVAL);
- HASHMAP_FOREACH(f, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(f, j->files, i) {
struct stat st;
if (fstat(f->fd, &st) < 0)
struct stat st;
if (fstat(f->fd, &st) < 0)
@@
-2490,6
+2484,7
@@
_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
j->unique_field = f;
j->unique_file = NULL;
j->unique_offset = 0;
j->unique_field = f;
j->unique_file = NULL;
j->unique_offset = 0;
+ j->unique_file_lost = false;
return 0;
}
return 0;
}
@@
-2506,9
+2501,13
@@
_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
k = strlen(j->unique_field);
if (!j->unique_file) {
k = strlen(j->unique_field);
if (!j->unique_file) {
- j->unique_file = hashmap_first(j->files);
+ if (j->unique_file_lost)
+ return 0;
+
+ j->unique_file = ordered_hashmap_first(j->files);
if (!j->unique_file)
return 0;
if (!j->unique_file)
return 0;
+
j->unique_offset = 0;
}
j->unique_offset = 0;
}
@@
-2538,20
+2537,17
@@
_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
/* We reached the end of the list? Then start again, with the next file */
if (j->unique_offset == 0) {
/* We reached the end of the list? Then start again, with the next file */
if (j->unique_offset == 0) {
- JournalFile *n;
-
- n = hashmap_next(j->files, j->unique_file->path);
- if (!n)
+ j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
+ if (!j->unique_file)
return 0;
return 0;
- j->unique_file = n;
continue;
}
continue;
}
- /* We do not use
the type context here, but 0 instead,
- * so that we can look at this data object at the same
+ /* We do not use
OBJECT_DATA context here, but OBJECT_UNUSED
+ *
instead,
so that we can look at this data object at the same
* time as one on another file */
* time as one on another file */
- r = journal_file_move_to_object(j->unique_file,
0
, j->unique_offset, &o);
+ r = journal_file_move_to_object(j->unique_file,
OBJECT_UNUSED
, j->unique_offset, &o);
if (r < 0)
return r;
if (r < 0)
return r;
@@
-2563,10
+2559,6
@@
_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
return -EBADMSG;
}
return -EBADMSG;
}
- r = journal_file_object_keep(j->unique_file, o, j->unique_offset);
- if (r < 0)
- return r;
-
r = return_data(j, j->unique_file, o, &odata, &ol);
if (r < 0)
return r;
r = return_data(j, j->unique_file, o, &odata, &ol);
if (r < 0)
return r;
@@
-2590,7
+2582,7
@@
_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
* object by checking if it exists in the earlier
* traversed files. */
found = false;
* object by checking if it exists in the earlier
* traversed files. */
found = false;
- HASHMAP_FOREACH(of, j->files, i) {
+
ORDERED_
HASHMAP_FOREACH(of, j->files, i) {
Object *oo;
uint64_t op;
Object *oo;
uint64_t op;
@@
-2614,10
+2606,6
@@
_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_
if (found)
continue;
if (found)
continue;
- r = journal_file_object_release(j->unique_file, o, j->unique_offset);
- if (r < 0)
- return r;
-
r = return_data(j, j->unique_file, o, data, l);
if (r < 0)
return r;
r = return_data(j, j->unique_file, o, data, l);
if (r < 0)
return r;
@@
-2632,6
+2620,7
@@
_public_ void sd_journal_restart_unique(sd_journal *j) {
j->unique_file = NULL;
j->unique_offset = 0;
j->unique_file = NULL;
j->unique_offset = 0;
+ j->unique_file_lost = false;
}
_public_ int sd_journal_reliable_fd(sd_journal *j) {
}
_public_ int sd_journal_reliable_fd(sd_journal *j) {