chiark / gitweb /
journald: take corrupted files into account when calculating disk space
[elogind.git] / src / journal / journald.c
index e9c00b443cff7decd1cf62df2b6e8fc82314337f..e5bcc2620399a5a0922408667c7f9a7934521e37 100644 (file)
@@ -29,6 +29,7 @@
 #include <sys/ioctl.h>
 #include <linux/sockios.h>
 #include <sys/statvfs.h>
+#include <sys/user.h>
 
 #include <systemd/sd-journal.h>
 #include <systemd/sd-login.h>
@@ -164,13 +165,17 @@ static uint64_t available_space(Server *s) {
                 if (!de)
                         break;
 
-                if (!dirent_is_file_with_suffix(de, ".journal"))
+                if (!endswith(de->d_name, ".journal") &&
+                    !endswith(de->d_name, ".journal~"))
                         continue;
 
                 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
                         continue;
 
-                sum += (uint64_t) st.st_blocks * (uint64_t) st.st_blksize;
+                if (!S_ISREG(st.st_mode))
+                        continue;
+
+                sum += (uint64_t) st.st_blocks * 512UL;
         }
 
         avail = sum >= m->max_use ? 0 : m->max_use - sum;
@@ -300,15 +305,13 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
                 journal_file_close(f);
         }
 
-        r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
+        r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
         free(p);
 
         if (r < 0)
                 return s->system_journal;
 
         server_fix_perms(s, f, uid);
-        f->metrics = s->system_metrics;
-        f->compress = s->compress;
 
         r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
         if (r < 0) {
@@ -603,8 +606,12 @@ retry:
         else {
                 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
 
-                if (r == -E2BIG && !vacuumed) {
-                        log_info("Allocation limit reached.");
+                if ((r == -EBADMSG || r == -E2BIG) && !vacuumed) {
+
+                        if (r == -E2BIG)
+                                log_info("Allocation limit reached, rotating.");
+                        else
+                                log_warning("Journal file corrupted, rotating.");
 
                         server_rotate(s);
                         server_vacuum(s);
@@ -1309,6 +1316,7 @@ finish:
                         free(iovec[j].iov_base);
         }
 
+        free(iovec);
         free(identifier);
         free(message);
 }
@@ -1873,7 +1881,7 @@ static int system_journal_open(Server *s) {
                 if (!fn)
                         return -ENOMEM;
 
-                r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
+                r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
                 free(fn);
 
                 if (r >= 0) {
@@ -1904,7 +1912,7 @@ static int system_journal_open(Server *s) {
                          * if it already exists, so that we can flush
                          * it into the system journal */
 
-                        r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
+                        r = journal_file_open_reliably(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
                         free(fn);
 
                         if (r < 0) {
@@ -1920,7 +1928,7 @@ static int system_journal_open(Server *s) {
                          * it if necessary. */
 
                         (void) mkdir_parents(fn, 0755);
-                        r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
+                        r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
                         free(fn);
 
                         if (r < 0) {
@@ -2133,7 +2141,7 @@ static int process_event(Server *s, struct epoll_event *ev) {
                                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
                                             CMSG_SPACE(sizeof(struct timeval)) +
                                             CMSG_SPACE(sizeof(int)) +
-                                            CMSG_SPACE(LINE_MAX)]; /* selinux label */
+                                            CMSG_SPACE(PAGE_SIZE)]; /* selinux label */
                         } control;
                         ssize_t n;
                         int v;
@@ -2306,10 +2314,12 @@ static int open_syslog_socket(Server *s) {
                 return -errno;
         }
 
+#ifdef HAVE_SELINUX
         one = 1;
         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
         if (r < 0)
                 log_warning("SO_PASSSEC failed: %m");
+#endif
 
         one = 1;
         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
@@ -2367,10 +2377,12 @@ static int open_native_socket(Server*s) {
                 return -errno;
         }
 
+#ifdef HAVE_SELINUX
         one = 1;
         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
         if (r < 0)
                 log_warning("SO_PASSSEC failed: %m");
+#endif
 
         one = 1;
         r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
@@ -2660,10 +2672,6 @@ static int server_init(Server *s) {
         if (r < 0)
                 return r;
 
-        r = system_journal_open(s);
-        if (r < 0)
-                return r;
-
         r = open_signalfd(s);
         if (r < 0)
                 return r;
@@ -2672,6 +2680,10 @@ static int server_init(Server *s) {
         if (!s->rate_limit)
                 return -ENOMEM;
 
+        r = system_journal_open(s);
+        if (r < 0)
+                return r;
+
         return 0;
 }