/* We return an error here only if we didn't manage to
memorize the real error. */
-static int set_put_error(Set* errors, int r) {
+static int set_put_error(sd_journal *j, int r) {
+ int k;
+
if (r >= 0)
return r;
- return set_put(errors, INT_TO_PTR(r));
+ k = set_ensure_allocated(&j->errors, trivial_hash_func, trivial_compare_func);
+ if (k < 0)
+ return k;
+
+ return set_put(j->errors, INT_TO_PTR(r));
}
static void detach_location(sd_journal *j) {
return r;
if ((direction == DIRECTION_DOWN ? cp >= after_offset : cp <= after_offset) &&
- (np == 0 || (direction == DIRECTION_DOWN ? cp > np : np < cp))) {
+ (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))) {
np = cp;
continue_looking = true;
}
if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
log_debug("Too many open journal files, not adding %s, ignoring.", path);
- return set_put_error(j->errors, -ETOOMANYREFS);
+ return set_put_error(j, -ETOOMANYREFS);
}
r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
if (r < 0) {
log_debug("Failed to add file %s/%s: %s",
m->path, de->d_name, strerror(-r));
- r = set_put_error(j->errors, r);
+ r = set_put_error(j, r);
if (r < 0)
return r;
}
if (r < 0) {
log_debug("Failed to add file %s/%s: %s",
m->path, de->d_name, strerror(-r));
- r = set_put_error(j->errors, r);
+ r = set_put_error(j, r);
if (r < 0)
return r;
}
NULSTR_FOREACH(p, search_paths) {
r = add_root_directory(j, p);
- if (r < 0)
- return set_put_error(j->errors, r);
+ if (r < 0 && r != -ENOENT) {
+ r = set_put_error(j, r);
+ if (r < 0)
+ return r;
+ }
}
return 0;
j->files = hashmap_new(string_hash_func, string_compare_func);
j->directories_by_path = hashmap_new(string_hash_func, string_compare_func);
j->mmap = mmap_cache_new();
- j->errors = set_new(trivial_hash_func, trivial_compare_func);
- if (!j->files || !j->directories_by_path || !j->mmap || !j->errors)
+ if (!j->files || !j->directories_by_path || !j->mmap)
goto fail;
return j;
r = add_root_directory(j, path);
if (r < 0) {
- set_put_error(j->errors, r);
+ set_put_error(j, r);
goto fail;
}
if (!j)
return;
+ sd_journal_flush_matches(j);
+
while ((f = hashmap_steal_first(j->files)))
journal_file_close(f);
if (j->inotify_fd >= 0)
close_nointr_nofail(j->inotify_fd);
- sd_journal_flush_matches(j);
-
if (j->mmap)
mmap_cache_unref(j->mmap);
return j->inotify_fd;
}
+_public_ int sd_journal_get_events(sd_journal *j) {
+ int fd;
+
+ if (!j)
+ return -EINVAL;
+
+ fd = sd_journal_get_fd(j);
+ if (fd < 0)
+ return fd;
+
+ return POLLIN;
+}
+
+_public_ int sd_journal_get_timeout(sd_journal *j, uint64_t *timeout_usec) {
+ int fd;
+
+ if (!j)
+ return -EINVAL;
+ if (!timeout_usec)
+ return -EINVAL;
+
+ 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;
+}
+
static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
Directory *d;
int r;
if (r < 0) {
log_debug("Failed to add file %s/%s: %s",
d->path, e->name, strerror(-r));
- set_put_error(j->errors, r);
+ set_put_error(j, r);
}
} else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {
if (!j)
return -EINVAL;
+ j->last_process_usec = now(CLOCK_MONOTONIC);
+
for (;;) {
struct inotify_event *e;
ssize_t l;
_public_ int sd_journal_wait(sd_journal *j, uint64_t timeout_usec) {
int r;
+ uint64_t t;
assert(j);
return determine_change(j);
}
- if (j->on_network) {
- /* If we are on the network we need to regularly check
- * for changes manually */
+ 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;
- if (timeout_usec == (uint64_t) -1 || timeout_usec > JOURNAL_FILES_RECHECK_USEC)
- timeout_usec = JOURNAL_FILES_RECHECK_USEC;
+ if (timeout_usec == (uint64_t) -1 || timeout_usec > t)
+ timeout_usec = t;
}
do {
if (r < 0)
return r;
- r = catalog_get(id, &text);
+ r = catalog_get(CATALOG_DATABASE, id, &text);
if (r < 0)
return r;
if (!ret)
return -EINVAL;
- return catalog_get(id, ret);
+ return catalog_get(CATALOG_DATABASE, id, ret);
}
_public_ int sd_journal_set_data_threshold(sd_journal *j, size_t sz) {