]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-import: extract helpers for handling arrays of iovec and make them available...
authorFranck Bui <fbui@suse.com>
Tue, 25 Jun 2019 13:54:44 +0000 (15:54 +0200)
committerFranck Bui <fbui@suse.com>
Thu, 27 Jun 2019 17:14:23 +0000 (19:14 +0200)
src/basic/io-util.c
src/basic/io-util.h
src/coredump/coredump.c
src/journal-remote/journal-remote-parse.c
src/shared/journal-importer.c
src/shared/journal-importer.h
src/test/test-journal-importer.c

index 9394e54bc5d5a345fbaac4da748c20aa4645e495..38de26a72cb8c323b1d10f0a3eec7dc5eeac83db 100644 (file)
@@ -270,3 +270,56 @@ char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const ch
         free(value);
         return x;
 }
+
+struct iovec_wrapper *iovw_new(void) {
+        return malloc0(sizeof(struct iovec_wrapper));
+}
+
+void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors) {
+        if (free_vectors)
+                for (size_t i = 0; i < iovw->count; i++)
+                        free(iovw->iovec[i].iov_base);
+
+        iovw->iovec = mfree(iovw->iovec);
+        iovw->count = 0;
+        iovw->size_bytes = 0;
+}
+
+struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw) {
+        iovw_free_contents(iovw, true);
+
+        return mfree(iovw);
+}
+
+struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw) {
+        iovw_free_contents(iovw, false);
+
+        return mfree(iovw);
+}
+
+int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
+        if (iovw->count >= IOV_MAX)
+                return -E2BIG;
+
+        if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
+                return log_oom();
+
+        iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
+        return 0;
+}
+
+void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
+        size_t i;
+
+        for (i = 0; i < iovw->count; i++)
+                iovw->iovec[i].iov_base = (char *)iovw->iovec[i].iov_base - old + new;
+}
+
+size_t iovw_size(struct iovec_wrapper *iovw) {
+        size_t n = 0, i;
+
+        for (i = 0; i < iovw->count; i++)
+                n += iovw->iovec[i].iov_len;
+
+        return n;
+}
index d1fb7e78d205b2a14893011a0ef726b9e1f788e6..e689fe1f43cd7d893653eb4ff282051a188a5183 100644 (file)
@@ -74,3 +74,17 @@ static inline bool FILE_SIZE_VALID_OR_INFINITY(uint64_t l) {
 
 char* set_iovec_string_field(struct iovec *iovec, size_t *n_iovec, const char *field, const char *value);
 char* set_iovec_string_field_free(struct iovec *iovec, size_t *n_iovec, const char *field, char *value);
+
+struct iovec_wrapper {
+        struct iovec *iovec;
+        size_t count;
+        size_t size_bytes;
+};
+
+struct iovec_wrapper *iovw_new(void);
+struct iovec_wrapper *iovw_free(struct iovec_wrapper *iovw);
+struct iovec_wrapper *iovw_free_free(struct iovec_wrapper *iovw);
+void iovw_free_contents(struct iovec_wrapper *iovw, bool free_vectors);
+int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len);
+void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new);
+size_t iovw_size(struct iovec_wrapper *iovw);
index 1a5a2dc81dc3cf00e1c09321c89f1e7adaf34037..e991fe4af00365780d4d7c54c78ed450f8566c80 100644 (file)
@@ -1237,9 +1237,7 @@ static int process_backtrace(int argc, char *argv[]) {
         _cleanup_free_ struct iovec *iovec = NULL;
         size_t n_iovec, n_allocated, n_to_free = 0, i;
         int r;
-         _cleanup_(journal_importer_cleanup) JournalImporter importer = {
-                .fd = STDIN_FILENO,
-        };
+         _cleanup_(journal_importer_cleanup) JournalImporter importer = JOURNAL_IMPORTER_INIT(STDIN_FILENO);
 
         log_debug("Processing backtrace on stdin...");
 
index ebcae2fcaaa9b03afb01596f0bd386bb8752bbc3..dc047b2d49955a56266f1e142a76fb99a717d456 100644 (file)
@@ -38,7 +38,7 @@ RemoteSource* source_new(int fd, bool passive_fd, char *name, Writer *writer) {
         if (!source)
                 return NULL;
 
-        source->importer.fd = fd;
+        source->importer = JOURNAL_IMPORTER_MAKE(fd);
         source->importer.passive_fd = passive_fd;
         source->importer.name = name;
 
index 8dc2c42ad15d335c39100de5ccef14198f4a50e2..218fbe90572c1315ca4d5e8046769be155fc71af 100644 (file)
@@ -22,37 +22,6 @@ enum {
         IMPORTER_STATE_EOF,         /* done */
 };
 
-static int iovw_put(struct iovec_wrapper *iovw, void* data, size_t len) {
-        if (iovw->count >= ENTRY_FIELD_COUNT_MAX)
-                return -E2BIG;
-
-        if (!GREEDY_REALLOC(iovw->iovec, iovw->size_bytes, iovw->count + 1))
-                return log_oom();
-
-        iovw->iovec[iovw->count++] = IOVEC_MAKE(data, len);
-        return 0;
-}
-
-static void iovw_free_contents(struct iovec_wrapper *iovw) {
-        iovw->iovec = mfree(iovw->iovec);
-        iovw->size_bytes = iovw->count = 0;
-}
-
-static void iovw_rebase(struct iovec_wrapper *iovw, char *old, char *new) {
-        size_t i;
-
-        for (i = 0; i < iovw->count; i++)
-                iovw->iovec[i].iov_base = (char*) iovw->iovec[i].iov_base - old + new;
-}
-
-size_t iovw_size(struct iovec_wrapper *iovw) {
-        size_t n = 0, i;
-
-        for (i = 0; i < iovw->count; i++)
-                n += iovw->iovec[i].iov_len;
-
-        return n;
-}
 
 void journal_importer_cleanup(JournalImporter *imp) {
         if (imp->fd >= 0 && !imp->passive_fd) {
@@ -62,7 +31,7 @@ void journal_importer_cleanup(JournalImporter *imp) {
 
         free(imp->name);
         free(imp->buf);
-        iovw_free_contents(&imp->iovw);
+        iovw_free_contents(&imp->iovw, false);
 }
 
 static char* realloc_buffer(JournalImporter *imp, size_t size) {
@@ -466,7 +435,7 @@ void journal_importer_drop_iovw(JournalImporter *imp) {
 
         /* This function drops processed data that along with the iovw that points at it */
 
-        iovw_free_contents(&imp->iovw);
+        iovw_free_contents(&imp->iovw, false);
 
         /* possibly reset buffer position */
         remain = imp->filled - imp->offset;
index 7914c0cf5fbc88176fc25c632150f74b1f73d18c..b2e3c817f5a3a3fee8e305fca201740ff131fbec 100644 (file)
@@ -6,8 +6,8 @@
 #include <stdbool.h>
 #include <sys/uio.h>
 
+#include "io-util.h"
 #include "sd-id128.h"
-
 #include "time-util.h"
 
 /* Make sure not to make this smaller than the maximum coredump size.
 /* The maximum number of fields in an entry */
 #define ENTRY_FIELD_COUNT_MAX 1024
 
-struct iovec_wrapper {
-        struct iovec *iovec;
-        size_t size_bytes;
-        size_t count;
-};
-
-size_t iovw_size(struct iovec_wrapper *iovw);
-
 typedef struct JournalImporter {
         int fd;
         bool passive_fd;
@@ -53,6 +45,9 @@ typedef struct JournalImporter {
         sd_id128_t boot_id;
 } JournalImporter;
 
+#define JOURNAL_IMPORTER_INIT(_fd) { .fd = (_fd), .iovw = {} }
+#define JOURNAL_IMPORTER_MAKE(_fd) (JournalImporter) JOURNAL_IMPORTER_INIT(_fd)
+
 void journal_importer_cleanup(JournalImporter *);
 int journal_importer_process_data(JournalImporter *);
 int journal_importer_push_data(JournalImporter *, const char *data, size_t size);
index cddbfa7022416b65c21eb60d2d27ae5ec7ac8c63..7e898735c939e5259c57e43ebe1c1b9b9637a352 100644 (file)
@@ -21,7 +21,7 @@ static void assert_iovec_entry(const struct iovec *iovec, const char* content) {
         "0::/user.slice/user-1002.slice/user@1002.service/gnome-terminal-server.service\n"
 
 static void test_basic_parsing(void) {
-        _cleanup_(journal_importer_cleanup) JournalImporter imp = {};
+        _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1);
         _cleanup_free_ char *journal_data_path = NULL;
         int r;
 
@@ -52,7 +52,7 @@ static void test_basic_parsing(void) {
 }
 
 static void test_bad_input(void) {
-        _cleanup_(journal_importer_cleanup) JournalImporter imp = {};
+        _cleanup_(journal_importer_cleanup) JournalImporter imp = JOURNAL_IMPORTER_INIT(-1);
         _cleanup_free_ char *journal_data_path = NULL;
         int r;