sd-journal.c revision 115646c789d84ea182c5eab6b0945263e3c49305
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc This file is part of systemd.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Copyright 2011 Lennart Poettering
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc systemd is free software; you can redistribute it and/or modify it
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc under the terms of the GNU Lesser General Public License as published by
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc the Free Software Foundation; either version 2.1 of the License, or
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc (at your option) any later version.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc systemd is distributed in the hope that it will be useful, but
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc WITHOUT ANY WARRANTY; without even the implied warranty of
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Lesser General Public License for more details.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc You should have received a copy of the GNU Lesser General Public License
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc along with systemd; If not, see <http://www.gnu.org/licenses/>.
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsstatic void reset_location(sd_journal *j) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems l->seqnum_id = f->header->seqnum_id;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic void set_location(sd_journal *j, LocationType type, JournalFile *f, Object *o, uint64_t offset) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic int match_is_valid(const void *data, size_t size) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini const char *b, *p;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return false;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini for (p = b; p < b + size; p++) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (*p == '=')
3fd94f8c011031b38162a1db3b554de4371c167fam return p > b;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (*p == '_')
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return false;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return false;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems for (j = 0; j < s && j < t; j++) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return true;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic Match *match_new(Match *p, MatchType t) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* level 0: OR term
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * level 1: AND terms
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * level 2: OR terms
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * level 3: concrete matches */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->level0) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->level1) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan LIST_FOREACH(matches, l2, j->level1->matches) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Exactly the same match already? Then ignore
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan * this addition */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Same field? Then let's add this to this OR term */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (same_field(data, size, l3->data, l3->size)) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan add_here = match_new(j->level1, MATCH_OR_TERM);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ int sd_journal_add_disjunction(sd_journal *j) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini bool enclose = false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini char *t, *k;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int compare_entry_order(JournalFile *af, Object *_ao,
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* The mmap cache might invalidate the object from the first
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * file if we look at the one from the second file. Hence
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini * temporarily copy the header of the first one, and look at
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * that only. */
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems ao = alloca(offsetof(EntryObject, items));
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini r = journal_file_move_to_object(bf, OBJECT_ENTRY, bp, &bo);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return strcmp(af->path, bf->path);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* We operate on two different files here, hence we can access
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * two objects at the same time, which we normally can't.
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems * If contents and timestamps match, these entries are
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * identical, even if the seqnum does not match */
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id) &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems ao->entry.monotonic == bo->entry.monotonic &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems ao->entry.realtime == bo->entry.realtime &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems ao->entry.xor_hash == bo->entry.xor_hash)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* If this is from the same seqnum source, compare
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * seqnums */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Wow! This is weird, different data but the same
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * seqnums? Something is borked, but let's make the
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini * best of it and compare by time. */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* If the boot id matches compare monotonic time */
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems b = le64toh(bo->entry.monotonic);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* Otherwise compare UTC time */
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems b = le64toh(bo->entry.realtime);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Finally, compare by contents */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic int compare_with_location(JournalFile *af, Object *ao, Location *l) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems assert(l->type == LOCATION_DISCRETE || l->type == LOCATION_SEEK);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems le64toh(ao->entry.realtime) == l->realtime &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a > l->monotonic)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (a < l->realtime)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a > l->realtime)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a < l->xor_hash)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a > l->xor_hash)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* Find the earliest match beyond after_offset */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini else if (r > 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Always jump to the next matching entry and repeat
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini * this until we fine and offset that matches for all
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * matches. */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (np == 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems else if (direction == DIRECTION_DOWN)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems limit = MAX(np, after_offset);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems r = next_for_match(j, i, f, limit, direction, NULL, &cp);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if ((direction == DIRECTION_DOWN ? cp >= after_offset : cp <= after_offset) &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems (np == 0 || (direction == DIRECTION_DOWN ? cp > np : np < cp))) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems continue_looking = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* FIXME: missing: find by monotonic */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (r != -ENOENT)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* Find the earliest match */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = find_location_for_match(j, i, f, direction, NULL, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz else if (r > 0) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (np == 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* First jump to the last match, and then find the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * next one where all matches match */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = find_location_for_match(j, i, f, direction, NULL, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (np == 0 || (direction == DIRECTION_DOWN ? np < cp : np > cp))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return next_for_match(j, m, f, np, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->level0) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* No matches is simple */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, ret, offset);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return journal_file_next_entry(f, NULL, 0, DIRECTION_UP, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
3fd94f8c011031b38162a1db3b554de4371c167fam if (r != -ENOENT)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry(f, NULL, 0, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return find_location_for_match(j, j->level0, f, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* No matches is easy. We simple advance the file
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * pointer by one. */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry(f, c, cp, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* If we have a match then we look for the next matching entry
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * with an offset at least one step larger */
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return next_for_match(j, j->level0, f, direction == DIRECTION_DOWN ? cp+1 : cp-1, direction, ret, offset);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsstatic int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction, Object **ret, uint64_t *offset) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (f->current_offset > 0) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems cp = f->current_offset;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = next_with_matches(j, f, direction, &c, &cp);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = find_location_with_matches(j, f, direction, &c, &cp);
13a55820e952b584554bc6b9d4e9303052a2cf29ar /* OK, we found the spot, now let's advance until to an entry
13a55820e952b584554bc6b9d4e9303052a2cf29ar * that is actually different from what we were previously
13a55820e952b584554bc6b9d4e9303052a2cf29ar * looking at. This is necessary to handle entries which exist
13a55820e952b584554bc6b9d4e9303052a2cf29ar * in two (or more) journal files, and which shall all be
13a55820e952b584554bc6b9d4e9303052a2cf29ar * suppressed but one. */
13a55820e952b584554bc6b9d4e9303052a2cf29ar for (;;) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini k = compare_with_location(f, c, &j->current_location);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int real_journal_next(sd_journal *j, direction_t direction) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r < 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Can't iterate through %s, ignoring: %s", f->path, strerror(-r));
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc } else if (r == 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz set_location(j, LOCATION_DISCRETE, new_file, o, new_offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int c = 0, r;
308903890e892f9c21ee582e3a52fdd67e52870bartem if (skip == 0) {
308903890e892f9c21ee582e3a52fdd67e52870bartem /* If this is not a discrete skip, then at least
308903890e892f9c21ee582e3a52fdd67e52870bartem * resolve the current location */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r == 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return real_journal_next_skip(j, DIRECTION_DOWN, skip);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return real_journal_next_skip(j, DIRECTION_UP, skip);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!j->current_file || j->current_file->current_offset <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems sd_id128_to_string(j->current_file->header->seqnum_id, sid);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx",
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *w, *state;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long long seqnum, monotonic, realtime, xor_hash;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int k = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz switch (w[0]) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_SEEK;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.realtime = (uint64_t) realtime;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.realtime_set = true;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (seqnum_set && seqnum_id_set) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.seqnum = (uint64_t) seqnum;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.seqnum_id = seqnum_id;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.seqnum_set = true;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (monotonic_set && boot_id_set) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.monotonic = (uint64_t) monotonic;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.boot_id = boot_id;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.monotonic_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j->current_file || j->current_file->current_offset <= 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long long ll;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc int k = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz switch (w[0]) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (k < 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (sscanf(item+2, "%llx", &ll) != 1)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (k < 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (ll != le64toh(o->entry.monotonic))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (sscanf(item+2, "%llx", &ll) != 1)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (ll != le64toh(o->entry.xor_hash))
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_SEEK;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.boot_id = boot_id;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.monotonic = usec;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.monotonic_set = true;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_SEEK;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.realtime = usec;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.realtime_set = true;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_seek_head(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_HEAD;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_seek_tail(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_TAIL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int add_file(sd_journal *j, const char *prefix, const char *filename) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems streq(filename, "system.journal~") ||
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems (startswith(filename, "system@") &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems (endswith(filename, ".journal") || endswith(filename, ".journal~")))))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems path = strjoin(prefix, "/", filename, NULL);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (hashmap_get(j->files, path)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Too many open journal files, not adding %s, ignoring.", path);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r < 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* journal_file_dump(f); */
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int remove_file(sd_journal *j, const char *prefix, const char *filename) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems path = strjoin(prefix, "/", filename, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_file == f) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->unique_file == f) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems m = hashmap_get(j->directories_by_path, path);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems m = new0(Directory, 1);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (m->is_root) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh for (;;) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r != 0 || !de)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Failed to add file %s/%s: %s", m->path, de->d_name, strerror(-r));
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsstatic int add_root_directory(sd_journal *j, const char *p) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems !path_startswith(p, "/run"))
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems m = new0(Directory, 1);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (!m->is_root) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m->wd = inotify_add_watch(j->inotify_fd, m->path,
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r != 0 || !de)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh log_debug("Failed to add file %s/%s: %s", m->path, de->d_name, strerror(-r));
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini } else if ((de->d_type == DT_DIR || de->d_type == DT_LNK || de->d_type == DT_UNKNOWN) &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Failed to add directory %s/%s: %s", m->path, de->d_name, strerror(-r));
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int remove_directory(sd_journal *j, Directory *d) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (d->wd > 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz const char search_paths[] =
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh const char *p;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* We ignore most errors here, since the idea is to only open
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * what's actually accessible, and ignore the rest. */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->inotify_fd < 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->inotify_fd < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->directories_by_wd = hashmap_new(trivial_hash_func, trivial_compare_func);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic sd_journal *journal_new(int flags, const char *path) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->path) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->files = hashmap_new(string_hash_func, string_compare_func);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->files) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->directories_by_path = hashmap_new(string_hash_func, string_compare_func);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ int sd_journal_open(sd_journal **ret, int flags) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan while ((d = hashmap_first(j->directories_by_path)))
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan while ((d = hashmap_first(j->directories_by_wd)))
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (j->inotify_fd >= 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems close_nointr_nofail(j->inotify_fd);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (f->current_offset <= 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return -EADDRNOTAVAIL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz const char *p;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return false;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return false;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (p = field; *p; p++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (*p == '_')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return false;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return true;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (f->current_offset <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (i = 0; i < n; i++) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l = le64toh(o->object.size) - offsetof(Object, data.payload);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (uncompress_startswith(o->data.payload, l,
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan &f->compress_buffer, &f->compress_buffer_size,
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan &f->compress_buffer, &f->compress_buffer_size, &rsize))
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini memcmp(o->data.payload, field, field_length) == 0 &&
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if ((uint64_t) t != l)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic int return_data(JournalFile *f, Object *o, const void **data, size_t *size) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan l = le64toh(o->object.size) - offsetof(Object, data.payload);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* We can't read objects larger than 4G on a 32bit machine */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EPROTONOSUPPORT;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini p = le64toh(o->entry.items[j->current_field].object_offset);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems le_hash = o->entry.items[j->current_field].hash;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ void sd_journal_restart_data(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (j->inotify_fd >= 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* Iterate through all dirs again, to add them to the
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * inotify */
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic void process_inotify_event(sd_journal *j, struct inotify_event *e) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Is this a subdirectory we watch? */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!(e->mask & IN_ISDIR) && e->len > 0 &&
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems (endswith(e->name, ".journal") ||
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Event for a journal file */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Failed to add file %s/%s: %s", d->path, e->name, strerror(-r));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Failed to remove file %s/%s: %s", d->path, e->name, strerror(-r));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Event for a subdirectory */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Failed to remove directory %s: %s", d->path, strerror(-r));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Event for root directory */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini log_debug("Failed to add directory %s/%s: %s", d->path, e->name, strerror(-r));
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini b = j->current_invalidate_counter != j->last_invalidate_counter;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->last_invalidate_counter = j->current_invalidate_counter;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX] _alignas_(struct inotify_event);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini bool got_something = false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (l < 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return got_something ? determine_change(j) : SD_JOURNAL_NOP;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini while (l > 0) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems process_inotify_event(j, e);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return determine_change(j);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (j->inotify_fd < 0) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems /* This is the first invocation, hence create the
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * inotify watch */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* The journal might have changed since the context
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems * object was created and we weren't watching before,
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini * hence don't wait for anything, and return
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * immediately. */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } while (r == -EINTR);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini bool first = true;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r == 0)
eae72b5b807baa9116e64502cbb278edf15f3146Sebastien Roy_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r == 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini bool newline = false;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems free(j->unique_field);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems j->unique_file = NULL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini const void *odata;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* Proceed to next data object in list the field's linked list */
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (j->unique_offset == 0) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->unique_offset = le64toh(o->data.next_field_offset);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems /* We reached the end of the list? Then start again, with the next file */
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (j->unique_offset == 0) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems n = hashmap_next(j->files, j->unique_file->path);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems /* We do not use the type context here, but 0 instead,
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems * so that we can look at this data object at the same
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems * time as one on another file */
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = journal_file_move_to_object(j->unique_file, 0, j->unique_offset, &o);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Let's do the type check by hand, since we used 0 context above. */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = return_data(j->unique_file, o, &odata, &ol);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* OK, now let's see if we already returned this data
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan * object by checking if it exists in the earlier
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan * traversed files. */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Skip this file it didn't have any fields
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) &&
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), &oo, &op);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = return_data(j->unique_file, o, data, l);