journald.c revision 99add6fd0b8f4307ed79256e25f38da318e795c4
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
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering under the terms of the GNU Lesser General Public License as published by
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering the Free Software Foundation; either version 2.1 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
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering Lesser General Public License for more details.
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
5430f7f2bc7330f3088b894166bf3524a067e3d8Lennart Poettering You should have received a copy of the GNU Lesser 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>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <stddef.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <sys/ioctl.h>
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering#include <linux/sockios.h>
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering#include <sys/statvfs.h>
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering#include <sys/mman.h>
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering#include <libudev.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-journal.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-messages.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering#include <systemd/sd-daemon.h>
81527be142678057215665be66e4b3c8306a7ab3Lennart Poettering
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#ifdef HAVE_LOGIND
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#include <systemd/sd-login.h>
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#endif
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering
49e942b2bc9fdedba79cd266a076ce9c9d91fc13Kay Sievers#include "mkdir.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "hashmap.h"
cec736d21ff86c4ac81b4d306ddba2120333818cLennart Poettering#include "journal-file.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering#include "socket-util.h"
69e5d42db09dfb638bc74055c33bb2645f81563dLennart Poettering#include "cgroup-util.h"
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering#include "list.h"
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "virt.h"
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "missing.h"
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "conf-parser.h"
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering#include "journal-internal.h"
0284adc6a60ce0af1107cb0b50041a65d731f39eLennart Poettering#include "journal-vacuum.h"
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering#include "journal-authenticate.h"
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering#include "journald.h"
d2bd7630d7ebbf9a86cca96e39d56efd4558ead4Lennart Poettering#include "journald-rate-limit.h"
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering#include "journald-kmsg.h"
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering#include "journald-syslog.h"
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering#include "journald-stream.h"
3b7124a8db56ed57525b9ecfd19cfdc8c9facba0Lennart Poettering#include "journald-console.h"
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering#include "journald-native.h"
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#ifdef HAVE_ACL
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#include <sys/acl.h>
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#include <acl/libacl.h>
79c077224be5a868d0bba66972ef9546dae85977Kay Sievers#include "acl-util.h"
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#endif
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering#ifdef HAVE_SELINUX
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering#include <selinux/selinux.h>
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering#endif
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering
cab8ac60837b489b27a247990f741315c71cb389Lennart Poettering#define USER_JOURNALS_MAX 1024
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
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poetteringstatic const char* const storage_table[] = {
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering [STORAGE_AUTO] = "auto",
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering [STORAGE_VOLATILE] = "volatile",
205c4d1d6a3f5343f970abf20283d69dcec359edLennart Poettering [STORAGE_PERSISTENT] = "persistent",
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering [STORAGE_NONE] = "none"
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering};
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(storage, Storage);
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart 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
34a35eced40860181757abe5ec00ac0e5d8d0225Lennart Poettering r = readdir_r(d, &buf, &de);
34a35eced40860181757abe5ec00ac0e5d8d0225Lennart Poettering if (r != 0)
34a35eced40860181757abe5ec00ac0e5d8d0225Lennart Poettering break;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (!de)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering break;
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering
c042179388f329fb1e45b00e1a533a0dc27fb9aeLennart Poettering if (!endswith(de->d_name, ".journal") &&
c042179388f329fb1e45b00e1a533a0dc27fb9aeLennart Poettering !endswith(de->d_name, ".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
a3a52c0fd0c2597bf8233730c3ed3408affe0c9fLennart Poettering if (!S_ISREG(st.st_mode))
a3a52c0fd0c2597bf8233730c3ed3408affe0c9fLennart Poettering continue;
a3a52c0fd0c2597bf8233730c3ed3408affe0c9fLennart Poettering
a3a52c0fd0c2597bf8233730c3ed3408affe0c9fLennart Poettering sum += (uint64_t) st.st_blocks * 512UL;
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
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poetteringstatic void server_read_file_gid(Server *s) {
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering const char *adm = "adm";
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering int r;
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering assert(s);
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering if (s->file_gid_valid)
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering return;
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering r = get_group_creds(&adm, &s->file_gid);
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering if (r < 0)
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering log_warning("Failed to resolve 'adm' group: %s", strerror(-r));
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering /* if we couldn't read the gid, then it will be 0, but that's
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering * fine and we shouldn't try to resolve the group again, so
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering * let's just pretend it worked right-away. */
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering s->file_gid_valid = true;
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering}
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poetteringstatic void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering int r;
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#ifdef HAVE_ACL
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_t acl;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_entry_t entry;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering acl_permset_t permset;
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#endif
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering assert(f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering server_read_file_gid(s);
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering r = fchmod_and_fchown(f->fd, 0640, 0, s->file_gid);
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
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#ifdef HAVE_ACL
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);
e6520a0fcebb4d5e07f4c81a51382501b0beb22fLennart Poettering#endif
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 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
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/user-%lu.journal",
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering SD_ID128_FORMAT_VAL(machine), (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
eb53b74f7e2f83d936b35107806a157fdffdbf5aLennart Poettering r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, s->system_journal, &f);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering free(p);
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering if (r < 0)
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering return s->system_journal;
f4b4781191e8edfb5690e4447166e3ba7bcb48f5Lennart Poettering
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering server_fix_perms(s, f, uid);
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) {
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering r = journal_file_rotate(&s->runtime_journal, s->compress, false);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0)
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons if (s->runtime_journal)
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons else
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons log_error("Failed to create new runtime journal: %s", strerror(-r));
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering else
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering server_fix_perms(s, s->runtime_journal, 0);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (s->system_journal) {
eb53b74f7e2f83d936b35107806a157fdffdbf5aLennart Poettering r = journal_file_rotate(&s->system_journal, s->compress, s->seal);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0)
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons if (s->system_journal)
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons else
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons log_error("Failed to create new system journal: %s", strerror(-r));
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering else
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering server_fix_perms(s, s->system_journal, 0);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering }
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
eb53b74f7e2f83d936b35107806a157fdffdbf5aLennart Poettering r = journal_file_rotate(&f, s->compress, s->seal);
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering if (r < 0)
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons if (f->path)
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons log_error("Failed to rotate %s: %s", f->path, strerror(-r));
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons else
499fb2152f8518796607dd0406698cb6ce4c7098Sjoerd Simons log_error("Failed to create user journal: %s", strerror(-r));
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering else {
bc85bfee87e11317fbcd1160c9003860dc6edde9Lennart Poettering hashmap_replace(s->user_journals, k, f);
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering server_fix_perms(s, s->system_journal, PTR_TO_UINT32(k));
5e62067d08d989ab98b12497a9b27a877de8515bLennart Poettering }
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) {
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poettering p = strappend("/var/log/journal/", ids);
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poettering if (!p) {
0d0f0c50d3a1d90f03972a6abb82e6413daaa583Shawn Landden log_oom();
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 if (s->runtime_journal) {
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poettering p = strappend("/run/log/journal/", ids);
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poettering if (!p) {
0d0f0c50d3a1d90f03972a6abb82e6413daaa583Shawn Landden log_oom();
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
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poetteringstatic void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering JournalFile *f;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering bool vacuumed = false;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering int r;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering assert(s);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering assert(iovec);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering assert(n > 0);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering f = find_journal(s, uid);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering if (!f)
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering return;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering if (journal_file_rotate_suggested(f)) {
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering log_info("Journal header limits reached or header out-of-date, rotating.");
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering server_rotate(s);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering server_vacuum(s);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering vacuumed = true;
c2a623d842af3a4d67e1f4e365f6d6a47f1c6cb4Lennart Poettering
c2a623d842af3a4d67e1f4e365f6d6a47f1c6cb4Lennart Poettering f = find_journal(s, uid);
c2a623d842af3a4d67e1f4e365f6d6a47f1c6cb4Lennart Poettering if (!f)
c2a623d842af3a4d67e1f4e365f6d6a47f1c6cb4Lennart Poettering return;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering }
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering for (;;) {
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering if (r >= 0)
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering return;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering if (vacuumed ||
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering (r != -E2BIG && /* hit limit */
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r != -EFBIG && /* hit fs limit */
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r != -EDQUOT && /* quota hit */
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r != -ENOSPC && /* disk full */
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r != -EBADMSG && /* corrupted */
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r != -ENODATA && /* truncated */
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering r != -EHOSTDOWN && /* other machine */
71fa6f006f92831c0c02e844b4c35e4b7197c6d6Lennart Poettering r != -EPROTONOSUPPORT && /* unsupported feature */
71fa6f006f92831c0c02e844b4c35e4b7197c6d6Lennart Poettering r != -EBUSY && /* unclean shutdown */
71fa6f006f92831c0c02e844b4c35e4b7197c6d6Lennart Poettering r != -ESHUTDOWN /* already archived */)) {
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering log_error("Failed to write entry, ignoring: %s", strerror(-r));
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering return;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering }
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering if (r == -E2BIG || r == -EFBIG || r == EDQUOT || r == ENOSPC)
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering log_info("Allocation limit reached, rotating.");
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering else if (r == -EHOSTDOWN)
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering log_info("Journal file from other machine, rotating.");
71fa6f006f92831c0c02e844b4c35e4b7197c6d6Lennart Poettering else if (r == -EBUSY)
71fa6f006f92831c0c02e844b4c35e4b7197c6d6Lennart Poettering log_info("Unlcean shutdown, rotating.");
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering else
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering log_warning("Journal file corrupted, rotating.");
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering server_rotate(s);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering server_vacuum(s);
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering vacuumed = true;
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
32fca2d5b746b86bbbbd4f1b57064076cfc2ae4bLennart Poettering f = find_journal(s, uid);
32fca2d5b746b86bbbbd4f1b57064076cfc2ae4bLennart Poettering if (!f)
32fca2d5b746b86bbbbd4f1b57064076cfc2ae4bLennart Poettering return;
32fca2d5b746b86bbbbd4f1b57064076cfc2ae4bLennart Poettering
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering log_info("Retrying write.");
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering }
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering}
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poetteringstatic void dispatch_message_real(
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering Server *s,
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering struct iovec *iovec, unsigned n, unsigned m,
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering struct ucred *ucred,
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering struct timeval *tv,
8b38f3cc3eb73adf9536cb73d0f319e60d42ea0cLennart Poettering const char *label, size_t label_len,
8b38f3cc3eb73adf9536cb73d0f319e60d42ea0cLennart Poettering const char *unit_id) {
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,
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering *owner_uid = NULL, *unit = NULL, *selinux_context = 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;
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;
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#ifdef HAVE_LOGIND
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering uid_t owner;
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#endif
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
5c3759bf8a3d418fa877e6a278f3150f404745b2Lennart Poettering if (exe)
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
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#ifdef HAVE_LOGIND
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
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering if (sd_pid_get_owner_uid(ucred->uid, &owner) >= 0)
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering if (asprintf(&owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner) >= 0)
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering IOVEC_SET_STRING(iovec[n++], owner_uid);
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering#endif
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering
ba1261bc02693ac8a7712ade14aab8e22989ba88Lennart Poettering if (cg_pid_get_unit(ucred->pid, &t) >= 0) {
94fb446e55babb713fb24850455627acf30d999bLennart Poettering unit = strappend("_SYSTEMD_UNIT=", t);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering free(t);
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra } else if (unit_id)
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra unit = strappend("_SYSTEMD_UNIT=", unit_id);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra if (unit)
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra IOVEC_SET_STRING(iovec[n++], unit);
85d83bf41c8bdf55c10b7f845d81e5787ef6c295Lennart Poettering
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering#ifdef HAVE_SELINUX
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering if (label) {
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering selinux_context = malloc(sizeof("_SELINUX_CONTEXT=") + label_len);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering if (selinux_context) {
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering memcpy(selinux_context, "_SELINUX_CONTEXT=", sizeof("_SELINUX_CONTEXT=")-1);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering memcpy(selinux_context+sizeof("_SELINUX_CONTEXT=")-1, label, label_len);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering selinux_context[sizeof("_SELINUX_CONTEXT=")-1+label_len] = 0;
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering IOVEC_SET_STRING(iovec[n++], selinux_context);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering }
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering } else {
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering security_context_t con;
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering if (getpidcon(ucred->pid, &con) >= 0) {
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering selinux_context = strappend("_SELINUX_CONTEXT=", con);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering if (selinux_context)
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering IOVEC_SET_STRING(iovec[n++], selinux_context);
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering freecon(con);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering }
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering }
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering#endif
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
dca6219e04505e9fa10b32e71059ce2abfae1dadLennart Poettering write_to_journal(s, realuid == 0 ? 0 : loginuid, iovec, n);
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);
8a0f04e6a283cc6734ee09a20305c13e09ba0418Lennart Poettering free(selinux_context);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering}
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringvoid server_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];
33eb8abfa51d5bffbf8819215fafdd1123a4a8c0Lennart Poettering struct iovec iovec[N_IOVEC_META_FIELDS + 4];
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
c51742d02981196f992aef5f3d72799f22ee89e0Lennart Poettering IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
33eb8abfa51d5bffbf8819215fafdd1123a4a8c0Lennart Poettering IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
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
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poetteringvoid server_dispatch_message(
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering Server *s,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering struct iovec *iovec, unsigned n, unsigned m,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering struct ucred *ucred,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering struct timeval *tv,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering const char *label, size_t label_len,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering const char *unit_id,
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering int priority) {
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering
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
213ba152fdf7978773be5b8a72e040584765137fLennart Poettering if (LOG_PRI(priority) > s->max_level_store)
213ba152fdf7978773be5b8a72e040584765137fLennart Poettering return;
213ba152fdf7978773be5b8a72e040584765137fLennart 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)
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering server_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:
62bca2c657bf95fd1f69935eef09915afa5c69d9Eelco Dolstra dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id);
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering}
224f2ee221e77c326d1d7761abb6e812432b2163Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart 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
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering if (!s->system_journal &&
b8156be03f71c53072c7126bb418410acbef6c81Lennart Poettering (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
b8156be03f71c53072c7126bb418410acbef6c81Lennart Poettering access("/run/systemd/journal/flushed", F_OK) >= 0) {
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering /* If in auto mode: first try to create the machine
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering * path, but not the prefix.
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering *
205c4d1d6a3f5343f970abf20283d69dcec359edLennart Poettering * If in persistent mode: create /var/log/journal and
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering * the machine path */
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering
b8156be03f71c53072c7126bb418410acbef6c81Lennart Poettering if (s->storage == STORAGE_PERSISTENT)
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering (void) mkdir("/var/log/journal/", 0755);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering fn = strappend("/var/log/journal/", ids);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!fn)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return -ENOMEM;
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering (void) mkdir(fn, 0755);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering free(fn);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
b7def684941808600c344f0be7a2b9fcdda97e0fLennart Poettering fn = strjoin("/var/log/journal/", ids, "/system.journal", NULL);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (!fn)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return -ENOMEM;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
eb53b74f7e2f83d936b35107806a157fdffdbf5aLennart Poettering r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering free(fn);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering if (r >= 0)
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering server_fix_perms(s, s->system_journal, 0);
7560fffcd2531786b9c1ca657667a43e90331326Lennart 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
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering if (!s->runtime_journal &&
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering (s->storage != STORAGE_NONE)) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
b7def684941808600c344f0be7a2b9fcdda97e0fLennart Poettering fn = strjoin("/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
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, 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
b8156be03f71c53072c7126bb418410acbef6c81Lennart Poettering (void) mkdir_parents(fn, 0755);
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, 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
7560fffcd2531786b9c1ca657667a43e90331326Lennart Poettering if (s->runtime_journal)
5e41cfec83aa47af12e469bc62e336f8213ee066Lennart Poettering server_fix_perms(s, s->runtime_journal, 0);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering}
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poetteringstatic int server_flush_to_var(Server *s) {
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering Object *o = NULL;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering int r;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_id128_t machine;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering sd_journal *j;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering assert(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering if (s->storage != STORAGE_AUTO &&
205c4d1d6a3f5343f970abf20283d69dcec359edLennart Poettering s->storage != STORAGE_PERSISTENT)
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering return 0;
e156d769c3ee756cdb20f8522ace9ea459a82655Lennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering if (!s->runtime_journal)
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering return 0;
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering system_journal_open(s);
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
54a7b863dd3937893abae47b20b6f655b8e9252aLennart Poettering if (!s->system_journal)
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering return 0;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering log_info("Flushing to /var...");
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart 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
4a299a7ad547becb70e8271eaf8cdc27e8e6836aLennart Poettering if (r >= 0)
4a299a7ad547becb70e8271eaf8cdc27e8e6836aLennart Poettering rm_rf("/run/log/journal", false, true, false);
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);
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering assert(ev);
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)
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering return 1;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return -errno;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
3146a302d184d64a5c75237129f9e4f54ce46cd3Lennart Poettering log_info("Received SIG%s", signal_to_string(sfsi.ssi_signo));
3146a302d184d64a5c75237129f9e4f54ce46cd3Lennart Poettering
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering if (sfsi.ssi_signo == SIGUSR1) {
b8156be03f71c53072c7126bb418410acbef6c81Lennart Poettering touch("/run/systemd/journal/flushed");
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering server_flush_to_var(s);
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering return 1;
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering }
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering if (sfsi.ssi_signo == SIGUSR2) {
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering server_rotate(s);
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering server_vacuum(s);
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering return 1;
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering }
cf244689e9d1ab50082c9ddd0f3c4d1eb982badcLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering } else if (ev->data.fd == s->dev_kmsg_fd) {
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering int r;
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering if (ev->events != EPOLLIN) {
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering log_info("Got invalid event from epoll.");
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering return -EIO;
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering }
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering r = server_read_dev_kmsg(s);
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering if (r < 0)
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering return r;
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering return 1;
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart 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;
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering char *label = NULL;
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering size_t label_len = 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering union {
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering struct cmsghdr cmsghdr;
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering /* We use NAME_MAX space for the
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * SELinux label here. The kernel
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * currently enforces no limit, but
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * according to suggestions from the
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * SELinux people this will change and
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * it will probably be identical to
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * NAME_MAX. For now we use that, but
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * this should be updated one day when
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering * the final limit is known.*/
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering CMSG_SPACE(sizeof(struct timeval)) +
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering CMSG_SPACE(sizeof(int)) + /* fd */
7264278fbbdc1dc6c30fedc902d1337594aa6ff6Lennart Poettering CMSG_SPACE(NAME_MAX)]; /* selinux label */
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering } control;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering ssize_t n;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering int v;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering int *fds = NULL;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering unsigned n_fds = 0;
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 (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
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
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 &&
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering cmsg->cmsg_type == SCM_SECURITY) {
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering label = (char*) CMSG_DATA(cmsg);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart Poettering label_len = cmsg->cmsg_len - CMSG_LEN(0);
7f2c63cbf47c89ec56f50469f6551df473dd65d8Lennart 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);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering else if (cmsg->cmsg_level == SOL_SOCKET &&
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering cmsg->cmsg_type == SCM_RIGHTS) {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering fds = (int*) CMSG_DATA(cmsg);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (ev->data.fd == s->syslog_fd) {
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering char *e;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if (n > 0 && n_fds == 0) {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering e = memchr(s->buffer, '\n', n);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if (e)
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering *e = 0;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering else
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering s->buffer[n] = 0;
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering server_process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering } else if (n_fds > 0)
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering log_warning("Got file descriptors via syslog socket. Ignoring.");
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering } else {
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering if (n > 0 && n_fds == 0)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering server_process_native_message(s, s->buffer, n, ucred, tv, label, label_len);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering else if (n == 0 && n_fds == 1)
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering server_process_native_file(s, fds[0], ucred, tv, label, label_len);
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering else if (n_fds > 0)
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering log_warning("Got too many file descriptors via native socket. Ignoring.");
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
0dad12c190b7493955cd60d2a1625199b1709f69Lennart Poettering close_many(fds, n_fds);
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
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);
ba6b3039531a70466dd955c6faa9bf521b3dfbacLennart Poettering sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -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
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poetteringstatic int server_parse_proc_cmdline(Server *s) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering char *line, *w, *state;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering int r;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering size_t l;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering if (detect_container(NULL) > 0)
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering return 0;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering r = read_one_line_file("/proc/cmdline", &line);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering if (r < 0) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering return 0;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering }
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering FOREACH_WORD_QUOTED(w, l, line, state) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering char *word;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering word = strndup(w, l);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering if (!word) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering r = -ENOMEM;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering goto finish;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering }
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering if (startswith(word, "systemd.journald.forward_to_syslog=")) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering r = parse_boolean(word + 35);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering if (r < 0)
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering else
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering s->forward_to_syslog = r;
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering r = parse_boolean(word + 33);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering if (r < 0)
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering else
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering s->forward_to_kmsg = r;
c66e7bc7a19c068ca1c414f2f8bd5dc13c20907fLennart Poettering } else if (startswith(word, "systemd.journald.forward_to_console=")) {
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering r = parse_boolean(word + 36);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering if (r < 0)
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering else
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering s->forward_to_console = r;
66a78c2b95ba6cc0be15dab68c5af816fb5b7a33Lennart Poettering } else if (startswith(word, "systemd.journald"))
66a78c2b95ba6cc0be15dab68c5af816fb5b7a33Lennart Poettering log_warning("Invalid systemd.journald parameter. Ignoring.");
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering free(word);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering }
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering r = 0;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poetteringfinish:
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering free(line);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering return r;
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering}
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart 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
18b754d345ecb0b15e369978aaffa72e9814b86aKay Sievers fn = "/etc/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);
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = s->dev_kmsg_fd = -1;
807e17f05e217b474af39503efb9503d81b12596Lennart Poettering s->compress = true;
eb53b74f7e2f83d936b35107806a157fdffdbf5aLennart Poettering s->seal = 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
213ba152fdf7978773be5b8a72e040584765137fLennart Poettering s->max_level_store = LOG_DEBUG;
213ba152fdf7978773be5b8a72e040584765137fLennart Poettering s->max_level_syslog = LOG_DEBUG;
213ba152fdf7978773be5b8a72e040584765137fLennart Poettering s->max_level_kmsg = LOG_NOTICE;
213ba152fdf7978773be5b8a72e040584765137fLennart Poettering s->max_level_console = LOG_INFO;
213ba152fdf7978773be5b8a72e040584765137fLennart 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);
effb1102d3f1d6bc2d0bda5328d3ce2139af9850Lennart Poettering server_parse_proc_cmdline(s);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering mkdir_p("/run/systemd/journal", 0755);
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
0d0f0c50d3a1d90f03972a6abb82e6413daaa583Shawn Landden if (!s->user_journals)
0d0f0c50d3a1d90f03972a6abb82e6413daaa583Shawn Landden return log_oom();
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering
84168d8068bb67dcd5468ab3b376535d81643aefLennart Poettering s->mmap = mmap_cache_new();
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering if (!s->mmap)
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering return log_oom();
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart 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
259d2e762041d8d50c2a17bfea90b1a96f6b880bLennart Poettering if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 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
259d2e762041d8d50c2a17bfea90b1a96f6b880bLennart Poettering } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/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
35e2e347d38cc2f8bd7c38a0d8a5129f5fbb0ab9Lennart Poettering r = server_open_syslog_socket(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
0153028ae379eb7c9a463c548ef73ea392c6cdb0Lennart Poettering r = server_open_native_socket(s);
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering if (r < 0)
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering return r;
7f3e62571a63ac90de6ac5eefeeb8d3e9aa6f49eLennart Poettering
a45b9fca6b91a767dcd9060cfcb30617dad234c7Lennart Poettering r = server_open_stdout_socket(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = server_open_dev_kmsg(s);
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering if (r < 0)
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering return r;
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering r = server_open_kernel_seqnum(s);
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering if (r < 0)
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering return r;
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering r = open_signalfd(s);
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering if (r < 0)
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering return r;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering s->udev = udev_new();
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering if (!s->udev)
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering return -ENOMEM;
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart 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
9447a7f1de5e5798cf324ef26579d0dd7e601203Lennart Poettering r = system_journal_open(s);
9447a7f1de5e5798cf324ef26579d0dd7e601203Lennart Poettering if (r < 0)
9447a7f1de5e5798cf324ef26579d0dd7e601203Lennart Poettering return r;
9447a7f1de5e5798cf324ef26579d0dd7e601203Lennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering return 0;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering}
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poetteringstatic void server_maybe_append_tags(Server *s) {
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering#ifdef HAVE_GCRYPT
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering JournalFile *f;
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering Iterator i;
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering usec_t n;
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering n = now(CLOCK_REALTIME);
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering if (s->system_journal)
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering journal_file_maybe_append_tag(s->system_journal, n);
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering HASHMAP_FOREACH(f, s->user_journals, i)
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering journal_file_maybe_append_tag(f, n);
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering#endif
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering}
671e021c92c835c6c701dc61463149d05b6f31afLennart 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
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering if (s->dev_kmsg_fd >= 0)
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering close_nointr_nofail(s->dev_kmsg_fd);
6c1e6b98bde8288d65d7cddf87ff9bedf94a3e1cLennart Poettering
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering if (s->rate_limit)
6e409ce10d134625626d1eddfd6152755ef1908dLennart Poettering journal_rate_limit_free(s->rate_limit);
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering if (s->kernel_seqnum)
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering munmap(s->kernel_seqnum, sizeof(uint64_t));
cee5e9a7ca75f05d4a0f7759566e657587a500d5Lennart Poettering
783d2675eff73d1937bf8f78b368b1004c2d28c5Lennart Poettering free(s->buffer);
0d9243f022d244632b1ab26cfc8b46794b7fc5d6Lennart Poettering free(s->tty_path);
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering if (s->mmap)
16e9f408fa9a9626059bdd6c89dc175e06b9e976Lennart Poettering mmap_cache_unref(s->mmap);
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering if (s->udev)
bdfb9e7f7c315af5a6755ac4701b696ce2305a19Lennart Poettering udev_unref(s->udev);
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
a6903061530cac5fbaa99a080a93221c02c349f9Lennart Poettering log_set_target(LOG_TARGET_SAFE);
3eff4208ffecedd778fec260f0d4b18e94dab443Lennart Poettering log_set_facility(LOG_SYSLOG);
f65425cbc4385b4f5b20ded641cbb07ad6a0938fLennart Poettering log_set_max_level(LOG_DEBUG);
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);
51abe64c859c01ffad4f80840396ad9f6d5c9e62Lennart Poettering server_flush_dev_kmsg(&server);
e6960940b6dc5ab81eb2fca4061c333e1795f38dLennart Poettering
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering server_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;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering int t;
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering#ifdef HAVE_GCRYPT
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering usec_t u;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering if (server.system_journal &&
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering journal_file_next_evolve_usec(server.system_journal, &u)) {
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering usec_t n;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
671e021c92c835c6c701dc61463149d05b6f31afLennart Poettering n = now(CLOCK_REALTIME);
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering if (n >= u)
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering t = 0;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering else
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering t = (int) ((u - n + USEC_PER_MSEC - 1) / USEC_PER_MSEC);
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering } else
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering#endif
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering t = -1;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering r = epoll_wait(server.epoll_fd, &event, 1, t);
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;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering if (r > 0) {
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering r = process_event(&server, &event);
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering if (r < 0)
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering goto finish;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering else if (r == 0)
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering break;
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering }
89fef99014662a5a63e7deaedd6292b7fb4ab2f8Lennart Poettering
99add6fd0b8f4307ed79256e25f38da318e795c4Lennart Poettering server_maybe_append_tags(&server);
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering }
87d2c1ff6a7375f03476767e6f59454bcc5cd04bLennart Poettering
fe6521272ba203ec8f0d5a94f0729960b3f90525Lennart Poettering log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
ef63833d532dd86bdba63211e6a1363cbb3ef61dLennart Poettering server_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}