LCOV - code coverage report
Current view: top level - journal-remote - journal-remote-write.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 1 61 1.6 %
Date: 2019-08-22 15:41:25 Functions: 1 6 16.7 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include "alloc-util.h"
       4             : #include "journal-remote.h"
       5             : 
       6           0 : static int do_rotate(JournalFile **f, bool compress, bool seal) {
       7           0 :         int r = journal_file_rotate(f, compress, (uint64_t) -1, seal, NULL);
       8           0 :         if (r < 0) {
       9           0 :                 if (*f)
      10           0 :                         log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
      11             :                 else
      12           0 :                         log_error_errno(r, "Failed to create rotated journal: %m");
      13             :         }
      14             : 
      15           0 :         return r;
      16             : }
      17             : 
      18           0 : Writer* writer_new(RemoteServer *server) {
      19             :         Writer *w;
      20             : 
      21           0 :         w = new0(Writer, 1);
      22           0 :         if (!w)
      23           0 :                 return NULL;
      24             : 
      25           0 :         memset(&w->metrics, 0xFF, sizeof(w->metrics));
      26             : 
      27           0 :         w->mmap = mmap_cache_new();
      28           0 :         if (!w->mmap)
      29           0 :                 return mfree(w);
      30             : 
      31           0 :         w->n_ref = 1;
      32           0 :         w->server = server;
      33             : 
      34           0 :         return w;
      35             : }
      36             : 
      37           0 : static Writer* writer_free(Writer *w) {
      38           0 :         if (!w)
      39           0 :                 return NULL;
      40             : 
      41           0 :         if (w->journal) {
      42           0 :                 log_debug("Closing journal file %s.", w->journal->path);
      43           0 :                 journal_file_close(w->journal);
      44             :         }
      45             : 
      46           0 :         if (w->server && w->hashmap_key)
      47           0 :                 hashmap_remove(w->server->writers, w->hashmap_key);
      48             : 
      49           0 :         free(w->hashmap_key);
      50             : 
      51           0 :         if (w->mmap)
      52           0 :                 mmap_cache_unref(w->mmap);
      53             : 
      54           0 :         return mfree(w);
      55             : }
      56             : 
      57           4 : DEFINE_TRIVIAL_REF_UNREF_FUNC(Writer, writer, writer_free);
      58             : 
      59           0 : int writer_write(Writer *w,
      60             :                  struct iovec_wrapper *iovw,
      61             :                  dual_timestamp *ts,
      62             :                  sd_id128_t *boot_id,
      63             :                  bool compress,
      64             :                  bool seal) {
      65             :         int r;
      66             : 
      67           0 :         assert(w);
      68           0 :         assert(iovw);
      69           0 :         assert(iovw->count > 0);
      70             : 
      71           0 :         if (journal_file_rotate_suggested(w->journal, 0)) {
      72           0 :                 log_info("%s: Journal header limits reached or header out-of-date, rotating",
      73             :                          w->journal->path);
      74           0 :                 r = do_rotate(&w->journal, compress, seal);
      75           0 :                 if (r < 0)
      76           0 :                         return r;
      77             :         }
      78             : 
      79           0 :         r = journal_file_append_entry(w->journal, ts, boot_id,
      80           0 :                                       iovw->iovec, iovw->count,
      81             :                                       &w->seqnum, NULL, NULL);
      82           0 :         if (r >= 0) {
      83           0 :                 if (w->server)
      84           0 :                         w->server->event_count += 1;
      85           0 :                 return 0;
      86           0 :         } else if (r == -EBADMSG)
      87           0 :                 return r;
      88             : 
      89           0 :         log_debug_errno(r, "%s: Write failed, rotating: %m", w->journal->path);
      90           0 :         r = do_rotate(&w->journal, compress, seal);
      91           0 :         if (r < 0)
      92           0 :                 return r;
      93             :         else
      94           0 :                 log_debug("%s: Successfully rotated journal", w->journal->path);
      95             : 
      96           0 :         log_debug("Retrying write.");
      97           0 :         r = journal_file_append_entry(w->journal, ts, boot_id,
      98           0 :                                       iovw->iovec, iovw->count,
      99             :                                       &w->seqnum, NULL, NULL);
     100           0 :         if (r < 0)
     101           0 :                 return r;
     102             : 
     103           0 :         if (w->server)
     104           0 :                 w->server->event_count += 1;
     105           0 :         return 0;
     106             : }

Generated by: LCOV version 1.14