journal-upload-journal.c revision 5ffa8c818120e35c89becd938d160235c069dd12
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering * Write up to size bytes to buf. Return negative on error, and number of
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering * bytes written otherwise. The last case is a kind of an error too.
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poetteringstatic ssize_t write_entry(char *buf, size_t size, Uploader *u) {
4e731273edfe852a3eee2949cd20f49fd5b4f6d7Lennart Poettering r = sd_journal_get_cursor(u->journal, &u->current_cursor);
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering return log_error_errno(r, "Failed to get cursor: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* not enough space */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* exactly one character short, but we don't need it */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } /* fall through */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = sd_journal_get_realtime_usec(u->journal, &realtime);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return log_error_errno(r, "Failed to get realtime timestamp: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek /* not enough space */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* exactly one character short, but we don't need it */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } /* fall through */
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek r = sd_journal_get_monotonic_usec(u->journal, &monotonic, &boot_id);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return log_error_errno(r, "Failed to get monotonic timestamp: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "__MONOTONIC_TIMESTAMP="USEC_FMT"\n", monotonic);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* not enough space */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* exactly one character short, but we don't need it */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } /* fall through */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = sd_journal_get_monotonic_usec(u->journal, NULL, &boot_id);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return log_error_errno(r, "Failed to get monotonic timestamp: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "_BOOT_ID=%s\n", sd_id128_to_string(boot_id, sid));
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* not enough space */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* exactly one character short, but we don't need it */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } /* fall through */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering r = sd_journal_enumerate_data(u->journal,
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return log_error_errno(r, "Failed to move to next field in entry: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering else if (r == 0) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering if (!utf8_is_printable_newline(u->field_data,
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering u->entry_state = ENTRY_BINARY_FIELD_START;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } /* fall through */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering done = size - pos > u->field_length - u->field_pos;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering const char *c;
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering c = memchr(u->field_data, '=', u->field_length);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering if (!c || c == u->field_data) {
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* need space for label + '\n' */
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek memcpy(buf + pos, u->field_data, len);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } /* fall through */
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek /* need space for uint64_t */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering le64 = htole64(u->field_length - u->field_pos);
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett /* need space for '\n' */
3cadce7d33e263ec7a6a83c00c11144930258b22Thomas Bächlerstatic size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett log_error_errno(r, "Failed to move to next entry in journal: %m");
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett } else if (r == 0) {
4cd2b2cf8ca585d15ebc859701b346658262b5bbDenis Tikhomirov log_debug("No more entries, waiting for journal.");
4cd2b2cf8ca585d15ebc859701b346658262b5bbDenis Tikhomirov log_info("No more entries, closing journal.");
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett w = write_entry((char*)buf + filled, size * nmemb - filled, u);
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett log_error("Buffer space is too small to write entry.");
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett /* This means that all available space was used up */
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek log_debug("Entry %zu (%s) has been uploaded.",
c33b329709ebe2755181980a050d02ec7c81ed87Michal Schmidtstatic int process_journal_input(Uploader *u, int skip) {
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering r = sd_journal_next_skip(u->journal, skip);
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering return log_error_errno(r, "Failed to skip to next entry: %m");
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering /* have data */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return start_upload(u, journal_input_callback, u);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering log_error_errno(r, "Failed to process journal: %m");
b3267152783d5784c45010615045d4e8ee459da2Zbigniew Jędrzejewski-Szmekstatic int dispatch_journal_input(sd_event_source *event,
3cadce7d33e263ec7a6a83c00c11144930258b22Thomas Bächler log_warning("dispatch_journal_input called when uploading, ignoring.");
3cadce7d33e263ec7a6a83c00c11144930258b22Thomas Bächler log_debug("Detected journal input, checking for new data.");
be3f52f4ed02a9256b1577719677b32a17b525acLennart Poettering return log_error_errno(fd, "sd_journal_get_fd failed: %m");
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering u->timeout = JOURNAL_UPLOAD_POLL_TIMEOUT;
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek r = sd_event_add_io(u->events, &u->input_event,
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return log_error_errno(r, "Failed to register input event: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering log_debug("Listening for journal events on fd:%d, timeout %d",
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering fd, u->timeout == (uint64_t) -1 ? -1 : (int) u->timeout);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering log_debug("Not listening for journal events.");
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering return log_error_errno(r, "Failed to seek to cursor %s: %m",