sd-journal.c revision de7b95cdc3228131498021c2fdcf6647004c3920
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <stddef.h>
#include "sd-journal.h"
#include "journal-def.h"
#include "journal-file.h"
#include "hashmap.h"
#include "list.h"
#include "lookup3.h"
struct Match {
char *data;
};
struct sd_journal {
unsigned n_matches;
};
Match *m;
assert(j);
if (size <= 0)
return -EINVAL;
if (!m)
return -ENOMEM;
if (!m->data) {
free(m);
return -ENOMEM;
}
j->n_matches ++;
return 0;
}
void sd_journal_flush_matches(sd_journal *j) {
assert(j);
while (j->matches) {
free(m);
}
j->n_matches = 0;
}
uint64_t a, b;
/* We operate on two different files here, hence we can access
* two objects at the same time, which we normally can't.
*
* If contents and timestamps match, these entries are
* identical, even if the seqnum does not match */
return 0;
/* If this is from the same seqnum source, compare
* seqnums */
if (a < b)
return -1;
if (a > b)
return 1;
/* Wow! This is weird, different data but the same
* seqnums? Something is borked, but let's make the
* best of it and compare by time. */
}
/* If the boot id matches compare monotonic time */
if (a < b)
return -1;
if (a > b)
return 1;
}
/* Otherwise compare UTC time */
if (a < b)
return -1;
if (a > b)
return 1;
/* Finally, compare by contents */
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
int r;
Object *c;
assert(j);
assert(f);
assert(o);
assert(p);
if (!j->matches) {
/* No matches is easy, just go on to the next entry */
if (f->current_offset > 0) {
if (r < 0)
return r;
} else
c = NULL;
return journal_file_next_entry(f, c, o, p);
}
/* So there are matches we have to adhere to, let's find the
* first entry that matches all of them */
if (f->current_offset > 0)
cp = f->current_offset;
else {
if (r <= 0)
return r;
/* We can shortcut this if there's only one match */
if (j->n_matches == 1) {
*o = c;
*p = cp;
return r;
}
}
for (;;) {
bool found;
Match *m;
if (r < 0)
return r;
n = journal_file_entry_n_items(c);
/* Make sure we don't match the entry we are starting
* from. */
np = 0;
uint64_t q, k;
for (k = 0; k < n; k++)
break;
if (k >= n) {
/* Hmm, didn't find any field that matched, so ignore
* this match. Go on with next match */
found = false;
continue;
}
/* Hmm, so, this field matched, let's remember
* where we'd have to try next, in case the other
* matches are not OK */
if (q > np)
np = q;
}
/* Did this entry match against all matches? */
if (found) {
*o = c;
*p = cp;
return 1;
}
/* Did we find a subsequent entry? */
if (np == 0)
return 0;
/* Hmm, ok, this entry only matched partially, so
* let's try another one */
}
}
int sd_journal_next(sd_journal *j) {
Iterator i;
int r;
uint64_t new_offset = 0;
assert(j);
HASHMAP_FOREACH(f, j->files, i) {
Object *o;
uint64_t p;
r = move_to_next_with_matches(j, f, &o, &p);
if (r < 0)
return r;
else if (r == 0)
continue;
if (!new_current ||
new_current = f;
new_entry = o;
new_offset = p;
}
}
if (new_current) {
j->current_file = new_current;
j->current_field = 0;
/* Skip over any identical entries in the other files too */
HASHMAP_FOREACH(f, j->files, i) {
Object *o;
uint64_t p;
if (j->current_file == f)
continue;
r = move_to_next_with_matches(j, f, &o, &p);
if (r < 0)
return r;
else if (r == 0)
continue;
f->current_offset = p;
}
return 1;
}
return 0;
}
int sd_journal_previous(sd_journal *j) {
Iterator i;
int r;
uint64_t new_offset = 0;
assert(j);
HASHMAP_FOREACH(f, j->files, i) {
Object *o;
uint64_t p;
if (f->current_offset > 0) {
if (r < 0)
return r;
} else
o = NULL;
r = journal_file_prev_entry(f, o, &o, &p);
if (r < 0)
return r;
else if (r == 0)
continue;
new_current = f;
new_entry = o;
new_offset = p;
}
}
if (new_current) {
j->current_file = new_current;
j->current_field = 0;
/* Skip over any identical entries in the other files too */
HASHMAP_FOREACH(f, j->files, i) {
Object *o;
uint64_t p;
if (j->current_file == f)
continue;
if (f->current_offset > 0) {
if (r < 0)
return r;
} else
o = NULL;
r = journal_file_prev_entry(f, o, &o, &p);
if (r < 0)
return r;
else if (r == 0)
continue;
f->current_offset = p;
}
return 1;
}
return 0;
}
Object *o;
int r;
assert(j);
return -EADDRNOTAVAIL;
r = journal_file_move_to_object(j->current_file, j->current_file->current_offset, OBJECT_ENTRY, &o);
if (r < 0)
return r;
"s=%s;i=%llx;b=%s;m=%llx;t=%llx;x=%llx;p=%s",
return -ENOMEM;
return 1;
}
return -EINVAL;
}
char *fn;
int r;
JournalFile *f;
assert(j);
if (dir)
else
if (!fn)
return -ENOMEM;
if (r < 0) {
return 0;
return r;
}
if (r < 0) {
return r;
}
return 0;
}
char *fn;
int r;
DIR *d;
assert(j);
if (!fn)
return -ENOMEM;
if (!d) {
return 0;
return -errno;
}
for (;;) {
if (r != 0 || !de)
break;
continue;
if (r < 0)
}
closedir(d);
return 0;
}
sd_journal *j;
const char *p;
const char search_paths[] =
int r;
if (!j)
return -ENOMEM;
if (!j->files) {
r = -ENOMEM;
goto fail;
}
/* We ignore most errors here, since the idea is to only open
* what's actually accessible, and ignore the rest. */
NULSTR_FOREACH(p, search_paths) {
DIR *d;
d = opendir(p);
if (!d) {
log_debug("Failed to open %s: %m", p);
continue;
}
for (;;) {
if (r != 0 || !de)
break;
if (r < 0)
if (r < 0)
}
}
closedir(d);
}
*ret = j;
return 0;
fail:
sd_journal_close(j);
return r;
};
void sd_journal_close(sd_journal *j) {
assert(j);
if (j->files) {
JournalFile *f;
while ((f = hashmap_steal_first(j->files)))
hashmap_free(j->files);
}
free(j);
}
Object *o;
JournalFile *f;
int r;
assert(j);
f = j->current_file;
if (!f)
return 0;
if (f->current_offset <= 0)
return 0;
if (r < 0)
return r;
return 1;
}
Object *o;
JournalFile *f;
int r;
assert(j);
f = j->current_file;
if (!f)
return 0;
if (f->current_offset <= 0)
return 0;
r = sd_id128_get_boot(&id);
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
return 1;
}
JournalFile *f;
uint64_t i, n;
int r;
Object *o;
assert(j);
return -EINVAL;
f = j->current_file;
if (!f)
return 0;
if (f->current_offset <= 0)
return 0;
if (r < 0)
return r;
n = journal_file_entry_n_items(o);
for (i = 0; i < n; i++) {
uint64_t p, l, h;
size_t t;
r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
if (r < 0)
return r;
return -EBADMSG;
if (l >= field_length+1 &&
t = (size_t) l;
if ((uint64_t) t != l)
return -E2BIG;
*size = t;
return 1;
}
if (r < 0)
return r;
}
return 0;
}
JournalFile *f;
uint64_t p, l, n, h;
size_t t;
int r;
Object *o;
assert(j);
f = j->current_file;
if (!f)
return 0;
if (f->current_offset <= 0)
return 0;
if (r < 0)
return r;
n = journal_file_entry_n_items(o);
if (j->current_field >= n)
return 0;
r = journal_file_move_to_object(f, p, OBJECT_DATA, &o);
if (r < 0)
return r;
return -EBADMSG;
t = (size_t) l;
/* We can't read objects larger than 4G on a 32bit machine */
if ((uint64_t) t != l)
return -E2BIG;
*size = t;
j->current_field ++;
return 1;
}
int sd_journal_seek_head(sd_journal *j) {
assert(j);
return -EINVAL;
}
int sd_journal_seek_tail(sd_journal *j) {
assert(j);
return -EINVAL;
}