chiark / gitweb /
journald: constify all things
[elogind.git] / src / journal / journald-server.c
index 4f5d002d21c4143df349d5e111aab39d183d7de1..019c3a649acae87f7dfd68292e479e3af701b4ab 100644 (file)
@@ -212,7 +212,7 @@ void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
 
         acl = acl_get_fd(f->fd);
         if (!acl) {
-                log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
+                log_warning_errno(errno, "Failed to read ACL on %s, ignoring: %m", f->path);
                 return;
         }
 
@@ -222,7 +222,7 @@ void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
                 if (acl_create_entry(&acl, &entry) < 0 ||
                     acl_set_tag_type(entry, ACL_USER) < 0 ||
                     acl_set_qualifier(entry, &uid) < 0) {
-                        log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
+                        log_warning_errno(errno, "Failed to patch ACL on %s, ignoring: %m", f->path);
                         goto finish;
                 }
         }
@@ -232,12 +232,12 @@ void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
         if (acl_get_permset(entry, &permset) < 0 ||
             acl_add_perm(permset, ACL_READ) < 0 ||
             calc_acl_mask_if_needed(&acl) < 0) {
-                log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
+                log_warning_errno(errno, "Failed to patch ACL on %s, ignoring: %m", f->path);
                 goto finish;
         }
 
         if (acl_set_fd(f->fd, acl) < 0)
-                log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
+                log_warning_errno(errno, "Failed to set ACL on %s, ignoring: %m", f->path);
 
 finish:
         acl_free(acl);
@@ -297,8 +297,13 @@ static JournalFile* find_journal(Server *s, uid_t uid) {
         return f;
 }
 
-static int do_rotate(Server *s, JournalFile **f, const char* name,
-                     bool seal, uint32_t uid) {
+static int do_rotate(
+                Server *s,
+                JournalFile **f,
+                const char* name,
+                bool seal,
+                uint32_t uid) {
+
         int r;
         assert(s);
 
@@ -308,11 +313,9 @@ static int do_rotate(Server *s, JournalFile **f, const char* name,
         r = journal_file_rotate(f, s->compress, seal);
         if (r < 0)
                 if (*f)
-                        log_error_errno(r, "Failed to rotate %s: %m",
-                                        (*f)->path);
+                        log_error_errno(r, "Failed to rotate %s: %m", (*f)->path);
                 else
-                        log_error_errno(r, "Failed to create new %s journal: %m",
-                                        name);
+                        log_error_errno(r, "Failed to create new %s journal: %m", name);
         else
                 server_fix_perms(s, *f, uid);
         return r;
@@ -366,15 +369,20 @@ void server_sync(Server *s) {
         s->sync_scheduled = false;
 }
 
-static void do_vacuum(Server *s, char *ids, JournalFile *f, const char* path,
-                      JournalMetrics *metrics) {
-        char *p;
+static void do_vacuum(
+                Server *s,
+                const char *id,
+                JournalFile *f,
+                const char* path,
+                JournalMetrics *metrics) {
+
+        const char *p;
         int r;
 
         if (!f)
                 return;
 
-        p = strappenda(path, ids);
+        p = strappenda(path, id);
         r = journal_directory_vacuum(p, metrics->max_use, s->max_retention_usec, &s->oldest_file_usec, false);
         if (r < 0 && r != -ENOENT)
                 log_error_errno(r, "Failed to vacuum %s: %m", p);
@@ -446,12 +454,13 @@ static void server_cache_hostname(Server *s) {
         s->hostname_field = x;
 }
 
-bool shall_try_append_again(JournalFile *f, int r) {
+static bool shall_try_append_again(JournalFile *f, int r) {
 
         /* -E2BIG            Hit configured limit
            -EFBIG            Hit fs limit
            -EDQUOT           Quota limit hit
            -ENOSPC           Disk full
+           -EIO              I/O error of some kind (mmap)
            -EHOSTDOWN        Other machine
            -EBUSY            Unclean shutdown
            -EPROTONOSUPPORT  Unsupported feature
@@ -469,6 +478,8 @@ bool shall_try_append_again(JournalFile *f, int r) {
                 log_info("%s: Unsupported feature, rotating.", f->path);
         else if (r == -EBADMSG || r == -ENODATA || r == ESHUTDOWN)
                 log_warning("%s: Journal file corrupted, rotating.", f->path);
+        else if (r == -EIO)
+                log_warning("%s: IO error, rotating.", f->path);
         else
                 return false;
 
@@ -811,7 +822,7 @@ static void dispatch_message_real(
                  * realuid is not root, in order not to accidentally
                  * leak privileged information to the user that is
                  * logged by a privileged process that is part of an
-                 * unprivileged session.*/
+                 * unprivileged session. */
                 journal_uid = owner;
         else
                 journal_uid = 0;
@@ -927,10 +938,8 @@ static int system_journal_open(Server *s, bool flush_requested) {
         char ids[33];
 
         r = sd_id128_get_machine(&machine);
-        if (r < 0) {
-                log_error_errno(r, "Failed to get machine id: %m");
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to get machine id: %m");
 
         sd_id128_to_string(machine, ids);
 
@@ -999,10 +1008,8 @@ static int system_journal_open(Server *s, bool flush_requested) {
                         r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
                         free(fn);
 
-                        if (r < 0) {
-                                log_error_errno(r, "Failed to open runtime journal: %m");
-                                return r;
-                        }
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to open runtime journal: %m");
                 }
 
                 if (s->runtime_journal)
@@ -1045,10 +1052,8 @@ int server_flush_to_var(Server *s) {
                 return r;
 
         r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
-        if (r < 0) {
-                log_error_errno(r, "Failed to read runtime journal: %m");
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to read runtime journal: %m");
 
         sd_journal_set_data_threshold(j, 0);
 
@@ -1109,7 +1114,7 @@ finish:
         return r;
 }
 
-int process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
+int server_process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userdata) {
         Server *s = userdata;
 
         assert(s);
@@ -1137,7 +1142,7 @@ int process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userda
                          * the SELinux people this will change and it
                          * will probably be identical to NAME_MAX. For
                          * now we use that, but this should be updated
-                         * one day when the final limit is known.*/
+                         * one day when the final limit is known. */
                         uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
                                     CMSG_SPACE(sizeof(struct timeval)) +
                                     CMSG_SPACE(sizeof(int)) + /* fd */
@@ -1164,7 +1169,7 @@ int process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userda
                  * don't rely on it. */
                 (void) ioctl(fd, SIOCINQ, &v);
 
-                /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful!*/
+                /* Fix it up, if it is too small. We use the same fixed value as auditd here. Awful! */
                 m = PAGE_ALIGN(MAX3((size_t) v + 1,
                                     (size_t) LINE_MAX,
                                     ALIGN(sizeof(struct nlmsghdr)) + ALIGN((size_t) MAX_AUDIT_MESSAGE_LENGTH)) + 1);
@@ -1180,7 +1185,7 @@ int process_datagram(sd_event_source *es, int fd, uint32_t revents, void *userda
                         if (errno == EINTR || errno == EAGAIN)
                                 return 0;
 
-                        log_error("recvmsg() failed: %m");
+                        log_error_errno(errno, "recvmsg() failed: %m");
                         return -errno;
                 }
 
@@ -1357,10 +1362,11 @@ static int server_parse_proc_cmdline(Server *s) {
 static int server_parse_config_file(Server *s) {
         assert(s);
 
-        return config_parse(NULL, "/etc/systemd/journald.conf", NULL,
-                            "Journal\0",
-                            config_item_perf_lookup, journald_gperf_lookup,
-                            false, false, true, s);
+        return config_parse_many("/etc/systemd/journald.conf",
+                                 CONF_DIRS_NULSTR("systemd/journald.conf"),
+                                 "Journal\0",
+                                 config_item_perf_lookup, journald_gperf_lookup,
+                                 false, s);
 }
 
 static int server_dispatch_sync(sd_event_source *es, usec_t t, void *userdata) {
@@ -1437,10 +1443,8 @@ static int server_open_hostname(Server *s) {
         assert(s);
 
         s->hostname_fd = open("/proc/sys/kernel/hostname", O_RDONLY|O_CLOEXEC|O_NDELAY|O_NOCTTY);
-        if (s->hostname_fd < 0) {
-                log_error("Failed to open /proc/sys/kernel/hostname: %m");
-                return -errno;
-        }
+        if (s->hostname_fd < 0)
+                return log_error_errno(errno, "Failed to open /proc/sys/kernel/hostname: %m");
 
         r = sd_event_add_io(s->event, &s->hostname_event_source, s->hostname_fd, 0, dispatch_hostname_change, s);
         if (r < 0) {
@@ -1453,15 +1457,12 @@ static int server_open_hostname(Server *s) {
                         return 0;
                 }
 
-                log_error_errno(r, "Failed to register hostname fd in event loop: %m");
-                return r;
+                return log_error_errno(r, "Failed to register hostname fd in event loop: %m");
         }
 
         r = sd_event_source_set_priority(s->hostname_event_source, SD_EVENT_PRIORITY_IMPORTANT-10);
-        if (r < 0) {
-                log_error_errno(r, "Failed to adjust priority of host name event source: %m");
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to adjust priority of host name event source: %m");
 
         return 0;
 }
@@ -1514,18 +1515,14 @@ int server_init(Server *s) {
                 return log_oom();
 
         r = sd_event_default(&s->event);
-        if (r < 0) {
-                log_error_errno(r, "Failed to create event loop: %m");
-                return r;
-        }
+        if (r < 0)
+                return log_error_errno(r, "Failed to create event loop: %m");
 
         sd_event_set_watchdog(s->event, true);
 
         n = sd_listen_fds(true);
-        if (n < 0) {
-                log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
-                return n;
-        }
+        if (n < 0)
+                return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
 
         for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
 
@@ -1566,8 +1563,16 @@ int server_init(Server *s) {
 
                         s->audit_fd = fd;
 
-                } else
-                        log_error("Unknown socket passed as file descriptor %d, ignoring.", fd);
+                } else {
+                        log_warning("Unknown socket passed as file descriptor %d, ignoring.", fd);
+
+                        /* Let's close the fd, better be safe than
+                           sorry. The fd might reference some resource
+                           that we really want to release if we don't
+                           make use of it. */
+
+                        safe_close(fd);
+                }
         }
 
         r = server_open_syslog_socket(s);