journald.c revision 81527be142678057215665be66e4b3c8306a7ab3
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering/***
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering This file is part of systemd.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Copyright 2011 Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is free software; you can redistribute it and/or modify it
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering under the terms of the GNU General Public License as published by
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering the Free Software Foundation; either version 2 of the License, or
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (at your option) any later version.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering systemd is distributed in the hope that it will be useful, but
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering General Public License for more details.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering You should have received a copy of the GNU General Public License
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering***/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <sys/epoll.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <sys/socket.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <errno.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <sys/signalfd.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <unistd.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include <fcntl.h>
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering#include <sys/acl.h>
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering#include <acl/libacl.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <stddef.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <sys/ioctl.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <linux/sockios.h>
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering#include <sys/statvfs.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-journal.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-login.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-messages.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-daemon.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "hashmap.h"
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering#include "journal-file.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "socket-util.h"
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering#include "acl-util.h"
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering#include "cgroup-util.h"
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering#include "list.h"
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering#include "journal-rate-limit.h"
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering#include "journal-internal.h"
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering#include "conf-parser.h"
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering#include "journald.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering#define USER_JOURNALS_MAX 1024
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering#define STDOUT_STREAMS_MAX 4096
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering#define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering#define DEFAULT_RATE_LIMIT_BURST 200
de97b26ac5e29063632312ec1a20eb6318ca924cLennart Poettering
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering#define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering#define RECHECK_VAR_AVAILABLE_USEC (30*USEC_PER_SEC)
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering#define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering#define N_IOVEC_META_FIELDS 16
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringtypedef enum StdoutStreamState {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering STDOUT_STREAM_TAG,
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering STDOUT_STREAM_PRIORITY,
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering STDOUT_STREAM_PRIORITY_PREFIX,
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering STDOUT_STREAM_FORWARD_TO_SYSLOG,
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering STDOUT_STREAM_FORWARD_TO_KMSG,
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering STDOUT_STREAM_FORWARD_TO_CONSOLE,
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering STDOUT_STREAM_RUNNING
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering} StdoutStreamState;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstruct StdoutStream {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering Server *server;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering StdoutStreamState state;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering struct ucred ucred;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering char *tag;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int priority;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering bool priority_prefix:1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering bool forward_to_syslog:1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering bool forward_to_kmsg:1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering bool forward_to_console:1;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering char buffer[LINE_MAX+1];
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering size_t length;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_FIELDS(StdoutStream, stdout_stream);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering};
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int server_flush_to_var(Server *s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic uint64_t available_space(Server *s) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering char ids[33], *p;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering const char *f;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering sd_id128_t machine;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct statvfs ss;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering uint64_t sum = 0, avail = 0, ss_avail = 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering int r;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering DIR *d;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering usec_t ts;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering JournalMetrics *m;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering ts = now(CLOCK_MONOTONIC);
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering return s->cached_available_space;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering r = sd_id128_get_machine(&machine);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (r < 0)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (s->system_journal) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering f = "/var/log/journal/";
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering m = &s->system_metrics;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering } else {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering f = "/run/log/journal/";
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering m = &s->runtime_metrics;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering }
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering assert(m);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering p = strappend(f, sd_id128_to_string(machine, ids));
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!p)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering d = opendir(p);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering free(p);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!d)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (fstatvfs(dirfd(d), &ss) < 0)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering goto finish;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering for (;;) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct stat st;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct dirent buf, *de;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering int k;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering k = readdir_r(d, &buf, &de);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (k != 0) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering r = -k;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering goto finish;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!de)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering break;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!dirent_is_file_with_suffix(de, ".journal"))
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering continue;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering continue;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering sum += (uint64_t) st.st_blocks * (uint64_t) st.st_blksize;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering avail = sum >= m->max_use ? 0 : m->max_use - sum;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering ss_avail = ss.f_bsize * ss.f_bavail;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering ss_avail = ss_avail < m->keep_free ? 0 : ss_avail - m->keep_free;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (ss_avail < avail)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering avail = ss_avail;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering s->cached_available_space = avail;
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering s->cached_available_space_timestamp = ts;
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringfinish:
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering closedir(d);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return avail;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering}
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poetteringstatic void fix_perms(JournalFile *f, uid_t uid) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_t acl;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_entry_t entry;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_permset_t permset;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering int r;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering assert(f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering r = fchmod_and_fchown(f->fd, 0640, 0, 0);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (r < 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (uid <= 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl = acl_get_fd(f->fd);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (!acl) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering }
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering r = acl_find_uid(acl, uid, &entry);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (r <= 0) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (acl_create_entry(&acl, &entry) < 0 ||
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_set_tag_type(entry, ACL_USER) < 0 ||
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_set_qualifier(entry, &uid) < 0) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering goto finish;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering }
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering }
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (acl_get_permset(entry, &permset) < 0 ||
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_add_perm(permset, ACL_READ) < 0 ||
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_calc_mask(&acl) < 0) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering goto finish;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering }
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (acl_set_fd(f->fd, acl) < 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poetteringfinish:
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_free(acl);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering}
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poetteringstatic JournalFile* find_journal(Server *s, uid_t uid) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering char *p;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering int r;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering JournalFile *f;
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering char ids[33];
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering sd_id128_t machine;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering assert(s);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* We split up user logs only on /var, not on /run. If the
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * runtime file is open, we write to it exclusively, in order
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * to guarantee proper order as soon as we flush /run to
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * /var and close the runtime file. */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (s->runtime_journal)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return s->runtime_journal;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (uid <= 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return s->system_journal;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering r = sd_id128_get_machine(&machine);
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering if (r < 0)
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering return s->system_journal;
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (f)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return f;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
3fbf9cbb02690e40cd65802e777519f3f3c8d88aLennart Poettering if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return s->system_journal;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering while (hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering /* Too many open? Then let's close one */
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering f = hashmap_steal_first(s->user_journals);
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering assert(f);
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering journal_file_close(f);
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering }
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering
c2373f848dddcc1827cf715c5ef778dc8d475761Lennart Poettering r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering free(p);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (r < 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return s->system_journal;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering fix_perms(f, uid);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering f->metrics = s->system_metrics;
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering f->compress = s->compress;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (r < 0) {
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering journal_file_close(f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return s->system_journal;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering }
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return f;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering}
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poetteringstatic void server_rotate(Server *s) {
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering JournalFile *f;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering void *k;
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering Iterator i;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering int r;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_info("Rotating...");
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (s->runtime_journal) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_file_rotate(&s->runtime_journal);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0)
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (s->system_journal) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_file_rotate(&s->system_journal);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0)
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = journal_file_rotate(&f);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0)
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to rotate %s: %s", f->path, strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering else
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering hashmap_replace(s->user_journals, k, f);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering}
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poetteringstatic void server_vacuum(Server *s) {
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering char *p;
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering char ids[33];
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering sd_id128_t machine;
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering int r;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_info("Vacuuming...");
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering r = sd_id128_get_machine(&machine);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_error("Failed to get machine ID: %s", strerror(-r));
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering return;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering sd_id128_to_string(machine, ids);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (s->system_journal) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (asprintf(&p, "/var/log/journal/%s", ids) < 0) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering log_error("Out of memory.");
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering return;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (r < 0 && r != -ENOENT)
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering log_error("Failed to vacuum %s: %s", p, strerror(-r));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering free(p);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (s->runtime_journal) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering log_error("Out of memory.");
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering return;
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering }
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering if (r < 0 && r != -ENOENT)
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering log_error("Failed to vacuum %s: %s", p, strerror(-r));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering free(p);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering }
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering
9cfb57c989b62d11c073c77179df4bb7fa19f35dLennart Poettering s->cached_available_space_timestamp = 0;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering}
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic char *shortened_cgroup_path(pid_t pid) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering int r;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering char *process_path, *init_path, *path;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering assert(pid > 0);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &process_path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (r < 0)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return NULL;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &init_path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (r < 0) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering free(process_path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return NULL;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
bad75c274a488715a845ff8e5ec3140036929e94Lennart Poettering if (endswith(init_path, "/system"))
bad75c274a488715a845ff8e5ec3140036929e94Lennart Poettering init_path[strlen(init_path) - 7] = 0;
bad75c274a488715a845ff8e5ec3140036929e94Lennart Poettering else if (streq(init_path, "/"))
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering init_path[0] = 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering if (startswith(process_path, init_path)) {
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering char *p;
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering p = strdup(process_path + strlen(init_path));
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering if (!p) {
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering free(process_path);
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering free(init_path);
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering return NULL;
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering }
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering path = p;
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering } else {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering path = process_path;
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering process_path = NULL;
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering free(process_path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering free(init_path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return path;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering}
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic void dispatch_message_real(Server *s,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct iovec *iovec, unsigned n, unsigned m,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct ucred *ucred,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct timeval *tv) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *pid = NULL, *uid = NULL, *gid = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *comm = NULL, *cmdline = NULL, *hostname = NULL,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering *audit_session = NULL, *audit_loginuid = NULL,
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering *exe = NULL, *cgroup = NULL, *session = NULL,
94fb446e55babb713fb24850455627acf30d999bLennart Poettering *owner_uid = NULL, *unit = NULL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering char idbuf[33];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering sd_id128_t id;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering int r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering char *t;
de190aef08bb267b645205a747762df573b36834Lennart Poettering uid_t loginuid = 0, realuid = 0;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering JournalFile *f;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering bool vacuumed = false;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(s);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering assert(iovec);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering assert(n > 0);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(n + N_IOVEC_META_FIELDS <= m);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (ucred) {
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering uint32_t audit;
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering uid_t owner;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
de190aef08bb267b645205a747762df573b36834Lennart Poettering realuid = ucred->uid;
de190aef08bb267b645205a747762df573b36834Lennart Poettering
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&pid, "_PID=%lu", (unsigned long) ucred->pid) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], pid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&uid, "_UID=%lu", (unsigned long) ucred->uid) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], uid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&gid, "_GID=%lu", (unsigned long) ucred->gid) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], gid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = get_process_comm(ucred->pid, &t);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0) {
de190aef08bb267b645205a747762df573b36834Lennart Poettering comm = strappend("_COMM=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (comm)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], comm);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = get_process_exe(ucred->pid, &t);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0) {
de190aef08bb267b645205a747762df573b36834Lennart Poettering exe = strappend("_EXE=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (comm)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], exe);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0) {
de190aef08bb267b645205a747762df573b36834Lennart Poettering cmdline = strappend("_CMDLINE=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (cmdline)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], cmdline);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering r = audit_session_from_pid(ucred->pid, &audit);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0)
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (asprintf(&audit_session, "_AUDIT_SESSION=%lu", (unsigned long) audit) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], audit_session);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = audit_loginuid_from_pid(ucred->pid, &loginuid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0)
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], audit_loginuid);
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering t = shortened_cgroup_path(ucred->pid);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (t) {
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering cgroup = strappend("_SYSTEMD_CGROUP=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering if (cgroup)
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering IOVEC_SET_STRING(iovec[n++], cgroup);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering }
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (sd_pid_get_session(ucred->pid, &t) >= 0) {
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering session = strappend("_SYSTEMD_SESSION=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (session)
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering IOVEC_SET_STRING(iovec[n++], session);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
94fb446e55babb713fb24850455627acf30d999bLennart Poettering if (sd_pid_get_unit(ucred->pid, &t) >= 0) {
94fb446e55babb713fb24850455627acf30d999bLennart Poettering unit = strappend("_SYSTEMD_UNIT=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
94fb446e55babb713fb24850455627acf30d999bLennart Poettering if (unit)
94fb446e55babb713fb24850455627acf30d999bLennart Poettering IOVEC_SET_STRING(iovec[n++], unit);
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering }
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (sd_pid_get_owner_uid(ucred->uid, &owner) >= 0)
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering if (asprintf(&owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner) >= 0)
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering IOVEC_SET_STRING(iovec[n++], owner_uid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (tv) {
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu",
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering (unsigned long long) timeval_load(tv)) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], source_time);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering /* Note that strictly speaking storing the boot id here is
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering * redundant since the entry includes this in-line
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering * anyway. However, we need this indexed, too. */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = sd_id128_get_boot(&id);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0)
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&boot_id, "_BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], boot_id);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = sd_id128_get_machine(&id);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r >= 0)
de190aef08bb267b645205a747762df573b36834Lennart Poettering if (asprintf(&machine_id, "_MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], machine_id);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering t = gethostname_malloc();
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (t) {
de190aef08bb267b645205a747762df573b36834Lennart Poettering hostname = strappend("_HOSTNAME=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (hostname)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering IOVEC_SET_STRING(iovec[n++], hostname);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(n <= m);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering server_flush_to_var(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poetteringretry:
de190aef08bb267b645205a747762df573b36834Lennart Poettering f = find_journal(s, realuid == 0 ? 0 : loginuid);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (!f)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_warning("Dropping message, as we can't find a place to store the data.");
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering else {
c2373f848dddcc1827cf715c5ef778dc8d475761Lennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r == -E2BIG && !vacuumed) {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_info("Allocation limit reached.");
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering server_rotate(s);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering server_vacuum(s);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering vacuumed = true;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering log_info("Retrying write.");
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering goto retry;
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (r < 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_error("Failed to write entry, ignoring: %s", strerror(-r));
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(pid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(uid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(gid);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(comm);
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering free(exe);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(cmdline);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(source_time);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(boot_id);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(machine_id);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(hostname);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(audit_session);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(audit_loginuid);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering free(cgroup);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(session);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(owner_uid);
94fb446e55babb713fb24850455627acf30d999bLennart Poettering free(unit);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char mid[11 + 32 + 1];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char buffer[16 + LINE_MAX + 1];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 3];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering int n = 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering va_list ap;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct ucred ucred;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(s);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(format);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], "PRIORITY=5");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcpy(buffer, "MESSAGE=", 8);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering va_start(ap, format);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering va_end(ap);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(buffer);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], buffer);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(mid, sizeof(mid), "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(message_id));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(mid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], mid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering zero(ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering ucred.pid = getpid();
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering ucred.uid = getuid();
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering ucred.gid = getgid();
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringstatic void dispatch_message(Server *s,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct iovec *iovec, unsigned n, unsigned m,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct ucred *ucred,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering struct timeval *tv,
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering int priority) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering int rl;
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering char *path = NULL, *c;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering assert(s);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering assert(iovec || n == 0);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (n == 0)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!ucred)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering goto finish;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering path = shortened_cgroup_path(ucred->pid);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!path)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering goto finish;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering /* example: /user/lennart/3/foobar
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * /system/dbus.service/foobar
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering *
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * So let's cut of everything past the third /, since that is
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * wher user directories start */
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering c = strchr(path, '/');
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (c) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering c = strchr(c+1, '/');
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (c) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering c = strchr(c+1, '/');
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (c)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering *c = 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering rl = journal_rate_limit_test(s->rate_limit, path, priority & LOG_PRIMASK, available_space(s));
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (rl == 0) {
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering free(path);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Write a suppression message if we suppressed something */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (rl > 1)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering driver_message(s, SD_MESSAGE_JOURNAL_DROPPED, "Suppressed %u messages from %s", rl - 1, path);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(path);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringfinish:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message_real(s, iovec, n, m, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct msghdr msghdr;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct cmsghdr *cmsg;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering union {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct cmsghdr cmsghdr;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } control;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering union sockaddr_union sa;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(s);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(iovec);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(n_iovec > 0);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering zero(msghdr);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_iov = (struct iovec*) iovec;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_iovlen = n_iovec;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering zero(sa);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering sa.un.sun_family = AF_UNIX;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_name = &sa;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering zero(control);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_control = &control;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_controllen = sizeof(control);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering cmsg = CMSG_FIRSTHDR(&msghdr);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering cmsg->cmsg_level = SOL_SOCKET;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering cmsg->cmsg_type = SCM_CREDENTIALS;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering msghdr.msg_controllen = cmsg->cmsg_len;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Forward the syslog message we received via /dev/log to
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * /run/systemd/syslog. Unfortunately we currently can't set
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * the SO_TIMESTAMP auxiliary data, and hence we don't. */
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (ucred && errno == ESRCH) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct ucred u;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Hmm, presumably the sender process vanished
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * by now, so let's fix it as good as we
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * can, and retry */
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering u = *ucred;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering u.pid = getpid();
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering }
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to forward syslog message: %m");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_syslog_raw(Server *s, const char *buffer, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(s);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(buffer);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec, buffer);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog_iovec(s, &iovec, 1, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_syslog(Server *s, int priority, const char *tag, const char *message, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[5];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char header_priority[6], header_time[64], header_pid[16];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering int n = 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering time_t t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct tm *tm;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *tag_buf = NULL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(s);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(priority >= 0);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(priority <= 999);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* First: priority field */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(header_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Second: timestamp */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering tm = localtime(&t);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (!tm)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_time);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Third: tag and PID */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (!tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering get_process_comm(ucred->pid, &tag_buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering tag = tag_buf;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (tag)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } else if (tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], ": ");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Fourth: message */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog_iovec(s, iovec, n, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(tag_buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic int fixup_priority(int priority) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if ((priority & LOG_FACMASK) == 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return (priority & LOG_PRIMASK) | LOG_USER;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return priority;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_kmsg(Server *s, int priority, const char *tag, const char *message, struct ucred *ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[5];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char header_priority[6], header_pid[16];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering int n = 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *tag_buf = NULL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering int fd;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(s);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(priority >= 0);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(priority <= 999);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Never allow messages with kernel facility to be written to
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering * kmsg, regardless where the data comes from. */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = fixup_priority(priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* First: priority field */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(header_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Second: tag and PID */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (!tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering get_process_comm(ucred->pid, &tag_buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering tag = tag_buf;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (tag)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } else if (tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], ": ");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Fourth: message */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], "\n");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (fd < 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to open /dev/kmsg for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering goto finish;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (writev(fd, iovec, n) < 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering close_nointr_nofail(fd);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poetteringfinish:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(tag_buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void forward_console(Server *s, const char *tag, const char *message, struct ucred *ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[4];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char header_pid[16];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering int n = 0, fd;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *tag_buf = NULL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(s);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* First: tag and PID */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (ucred) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (!tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering get_process_comm(ucred->pid, &tag_buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering tag = tag_buf;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char_array_0(header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (tag)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } else if (tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], ": ");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering /* Third: message */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], "\n");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (fd < 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to open /dev/console for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering goto finish;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (writev(fd, iovec, n) < 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_debug("Failed to write to /dev/console for logging: %s", strerror(errno));
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering close_nointr_nofail(fd);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringfinish:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(tag_buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic void read_tag(const char **buf, char **tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering const char *p;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering size_t l, e;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering assert(tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p = *buf;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p += strspn(p, WHITESPACE);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering l = strcspn(p, WHITESPACE);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (l <= 0 ||
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p[l-1] != ':')
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering e = l;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering l--;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (p[l-1] == ']') {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering size_t k = l-1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering for (;;) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (p[k] == '[') {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering l = k;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering break;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (k == 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering break;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering k--;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering t = strndup(p, l);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (t)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering *tag = t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering *buf = p + e;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering *buf += strspn(*buf, WHITESPACE);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering}
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_tag = NULL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unsigned n = 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int priority = LOG_USER | LOG_INFO;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *tag = NULL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(buf);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_syslog)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog_raw(s, buf, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering parse_syslog_priority((char**) &buf, &priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering skip_syslog_date((char**) &buf);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering read_tag(&buf, &tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_kmsg)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_kmsg(s, priority, tag, buf, ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_console)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_console(s, tag, buf, ucred);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (priority & LOG_FACMASK)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering syslog_tag = strappend("SYSLOG_TAG=", tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (syslog_tag)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering message = strappend("MESSAGE=", buf);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (message)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering IOVEC_SET_STRING(iovec[n++], message);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering free(message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(tag);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering free(syslog_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(syslog_facility);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(syslog_tag);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poetteringstatic bool valid_user_field(const char *p, size_t l) {
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering const char *a;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* We kinda enforce POSIX syntax recommendations for
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering environment variables here, but make a couple of additional
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering requirements.
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* No empty field names */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (l <= 0)
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering return false;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Don't allow names longer than 64 chars */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (l > 64)
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering return false;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Variables starting with an underscore are protected */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (p[0] == '_')
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering return false;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Don't allow digits as first character */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (p[0] >= '0' && p[0] <= '9')
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering return false;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Only allow A-Z0-9 and '_' */
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering for (a = p; a < p + l; a++)
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (!((*a >= 'A' && *a <= 'Z') ||
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering (*a >= '0' && *a <= '9') ||
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering *a == '_'))
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering return false;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering return true;
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering}
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic void process_native_message(Server *s, const void *buffer, size_t buffer_size, struct ucred *ucred, struct timeval *tv) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering struct iovec *iovec = NULL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unsigned n = 0, m = 0, j;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering const char *p;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering size_t remaining;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering int priority = LOG_INFO;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *tag = NULL, *message = NULL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(buffer || n == 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering p = buffer;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering remaining = buffer_size;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering while (remaining > 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering const char *e, *q;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering e = memchr(p, '\n', remaining);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!e) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Trailing noise, let's ignore it, and flush what we collected */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Received message with trailing noise, ignoring.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering break;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (e == p) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* Entry separator */
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering dispatch_message(s, iovec, n, m, ucred, tv, priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering n = 0;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering priority = LOG_INFO;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering p++;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering remaining--;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering continue;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (*p == '.' || *p == '#') {
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering /* Ignore control commands for now, and
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering * comments too. */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering remaining -= (e - p) + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering p = e + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering continue;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering /* A property follows */
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (n+N_IOVEC_META_FIELDS >= m) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering struct iovec *c;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unsigned u;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering u = MAX((n+N_IOVEC_META_FIELDS) * 2U, 4U);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering c = realloc(iovec, u * sizeof(struct iovec));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!c) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Out of memory");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering break;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering iovec = c;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering m = u;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering q = memchr(p, '=', e - p);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (q) {
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (valid_user_field(p, q - p)) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering size_t l;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering l = e - p;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering /* If the field name starts with an
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering * underscore, skip the variable,
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering * since that indidates a trusted
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering * field */
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering iovec[n].iov_base = (char*) p;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering iovec[n].iov_len = l;
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering n++;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering /* We need to determine the priority
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * of this entry for the rate limiting
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering * logic */
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (l == 10 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "PRIORITY=", 9) == 0 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p[9] >= '0' && p[9] <= '9')
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = (priority & LOG_FACMASK) | (p[9] - '0');
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (l == 17 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p[16] >= '0' && p[16] <= '9')
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = (priority & LOG_PRIMASK) | ((p[16] - '0') << 3);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (l == 18 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p[16] >= '0' && p[16] <= '9' &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p[17] >= '0' && p[17] <= '9')
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering priority = (priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (l >= 12 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "SYSLOG_TAG=", 11) == 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering t = strndup(p + 11, l - 11);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (t) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering tag = t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } else if (l >= 8 &&
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering memcmp(p, "MESSAGE=", 8) == 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering t = strndup(p + 8, l - 8);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (t) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(message);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering message = t;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering remaining -= (e - p) + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering p = e + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering continue;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering } else {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering uint64_t l;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *k;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Failed to parse message, ignoring.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering break;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering memcpy(&l, e + 1, sizeof(uint64_t));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering l = le64toh(l);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering e[1+sizeof(uint64_t)+l] != '\n') {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_debug("Failed to parse message, ignoring.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering break;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering k = malloc((e - p) + 1 + l);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!k) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Out of memory");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering break;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering memcpy(k, p, e - p);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering k[e - p] = '=';
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
6ad1d1c30621280bfad3e63fcc1c7ceb7d8ffa98Lennart Poettering if (valid_user_field(p, e - p)) {
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering iovec[n].iov_base = k;
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering iovec[n].iov_len = (e - p) + 1 + l;
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering n++;
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering } else
2b0ba69bb127b6b1d76512ce32fc9cfd89670f97Lennart Poettering free(k);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering p = e + 1 + sizeof(uint64_t) + l + 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (message) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_syslog)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog(s, priority, tag, message, ucred, tv);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_kmsg)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_kmsg(s, priority, tag, message, ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_console)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_console(s, tag, message, ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering dispatch_message(s, iovec, n, m, ucred, tv, priority);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (j = 0; j < n; j++)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (iovec[j].iov_base < buffer ||
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering free(iovec[j].iov_base);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(message);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic int stdout_stream_log(StdoutStream *s, const char *p) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_tag = NULL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering unsigned n = 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int priority;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(p);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering priority = s->priority;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->priority_prefix)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering parse_syslog_priority((char**) &p, &priority);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_syslog || s->server->forward_to_syslog)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_syslog(s->server, fixup_priority(priority), s->tag, p, &s->ucred, NULL);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_kmsg || s->server->forward_to_kmsg)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_kmsg(s->server, priority, s->tag, p, &s->ucred);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->forward_to_console || s->server->forward_to_console)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering forward_console(s->server, s->tag, p, &s->ucred);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_priority);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (priority & LOG_FACMASK)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_facility);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (s->tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering syslog_tag = strappend("SYSLOG_TAG=", s->tag);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (syslog_tag)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], syslog_tag);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering message = strappend("MESSAGE=", p);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (message)
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering IOVEC_SET_STRING(iovec[n++], message);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, priority);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering free(message);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering free(syslog_priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(syslog_facility);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering free(syslog_tag);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poetteringstatic int stdout_stream_line(StdoutStream *s, char *p) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering int r;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(p);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p = strstrip(p);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering switch (s->state) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering case STDOUT_STREAM_TAG:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->tag = strdup(p);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (!s->tag) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_error("Out of memory");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return -ENOMEM;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->state = STDOUT_STREAM_PRIORITY;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering case STDOUT_STREAM_PRIORITY:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = safe_atoi(p, &s->priority);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (r < 0 || s->priority <= 0 || s->priority >= 999) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to parse log priority line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->state = STDOUT_STREAM_PRIORITY_PREFIX;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering case STDOUT_STREAM_PRIORITY_PREFIX:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = parse_boolean(p);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (r < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to parse priority prefix line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->priority_prefix = !!r;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering case STDOUT_STREAM_FORWARD_TO_SYSLOG:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = parse_boolean(p);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (r < 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_warning("Failed to parse forward to syslog line.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->forward_to_syslog = !!r;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering case STDOUT_STREAM_FORWARD_TO_KMSG:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = parse_boolean(p);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (r < 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_warning("Failed to parse copy to kmsg line.");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return -EINVAL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->forward_to_kmsg = !!r;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering case STDOUT_STREAM_FORWARD_TO_CONSOLE:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = parse_boolean(p);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (r < 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering log_warning("Failed to parse copy to console line.");
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return -EINVAL;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering }
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->forward_to_console = !!r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->state = STDOUT_STREAM_RUNNING;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering case STDOUT_STREAM_RUNNING:
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering return stdout_stream_log(s, p);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert_not_reached("Unknown stream state");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_scan(StdoutStream *s, bool force_flush) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering char *p;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering size_t remaining;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering p = s->buffer;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering remaining = s->length;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering for (;;) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering char *end;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering size_t skip;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering end = memchr(p, '\n', remaining);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering if (end)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering skip = end - p + 1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering else if (remaining >= sizeof(s->buffer) - 1) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering end = p + sizeof(s->buffer) - 1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering skip = sizeof(s->buffer) - 1;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering } else
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering break;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering *end = 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = stdout_stream_line(s, p);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering remaining -= skip;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering p += skip;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (force_flush && remaining > 0) {
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering p[remaining] = 0;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering r = stdout_stream_line(s, p);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering p += remaining;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering remaining = 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (p > s->buffer) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering memmove(s->buffer, p, remaining);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->length = remaining;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_process(StdoutStream *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ssize_t l;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (l < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (errno == EAGAIN)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Failed to read from stream: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (l == 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = stdout_stream_scan(s, true);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->length += l;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = stdout_stream_scan(s, false);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 1;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic void stdout_stream_free(StdoutStream *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->server) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s->server->n_stdout_streams > 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->server->n_stdout_streams --;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->fd >= 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->server)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(s->fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering free(s->tag);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering free(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int stdout_stream_new(Server *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering StdoutStream *stream;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int fd, r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering socklen_t len;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering struct epoll_event ev;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (fd < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (errno == EAGAIN)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to accept stdout connection: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_warning("Too many stdout streams, refusing connection.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stream = new0(StdoutStream, 1);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (!stream) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Out of memory.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -ENOMEM;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stream->fd = fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering len = sizeof(stream->ucred);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to determine peer credentials: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering goto fail;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (shutdown(fd, SHUT_WR) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to shutdown writing side of socket: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering goto fail;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering zero(ev);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.data.ptr = stream;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.events = EPOLLIN;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add stream to event loop: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering goto fail;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stream->server = s;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->n_stdout_streams ++;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringfail:
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stdout_stream_free(stream);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int system_journal_open(Server *s) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering int r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering char *fn;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_id128_t machine;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering char ids[33];
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = sd_id128_get_machine(&machine);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_id128_to_string(machine, ids);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!s->system_journal) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* First try to create the machine path, but not the prefix */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = strappend("/var/log/journal/", ids);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!fn)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return -ENOMEM;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering (void) mkdir(fn, 0755);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering free(fn);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* The create the system journal file */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = join("/var/log/journal/", ids, "/system.journal", NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!fn)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return -ENOMEM;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering free(fn);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r >= 0) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering journal_default_metrics(&s->system_metrics, s->system_journal->fd);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering s->system_journal->metrics = s->system_metrics;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering s->system_journal->compress = s->compress;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fix_perms(s->system_journal, 0);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering } else if (r < 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering if (r != -ENOENT && r != -EROFS)
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering log_warning("Failed to open system journal: %s", strerror(-r));
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering r = 0;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!s->runtime_journal) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = join("/run/log/journal/", ids, "/system.journal", NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!fn)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return -ENOMEM;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (s->system_journal) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* Try to open the runtime journal, but only
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * if it already exists, so that we can flush
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * it into the system journal */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering free(fn);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0) {
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering if (r != -ENOENT)
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering log_warning("Failed to open runtime journal: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
adf7d506b50af9ba398a9925c8cd47ba328e720cLennart Poettering r = 0;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering } else {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering /* OK, we really need the runtime journal, so create
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering * it if necessary. */
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering (void) mkdir_parents(fn, 0755);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering free(fn);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Failed to open runtime journal: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (s->runtime_journal) {
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering journal_default_metrics(&s->runtime_metrics, s->runtime_journal->fd);
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering s->runtime_journal->metrics = s->runtime_metrics;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering s->runtime_journal->compress = s->compress;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fix_perms(s->runtime_journal, 0);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering}
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int server_flush_to_var(Server *s) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering Object *o = NULL;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering int r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_id128_t machine;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_journal *j;
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering usec_t ts;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering assert(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering if (!s->runtime_journal)
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering return 0;
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering ts = now(CLOCK_MONOTONIC);
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering if (s->var_available_timestamp + RECHECK_VAR_AVAILABLE_USEC > ts)
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering return 0;
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering s->var_available_timestamp = ts;
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering system_journal_open(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering if (!s->system_journal)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return 0;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = sd_id128_get_machine(&machine);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Failed to get machine id: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Failed to read runtime journal: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering SD_JOURNAL_FOREACH(j) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering JournalFile *f;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering f = j->current_file;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering assert(f && f->current_offset > 0);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Can't read entry: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering goto finish;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r == -E2BIG) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_info("Allocation limit reached.");
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering journal_file_post_change(s->system_journal);
b1a0ab714863ae77e4683820757b3f49c03a0049Lennart Poettering server_rotate(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering server_vacuum(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r < 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering log_error("Can't write entry: %s", strerror(-r));
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering goto finish;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringfinish:
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering journal_file_post_change(s->system_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering journal_file_close(s->runtime_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering s->runtime_journal = NULL;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (r >= 0) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_id128_to_string(machine, path + 17);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering rm_rf(path, false, true, false);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering}
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int process_event(Server *s, struct epoll_event *ev) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (ev->data.fd == s->signal_fd) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct signalfd_siginfo sfsi;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering ssize_t n;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (ev->events != EPOLLIN) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EIO;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering n = read(s->signal_fd, &sfsi, sizeof(sfsi));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (n != sizeof(sfsi)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (n >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -EIO;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (errno == EINTR || errno == EAGAIN)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (sfsi.ssi_signo == SIGUSR1) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering server_flush_to_var(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return 0;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (ev->data.fd == s->native_fd ||
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev->data.fd == s->syslog_fd) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (ev->events != EPOLLIN) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EIO;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (;;) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct msghdr msghdr;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct iovec iovec;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct ucred *ucred = NULL;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct timeval *tv = NULL;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct cmsghdr *cmsg;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering union {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct cmsghdr cmsghdr;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering CMSG_SPACE(sizeof(struct timeval))];
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } control;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering ssize_t n;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int v;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("SIOCINQ failed: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (v <= 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->buffer_size < (size_t) v) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering void *b;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering size_t l;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering b = realloc(s->buffer, l+1);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (!b) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Couldn't increase buffer.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -ENOMEM;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->buffer_size = l;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->buffer = b;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering zero(iovec);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering iovec.iov_base = s->buffer;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering iovec.iov_len = s->buffer_size;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering zero(control);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering zero(msghdr);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering msghdr.msg_iov = &iovec;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering msghdr.msg_iovlen = 1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering msghdr.msg_control = &control;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering msghdr.msg_controllen = sizeof(control);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (n < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (errno == EINTR || errno == EAGAIN)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("recvmsg() failed: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (cmsg->cmsg_level == SOL_SOCKET &&
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering cmsg->cmsg_type == SCM_CREDENTIALS &&
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering ucred = (struct ucred*) CMSG_DATA(cmsg);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering cmsg->cmsg_type == SO_TIMESTAMP &&
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering tv = (struct timeval*) CMSG_DATA(cmsg);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (ev->data.fd == s->syslog_fd) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *e;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering e = memchr(s->buffer, '\n', n);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (e)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering *e = 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering else
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->buffer[n] = 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering process_syslog_message(s, strstrip(s->buffer), ucred, tv);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering } else
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering process_native_message(s, s->buffer, n, ucred, tv);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering return 1;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (ev->data.fd == s->stdout_fd) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (ev->events != EPOLLIN) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EIO;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stdout_stream_new(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 1;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering StdoutStream *stream;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_info("Got invalid event from epoll.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EIO;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering /* If it is none of the well-known fds, it must be an
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * stdout stream fd. Note that this is a bit ugly here
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * (since we rely that none of the well-known fds
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * could be interpreted as pointer), but nonetheless
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * safe, since the well-known fds would never get an
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering * fd > 4096, i.e. beyond the first memory page */
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stream = ev->data.ptr;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (stdout_stream_process(stream) <= 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stdout_stream_free(stream);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering log_error("Unknown event.");
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering return 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int open_syslog_socket(Server *s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering union sockaddr_union sa;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int one, r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering struct epoll_event ev;
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering struct timeval tv;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering assert(s);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->syslog_fd < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->syslog_fd < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("socket() failed: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering zero(sa);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering sa.un.sun_family = AF_UNIX;
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering unlink(sa.un.sun_path);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("bind() failed: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering chmod(sa.un.sun_path, 0666);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering one = 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("SO_PASSCRED failed: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering one = 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("SO_TIMESTAMP failed: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering /* Since we use the same socket for forwarding this to some
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering * other syslog implementation, make sure we don't hang
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering * forever */
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering if (setsockopt(s->syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering log_error("SO_SNDTIMEO failed: %m");
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering return -errno;
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering }
8b18eb674ce4d14e4819e102a0d6679a0fd2e6ceLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering zero(ev);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.events = EPOLLIN;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.data.fd = s->syslog_fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add syslog server fd to epoll object: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poetteringstatic int open_native_socket(Server*s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering union sockaddr_union sa;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int one, r;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering struct epoll_event ev;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->native_fd < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->native_fd < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("socket() failed: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering zero(sa);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering sa.un.sun_family = AF_UNIX;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering strncpy(sa.un.sun_path, "/run/systemd/journal", sizeof(sa.un.sun_path));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering unlink(sa.un.sun_path);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("bind() failed: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering chmod(sa.un.sun_path, 0666);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering one = 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("SO_PASSCRED failed: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering one = 1;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("SO_TIMESTAMP failed: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering zero(ev);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.events = EPOLLIN;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.data.fd = s->native_fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->native_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add native server fd to epoll object: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return 0;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int open_stdout_socket(Server *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering union sockaddr_union sa;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering struct epoll_event ev;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->stdout_fd < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->stdout_fd < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("socket() failed: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering zero(sa);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering sa.un.sun_family = AF_UNIX;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering strncpy(sa.un.sun_path, "/run/systemd/stdout", sizeof(sa.un.sun_path));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering unlink(sa.un.sun_path);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("bind() failed: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering chmod(sa.un.sun_path, 0666);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (listen(s->stdout_fd, SOMAXCONN) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("liste() failed: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering zero(ev);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.events = EPOLLIN;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.data.fd = s->stdout_fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Failed to add stdout server fd to epoll object: %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int open_signalfd(Server *s) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering sigset_t mask;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering struct epoll_event ev;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert_se(sigemptyset(&mask) == 0);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, -1);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->signal_fd < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("signalfd(): %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering zero(ev);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.events = EPOLLIN;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering ev.data.fd = s->signal_fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("epoll_ctl(): %m");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -errno;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return 0;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering}
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poetteringstatic int server_parse_config_file(Server *s) {
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering FILE *f;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering const char *fn;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering int r;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering assert(s);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering fn = "/etc/systemd/systemd-journald.conf";
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering f = fopen(fn, "re");
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering if (!f) {
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering if (errno == ENOENT)
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering return 0;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering log_warning("Failed to open configuration file %s: %m", fn);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering return -errno;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering }
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering r = config_parse(fn, f, "Journal\0", config_item_perf_lookup, (void*) journald_gperf_lookup, false, s);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering if (r < 0)
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering log_warning("Failed to parse configuration file: %s", strerror(-r));
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering fclose(f);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering return r;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering}
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poetteringstatic int server_init(Server *s) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering int n, r, fd;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering assert(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering zero(*s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = -1;
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering s->compress = true;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering s->forward_to_syslog = true;
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
babfc09177c1e71cec6c1ef9602e265ed40cfe4fLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering server_parse_config_file(s);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (!s->user_journals) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Out of memory.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -ENOMEM;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->epoll_fd < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Failed to create epoll object: %m");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -errno;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering n = sd_listen_fds(true);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (n < 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return n;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/native", 0) > 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->native_fd >= 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Too many native sockets passed.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -EINVAL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->native_fd = fd;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/stdout", 0) > 0) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->stdout_fd >= 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Too many stdout sockets passed.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -EINVAL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->stdout_fd = fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->syslog_fd >= 0) {
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_error("Too many /dev/log sockets passed.");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return -EINVAL;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering }
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->syslog_fd = fd;
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering } else {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering log_error("Unknown socket passed.");
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return -EINVAL;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering }
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = open_syslog_socket(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering r = open_native_socket(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = open_stdout_socket(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering r = system_journal_open(s);
ed49ef3f349bcd4f0483ba8254a2537fe8e9cd17Lennart Poettering if (r < 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = open_signalfd(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!s->rate_limit)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering return -ENOMEM;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringstatic void server_done(Server *s) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering JournalFile *f;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering assert(s);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering while (s->stdout_streams)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering stdout_stream_free(s->stdout_streams);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (s->system_journal)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering journal_file_close(s->system_journal);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (s->runtime_journal)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering journal_file_close(s->runtime_journal);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering while ((f = hashmap_steal_first(s->user_journals)))
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering journal_file_close(f);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering hashmap_free(s->user_journals);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (s->epoll_fd >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering close_nointr_nofail(s->epoll_fd);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (s->signal_fd >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering close_nointr_nofail(s->signal_fd);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (s->syslog_fd >= 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering close_nointr_nofail(s->syslog_fd);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (s->native_fd >= 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering close_nointr_nofail(s->native_fd);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (s->stdout_fd >= 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering close_nointr_nofail(s->stdout_fd);
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (s->rate_limit)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering journal_rate_limit_free(s->rate_limit);
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering free(s->buffer);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringint main(int argc, char *argv[]) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering Server server;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering int r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* if (getppid() != 1) { */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* log_error("This program should be invoked by init only."); */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* return EXIT_FAILURE; */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering /* } */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (argc > 1) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("This program does not take arguments.");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return EXIT_FAILURE;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering log_set_target(LOG_TARGET_CONSOLE);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_parse_environment();
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_open();
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering umask(0022);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = server_init(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering goto finish;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering server_vacuum(&server);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering server_flush_to_var(&server);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering sd_notify(false,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering "READY=1\n"
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering "STATUS=Processing requests...");
50f20cfdb0f127e415ab38c024d9ca7a3602f74bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering for (;;) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct epoll_event event;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = epoll_wait(server.epoll_fd, &event, 1, -1);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0) {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (errno == EINTR)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering continue;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_error("epoll_wait() failed: %m");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering goto finish;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } else if (r == 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering r = process_event(&server, &event);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering if (r < 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering goto finish;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering else if (r == 0)
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering break;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poetteringfinish:
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering sd_notify(false,
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering "STATUS=Shutting down...");
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering server_done(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}