#include <stddef.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
+#include <sys/statvfs.h>
#include "hashmap.h"
#include "journal-file.h"
#include "acl-util.h"
#include "cgroup-util.h"
#include "list.h"
+#include "journal-rate-limit.h"
+#include "sd-journal.h"
+#include "journal-internal.h"
#define USER_JOURNALS_MAX 1024
#define STDOUT_STREAMS_MAX 4096
+#define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
+#define DEFAULT_RATE_LIMIT_BURST 200
+
+#define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
+
typedef struct StdoutStream StdoutStream;
typedef struct Server {
char *buffer;
size_t buffer_size;
+ JournalRateLimit *rate_limit;
+
JournalMetrics metrics;
uint64_t max_use;
bool compress;
+ uint64_t cached_available_space;
+ usec_t cached_available_space_timestamp;
+
LIST_HEAD(StdoutStream, stdout_streams);
unsigned n_stdout_streams;
} Server;
LIST_FIELDS(StdoutStream, stdout_stream);
};
+static int server_flush_to_var(Server *s);
+
+static uint64_t available_space(Server *s) {
+ char ids[33];
+ sd_id128_t machine;
+ char *p;
+ const char *f;
+ struct statvfs ss;
+ uint64_t sum = 0, avail = 0, ss_avail = 0;
+ int r;
+ DIR *d;
+ usec_t ts = now(CLOCK_MONOTONIC);
+
+ if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
+ return s->cached_available_space;
+
+ r = sd_id128_get_machine(&machine);
+ if (r < 0)
+ return 0;
+
+ if (s->system_journal)
+ f = "/var/log/journal/";
+ else
+ f = "/run/log/journal/";
+
+ p = strappend(f, sd_id128_to_string(machine, ids));
+ if (!p)
+ return 0;
+
+ d = opendir(p);
+ free(p);
+
+ if (!d)
+ return 0;
+
+ if (fstatvfs(dirfd(d), &ss) < 0)
+ goto finish;
+
+ for (;;) {
+ struct stat st;
+ struct dirent buf, *de;
+ int k;
+
+ k = readdir_r(d, &buf, &de);
+ if (k != 0) {
+ r = -k;
+ goto finish;
+ }
+
+ if (!de)
+ break;
+
+ if (!dirent_is_file_with_suffix(de, ".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;
+ }
+
+ avail = sum >= s->max_use ? 0 : s->max_use - sum;
+
+ ss_avail = ss.f_bsize * ss.f_bavail;
+
+ ss_avail = ss_avail < s->metrics.keep_free ? 0 : ss_avail - s->metrics.keep_free;
+
+ if (ss_avail < avail)
+ avail = ss_avail;
+
+ s->cached_available_space = avail;
+ s->cached_available_space_timestamp = ts;
+
+finish:
+ closedir(d);
+
+ return avail;
+}
+
static void fix_perms(JournalFile *f, uid_t uid) {
acl_t acl;
acl_entry_t entry;
assert(s);
- /* We split up user logs only on /var, not on /run */
- if (!s->system_journal)
+ /* We split up user logs only on /var, not on /run. If the
+ * runtime file is open, we write to it exclusively, in order
+ * to guarantee proper order as soon as we flush /run to
+ * /var and close the runtime file. */
+
+ if (s->runtime_journal)
return s->runtime_journal;
if (uid <= 0)
if (r < 0 && r != -ENOENT)
log_error("Failed to vacuum %s: %s", p, strerror(-r));
free(p);
+
+ s->cached_available_space_timestamp = 0;
}
-static void dispatch_message(Server *s, struct iovec *iovec, unsigned n, unsigned m, struct ucred *ucred, struct timeval *tv) {
+static char *shortened_cgroup_path(pid_t pid) {
+ int r;
+ char *process_path, *init_path, *path;
+
+ assert(pid > 0);
+
+ r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &process_path);
+ if (r < 0)
+ return NULL;
+
+ r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &init_path);
+ if (r < 0) {
+ free(process_path);
+ return NULL;
+ }
+
+ if (streq(init_path, "/"))
+ init_path[0] = 0;
+
+ if (startswith(process_path, init_path))
+ path = process_path + strlen(init_path);
+ else
+ path = process_path;
+
+ free(init_path);
+
+ return path;
+}
+
+static void dispatch_message_real(Server *s,
+ struct iovec *iovec, unsigned n, unsigned m,
+ struct ucred *ucred,
+ struct timeval *tv) {
+
char *pid = NULL, *uid = NULL, *gid = NULL,
*source_time = NULL, *boot_id = NULL, *machine_id = NULL,
*comm = NULL, *cmdline = NULL, *hostname = NULL,
bool vacuumed = false;
assert(s);
- assert(iovec || n == 0);
-
- if (n == 0)
- return;
-
+ assert(iovec);
+ assert(n > 0);
assert(n + 13 <= m);
if (ucred) {
if (asprintf(&audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
IOVEC_SET_STRING(iovec[n++], audit_loginuid);
- r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, ucred->pid, &path);
- if (r >= 0) {
+ path = shortened_cgroup_path(ucred->pid);
+ if (path) {
cgroup = strappend("_SYSTEMD_CGROUP=", path);
if (cgroup)
IOVEC_SET_STRING(iovec[n++], cgroup);
+
free(path);
}
}
assert(n <= m);
+ server_flush_to_var(s);
+
retry:
f = find_journal(s, realuid == 0 ? 0 : loginuid);
if (!f)
free(cgroup);
}
+static void dispatch_message(Server *s,
+ struct iovec *iovec, unsigned n, unsigned m,
+ struct ucred *ucred,
+ struct timeval *tv,
+ int priority) {
+ int rl;
+ char *path, *c;
+
+ assert(s);
+ assert(iovec || n == 0);
+
+ if (n == 0)
+ return;
+
+ if (!ucred)
+ goto finish;
+
+ path = shortened_cgroup_path(ucred->pid);
+ if (!path)
+ goto finish;
+
+ /* example: /user/lennart/3/foobar
+ * /system/dbus.service/foobar
+ *
+ * So let's cut of everything past the third /, since that is
+ * wher user directories start */
+
+ c = strchr(path, '/');
+ if (c) {
+ c = strchr(c+1, '/');
+ if (c) {
+ c = strchr(c+1, '/');
+ if (c)
+ *c = 0;
+ }
+ }
+
+ rl = journal_rate_limit_test(s->rate_limit, path, priority, available_space(s));
+
+ if (rl == 0) {
+ free(path);
+ return;
+ }
+
+ if (rl > 1) {
+ int j = 0;
+ char suppress_message[LINE_MAX];
+ struct iovec suppress_iovec[15];
+
+ /* Write a suppression message if we suppressed something */
+
+ snprintf(suppress_message, sizeof(suppress_message), "MESSAGE=Suppressed %u messages from %s", rl - 1, path);
+ char_array_0(suppress_message);
+
+ IOVEC_SET_STRING(suppress_iovec[j++], "PRIORITY=5");
+ IOVEC_SET_STRING(suppress_iovec[j++], suppress_message);
+
+ dispatch_message_real(s, suppress_iovec, j, ELEMENTSOF(suppress_iovec), NULL, NULL);
+ }
+
+ free(path);
+
+finish:
+ dispatch_message_real(s, iovec, n, m, ucred, tv);
+}
+
static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL;
struct iovec iovec[16];
if (message)
IOVEC_SET_STRING(iovec[n++], message);
- dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv);
+ dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, priority & LOG_PRIMASK);
free(message);
free(syslog_facility);
unsigned n = 0, m = 0, j;
const char *p;
size_t remaining;
+ int priority = LOG_INFO;
assert(s);
assert(buffer || n == 0);
if (e == p) {
/* Entry separator */
- dispatch_message(s, iovec, n, m, ucred, tv);
+ dispatch_message(s, iovec, n, m, ucred, tv, priority);
n = 0;
+ priority = LOG_INFO;
p++;
remaining--;
iovec[n].iov_base = (char*) p;
iovec[n].iov_len = e - p;
n++;
+
+ /* We need to determine the priority
+ * of this entry for the rate limiting
+ * logic */
+ if (e - p == 10 &&
+ memcmp(p, "PRIORITY=", 10) == 0 &&
+ p[10] >= '0' &&
+ p[10] <= '9')
+ priority = p[10] - '0';
}
remaining -= (e - p) + 1;
}
}
- dispatch_message(s, iovec, n, m, ucred, tv);
+ dispatch_message(s, iovec, n, m, ucred, tv, priority);
for (j = 0; j < n; j++)
if (iovec[j].iov_base < buffer ||
n++;
}
- dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL);
+ dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, priority);
if (s->tee_console) {
int console;
return r;
}
+static int system_journal_open(Server *s) {
+ int r;
+ char *fn;
+ sd_id128_t machine;
+ char ids[33];
+
+ r = sd_id128_get_machine(&machine);
+ if (r < 0)
+ return r;
+
+ sd_id128_to_string(machine, ids);
+
+ if (!s->system_journal) {
+
+ /* First try to create the machine path, but not the prefix */
+ fn = strappend("/var/log/journal/", ids);
+ if (!fn)
+ return -ENOMEM;
+ (void) mkdir(fn, 0755);
+ free(fn);
+
+ /* The create the system journal file */
+ fn = join("/var/log/journal/", ids, "/system.journal", NULL);
+ if (!fn)
+ return -ENOMEM;
+
+ r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
+ free(fn);
+
+ if (r >= 0) {
+ s->system_journal->metrics = s->metrics;
+ s->system_journal->compress = s->compress;
+
+ fix_perms(s->system_journal, 0);
+ } else if (r < 0) {
+
+ if (r == -ENOENT)
+ r = 0;
+ else {
+ log_error("Failed to open system journal: %s", strerror(-r));
+ return r;
+ }
+ }
+ }
+
+ if (!s->runtime_journal) {
+
+ fn = join("/run/log/journal/", ids, "/system.journal", NULL);
+ if (!fn)
+ return -ENOMEM;
+
+ if (s->system_journal) {
+
+ /* Try to open the runtime journal, but only
+ * 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);
+ free(fn);
+
+ if (r < 0) {
+
+ if (r == -ENOENT)
+ r = 0;
+ else {
+ log_error("Failed to open runtime journal: %s", strerror(-r));
+ return r;
+ }
+ }
+
+ } else {
+
+ /* OK, we really need the runtime journal, so create
+ * it if necessary. */
+
+ (void) mkdir_parents(fn, 0755);
+ r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
+ free(fn);
+
+ if (r < 0) {
+ log_error("Failed to open runtime journal: %s", strerror(-r));
+ return r;
+ }
+ }
+
+ if (s->runtime_journal) {
+ s->runtime_journal->metrics = s->metrics;
+ s->runtime_journal->compress = s->compress;
+
+ fix_perms(s->runtime_journal, 0);
+ }
+ }
+
+ return r;
+}
+
+static int server_flush_to_var(Server *s) {
+ char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
+ Object *o = NULL;
+ int r;
+ sd_id128_t machine;
+ sd_journal *j;
+
+ assert(s);
+
+ system_journal_open(s);
+
+ if (!s->system_journal || !s->runtime_journal)
+ return 0;
+
+ r = sd_id128_get_machine(&machine);
+ if (r < 0) {
+ log_error("Failed to get machine id: %s", strerror(-r));
+ return r;
+ }
+
+ r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
+ if (r < 0) {
+ log_error("Failed to read runtime journal: %s", strerror(-r));
+ return r;
+ }
+
+ SD_JOURNAL_FOREACH(j) {
+ JournalFile *f;
+
+ f = j->current_file;
+ assert(f && f->current_offset > 0);
+
+ r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
+ if (r < 0) {
+ log_error("Can't read entry: %s", strerror(-r));
+ goto finish;
+ }
+
+ r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
+ if (r == -E2BIG) {
+ log_info("Allocation limit reached.");
+
+ journal_file_post_change(s->system_journal);
+ server_vacuum(s);
+
+ r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
+ }
+
+ if (r < 0) {
+ log_error("Can't write entry: %s", strerror(-r));
+ goto finish;
+ }
+ }
+
+finish:
+ journal_file_post_change(s->system_journal);
+
+ journal_file_close(s->runtime_journal);
+ s->runtime_journal = NULL;
+
+ if (r >= 0) {
+ sd_id128_to_string(machine, path + 17);
+ rm_rf(path, false, true, false);
+ }
+
+ return r;
+}
+
static int process_event(Server *s, struct epoll_event *ev) {
assert(s);
return -errno;
}
+ if (sfsi.ssi_signo == SIGUSR1) {
+ server_flush_to_var(s);
+ return 0;
+ }
+
log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
return 0;
return 0;
}
-static int system_journal_open(Server *s) {
- int r;
- char *fn;
- sd_id128_t machine;
- char ids[33];
-
- r = sd_id128_get_machine(&machine);
- if (r < 0)
- return r;
-
- /* First try to create the machine path, but not the prefix */
- fn = strappend("/var/log/journal/", sd_id128_to_string(machine, ids));
- if (!fn)
- return -ENOMEM;
- (void) mkdir(fn, 0755);
- free(fn);
-
- /* The create the system journal file */
- fn = join("/var/log/journal/", ids, "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
-
- r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
- free(fn);
-
- if (r >= 0) {
- s->system_journal->metrics = s->metrics;
- s->system_journal->compress = s->compress;
-
- fix_perms(s->system_journal, 0);
- return r;
- }
-
- if (r < 0 && r != -ENOENT) {
- log_error("Failed to open system journal: %s", strerror(-r));
- return r;
- }
-
- /* /var didn't work, so try /run, but this time we
- * create the prefix too */
- fn = join("/run/log/journal/", ids, "/system.journal", NULL);
- if (!fn)
- return -ENOMEM;
-
- (void) mkdir_parents(fn, 0755);
- r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
- free(fn);
-
- if (r < 0) {
- log_error("Failed to open runtime journal: %s", strerror(-r));
- return r;
- }
-
- s->runtime_journal->metrics = s->metrics;
- s->runtime_journal->compress = s->compress;
-
- fix_perms(s->runtime_journal, 0);
- return r;
-}
-
static int open_syslog_socket(Server *s) {
union sockaddr_union sa;
int one, r;
assert(s);
assert_se(sigemptyset(&mask) == 0);
- sigset_add_many(&mask, SIGINT, SIGTERM, -1);
+ sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, -1);
assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
if (r < 0)
return r;
+ s->rate_limit = journal_rate_limit_new(DEFAULT_RATE_LIMIT_INTERVAL, DEFAULT_RATE_LIMIT_BURST);
+ if (!s->rate_limit)
+ return -ENOMEM;
+
return 0;
}
if (s->stdout_fd >= 0)
close_nointr_nofail(s->stdout_fd);
+
+ if (s->rate_limit)
+ journal_rate_limit_free(s->rate_limit);
}
int main(int argc, char *argv[]) {
"READY=1\n"
"STATUS=Processing requests...");
+ server_vacuum(&server);
+ server_flush_to_var(&server);
+
for (;;) {
struct epoll_event event;