journal-upload-journal.c revision eacbb4d33e2bb5c54311544851140efe3dd0f774
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 Poetteringstatic ssize_t write_entry(char *buf, size_t size, Uploader *u) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering while (true) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_get_cursor(u->journal, &u->last_cursor);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("Failed to get cursor: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering /* exactly one character short, but we don't need it */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_get_realtime_usec(u->journal, &realtime);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to get realtime timestamp: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* exactly one character short, but we don't need it */
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering } /* fall through */
9bf3b53533cdc9b95c921b71da755401f223f765Lennart Poettering r = sd_journal_get_monotonic_usec(u->journal, &monotonic, &boot_id);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to get monotonic timestamp: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "__MONOTONIC_TIMESTAMP="USEC_FMT"\n", monotonic);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
18cd5fe99f70a55a2d6f2303d6ee0624942695b1Zbigniew Jędrzejewski-Szmek /* exactly one character short, but we don't need it */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_get_monotonic_usec(u->journal, NULL, &boot_id);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to get monotonic timestamp: %s", strerror(-r));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering "_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* not enough space */
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek /* exactly one character short, but we don't need it */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_enumerate_data(u->journal,
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to move to next field in entry: %s",
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek } else if (r == 0) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (!utf8_is_printable_newline(u->field_data,
4b8268f843b0da1cfe1995d93a0b1f95faccc454Zbigniew Jędrzejewski-Szmek u->entry_state = ENTRY_BINARY_FIELD_START;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek } /* fall through */
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek done = size - pos > u->field_length - u->field_pos;
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek tocopy = u->field_length - u->field_pos;
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek (char*) u->field_data + u->field_pos,
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek case ENTRY_BINARY_FIELD_START: {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek c = memchr(u->field_data, '=', u->field_length);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (!c || c == u->field_data) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* need space for label + '\n' */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering } /* fall through */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* need space for uint64_t */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering le64 = htole64(u->field_length - u->field_pos);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* need space for '\n' */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek assert(nmemb <= SSIZE_MAX / size);
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek if (u->entry_state == ENTRY_DONE) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek log_error("Failed to move to next entry in journal: %s",
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek } else if (r == 0) {
c7332b0844e28d9b70c3c763b929f105c1056fe8Zbigniew Jędrzejewski-Szmek log_debug("No more entries, waiting for journal.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_info("No more entries, closing journal.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering w = write_entry((char*)buf + filled, size * nmemb - filled, u);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Buffer space is too small to write entry.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering /* This means that all available space was used up */
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_debug("Entry %zu (%s) has been uploaded.",
d4205751d4643c272059a3728045929dd0e5e800Lennart Poetteringstatic int process_journal_input(Uploader *u, int skip) {
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering r = sd_journal_next_skip(u->journal, skip);
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to skip to next entry: %s", strerror(-r));
7fd1b19bc9e9f5574f2877936b8ac267c7706947Harald Hoyer return start_upload(u, journal_input_callback, u);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint check_journal_input(Uploader *u) {
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek r = sd_journal_process(u->journal);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("Failed to process journal: %s", strerror(-r));
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek return process_journal_input(u, 1);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekstatic int dispatch_journal_input(sd_event_source *event,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_warning("dispatch_journal_input called when uploading, ignoring.");
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_debug("Detected journal input, checking for new data.");
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmekint open_journal_for_upload(Uploader *u,
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek sd_journal_set_data_threshold(j, 0);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek log_error("sd_journal_get_fd failed: %s", strerror(-fd));
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek events = sd_journal_get_events(j);
844ec79b3c2f246114ea316ebe1f36044bdb688eZbigniew Jędrzejewski-Szmek u->timeout = JOURNAL_UPLOAD_POLL_TIMEOUT;
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 log_error("Failed to register input event: %s", strerror(-r));
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 log_debug("Not listening for journal events.");
d4205751d4643c272059a3728045929dd0e5e800Lennart Poettering log_error("Failed to seek to cursor %s: %s",