journal-upload-journal.c revision 8a3db16df40c1bbcb1d1debc80fbf92339503235
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Zbigniew Jędrzejewski-Szmek
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering * Write up to size bytes to buf. Return negative on error, and number of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * bytes written otherwise. The last case is a kind of an error too.
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poetteringstatic ssize_t write_entry(char *buf, size_t size, Uploader *u) {
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering u->current_cursor = mfree(u->current_cursor);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering r = sd_journal_get_cursor(u->journal, &u->current_cursor);
818f766b12e025683cf4fed12b3da2a025bb0b31Lennart Poettering return log_error_errno(r, "Failed to get cursor: %m");
019036a47fcd10fcf0286800d144c706f3773e2fLennart Poettering /* not enough space */
91adc4db33f69606aabd332813a5d7d5751c859fLennart Poettering /* exactly one character short, but we don't need it */
4afd3348c7506dd1d36305b7bcb9feb8952b9d6bLennart Poettering } /* fall through */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_journal_get_realtime_usec(u->journal, &realtime);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_error_errno(r, "Failed to get realtime timestamp: %m");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* not enough space */
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* exactly one character short, but we don't need it */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering } /* fall through */
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering r = sd_journal_get_monotonic_usec(u->journal, &monotonic, &boot_id);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering return log_error_errno(r, "Failed to get monotonic timestamp: %m");
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering "__MONOTONIC_TIMESTAMP="USEC_FMT"\n", monotonic);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering /* not enough space */
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering /* exactly one character short, but we don't need it */
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering } /* fall through */
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering r = sd_journal_get_monotonic_usec(u->journal, NULL, &boot_id);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return log_error_errno(r, "Failed to get monotonic timestamp: %m");
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering "_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering /* not enough space */
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering /* exactly one character short, but we don't need it */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } /* fall through */
ec2c5e4398f9d65e5dfe61530f2556224733d1e6Lennart Poettering r = sd_journal_enumerate_data(u->journal,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_error_errno(r, "Failed to move to next field in entry: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering else if (r == 0) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering if (!utf8_is_printable_newline(u->field_data,
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering } /* fall through */
ce736ace37399f3dc45e6981852881a6f2448de2Lennart Poettering done = size - pos > u->field_length - u->field_pos;
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering const char *c;
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering c = memchr(u->field_data, '=', u->field_length);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering if (!c || c == u->field_data) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* need space for label + '\n' */
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering } /* fall through */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* need space for uint64_t */
51323288fc628a5cac50914df915545d685b793eLennart Poettering le64 = htole64(u->field_length - u->field_pos);
51323288fc628a5cac50914df915545d685b793eLennart Poettering /* need space for '\n' */
51323288fc628a5cac50914df915545d685b793eLennart Poetteringstatic size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_error_errno(r, "Failed to move to next entry in journal: %m");
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering } else if (r == 0) {
23b298bce75a0d1f4f15f34458af9678b4a30c3aLennart Poettering log_debug("No more entries, waiting for journal.");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering log_info("No more entries, closing journal.");
82bd6dddc4a363a9c3c6f41eb46eb171a80dca27Lennart Poettering w = write_entry((char*)buf + filled, size * nmemb - filled, u);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error("Buffer space is too small to write entry.");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering /* This means that all available space was used up */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Entry %zu (%s) has been uploaded.",
95d46fcaa4f27bc5e675e8de39ab3acc4732e39bTom Gundersenstatic int process_journal_input(Uploader *u, int skip) {
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poettering r = sd_journal_next_skip(u->journal, skip);
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering return log_error_errno(r, "Failed to skip to next entry: %m");
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering /* have data */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return start_upload(u, journal_input_callback, u);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_error_errno(r, "Failed to process journal: %m");
45ec7efb6c2560c80dfa752bc9d3733749dc52cbLennart Poetteringstatic int dispatch_journal_input(sd_event_source *event,
51323288fc628a5cac50914df915545d685b793eLennart Poettering log_debug("Detected journal input, checking for new data.");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return log_error_errno(fd, "sd_journal_get_fd failed: %m");
51323288fc628a5cac50914df915545d685b793eLennart Poettering u->timeout = JOURNAL_UPLOAD_POLL_TIMEOUT;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering r = sd_event_add_io(u->events, &u->input_event,
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering return log_error_errno(r, "Failed to register input event: %m");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Listening for journal events on fd:%d, timeout %d",
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering fd, u->timeout == (uint64_t) -1 ? -1 : (int) u->timeout);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_debug("Not listening for journal events.");
51323288fc628a5cac50914df915545d685b793eLennart Poettering return log_error_errno(r, "Failed to seek to cursor %s: %m",