chiark / gitweb /
build-sys: make test output a bit nicer
[elogind.git] / src / journal / sd-journal.c
index 2ba7ca4559988a9573f8358bf23b7e2217a7b49b..283d5930630c34c4dbdfa17944a3b6c73cab41eb 100644 (file)
@@ -41,6 +41,7 @@
 #include "missing.h"
 #include "catalog.h"
 #include "replace-var.h"
+#include "fileio.h"
 
 #define JOURNAL_FILES_MAX 1024
 
@@ -1441,10 +1442,16 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
 
         for (;;) {
                 struct dirent *de;
-                union dirent_storage buf;
 
-                r = readdir_r(d, &buf.de, &de);
-                if (r != 0 || !de)
+                errno = 0;
+                de = readdir(d);
+                if (!de && errno != 0) {
+                        r = -errno;
+                        log_debug("Failed to read directory %s: %s",
+                                  m->path, strerror(errno));
+                        return r;
+                }
+                if (!de)
                         break;
 
                 if (dirent_is_file_with_suffix(de, ".journal") ||
@@ -1477,6 +1484,9 @@ static int add_root_directory(sd_journal *j, const char *p) {
             !path_startswith(p, "/run"))
                 return -EINVAL;
 
+        if (j->prefix)
+                p = strappenda(j->prefix, p);
+
         d = opendir(p);
         if (!d)
                 return -errno;
@@ -1522,11 +1532,17 @@ static int add_root_directory(sd_journal *j, const char *p) {
 
         for (;;) {
                 struct dirent *de;
-                union dirent_storage buf;
                 sd_id128_t id;
 
-                r = readdir_r(d, &buf.de, &de);
-                if (r != 0 || !de)
+                errno = 0;
+                de = readdir(d);
+                if (!de && errno != 0) {
+                        r = -errno;
+                        log_debug("Failed to read directory %s: %s",
+                                  m->path, strerror(errno));
+                        return r;
+                }
+                if (!de)
                         break;
 
                 if (dirent_is_file_with_suffix(de, ".journal") ||
@@ -1612,8 +1628,8 @@ static int add_current_paths(sd_journal *j) {
          * treat them as fatal. */
 
         HASHMAP_FOREACH(f, j->files, i) {
-                int r;
                 _cleanup_free_ char *dir;
+                int r;
 
                 dir = dirname_malloc(f->path);
                 if (!dir)
@@ -1684,12 +1700,7 @@ _public_ int sd_journal_open(sd_journal **ret, int flags) {
         int r;
 
         assert_return(ret, -EINVAL);
-
-        if (flags & ~(SD_JOURNAL_LOCAL_ONLY|
-                      SD_JOURNAL_RUNTIME_ONLY|
-                      SD_JOURNAL_SYSTEM|
-                      SD_JOURNAL_CURRENT_USER))
-                return -EINVAL;
+        assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_RUNTIME_ONLY|SD_JOURNAL_SYSTEM|SD_JOURNAL_CURRENT_USER)) == 0, -EINVAL);
 
         j = journal_new(flags, NULL);
         if (!j)
@@ -1708,6 +1719,48 @@ fail:
         return r;
 }
 
+_public_ int sd_journal_open_container(sd_journal **ret, const char *machine, int flags) {
+        _cleanup_free_ char *root = NULL, *class = NULL;
+        sd_journal *j;
+        char *p;
+        int r;
+
+        assert_return(machine, -EINVAL);
+        assert_return(ret, -EINVAL);
+        assert_return((flags & ~(SD_JOURNAL_LOCAL_ONLY|SD_JOURNAL_SYSTEM)) == 0, -EINVAL);
+        assert_return(filename_is_safe(machine), -EINVAL);
+
+        p = strappenda("/run/systemd/machines/", machine);
+        r = parse_env_file(p, NEWLINE, "ROOT", &root, "CLASS", &class, NULL);
+        if (r == -ENOENT)
+                return -EHOSTDOWN;
+        if (r < 0)
+                return r;
+        if (!root)
+                return -ENODATA;
+
+        if (!streq_ptr(class, "container"))
+                return -EIO;
+
+        j = journal_new(flags, NULL);
+        if (!j)
+                return -ENOMEM;
+
+        j->prefix = root;
+        root = NULL;
+
+        r = add_search_paths(j);
+        if (r < 0)
+                goto fail;
+
+        *ret = j;
+        return 0;
+
+fail:
+        sd_journal_close(j);
+        return r;
+}
+
 _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int flags) {
         sd_journal *j;
         int r;
@@ -1798,6 +1851,7 @@ _public_ void sd_journal_close(sd_journal *j) {
         }
 
         free(j->path);
+        free(j->prefix);
         free(j->unique_field);
         set_free(j->errors);
         free(j);