chiark / gitweb /
journal: don't access j->files after use
[elogind.git] / src / journal / sd-journal.c
index ef4b9b2242a422c64867eb776feadc987eee566d..82cacf3674446cd6cc600277da4a8e13364d1539 100644 (file)
 
 #define DEFAULT_DATA_THRESHOLD (64*1024)
 
 
 #define DEFAULT_DATA_THRESHOLD (64*1024)
 
+/* We return an error here only if we didn't manage to
+   memorize the real error. */
+static int set_put_error(sd_journal *j, int r) {
+        int k;
+
+        if (r >= 0)
+                return r;
+
+        k = set_ensure_allocated(&j->errors, trivial_hash_func, trivial_compare_func);
+        if (k < 0)
+                return k;
+
+        return set_put(j->errors, INT_TO_PTR(r));
+}
+
 static void detach_location(sd_journal *j) {
         Iterator i;
         JournalFile *f;
 static void detach_location(sd_journal *j) {
         Iterator i;
         JournalFile *f;
@@ -1239,7 +1254,7 @@ static int add_file(sd_journal *j, const char *prefix, const char *filename) {
 
         if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
                 log_debug("Too many open journal files, not adding %s, ignoring.", path);
 
         if (hashmap_size(j->files) >= JOURNAL_FILES_MAX) {
                 log_debug("Too many open journal files, not adding %s, ignoring.", path);
-                return 0;
+                return set_put_error(j, -ETOOMANYREFS);
         }
 
         r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
         }
 
         r = journal_file_open(path, O_RDONLY, 0, false, false, NULL, j->mmap, NULL, &f);
@@ -1380,8 +1395,13 @@ static int add_directory(sd_journal *j, const char *prefix, const char *dirname)
                 if (dirent_is_file_with_suffix(de, ".journal") ||
                     dirent_is_file_with_suffix(de, ".journal~")) {
                         r = add_file(j, m->path, de->d_name);
                 if (dirent_is_file_with_suffix(de, ".journal") ||
                     dirent_is_file_with_suffix(de, ".journal~")) {
                         r = add_file(j, m->path, de->d_name);
-                        if (r < 0)
-                                log_debug("Failed to add file %s/%s: %s", m->path, de->d_name, strerror(-r));
+                        if (r < 0) {
+                                log_debug("Failed to add file %s/%s: %s",
+                                          m->path, de->d_name, strerror(-r));
+                                r = set_put_error(j, r);
+                                if (r < 0)
+                                        return r;
+                        }
                 }
         }
 
                 }
         }
 
@@ -1454,9 +1474,13 @@ static int add_root_directory(sd_journal *j, const char *p) {
                 if (dirent_is_file_with_suffix(de, ".journal") ||
                     dirent_is_file_with_suffix(de, ".journal~")) {
                         r = add_file(j, m->path, de->d_name);
                 if (dirent_is_file_with_suffix(de, ".journal") ||
                     dirent_is_file_with_suffix(de, ".journal~")) {
                         r = add_file(j, m->path, de->d_name);
-                        if (r < 0)
-                                log_debug("Failed to add file %s/%s: %s", m->path, de->d_name, strerror(-r));
-
+                        if (r < 0) {
+                                log_debug("Failed to add file %s/%s: %s",
+                                          m->path, de->d_name, strerror(-r));
+                                r = set_put_error(j, r);
+                                if (r < 0)
+                                        return r;
+                        }
                 } else if ((de->d_type == DT_DIR || de->d_type == DT_LNK || de->d_type == DT_UNKNOWN) &&
                            sd_id128_from_string(de->d_name, &id) >= 0) {
 
                 } else if ((de->d_type == DT_DIR || de->d_type == DT_LNK || de->d_type == DT_UNKNOWN) &&
                            sd_id128_from_string(de->d_name, &id) >= 0) {
 
@@ -1495,7 +1519,7 @@ static int remove_directory(sd_journal *j, Directory *d) {
 }
 
 static int add_search_paths(sd_journal *j) {
 }
 
 static int add_search_paths(sd_journal *j) {
-
+        int r;
         const char search_paths[] =
                 "/run/log/journal\0"
                 "/var/log/journal\0";
         const char search_paths[] =
                 "/run/log/journal\0"
                 "/var/log/journal\0";
@@ -1506,8 +1530,14 @@ static int add_search_paths(sd_journal *j) {
         /* We ignore most errors here, since the idea is to only open
          * what's actually accessible, and ignore the rest. */
 
         /* We ignore most errors here, since the idea is to only open
          * what's actually accessible, and ignore the rest. */
 
-        NULSTR_FOREACH(p, search_paths)
-                add_root_directory(j, p);
+        NULSTR_FOREACH(p, search_paths) {
+                r = add_root_directory(j, p);
+                if (r < 0 && r != -ENOENT) {
+                        r = set_put_error(j, r);
+                        if (r < 0)
+                                return r;
+                }
+        }
 
         return 0;
 }
 
         return 0;
 }
@@ -1607,8 +1637,10 @@ _public_ int sd_journal_open_directory(sd_journal **ret, const char *path, int f
                 return -ENOMEM;
 
         r = add_root_directory(j, path);
                 return -ENOMEM;
 
         r = add_root_directory(j, path);
-        if (r < 0)
+        if (r < 0) {
+                set_put_error(j, r);
                 goto fail;
                 goto fail;
+        }
 
         *ret = j;
         return 0;
 
         *ret = j;
         return 0;
@@ -1626,6 +1658,8 @@ _public_ void sd_journal_close(sd_journal *j) {
         if (!j)
                 return;
 
         if (!j)
                 return;
 
+        sd_journal_flush_matches(j);
+
         while ((f = hashmap_steal_first(j->files)))
                 journal_file_close(f);
 
         while ((f = hashmap_steal_first(j->files)))
                 journal_file_close(f);
 
@@ -1643,13 +1677,12 @@ _public_ void sd_journal_close(sd_journal *j) {
         if (j->inotify_fd >= 0)
                 close_nointr_nofail(j->inotify_fd);
 
         if (j->inotify_fd >= 0)
                 close_nointr_nofail(j->inotify_fd);
 
-        sd_journal_flush_matches(j);
-
         if (j->mmap)
                 mmap_cache_unref(j->mmap);
 
         free(j->path);
         free(j->unique_field);
         if (j->mmap)
                 mmap_cache_unref(j->mmap);
 
         free(j->path);
         free(j->unique_field);
+        set_free(j->errors);
         free(j);
 }
 
         free(j);
 }
 
@@ -1968,8 +2001,11 @@ static void process_inotify_event(sd_journal *j, struct inotify_event *e) {
 
                         if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
                                 r = add_file(j, d->path, e->name);
 
                         if (e->mask & (IN_CREATE|IN_MOVED_TO|IN_MODIFY|IN_ATTRIB)) {
                                 r = add_file(j, d->path, e->name);
-                                if (r < 0)
-                                        log_debug("Failed to add file %s/%s: %s", d->path, e->name, strerror(-r));
+                                if (r < 0) {
+                                        log_debug("Failed to add file %s/%s: %s",
+                                                  d->path, e->name, strerror(-r));
+                                        set_put_error(j, r);
+                                }
 
                         } else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {
 
 
                         } else if (e->mask & (IN_DELETE|IN_MOVED_FROM|IN_UNMOUNT)) {