[systemd-devel] [RFC PATCH 1/3] journal: add sd_journal_add_file

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Mon Mar 18 18:29:09 PDT 2013


---
A serie composed of three patches:                                                        
1. this one: make sd_journal_add_file available to the wide public                        
2. second one: allow opening of the journal in "stub" mode, without                       
   any files                                                                              
3. third one: journalctl --file path.journal can be used to look                          
   at just one file.                                                                      
                                                                                          
This has been requested before, and seems useful.

 src/journal/libsystemd-journal.sym |  5 +++
 src/journal/sd-journal.c           | 72 ++++++++++++++++++++++----------------
 src/systemd/sd-journal.h           |  1 +
 3 files changed, 48 insertions(+), 30 deletions(-)

diff --git a/src/journal/libsystemd-journal.sym b/src/journal/libsystemd-journal.sym
index fbe4150..5e452b1 100644
--- a/src/journal/libsystemd-journal.sym
+++ b/src/journal/libsystemd-journal.sym
@@ -93,3 +93,8 @@ LIBSYSTEMD_JOURNAL_198 {
 global:
         sd_journal_reliable_fd;
 } LIBSYSTEMD_JOURNAL_196;
+
+LIBSYSTEMD_JOURNAL_199 {
+global:
+        sd_journal_add_file;
+} LIBSYSTEMD_JOURNAL_198;
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index ef4b9b2..46f9fb8 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1217,7 +1217,6 @@ static void check_network(sd_journal *j, int fd) {
 static int add_file(sd_journal *j, const char *prefix, const char *filename) {
         char _cleanup_free_ *path = NULL;
         int r;
-        JournalFile *f;
 
         assert(j);
         assert(prefix);
@@ -1234,37 +1233,11 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
         if (!path)
                 return -ENOMEM;
 
-        if (hashmap_get(j->files, path))
+        r = sd_journal_add_file(j, path);
+        if (r == -ENOENT)
                 return 0;
 
-        if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
-                log_debug("Too many open journal files, not adding %s, ignoring.", path);
-                return 0;
-        }
-
-        r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
-        if (r < 0) {
-                if (errno == ENOENT)
-                        return 0;
-
-                return r;
-        }
-
-        /* journal_file_dump(f); */
-
-        r = hashmap_put(j->files, f->path, f);
-        if (r < 0) {
-                journal_file_close(f);
-                return r;
-        }
-
-        log_debug("File %s got added.", f->path);
-
-        check_network(j, f->fd);
-
-        j->current_invalidate_counter ++;
-
-        return 0;
+        return r;
 }
 
 static int remove_file(sd_journal *j, const char *prefix, const char *filename) {
@@ -1619,6 +1592,45 @@ fail:
         return r;
 }
 
+_public_ int sd_journal_add_file(sd_journal *j, const char *path) {
+        JournalFile *f;
+        int r;
+
+        if (!j)
+                return -EINVAL;
+
+        if (!path)
+                return -EINVAL;
+
+        if (hashmap_get(j->files, path))
+                return 0;
+
+        if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
+                log_debug("Too many open journal files, not adding %s.", path);
+                return -ETOOMANYREFS;
+        }
+
+        r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
+        if (r < 0)
+                return r;
+
+        /* journal_file_dump(f); */
+
+        r = hashmap_put(j->files, f->path, f);
+        if (r < 0) {
+                journal_file_close(f);
+                return r;
+        }
+
+        log_debug("File %s got added.", f->path);
+
+        check_network(j, f->fd);
+
+        j->current_invalidate_counter ++;
+
+        return 0;
+}
+
 _public_ void sd_journal_close(sd_journal *j) {
         Directory *d;
         JournalFile *f;
diff --git a/src/systemd/sd-journal.h b/src/systemd/sd-journal.h
index 2e8d2d8..30b7c75 100644
--- a/src/systemd/sd-journal.h
+++ b/src/systemd/sd-journal.h
@@ -86,6 +86,7 @@ enum {
 
 int sd_journal_open(sd_journal **ret, int flags);
 int sd_journal_open_directory(sd_journal **ret, const char *path, int flags);
+int sd_journal_add_file(sd_journal *j, const char *path);
 void sd_journal_close(sd_journal *j);
 
 int sd_journal_previous(sd_journal *j);
-- 
1.8.1.4



More information about the systemd-devel mailing list