sd-journal.c revision 115646c789d84ea182c5eab6b0945263e3c49305
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc/***
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc This file is part of systemd.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Copyright 2011 Lennart Poettering
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
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
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
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/>.
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc***/
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include <errno.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include <fcntl.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include <stddef.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include <unistd.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include <sys/inotify.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include <sys/poll.h>
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include "sd-journal.h"
d62bc4badc1c1f1549c961cfb8b420e650e1272byz#include "journal-def.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh#include "journal-file.h"
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc#include "hashmap.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh#include "list.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh#include "path-util.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh#include "lookup3.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh#include "compress.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh#include "journal-internal.h"
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f595a68a3b8953a12aa778c2abd7642df8da8c3ayz#define JOURNAL_FILES_MAX 1024
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void detach_location(sd_journal *j) {
f595a68a3b8953a12aa778c2abd7642df8da8c3ayz Iterator i;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini j->current_file = NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini j->current_field = 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini HASHMAP_FOREACH(f, j->files, i)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan f->current_offset = 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsstatic void reset_location(sd_journal *j) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems assert(j);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz detach_location(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz zero(j->current_location);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(l);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(f);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(o->object.type == OBJECT_ENTRY);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l->type = type;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini l->seqnum = le64toh(o->entry.seqnum);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems l->seqnum_id = f->header->seqnum_id;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh l->realtime = le64toh(o->entry.realtime);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz l->monotonic = le64toh(o->entry.monotonic);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz l->boot_id = o->entry.boot_id;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz l->xor_hash = le64toh(o->entry.xor_hash);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic void set_location(sd_journal *j, LocationType type, JournalFile *f, Object *o, uint64_t offset) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(j);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(f);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(o);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini init_location(&j->current_location, type, f, o);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->current_file = f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_field = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz f->current_offset = offset;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic int match_is_valid(const void *data, size_t size) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini const char *b, *p;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(data);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (size < 2)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (startswith(data, "__"))
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini b = data;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini for (p = b; p < b + size; p++) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (*p == '=')
3fd94f8c011031b38162a1db3b554de4371c167fam return p > b;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (*p == '_')
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan continue;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (*p >= 'A' && *p <= 'Z')
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan continue;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (*p >= '0' && *p <= '9')
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan continue;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return false;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return false;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz const uint8_t *a = _a, *b = _b;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz size_t j;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems for (j = 0; j < s && j < t; j++) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (a[j] != b[j])
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (a[j] == '=')
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return true;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return true;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic Match *match_new(Match *p, MatchType t) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan Match *m;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m = new0(Match, 1);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (!m)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz m->type = t;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (p) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz m->parent = p;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LIST_PREPEND(Match, matches, p->matches, m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return m;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void match_free(Match *m) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz while (m->matches)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz match_free(m->matches);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->parent)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LIST_REMOVE(Match, matches, m->parent->matches, m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(m->data);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic void match_free_if_empty(Match *m) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->matches)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz match_free(m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Match *l2, *l3, *add_here = NULL, *m;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz le64_t le_hash;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!data)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (size == 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz size = strlen(data);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!match_is_valid(data, size))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* level 0: OR term
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * level 1: AND terms
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * level 2: OR terms
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * level 3: concrete matches */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->level0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->level0 = match_new(NULL, MATCH_OR_TERM);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->level0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->level1) {
3fd94f8c011031b38162a1db3b554de4371c167fam j->level1 = match_new(j->level0, MATCH_AND_TERM);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j->level1)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -ENOMEM;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan assert(j->level0->type == MATCH_OR_TERM);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan assert(j->level1->type == MATCH_AND_TERM);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan le_hash = htole64(hash64(data, size));
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan LIST_FOREACH(matches, l2, j->level1->matches) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan assert(l2->type == MATCH_OR_TERM);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan LIST_FOREACH(matches, l3, l2->matches) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan assert(l3->type == MATCH_DISCRETE);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Exactly the same match already? Then ignore
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan * this addition */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (l3->le_hash == le_hash &&
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan l3->size == size &&
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan memcmp(l3->data, data, size) == 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
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 = l2;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan break;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (add_here)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan break;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!add_here) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan add_here = match_new(j->level1, MATCH_OR_TERM);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!add_here)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan goto fail;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m = match_new(add_here, MATCH_DISCRETE);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!m)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan goto fail;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m->le_hash = le_hash;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m->size = size;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m->data = memdup(data, size);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!m->data)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan goto fail;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini detach_location(j);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowminifail:
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (add_here)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini match_free_if_empty(add_here);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (j->level1)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan match_free_if_empty(j->level1);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (j->level0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan match_free_if_empty(j->level0);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -ENOMEM;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ int sd_journal_add_disjunction(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems Match *m;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(j);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j->level0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!j->level1)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!j->level1->matches)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini m = match_new(j->level0, MATCH_AND_TERM);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!m)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -ENOMEM;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini j->level1 = m;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic char *match_make_string(Match *m) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan char *p, *r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan Match *i;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini bool enclose = false;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!m)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return strdup("");
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (m->type == MATCH_DISCRETE)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return strndup(m->data, m->size);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini p = NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini LIST_FOREACH(matches, i, m->matches) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini char *t, *k;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini t = match_make_string(i);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!t) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(p);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (p) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t, NULL);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(p);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(t);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!k)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini p = k;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini enclose = true;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini free(p);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz p = t;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (enclose) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = strjoin("(", p, ")", NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(p);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return p;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowminichar *journal_make_match_string(sd_journal *j) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return match_make_string(j->level0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ void sd_journal_flush_matches(sd_journal *j) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->level0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz match_free(j->level0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->level0 = j->level1 = NULL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz detach_location(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int compare_entry_order(JournalFile *af, Object *_ao,
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini JournalFile *bf, uint64_t bp) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini uint64_t a, b;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems Object *ao, *bo;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(af);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini assert(bf);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh assert(_ao);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
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));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini memcpy(ao, _ao, offsetof(EntryObject, items));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini r = journal_file_move_to_object(bf, OBJECT_ENTRY, bp, &bo);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return strcmp(af->path, bf->path);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* We operate on two different files here, hence we can access
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * two objects at the same time, which we normally can't.
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems * If contents and timestamps match, these entries are
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * identical, even if the seqnum does not match */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
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 return 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (sd_id128_equal(af->header->seqnum_id, bf->header->seqnum_id)) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* If this is from the same seqnum source, compare
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * seqnums */
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini a = le64toh(ao->entry.seqnum);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini b = le64toh(bo->entry.seqnum);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (a < b)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -1;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (a > b)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 1;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
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 }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (sd_id128_equal(ao->entry.boot_id, bo->entry.boot_id)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* If the boot id matches compare monotonic time */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini a = le64toh(ao->entry.monotonic);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems b = le64toh(bo->entry.monotonic);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (a < b)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return -1;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (a > b)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 1;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* Otherwise compare UTC time */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini a = le64toh(ao->entry.realtime);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems b = le64toh(bo->entry.realtime);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (a < b)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return -1;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (a > b)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 1;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Finally, compare by contents */
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini a = le64toh(ao->entry.xor_hash);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini b = le64toh(bo->entry.xor_hash);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (a < b)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -1;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (a > b)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 1;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic int compare_with_location(JournalFile *af, Object *ao, Location *l) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems uint64_t a;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(af);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini assert(ao);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(l);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems assert(l->type == LOCATION_DISCRETE || l->type == LOCATION_SEEK);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (l->monotonic_set &&
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini sd_id128_equal(ao->entry.boot_id, l->boot_id) &&
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l->realtime_set &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems le64toh(ao->entry.realtime) == l->realtime &&
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l->xor_hash_set &&
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini le64toh(ao->entry.xor_hash) == l->xor_hash)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (l->seqnum_set &&
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc sd_id128_equal(af->header->seqnum_id, l->seqnum_id)) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan a = le64toh(ao->entry.seqnum);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (a < l->seqnum)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -1;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (a > l->seqnum)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (l->monotonic_set &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_id128_equal(ao->entry.boot_id, l->boot_id)) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz a = le64toh(ao->entry.monotonic);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (a < l->monotonic)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return -1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a > l->monotonic)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (l->realtime_set) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz a = le64toh(ao->entry.realtime);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (a < l->realtime)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a > l->realtime)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (l->xor_hash_set) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz a = le64toh(ao->entry.xor_hash);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a < l->xor_hash)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (a > l->xor_hash)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int next_for_match(
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_journal *j,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Match *m,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t after_offset,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc direction_t direction,
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh Object **ret,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t *offset) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc int r;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc uint64_t np = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Object *n;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(m);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc assert(f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->type == MATCH_DISCRETE) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t dp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
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 r;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc } else if (m->type == MATCH_OR_TERM) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Match *i;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* Find the earliest match beyond after_offset */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LIST_FOREACH(matches, i, m->matches) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t cp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini else if (r > 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini np = cp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (m->type == MATCH_AND_TERM) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Match *i;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini bool continue_looking;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Always jump to the next matching entry and repeat
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini * this until we fine and offset that matches for all
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * matches. */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!m->matches)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh np = 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh do {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue_looking = false;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LIST_FOREACH(matches, i, m->matches) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t cp, limit;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (np == 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz limit = after_offset;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems else if (direction == DIRECTION_DOWN)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems limit = MAX(np, after_offset);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini else
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini limit = MIN(np, after_offset);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems r = next_for_match(j, i, f, limit, direction, NULL, &cp);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (r <= 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return r;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
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 np = cp;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems continue_looking = true;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini } while (continue_looking);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (np == 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return 0;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ret)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ret = n;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (offset)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *offset = np;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int find_location_for_match(
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_journal *j,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Match *m,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz direction_t direction,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Object **ret,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t *offset) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(m);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->type == MATCH_DISCRETE) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t dp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
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 r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* FIXME: missing: find by monotonic */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.type == LOCATION_HEAD)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (j->current_location.type == LOCATION_TAIL)
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 if (j->current_location.monotonic_set) {
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)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (j->current_location.realtime_set)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (m->type == MATCH_OR_TERM) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini uint64_t np = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Object *n;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Match *i;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* Find the earliest match */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LIST_FOREACH(matches, i, m->matches) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t cp;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = find_location_for_match(j, i, f, direction, NULL, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz else if (r > 0) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz np = cp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (np == 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (ret)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ret = n;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (offset)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *offset = np;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc } else {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Match *i;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t np = 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(m->type == MATCH_AND_TERM);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* First jump to the last match, and then find the
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc * next one where all matches match */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!m->matches)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz LIST_FOREACH(matches, i, m->matches) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t cp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = find_location_for_match(j, i, f, direction, NULL, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (np == 0 || (direction == DIRECTION_DOWN ? np < cp : np > cp))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz np = cp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return next_for_match(j, m, f, np, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int find_location_with_matches(
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_journal *j,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc direction_t direction,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Object **ret,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t *offset) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc int r;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(f);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc assert(ret);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->level0) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc /* No matches is simple */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.type == LOCATION_HEAD)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.type == LOCATION_TAIL)
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);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.monotonic_set) {
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)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.realtime_set)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry(f, NULL, 0, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return find_location_for_match(j, j->level0, f, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic int next_with_matches(
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini sd_journal *j,
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini JournalFile *f,
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini direction_t direction,
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems Object **ret,
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini uint64_t *offset) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini Object *c;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini uint64_t cp;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(f);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc assert(ret);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz c = *ret;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz cp = *offset;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* No matches is easy. We simple advance the file
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * pointer by one. */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->level0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return journal_file_next_entry(f, c, cp, direction, ret, offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
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 Microsystems}
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
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 Object *c;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems uint64_t cp;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems int r;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems assert(j);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems assert(f);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (f->current_offset > 0) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems cp = f->current_offset;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = journal_file_move_to_object(f, OBJECT_ENTRY, cp, &c);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (r < 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return r;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = next_with_matches(j, f, direction, &c, &cp);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (r <= 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return r;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems } else {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = find_location_with_matches(j, f, direction, &c, &cp);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
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
13a55820e952b584554bc6b9d4e9303052a2cf29ar for (;;) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz bool found;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_location.type == LOCATION_DISCRETE) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc int k;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini k = compare_with_location(f, c, &j->current_location);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (direction == DIRECTION_DOWN)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems found = k > 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini else
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini found = k < 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini } else
d62bc4badc1c1f1549c961cfb8b420e650e1272byz found = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (found) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ret)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ret = c;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (offset)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems *offset = cp;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = next_with_matches(j, f, direction, &c, &cp);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int real_journal_next(sd_journal *j, direction_t direction) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f, *new_file = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t new_offset = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Object *o;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t p;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Iterator i;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz HASHMAP_FOREACH(f, j->files, i) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz bool found;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = next_beyond_location(j, f, direction, &o, &p);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r < 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Can't iterate through %s, ignoring: %s", f->path, strerror(-r));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc } else if (r == 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!new_file)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz found = true;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc else {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int k;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc k = compare_entry_order(f, o, new_file, new_offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (direction == DIRECTION_DOWN)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz found = k < 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc else
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc found = k > 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (found) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz new_file = f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz new_offset = p;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!new_file)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz set_location(j, LOCATION_DISCRETE, new_file, o, new_offset);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_next(sd_journal *j) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return real_journal_next(j, DIRECTION_DOWN);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_previous(sd_journal *j) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return real_journal_next(j, DIRECTION_UP);
308903890e892f9c21ee582e3a52fdd67e52870bartem}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int c = 0, r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
308903890e892f9c21ee582e3a52fdd67e52870bartem if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
308903890e892f9c21ee582e3a52fdd67e52870bartem
308903890e892f9c21ee582e3a52fdd67e52870bartem if (skip == 0) {
308903890e892f9c21ee582e3a52fdd67e52870bartem /* If this is not a discrete skip, then at least
308903890e892f9c21ee582e3a52fdd67e52870bartem * resolve the current location */
308903890e892f9c21ee582e3a52fdd67e52870bartem if (j->current_location.type != LOCATION_DISCRETE)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return real_journal_next(j, direction);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
308903890e892f9c21ee582e3a52fdd67e52870bartem return 0;
308903890e892f9c21ee582e3a52fdd67e52870bartem }
308903890e892f9c21ee582e3a52fdd67e52870bartem
308903890e892f9c21ee582e3a52fdd67e52870bartem do {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = real_journal_next(j, direction);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r == 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return c;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini skip--;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems c++;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems } while (skip > 0);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return c;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
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);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return real_journal_next_skip(j, DIRECTION_UP, skip);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Object *o;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems int r;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems char bid[33], sid[33];
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!cursor)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!j->current_file || j->current_file->current_offset <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EADDRNOTAVAIL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems sd_id128_to_string(j->current_file->header->seqnum_id, sid);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc sd_id128_to_string(o->entry.boot_id, bid);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (asprintf(cursor,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx",
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sid, (unsigned long long) le64toh(o->entry.seqnum),
d62bc4badc1c1f1549c961cfb8b420e650e1272byz bid, (unsigned long long) le64toh(o->entry.monotonic),
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (unsigned long long) le64toh(o->entry.realtime),
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (unsigned long long) le64toh(o->entry.xor_hash)) < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 1;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *w, *state;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz size_t l;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long long seqnum, monotonic, realtime, xor_hash;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz bool
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc seqnum_id_set = false,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz seqnum_set = false,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boot_id_set = false,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz monotonic_set = false,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc realtime_set = false,
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc xor_hash_set = false;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_id128_t seqnum_id, boot_id;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (isempty(cursor))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *item;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int k = 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (l < 2 || w[1] != '=')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz item = strndup(w, l);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!item)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz switch (w[0]) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 's':
d62bc4badc1c1f1549c961cfb8b420e650e1272byz seqnum_id_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz k = sd_id128_from_string(item+2, &seqnum_id);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 'i':
d62bc4badc1c1f1549c961cfb8b420e650e1272byz seqnum_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (sscanf(item+2, "%llx", &seqnum) != 1)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc k = -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 'b':
d62bc4badc1c1f1549c961cfb8b420e650e1272byz boot_id_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz k = sd_id128_from_string(item+2, &boot_id);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 'm':
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc monotonic_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (sscanf(item+2, "%llx", &monotonic) != 1)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz k = -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 't':
d62bc4badc1c1f1549c961cfb8b420e650e1272byz realtime_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (sscanf(item+2, "%llx", &realtime) != 1)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz k = -EINVAL;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc break;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini case 'x':
d62bc4badc1c1f1549c961cfb8b420e650e1272byz xor_hash_set = true;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (sscanf(item+2, "%llx", &xor_hash) != 1)
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini k = -EINVAL;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc break;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(item);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (k < 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return k;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((!seqnum_set || !seqnum_id_set) &&
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc (!monotonic_set || !boot_id_set) &&
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems !realtime_set)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems reset_location(j);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_SEEK;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (realtime_set) {
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 }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
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 }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
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;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (xor_hash_set) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_location.xor_hash = (uint64_t) xor_hash;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_location.xor_hash_set = true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems int r;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems char *w, *state;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems size_t l;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems Object *o;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (isempty(cursor))
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j->current_file || j->current_file->current_offset <= 0)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return -EADDRNOTAVAIL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz FOREACH_WORD_SEPARATOR(w, l, cursor, ";", state) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz _cleanup_free_ char *item = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_id128_t id;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz unsigned long long ll;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc int k = 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (l < 2 || w[1] != '=')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz item = strndup(w, l);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!item)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return -ENOMEM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz switch (w[0]) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 's':
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc k = sd_id128_from_string(item+2, &id);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (k < 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return k;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems break;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems case 'i':
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (sscanf(item+2, "%llx", &ll) != 1)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ll != le64toh(o->entry.seqnum))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 'b':
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc k = sd_id128_from_string(item+2, &id);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (k < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return k;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (!sd_id128_equal(id, o->entry.boot_id))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz case 'm':
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (sscanf(item+2, "%llx", &ll) != 1)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (ll != le64toh(o->entry.monotonic))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems break;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems case 't':
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (sscanf(item+2, "%llx", &ll) != 1)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (ll != le64toh(o->entry.realtime))
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini case 'x':
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (sscanf(item+2, "%llx", &ll) != 1)
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (ll != le64toh(o->entry.xor_hash))
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems break;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 1;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc reset_location(j);
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
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems reset_location(j);
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
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_seek_head(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems reset_location(j);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_HEAD;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems_public_ int sd_journal_seek_tail(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!j)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems reset_location(j);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->current_location.type = LOCATION_TAIL;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int add_file(sd_journal *j, const char *prefix, const char *filename) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems char *path;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems int r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(j);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems assert(prefix);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems assert(filename);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((j->flags & SD_JOURNAL_SYSTEM_ONLY) &&
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc !(streq(filename, "system.journal") ||
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 return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems path = strjoin(prefix, "/", filename, NULL);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!path)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (hashmap_get(j->files, path)) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems free(path);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc return 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Too many open journal files, not adding %s, ignoring.", path);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems free(path);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return 0;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems free(path);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc if (r < 0) {
3fd94f8c011031b38162a1db3b554de4371c167fam if (errno == ENOENT)
149b7eb2919570a01d698963b7ab14d45b4edda8Sowmini Varadhan return 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* journal_file_dump(f); */
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc r = hashmap_put(j->files, f->path, f);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (r < 0) {
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc journal_file_close(f);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_invalidate_counter ++;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("File %s got added.", f->path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int remove_file(sd_journal *j, const char *prefix, const char *filename) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *path;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(j);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc assert(prefix);
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc assert(filename);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems path = strjoin(prefix, "/", filename, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!path)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc f = hashmap_get(j->files, path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!f)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc hashmap_remove(j->files, f->path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc log_debug("File %s got removed.", f->path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->current_file == f) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_file = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_field = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->unique_file == f) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->unique_file = NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->unique_offset = 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz journal_file_close(f);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_invalidate_counter ++;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxcstatic int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz char *path;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc DIR *d;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_id128_t id, mid;
0ba2cbe97e0678a691742f98d2532caed0a2c4aaxc Directory *m;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(prefix);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz assert(dirname);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
d62bc4badc1c1f1549c961cfb8b420e650e1272byz (sd_id128_from_string(dirname, &id) < 0 ||
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh sd_id128_get_machine(&mid) < 0 ||
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini !sd_id128_equal(id, mid)))
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini return 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz path = strjoin(prefix, "/", dirname, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!path)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz d = opendir(path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!d) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Failed to open %s: %m", path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (errno == ENOENT)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -errno;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems m = hashmap_get(j->directories_by_path, path);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!m) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems m = new0(Directory, 1);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (!m) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems closedir(d);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems free(path);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -ENOMEM;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems m->is_root = false;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh m->path = path;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems closedir(d);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems free(m->path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh free(m);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -ENOMEM;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->current_invalidate_counter ++;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Directory %s got added.", m->path);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (m->is_root) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh free (path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz closedir(d);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh free(path);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (m->wd <= 0 && j->inotify_fd >= 0) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh m->wd = inotify_add_watch(j->inotify_fd, m->path,
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
d62bc4badc1c1f1549c961cfb8b420e650e1272byz IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
d62bc4badc1c1f1549c961cfb8b420e650e1272byz IN_ONLYDIR);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz inotify_rm_watch(j->inotify_fd, m->wd);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh for (;;) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz struct dirent *de;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz union dirent_storage buf;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = readdir_r(d, &buf.de, &de);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r != 0 || !de)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh break;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (dirent_is_file_with_suffix(de, ".journal") ||
d62bc4badc1c1f1549c961cfb8b420e650e1272byz dirent_is_file_with_suffix(de, ".journal~")) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = add_file(j, m->path, de->d_name);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Failed to add file %s/%s: %s", m->path, de->d_name, strerror(-r));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
d62bc4badc1c1f1549c961cfb8b420e650e1272byz closedir(d);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsstatic int add_root_directory(sd_journal *j, const char *p) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems DIR *d;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh Directory *m;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems int r;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems assert(j);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems assert(p);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems !path_startswith(p, "/run"))
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
d62bc4badc1c1f1549c961cfb8b420e650e1272byz d = opendir(p);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!d)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return -errno;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini m = hashmap_get(j->directories_by_path, p);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!m) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems m = new0(Directory, 1);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!m) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems closedir(d);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return -ENOMEM;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini }
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini m->is_root = true;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini m->path = strdup(p);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (!m->path) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini closedir(d);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini free(m);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini return -ENOMEM;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini }
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini closedir(d);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini free(m->path);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini free(m);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -ENOMEM;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->current_invalidate_counter ++;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Root directory %s got added.", m->path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz } else if (!m->is_root) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz closedir(d);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (m->wd <= 0 && j->inotify_fd >= 0) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan m->wd = inotify_add_watch(j->inotify_fd, m->path,
d62bc4badc1c1f1549c961cfb8b420e650e1272byz IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
d62bc4badc1c1f1549c961cfb8b420e650e1272byz IN_ONLYDIR);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz inotify_rm_watch(j->inotify_fd, m->wd);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan for (;;) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz struct dirent *de;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan union dirent_storage buf;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh sd_id128_t id;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh r = readdir_r(d, &buf.de, &de);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r != 0 || !de)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh break;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (dirent_is_file_with_suffix(de, ".journal") ||
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh dirent_is_file_with_suffix(de, ".journal~")) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = add_file(j, m->path, de->d_name);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh log_debug("Failed to add file %s/%s: %s", m->path, de->d_name, strerror(-r));
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini } else if ((de->d_type == DT_DIR || de->d_type == DT_LNK || de->d_type == DT_UNKNOWN) &&
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini sd_id128_from_string(de->d_name, &id) >= 0) {
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini r = add_directory(j, m->path, de->d_name);
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz log_debug("Failed to add directory %s/%s: %s", m->path, de->d_name, strerror(-r));
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz closedir(d);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic int remove_directory(sd_journal *j, Directory *d) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh assert(j);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (d->wd > 0) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (j->inotify_fd >= 0)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh inotify_rm_watch(j->inotify_fd, d->wd);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz hashmap_remove(j->directories_by_path, d->path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (d->is_root)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh log_debug("Root directory %s got removed.", d->path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh else
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh log_debug("Directory %s got removed.", d->path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(d->path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(d);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dhstatic int add_search_paths(sd_journal *j) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz const char search_paths[] =
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "/run/log/journal\0"
d62bc4badc1c1f1549c961cfb8b420e650e1272byz "/var/log/journal\0";
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh const char *p;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems assert(j);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz /* We ignore most errors here, since the idea is to only open
d62bc4badc1c1f1549c961cfb8b420e650e1272byz * what's actually accessible, and ignore the rest. */
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz NULSTR_FOREACH(p, search_paths)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz add_root_directory(j, p);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dhstatic int allocate_inotify(sd_journal *j) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh assert(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->inotify_fd < 0) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (j->inotify_fd < 0)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -errno;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->directories_by_wd) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->directories_by_wd = hashmap_new(trivial_hash_func, trivial_compare_func);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->directories_by_wd)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -ENOMEM;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic sd_journal *journal_new(int flags, const char *path) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems sd_journal *j;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j = new0(sd_journal, 1);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->inotify_fd = -1;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->flags = flags;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (path) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->path = strdup(path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j->path) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh free(j);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return NULL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->files = hashmap_new(string_hash_func, string_compare_func);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->files) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(j->path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(j);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return NULL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->directories_by_path = hashmap_new(string_hash_func, string_compare_func);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!j->directories_by_path) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh hashmap_free(j->files);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan free(j->path);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh free(j);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return NULL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh j->mmap = mmap_cache_new();
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j->mmap) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz hashmap_free(j->files);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini hashmap_free(j->directories_by_path);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems free(j->path);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems free(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return NULL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return j;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ int sd_journal_open(sd_journal **ret, int flags) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan sd_journal *j;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!ret)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh SD_JOURNAL_RUNTIME_ONLY|
d62bc4badc1c1f1549c961cfb8b420e650e1272byz SD_JOURNAL_SYSTEM_ONLY))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j = journal_new(flags, NULL);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -ENOMEM;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = add_search_paths(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ret = j;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystemsfail:
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems sd_journal_close(j);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_journal *j;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!ret)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!path || !path_is_absolute(path))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (flags != 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz j = journal_new(flags, path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -ENOMEM;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh r = add_root_directory(j, path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz goto fail;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan *ret = j;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzfail:
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_journal_close(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ void sd_journal_close(sd_journal *j) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Directory *d;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh while ((f = hashmap_steal_first(j->files)))
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh journal_file_close(f);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh hashmap_free(j->files);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan while ((d = hashmap_first(j->directories_by_path)))
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh remove_directory(j, d);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan while ((d = hashmap_first(j->directories_by_wd)))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz remove_directory(j, d);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini hashmap_free(j->directories_by_path);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz hashmap_free(j->directories_by_wd);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (j->inotify_fd >= 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems close_nointr_nofail(j->inotify_fd);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_journal_flush_matches(j);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (j->mmap)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz mmap_cache_unref(j->mmap);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan free(j->path);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan free(j->unique_field);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz free(j);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz Object *o;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!ret)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz f = j->current_file;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!f)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EADDRNOTAVAIL;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (f->current_offset <= 0)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return -EADDRNOTAVAIL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh *ret = le64toh(o->entry.realtime);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh Object *o;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz int r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz sd_id128_t id;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz f = j->current_file;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!f)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -EADDRNOTAVAIL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (f->current_offset <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EADDRNOTAVAIL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (ret_boot_id)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz *ret_boot_id = o->entry.boot_id;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz else {
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh r = sd_id128_get_boot(&id);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!sd_id128_equal(id, o->entry.boot_id))
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -ESTALE;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (ret)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh *ret = le64toh(o->entry.monotonic);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return 0;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byzstatic bool field_is_valid(const char *field) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz const char *p;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh assert(field);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (isempty(field))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return false;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (startswith(field, "__"))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return false;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (p = field; *p; p++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (*p == '_')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (*p >= 'A' && *p <= 'Z')
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (*p >= '0' && *p <= '9')
d62bc4badc1c1f1549c961cfb8b420e650e1272byz continue;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return false;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh }
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return true;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz}
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz JournalFile *f;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t i, n;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh size_t field_length;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh int r;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh Object *o;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!j)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh if (!field)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!data)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!size)
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh return -EINVAL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!field_is_valid(field))
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EINVAL;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz f = j->current_file;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (!f)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EADDRNOTAVAIL;
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (f->current_offset <= 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return -EADDRNOTAVAIL;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz if (r < 0)
d62bc4badc1c1f1549c961cfb8b420e650e1272byz return r;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
d62bc4badc1c1f1549c961cfb8b420e650e1272byz field_length = strlen(field);
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh n = journal_file_entry_n_items(o);
d62bc4badc1c1f1549c961cfb8b420e650e1272byz for (i = 0; i < n; i++) {
d62bc4badc1c1f1549c961cfb8b420e650e1272byz uint64_t p, l;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz le64_t le_hash;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz size_t t;
d62bc4badc1c1f1549c961cfb8b420e650e1272byz
f4b3ec61df05330d25f55a36b975b4d7519fdeb1dh p = le64toh(o->entry.items[i].object_offset);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini le_hash = o->entry.items[i].hash;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (le_hash != o->data.hash)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EBADMSG;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l = le64toh(o->object.size) - offsetof(Object, data.payload);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (o->object.flags & OBJECT_COMPRESSED) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini#ifdef HAVE_XZ
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (uncompress_startswith(o->data.payload, l,
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan &f->compress_buffer, &f->compress_buffer_size,
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan field, field_length, '=')) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan uint64_t rsize;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!uncompress_blob(o->data.payload, l,
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan &f->compress_buffer, &f->compress_buffer_size, &rsize))
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EBADMSG;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan *data = f->compress_buffer;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *size = (size_t) rsize;
3fd94f8c011031b38162a1db3b554de4371c167fam
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan#else
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EPROTONOSUPPORT;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini#endif
3fd94f8c011031b38162a1db3b554de4371c167fam
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (l >= field_length+1 &&
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini memcmp(o->data.payload, field, field_length) == 0 &&
3fd94f8c011031b38162a1db3b554de4371c167fam o->data.payload[field_length] == '=') {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini t = (size_t) l;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if ((uint64_t) t != l)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -E2BIG;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *data = o->data.payload;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini *size = t;
3fd94f8c011031b38162a1db3b554de4371c167fam
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini return 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -ENOENT;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhanstatic int return_data(JournalFile *f, Object *o, const void **data, size_t *size) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan size_t t;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan uint64_t l;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan l = le64toh(o->object.size) - offsetof(Object, data.payload);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan t = (size_t) l;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* We can't read objects larger than 4G on a 32bit machine */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if ((uint64_t) t != l)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -E2BIG;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (o->object.flags & OBJECT_COMPRESSED) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan#ifdef HAVE_XZ
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan uint64_t rsize;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!uncompress_blob(o->data.payload, l, &f->compress_buffer, &f->compress_buffer_size, &rsize))
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EBADMSG;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan *data = f->compress_buffer;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *size = (size_t) rsize;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini#else
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EPROTONOSUPPORT;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini#endif
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else {
3fd94f8c011031b38162a1db3b554de4371c167fam *data = o->data.payload;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *size = t;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini JournalFile *f;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini uint64_t p, n;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini le64_t le_hash;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini Object *o;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!data)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!size)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini f = j->current_file;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!f)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EADDRNOTAVAIL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (f->current_offset <= 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EADDRNOTAVAIL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini n = journal_file_entry_n_items(o);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (j->current_field >= n)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
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);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (r < 0)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return r;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (le_hash != o->data.hash)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EBADMSG;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = return_data(f, o, data, size);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (r < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->current_field ++;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 1;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ void sd_journal_restart_data(sd_journal *j) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini j->current_field = 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini_public_ int sd_journal_get_fd(sd_journal *j) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
3fd94f8c011031b38162a1db3b554de4371c167fam
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (j->inotify_fd >= 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return j->inotify_fd;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3fd94f8c011031b38162a1db3b554de4371c167fam r = allocate_inotify(j);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (r < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return r;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini /* Iterate through all dirs again, to add them to the
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * inotify */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (j->path)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = add_root_directory(j, j->path);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini else
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = add_search_paths(j);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (r < 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return j->inotify_fd;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystemsstatic void process_inotify_event(sd_journal *j, struct inotify_event *e) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems Directory *d;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini assert(j);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini assert(e);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Is this a subdirectory we watch? */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (d) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini sd_id128_t id;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!(e->mask & IN_ISDIR) && e->len > 0 &&
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems (endswith(e->name, ".journal") ||
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini endswith(e->name, ".journal~"))) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Event for a journal file */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = add_file(j, d->path, e->name);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Failed to add file %s/%s: %s", d->path, e->name, strerror(-r));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = remove_file(j, d->path, e->name);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Failed to remove file %s/%s: %s", d->path, e->name, strerror(-r));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (!d->is_root && e->len == 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Event for a subdirectory */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = remove_directory(j, d);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_debug("Failed to remove directory %s: %s", d->path, strerror(-r));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini /* Event for root directory */
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = add_directory(j, d->path, e->name);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini log_debug("Failed to add directory %s/%s: %s", d->path, e->name, strerror(-r));
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini }
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini }
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini return;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini }
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini if (e->mask & IN_IGNORED)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini log_warning("Unknown inotify event.");
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowministatic int determine_change(sd_journal *j) {
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems bool b;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems assert(j);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini b = j->current_invalidate_counter != j->last_invalidate_counter;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->last_invalidate_counter = j->current_invalidate_counter;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
3fd94f8c011031b38162a1db3b554de4371c167fam return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini_public_ int sd_journal_process(sd_journal *j) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini uint8_t buffer[sizeof(struct inotify_event) + FILENAME_MAX] _alignas_(struct inotify_event);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini bool got_something = false;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini for (;;) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini struct inotify_event *e;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini ssize_t l;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini l = read(j->inotify_fd, buffer, sizeof(buffer));
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (l < 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (errno == EAGAIN || errno == EINTR)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return got_something ? determine_change(j) : SD_JOURNAL_NOP;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -errno;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini got_something = true;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini e = (struct inotify_event*) buffer;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini while (l > 0) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini size_t step;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems process_inotify_event(j, e);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini step = sizeof(struct inotify_event) + e->len;
3fd94f8c011031b38162a1db3b554de4371c167fam assert(step <= (size_t) l);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini e = (struct inotify_event*) ((uint8_t*) e + step);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems l -= step;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return determine_change(j);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini assert(j);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems if (j->inotify_fd < 0) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems /* This is the first invocation, hence create the
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini * inotify watch */
3fd94f8c011031b38162a1db3b554de4371c167fam r = sd_journal_get_fd(j);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
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 return determine_change(j);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems do {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } while (r == -EINTR);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
6b9e797c2ea518518cb2b57895991d8bdaa167fesowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return sd_journal_process(j);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems}
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini Iterator i;
3fd94f8c011031b38162a1db3b554de4371c167fam JournalFile *f;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini bool first = true;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems return -EINVAL;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (!from && !to)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini HASHMAP_FOREACH(f, j->files, i) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini usec_t fr, t;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r == -ENOENT)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini continue;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r == 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini continue;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (first) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (from)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *from = fr;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (to)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems *to = t;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini first = false;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini } else {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (from)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *from = MIN(fr, *from);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (to)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *to = MIN(t, *to);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return first ? 0 : 1;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
eae72b5b807baa9116e64502cbb278edf15f3146Sebastien Roy_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
3fd94f8c011031b38162a1db3b554de4371c167fam Iterator i;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini JournalFile *f;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan bool first = true;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini int r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!from && !to)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
3fd94f8c011031b38162a1db3b554de4371c167fam
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini HASHMAP_FOREACH(f, j->files, i) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini usec_t fr, t;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r == -ENOENT)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini continue;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (r == 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini continue;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (first) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (from)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini *from = fr;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (to)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini *to = t;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini first = false;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan } else {
3fd94f8c011031b38162a1db3b554de4371c167fam if (from)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *from = MIN(fr, *from);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (to)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *to = MIN(t, *to);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return first ? 0 : 1;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowminivoid journal_print_header(sd_journal *j) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini Iterator i;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini JournalFile *f;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini bool newline = false;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini assert(j);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini HASHMAP_FOREACH(f, j->files, i) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (newline)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini putchar('\n');
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini else
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini newline = true;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan journal_file_print_header(f);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini Iterator i;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini JournalFile *f;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini uint64_t sum = 0;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!bytes)
3fd94f8c011031b38162a1db3b554de4371c167fam return -EINVAL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini HASHMAP_FOREACH(f, j->files, i) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini struct stat st;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (fstat(f->fd, &st) < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -errno;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini sum += (uint64_t) st.st_blocks * 512ULL;
3fd94f8c011031b38162a1db3b554de4371c167fam }
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini *bytes = sum;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return 0;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini}
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini char *f;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!j)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (isempty(field))
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EINVAL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini f = strdup(field);
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!f)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -ENOMEM;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems free(j->unique_field);
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems j->unique_field = f;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems j->unique_file = NULL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems j->unique_offset = 0;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return 0;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems}
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems Object *o;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems size_t k;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems int r;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!j)
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems return -EINVAL;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems if (!data)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return -EINVAL;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini if (!l)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return -EINVAL;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (!j->unique_field)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return -EINVAL;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini k = strlen(j->unique_field);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (!j->unique_file) {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->unique_file = hashmap_first(j->files);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (!j->unique_file)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->unique_offset = 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini }
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini for (;;) {
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems JournalFile *of;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini Iterator i;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini const void *odata;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini size_t ol;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini bool found;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
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 if (r < 0)
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini return r;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini } else {
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (r < 0)
13a55820e952b584554bc6b9d4e9303052a2cf29ar return r;
13a55820e952b584554bc6b9d4e9303052a2cf29ar
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems j->unique_offset = le64toh(o->data.next_field_offset);
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems }
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
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 JournalFile *n;
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems
3bc21d0a9c7b31b1132c254e389a4114c23bcf00Aruna Ramakrishna - Sun Microsystems n = hashmap_next(j->files, j->unique_file->path);
13a55820e952b584554bc6b9d4e9303052a2cf29ar if (!n)
3fd94f8c011031b38162a1db3b554de4371c167fam return 0;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini j->unique_file = n;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems continue;
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems }
afdda45f890ee5dfc86e5131a30b11b354d51633Vasumathi Sundaram - Sun Microsystems
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);
4045d94132614e1de2073685a6cdd4fbd86bec33sowmini if (r < 0)
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini return r;
e7801d59e8ceda0cde8ebdfdddd7582ee2ea96efsowmini
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Let's do the type check by hand, since we used 0 context above. */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (o->object.type != OBJECT_DATA)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return -EBADMSG;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = return_data(j->unique_file, o, &odata, &ol);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (r < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
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 found = false;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan HASHMAP_FOREACH(of, j->files, i) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan Object *oo;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan uint64_t op;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (of == j->unique_file)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan break;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan /* Skip this file it didn't have any fields
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan * indexed */
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) &&
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan le64toh(of->header->n_fields) <= 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan continue;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), &oo, &op);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (r < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (r > 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan found = true;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (found)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan continue;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan r = return_data(j->unique_file, o, data, l);
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (r < 0)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return r;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return 1;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan }
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan_public_ void sd_journal_restart_unique(sd_journal *j) {
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan if (!j)
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan return;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->unique_file = NULL;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan j->unique_offset = 0;
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan}
bcb5c89da22515e2ccf139578bad3caebcd716adSowmini Varadhan