journal-upload-journal.c revision c33b329709ebe2755181980a050d02ec7c81ed87
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 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 log_error_errno(r, "Failed to get realtime timestamp: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering "__REALTIME_TIMESTAMP="USEC_FMT"\n", realtime);
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, &monotonic, &boot_id);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering 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 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 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);
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek 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 */
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett /* need space for uint64_t */
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett le64 = htole64(u->field_length - u->field_pos);
c7fdf44d08e1217d40dc092fb90a65978a0f541fLennart Poettering /* need space for '\n' */
3cadce7d33e263ec7a6a83c00c11144930258b22Thomas Bächlerstatic size_t journal_input_callback(void *buf, size_t size, size_t nmemb, void *userp) {
0c9d8f1d4b5018199cb5a9b57580dc1480a7f915Jani Nikula log_error_errno(r, "Failed to move to next entry in journal: %m");
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett } else if (r == 0) {
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett log_debug("No more entries, waiting for journal.");
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett log_info("No more entries, closing journal.");
7b909d7407965c03caaba30daae7aee113627a83Josh Triplett w = write_entry((char*)buf + filled, size * nmemb - filled, u);
3cadce7d33e263ec7a6a83c00c11144930258b22Thomas Bächler log_error("Buffer space is too small to write entry.");
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering /* This means that all available space was used up */
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering log_debug("Entry %zu (%s) has been uploaded.",
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek log_debug("Closing journal input.");
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poetteringstatic int process_journal_input(Uploader *u, int skip) {
938d2699d2e818bd996614e89ea3d668200ad2a8Zbigniew Jędrzejewski-Szmek r = sd_journal_next_skip(u->journal, skip);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering log_error_errno(r, "Failed to skip to next entry: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering } else if (r < skip)
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering /* have data */
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering return start_upload(u, journal_input_callback, u);
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering log_error_errno(r, "Failed to process journal: %m");
3cadce7d33e263ec7a6a83c00c11144930258b22Thomas Bächlerstatic int dispatch_journal_input(sd_event_source *event,
be3f52f4ed02a9256b1577719677b32a17b525acLennart Poettering log_warning("dispatch_journal_input called when uploading, ignoring.");
be3f52f4ed02a9256b1577719677b32a17b525acLennart Poettering log_debug("Detected journal input, checking for new data.");
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek log_error_errno(fd, "sd_journal_get_fd failed: %m");
0f4ba83c397e807939a4eb0b2cbd04ad4ab548ccLennart Poettering u->timeout = JOURNAL_UPLOAD_POLL_TIMEOUT;
b76388e123e8d73ded1fd53937d816b314948517Michael Biebl r = sd_event_add_io(u->events, &u->input_event,
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering 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);
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering log_debug("Not listening for journal events.");
1ca208fb4f93e5869704af1812cbff7130a2fc03Zbigniew Jędrzejewski-Szmek r = sd_journal_seek_cursor(j, cursor);
3731acf1acfb4a6eb68374a5b137f3b368f63381Lennart Poettering log_error_errno(r, "Failed to seek to cursor %s: %m",