2011-10-07 21:06:39 +02:00
|
|
|
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
This file is part of systemd.
|
|
|
|
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
2012-04-12 00:20:58 +02:00
|
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
2011-10-07 21:06:39 +02:00
|
|
|
(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
|
2012-04-12 00:20:58 +02:00
|
|
|
Lesser General Public License for more details.
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-04-12 00:20:58 +02:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2011-10-07 21:06:39 +02:00
|
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
***/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <linux/magic.h>
|
|
|
|
#include <poll.h>
|
2011-10-14 04:44:50 +02:00
|
|
|
#include <stddef.h>
|
2011-12-19 22:35:46 +01:00
|
|
|
#include <sys/inotify.h>
|
2012-10-26 20:05:19 +02:00
|
|
|
#include <sys/vfs.h>
|
2015-10-24 22:58:24 +02:00
|
|
|
#include <unistd.h>
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
#include "sd-journal.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
|
|
|
|
#include "catalog.h"
|
|
|
|
#include "compress.h"
|
|
|
|
#include "fileio.h"
|
|
|
|
#include "formats-util.h"
|
|
|
|
#include "hashmap.h"
|
|
|
|
#include "hostname-util.h"
|
2011-10-07 21:06:39 +02:00
|
|
|
#include "journal-def.h"
|
2011-10-08 02:20:44 +02:00
|
|
|
#include "journal-file.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "journal-internal.h"
|
2011-10-08 02:20:44 +02:00
|
|
|
#include "list.h"
|
2011-10-15 01:13:37 +02:00
|
|
|
#include "lookup3.h"
|
2012-10-26 20:05:19 +02:00
|
|
|
#include "missing.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "path-util.h"
|
2012-11-15 23:03:31 +01:00
|
|
|
#include "replace-var.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
|
|
|
#include "strv.h"
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2015-01-29 02:10:15 +01:00
|
|
|
#define JOURNAL_FILES_MAX 7168
|
|
|
|
|
2012-10-26 20:05:19 +02:00
|
|
|
#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
|
|
|
|
|
2012-11-15 23:03:31 +01:00
|
|
|
#define REPLACE_VAR_MAX 256
|
|
|
|
|
2012-11-21 00:28:00 +01:00
|
|
|
#define DEFAULT_DATA_THRESHOLD (64*1024)
|
|
|
|
|
2014-02-27 06:11:54 +01:00
|
|
|
static void remove_file_real(sd_journal *j, JournalFile *f);
|
|
|
|
|
2013-06-27 01:55:48 +02:00
|
|
|
static bool journal_pid_changed(sd_journal *j) {
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
/* We don't support people creating a journal object and
|
|
|
|
* keeping it around over a fork(). Let's complain. */
|
|
|
|
|
|
|
|
return j->original_pid != getpid();
|
|
|
|
}
|
|
|
|
|
2013-03-11 23:03:13 +01:00
|
|
|
/* We return an error here only if we didn't manage to
|
|
|
|
memorize the real error. */
|
2013-03-23 01:12:22 +01:00
|
|
|
static int set_put_error(sd_journal *j, int r) {
|
|
|
|
int k;
|
|
|
|
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r >= 0)
|
|
|
|
return r;
|
|
|
|
|
2014-08-13 01:00:18 +02:00
|
|
|
k = set_ensure_allocated(&j->errors, NULL);
|
2013-03-23 01:12:22 +01:00
|
|
|
if (k < 0)
|
|
|
|
return k;
|
|
|
|
|
|
|
|
return set_put(j->errors, INT_TO_PTR(r));
|
2013-03-11 23:03:13 +01:00
|
|
|
}
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
static void detach_location(sd_journal *j) {
|
2011-10-15 02:53:04 +02:00
|
|
|
Iterator i;
|
|
|
|
JournalFile *f;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
j->current_file = NULL;
|
|
|
|
j->current_field = 0;
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i)
|
2014-12-16 20:54:56 +01:00
|
|
|
journal_file_reset_location(f);
|
2011-10-15 02:53:04 +02:00
|
|
|
}
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
static void reset_location(sd_journal *j) {
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
detach_location(j);
|
|
|
|
zero(j->current_location);
|
|
|
|
}
|
|
|
|
|
2012-10-10 01:34:27 +02:00
|
|
|
static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
|
2011-11-08 18:20:03 +01:00
|
|
|
assert(l);
|
2012-10-10 01:34:27 +02:00
|
|
|
assert(type == LOCATION_DISCRETE || type == LOCATION_SEEK);
|
2011-11-08 18:20:03 +01:00
|
|
|
assert(f);
|
|
|
|
assert(o->object.type == OBJECT_ENTRY);
|
|
|
|
|
2012-10-10 01:34:27 +02:00
|
|
|
l->type = type;
|
2011-11-08 18:20:03 +01:00
|
|
|
l->seqnum = le64toh(o->entry.seqnum);
|
|
|
|
l->seqnum_id = f->header->seqnum_id;
|
|
|
|
l->realtime = le64toh(o->entry.realtime);
|
|
|
|
l->monotonic = le64toh(o->entry.monotonic);
|
2012-01-19 12:42:53 +01:00
|
|
|
l->boot_id = o->entry.boot_id;
|
2011-11-08 18:20:03 +01:00
|
|
|
l->xor_hash = le64toh(o->entry.xor_hash);
|
|
|
|
|
|
|
|
l->seqnum_set = l->realtime_set = l->monotonic_set = l->xor_hash_set = true;
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:38:09 +01:00
|
|
|
static void set_location(sd_journal *j, JournalFile *f, Object *o) {
|
2011-11-08 18:20:03 +01:00
|
|
|
assert(j);
|
|
|
|
assert(f);
|
|
|
|
assert(o);
|
|
|
|
|
2014-12-16 22:38:09 +01:00
|
|
|
init_location(&j->current_location, LOCATION_DISCRETE, f, o);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
j->current_file = f;
|
|
|
|
j->current_field = 0;
|
|
|
|
|
2014-12-16 21:03:36 +01:00
|
|
|
/* Let f know its candidate entry was picked. */
|
|
|
|
assert(f->location_type == LOCATION_SEEK);
|
|
|
|
f->location_type = LOCATION_DISCRETE;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
static int match_is_valid(const void *data, size_t size) {
|
|
|
|
const char *b, *p;
|
|
|
|
|
|
|
|
assert(data);
|
|
|
|
|
|
|
|
if (size < 2)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (startswith(data, "__"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
b = data;
|
|
|
|
for (p = b; p < b + size; p++) {
|
|
|
|
|
|
|
|
if (*p == '=')
|
|
|
|
return p > b;
|
|
|
|
|
|
|
|
if (*p == '_')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p >= 'A' && *p <= 'Z')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p >= '0' && *p <= '9')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
|
2011-11-08 18:20:03 +01:00
|
|
|
const uint8_t *a = _a, *b = _b;
|
|
|
|
size_t j;
|
|
|
|
|
|
|
|
for (j = 0; j < s && j < t; j++) {
|
|
|
|
|
|
|
|
if (a[j] != b[j])
|
2012-07-13 00:29:26 +02:00
|
|
|
return false;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (a[j] == '=')
|
|
|
|
return true;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2013-06-07 05:30:46 +02:00
|
|
|
assert_not_reached("\"=\" not found");
|
2012-07-13 00:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static Match *match_new(Match *p, MatchType t) {
|
|
|
|
Match *m;
|
|
|
|
|
|
|
|
m = new0(Match, 1);
|
|
|
|
if (!m)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
m->type = t;
|
|
|
|
|
|
|
|
if (p) {
|
|
|
|
m->parent = p;
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_PREPEND(matches, p->matches, m);
|
2012-07-13 00:29:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void match_free(Match *m) {
|
|
|
|
assert(m);
|
|
|
|
|
|
|
|
while (m->matches)
|
|
|
|
match_free(m->matches);
|
|
|
|
|
|
|
|
if (m->parent)
|
2013-10-14 06:10:14 +02:00
|
|
|
LIST_REMOVE(matches, m->parent->matches, m);
|
2012-07-13 00:29:26 +02:00
|
|
|
|
|
|
|
free(m->data);
|
|
|
|
free(m);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void match_free_if_empty(Match *m) {
|
2013-06-02 21:00:00 +02:00
|
|
|
if (!m || m->matches)
|
2012-07-13 00:29:26 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
match_free(m);
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
|
2013-04-11 15:27:55 +02:00
|
|
|
Match *l3, *l4, *add_here = NULL, *m;
|
2012-03-16 11:59:04 +01:00
|
|
|
le64_t le_hash;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(data, -EINVAL);
|
2012-07-13 00:29:26 +02:00
|
|
|
|
|
|
|
if (size == 0)
|
|
|
|
size = strlen(data);
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(match_is_valid(data, size), -EINVAL);
|
2011-10-14 16:52:42 +02:00
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
/* level 0: AND term
|
|
|
|
* level 1: OR terms
|
|
|
|
* level 2: AND terms
|
|
|
|
* level 3: OR terms
|
|
|
|
* level 4: concrete matches */
|
2012-07-13 00:29:26 +02:00
|
|
|
|
|
|
|
if (!j->level0) {
|
2013-04-11 15:27:55 +02:00
|
|
|
j->level0 = match_new(NULL, MATCH_AND_TERM);
|
2012-07-13 00:29:26 +02:00
|
|
|
if (!j->level0)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!j->level1) {
|
2013-04-11 15:27:55 +02:00
|
|
|
j->level1 = match_new(j->level0, MATCH_OR_TERM);
|
2012-07-13 00:29:26 +02:00
|
|
|
if (!j->level1)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
if (!j->level2) {
|
|
|
|
j->level2 = match_new(j->level1, MATCH_AND_TERM);
|
|
|
|
if (!j->level2)
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(j->level0->type == MATCH_AND_TERM);
|
|
|
|
assert(j->level1->type == MATCH_OR_TERM);
|
|
|
|
assert(j->level2->type == MATCH_AND_TERM);
|
2012-05-30 22:30:35 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
le_hash = htole64(hash64(data, size));
|
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
LIST_FOREACH(matches, l3, j->level2->matches) {
|
|
|
|
assert(l3->type == MATCH_OR_TERM);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
LIST_FOREACH(matches, l4, l3->matches) {
|
|
|
|
assert(l4->type == MATCH_DISCRETE);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
/* Exactly the same match already? Then ignore
|
|
|
|
* this addition */
|
2013-04-11 15:27:55 +02:00
|
|
|
if (l4->le_hash == le_hash &&
|
|
|
|
l4->size == size &&
|
|
|
|
memcmp(l4->data, data, size) == 0)
|
2012-07-13 00:29:26 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Same field? Then let's add this to this OR term */
|
2013-04-11 15:27:55 +02:00
|
|
|
if (same_field(data, size, l4->data, l4->size)) {
|
|
|
|
add_here = l3;
|
2012-07-13 00:29:26 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (add_here)
|
|
|
|
break;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (!add_here) {
|
2013-04-11 15:27:55 +02:00
|
|
|
add_here = match_new(j->level2, MATCH_OR_TERM);
|
2012-07-13 00:29:26 +02:00
|
|
|
if (!add_here)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
m = match_new(add_here, MATCH_DISCRETE);
|
2011-10-08 02:20:44 +02:00
|
|
|
if (!m)
|
2012-07-13 00:29:26 +02:00
|
|
|
goto fail;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
m->le_hash = le_hash;
|
2011-10-14 16:52:42 +02:00
|
|
|
m->size = size;
|
2012-07-13 00:29:26 +02:00
|
|
|
m->data = memdup(data, size);
|
|
|
|
if (!m->data)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
detach_location(j);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
2013-06-02 21:00:00 +02:00
|
|
|
match_free_if_empty(add_here);
|
|
|
|
match_free_if_empty(j->level2);
|
|
|
|
match_free_if_empty(j->level1);
|
|
|
|
match_free_if_empty(j->level0);
|
2012-07-13 00:29:26 +02:00
|
|
|
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
_public_ int sd_journal_add_conjunction(sd_journal *j) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-10-14 16:52:42 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (!j->level0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!j->level1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!j->level1->matches)
|
|
|
|
return 0;
|
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
j->level1 = NULL;
|
|
|
|
j->level2 = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_add_disjunction(sd_journal *j) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2013-04-11 15:27:55 +02:00
|
|
|
|
|
|
|
if (!j->level0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!j->level1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!j->level2)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!j->level2->matches)
|
|
|
|
return 0;
|
2012-07-13 00:29:26 +02:00
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
j->level2 = NULL;
|
2012-07-13 00:29:26 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *match_make_string(Match *m) {
|
|
|
|
char *p, *r;
|
|
|
|
Match *i;
|
|
|
|
bool enclose = false;
|
|
|
|
|
|
|
|
if (!m)
|
2013-07-16 20:45:28 +02:00
|
|
|
return strdup("none");
|
2012-07-13 00:29:26 +02:00
|
|
|
|
|
|
|
if (m->type == MATCH_DISCRETE)
|
|
|
|
return strndup(m->data, m->size);
|
|
|
|
|
|
|
|
p = NULL;
|
|
|
|
LIST_FOREACH(matches, i, m->matches) {
|
|
|
|
char *t, *k;
|
|
|
|
|
|
|
|
t = match_make_string(i);
|
|
|
|
if (!t) {
|
|
|
|
free(p);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p) {
|
2012-07-13 13:41:01 +02:00
|
|
|
k = strjoin(p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t, NULL);
|
2012-07-13 00:29:26 +02:00
|
|
|
free(p);
|
|
|
|
free(t);
|
|
|
|
|
|
|
|
if (!k)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
p = k;
|
|
|
|
|
|
|
|
enclose = true;
|
2013-06-07 05:30:46 +02:00
|
|
|
} else
|
2012-07-13 00:29:26 +02:00
|
|
|
p = t;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (enclose) {
|
2012-07-13 13:41:01 +02:00
|
|
|
r = strjoin("(", p, ")", NULL);
|
2012-07-13 00:29:26 +02:00
|
|
|
free(p);
|
|
|
|
return r;
|
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
return p;
|
|
|
|
}
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
char *journal_make_match_string(sd_journal *j) {
|
|
|
|
assert(j);
|
2011-10-15 02:53:04 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
return match_make_string(j->level0);
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ void sd_journal_flush_matches(sd_journal *j) {
|
|
|
|
if (!j)
|
|
|
|
return;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (j->level0)
|
|
|
|
match_free(j->level0);
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2013-04-11 15:27:55 +02:00
|
|
|
j->level0 = j->level1 = j->level2 = NULL;
|
2011-10-15 02:53:04 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
detach_location(j);
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
_pure_ static int compare_with_location(JournalFile *f, Location *l) {
|
|
|
|
assert(f);
|
2011-11-08 18:20:03 +01:00
|
|
|
assert(l);
|
2014-12-19 15:05:30 +01:00
|
|
|
assert(f->location_type == LOCATION_SEEK);
|
2012-10-10 01:34:27 +02:00
|
|
|
assert(l->type == LOCATION_DISCRETE || l->type == LOCATION_SEEK);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
if (l->monotonic_set &&
|
2014-12-19 15:05:30 +01:00
|
|
|
sd_id128_equal(f->current_boot_id, l->boot_id) &&
|
2011-11-08 18:20:03 +01:00
|
|
|
l->realtime_set &&
|
2014-12-19 15:05:30 +01:00
|
|
|
f->current_realtime == l->realtime &&
|
2011-11-08 18:20:03 +01:00
|
|
|
l->xor_hash_set &&
|
2014-12-19 15:05:30 +01:00
|
|
|
f->current_xor_hash == l->xor_hash)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (l->seqnum_set &&
|
2014-12-19 15:05:30 +01:00
|
|
|
sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_seqnum < l->seqnum)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -1;
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_seqnum > l->seqnum)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->monotonic_set &&
|
2014-12-19 15:05:30 +01:00
|
|
|
sd_id128_equal(f->current_boot_id, l->boot_id)) {
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_monotonic < l->monotonic)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -1;
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_monotonic > l->monotonic)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->realtime_set) {
|
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_realtime < l->realtime)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -1;
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_realtime > l->realtime)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (l->xor_hash_set) {
|
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_xor_hash < l->xor_hash)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -1;
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->current_xor_hash > l->xor_hash)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
static int next_for_match(
|
|
|
|
sd_journal *j,
|
|
|
|
Match *m,
|
|
|
|
JournalFile *f,
|
|
|
|
uint64_t after_offset,
|
|
|
|
direction_t direction,
|
|
|
|
Object **ret,
|
|
|
|
uint64_t *offset) {
|
|
|
|
|
2011-10-15 01:13:37 +02:00
|
|
|
int r;
|
2012-07-13 00:29:26 +02:00
|
|
|
uint64_t np = 0;
|
|
|
|
Object *n;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
|
|
|
assert(j);
|
2012-07-13 00:29:26 +02:00
|
|
|
assert(m);
|
|
|
|
assert(f);
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (m->type == MATCH_DISCRETE) {
|
|
|
|
uint64_t dp;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
|
2011-11-08 18:20:03 +01:00
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
return journal_file_move_to_entry_by_offset_for_data(f, dp, after_offset, direction, ret, offset);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
} else if (m->type == MATCH_OR_TERM) {
|
|
|
|
Match *i;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
/* Find the earliest match beyond after_offset */
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
LIST_FOREACH(matches, i, m->matches) {
|
|
|
|
uint64_t cp;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
|
2012-01-04 03:45:24 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-07-13 00:29:26 +02:00
|
|
|
else if (r > 0) {
|
2013-06-07 05:30:46 +02:00
|
|
|
if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
|
2012-07-13 00:29:26 +02:00
|
|
|
np = cp;
|
|
|
|
}
|
|
|
|
}
|
2012-01-04 03:45:24 +01:00
|
|
|
|
2013-06-07 05:30:46 +02:00
|
|
|
if (np == 0)
|
|
|
|
return 0;
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
} else if (m->type == MATCH_AND_TERM) {
|
2013-06-05 06:56:03 +02:00
|
|
|
Match *i, *last_moved;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
/* Always jump to the next matching entry and repeat
|
2013-06-05 06:56:03 +02:00
|
|
|
* this until we find an offset that matches for all
|
2012-07-13 00:29:26 +02:00
|
|
|
* matches. */
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (!m->matches)
|
|
|
|
return 0;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2013-06-05 06:56:03 +02:00
|
|
|
r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-06-05 06:56:03 +02:00
|
|
|
assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
|
|
|
|
last_moved = m->matches;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-06-05 06:56:03 +02:00
|
|
|
LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
|
|
|
|
uint64_t cp;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-06-05 06:56:03 +02:00
|
|
|
r = next_for_match(j, i, f, np, direction, NULL, &cp);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-06-05 06:56:03 +02:00
|
|
|
assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
|
|
|
|
if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
|
|
|
|
np = cp;
|
|
|
|
last_moved = i;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
2013-06-05 06:56:03 +02:00
|
|
|
}
|
2012-07-13 00:29:26 +02:00
|
|
|
}
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-06-07 05:30:46 +02:00
|
|
|
assert(np > 0);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
if (ret)
|
2012-07-13 00:29:26 +02:00
|
|
|
*ret = n;
|
2011-11-08 18:20:03 +01:00
|
|
|
if (offset)
|
2012-07-13 00:29:26 +02:00
|
|
|
*offset = np;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
static int find_location_for_match(
|
|
|
|
sd_journal *j,
|
|
|
|
Match *m,
|
|
|
|
JournalFile *f,
|
|
|
|
direction_t direction,
|
|
|
|
Object **ret,
|
|
|
|
uint64_t *offset) {
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(j);
|
2012-07-13 00:29:26 +02:00
|
|
|
assert(m);
|
2011-11-08 18:20:03 +01:00
|
|
|
assert(f);
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (m->type == MATCH_DISCRETE) {
|
|
|
|
uint64_t dp;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
r = journal_file_find_data_object_with_hash(f, m->data, m->size, le64toh(m->le_hash), NULL, &dp);
|
2011-10-15 01:13:37 +02:00
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
/* FIXME: missing: find by monotonic */
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (j->current_location.type == LOCATION_HEAD)
|
|
|
|
return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_DOWN, ret, offset);
|
|
|
|
if (j->current_location.type == LOCATION_TAIL)
|
|
|
|
return journal_file_next_entry_for_data(f, NULL, 0, dp, DIRECTION_UP, ret, offset);
|
|
|
|
if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
|
|
|
|
return journal_file_move_to_entry_by_seqnum_for_data(f, dp, j->current_location.seqnum, direction, ret, offset);
|
|
|
|
if (j->current_location.monotonic_set) {
|
|
|
|
r = journal_file_move_to_entry_by_monotonic_for_data(f, dp, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
|
|
|
|
if (r != -ENOENT)
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (j->current_location.realtime_set)
|
|
|
|
return journal_file_move_to_entry_by_realtime_for_data(f, dp, j->current_location.realtime, direction, ret, offset);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
return journal_file_next_entry_for_data(f, NULL, 0, dp, direction, ret, offset);
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
} else if (m->type == MATCH_OR_TERM) {
|
|
|
|
uint64_t np = 0;
|
|
|
|
Object *n;
|
|
|
|
Match *i;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
/* Find the earliest match */
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
LIST_FOREACH(matches, i, m->matches) {
|
|
|
|
uint64_t cp;
|
|
|
|
|
|
|
|
r = find_location_for_match(j, i, f, direction, NULL, &cp);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
else if (r > 0) {
|
|
|
|
if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
|
|
|
|
np = cp;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
2012-07-13 00:29:26 +02:00
|
|
|
}
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (np == 0)
|
|
|
|
return 0;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, np, &n);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
if (ret)
|
|
|
|
*ret = n;
|
|
|
|
if (offset)
|
|
|
|
*offset = np;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
return 1;
|
2011-10-15 01:38:44 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
} else {
|
|
|
|
Match *i;
|
|
|
|
uint64_t np = 0;
|
|
|
|
|
|
|
|
assert(m->type == MATCH_AND_TERM);
|
|
|
|
|
|
|
|
/* First jump to the last match, and then find the
|
|
|
|
* next one where all matches match */
|
|
|
|
|
|
|
|
if (!m->matches)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
LIST_FOREACH(matches, i, m->matches) {
|
|
|
|
uint64_t cp;
|
|
|
|
|
|
|
|
r = find_location_for_match(j, i, f, direction, NULL, &cp);
|
|
|
|
if (r <= 0)
|
2012-01-04 02:15:08 +01:00
|
|
|
return r;
|
|
|
|
|
2013-06-07 05:30:46 +02:00
|
|
|
if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
|
2012-07-13 00:29:26 +02:00
|
|
|
np = cp;
|
2011-10-15 01:13:37 +02:00
|
|
|
}
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
return next_for_match(j, m, f, np, direction, ret, offset);
|
|
|
|
}
|
|
|
|
}
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
static int find_location_with_matches(
|
|
|
|
sd_journal *j,
|
|
|
|
JournalFile *f,
|
|
|
|
direction_t direction,
|
|
|
|
Object **ret,
|
|
|
|
uint64_t *offset) {
|
|
|
|
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(f);
|
|
|
|
assert(ret);
|
|
|
|
assert(offset);
|
|
|
|
|
|
|
|
if (!j->level0) {
|
|
|
|
/* No matches is simple */
|
|
|
|
|
|
|
|
if (j->current_location.type == LOCATION_HEAD)
|
2014-12-17 15:45:10 +01:00
|
|
|
return journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, offset);
|
2012-07-13 00:29:26 +02:00
|
|
|
if (j->current_location.type == LOCATION_TAIL)
|
2014-12-17 15:45:10 +01:00
|
|
|
return journal_file_next_entry(f, 0, DIRECTION_UP, ret, offset);
|
2012-07-13 00:29:26 +02:00
|
|
|
if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
|
|
|
|
return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, offset);
|
|
|
|
if (j->current_location.monotonic_set) {
|
|
|
|
r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, offset);
|
|
|
|
if (r != -ENOENT)
|
|
|
|
return r;
|
2011-10-15 01:13:37 +02:00
|
|
|
}
|
2012-07-13 00:29:26 +02:00
|
|
|
if (j->current_location.realtime_set)
|
|
|
|
return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, offset);
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2014-12-17 15:45:10 +01:00
|
|
|
return journal_file_next_entry(f, 0, direction, ret, offset);
|
2012-07-13 00:29:26 +02:00
|
|
|
} else
|
|
|
|
return find_location_for_match(j, j->level0, f, direction, ret, offset);
|
|
|
|
}
|
2011-10-15 01:13:37 +02:00
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
static int next_with_matches(
|
|
|
|
sd_journal *j,
|
|
|
|
JournalFile *f,
|
|
|
|
direction_t direction,
|
|
|
|
Object **ret,
|
|
|
|
uint64_t *offset) {
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(f);
|
|
|
|
assert(ret);
|
|
|
|
assert(offset);
|
|
|
|
|
|
|
|
/* No matches is easy. We simple advance the file
|
|
|
|
* pointer by one. */
|
|
|
|
if (!j->level0)
|
2014-12-19 14:42:55 +01:00
|
|
|
return journal_file_next_entry(f, f->current_offset, direction, ret, offset);
|
2012-07-13 00:29:26 +02:00
|
|
|
|
|
|
|
/* If we have a match then we look for the next matching entry
|
2012-07-15 10:41:40 +02:00
|
|
|
* with an offset at least one step larger */
|
2014-12-19 14:42:55 +01:00
|
|
|
return next_for_match(j, j->level0, f,
|
|
|
|
direction == DIRECTION_DOWN ? f->current_offset + 1
|
|
|
|
: f->current_offset - 1,
|
|
|
|
direction, ret, offset);
|
2011-10-15 01:13:37 +02:00
|
|
|
}
|
|
|
|
|
2014-12-17 15:37:52 +01:00
|
|
|
static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
|
2011-11-08 18:20:03 +01:00
|
|
|
Object *c;
|
2014-12-18 14:21:55 +01:00
|
|
|
uint64_t cp, n_entries;
|
2012-07-13 00:29:26 +02:00
|
|
|
int r;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(f);
|
|
|
|
|
2015-02-24 19:45:17 +01:00
|
|
|
n_entries = le64toh(f->header->n_entries);
|
|
|
|
|
|
|
|
/* If we hit EOF before, we don't need to look into this file again
|
|
|
|
* unless direction changed or new entries appeared. */
|
|
|
|
if (f->last_direction == direction && f->location_type == LOCATION_TAIL &&
|
|
|
|
n_entries == f->last_n_entries)
|
|
|
|
return 0;
|
2014-12-19 17:11:22 +01:00
|
|
|
|
2015-02-24 19:45:17 +01:00
|
|
|
f->last_n_entries = n_entries;
|
|
|
|
|
|
|
|
if (f->last_direction == direction && f->current_offset > 0) {
|
2014-12-17 15:07:25 +01:00
|
|
|
/* LOCATION_SEEK here means we did the work in a previous
|
|
|
|
* iteration and the current location already points to a
|
|
|
|
* candidate entry. */
|
2014-12-19 15:05:30 +01:00
|
|
|
if (f->location_type != LOCATION_SEEK) {
|
|
|
|
r = next_with_matches(j, f, direction, &c, &cp);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2011-12-20 00:38:14 +01:00
|
|
|
|
2015-02-24 19:45:17 +01:00
|
|
|
journal_file_save_location(f, c, cp);
|
2014-12-19 15:05:30 +01:00
|
|
|
}
|
2011-11-08 18:20:03 +01:00
|
|
|
} else {
|
2015-02-24 19:45:17 +01:00
|
|
|
f->last_direction = direction;
|
|
|
|
|
2012-07-13 00:29:26 +02:00
|
|
|
r = find_location_with_matches(j, f, direction, &c, &cp);
|
2011-11-08 18:20:03 +01:00
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2014-12-19 15:05:30 +01:00
|
|
|
|
2015-02-24 19:45:17 +01:00
|
|
|
journal_file_save_location(f, c, cp);
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2013-06-07 05:30:46 +02:00
|
|
|
/* OK, we found the spot, now let's advance until an entry
|
2012-07-13 00:29:26 +02:00
|
|
|
* that is actually different from what we were previously
|
|
|
|
* looking at. This is necessary to handle entries which exist
|
|
|
|
* in two (or more) journal files, and which shall all be
|
|
|
|
* suppressed but one. */
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
for (;;) {
|
|
|
|
bool found;
|
|
|
|
|
|
|
|
if (j->current_location.type == LOCATION_DISCRETE) {
|
|
|
|
int k;
|
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
k = compare_with_location(f, &j->current_location);
|
2014-10-13 22:12:33 +02:00
|
|
|
|
|
|
|
found = direction == DIRECTION_DOWN ? k > 0 : k < 0;
|
2011-11-08 18:20:03 +01:00
|
|
|
} else
|
|
|
|
found = true;
|
|
|
|
|
2014-12-19 15:05:30 +01:00
|
|
|
if (found)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
r = next_with_matches(j, f, direction, &c, &cp);
|
|
|
|
if (r <= 0)
|
|
|
|
return r;
|
2014-12-19 15:05:30 +01:00
|
|
|
|
2015-02-24 19:45:17 +01:00
|
|
|
journal_file_save_location(f, c, cp);
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-15 01:38:44 +02:00
|
|
|
static int real_journal_next(sd_journal *j, direction_t direction) {
|
2012-09-19 09:56:00 +02:00
|
|
|
JournalFile *f, *new_file = NULL;
|
2011-10-08 02:20:44 +02:00
|
|
|
Iterator i;
|
2014-02-20 17:21:13 +01:00
|
|
|
Object *o;
|
2011-10-07 21:06:39 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
2011-11-08 18:20:03 +01:00
|
|
|
bool found;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2014-12-17 15:37:52 +01:00
|
|
|
r = next_beyond_location(j, f, direction);
|
2012-07-03 11:58:16 +02:00
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_debug_errno(r, "Can't iterate through %s, ignoring: %m", f->path);
|
2014-02-27 06:11:54 +01:00
|
|
|
remove_file_real(j, f);
|
2012-07-03 11:58:16 +02:00
|
|
|
continue;
|
2014-12-16 21:03:36 +01:00
|
|
|
} else if (r == 0) {
|
|
|
|
f->location_type = LOCATION_TAIL;
|
2011-10-08 02:20:44 +02:00
|
|
|
continue;
|
2014-12-16 21:03:36 +01:00
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-09-19 09:56:00 +02:00
|
|
|
if (!new_file)
|
2011-11-08 18:20:03 +01:00
|
|
|
found = true;
|
|
|
|
else {
|
|
|
|
int k;
|
|
|
|
|
2014-12-17 14:06:28 +01:00
|
|
|
k = journal_file_compare_locations(f, new_file);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2013-06-07 05:30:46 +02:00
|
|
|
found = direction == DIRECTION_DOWN ? k < 0 : k > 0;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
|
|
|
|
2014-12-18 09:56:04 +01:00
|
|
|
if (found)
|
2012-09-19 09:56:00 +02:00
|
|
|
new_file = f;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-09-19 09:56:00 +02:00
|
|
|
if (!new_file)
|
2011-11-08 18:20:03 +01:00
|
|
|
return 0;
|
2011-10-14 05:34:19 +02:00
|
|
|
|
2014-12-18 09:56:04 +01:00
|
|
|
r = journal_file_move_to_object(new_file, OBJECT_ENTRY, new_file->current_offset, &o);
|
2012-09-19 09:56:00 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-12-16 22:38:09 +01:00
|
|
|
set_location(j, new_file, o);
|
2011-10-14 05:34:19 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2011-10-14 05:34:19 +02:00
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_next(sd_journal *j) {
|
2011-11-08 18:20:03 +01:00
|
|
|
return real_journal_next(j, DIRECTION_DOWN);
|
|
|
|
}
|
2011-10-14 05:34:19 +02:00
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_previous(sd_journal *j) {
|
2011-11-08 18:20:03 +01:00
|
|
|
return real_journal_next(j, DIRECTION_UP);
|
|
|
|
}
|
2011-10-14 05:34:19 +02:00
|
|
|
|
2012-01-04 04:00:14 +01:00
|
|
|
static int real_journal_next_skip(sd_journal *j, direction_t direction, uint64_t skip) {
|
2011-11-08 18:20:03 +01:00
|
|
|
int c = 0, r;
|
2011-10-14 05:34:19 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-01-04 04:00:14 +01:00
|
|
|
if (skip == 0) {
|
|
|
|
/* If this is not a discrete skip, then at least
|
|
|
|
* resolve the current location */
|
|
|
|
if (j->current_location.type != LOCATION_DISCRETE)
|
|
|
|
return real_journal_next(j, direction);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
r = real_journal_next(j, direction);
|
2011-11-08 18:20:03 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
return c;
|
|
|
|
|
|
|
|
skip--;
|
|
|
|
c++;
|
2012-01-04 04:00:14 +01:00
|
|
|
} while (skip > 0);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
return c;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-04 04:00:14 +01:00
|
|
|
_public_ int sd_journal_next_skip(sd_journal *j, uint64_t skip) {
|
|
|
|
return real_journal_next_skip(j, DIRECTION_DOWN, skip);
|
|
|
|
}
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2012-01-04 04:00:14 +01:00
|
|
|
_public_ int sd_journal_previous_skip(sd_journal *j, uint64_t skip) {
|
|
|
|
return real_journal_next_skip(j, DIRECTION_UP, skip);
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_get_cursor(sd_journal *j, char **cursor) {
|
2011-10-08 02:20:44 +02:00
|
|
|
Object *o;
|
2011-10-07 21:06:39 +02:00
|
|
|
int r;
|
2011-10-14 04:44:50 +02:00
|
|
|
char bid[33], sid[33];
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(cursor, -EINVAL);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-14 04:44:50 +02:00
|
|
|
if (!j->current_file || j->current_file->current_offset <= 0)
|
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-10-14 04:44:50 +02:00
|
|
|
sd_id128_to_string(j->current_file->header->seqnum_id, sid);
|
|
|
|
sd_id128_to_string(o->entry.boot_id, bid);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2011-10-14 04:44:50 +02:00
|
|
|
if (asprintf(cursor,
|
2013-06-06 00:44:16 +02:00
|
|
|
"s=%s;i=%"PRIx64";b=%s;m=%"PRIx64";t=%"PRIx64";x=%"PRIx64,
|
|
|
|
sid, le64toh(o->entry.seqnum),
|
|
|
|
bid, le64toh(o->entry.monotonic),
|
|
|
|
le64toh(o->entry.realtime),
|
|
|
|
le64toh(o->entry.xor_hash)) < 0)
|
2011-10-14 04:44:50 +02:00
|
|
|
return -ENOMEM;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-02-22 11:18:46 +01:00
|
|
|
return 0;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_seek_cursor(sd_journal *j, const char *cursor) {
|
2014-07-30 04:01:36 +02:00
|
|
|
const char *word, *state;
|
2011-11-08 18:20:03 +01:00
|
|
|
size_t l;
|
|
|
|
unsigned long long seqnum, monotonic, realtime, xor_hash;
|
|
|
|
bool
|
|
|
|
seqnum_id_set = false,
|
|
|
|
seqnum_set = false,
|
|
|
|
boot_id_set = false,
|
|
|
|
monotonic_set = false,
|
|
|
|
realtime_set = false,
|
|
|
|
xor_hash_set = false;
|
|
|
|
sd_id128_t seqnum_id, boot_id;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(!isempty(cursor), -EINVAL);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
|
2011-11-08 18:20:03 +01:00
|
|
|
char *item;
|
|
|
|
int k = 0;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
if (l < 2 || word[1] != '=')
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EINVAL;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
item = strndup(word, l);
|
2011-11-08 18:20:03 +01:00
|
|
|
if (!item)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
switch (word[0]) {
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
case 's':
|
|
|
|
seqnum_id_set = true;
|
2012-09-27 23:28:54 +02:00
|
|
|
k = sd_id128_from_string(item+2, &seqnum_id);
|
2011-11-08 18:20:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
seqnum_set = true;
|
2012-09-27 23:28:54 +02:00
|
|
|
if (sscanf(item+2, "%llx", &seqnum) != 1)
|
2011-11-08 18:20:03 +01:00
|
|
|
k = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
boot_id_set = true;
|
2012-09-27 23:28:54 +02:00
|
|
|
k = sd_id128_from_string(item+2, &boot_id);
|
2011-11-08 18:20:03 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
monotonic_set = true;
|
2012-09-27 23:28:54 +02:00
|
|
|
if (sscanf(item+2, "%llx", &monotonic) != 1)
|
2011-11-08 18:20:03 +01:00
|
|
|
k = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
realtime_set = true;
|
2012-09-27 23:28:54 +02:00
|
|
|
if (sscanf(item+2, "%llx", &realtime) != 1)
|
2011-11-08 18:20:03 +01:00
|
|
|
k = -EINVAL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
xor_hash_set = true;
|
2012-09-27 23:28:54 +02:00
|
|
|
if (sscanf(item+2, "%llx", &xor_hash) != 1)
|
2011-11-08 18:20:03 +01:00
|
|
|
k = -EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(item);
|
|
|
|
|
|
|
|
if (k < 0)
|
|
|
|
return k;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!seqnum_set || !seqnum_id_set) &&
|
|
|
|
(!monotonic_set || !boot_id_set) &&
|
|
|
|
!realtime_set)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
reset_location(j);
|
|
|
|
|
2012-10-10 01:34:27 +02:00
|
|
|
j->current_location.type = LOCATION_SEEK;
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
if (realtime_set) {
|
|
|
|
j->current_location.realtime = (uint64_t) realtime;
|
|
|
|
j->current_location.realtime_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (seqnum_set && seqnum_id_set) {
|
|
|
|
j->current_location.seqnum = (uint64_t) seqnum;
|
|
|
|
j->current_location.seqnum_id = seqnum_id;
|
|
|
|
j->current_location.seqnum_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (monotonic_set && boot_id_set) {
|
|
|
|
j->current_location.monotonic = (uint64_t) monotonic;
|
|
|
|
j->current_location.boot_id = boot_id;
|
|
|
|
j->current_location.monotonic_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (xor_hash_set) {
|
|
|
|
j->current_location.xor_hash = (uint64_t) xor_hash;
|
|
|
|
j->current_location.xor_hash_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-10 22:39:45 +02:00
|
|
|
_public_ int sd_journal_test_cursor(sd_journal *j, const char *cursor) {
|
|
|
|
int r;
|
2014-07-30 04:01:36 +02:00
|
|
|
const char *word, *state;
|
2012-10-10 22:39:45 +02:00
|
|
|
size_t l;
|
|
|
|
Object *o;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(!isempty(cursor), -EINVAL);
|
2012-10-10 22:39:45 +02:00
|
|
|
|
|
|
|
if (!j->current_file || j->current_file->current_offset <= 0)
|
|
|
|
return -EADDRNOTAVAIL;
|
|
|
|
|
|
|
|
r = journal_file_move_to_object(j->current_file, OBJECT_ENTRY, j->current_file->current_offset, &o);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
FOREACH_WORD_SEPARATOR(word, l, cursor, ";", state) {
|
2012-10-10 22:39:45 +02:00
|
|
|
_cleanup_free_ char *item = NULL;
|
|
|
|
sd_id128_t id;
|
|
|
|
unsigned long long ll;
|
|
|
|
int k = 0;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
if (l < 2 || word[1] != '=')
|
2012-10-10 22:39:45 +02:00
|
|
|
return -EINVAL;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
item = strndup(word, l);
|
2012-10-10 22:39:45 +02:00
|
|
|
if (!item)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-07-30 04:01:36 +02:00
|
|
|
switch (word[0]) {
|
2012-10-10 22:39:45 +02:00
|
|
|
|
|
|
|
case 's':
|
|
|
|
k = sd_id128_from_string(item+2, &id);
|
|
|
|
if (k < 0)
|
|
|
|
return k;
|
|
|
|
if (!sd_id128_equal(id, j->current_file->header->seqnum_id))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
if (sscanf(item+2, "%llx", &ll) != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ll != le64toh(o->entry.seqnum))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b':
|
|
|
|
k = sd_id128_from_string(item+2, &id);
|
|
|
|
if (k < 0)
|
|
|
|
return k;
|
|
|
|
if (!sd_id128_equal(id, o->entry.boot_id))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'm':
|
|
|
|
if (sscanf(item+2, "%llx", &ll) != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ll != le64toh(o->entry.monotonic))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't':
|
|
|
|
if (sscanf(item+2, "%llx", &ll) != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ll != le64toh(o->entry.realtime))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
if (sscanf(item+2, "%llx", &ll) != 1)
|
|
|
|
return -EINVAL;
|
|
|
|
if (ll != le64toh(o->entry.xor_hash))
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_seek_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t usec) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
reset_location(j);
|
2012-10-10 01:34:27 +02:00
|
|
|
j->current_location.type = LOCATION_SEEK;
|
2011-11-08 18:20:03 +01:00
|
|
|
j->current_location.boot_id = boot_id;
|
|
|
|
j->current_location.monotonic = usec;
|
|
|
|
j->current_location.monotonic_set = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_seek_realtime_usec(sd_journal *j, uint64_t usec) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
reset_location(j);
|
2012-10-10 01:34:27 +02:00
|
|
|
j->current_location.type = LOCATION_SEEK;
|
2011-11-08 18:20:03 +01:00
|
|
|
j->current_location.realtime = usec;
|
|
|
|
j->current_location.realtime_set = true;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_seek_head(sd_journal *j) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
reset_location(j);
|
|
|
|
j->current_location.type = LOCATION_HEAD;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_seek_tail(sd_journal *j) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-11-08 18:20:03 +01:00
|
|
|
|
|
|
|
reset_location(j);
|
|
|
|
j->current_location.type = LOCATION_TAIL;
|
|
|
|
|
|
|
|
return 0;
|
2011-10-07 21:06:39 +02:00
|
|
|
}
|
|
|
|
|
2012-10-26 20:05:19 +02:00
|
|
|
static void check_network(sd_journal *j, int fd) {
|
|
|
|
struct statfs sfs;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
if (j->on_network)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (fstatfs(fd, &sfs) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
j->on_network =
|
2013-08-16 05:13:48 +02:00
|
|
|
F_TYPE_EQUAL(sfs.f_type, CIFS_MAGIC_NUMBER) ||
|
|
|
|
F_TYPE_EQUAL(sfs.f_type, CODA_SUPER_MAGIC) ||
|
|
|
|
F_TYPE_EQUAL(sfs.f_type, NCP_SUPER_MAGIC) ||
|
|
|
|
F_TYPE_EQUAL(sfs.f_type, NFS_SUPER_MAGIC) ||
|
|
|
|
F_TYPE_EQUAL(sfs.f_type, SMB_SUPER_MAGIC);
|
2012-10-26 20:05:19 +02:00
|
|
|
}
|
|
|
|
|
2013-06-05 04:31:05 +02:00
|
|
|
static bool file_has_type_prefix(const char *prefix, const char *filename) {
|
|
|
|
const char *full, *tilded, *atted;
|
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
full = strjoina(prefix, ".journal");
|
|
|
|
tilded = strjoina(full, "~");
|
|
|
|
atted = strjoina(prefix, "@");
|
2013-06-05 04:31:05 +02:00
|
|
|
|
|
|
|
return streq(filename, full) ||
|
|
|
|
streq(filename, tilded) ||
|
|
|
|
startswith(filename, atted);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool file_type_wanted(int flags, const char *filename) {
|
|
|
|
if (!endswith(filename, ".journal") && !endswith(filename, ".journal~"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* no flags set → every type is OK */
|
|
|
|
if (!(flags & (SD_JOURNAL_SYSTEM | SD_JOURNAL_CURRENT_USER)))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (flags & SD_JOURNAL_SYSTEM && file_has_type_prefix("system", filename))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (flags & SD_JOURNAL_CURRENT_USER) {
|
|
|
|
char prefix[5 + DECIMAL_STR_MAX(uid_t) + 1];
|
|
|
|
|
2015-01-27 14:00:11 +01:00
|
|
|
xsprintf(prefix, "user-"UID_FMT, getuid());
|
2013-06-05 04:31:05 +02:00
|
|
|
|
|
|
|
if (file_has_type_prefix(prefix, filename))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:39:26 +02:00
|
|
|
static int add_any_file(sd_journal *j, const char *path) {
|
2014-02-19 17:47:11 +01:00
|
|
|
JournalFile *f = NULL;
|
2013-06-06 01:39:26 +02:00
|
|
|
int r;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
assert(j);
|
2013-06-06 01:39:26 +02:00
|
|
|
assert(path);
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
if (ordered_hashmap_get(j->files, path))
|
2011-12-19 22:35:46 +01:00
|
|
|
return 0;
|
|
|
|
|
2015-01-29 02:10:15 +01:00
|
|
|
if (ordered_hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
|
|
|
|
log_warning("Too many open journal files, not adding %s.", path);
|
|
|
|
return set_put_error(j, -ETOOMANYREFS);
|
|
|
|
}
|
|
|
|
|
2012-08-14 22:02:24 +02:00
|
|
|
r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
|
2013-06-06 01:39:26 +02:00
|
|
|
if (r < 0)
|
2011-10-14 04:44:50 +02:00
|
|
|
return r;
|
|
|
|
|
2011-12-21 18:17:22 +01:00
|
|
|
/* journal_file_dump(f); */
|
2011-11-08 18:20:03 +01:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
r = ordered_hashmap_put(j->files, f->path, f);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0) {
|
|
|
|
journal_file_close(f);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2013-06-04 00:28:12 +02:00
|
|
|
log_debug("File %s added.", f->path);
|
2013-03-18 03:00:01 +01:00
|
|
|
|
2012-10-26 20:05:19 +02:00
|
|
|
check_network(j, f->fd);
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
j->current_invalidate_counter ++;
|
|
|
|
|
2011-12-19 22:35:46 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-06 01:39:26 +02:00
|
|
|
static int add_file(sd_journal *j, const char *prefix, const char *filename) {
|
|
|
|
_cleanup_free_ char *path = NULL;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(prefix);
|
|
|
|
assert(filename);
|
|
|
|
|
|
|
|
if (j->no_new_files ||
|
|
|
|
!file_type_wanted(j->flags, filename))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
path = strjoin(prefix, "/", filename, NULL);
|
|
|
|
if (!path)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = add_any_file(j, path);
|
|
|
|
if (r == -ENOENT)
|
|
|
|
return 0;
|
2015-03-08 16:11:50 +01:00
|
|
|
return r;
|
2013-06-06 01:39:26 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
static int remove_file(sd_journal *j, const char *prefix, const char *filename) {
|
2014-02-27 06:11:54 +01:00
|
|
|
_cleanup_free_ char *path;
|
2011-12-19 22:35:46 +01:00
|
|
|
JournalFile *f;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(prefix);
|
|
|
|
assert(filename);
|
|
|
|
|
2012-07-13 13:41:01 +02:00
|
|
|
path = strjoin(prefix, "/", filename, NULL);
|
2012-07-11 01:08:38 +02:00
|
|
|
if (!path)
|
2011-12-19 22:35:46 +01:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
f = ordered_hashmap_get(j->files, path);
|
2011-12-19 22:35:46 +01:00
|
|
|
if (!f)
|
|
|
|
return 0;
|
|
|
|
|
2014-02-27 06:11:54 +01:00
|
|
|
remove_file_real(j, f);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void remove_file_real(sd_journal *j, JournalFile *f) {
|
|
|
|
assert(j);
|
|
|
|
assert(f);
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ordered_hashmap_remove(j->files, f->path);
|
2012-10-16 22:59:28 +02:00
|
|
|
|
2013-06-04 00:28:12 +02:00
|
|
|
log_debug("File %s removed.", f->path);
|
2012-10-16 22:59:28 +02:00
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
if (j->current_file == f) {
|
|
|
|
j->current_file = NULL;
|
|
|
|
j->current_field = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (j->unique_file == f) {
|
2014-10-10 04:44:29 +02:00
|
|
|
/* Jump to the next unique_file or NULL if that one was last */
|
2014-08-19 13:38:53 +02:00
|
|
|
j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
|
2012-10-18 03:29:19 +02:00
|
|
|
j->unique_offset = 0;
|
2014-10-10 04:44:29 +02:00
|
|
|
if (!j->unique_file)
|
|
|
|
j->unique_file_lost = true;
|
2012-10-18 03:29:19 +02:00
|
|
|
}
|
|
|
|
|
2011-12-19 22:35:46 +01:00
|
|
|
journal_file_close(f);
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
j->current_invalidate_counter ++;
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
static int add_directory(sd_journal *j, const char *prefix, const char *dirname) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_free_ char *path = NULL;
|
2011-10-14 04:44:50 +02:00
|
|
|
int r;
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2011-12-29 15:00:57 +01:00
|
|
|
sd_id128_t id, mid;
|
2012-07-11 01:08:38 +02:00
|
|
|
Directory *m;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(prefix);
|
2012-07-11 01:08:38 +02:00
|
|
|
assert(dirname);
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2013-02-25 05:51:41 +01:00
|
|
|
log_debug("Considering %s/%s.", prefix, dirname);
|
|
|
|
|
2011-12-29 15:00:57 +01:00
|
|
|
if ((j->flags & SD_JOURNAL_LOCAL_ONLY) &&
|
2012-07-11 01:08:38 +02:00
|
|
|
(sd_id128_from_string(dirname, &id) < 0 ||
|
2011-12-29 15:00:57 +01:00
|
|
|
sd_id128_get_machine(&mid) < 0 ||
|
2013-02-25 05:51:41 +01:00
|
|
|
!(sd_id128_equal(id, mid) || path_startswith(prefix, "/run"))))
|
2011-12-29 15:00:57 +01:00
|
|
|
return 0;
|
|
|
|
|
2012-07-13 13:41:01 +02:00
|
|
|
path = strjoin(prefix, "/", dirname, NULL);
|
2012-07-11 01:08:38 +02:00
|
|
|
if (!path)
|
2011-10-14 04:44:50 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
d = opendir(path);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (!d) {
|
2014-11-28 19:29:59 +01:00
|
|
|
log_debug_errno(errno, "Failed to open %s: %m", path);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (errno == ENOENT)
|
|
|
|
return 0;
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
m = hashmap_get(j->directories_by_path, path);
|
|
|
|
if (!m) {
|
|
|
|
m = new0(Directory, 1);
|
2013-03-18 03:00:01 +01:00
|
|
|
if (!m)
|
2012-07-11 01:08:38 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
m->is_root = false;
|
|
|
|
m->path = path;
|
|
|
|
|
|
|
|
if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
|
|
|
|
free(m);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2013-03-18 03:00:01 +01:00
|
|
|
path = NULL; /* avoid freeing in cleanup */
|
2012-07-11 01:08:38 +02:00
|
|
|
j->current_invalidate_counter ++;
|
|
|
|
|
2013-06-04 00:28:12 +02:00
|
|
|
log_debug("Directory %s added.", m->path);
|
2012-07-11 01:08:38 +02:00
|
|
|
|
2013-03-18 03:00:01 +01:00
|
|
|
} else if (m->is_root)
|
2012-07-11 01:08:38 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (m->wd <= 0 && j->inotify_fd >= 0) {
|
|
|
|
|
|
|
|
m->wd = inotify_add_watch(j->inotify_fd, m->path,
|
|
|
|
IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
|
2012-10-16 23:00:04 +02:00
|
|
|
IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT|IN_MOVED_FROM|
|
2012-07-19 03:22:07 +02:00
|
|
|
IN_ONLYDIR);
|
2012-07-11 01:08:38 +02:00
|
|
|
|
|
|
|
if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
|
|
|
|
inotify_rm_watch(j->inotify_fd, m->wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;) {
|
2012-09-19 22:21:09 +02:00
|
|
|
struct dirent *de;
|
2012-07-11 01:08:38 +02:00
|
|
|
|
2013-12-19 12:42:32 +01:00
|
|
|
errno = 0;
|
|
|
|
de = readdir(d);
|
|
|
|
if (!de && errno != 0) {
|
|
|
|
r = -errno;
|
2014-11-28 19:29:59 +01:00
|
|
|
log_debug_errno(errno, "Failed to read directory %s: %m", m->path);
|
2013-12-19 12:42:32 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (!de)
|
2012-07-11 01:08:38 +02:00
|
|
|
break;
|
|
|
|
|
2012-08-07 00:51:17 +02:00
|
|
|
if (dirent_is_file_with_suffix(de, ".journal") ||
|
|
|
|
dirent_is_file_with_suffix(de, ".journal~")) {
|
2012-07-11 01:08:38 +02:00
|
|
|
r = add_file(j, m->path, de->d_name);
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 17:09:20 +01:00
|
|
|
log_debug_errno(r, "Failed to add file %s/%s: %m",
|
|
|
|
m->path, de->d_name);
|
2013-03-23 01:12:22 +01:00
|
|
|
r = set_put_error(j, r);
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2012-07-11 01:08:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-26 20:05:19 +02:00
|
|
|
check_network(j, dirfd(d));
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
static int add_root_directory(sd_journal *j, const char *p) {
|
2013-04-18 09:11:22 +02:00
|
|
|
_cleanup_closedir_ DIR *d = NULL;
|
2012-07-11 01:08:38 +02:00
|
|
|
Directory *m;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(p);
|
|
|
|
|
|
|
|
if ((j->flags & SD_JOURNAL_RUNTIME_ONLY) &&
|
|
|
|
!path_startswith(p, "/run"))
|
|
|
|
return -EINVAL;
|
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
if (j->prefix)
|
2015-02-03 02:05:59 +01:00
|
|
|
p = strjoina(j->prefix, p);
|
2013-12-11 22:04:03 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
d = opendir(p);
|
|
|
|
if (!d)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
m = hashmap_get(j->directories_by_path, p);
|
|
|
|
if (!m) {
|
|
|
|
m = new0(Directory, 1);
|
2013-03-18 03:00:01 +01:00
|
|
|
if (!m)
|
2012-07-11 01:08:38 +02:00
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
m->is_root = true;
|
|
|
|
m->path = strdup(p);
|
|
|
|
if (!m->path) {
|
|
|
|
free(m);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hashmap_put(j->directories_by_path, m->path, m) < 0) {
|
|
|
|
free(m->path);
|
|
|
|
free(m);
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
j->current_invalidate_counter ++;
|
|
|
|
|
2013-06-04 00:28:12 +02:00
|
|
|
log_debug("Root directory %s added.", m->path);
|
2012-07-11 01:08:38 +02:00
|
|
|
|
2013-03-18 03:00:01 +01:00
|
|
|
} else if (!m->is_root)
|
2012-07-11 01:08:38 +02:00
|
|
|
return 0;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
if (m->wd <= 0 && j->inotify_fd >= 0) {
|
|
|
|
|
|
|
|
m->wd = inotify_add_watch(j->inotify_fd, m->path,
|
|
|
|
IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB|IN_DELETE|
|
2012-07-19 03:22:07 +02:00
|
|
|
IN_ONLYDIR);
|
2012-07-11 01:08:38 +02:00
|
|
|
|
|
|
|
if (m->wd > 0 && hashmap_put(j->directories_by_wd, INT_TO_PTR(m->wd), m) < 0)
|
|
|
|
inotify_rm_watch(j->inotify_fd, m->wd);
|
|
|
|
}
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-06-06 01:39:26 +02:00
|
|
|
if (j->no_new_files)
|
|
|
|
return 0;
|
|
|
|
|
2011-10-14 04:44:50 +02:00
|
|
|
for (;;) {
|
2012-09-19 22:21:09 +02:00
|
|
|
struct dirent *de;
|
2012-07-11 01:08:38 +02:00
|
|
|
sd_id128_t id;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2013-12-19 12:42:32 +01:00
|
|
|
errno = 0;
|
|
|
|
de = readdir(d);
|
|
|
|
if (!de && errno != 0) {
|
|
|
|
r = -errno;
|
2014-11-28 19:29:59 +01:00
|
|
|
log_debug_errno(errno, "Failed to read directory %s: %m", m->path);
|
2013-12-19 12:42:32 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if (!de)
|
2011-10-14 04:44:50 +02:00
|
|
|
break;
|
|
|
|
|
2012-08-07 00:51:17 +02:00
|
|
|
if (dirent_is_file_with_suffix(de, ".journal") ||
|
|
|
|
dirent_is_file_with_suffix(de, ".journal~")) {
|
2012-07-11 01:08:38 +02:00
|
|
|
r = add_file(j, m->path, de->d_name);
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 17:09:20 +01:00
|
|
|
log_debug_errno(r, "Failed to add file %s/%s: %m",
|
|
|
|
m->path, de->d_name);
|
2013-03-23 01:12:22 +01:00
|
|
|
r = set_put_error(j, r);
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2012-07-19 02:35:17 +02:00
|
|
|
} else if ((de->d_type == DT_DIR || de->d_type == DT_LNK || de->d_type == DT_UNKNOWN) &&
|
2012-07-11 01:08:38 +02:00
|
|
|
sd_id128_from_string(de->d_name, &id) >= 0) {
|
|
|
|
|
|
|
|
r = add_directory(j, m->path, de->d_name);
|
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_debug_errno(r, "Failed to add directory %s/%s: %m", m->path, de->d_name);
|
2012-07-11 01:08:38 +02:00
|
|
|
}
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-10-26 20:05:19 +02:00
|
|
|
check_network(j, dirfd(d));
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-14 23:40:27 +02:00
|
|
|
static void remove_directory(sd_journal *j, Directory *d) {
|
2012-07-11 01:08:38 +02:00
|
|
|
assert(j);
|
|
|
|
|
|
|
|
if (d->wd > 0) {
|
|
|
|
hashmap_remove(j->directories_by_wd, INT_TO_PTR(d->wd));
|
|
|
|
|
|
|
|
if (j->inotify_fd >= 0)
|
|
|
|
inotify_rm_watch(j->inotify_fd, d->wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
hashmap_remove(j->directories_by_path, d->path);
|
|
|
|
|
|
|
|
if (d->is_root)
|
2013-06-04 00:28:12 +02:00
|
|
|
log_debug("Root directory %s removed.", d->path);
|
2012-07-11 01:08:38 +02:00
|
|
|
else
|
2013-06-04 00:28:12 +02:00
|
|
|
log_debug("Directory %s removed.", d->path);
|
2012-07-11 01:08:38 +02:00
|
|
|
|
|
|
|
free(d->path);
|
|
|
|
free(d);
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
static int add_search_paths(sd_journal *j) {
|
2013-03-11 23:03:13 +01:00
|
|
|
int r;
|
2012-07-11 01:08:38 +02:00
|
|
|
const char search_paths[] =
|
|
|
|
"/run/log/journal\0"
|
|
|
|
"/var/log/journal\0";
|
|
|
|
const char *p;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
/* We ignore most errors here, since the idea is to only open
|
|
|
|
* what's actually accessible, and ignore the rest. */
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-03-11 23:03:13 +01:00
|
|
|
NULSTR_FOREACH(p, search_paths) {
|
2013-12-11 23:10:36 +01:00
|
|
|
r = add_root_directory(j, p);
|
2013-03-23 01:12:22 +01:00
|
|
|
if (r < 0 && r != -ENOENT) {
|
|
|
|
r = set_put_error(j, r);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
}
|
2013-03-11 23:03:13 +01:00
|
|
|
}
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
return 0;
|
2011-12-19 22:35:46 +01:00
|
|
|
}
|
|
|
|
|
2013-06-06 01:39:26 +02:00
|
|
|
static int add_current_paths(sd_journal *j) {
|
|
|
|
Iterator i;
|
|
|
|
JournalFile *f;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(j->no_new_files);
|
|
|
|
|
|
|
|
/* Simply adds all directories for files we have open as
|
|
|
|
* "root" directories. We don't expect errors here, so we
|
|
|
|
* treat them as fatal. */
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
2013-06-06 01:39:26 +02:00
|
|
|
_cleanup_free_ char *dir;
|
2013-12-11 23:31:07 +01:00
|
|
|
int r;
|
2013-06-06 01:39:26 +02:00
|
|
|
|
|
|
|
dir = dirname_malloc(f->path);
|
|
|
|
if (!dir)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
r = add_root_directory(j, dir);
|
2013-06-06 01:39:26 +02:00
|
|
|
if (r < 0) {
|
|
|
|
set_put_error(j, r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
static int allocate_inotify(sd_journal *j) {
|
2011-12-19 22:35:46 +01:00
|
|
|
assert(j);
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
if (j->inotify_fd < 0) {
|
|
|
|
j->inotify_fd = inotify_init1(IN_NONBLOCK|IN_CLOEXEC);
|
|
|
|
if (j->inotify_fd < 0)
|
|
|
|
return -errno;
|
|
|
|
}
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
if (!j->directories_by_wd) {
|
2014-08-13 01:00:18 +02:00
|
|
|
j->directories_by_wd = hashmap_new(NULL);
|
2012-07-11 01:08:38 +02:00
|
|
|
if (!j->directories_by_wd)
|
|
|
|
return -ENOMEM;
|
2011-12-19 22:35:46 +01:00
|
|
|
}
|
2012-07-11 01:08:38 +02:00
|
|
|
|
|
|
|
return 0;
|
2011-12-19 22:35:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-19 03:21:49 +02:00
|
|
|
static sd_journal *journal_new(int flags, const char *path) {
|
2012-07-11 01:08:38 +02:00
|
|
|
sd_journal *j;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
j = new0(sd_journal, 1);
|
|
|
|
if (!j)
|
|
|
|
return NULL;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-06-27 01:55:48 +02:00
|
|
|
j->original_pid = getpid();
|
2012-07-11 01:08:38 +02:00
|
|
|
j->inotify_fd = -1;
|
|
|
|
j->flags = flags;
|
2012-11-21 00:28:00 +01:00
|
|
|
j->data_threshold = DEFAULT_DATA_THRESHOLD;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-19 03:21:49 +02:00
|
|
|
if (path) {
|
|
|
|
j->path = strdup(path);
|
2013-03-18 02:52:57 +01:00
|
|
|
if (!j->path)
|
|
|
|
goto fail;
|
2012-07-19 03:21:49 +02:00
|
|
|
}
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
j->files = ordered_hashmap_new(&string_hash_ops);
|
2014-08-13 01:00:18 +02:00
|
|
|
j->directories_by_path = hashmap_new(&string_hash_ops);
|
2012-08-18 01:46:20 +02:00
|
|
|
j->mmap = mmap_cache_new();
|
2013-03-23 01:12:22 +01:00
|
|
|
if (!j->files || !j->directories_by_path || !j->mmap)
|
2013-03-18 02:52:57 +01:00
|
|
|
goto fail;
|
2012-08-14 22:02:24 +02:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
return j;
|
2013-03-18 02:52:57 +01:00
|
|
|
|
|
|
|
fail:
|
|
|
|
sd_journal_close(j);
|
|
|
|
return NULL;
|
2011-12-19 22:35:46 +01:00
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_open(sd_journal **ret, int flags) {
|
2011-10-07 21:06:39 +02:00
|
|
|
sd_journal *j;
|
2011-10-14 04:44:50 +02:00
|
|
|
int r;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(ret, -EINVAL);
|
2013-12-11 22:04:03 +01:00
|
|
|
assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY|SD_JOURNAL_SYSTEM|SD_JOURNAL_CURRENT_USER)) == 0, -EINVAL);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-19 03:21:49 +02:00
|
|
|
j = journal_new(flags, NULL);
|
2011-10-07 21:06:39 +02:00
|
|
|
if (!j)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
r = add_search_paths(j);
|
2012-07-11 01:08:38 +02:00
|
|
|
if (r < 0)
|
2011-12-19 22:35:46 +01:00
|
|
|
goto fail;
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
*ret = j;
|
|
|
|
return 0;
|
2011-12-29 15:00:57 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
fail:
|
|
|
|
sd_journal_close(j);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
return r;
|
|
|
|
}
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-12-11 22:04:03 +01:00
|
|
|
_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
|
|
|
|
_cleanup_free_ char *root = NULL, *class = NULL;
|
|
|
|
sd_journal *j;
|
|
|
|
char *p;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert_return(machine, -EINVAL);
|
|
|
|
assert_return(ret, -EINVAL);
|
|
|
|
assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL);
|
2014-10-22 23:20:59 +02:00
|
|
|
assert_return(machine_name_is_valid(machine), -EINVAL);
|
2013-12-11 22:04:03 +01:00
|
|
|
|
2015-02-03 02:05:59 +01:00
|
|
|
p = strjoina("/run/systemd/machines/", machine);
|
2013-12-11 22:04:03 +01:00
|
|
|
r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
|
|
|
|
if (r == -ENOENT)
|
|
|
|
return -EHOSTDOWN;
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (!root)
|
|
|
|
return -ENODATA;
|
|
|
|
|
|
|
|
if (!streq_ptr(class, "container"))
|
|
|
|
return -EIO;
|
|
|
|
|
|
|
|
j = journal_new(flags, NULL);
|
|
|
|
if (!j)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
j->prefix = root;
|
|
|
|
root = NULL;
|
|
|
|
|
|
|
|
r = add_search_paths(j);
|
2013-12-11 22:04:03 +01:00
|
|
|
if (r < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*ret = j;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sd_journal_close(j);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
_public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
|
|
|
|
sd_journal *j;
|
|
|
|
int r;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(ret, -EINVAL);
|
|
|
|
assert_return(path, -EINVAL);
|
|
|
|
assert_return(flags == 0, -EINVAL);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-19 03:21:49 +02:00
|
|
|
j = journal_new(flags, path);
|
2012-07-11 01:08:38 +02:00
|
|
|
if (!j)
|
|
|
|
return -ENOMEM;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2013-12-11 23:10:36 +01:00
|
|
|
r = add_root_directory(j, path);
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r < 0) {
|
2013-03-23 01:12:22 +01:00
|
|
|
set_put_error(j, r);
|
2012-07-11 01:08:38 +02:00
|
|
|
goto fail;
|
2013-03-11 23:03:13 +01:00
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
*ret = j;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sd_journal_close(j);
|
|
|
|
|
|
|
|
return r;
|
2012-07-11 01:08:38 +02:00
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-06-06 01:39:26 +02:00
|
|
|
_public_ int sd_journal_open_files(sd_journal **ret, const char **paths, int flags) {
|
|
|
|
sd_journal *j;
|
|
|
|
const char **path;
|
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(ret, -EINVAL);
|
|
|
|
assert_return(flags == 0, -EINVAL);
|
2013-06-06 01:39:26 +02:00
|
|
|
|
|
|
|
j = journal_new(flags, NULL);
|
|
|
|
if (!j)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
STRV_FOREACH(path, paths) {
|
|
|
|
r = add_any_file(j, *path);
|
|
|
|
if (r < 0) {
|
2014-11-28 13:19:16 +01:00
|
|
|
log_error_errno(r, "Failed to open %s: %m", *path);
|
2013-06-06 01:39:26 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
j->no_new_files = true;
|
|
|
|
|
|
|
|
*ret = j;
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
sd_journal_close(j);
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ void sd_journal_close(sd_journal *j) {
|
2012-07-11 01:08:38 +02:00
|
|
|
Directory *d;
|
|
|
|
JournalFile *f;
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
if (!j)
|
|
|
|
return;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-03-23 04:11:23 +01:00
|
|
|
sd_journal_flush_matches(j);
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
while ((f = ordered_hashmap_steal_first(j->files)))
|
2012-07-11 01:08:38 +02:00
|
|
|
journal_file_close(f);
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ordered_hashmap_free(j->files);
|
2011-10-07 22:00:05 +02:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
while ((d = hashmap_first(j->directories_by_path)))
|
|
|
|
remove_directory(j, d);
|
2011-10-07 22:00:05 +02:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
while ((d = hashmap_first(j->directories_by_wd)))
|
|
|
|
remove_directory(j, d);
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
hashmap_free(j->directories_by_path);
|
|
|
|
hashmap_free(j->directories_by_wd);
|
2011-10-14 16:52:42 +02:00
|
|
|
|
2014-03-18 19:22:43 +01:00
|
|
|
safe_close(j->inotify_fd);
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-11-26 18:58:44 +01:00
|
|
|
if (j->mmap) {
|
|
|
|
log_debug("mmap cache statistics: %u hit, %u miss", mmap_cache_get_hit(j->mmap), mmap_cache_get_missed(j->mmap));
|
2012-08-14 22:02:24 +02:00
|
|
|
mmap_cache_unref(j->mmap);
|
2013-11-26 18:58:44 +01:00
|
|
|
}
|
2012-08-14 22:02:24 +02:00
|
|
|
|
2012-07-19 03:21:49 +02:00
|
|
|
free(j->path);
|
2013-12-11 23:10:36 +01:00
|
|
|
free(j->prefix);
|
2012-10-18 03:29:19 +02:00
|
|
|
free(j->unique_field);
|
2013-03-11 23:03:13 +01:00
|
|
|
set_free(j->errors);
|
2011-10-07 21:06:39 +02:00
|
|
|
free(j);
|
|
|
|
}
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_get_realtime_usec(sd_journal *j, uint64_t *ret) {
|
2011-10-14 04:44:50 +02:00
|
|
|
Object *o;
|
|
|
|
JournalFile *f;
|
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(ret, -EINVAL);
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
f = j->current_file;
|
|
|
|
if (!f)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
if (f->current_offset <= 0)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
*ret = le64toh(o->entry.realtime);
|
2011-11-08 18:20:03 +01:00
|
|
|
return 0;
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_get_monotonic_usec(sd_journal *j, uint64_t *ret, sd_id128_t *ret_boot_id) {
|
2011-10-14 04:44:50 +02:00
|
|
|
Object *o;
|
|
|
|
JournalFile *f;
|
|
|
|
int r;
|
|
|
|
sd_id128_t id;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
f = j->current_file;
|
|
|
|
if (!f)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
if (f->current_offset <= 0)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
if (ret_boot_id)
|
|
|
|
*ret_boot_id = o->entry.boot_id;
|
|
|
|
else {
|
|
|
|
r = sd_id128_get_boot(&id);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
if (!sd_id128_equal(id, o->entry.boot_id))
|
2012-01-04 18:33:36 +01:00
|
|
|
return -ESTALE;
|
2011-11-08 18:20:03 +01:00
|
|
|
}
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2012-07-11 01:36:55 +02:00
|
|
|
if (ret)
|
|
|
|
*ret = le64toh(o->entry.monotonic);
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
return 0;
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-07-13 00:10:37 +02:00
|
|
|
static bool field_is_valid(const char *field) {
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
assert(field);
|
|
|
|
|
|
|
|
if (isempty(field))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (startswith(field, "__"))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (p = field; *p; p++) {
|
|
|
|
|
|
|
|
if (*p == '_')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p >= 'A' && *p <= 'Z')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (*p >= '0' && *p <= '9')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **data, size_t *size) {
|
2011-10-14 04:44:50 +02:00
|
|
|
JournalFile *f;
|
|
|
|
uint64_t i, n;
|
|
|
|
size_t field_length;
|
|
|
|
int r;
|
|
|
|
Object *o;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(field, -EINVAL);
|
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
assert_return(size, -EINVAL);
|
|
|
|
assert_return(field_is_valid(field), -EINVAL);
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
f = j->current_file;
|
|
|
|
if (!f)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
if (f->current_offset <= 0)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
field_length = strlen(field);
|
|
|
|
|
|
|
|
n = journal_file_entry_n_items(o);
|
|
|
|
for (i = 0; i < n; i++) {
|
2012-03-16 11:59:04 +01:00
|
|
|
uint64_t p, l;
|
|
|
|
le64_t le_hash;
|
2011-10-14 04:44:50 +02:00
|
|
|
size_t t;
|
2014-07-08 00:31:10 +02:00
|
|
|
int compression;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
p = le64toh(o->entry.items[i].object_offset);
|
2011-12-21 02:40:59 +01:00
|
|
|
le_hash = o->entry.items[i].hash;
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
if (le_hash != o->data.hash)
|
2011-10-15 01:13:37 +02:00
|
|
|
return -EBADMSG;
|
|
|
|
|
2011-10-14 04:44:50 +02:00
|
|
|
l = le64toh(o->object.size) - offsetof(Object, data.payload);
|
|
|
|
|
2014-07-08 00:31:10 +02:00
|
|
|
compression = o->object.flags & OBJECT_COMPRESSION_MASK;
|
2014-07-11 16:42:06 +02:00
|
|
|
if (compression) {
|
|
|
|
#if defined(HAVE_XZ) || defined(HAVE_LZ4)
|
|
|
|
if (decompress_startswith(compression,
|
|
|
|
o->data.payload, l,
|
|
|
|
&f->compress_buffer, &f->compress_buffer_size,
|
|
|
|
field, field_length, '=')) {
|
|
|
|
|
2014-08-04 04:50:00 +02:00
|
|
|
size_t rsize;
|
2014-07-11 16:42:06 +02:00
|
|
|
|
|
|
|
r = decompress_blob(compression,
|
|
|
|
o->data.payload, l,
|
|
|
|
&f->compress_buffer, &f->compress_buffer_size, &rsize,
|
|
|
|
j->data_threshold);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-12-21 02:40:59 +01:00
|
|
|
|
2014-07-11 16:42:06 +02:00
|
|
|
*data = f->compress_buffer;
|
|
|
|
*size = (size_t) rsize;
|
2011-12-21 02:40:59 +01:00
|
|
|
|
2014-07-11 16:42:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
#endif
|
2011-12-21 02:40:59 +01:00
|
|
|
} else if (l >= field_length+1 &&
|
|
|
|
memcmp(o->data.payload, field, field_length) == 0 &&
|
|
|
|
o->data.payload[field_length] == '=') {
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-10-14 04:52:56 +02:00
|
|
|
t = (size_t) l;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-10-14 04:52:56 +02:00
|
|
|
if ((uint64_t) t != l)
|
|
|
|
return -E2BIG;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-10-14 04:52:56 +02:00
|
|
|
*data = o->data.payload;
|
|
|
|
*size = t;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2013-03-07 06:40:30 +01:00
|
|
|
return 0;
|
2011-10-14 04:52:56 +02:00
|
|
|
}
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
|
2011-10-14 04:52:56 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
return -ENOENT;
|
2011-10-14 04:44:50 +02:00
|
|
|
}
|
|
|
|
|
2012-11-21 00:28:00 +01:00
|
|
|
static int return_data(sd_journal *j, JournalFile *f, Object *o, const void **data, size_t *size) {
|
2012-10-18 03:29:19 +02:00
|
|
|
size_t t;
|
|
|
|
uint64_t l;
|
2014-07-11 16:42:06 +02:00
|
|
|
int compression;
|
2012-10-18 03:29:19 +02:00
|
|
|
|
|
|
|
l = le64toh(o->object.size) - offsetof(Object, data.payload);
|
|
|
|
t = (size_t) l;
|
|
|
|
|
|
|
|
/* We can't read objects larger than 4G on a 32bit machine */
|
|
|
|
if ((uint64_t) t != l)
|
|
|
|
return -E2BIG;
|
|
|
|
|
2014-07-08 00:31:10 +02:00
|
|
|
compression = o->object.flags & OBJECT_COMPRESSION_MASK;
|
|
|
|
if (compression) {
|
2014-07-11 16:42:06 +02:00
|
|
|
#if defined(HAVE_XZ) || defined(HAVE_LZ4)
|
2014-08-04 04:50:00 +02:00
|
|
|
size_t rsize;
|
2014-07-11 16:42:06 +02:00
|
|
|
int r;
|
2012-10-18 03:29:19 +02:00
|
|
|
|
2014-07-08 00:31:10 +02:00
|
|
|
r = decompress_blob(compression,
|
|
|
|
o->data.payload, l, &f->compress_buffer,
|
|
|
|
&f->compress_buffer_size, &rsize, j->data_threshold);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2012-10-18 03:29:19 +02:00
|
|
|
|
|
|
|
*data = f->compress_buffer;
|
|
|
|
*size = (size_t) rsize;
|
2014-07-11 16:42:06 +02:00
|
|
|
#else
|
|
|
|
return -EPROTONOSUPPORT;
|
|
|
|
#endif
|
2012-10-18 03:29:19 +02:00
|
|
|
} else {
|
|
|
|
*data = o->data.payload;
|
|
|
|
*size = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_enumerate_data(sd_journal *j, const void **data, size_t *size) {
|
2011-10-14 04:44:50 +02:00
|
|
|
JournalFile *f;
|
2012-10-18 03:29:19 +02:00
|
|
|
uint64_t p, n;
|
2012-03-16 11:59:04 +01:00
|
|
|
le64_t le_hash;
|
2011-10-14 04:44:50 +02:00
|
|
|
int r;
|
|
|
|
Object *o;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
assert_return(size, -EINVAL);
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
f = j->current_file;
|
|
|
|
if (!f)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
if (f->current_offset <= 0)
|
2011-11-08 18:20:03 +01:00
|
|
|
return -EADDRNOTAVAIL;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
n = journal_file_entry_n_items(o);
|
2011-10-14 05:38:40 +02:00
|
|
|
if (j->current_field >= n)
|
2011-10-14 04:44:50 +02:00
|
|
|
return 0;
|
|
|
|
|
2011-10-14 05:38:40 +02:00
|
|
|
p = le64toh(o->entry.items[j->current_field].object_offset);
|
2011-11-08 18:20:03 +01:00
|
|
|
le_hash = o->entry.items[j->current_field].hash;
|
|
|
|
r = journal_file_move_to_object(f, OBJECT_DATA, p, &o);
|
2011-10-14 04:44:50 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-11-08 18:20:03 +01:00
|
|
|
if (le_hash != o->data.hash)
|
2011-10-15 01:13:37 +02:00
|
|
|
return -EBADMSG;
|
|
|
|
|
2012-11-21 00:28:00 +01:00
|
|
|
r = return_data(j, f, o, data, size);
|
2012-10-18 03:29:19 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
2011-10-14 05:38:40 +02:00
|
|
|
j->current_field ++;
|
2011-10-14 04:44:50 +02:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-10-14 05:12:58 +02:00
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ void sd_journal_restart_data(sd_journal *j) {
|
|
|
|
if (!j)
|
|
|
|
return;
|
2011-10-15 02:45:58 +02:00
|
|
|
|
|
|
|
j->current_field = 0;
|
2011-10-14 05:12:58 +02:00
|
|
|
}
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_get_fd(sd_journal *j) {
|
2012-07-11 01:08:38 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
if (j->inotify_fd >= 0)
|
|
|
|
return j->inotify_fd;
|
|
|
|
|
|
|
|
r = allocate_inotify(j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Iterate through all dirs again, to add them to the
|
|
|
|
* inotify */
|
2013-06-06 01:39:26 +02:00
|
|
|
if (j->no_new_files)
|
|
|
|
r = add_current_paths(j);
|
|
|
|
else if (j->path)
|
2013-12-11 23:10:36 +01:00
|
|
|
r = add_root_directory(j, j->path);
|
2012-07-19 03:21:49 +02:00
|
|
|
else
|
2013-12-11 23:10:36 +01:00
|
|
|
r = add_search_paths(j);
|
2012-07-11 01:08:38 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2011-12-19 22:35:46 +01:00
|
|
|
return j->inotify_fd;
|
|
|
|
}
|
|
|
|
|
2013-04-04 17:22:28 +02:00
|
|
|
_public_ int sd_journal_get_events(sd_journal *j) {
|
|
|
|
int fd;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2013-04-04 17:22:28 +02:00
|
|
|
|
|
|
|
fd = sd_journal_get_fd(j);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
return POLLIN;
|
|
|
|
}
|
|
|
|
|
2013-04-04 20:07:48 +02:00
|
|
|
_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
|
|
|
|
int fd;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(timeout_usec, -EINVAL);
|
2013-04-04 20:07:48 +02:00
|
|
|
|
|
|
|
fd = sd_journal_get_fd(j);
|
|
|
|
if (fd < 0)
|
|
|
|
return fd;
|
|
|
|
|
|
|
|
if (!j->on_network) {
|
|
|
|
*timeout_usec = (uint64_t) -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are on the network we need to regularly check for
|
|
|
|
* changes manually */
|
|
|
|
|
|
|
|
*timeout_usec = j->last_process_usec + JOURNAL_FILES_RECHECK_USEC;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2011-12-19 22:35:46 +01:00
|
|
|
static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
|
2012-07-11 01:08:38 +02:00
|
|
|
Directory *d;
|
2011-12-19 22:35:46 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
assert(e);
|
|
|
|
|
|
|
|
/* Is this a subdirectory we watch? */
|
2012-07-11 01:08:38 +02:00
|
|
|
d = hashmap_get(j->directories_by_wd, INT_TO_PTR(e->wd));
|
|
|
|
if (d) {
|
|
|
|
sd_id128_t id;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-08-07 00:51:17 +02:00
|
|
|
if (!(e->mask & IN_ISDIR) && e->len > 0 &&
|
|
|
|
(endswith(e->name, ".journal") ||
|
|
|
|
endswith(e->name, ".journal~"))) {
|
2011-12-19 22:35:46 +01:00
|
|
|
|
|
|
|
/* Event for a journal file */
|
|
|
|
|
|
|
|
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
|
2012-07-11 01:08:38 +02:00
|
|
|
r = add_file(j, d->path, e->name);
|
2013-03-11 23:03:13 +01:00
|
|
|
if (r < 0) {
|
2014-11-28 17:09:20 +01:00
|
|
|
log_debug_errno(r, "Failed to add file %s/%s: %m",
|
|
|
|
d->path, e->name);
|
2013-03-23 01:12:22 +01:00
|
|
|
set_put_error(j, r);
|
2013-03-11 23:03:13 +01:00
|
|
|
}
|
2012-07-11 01:08:38 +02:00
|
|
|
|
2012-10-16 23:00:04 +02:00
|
|
|
} else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
r = remove_file(j, d->path, e->name);
|
2011-12-19 22:35:46 +01:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_debug_errno(r, "Failed to remove file %s/%s: %m", d->path, e->name);
|
2011-12-19 22:35:46 +01:00
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
} else if (!d->is_root && e->len == 0) {
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
/* Event for a subdirectory */
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2015-08-14 23:40:27 +02:00
|
|
|
if (e->mask & (IN_DELETE_SELF|IN_MOVE_SELF|IN_UNMOUNT))
|
|
|
|
remove_directory(j, d);
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
} else if (d->is_root && (e->mask & IN_ISDIR) && e->len > 0 && sd_id128_from_string(e->name, &id) >= 0) {
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
/* Event for root directory */
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
|
|
|
|
r = add_directory(j, d->path, e->name);
|
2011-12-19 22:35:46 +01:00
|
|
|
if (r < 0)
|
2014-11-28 13:19:16 +01:00
|
|
|
log_debug_errno(r, "Failed to add directory %s/%s: %m", d->path, e->name);
|
2011-12-19 22:35:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e->mask & IN_IGNORED)
|
|
|
|
return;
|
|
|
|
|
|
|
|
log_warning("Unknown inotify event.");
|
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
static int determine_change(sd_journal *j) {
|
|
|
|
bool b;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
b = j->current_invalidate_counter != j->last_invalidate_counter;
|
|
|
|
j->last_invalidate_counter = j->current_invalidate_counter;
|
|
|
|
|
|
|
|
return b ? SD_JOURNAL_INVALIDATE : SD_JOURNAL_APPEND;
|
|
|
|
}
|
|
|
|
|
2012-01-03 20:54:15 +01:00
|
|
|
_public_ int sd_journal_process(sd_journal *j) {
|
2012-07-11 01:08:38 +02:00
|
|
|
bool got_something = false;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2011-12-19 22:35:46 +01:00
|
|
|
|
2013-04-04 20:07:48 +02:00
|
|
|
j->last_process_usec = now(CLOCK_MONOTONIC);
|
|
|
|
|
2011-12-19 22:35:46 +01:00
|
|
|
for (;;) {
|
2014-12-23 22:47:16 +01:00
|
|
|
union inotify_event_buffer buffer;
|
2011-12-19 22:35:46 +01:00
|
|
|
struct inotify_event *e;
|
|
|
|
ssize_t l;
|
|
|
|
|
2014-12-23 22:47:16 +01:00
|
|
|
l = read(j->inotify_fd, &buffer, sizeof(buffer));
|
2011-12-19 22:35:46 +01:00
|
|
|
if (l < 0) {
|
2012-07-11 01:08:38 +02:00
|
|
|
if (errno == EAGAIN || errno == EINTR)
|
|
|
|
return got_something ? determine_change(j) : SD_JOURNAL_NOP;
|
2011-12-19 22:35:46 +01:00
|
|
|
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
got_something = true;
|
|
|
|
|
2014-12-10 01:45:43 +01:00
|
|
|
FOREACH_INOTIFY_EVENT(e, buffer, l)
|
2011-12-19 22:35:46 +01:00
|
|
|
process_inotify_event(j, e);
|
|
|
|
}
|
|
|
|
}
|
2011-12-22 00:35:04 +01:00
|
|
|
|
2012-07-10 21:46:11 +02:00
|
|
|
_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
|
2012-07-11 01:08:38 +02:00
|
|
|
int r;
|
2013-04-04 20:07:48 +02:00
|
|
|
uint64_t t;
|
2012-07-10 21:46:11 +02:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2012-07-10 21:46:11 +02:00
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
if (j->inotify_fd < 0) {
|
|
|
|
|
|
|
|
/* This is the first invocation, hence create the
|
|
|
|
* inotify watch */
|
|
|
|
r = sd_journal_get_fd(j);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* The journal might have changed since the context
|
|
|
|
* object was created and we weren't watching before,
|
|
|
|
* hence don't wait for anything, and return
|
|
|
|
* immediately. */
|
|
|
|
return determine_change(j);
|
|
|
|
}
|
|
|
|
|
2013-04-04 20:07:48 +02:00
|
|
|
r = sd_journal_get_timeout(j, &t);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (t != (uint64_t) -1) {
|
|
|
|
usec_t n;
|
|
|
|
|
|
|
|
n = now(CLOCK_MONOTONIC);
|
|
|
|
t = t > n ? t - n : 0;
|
2012-10-26 20:05:19 +02:00
|
|
|
|
2013-04-04 20:07:48 +02:00
|
|
|
if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
|
|
|
|
timeout_usec = t;
|
2012-10-26 20:05:19 +02:00
|
|
|
}
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
do {
|
|
|
|
r = fd_wait_for_event(j->inotify_fd, POLLIN, timeout_usec);
|
|
|
|
} while (r == -EINTR);
|
2012-07-10 21:46:11 +02:00
|
|
|
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2012-07-11 01:08:38 +02:00
|
|
|
return sd_journal_process(j);
|
2012-07-10 21:46:11 +02:00
|
|
|
}
|
|
|
|
|
2012-06-09 10:32:38 +02:00
|
|
|
_public_ int sd_journal_get_cutoff_realtime_usec(sd_journal *j, uint64_t *from, uint64_t *to) {
|
|
|
|
Iterator i;
|
|
|
|
JournalFile *f;
|
|
|
|
bool first = true;
|
2014-02-19 17:49:50 +01:00
|
|
|
uint64_t fmin = 0, tmax = 0;
|
2012-06-09 10:32:38 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(from || to, -EINVAL);
|
|
|
|
assert_return(from != to, -EINVAL);
|
2012-06-09 10:32:38 +02:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
2012-06-09 10:32:38 +02:00
|
|
|
usec_t fr, t;
|
|
|
|
|
|
|
|
r = journal_file_get_cutoff_realtime_usec(f, &fr, &t);
|
2012-07-19 02:03:22 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
continue;
|
2012-06-09 10:32:38 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (first) {
|
2014-02-19 17:49:50 +01:00
|
|
|
fmin = fr;
|
|
|
|
tmax = t;
|
2012-06-09 10:32:38 +02:00
|
|
|
first = false;
|
|
|
|
} else {
|
2014-02-19 17:49:50 +01:00
|
|
|
fmin = MIN(fr, fmin);
|
|
|
|
tmax = MAX(t, tmax);
|
2012-06-09 10:32:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-19 17:49:50 +01:00
|
|
|
if (from)
|
|
|
|
*from = fmin;
|
|
|
|
if (to)
|
|
|
|
*to = tmax;
|
|
|
|
|
2012-06-09 10:32:38 +02:00
|
|
|
return first ? 0 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_get_cutoff_monotonic_usec(sd_journal *j, sd_id128_t boot_id, uint64_t *from, uint64_t *to) {
|
|
|
|
Iterator i;
|
|
|
|
JournalFile *f;
|
2014-07-19 03:44:36 +02:00
|
|
|
bool found = false;
|
2012-06-09 10:32:38 +02:00
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(from || to, -EINVAL);
|
|
|
|
assert_return(from != to, -EINVAL);
|
2012-06-09 10:32:38 +02:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
2012-06-09 10:32:38 +02:00
|
|
|
usec_t fr, t;
|
|
|
|
|
|
|
|
r = journal_file_get_cutoff_monotonic_usec(f, boot_id, &fr, &t);
|
2012-07-19 02:03:22 +02:00
|
|
|
if (r == -ENOENT)
|
|
|
|
continue;
|
2012-06-09 10:32:38 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
if (r == 0)
|
|
|
|
continue;
|
|
|
|
|
2014-07-19 03:44:36 +02:00
|
|
|
if (found) {
|
2012-06-09 10:32:38 +02:00
|
|
|
if (from)
|
2014-07-19 03:44:36 +02:00
|
|
|
*from = MIN(fr, *from);
|
2012-06-09 10:32:38 +02:00
|
|
|
if (to)
|
2014-07-19 03:44:36 +02:00
|
|
|
*to = MAX(t, *to);
|
2012-06-09 10:32:38 +02:00
|
|
|
} else {
|
|
|
|
if (from)
|
2014-07-19 03:44:36 +02:00
|
|
|
*from = fr;
|
2012-06-09 10:32:38 +02:00
|
|
|
if (to)
|
2014-07-19 03:44:36 +02:00
|
|
|
*to = t;
|
|
|
|
found = true;
|
2012-06-09 10:32:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-19 03:44:36 +02:00
|
|
|
return found;
|
2012-06-09 10:32:38 +02:00
|
|
|
}
|
|
|
|
|
2012-07-16 22:24:02 +02:00
|
|
|
void journal_print_header(sd_journal *j) {
|
|
|
|
Iterator i;
|
|
|
|
JournalFile *f;
|
|
|
|
bool newline = false;
|
|
|
|
|
|
|
|
assert(j);
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
2012-07-16 22:24:02 +02:00
|
|
|
if (newline)
|
|
|
|
putchar('\n');
|
|
|
|
else
|
|
|
|
newline = true;
|
|
|
|
|
|
|
|
journal_file_print_header(f);
|
|
|
|
}
|
|
|
|
}
|
2012-06-09 10:32:38 +02:00
|
|
|
|
2012-09-07 23:20:28 +02:00
|
|
|
_public_ int sd_journal_get_usage(sd_journal *j, uint64_t *bytes) {
|
|
|
|
Iterator i;
|
|
|
|
JournalFile *f;
|
|
|
|
uint64_t sum = 0;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(bytes, -EINVAL);
|
2012-09-07 23:20:28 +02:00
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(f, j->files, i) {
|
2012-09-07 23:20:28 +02:00
|
|
|
struct stat st;
|
|
|
|
|
|
|
|
if (fstat(f->fd, &st) < 0)
|
|
|
|
return -errno;
|
|
|
|
|
|
|
|
sum += (uint64_t) st.st_blocks * 512ULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bytes = sum;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
_public_ int sd_journal_query_unique(sd_journal *j, const char *field) {
|
|
|
|
char *f;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(!isempty(field), -EINVAL);
|
|
|
|
assert_return(field_is_valid(field), -EINVAL);
|
2012-10-18 03:29:19 +02:00
|
|
|
|
|
|
|
f = strdup(field);
|
|
|
|
if (!f)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
free(j->unique_field);
|
|
|
|
j->unique_field = f;
|
|
|
|
j->unique_file = NULL;
|
|
|
|
j->unique_offset = 0;
|
2014-10-10 04:44:29 +02:00
|
|
|
j->unique_file_lost = false;
|
2012-10-18 03:29:19 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_enumerate_unique(sd_journal *j, const void **data, size_t *l) {
|
|
|
|
size_t k;
|
2012-01-11 03:23:04 +01:00
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(data, -EINVAL);
|
|
|
|
assert_return(l, -EINVAL);
|
|
|
|
assert_return(j->unique_field, -EINVAL);
|
2012-01-11 03:23:04 +01:00
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
k = strlen(j->unique_field);
|
2012-01-11 03:23:04 +01:00
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
if (!j->unique_file) {
|
2014-10-10 04:44:29 +02:00
|
|
|
if (j->unique_file_lost)
|
|
|
|
return 0;
|
|
|
|
|
2014-08-19 13:38:53 +02:00
|
|
|
j->unique_file = ordered_hashmap_first(j->files);
|
2012-10-18 03:29:19 +02:00
|
|
|
if (!j->unique_file)
|
|
|
|
return 0;
|
2014-10-10 04:44:29 +02:00
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
j->unique_offset = 0;
|
|
|
|
}
|
2012-01-11 03:23:04 +01:00
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
for (;;) {
|
|
|
|
JournalFile *of;
|
|
|
|
Iterator i;
|
2013-12-29 01:33:23 +01:00
|
|
|
Object *o;
|
2012-10-18 03:29:19 +02:00
|
|
|
const void *odata;
|
|
|
|
size_t ol;
|
|
|
|
bool found;
|
2013-12-29 01:33:23 +01:00
|
|
|
int r;
|
2012-10-18 03:29:19 +02:00
|
|
|
|
2012-10-18 22:36:50 +02:00
|
|
|
/* Proceed to next data object in the field's linked list */
|
2012-10-18 03:29:19 +02:00
|
|
|
if (j->unique_offset == 0) {
|
|
|
|
r = journal_file_find_field_object(j->unique_file, j->unique_field, k, &o, NULL);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
j->unique_offset = r > 0 ? le64toh(o->field.head_data_offset) : 0;
|
|
|
|
} else {
|
|
|
|
r = journal_file_move_to_object(j->unique_file, OBJECT_DATA, j->unique_offset, &o);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
j->unique_offset = le64toh(o->data.next_field_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We reached the end of the list? Then start again, with the next file */
|
|
|
|
if (j->unique_offset == 0) {
|
2014-08-19 13:38:53 +02:00
|
|
|
j->unique_file = ordered_hashmap_next(j->files, j->unique_file->path);
|
2014-10-10 04:44:29 +02:00
|
|
|
if (!j->unique_file)
|
2012-10-18 03:29:19 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-12-12 22:51:24 +01:00
|
|
|
/* We do not use OBJECT_DATA context here, but OBJECT_UNUSED
|
|
|
|
* instead, so that we can look at this data object at the same
|
2012-10-18 03:29:19 +02:00
|
|
|
* time as one on another file */
|
2014-12-12 22:51:24 +01:00
|
|
|
r = journal_file_move_to_object(j->unique_file, OBJECT_UNUSED, j->unique_offset, &o);
|
2012-10-18 03:29:19 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
/* Let's do the type check by hand, since we used 0 context above. */
|
2013-12-29 01:33:23 +01:00
|
|
|
if (o->object.type != OBJECT_DATA) {
|
2014-08-26 21:47:46 +02:00
|
|
|
log_debug("%s:offset " OFSfmt ": object has type %d, expected %d",
|
2013-12-29 01:33:23 +01:00
|
|
|
j->unique_file->path, j->unique_offset,
|
|
|
|
o->object.type, OBJECT_DATA);
|
2012-10-18 03:29:19 +02:00
|
|
|
return -EBADMSG;
|
2013-12-29 01:33:23 +01:00
|
|
|
}
|
|
|
|
|
2012-11-21 00:28:00 +01:00
|
|
|
r = return_data(j, j->unique_file, o, &odata, &ol);
|
2012-10-18 03:29:19 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2014-08-27 05:54:31 +02:00
|
|
|
/* Check if we have at least the field name and "=". */
|
|
|
|
if (ol <= k) {
|
|
|
|
log_debug("%s:offset " OFSfmt ": object has size %zu, expected at least %zu",
|
|
|
|
j->unique_file->path, j->unique_offset,
|
|
|
|
ol, k + 1);
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (memcmp(odata, j->unique_field, k) || ((const char*) odata)[k] != '=') {
|
|
|
|
log_debug("%s:offset " OFSfmt ": object does not start with \"%s=\"",
|
|
|
|
j->unique_file->path, j->unique_offset,
|
|
|
|
j->unique_field);
|
|
|
|
return -EBADMSG;
|
|
|
|
}
|
|
|
|
|
2012-10-18 03:29:19 +02:00
|
|
|
/* OK, now let's see if we already returned this data
|
|
|
|
* object by checking if it exists in the earlier
|
|
|
|
* traversed files. */
|
|
|
|
found = false;
|
2014-08-19 13:38:53 +02:00
|
|
|
ORDERED_HASHMAP_FOREACH(of, j->files, i) {
|
2012-10-18 03:29:19 +02:00
|
|
|
Object *oo;
|
|
|
|
uint64_t op;
|
|
|
|
|
|
|
|
if (of == j->unique_file)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Skip this file it didn't have any fields
|
|
|
|
* indexed */
|
|
|
|
if (JOURNAL_HEADER_CONTAINS(of->header, n_fields) &&
|
|
|
|
le64toh(of->header->n_fields) <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
r = journal_file_find_data_object_with_hash(of, odata, ol, le64toh(o->data.hash), &oo, &op);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
if (r > 0)
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
|
2014-09-06 10:36:34 +02:00
|
|
|
if (found)
|
|
|
|
continue;
|
|
|
|
|
2012-11-21 00:28:00 +01:00
|
|
|
r = return_data(j, j->unique_file, o, data, l);
|
2012-10-18 03:29:19 +02:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-18 21:18:20 +02:00
|
|
|
_public_ void sd_journal_restart_unique(sd_journal *j) {
|
2012-10-18 03:29:19 +02:00
|
|
|
if (!j)
|
|
|
|
return;
|
|
|
|
|
|
|
|
j->unique_file = NULL;
|
|
|
|
j->unique_offset = 0;
|
2014-10-10 04:44:29 +02:00
|
|
|
j->unique_file_lost = false;
|
2012-10-18 03:29:19 +02:00
|
|
|
}
|
2012-10-26 20:05:19 +02:00
|
|
|
|
|
|
|
_public_ int sd_journal_reliable_fd(sd_journal *j) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2012-10-26 20:05:19 +02:00
|
|
|
|
|
|
|
return !j->on_network;
|
|
|
|
}
|
2012-11-15 23:03:31 +01:00
|
|
|
|
|
|
|
static char *lookup_field(const char *field, void *userdata) {
|
|
|
|
sd_journal *j = userdata;
|
|
|
|
const void *data;
|
|
|
|
size_t size, d;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
assert(field);
|
|
|
|
assert(j);
|
|
|
|
|
|
|
|
r = sd_journal_get_data(j, field, &data, &size);
|
|
|
|
if (r < 0 ||
|
|
|
|
size > REPLACE_VAR_MAX)
|
|
|
|
return strdup(field);
|
|
|
|
|
|
|
|
d = strlen(field) + 1;
|
|
|
|
|
|
|
|
return strndup((const char*) data + d, size - d);
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_get_catalog(sd_journal *j, char **ret) {
|
|
|
|
const void *data;
|
|
|
|
size_t size;
|
|
|
|
sd_id128_t id;
|
|
|
|
_cleanup_free_ char *text = NULL, *cid = NULL;
|
|
|
|
char *t;
|
|
|
|
int r;
|
|
|
|
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(ret, -EINVAL);
|
2012-11-15 23:03:31 +01:00
|
|
|
|
|
|
|
r = sd_journal_get_data(j, "MESSAGE_ID", &data, &size);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
cid = strndup((const char*) data + 11, size - 11);
|
|
|
|
if (!cid)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
r = sd_id128_from_string(cid, &id);
|
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
2013-03-29 01:17:24 +01:00
|
|
|
r = catalog_get(CATALOG_DATABASE, id, &text);
|
2012-11-15 23:03:31 +01:00
|
|
|
if (r < 0)
|
|
|
|
return r;
|
|
|
|
|
|
|
|
t = replace_var(text, lookup_field, j);
|
|
|
|
if (!t)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*ret = t;
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-20 21:38:59 +01:00
|
|
|
|
|
|
|
_public_ int sd_journal_get_catalog_for_message_id(sd_id128_t id, char **ret) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(ret, -EINVAL);
|
2012-11-20 21:38:59 +01:00
|
|
|
|
2013-03-29 01:17:24 +01:00
|
|
|
return catalog_get(CATALOG_DATABASE, id, ret);
|
2012-11-20 21:38:59 +01:00
|
|
|
}
|
2012-11-21 00:28:00 +01:00
|
|
|
|
|
|
|
_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
2012-11-21 00:28:00 +01:00
|
|
|
|
|
|
|
j->data_threshold = sz;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
_public_ int sd_journal_get_data_threshold(sd_journal *j, size_t *sz) {
|
2013-12-02 22:42:01 +01:00
|
|
|
assert_return(j, -EINVAL);
|
|
|
|
assert_return(!journal_pid_changed(j), -ECHILD);
|
|
|
|
assert_return(sz, -EINVAL);
|
2012-11-21 00:28:00 +01:00
|
|
|
|
|
|
|
*sz = j->data_threshold;
|
|
|
|
return 0;
|
|
|
|
}
|