journal-upload-journal.c revision eacbb4d33e2bb5c54311544851140efe3dd0f774
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <stdbool.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include <curl/curl.h>
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "util.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "log.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "utf8.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering#include "journal-upload.h"
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering/**
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * Write up to size bytes to buf. Return negative on error, and number of
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering * bytes written otherwise. The last case is a kind of an error too.
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic ssize_t write_entry(char *buf, size_t size, Uploader *u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering size_t pos = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(size <= SSIZE_MAX);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering while (true) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering switch(u->entry_state) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering case ENTRY_CURSOR: {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering free(u->last_cursor);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->last_cursor = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_get_cursor(u->journal, &u->last_cursor);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("Failed to get cursor: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = snprintf(buf + pos, size - pos,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "__CURSOR=%s\n", u->last_cursor);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (pos + r > size)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (pos + r == size) {
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering /* exactly one character short, but we don't need it */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering buf[size - 1] = '\n';
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return size;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering pos += r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering case ENTRY_REALTIME: {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering usec_t realtime;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_get_realtime_usec(u->journal, &realtime);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to get realtime timestamp: %s", strerror(-r));
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering return r;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering }
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = snprintf(buf + pos, size - pos,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r + pos > size)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering if (r + pos == size) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* exactly one character short, but we don't need it */
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering buf[size - 1] = '\n';
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering return size;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering pos += r;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering } /* fall through */
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering case ENTRY_MONOTONIC: {
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering usec_t monotonic;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering sd_id128_t boot_id;
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering r = sd_journal_get_monotonic_usec(u->journal, &monotonic, &boot_id);
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering if (r < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to get monotonic timestamp: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = snprintf(buf + pos, size - pos,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "__MONOTONIC_TIMESTAMP="USEC_FMT"\n", monotonic);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r + pos > size)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r + pos == size) {
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek /* exactly one character short, but we don't need it */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering buf[size - 1] = '\n';
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return size;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering pos += r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering case ENTRY_BOOT_ID: {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering sd_id128_t boot_id;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering char sid[33];
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_get_monotonic_usec(u->journal, NULL, &boot_id);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to get monotonic timestamp: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = snprintf(buf + pos, size - pos,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r + pos> size)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r + pos == size) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek /* exactly one character short, but we don't need it */
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek buf[size - 1] = '\n';
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return size;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek }
83f6936a018b08880670838756e0f4e9ea98b4a7Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering pos += r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek case ENTRY_NEW_FIELD: {
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek u->field_pos = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_enumerate_data(u->journal,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering &u->field_data,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering &u->field_length);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to move to next field in entry: %s",
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering strerror(-r));
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return r;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek } else if (r == 0) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek u->entry_state = ENTRY_OUTRO;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek continue;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek }
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (!utf8_is_printable_newline(u->field_data,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek u->field_length, false)) {
4b8268f843b0da1cfe1995d93a0b1f95faccc454Zbigniew Jędrzejewski-Szmek u->entry_state = ENTRY_BINARY_FIELD_START;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek continue;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek }
4b8268f843b0da1cfe1995d93a0b1f95faccc454Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek u->entry_state ++;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek } /* fall through */
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek case ENTRY_TEXT_FIELD:
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek case ENTRY_BINARY_FIELD: {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek bool done;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek size_t tocopy;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek done = size - pos > u->field_length - u->field_pos;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (done)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek tocopy = u->field_length - u->field_pos;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering else
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering tocopy = size - pos;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering memcpy(buf + pos,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek (char*) u->field_data + u->field_pos,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering tocopy);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (done) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering buf[pos + tocopy] = '\n';
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering pos += tocopy + 1;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state = ENTRY_NEW_FIELD;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering continue;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } else {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->field_pos += tocopy;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return size;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek case ENTRY_BINARY_FIELD_START: {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek const char *c;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek size_t len;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek c = memchr(u->field_data, '=', u->field_length);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (!c || c == u->field_data) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Invalid field.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return -EINVAL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering len = c - (const char*)u->field_data;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* need space for label + '\n' */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (size - pos < len + 1)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering memcpy(buf + pos, u->field_data, len);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering buf[pos + len] = '\n';
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering pos += len + 1;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->field_pos = len + 1;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering case ENTRY_BINARY_FIELD_SIZE: {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering uint64_t le64;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* need space for uint64_t */
d3b6d0c21ea5a0d15ec6dbd8b8d179138b7463bcZbigniew Jędrzejewski-Szmek if (size - pos < 8)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering le64 = htole64(u->field_length - u->field_pos);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering memcpy(buf + pos, &le64, 8);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering pos += 8;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek continue;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering case ENTRY_OUTRO:
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* need space for '\n' */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (size - pos < 1)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek buf[pos++] = '\n';
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entries_sent ++;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return pos;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering default:
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert_not_reached("WTF?");
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek }
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek }
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert_not_reached("WTF?");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering Uploader *u = userp;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering sd_journal *j;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering size_t filled = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering ssize_t w;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(u);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(nmemb <= SSIZE_MAX / size);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering j = u->journal;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering while (j && filled < size * nmemb) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (u->entry_state == ENTRY_DONE) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek r = sd_journal_next(j);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (r < 0) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek log_error("Failed to move to next entry in journal: %s",
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek strerror(-r));
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek return CURL_READFUNC_ABORT;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek } else if (r == 0) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (u->input_event)
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek log_debug("No more entries, waiting for journal.");
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek else {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("No more entries, closing journal.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering close_journal_input(u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->uploading = false;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek break;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entry_state = ENTRY_CURSOR;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering w = write_entry((char*)buf + filled, size * nmemb - filled, u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (w < 0)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return CURL_READFUNC_ABORT;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering filled += w;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (filled == 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Buffer space is too small to write entry.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return CURL_READFUNC_ABORT;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } else if (u->entry_state != ENTRY_DONE)
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* This means that all available space was used up */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering break;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_debug("Entry %zu (%s) has been uploaded.",
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->entries_sent, u->last_cursor);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return filled;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringvoid close_journal_input(Uploader *u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering assert(u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (u->journal) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_debug("Closing journal input.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering sd_journal_close(u->journal);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->journal = NULL;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering u->timeout = 0;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering}
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic int process_journal_input(Uploader *u, int skip) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering int r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_next_skip(u->journal, skip);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to skip to next entry: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek } else if (r < skip)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek /* have data */
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek u->entry_state = ENTRY_CURSOR;
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer return start_upload(u, journal_input_callback, u);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek}
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint check_journal_input(Uploader *u) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (u->input_event) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = sd_journal_process(u->journal);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (r < 0) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("Failed to process journal: %s", strerror(-r));
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek close_journal_input(u);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return r;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (r == SD_JOURNAL_NOP)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return process_journal_input(u, 1);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek}
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekstatic int dispatch_journal_input(sd_event_source *event,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int fd,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek uint32_t revents,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek void *userp) {
80343dc19a9bcd703275ad2ad88f43e5310559d6Zbigniew Jędrzejewski-Szmek Uploader *u = userp;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(u);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (u->uploading) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_warning("dispatch_journal_input called when uploading, ignoring.");
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return 0;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_debug("Detected journal input, checking for new data.");
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return check_journal_input(u);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek}
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint open_journal_for_upload(Uploader *u,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek sd_journal *j,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek const char *cursor,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek bool after_cursor,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek bool follow) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek int fd, r, events;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek u->journal = j;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek sd_journal_set_data_threshold(j, 0);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (follow) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek fd = sd_journal_get_fd(j);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (fd < 0) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("sd_journal_get_fd failed: %s", strerror(-fd));
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return fd;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek }
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek events = sd_journal_get_events(j);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = sd_journal_reliable_fd(j);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek assert(r >= 0);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (r > 0)
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek u->timeout = -1;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek else
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek u->timeout = JOURNAL_UPLOAD_POLL_TIMEOUT;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = sd_event_add_io(u->events, &u->input_event,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek fd, events, dispatch_journal_input, u);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (r < 0) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("Failed to register input event: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_debug("Listening for journal events on fd:%d, timeout %d",
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering fd, u->timeout == (uint64_t) -1 ? -1 : (int) u->timeout);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } else
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_debug("Not listening for journal events.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek if (cursor) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_seek_cursor(j, cursor);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering if (r < 0) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to seek to cursor %s: %s",
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek cursor, strerror(-r));
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return r;
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering }
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering return process_journal_input(u, 1 + !!after_cursor);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek}
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering